ningshuxia
2022-10-28 3ccb7c60e1ed8b6748ed7fb8b64b1dbe50d62abf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
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;
 
        /// <summary>
        /// 开机
        /// </summary>
        /// <param name="session"></param>
        /// <returns></returns>
        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;
        }
 
        /// <summary>
        /// 是否需要当前类进行处理
        /// </summary>
        /// <param name="byteMessage"></param>
        /// <returns></returns>
        public static bool IsNeedHandle(byte[] byteMessage)
        {
            if(InstructionStatus != eStartUpInstructionStatus.未发送)
                return true;
            else
                return false;
        }
        /// <summary>
        /// 开机指令
        /// </summary>
        static string ControlMsgOpen = "01-06-00-0F-00-01-78-09";
        /// <summary>
        /// 读取返回消息 处理成功返回成功, 不成功或者不需要处理返回false
        /// </summary>
        /// <param name="byteMessage"></param>
        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) + ",  ");
        }
 
 
        /// <summary>
        /// 获取控制指令(开机)
        /// </summary>
        /// <returns></returns>
        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);
        }
 
        /// <summary>
        /// 获取控制指令(调频)
        /// </summary>
        /// <returns></returns>
        public static byte[] GetControlMsg调频(double frequece)
        {
            var iFrequece = (int)(frequece * 10);
            byte[] commandSend = null;
            ModBusRtuHelper.BuildSendMessage06(0x01, 20, 2,
                iFrequece, ref commandSend);
 
            return commandSend;
        }
 
 
 
 
 
    }
}