Пример #1
0
 public void AlarmStatusInsert(Lrdm.AlarmStatusModels alarmdata)
 {
     _localRadarDataDb.Insert(alarmdata);
 }
        private void RecDataProcessing(byte[] message)
        {
            Thread newThread = new Thread((ThreadStart) delegate
            {
                try
                {
                    if (message.Length <= 4)
                    {
                        CommonLogHelper.GetInstance("LogInfo").Info(@"未接收到采集数据"); return;
                    }
                    if (message[0] != Convert.ToByte("7E", 16))
                    {
                        return;
                    }
                    if (message[1] != Convert.ToByte("D1", 16))
                    {
                        return;
                    }
                    string m = message[4].ToString("X2");
                    switch (m)
                    {
                    case "03":

                        #region 获取到的状态、报警、电压数据处理

                        _synchContext.Post(a => { tSSLSystemStatus.Text = @"获取最新数据成功"; }, null);
                        DateTime dt         = DateTime.Now;
                        DateTime obdt       = new DateTime(dt.Year, dt.Month, dt.Day, dt.Hour, dt.Minute, 0);
                        DataAdapter dataadp = new DataAdapter(StationId, obdt);
                        try
                        {
                            string msg = string.Join(" ", message.Select(x => x.ToString("X2")).ToArray());
                            CommonLogHelper.GetInstance("LogInfo").Info($@"接收到采集数据:{msg}");
                            dataadp.DataResolve(message);
                            _synchContext.Post(a =>
                            {
                                try
                                {
                                    radarStatusUI.SetAlarmStatus(dataadp.AlarmStatusData);
                                    radarControlUI.SetControlStatus(dataadp.ControlStatusData);
                                    powerIndicationUI.SetPowerDataValue(dataadp.RadarPowerData);
                                    tSSLDataUpdateTime.Text = $"数据更新时间:{obdt.ToString("yyyy-MM-dd HH:mm")}";
                                }
                                catch (Exception ex)
                                {
                                    CommonLogHelper.GetInstance("LogError").Error(@"数据显示到界面上出错", ex);
                                }
                            }, null);

                            RadarFailureAlarm(dataadp.AlarmStatusData);
                        }
                        catch (Exception e)
                        {
                            CommonLogHelper.GetInstance("LogError").Error(@"接收处理数据并显示过程出错", e);
                        }

                        if (_isCollect)
                        {
                            AlarmStatusTable alarmStatus     = dataadp.DataResolve(dataadp.AlarmStatusData);
                            ControlStatusTable controlStatus = dataadp.DataResolve(dataadp.ControlStatusData);
                            PowerDataTable powerData         = dataadp.DataResolve(dataadp.RadarPowerData);

                            try
                            {
                                DataBaseHelper dbHelper = new DataBaseHelper(_conn);
                                dbHelper.AlarmStatusInsert(alarmStatus);
                                dbHelper.ControlStatusInsert(controlStatus);
                                dbHelper.PowerDataInsert(powerData);
                                dbHelper.SaveChanges();
                                CommonLogHelper.GetInstance("LogInfo").Info(@"数据入库成功");
                            }
                            catch (Exception e)
                            {
                                CommonLogHelper.GetInstance("LogError").Error(@"数据入库过程出错", e);
                                try
                                {
                                    Lrdm.AlarmStatusModels localalarmStatus     = dataadp.LocalDataResolve(dataadp.AlarmStatusData);
                                    Lrdm.ControlStatusModels localcontrolStatus = dataadp.LocalDataResolve(dataadp.ControlStatusData);
                                    Lrdm.PowerDataModels localpowerData         = dataadp.LocalDataResolve(dataadp.RadarPowerData);

                                    DataBaseHelper dbHelper = new DataBaseHelper();
                                    dbHelper.AlarmStatusInsert(localalarmStatus);
                                    dbHelper.ControlStatusInsert(localcontrolStatus);
                                    dbHelper.PowerDataInsert(localpowerData);
                                    CommonLogHelper.GetInstance("LogInfo").Info(@"数据入本地库成功");
                                }
                                catch (Exception exception)
                                {
                                    CommonLogHelper.GetInstance("LogError").Error(@"数据入本地库过程出错", exception);
                                }
                            }

                            try
                            {
                                RadarDataClassesDataContext radarDataContext = new RadarDataClassesDataContext(_conn);
                                PowerDataTable prepowerData =
                                    radarDataContext.PowerDataTable.First(
                                        x =>
                                        x.DateTime == dt.AddMinutes(-Para.UpdateDataIntl) &&
                                        x.StationID == StationId);
                                PowerAlarm(obdt, powerData, prepowerData);
                            }
                            catch (Exception ex)
                            {
                                CommonLogHelper.GetInstance("LogError").Error(@"电源跳变检查报警过程出错", ex);
                            }
                        }

                        #endregion 获取到的状态、报警、电压数据处理

                        break;

                    case "81":
                        string controlmsg = string.Join(" ", message.Select(x => x.ToString("X2")).ToArray());
                        CommonLogHelper.GetInstance("LogInfo").Info($@"接收到控制数据:{controlmsg}");
                        //isContinueKeyUp = isfirsControlcmd;
                        break;
                    }
                }
                catch (Exception e)
                {
                    CommonLogHelper.GetInstance("LogError").Error(_isCollect ? @"接收处理数据并入库过程出错" : @"接收处理数据并显示过程出错", e);
                }
                finally
                {
                    _isCollect = false;
                    _synchContext.Post(a => { tSSLSystemStatus.Text = radarControlUI.Enabled ? @"正在使用控制功能" : @"等待采集数据"; }, null);
                }
            })
            {
                IsBackground = true
            };

            newThread.Start();
        }