ningshuxia
2022-11-22 a5f4bf474fddbc129708f4ddb92eaaafc6ff1f73
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using IStation.Untity;
using IStation.Model;
using SuperSocket.Server;
using SuperSocket;
using IStation.DataDockingSocket;
 
namespace IStation.Server 
{
    /// <summary>
    /// 接收信息辅助类
    /// </summary>
    internal class RequestReceivedHelper
    {
        //接收数据
        public static async ValueTask Receive(IAppSession session, PackageInfo packageInfo)
        {
            await Task.Run(async () =>
            {
                if (packageInfo == null)
                {
                    SZJTKTLogHelper.Error(" packageInfo is null");
                    return;
                } 
                var bytes = packageInfo.Body;
                if (!Valid(bytes))
                {
                    SZJTKTLogHelper.Error(" bytes Validation failed!");
                    return;
                }
                var appParas = AppParasHelper.Get();
                if (appParas == null || appParas.RuleItems == null)
                {
                    SZJTKTLogHelper.Error(" json is null");
                    return;
                }
 
                var byte4 = new byte[4];
                //数据读取日期
                Array.Copy(bytes, 13, byte4, 0, 4);
                var date = Bytes2Int4(byte4); 
                //数据读取时间 
                Array.Copy(bytes, 17, byte4, 0, 4);
                var time = Bytes2Int4(byte4);
 
                var hmsStr = time.ToString();
                if (hmsStr.Length < 6)
                    hmsStr = "0" + hmsStr;
                var hms = hmsStr.Substring(0, 2) + ":" + hmsStr.Substring(2, 2) + ":" + hmsStr.Substring(4, 2);
                var strTime = $"{date} {hms}";
                DateTime dataTime = DateTime.ParseExact(strTime.ToString(), "yyyyMMdd hh:mm:ss", System.Globalization.CultureInfo.CurrentCulture);
 
                var list = new List<Model.MonitorDataDockingSrcRecord>();
                foreach (var x in appParas.RuleItems)
                {
                    var record = new Model.MonitorDataDockingSrcRecord();
                    record.SignId = x.SignId;
                    record.RecordType = Model.eMonitorType.General;
                    record.SrcTime = dataTime;
                    record.SrcValue = null;
                    switch (x.Rule)
                    {
                        case eRule.Double:
                            {
                                var btValue = bytes.Skip((x.Index - 1)).Take(x.Size).ToArray();
                                record.SrcValue = Bytes2Double(btValue).ToString();
                            }
                            break;
                        case eRule.Int4:
                            {
                                var btValue = bytes.Skip((x.Index - 1)).Take(x.Size).ToArray();
                                record.SrcValue = Bytes2Int4(btValue).ToString();
                            }
                            break;
                        case eRule.Int32:
                            {
                                var btValue = bytes.Skip((x.Index - 1)).Take(x.Size).ToArray();
                                record.SrcValue = Bytes2Int32(btValue).ToString();
                            }
                            break;
                        default: break;
                    }
                    list.Add(record);
                }
 
            });
 
        }
 
        //验证
        private static bool Valid(byte[] bytes)
        {
            SZJTKTLogHelper.Info($"字节长度:{bytes.Count()}");
            if (bytes == null || bytes.Count() < 1)
                return false;
            var bytePrefix = new byte[8];
            Array.Copy(bytes, 0, bytePrefix, 0, 8);
            var prefix = Encoding.ASCII.GetString(bytePrefix);
            SZJTKTLogHelper.Info($"前缀:{prefix}");
            if (prefix != Settings.Transfer.SZJTKT.Prefix)
                return false;
 
            byte[] byteProductID = new byte[4];
            Array.Copy(bytes, 9, byteProductID, 0, 4);
            var productId = Bytes2Int4(byteProductID);
            SZJTKTLogHelper.Info($"设备编号:{productId}");
            if (productId.ToString() != Settings.Transfer.SZJTKT.ProductId)
                return false;
            return true;
        }
 
 
        /// <summary>
        /// 处理数据
        /// </summary>
        public static List<Model.MonitorDataDockingSrcRecord> HandleData
            (
                byte[] bts
            )
        {
 
            var list = new List<Model.MonitorDataDockingSrcRecord>();
 
            //将指令转化为字节数组
            var instrution_bts = bts;
            var byte4 = new byte[4];
            var byte2 = new byte[2];
            var byte1 = new byte[1];
 
            //数据读取日期
            Array.Copy(instrution_bts, 13, byte4, 0, 4);
            var date = Bytes2Int4(byte4);
 
            //数据读取时间 
            Array.Copy(instrution_bts, 17, byte4, 0, 4);
            var time = Bytes2Int4(byte4);
 
            var hms = time.ToString();
            if (hms.Length < 6)
                hms = "0" + hms;
            var ttt = hms.Substring(0, 2) + ":" + hms.Substring(2, 2) + ":" + hms.Substring(4, 2);
            var strTime = $"{date} {ttt}";
            DateTime dataTime = DateTime.ParseExact(strTime.ToString(), "yyyyMMdd hh:mm:ss", System.Globalization.CultureInfo.CurrentCulture);
 
 
            //瞬时流量,m3/h 
            Array.Copy(instrution_bts, 21, byte4, 0, 4); 
            list.Add(new Model.MonitorDataDockingSrcRecord()
            {
                SignId = "1590274355240243200",
                SrcValue = Bytes2Double(byte4).ToString()
            });
 
            //瞬时压力,Mpa 
            Array.Copy(instrution_bts, 25, byte4, 0, 4);
            list.Add(new Model.MonitorDataDockingSrcRecord()
            {
                SignId = "1589903942416994304",
                SrcValue = Bytes2Double(byte4).ToString()
            });
 
            //瞬时流速,m3/h 
            Array.Copy(instrution_bts, 29, byte4, 0, 4);
            list.Add(new Model.MonitorDataDockingSrcRecord()
            {
                SignId = "1589904380742733824",
                SrcValue = Bytes2Double(byte4).ToString()
            });
 
            //正向累计整数 
            Array.Copy(instrution_bts, 33, byte4, 0, 4);
            var ForwardCumulativeFlowInteger = Bytes2Int4(byte4);
            //正向累计的小数/1000 
            Array.Copy(instrution_bts, 41, byte2, 0, 2);
            var ForwardCumulativeFlowDecimal = Bytes2Int32(byte2);
            //正向累积流量 
            list.Add(new Model.MonitorDataDockingSrcRecord()
            {
                SignId = "1589903863446638592",
                SrcValue = (ForwardCumulativeFlowInteger + ForwardCumulativeFlowDecimal * 0.001).ToString()
            });
 
            //反向累计整数 
            Array.Copy(instrution_bts, 37, byte4, 0, 4);
            var ReverseCumulativeFlowInteger = Bytes2Int4(byte4);
            //反向累计的小数 /1000 
            Array.Copy(instrution_bts, 43, byte2, 0, 2);
            var ReverseCumulativeFlowDecimal = Bytes2Int32(byte2);
            //反向累积流量 
            list.Add(new Model.MonitorDataDockingSrcRecord()
            {
                SignId = "1589904635718668288",
                SrcValue = (ReverseCumulativeFlowInteger + ReverseCumulativeFlowDecimal * 0.001).ToString()
            });
 
            //水表电池电量 
            Array.Copy(instrution_bts, 45, byte1, 0, 1);
            list.Add(new Model.MonitorDataDockingSrcRecord()
            {
                SignId = "1589906693322575872",
                SrcValue = Bytes2Int32(byte1).ToString()
            });
 
            //GPRS电池电量 
            Array.Copy(instrution_bts, 46, byte1, 0, 1);
            list.Add(new Model.MonitorDataDockingSrcRecord()
            {
                SignId = "1589906830698614784",
                SrcValue = Bytes2Int32(byte1).ToString()
            });
 
            //系统报警 
            Array.Copy(instrution_bts, 47, byte1, 0, 1);
            list.Add(new Model.MonitorDataDockingSrcRecord()
            {
                SignId = "1589905499946618880",
                SrcValue = Bytes2Int32(byte1).ToString()
            });
 
            //水表空管报警 
            Array.Copy(instrution_bts, 48, byte1, 0, 1);
            list.Add(new Model.MonitorDataDockingSrcRecord()
            {
                SignId = "1589905855032201216",
                SrcValue = Bytes2Int32(byte1).ToString()
            });
 
            //断励磁报警 
            Array.Copy(instrution_bts, 49, byte1, 0, 1);
            list.Add(new Model.MonitorDataDockingSrcRecord()
            {
                SignId = "1589905963715006464",
                SrcValue = Bytes2Int32(byte1).ToString()
            });
 
            //流量上线报警 
            Array.Copy(instrution_bts, 50, byte1, 0, 1);
            list.Add(new Model.MonitorDataDockingSrcRecord()
            {
                SignId = "1589906172004143104",
                SrcValue = Bytes2Int32(byte1).ToString()
            });
 
            //流量下线报警 
            Array.Copy(instrution_bts, 51, byte1, 0, 1);
            list.Add(new Model.MonitorDataDockingSrcRecord()
            {
                SignId = "1589906236487372800",
                SrcValue = Bytes2Int32(byte1).ToString()
            });
 
            //压力上限报警 
            Array.Copy(instrution_bts, 52, byte1, 0, 1);
            list.Add(new Model.MonitorDataDockingSrcRecord()
            {
                SignId = "1589906299754254336",
                SrcValue = Bytes2Int32(byte1).ToString()
            });
 
            //压力下限报警 
            Array.Copy(instrution_bts, 53, byte1, 0, 1);
            list.Add(new Model.MonitorDataDockingSrcRecord()
            {
                SignId = "1589906389663354880",
                SrcValue = Bytes2Int32(byte1).ToString()
            });
 
            return list;
        }
        
 
        public static int Bytes2Int4(byte[] src, int offset = 0)
        {
            src = src.Reverse().ToArray();
            int value;
            value = (int)(((src[offset] & 0xFF) << 24)
                    | ((src[offset + 1] & 0xFF) << 16)
                    | ((src[offset + 2] & 0xFF) << 8)
                    | (src[offset + 3] & 0xFF));
            return value;
        } 
        public static double Bytes2Double(byte[] value, int offset = 0)
        {
            return BitConverter.ToSingle(value.Reverse().ToArray(), 0);//采用了IEEE-754二进制浮点数算术标准
        } 
        public static int Bytes2Int32(byte[] bytes)
        {
            return Convert.ToInt32(Byte2HexStr(bytes.Reverse().ToArray()), 16); ;
        }
        
 
        /// <summary>
        /// byte[]转为16进制字符串
        /// </summary> 
        public static string Byte2HexStr(byte[] bytes)
        {
            string returnStr = "";
            if (bytes != null)
            {
                for (int i = 0; i < bytes.Length; i++)
                {
                    returnStr += bytes[i].ToString("X2");
                }
            }
            return returnStr;
        }
    }
}