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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using IStation.Untity;
 
namespace IStation.DataDockingSocket
{
    /// <summary>
    /// 处理数据辅助类
    /// </summary>
    internal class MonitorMsgHelper
    {
        /// <summary>
        /// 是否是当前MSG,表示需要处理
        /// </summary>
        /// <param name="byteMessage"></param>
        /// <returns></returns>
        public static bool IsNeedHandle(byte[] byteMessage)
        {
            if (byteMessage[0] == 0x01 && byteMessage[1] == 0x03 && byteMessage[2] == 0x42)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
 
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        internal static IEnumerable<QueryInstructionItem> GetInstructionQuerys()
        {
            var appParas = AppParasHelper.GetInstance();
            if (appParas == null)
            {
                NtLogHelper.Error("Socket 数据对接中,NT 缺少数据对接配置文件");
                return null;
            }
            return appParas.InstructionQuerys;
        }
 
        /// <summary>
        /// 获取查询指令(监控值)
        /// </summary>
        /// <returns></returns>
        public byte[] GetMonitorMsg监控值()
        {
            return new byte[] { 0x01, 0x03, 0x00, 0x19, 0x00, 0x21, 0x54, 0x15 };
        }
 
 
 
        /// <summary>
        /// 获取查询指令(状态值):01-03-00-12-00-04-E4-0C
        /// </summary>
        /// <returns></returns>
        public static byte[] GetMonitorMsg状态值()
        {
            return new byte[] { 0x01, 0x03, 0x00, 0x12, 0x00, 0x04, 0xE4, 0x0C };
        }
 
 
 
 
 
        /// <summary>
        /// 读取消息(监控值)
        /// </summary>
        /// <param name="byteMessage"></param>
        public static List<Model.MonitorDataDockingReceiveRecord> HandleReceive(byte[] byteMessage)
        {
            var list = new List<Model.MonitorDataDockingReceiveRecord>();
            byte start = 0x1A;
            if (byteMessage == null)
            {
                NtLogHelper.Error($"数据解析失败!数据为空");
                return null;
            }
            if (byteMessage.Count() < 70)
            {
                var byteMsg = BitConverter.ToString(byteMessage);
                NtLogHelper.Error($"数据解析失败!{byteMsg},长度不够, 长度为{byteMessage.Count()},正常长度71");
                return null;
            }
 
            try
            {
                //显示消息
                //string strMessage = BitConverter.ToString(byteMessage, 0, byteMessage.Length);
                //DisplayMessage(string.Format(" 收到的消息:  {0} ", strMessage));
 
 
                //DisplayMessage(string.Format(" 设备地址: {0} (0x{1})", byteMessage[0], Convert.ToString(byteMessage[0], 16)));
                //DisplayMessage(string.Format(" 功能码: {0} ", byteMessage[1]));
 
 
                string msg;
                var now = DateTime.Now;
                //当前管网压力
                list.Add(new Model.MonitorDataDockingReceiveRecord()
                {
                    SysId = 1575030496453332992,
                    RecordType = Model.eMonitorType.General,
                    SrcTime = now,
                    SrcValue = (ByteMsgHelper.GetInt2Byte(byteMessage, 3, out msg) * 0.001).ToString()
                });
 
                //变频频率
                list.Add(new Model.MonitorDataDockingReceiveRecord()
                {
                    SysId = 1575030605857558528,
                    RecordType = Model.eMonitorType.General,
                    SrcTime = now,
                    SrcValue = (ByteMsgHelper.GetInt2Byte(byteMessage, 3 + (27 - start) * 2, out msg) * 0.1).ToString()
                });
 
                //变频电流
                list.Add(new Model.MonitorDataDockingReceiveRecord()
                {
                    SysId = 1582301190702632960,
                    RecordType = Model.eMonitorType.General,
                    SrcTime = now,
                    SrcValue = (ByteMsgHelper.GetInt2Byte(byteMessage, 3 + (28 - start) * 2, out msg) * 0.1).ToString()
                });
 
                //真空当前压力
                list.Add(new Model.MonitorDataDockingReceiveRecord()
                {
                    SysId = 1575030435430404096,
                    RecordType = Model.eMonitorType.General,
                    SrcTime = now,
                    SrcValue = ((ByteMsgHelper.GetInt2Byte(byteMessage, 3 + (29 - start) * 2, out msg) * 0.001)*-1).ToString()
                }); 
 
                //A相电压
                list.Add(new Model.MonitorDataDockingReceiveRecord()
                {
                    SysId = 1582301249292865536,
                    RecordType = Model.eMonitorType.General,
                    SrcTime = now,
                    SrcValue = Math.Round(ByteMsgHelper.GetDouble1(byteMessage, 3 + (30 - start) * 2, out msg), 2).ToString()
                });
 
                //B相电压
                list.Add(new Model.MonitorDataDockingReceiveRecord()
                {
                    SysId = 1582301287175819264,
                    RecordType = Model.eMonitorType.General,
                    SrcTime = now,
                    SrcValue = Math.Round(ByteMsgHelper.GetDouble1(byteMessage, 3 + (32 - start) * 2, out msg), 2).ToString()
                });
 
                //C相电压
                list.Add(new Model.MonitorDataDockingReceiveRecord()
                {
                    SysId = 1582301332780486656,
                    RecordType = Model.eMonitorType.General,
                    SrcTime = now,
                    SrcValue = Math.Round(ByteMsgHelper.GetDouble1(byteMessage, 3 + (34 - start) * 2, out msg), 2).ToString()
                });
 
                //A相电流
                list.Add(new Model.MonitorDataDockingReceiveRecord()
                {
                    SysId = 1582301384118767616,
                    RecordType = Model.eMonitorType.General,
                    SrcTime = now,
                    SrcValue = Math.Round(ByteMsgHelper.GetDouble1(byteMessage, 3 + (36 - start) * 2, out msg), 3).ToString()
                });
 
                //B相电流
                list.Add(new Model.MonitorDataDockingReceiveRecord()
                {
                    SysId = 1582301478792597504,
                    RecordType = Model.eMonitorType.General,
                    SrcTime = now,
                    SrcValue = Math.Round(ByteMsgHelper.GetDouble1(byteMessage, 3 + (38 - start) * 2, out msg), 3).ToString()
                });
 
                //C相电流
                list.Add(new Model.MonitorDataDockingReceiveRecord()
                {
                    SysId = 1582301691439616000,
                    RecordType = Model.eMonitorType.General,
                    SrcTime = now,
                    SrcValue = Math.Round(ByteMsgHelper.GetDouble1(byteMessage, 3 + (40 - start) * 2, out msg), 3).ToString()
                });
 
                //变频泵状态
                list.Add(new Model.MonitorDataDockingReceiveRecord()
                {
                    SysId = 1582303421711650816,
                    RecordType = Model.eMonitorType.General,
                    SrcTime = now,
                    SrcValue = ByteMsgHelper.GetInt2Byte(byteMessage, 3 + (42 - start) * 2, out msg).ToString()
                });
 
                //真空泵状态
                list.Add(new Model.MonitorDataDockingReceiveRecord()
                {
                    SysId = 1582303241251721216,
                    RecordType = Model.eMonitorType.General,
                    SrcTime = now,
                    SrcValue = ByteMsgHelper.GetInt2Byte(byteMessage, 3 + (43 - start) * 2, out msg).ToString()
                });
 
                ////电动阀开阀状态
                //list.Add(new Model.MonitorDataDockingReceiveRecord() { SysId = 1582301332780486656, RecordType = Model.eMonitorType.General, SrcTime = now,
                //    SrcValue = string.Format("########电动阀开阀状态 -----> {0}   数据二进制{1}", GetInt2Byte(byteMessage, 3 + (44 - start) * 2, out msg).ToString()  });
 
                ////电动阀关阀状态
                //list.Add(new Model.MonitorDataDockingReceiveRecord() { SysId = 1582301332780486656, RecordType = Model.eMonitorType.General, SrcTime = now, 
                //    SrcValue = string.Format("########电动阀关阀状态 -----> {0}   数据二进制{1}", GetInt2Byte(byteMessage, 3 + (45 - start) * 2, out msg).ToString()  });
 
                //电磁阀状态
                list.Add(new Model.MonitorDataDockingReceiveRecord()
                {
                    SysId = 1582303081771700224,
                    RecordType = Model.eMonitorType.General,
                    SrcTime = now,
                    SrcValue = ByteMsgHelper.GetInt2Byte(byteMessage, 3 + (46 - start) * 2, out msg).ToString()
                });
 
                //水位状态
                list.Add(new Model.MonitorDataDockingReceiveRecord()
                {
                    SysId = 1582303471342850048,
                    RecordType = Model.eMonitorType.General,
                    SrcTime = now,
                    SrcValue = ByteMsgHelper.GetInt2Byte(byteMessage, 3 + (47 - start) * 2, out msg).ToString()
                });
 
                //超压状态
                list.Add(new Model.MonitorDataDockingReceiveRecord()
                {
                    SysId = 1582303606781120512,
                    RecordType = Model.eMonitorType.General,
                    SrcTime = now,
                    SrcValue = ByteMsgHelper.GetInt2Byte(byteMessage, 3 + (48 - start) * 2, out msg).ToString()
                });
 
                //报警状态
                list.Add(new Model.MonitorDataDockingReceiveRecord()
                {
                    SysId = 1582303637420511232,
                    RecordType = Model.eMonitorType.General,
                    SrcTime = now,
                    SrcValue = ByteMsgHelper.GetInt2Byte(byteMessage, 3 + (49 - start) * 2, out msg).ToString()
                });
 
                //变频器状态
                list.Add(new Model.MonitorDataDockingReceiveRecord()
                {
                    SysId = 1582303671927050240,
                    RecordType = Model.eMonitorType.General,
                    SrcTime = now,
                    SrcValue = ByteMsgHelper.GetInt2Byte(byteMessage, 3 + (50 - start) * 2, out msg).ToString()
                });
 
                //当前液位
                list.Add(new Model.MonitorDataDockingReceiveRecord()
                {
                    SysId = 1575388386963886080,
                    RecordType = Model.eMonitorType.General,
                    SrcTime = now,
                    SrcValue = (ByteMsgHelper.GetInt2Byte(byteMessage, 3 + (51 - start) * 2, out msg) * 0.01).ToString()
                });
 
                //瞬时流量
                list.Add(new Model.MonitorDataDockingReceiveRecord()
                {
                    SysId = 1575030320913321984,
                    RecordType = Model.eMonitorType.General,
                    SrcTime = now,
                    SrcValue = (ByteMsgHelper.GetInt4Byte(byteMessage, 3 + (52 - start) * 2, out msg) * 0.001).ToString()
                });
 
                //累计流量
                list.Add(new Model.MonitorDataDockingReceiveRecord()
                {
                    SysId = 1584475350807744512,
                    RecordType = Model.eMonitorType.General,
                    SrcTime = now,
                    SrcValue = (ByteMsgHelper.GetInt4Byte(byteMessage, 3 + (54 - start) * 2, out msg) * 1.0).ToString()
                });
 
                //累计流量有功功率
                list.Add(new Model.MonitorDataDockingReceiveRecord()
                {
                    SysId = 1584475756476633088,
                    RecordType = Model.eMonitorType.General,
                    SrcTime = now,
                    SrcValue = Math.Round(ByteMsgHelper.GetDouble1(byteMessage, 3 + (56 - start) * 2, out msg), 2).ToString()
                });
            }
            catch (Exception err)
            {
                var byteMsg = string.Join(',', byteMessage);
                NtLogHelper.Error($"数据解析异常!{byteMsg}" +
                    $"错误提示:{err.Message}");
            }
 
            return list;
        }
 
        /// <summary>
        /// 读取消息(状态值)
        /// </summary>
        /// <param name="byteMessage"></param>
        private static List<Model.MonitorDataDockingReceiveRecord> ReadMessage状态值(byte[] byteMessage)
        {
            var list = new List<Model.MonitorDataDockingReceiveRecord>();
            byte start = 0x12;
            if (byteMessage == null || byteMessage.Length < 8)
            {
                return null;
            }
            try
            {
                //显示消息
                //string strMessage = BitConverter.ToString(byteMessage, 0, byteMessage.Length);
                //DisplayMessage(string.Format(" 收到的消息:  {0} ", strMessage));
 
 
                //DisplayMessage(string.Format(" 设备地址: {0} (0x{1})", byteMessage[0], Convert.ToString(byteMessage[0], 16)));
                //DisplayMessage(string.Format(" 功能码: {0} ", byteMessage[1]));
 
 
                string msg;
 
                var now = DateTime.Now;
                //液位量程
                //list.Add(new Model.MonitorDataDockingReceiveRecord()
                //{
                //    SysId =  ,
                //    RecordType = Model.eMonitorType.General,
                //    SrcTime = now,
                //    SrcValue = (GetInt2Byte(byteMessage, 3 + (19 - start), out msg )* 0.1).ToString()
                //});
                //设定频率
                list.Add(new Model.MonitorDataDockingReceiveRecord()
                {
                    SysId = 1584894751252025344,
                    RecordType = Model.eMonitorType.General,
                    SrcTime = now,
                    SrcValue = (ByteMsgHelper.GetInt2Byte(byteMessage, 3 + (20 - start), out msg) * 0.1).ToString()
                });
            }
            catch// (Exception err)
            {
                return null;
            }
            return list;
        }
 
 
 
    }
 
}