using IStation.Model; using IStation.Untity; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using static System.Collections.Specialized.BitVector32; namespace IStation.DataDockingSocket { internal class StartUpMsgHelper { public static eStartUpInstructionStatus InstructionStatus = eStartUpInstructionStatus.未发送; public static DateTime _lastHzSendTime; public static DateTime _lastOpenSendTime; public static MonitorTestControlParas.StartContext _lastParas = null; /// /// 开机 /// /// /// public static bool StartJob(Model.IMonitorDataDockingSession session, MonitorTestControlParas.StartContext paras) { if (session == null) { NtLogHelper.Error($"开机 session:Close"); return false; } if(paras == null) { NtLogHelper.Error($"开机参数为空"); return false; } if (!session.IsConnected) { NtLogHelper.Error($"开机时 session:Not connected"); return false; } if (paras.HZ <= 10) { NtLogHelper.Error($"开机时 设置的调频频率过低!"); return false; } _lastParas = paras; //switch (context.OperatingMode) //{ // case Model.MonitorTestControlParas.eOperatingMode.Timing: // break; // case Model.MonitorTestControlParas.eOperatingMode.ConstantFlow: // break; // default: // break; //} byte[] bts = GetControlMsg调频(paras.HZ); session.Send(bts, 0, bts.Length); InstructionStatus = eStartUpInstructionStatus.调频指令发送; _lastHzSendTime = DateTime.Now; NtLogHelper.Info("开机指令: 调频 " + BitTransfer.ToString(bts) + ", HZ:"+ paras.HZ); return true; } /// /// 是否需要当前类进行处理 /// /// /// public static bool IsNeedHandle(byte[] byteMessage) { if(InstructionStatus != eStartUpInstructionStatus.未发送) return true; else return false; } /// /// 开机指令 /// static string ControlMsgOpen = "01-06-00-0F-00-01-78-09"; /// /// 读取返回消息 处理成功返回成功, 不成功或者不需要处理返回false /// /// public static bool HandleReceive(Model.IMonitorDataDockingSession session, byte[] byteMessage) { if (InstructionStatus == eStartUpInstructionStatus.未发送) { NtLogHelper.Info("InstructionStatus==eStartUpInstructionStatus.未发送"); return false; } if (InstructionStatus == eStartUpInstructionStatus.开机指令发送) { NtLogHelper.Info("InstructionStatus==eStartUpInstructionStatus.开机指令发送"); var byteMsgOpen = Encoding.Unicode.GetBytes(ControlMsgOpen); //if (byteMessage.Length >= byteMsgOpen.Length) //{ string strMessage = BitConverter.ToString(byteMessage, 0, byteMessage.Length); if (strMessage == ControlMsgOpen) { NtLogHelper.Info($"strMessage ({strMessage})== ControlMsgOpen ({ControlMsgOpen})"); //01-06-00-0F-00-01-78-09 InstructionStatus = eStartUpInstructionStatus.未发送; NtLogHelper.Info("开机指令返回:" + BitTransfer.ToString(byteMessage)); if (_lastParas.OperatingValue > 0) { //开启自动关闭 ShutDownMsgHelper.StartAutoClose(_lastParas.OperatingValue); } else { NtLogHelper.Info("时间小于等于0,不开启自动关闭"); } return true; } else { NtLogHelper.Info($"strMessage ({strMessage})!= ControlMsgOpen ({ControlMsgOpen})"); } //} //else //{ // NtLogHelper.Info($"byteMessage.Length ({byteMessage.Length}) <= ControlMsgOpen.Length ({ControlMsgOpen.Length})"); //} //返回失败, 再发送一次再确认一下 if ((DateTime.Now - _lastOpenSendTime).TotalSeconds > 5) { NtLogHelper.Info($"返回失败, 再发送一次再确认一下"); SendOpenMsg(session); } } if (InstructionStatus == eStartUpInstructionStatus.调频指令发送) { NtLogHelper.Info($"InstructionStatus == eStartUpInstructionStatus.调频指令发送"); //需要判断是否是调频指令:以后再补充 { SendOpenMsg(session); } //返回失败, 再发送一次再确认一下 if ((DateTime.Now - _lastHzSendTime).TotalSeconds > 5) { NtLogHelper.Info($"返回失败, 再发送一次再确认一下"); StartJob(session, _lastParas); } } return false; } private static void SendOpenMsg(Model.IMonitorDataDockingSession session) { byte[] bts = GetControlMsg开机(); session.Send(bts, 0, bts.Length); InstructionStatus = eStartUpInstructionStatus.开机指令发送; _lastOpenSendTime = DateTime.Now; NtLogHelper.Info("开机指令: 开机 " + BitTransfer.ToString(bts) + ", "); } /// /// 获取控制指令(开机) /// /// public static byte[] GetControlMsg开机() { //var appParas = AppParasHelper.GetInstance(); //if (appParas == null || appParas.InstructionStartUp == null || appParas.InstructionStartUp.Content == null) { return BitTransfer.FromString(ControlMsgOpen); } //return BitTransfer.FromString(appParas.InstructionStartUp.Content); } /// /// 获取控制指令(调频) /// /// public static byte[] GetControlMsg调频(double frequece) { var iFrequece = (int)(frequece * 10); byte[] commandSend = null; ModBusRtuHelper.BuildSendMessage06(0x01, 20, 2, iFrequece, ref commandSend); return commandSend; } } }