private void btnSetPwd_Click(object sender, EventArgs e)
 {
     try
     {
         if (tbOldPwd.Text.Length < 8 || tbNewPwd.Text.Length < 8 || tbNewPwdConfirm.Text.Length < 8)
         {
             MessageBox.Show(@"密码均要输入8位!");
             return;
         }
         List <byte> cmdBytes = new List <byte>
         {
             Convert.ToByte("83", 16)
         };
         byte[] oldpwds = HexBinDecOct.StringToBytes(tbOldPwd.Text);
         byte[] newpwds = HexBinDecOct.StringToBytes(tbNewPwd.Text);
         cmdBytes.AddRange(oldpwds);
         cmdBytes.AddRange(newpwds);
         byte[] cmd = SerialPortCmdCombine.SetBaseCommand("FF", cmdBytes.ToArray());
         SerilPortOp.SendCommand(cmd);
     }
     catch (Exception exception)
     {
         MessageBox.Show(@"设置密码失败!出错信息:" + Environment.NewLine + exception);
     }
 }
        private void tmrControl_Elapsed(object sender, EventArgs e)
        {
            _tmrControl.Stop();

            #region 台站级别发送指令

            try
            {
                Thread updateStatusThread = new Thread((ThreadStart) delegate
                {
                    if (Para.StationGrade.Contains("台站"))
                    {
                        byte[] cmd = SerialPortCmdCombine.SetBaseCommand("FF", "03");
                        CommonLogHelper.GetInstance("LogInfo")
                        .Info($@"延时8s后发送采集命令:{string.Join(" ", cmd.Select(x => x.ToString("X2")).ToArray())}");
                        _serialPort.SendCommand(cmd);
                    }
                })
                {
                    IsBackground = true
                };
                updateStatusThread.Start();
            }
            catch (Exception ex)
            {
                CommonLogHelper.GetInstance("LogError").Error(@"发送串口命令获取数据过程出错", ex);
            }

            #endregion 台站级别发送指令
        }
 private void CollectorSettingForm_Load(object sender, EventArgs e)
 {
     SerilPortOp.ReturnCollectorRecData += RecDataProcessing;
     byte[] cmd = SerialPortCmdCombine.SetBaseCommand("FF", "02");
     try
     {
         SerilPortOp.SendCommand(cmd);
     }
     catch (Exception exception)
     {
         MessageBox.Show(@"获取采集器基本信息失败!出错信息:" + Environment.NewLine + exception);
     }
 }
 private void btnRest_Click(object sender, EventArgs e)
 {
     try
     {
         if (MessageBox.Show(@"确实要复位采集器?", @"警告", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
         {
             return;
         }
         byte[] cmd = SerialPortCmdCombine.SetBaseCommand("FF", "01");
         SerilPortOp.SendCommand(cmd);
     }
     catch (Exception exception)
     {
         MessageBox.Show(@"复位采集器失败!出错信息:" + Environment.NewLine + exception);
     }
 }
        private void SendControlCmd(string cmd1, string cmd2)
        {
            Thread controlThread2 = new Thread((ThreadStart) delegate
            {
                try
                {
                    string pwd     = "88 88 88 88 88 88 88 88";
                    byte[] cmdbyte = SerialPortCmdCombine.SetBaseCommand("FF", pwd, "81", cmd1);
                    CommonLogHelper.GetInstance("LogInfo").Info(
                        $@"发送控制命令1:{string.Join(" ", cmdbyte.Select(x => x.ToString("X2")).ToArray())}");
                    //isfirsControlcmd = true;
                    _serialPort.SendCommand(cmdbyte);
                    //while (!isContinueKeyUp)
                    //{
                    //    //SpinWait.SpinUntil(() => isContinueKeyUp);
                    //}
                    Thread.Sleep(1200);
                    //Thread.SpinWait(1200);
                    //isfirsControlcmd = false;
                    //isContinueKeyUp = false;
                    byte[] cmdbyte2 = SerialPortCmdCombine.SetBaseCommand("FF", pwd, "81", cmd2);
                    CommonLogHelper.GetInstance("LogInfo").Info(
                        $@"发送控制命令2:{string.Join(" ", cmdbyte2.Select(x => x.ToString("X2")).ToArray())}");
                    _serialPort.SendCommand(cmdbyte2);
                    _tmrControl.Start();
                    MessageBox.Show(@"发送控制指令成功,等待8秒后更新界面", @"提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    //返回状态更新界面
                }
                catch (Exception ex)
                {
                    CommonLogHelper.GetInstance("LogError").Error(@"发送控制命令过程出错", ex);
                    MessageBox.Show(@"发送控制命令过程出错," + ex.Message, @"错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            })
            {
                IsBackground = true
            };

            controlThread2.Start();
        }
 private void btnSetEquipId_Click(object sender, EventArgs e)
 {
     try
     {
         if (tbEquipId.Text.Length < 16)
         {
             MessageBox.Show(@"请输入16位设备编号!");
             return;
         }
         List <byte> cmdBytes = new List <byte>
         {
             Convert.ToByte("82", 16)
         };
         byte[] newid = HexBinDecOct.StringToBytes(tbEquipId.Text);
         cmdBytes.AddRange(newid);
         byte[] cmd = SerialPortCmdCombine.SetBaseCommand("FF", cmdBytes.ToArray());
         SerilPortOp.SendCommand(cmd);
     }
     catch (Exception ex)
     {
         MessageBox.Show(@"设置采集器设备编号失败!出错信息:" + Environment.NewLine + ex);
     }
 }
        private void 实时采集ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            DateTime dt   = DateTime.Now;
            DateTime obdt = dt.AddSeconds(-dt.Second).AddMilliseconds(-dt.Millisecond);

            #region 台站级别发送指令

            if (Para.StationGrade.Contains("台站"))
            {
                Thread collectThread = new Thread((ThreadStart) delegate
                {
                    try
                    {
                        SerialPortOperate tempSerialPortOperate = _serialPort;
                        //_tempSerialPortOperate.ReturnCollectorRecData += ManualRecDataProcessing;
                        byte[] cmd = SerialPortCmdCombine.SetBaseCommand("FF", "03");
                        CommonLogHelper.GetInstance("LogInfo")
                        .Info($@"实时采集:发送采集命令:{string.Join(" ", cmd.Select(x => x.ToString("X2")).ToArray())}");
                        tempSerialPortOperate.SendCommand(cmd);
                    }
                    catch (Exception ex)
                    {
                        CommonLogHelper.GetInstance("LogInfo").Info(@"实时采集:发送串口命令获取数据过程出错");
                        CommonLogHelper.GetInstance("LogError").Error(@"实时采集:发送串口命令获取数据过程出错", ex);
                        _synchContext.Post(a =>
                                           MessageBox.Show(this, @"实时采集:发送串口命令获取数据过程出错," + ex.Message, @"错误", MessageBoxButtons.OK, MessageBoxIcon.Error)
                                           , null);
                    }
                })
                {
                    IsBackground = true
                };
                collectThread.Start();
            }

            #endregion 台站级别发送指令

            #region 省级级别操作数据库

            try
            {
                if (Para.StationGrade.Contains("省级"))
                {
                    Thread collectProThread = new Thread((ThreadStart) delegate
                    {
                        tSSLSystemStatus.Text = @"获取最新数据";
                        RadarDataClassesDataContext radarDataContext = new RadarDataClassesDataContext(_conn);
                        DataAdapter dataadp = new DataAdapter(StationId, obdt);
                        dataadp.DataResolve(radarDataContext);
                        _synchContext.Post(a =>
                        {
                            try
                            {
                                if (dataadp.AlarmStatusData != null)
                                {
                                    radarStatusUI.SetAlarmStatus(dataadp.AlarmStatusData);
                                }
                                if (dataadp.ControlStatusData != null)
                                {
                                    radarControlUI.SetControlStatus(dataadp.ControlStatusData);
                                }
                                if (dataadp.RadarPowerData != null)
                                {
                                    powerIndicationUI.SetPowerDataValue(dataadp.RadarPowerData);
                                }
                                if (dataadp.AlarmStatusData != null || dataadp.ControlStatusData != null ||
                                    dataadp.RadarPowerData != null)
                                {
                                    tSSLDataUpdateTime.Text = $"数据更新时间:{obdt.ToString("yyyy-MM-dd HH:mm")}";
                                }
                                else
                                {
                                    MessageBox.Show(@"无当前实时数据");
                                }
                            }
                            catch (Exception ex)
                            {
                                MessageBox.Show(@"数据显示到界面上出错" + ex);
                            }
                        }, null);
                        CommonLogHelper.GetInstance("LogInfo").Info(@"获取数据库数据成功");

                        try
                        {
                            RadarFailureAlarm(dataadp.AlarmStatusData);
                        }
                        catch (Exception ex)
                        {
                            CommonLogHelper.GetInstance("LogError").Error(@"雷达故障报警过程出错", ex);
                        }
                    })
                    {
                        IsBackground = true
                    };
                    collectProThread.Start();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(this, @"提取数据库数据处理过程出错" + ex, @"错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

            #endregion 省级级别操作数据库
        }
        private void tmr_Elapsed(object sender, EventArgs e)
        {
            DateTime dt   = DateTime.Now;
            DateTime obdt = new DateTime(dt.Year, dt.Month, dt.Day, dt.Hour, dt.Minute, 0);

            if (dt.Minute % Para.UpdateDataIntl == 0 && dt.Second == 30)
            {
                #region 台站级别发送指令

                if (Para.StationGrade.Contains("台站"))
                {
                    Thread collectTimeThread = new Thread((ThreadStart) delegate
                    {
                        try
                        {
                            _synchContext.Post(a =>
                            {
                                toolSslNextUpdateTime.Text =
                                    $"下次采集时间:{obdt.AddSeconds(30).AddMinutes(Para.UpdateDataIntl).ToString("yyyy-MM-dd HH:mm:ss")}";
                                tSSLSystemStatus.Text = @"正在获取最新数据";
                            }, null);
                            byte[] cmd = SerialPortCmdCombine.SetBaseCommand("FF", "03");
                            CommonLogHelper.GetInstance("LogInfo")
                            .Info($@"发送定时采集命令:{string.Join(" ", cmd.Select(x => x.ToString("X2")).ToArray())}");
                            _isCollect = true;
                            _serialPort.SendCommand(cmd);
                        }
                        catch (Exception ex)
                        {
                            CommonLogHelper.GetInstance("LogInfo").Info(@"发送串口命令获取数据过程出错");
                            CommonLogHelper.GetInstance("LogError").Error(@"发送串口命令获取数据过程出错", ex);
                        }
                    })
                    {
                        IsBackground = true
                    };
                    collectTimeThread.Start();
                }

                #endregion 台站级别发送指令
            }
            if (dt.Second == 40)
            {
                #region 停止声音报警

                _soundAlarm.StopAlarm();

                #endregion 停止声音报警
            }
            if (dt.Minute % Para.UpdateDataIntl == 0 && dt.Second == 45)
            {
                #region 省级级别操作数据库

                if (Para.StationGrade.Contains("省级"))
                {
                    Thread collectProThread = new Thread((ThreadStart) delegate
                    {
                        try
                        {
                            bool havePowerData    = false;
                            tSSLSystemStatus.Text = @"获取最新数据";
                            RadarDataClassesDataContext radarDataContext = new RadarDataClassesDataContext(_conn);
                            DataAdapter dataadp = new DataAdapter(StationId, obdt);
                            dataadp.DataResolve(radarDataContext);
                            _synchContext.Post(a =>
                            {
                                toolSslNextUpdateTime.Text =
                                    $"下次采集时间:{obdt.AddSeconds(45).AddMinutes(Para.UpdateDataIntl).ToString("yyyy-MM-dd HH:mm:ss")}";
                                try
                                {
                                    if (dataadp.AlarmStatusData != null)
                                    {
                                        radarStatusUI.SetAlarmStatus(dataadp.AlarmStatusData);
                                    }
                                    else
                                    {
                                        CommonLogHelper.GetInstance("LogInfo").Error(@"无当前报警数据");
                                    }
                                    if (dataadp.ControlStatusData != null)
                                    {
                                        radarControlUI.SetControlStatus(dataadp.ControlStatusData);
                                    }
                                    else
                                    {
                                        CommonLogHelper.GetInstance("LogInfo").Error(@"无当前状态数据");
                                    }
                                    if (dataadp.RadarPowerData != null)
                                    {
                                        powerIndicationUI.SetPowerDataValue(dataadp.RadarPowerData);
                                        havePowerData = true;
                                    }
                                    else
                                    {
                                        CommonLogHelper.GetInstance("LogInfo").Error(@"无当前电源数据");
                                    }
                                    if (dataadp.AlarmStatusData != null || dataadp.ControlStatusData != null || dataadp.RadarPowerData != null)
                                    {
                                        tSSLDataUpdateTime.Text = $"数据更新时间:{obdt.ToString("yyyy-MM-dd HH:mm")}";
                                    }
                                }
                                catch (Exception ex)
                                {
                                    CommonLogHelper.GetInstance("LogError").Error(@"数据显示到界面上出错", ex);
                                }
                            }, null);
                            CommonLogHelper.GetInstance("LogInfo").Info(@"获取数据库数据成功");

                            try
                            {
                                RadarFailureAlarm(dataadp.AlarmStatusData);
                            }
                            catch (Exception ex)
                            {
                                CommonLogHelper.GetInstance("LogError").Error(@"雷达故障报警过程出错", ex);
                            }

                            try
                            {
                                if (havePowerData)
                                {
                                    PowerDataTable nowpowerData =
                                        radarDataContext.PowerDataTable.First(x => x.DateTime == dt && x.StationID == StationId);
                                    PowerDataTable prepowerData =
                                        radarDataContext.PowerDataTable.First(
                                            x =>
                                            x.DateTime == dt.AddMinutes(-Para.UpdateDataIntl) &&
                                            x.StationID == StationId);
                                    PowerAlarm(obdt, nowpowerData, prepowerData);
                                }
                            }
                            catch (Exception ex)
                            {
                                CommonLogHelper.GetInstance("LogError").Error(@"电源跳变检查过程出错", ex);
                            }
                        }
                        catch (Exception ex)
                        {
                            CommonLogHelper.GetInstance("LogError").Error(@"提取数据库数据处理过程出错", ex);
                        }
                    })
                    {
                        IsBackground = true
                    };
                    collectProThread.Start();
                }

                #endregion 省级级别操作数据库
            }
            if (dt.Minute % 5 == 0 && dt.Second == 50)
            {
                #region 正常入库失败后批量数据补入

                Thread batchDataProcessThread = new Thread((ThreadStart) delegate
                {
                    bool dbisAvaliable = true;
                    try
                    {
                        RadarDataClassesDataContext radarDataContext = new RadarDataClassesDataContext(_conn);
                        radarDataContext.Connection.Open();
                        radarDataContext.Connection.Close();
                    }
                    catch (Exception)
                    {
                        dbisAvaliable = false;
                    }
                    if (dbisAvaliable)
                    {
                        DataBaseHelper dbHelper = new DataBaseHelper(_conn);
                        try
                        {
                            DataAdapter dataAdapter = new DataAdapter();
                            CommonLogHelper.GetInstance("LogInfo").Info(@"开始批量数据入库");
                            dbHelper.AlarmStatusInsert(dataAdapter.DataResolve(new DataBaseHelper().AlarmStatusSelect()));
                            dbHelper.ControlStatusInsert(
                                dataAdapter.DataResolve(new DataBaseHelper().ControlStatusSelect()));
                            dbHelper.PowerDataInsert(dataAdapter.DataResolve(new DataBaseHelper().PowerDataSelect()));
                            dbHelper.SaveChanges();
                            CommonLogHelper.GetInstance("LogInfo").Info(@"批量数据入库成功");
                            new DataBaseHelper().DeleteAllSqLiteData();
                            CommonLogHelper.GetInstance("LogInfo").Info(@"本地数据删除成功");
                        }
                        catch (Exception ex)
                        {
                            CommonLogHelper.GetInstance("LogInfo").Info(@"批量数据入库失败");
                            CommonLogHelper.GetInstance("LogError").Error(@"批量数据入库失败", ex);
                        }
                        finally
                        {
                            dbHelper.Dispose();
                        }
                    }
                })
                {
                    IsBackground = true
                };
                batchDataProcessThread.Start();

                #endregion 正常入库失败后批量数据补入
            }
        }