Exemplo n.º 1
0
        /// <summary>
        /// 从下位机获取波形或频谱数据,波形数据直接存储,频谱数据返回供提取转速
        /// </summary>
        /// <param name="meterageSamplerate">获取波形或频谱参数</param>
        /// <returns></returns>
        private DataTwModel GetTwFFTData(MeterageSamplerate ms)
        {
            //是否停机状态
            string key = $"A{ms.AreaId}M{ms.McId}";

            if (_stopMachineOa.GetValueOrDefault(key))
            {
                return(null);
            }

            byte[]        recData         = new byte[20];
            int           line            = ms.MsrLine;
            int           frequency       = ms.MsrRateMax;
            int           type            = ms.MsrParameter.ToLower().Contains("env") ? 2 : 0;
            char          typeName        = ms.MsrName.ToUpper()[0];                                                                                                                                    //类型的首字母  如:Disp 'D'
            ChannelStruct channelStruct   = _site.ChannelStructList.FirstOrDefault(cs => cs.AreaID == ms.AreaId && cs.MachineID == ms.McId && cs.MonitorID == ms.ParId && cs.Position_HVA == ms.DirId); //获取通道号
            List <int>    workStatuesList = channelStruct.StateStatus.Where(w => w != -1).ToList();

            byte[] cmd = OrderHelper.TwDataByChannel(channelStruct.ChannelID, typeName, frequency, line, 0); //获取每个通道取数据命令
            if (_socket.Send(cmd))
            {
                if (!_socket.Receive(recData))    //接收失败
                {
                    return(null);
                }

                int len = BitConverter.ToInt32(recData, 15); //获取要接收的数据长度
                if (len <= 0)                                //是否有数据
                {
                    return(null);
                }

                //获取波形数据
                cmd = OrderHelper.TwDataByChannel(channelStruct.ChannelID, typeName, frequency, line, 1);
                if (!_socket.Send(cmd))
                {
                    return(null);
                }

                byte[] data = new byte[len];
                if (!_socket.Receive(data)) //接收波形数据,最后4个字节是工况
                {
                    Console.WriteLine(string.Format("{0}站点{1}通道号接收波形异常!", _site.Sn, channelStruct.ChannelID));
                    return(null);
                }

                DataTw tw = new DataTw()  //数据转化波形对象
                {
                    AreaId         = ms.AreaId,
                    McId           = ms.McId,
                    ParId          = ms.ParId,
                    DirId          = ms.DirId,
                    DataLines      = GetLineByType(ms.MsrLine, type),
                    DataPoints     = (int)(GetLineByType(ms.MsrLine, type) * 2.56),
                    DataHz         = (int)(GetFrequencyByType(ms.MsrRateMax, type) * 2.56),
                    DataIsFFT      = 0,
                    DataType       = type + 1,
                    DataFormat     = 2,
                    Data           = new byte[len - 7 * 4],
                    DataWorkStatus = Convert.ToInt32(BitConverter.ToSingle(data, 24)),
                    Time           = ConvertTime(data)
                };

                //是否是要取的工况
                if (!workStatuesList.Exists(w => w == tw.DataWorkStatus))
                {
                    return(null);
                }
                Array.Copy(data, 7 * 4, tw.Data, 0, len - 7 * 4); //把波形数据copy到 tw.data中

                if (ms.MsrParameter.ToLower().Contains("env"))    //包络,直接存
                {
                    tw.DataIsFFT = 1;
                    tw.Data      = SplitMinFFT(tw.Data, ms);                                      //截取最小Fmin
                    _dataTwService.InsertDataTw(tw, string.Format("tb_data_tw_{0}", ms.MsrName)); //存入数据库
                }
                else if (ms.MsrParameter.ToLower().Contains("acc"))
                {
                    //存储波形
                    _dataTwService.InsertDataTw(tw, string.Format("tb_data_tw_{0}", ms.MsrName));

                    //频谱存储
                    float[] accSpectrum = GetAccSpectrum(ToFloatArray(tw.Data), tw.DataLines);
                    for (int z = 0; z < accSpectrum.Length; z++)
                    {
                        accSpectrum[z] = accSpectrum[z] / 1.414f;//峰值转有效值
                    }
                    tw.Data      = ToByteArray(accSpectrum);
                    tw.DataIsFFT = 1;
                    tw.Data      = SplitMinFFT(tw.Data, ms); //截取最小Fmin
                    _dataTwService.InsertDataTw(tw, string.Format("tb_data_tw_{0}", ms.MsrName));
                }
                else if (ms.MsrParameter.ToLower().Contains("disp"))
                {
                    float[] accSpectrum = GetAccSpectrum(ToFloatArray(tw.Data) /*原始波形*/, tw.DataLines);

                    //位移频谱
                    float[] dispSpectrum = GetDispSpectrum(accSpectrum, tw.DataHz, tw.DataPoints);
                    for (int k = 0; k < dispSpectrum.Length; k++)
                    {
                        //位移有效值
                        dispSpectrum[k] = dispSpectrum[k] * 9800 * 1000 / 1.414f;
                    }

                    //位移波形
                    float[] dispTw = new float[tw.DataPoints];
                    FFTHelper.IntegrateFromSpectrum(ToFloatArray(tw.Data) /*原始波形*/, tw.DataHz, tw.DataPoints, ms.MsrRateMin, GetFrequencyByType(ms.MsrRateMax, type), 2, ref dispTw);
                    for (int k = 0; k < dispTw.Length; k++)
                    {
                        dispTw[k] = dispTw[k] * 9800 * 1000;
                    }

                    //存储波形
                    //tw.data = ToByteArray(dispTw);
                    _dataTwService.InsertDataTw(tw, string.Format("tb_data_tw_{0}", ms.MsrName));

                    //频谱存储
                    tw.DataIsFFT = 1;
                    tw.Data      = ToByteArray(dispSpectrum);
                    tw.Data      = SplitMinFFT(tw.Data, ms); //截取最小Fmin
                    _dataTwService.InsertDataTw(tw, string.Format("tb_data_tw_{0}", ms.MsrName));
                }
                else if (ms.MsrParameter.ToLower().Contains("vel"))
                {
                    float[] accSpectrum = GetAccSpectrum(ToFloatArray(tw.Data) /*原始波形*/, tw.DataLines);

                    //速度波形
                    float[] velTw = new float[tw.DataPoints];
                    FFTHelper.IntegrateFromSpectrum(ToFloatArray(tw.Data) /*原始波形*/, tw.DataHz, tw.DataPoints, ms.MsrRateMin, GetFrequencyByType(ms.MsrRateMax, type), 1, ref velTw);
                    for (int k = 0; k < velTw.Length; k++)
                    {
                        velTw[k] = velTw[k] * 9800;
                    }

                    //速度频谱
                    float[] velSpectrum = GetVelSpectrum(accSpectrum, tw.DataHz, tw.DataPoints);
                    for (int k = 0; k < velSpectrum.Length; k++)
                    {
                        velSpectrum[k] = velSpectrum[k] * 9800 / 1.414f;
                    }

                    //波形存储
                    //tw.data = ToByteArray(velTw);
                    _dataTwService.InsertDataTw(tw, string.Format("tb_data_tw_{0}", ms.MsrName));

                    //频谱存储
                    tw.DataIsFFT = 1;
                    tw.Data      = ToByteArray(velSpectrum); //获取频谱
                    tw.Data      = SplitMinFFT(tw.Data, ms); //截取最小Fmin
                    _dataTwService.InsertDataTw(tw, string.Format("tb_data_tw_{0}", ms.MsrName));
                }
                return(_mapper.Map <DataTwModel>(tw));
            }

            return(null);
        }
Exemplo n.º 2
0
        public void GetDataTw(SiteModel site, SocketMiddleware socket)
        {
            Dictionary <string, bool> stopDic = RedisHelper.Get <Dictionary <string, bool> >($"{CallContext.GetData<ClientInfo>("clientInfo").Database}IsStop");

            Console.WriteLine("开始采集波形数据!");
            //存储集合
            List <int> workStatuesList     = null;
            List <MeterageSamplerate> list = null;

            int type, frequency, line;

            byte[] cmd     = null;
            byte[] recData = new byte[20];
            for (int i = 0; i < site.ChannelStructList.Count; i++)
            {
                //是否停机状态
                string key = $"A{site.ChannelStructList[i].AreaID}M{site.ChannelStructList[i].MachineID}";
                if (stopDic.GetValueOrDefault(key))
                {
                    continue;
                }

                list = _meterageSamplerateService.Query(s =>
                                                        s.AreaId == site.ChannelStructList[i].AreaID &&
                                                        s.McId == site.ChannelStructList[i].MachineID &&
                                                        s.ParId == site.ChannelStructList[i].MonitorID &&
                                                        s.DirId == site.ChannelStructList[i].Position_HVA &&
                                                        s.IsSamplerate == 1);

                workStatuesList = site.ChannelStructList[i].StateStatus.Where(w => w != -1).ToList();
                foreach (MeterageSamplerate item in list)
                {
                    line      = item.MsrLine;
                    frequency = item.MsrRateMax;
                    type      = item.MsrParameter.ToLower().Contains("env") ? 2 : 0;
                    char typeName = item.MsrName.ToUpper()[0];                                                            //类型的首字母  如:Disp 'D'
                    cmd = OrderHelper.TwDataByChannel(site.ChannelStructList[i].ChannelID, typeName, frequency, line, 0); //获取每个通道取数据命令
                    if (socket.Send(cmd))
                    {
                        if (!socket.Receive(recData))    //接收失败
                        {
                            continue;
                        }

                        int len = BitConverter.ToInt32(recData, 15); //获取要接收的数据长度
                        if (len <= 0)                                //是否有数据
                        {
                            continue;
                        }

                        //获取波形数据
                        cmd = OrderHelper.TwDataByChannel(site.ChannelStructList[i].ChannelID, typeName, frequency, line, 1);
                        if (!socket.Send(cmd))
                        {
                            continue;
                        }

                        byte[] data = new byte[len];
                        if (!socket.Receive(data)) //接收波形数据,最后4个字节是工况
                        {
                            Console.WriteLine(string.Format("{0}站点{1}通道号接收波形异常!", site.Sn, i));
                            continue;
                        }

                        DataTw tw = new DataTw()  //数据转化波形对象
                        {
                            AreaId         = item.AreaId,
                            McId           = item.McId,
                            ParId          = item.ParId,
                            DirId          = item.DirId,
                            DataLines      = GetLineByType(item.MsrLine, type),
                            DataPoints     = (int)(GetLineByType(item.MsrLine, type) * 2.56),
                            DataHz         = (int)(GetFrequencyByType(item.MsrRateMax, type) * 2.56),
                            DataIsFFT      = 0,
                            DataType       = type + 1,
                            DataFormat     = 2,
                            Data           = new byte[len - 7 * 4],
                            DataWorkStatus = Convert.ToInt32(BitConverter.ToSingle(data, 24)),
                            Time           = ConvertTime(data)
                        };

                        //是否是要取的工况
                        if (!workStatuesList.Exists(w => w == tw.DataWorkStatus))
                        {
                            continue;
                        }

                        Array.Copy(data, 7 * 4, tw.Data, 0, len - 7 * 4); //把波形数据copy到 tw.data中

                        if (item.MsrParameter.ToLower().Contains("env"))  //包络,直接存
                        {
                            tw.DataIsFFT = 1;
                            tw.Data      = SplitMinFFT(tw.Data, item);                                      //截取最小Fmin
                            _dataTwService.InsertDataTw(tw, string.Format("tb_data_tw_{0}", item.MsrName)); //存入数据库
                        }
                        else if (item.MsrParameter.ToLower().Contains("acc"))
                        {
                            //存储波形
                            _dataTwService.InsertDataTw(tw, string.Format("tb_data_tw_{0}", item.MsrName));

                            //频谱存储
                            float[] accSpectrum = GetAccSpectrum(ToFloatArray(tw.Data), tw.DataLines);
                            for (int z = 0; z < accSpectrum.Length; z++)
                            {
                                accSpectrum[z] = accSpectrum[z] / 1.414f;//峰值转有效值
                            }
                            tw.Data      = ToByteArray(accSpectrum);
                            tw.DataIsFFT = 1;
                            tw.Data      = SplitMinFFT(tw.Data, item); //截取最小Fmin
                            _dataTwService.InsertDataTw(tw, string.Format("tb_data_tw_{0}", item.MsrName));
                        }
                        else if (item.MsrParameter.ToLower().Contains("disp"))
                        {
                            float[] accSpectrum = GetAccSpectrum(ToFloatArray(tw.Data) /*原始波形*/, tw.DataLines);

                            //位移频谱
                            float[] dispSpectrum = GetDispSpectrum(accSpectrum, tw.DataHz, tw.DataPoints);
                            for (int k = 0; k < dispSpectrum.Length; k++)
                            {
                                //位移有效值
                                dispSpectrum[k] = dispSpectrum[k] * 9800 * 1000 / 1.414f;
                            }

                            //位移波形
                            float[] dispTw = new float[tw.DataPoints];
                            FFTHelper.IntegrateFromSpectrum(ToFloatArray(tw.Data) /*原始波形*/, tw.DataHz, tw.DataPoints, item.MsrRateMin, GetFrequencyByType(item.MsrRateMax, type), 2, ref dispTw);
                            for (int k = 0; k < dispTw.Length; k++)
                            {
                                dispTw[k] = dispTw[k] * 9800 * 1000;
                            }

                            //存储波形
                            //tw.data = ToByteArray(dispTw);
                            _dataTwService.InsertDataTw(tw, string.Format("tb_data_tw_{0}", item.MsrName));

                            //频谱存储
                            tw.DataIsFFT = 1;
                            tw.Data      = ToByteArray(dispSpectrum);
                            tw.Data      = SplitMinFFT(tw.Data, item); //截取最小Fmin
                            _dataTwService.InsertDataTw(tw, string.Format("tb_data_tw_{0}", item.MsrName));
                        }
                        else if (item.MsrParameter.ToLower().Contains("vel"))
                        {
                            float[] accSpectrum = GetAccSpectrum(ToFloatArray(tw.Data) /*原始波形*/, tw.DataLines);

                            //速度波形
                            float[] velTw = new float[tw.DataPoints];
                            FFTHelper.IntegrateFromSpectrum(ToFloatArray(tw.Data) /*原始波形*/, tw.DataHz, tw.DataPoints, item.MsrRateMin, GetFrequencyByType(item.MsrRateMax, type), 1, ref velTw);
                            for (int k = 0; k < velTw.Length; k++)
                            {
                                velTw[k] = velTw[k] * 9800;
                            }

                            //速度频谱
                            float[] velSpectrum = GetVelSpectrum(accSpectrum, tw.DataHz, tw.DataPoints);
                            for (int k = 0; k < velSpectrum.Length; k++)
                            {
                                velSpectrum[k] = velSpectrum[k] * 9800 / 1.414f;
                            }

                            //波形存储
                            //tw.data = ToByteArray(velTw);
                            _dataTwService.InsertDataTw(tw, string.Format("tb_data_tw_{0}", item.MsrName));

                            //频谱存储
                            tw.DataIsFFT = 1;
                            tw.Data      = ToByteArray(velSpectrum);   //获取频谱
                            tw.Data      = SplitMinFFT(tw.Data, item); //截取最小Fmin
                            _dataTwService.InsertDataTw(tw, string.Format("tb_data_tw_{0}", item.MsrName));
                        }
                    }
                }
            }

            Console.WriteLine("波形数据采集完成!");
        }
Exemplo n.º 3
0
        /// <summary>
        /// 执行业务逻辑
        /// </summary>
        public void ExcuteAnalyse()
        {
            try
            {
                IHandshakeModule handshakeModule = Program.container.Resolve <IHandshakeModule>();
                SiteModel        site            = handshakeModule.Handshake(_socket);
                if (site == null)
                {
                    Console.WriteLine("握手失败!");
                    return;
                }

                ISiteModule siteModule = Program.container.Resolve <ISiteModule>();
                siteModule.GetSiteDetails(ref site);

                if (site.HasOaData)
                {
                    IDataOaModule dataOaModule = Program.container.Resolve <IDataOaModule>();
                    dataOaModule.GetDataOa(site, _socket);
                }

                if (site.HasTwData)
                {
                    //IDataTwModule dataTwModule = Program.container.Resolve<IDataTwModule>();
                    //dataTwModule.GetDataTw(site, _socket);

                    IExtractModule extractModule = Program.container.Resolve <IExtractModule>();
                    extractModule.InitConfig(site, _socket);
                }

                #region 配置更新
                if (site.IsUpdate > 0)
                {
                    ISiteService  siteService  = Program.container.Resolve <ISiteService>();
                    IConfigModule configModule = Program.container.Resolve <IConfigModule>();
                    configModule._site   = site;
                    configModule._sokect = _socket;
                    //更新配置
                    switch (site.IsUpdate)
                    {
                    case 1:     //更新时间
                        configModule.UpdateTime();
                        break;

                    case 2:     //更新配置
                        configModule.UpdateSiteConfig();
                        break;

                    case 3:     //更新 时间 配置
                        configModule.UpdateTime();
                        configModule.UpdateSiteConfig();
                        break;

                    case 4:     //更新 固件
                        configModule.UpdateSolid();
                        break;

                    case 5:     // 更新 时间 固件
                        configModule.UpdateTime();
                        configModule.UpdateSolid();
                        break;

                    case 6:     //更新 配置 固件
                        configModule.UpdateSiteConfig();
                        configModule.UpdateSolid();
                        break;

                    case 7:     //更新 时间 配置 固件
                        configModule.UpdateTime();
                        configModule.UpdateSiteConfig();
                        configModule.UpdateSolid();
                        break;

                    default:
                        break;
                    }
                    Site mySite = siteService.Query(s => s.SiteSN == site.Sn).FirstOrDefault();
                    mySite.SiteUpdateTag = 0;
                    siteService.UpdateEntity(mySite);
                }
                #endregion

                //断开连接
                if (_socket.Send(OrderHelper.SiteOff(site.Sn)))
                {
                    byte[] dataTemp = new byte[13];
                    if (_socket.Receive(dataTemp))
                    {
                        Console.WriteLine(string.Format("与站点{0}断开成功!", site.Sn));
                    }
                }
            }
            catch (Exception ex)
            {
                Program.LoggerHelper.Error(typeof(MessageBusiness), ex.Message, ex);
                Console.WriteLine(string.Format("执行业务逻辑出现异常!{0}", ex));
            }
            finally
            {
                _socket.Close();
            }
        }