wei
2020-12-13 271015c6e90c195103cc7f34eda87966acd74dcc
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
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
using System;
using System.Collections.Generic;
using System.Text;
using HDL_ON.Entity;
using HDL_ON.UI;
using Shared;
 
namespace HDL_ON.DriverLayer
{
    /// <summary>
    /// bus数据包
    /// </summary>
    public class Packet
    {
        /// <summary>
        /// 缓冲区大小
        /// </summary>
        public const int Size = 1024 + 200;
 
        /// <summary>
        /// 接收到的数据
        /// </summary>
        public byte[] Bytes;
 
        /// <summary>
        /// 数据发送IP地址
        /// </summary>
        public System.Net.EndPoint RemoteEndPoint;
 
        public Packet()
        {
            this.Bytes = new byte[Size];
            RemoteEndPoint = new System.Net.IPEndPoint(System.Net.IPAddress.Any, 0);
        }
        public Packet(byte[] data, System.Net.EndPoint remoteEndPoint)
        {
            this.Bytes = data;
            this.RemoteEndPoint = remoteEndPoint;
        }
 
        /// <summary>
        /// 记录已经发送数据出去的时间
        /// </summary>
        public DateTime FlagDateTime;
 
        /// <summary>
        /// 已经发送了多少数
        /// </summary>
        public int HaveSendCount;
 
        /// <summary>
        /// 处理接收到的数据
        /// </summary>
        public virtual void Manager()
        {
            try
            { 
                //对于操作数据库的时间比较长的,可以创建另一个线程处理
                if (!"HDLMIRACLE".Equals(Encoding.ASCII.GetString(Bytes, 4, 10)))
                {
                    return;
                }
 
                byte subnetID = this.Bytes[17]; //源子网号
                byte deviceID = this.Bytes[18]; //源设备号
 
                //源设备类型
                int deviceType = this.Bytes[19] * 256 + this.Bytes[20];
 
                Command command = (Command)(Bytes[21] * 256 + Bytes[22]); //操作码控制命令
 
                byte targetSubnetID = this.Bytes[23];
                byte targetDeviceID = this.Bytes[24];
 
                //不是要接收的指令就返回
                if (!((targetSubnetID == 0 && targetDeviceID == 252) || (targetSubnetID == 0xff && targetDeviceID == 0xff)))
                {
                    return;
                }
                byte[] usefulBytes = null;
                if (this.Bytes[16] == 0xFF)
                {
                    usefulBytes = new byte[Bytes.Length - 16 - 11];
                    Array.Copy(Bytes, 27, usefulBytes, 0, usefulBytes.Length);
                }
                else
                {
                    //有用的附加数据
                    usefulBytes = new byte[this.Bytes[16] - 11];
                    Array.Copy(Bytes, 25, usefulBytes, 0, usefulBytes.Length);
                }
                //处理接收到的数据
                UdpPacket_DataProcessing(subnetID, deviceID, command, usefulBytes);
                //处理是否要重发数据
                ManagerReceive(subnetID, deviceID, command, usefulBytes);
            }
            catch (Exception ex)
            {
                MainPage.Log($"packet {ex.Message} ");
            }
        }
 
        /// <summary>
        /// 数据包处理
        /// ps:由commonpage转移过来,还需要转移合适的位置管理
        /// </summary>
        /// <param name="subnetID"></param>
        /// <param name="deviceID"></param>
        /// <param name="command"></param>
        /// <param name="receiveBytes"></param>
        void UdpPacket_DataProcessing(byte subnetID, byte deviceID, Command command, byte[] receiveBytes)
        {
            try
            {
                switch (command)
                {
                    case Command.SetSingleLightACK:
                        var queryList = new List<Function>();
                        queryList.AddRange(FunctionList.List.electricals);
                        queryList.AddRange(FunctionList.List.lights);
                        foreach (var updataObj in queryList)
                        {
                            if (updataObj.GetBusId() == subnetID + "_" + deviceID + "_" + receiveBytes[0])
                            {
                                if (updataObj.functionType != FunctionType.RGB)
                                {
                                    updataObj.trait_on_off.curValue = receiveBytes[2] > 0 ? "on" : "off";
                                    if (updataObj.trait_on_off.curValue.ToString() == "on")
                                    {
                                        switch (updataObj.functionType)
                                        {
                                            case FunctionType.Fan:
                                                (updataObj as Fan).openLevel = receiveBytes[2];
                                                updataObj.lastState = Language.StringByID(StringId.Level) + " : " + receiveBytes[2];
                                                break;
                                            case FunctionType.Dimmer:
                                                (updataObj as Light).brightness = receiveBytes[2];
                                                updataObj.lastState = Language.StringByID(StringId.Brightness) + " : " + receiveBytes[2] + "%";
                                                break;
                                        }
                                    }
                                    HomePage.UpdataFunctionStates(updataObj);
                                    RoomPage.UpdataStates(updataObj);
                                    FunctionPage.UpdataStates(updataObj);
                                    ClassificationPage.UpdataInfo(updataObj);
                                    switch (updataObj.functionType)
                                    {
                                        case FunctionType.Relay:
                                            RelayPage.UpdataState(updataObj as Light);
                                            break;
                                        case FunctionType.Dimmer:
                                            DimmerPage.UpdataStates(updataObj as Light);
                                            break;
                                        case FunctionType.Fan:
                                            FanPage.UpdataState(updataObj as Fan);
                                            break;
                                    }
                                    break;
                                }
                            }
                        }
                        break;
                    case Command.ReadLightAllLoopBrightnessACK:
                        for (int i = 0; i < receiveBytes[0]; i++)
                        {
                            var light = FunctionList.List.lights.Find((obj) => obj.bus_Data.SubnetID == subnetID && obj.bus_Data.DeviceID == deviceID && obj.bus_Data.loopId == (i + 1));
                            if (light != null)
                            {
                                if (light.functionType != FunctionType.RGB)
                                {
                                    light.trait_on_off.curValue = receiveBytes[light.bus_Data.loopId] == 0 ? "off" : "on";
                                    if (light.trait_on_off.curValue.ToString() == "on")
                                    {
                                        light.brightness = receiveBytes[2];
                                        light.lastState = Language.StringByID(StringId.Brightness) + " : " + receiveBytes[2] + "%";
                                    }
                                    HomePage.UpdataFunctionStates(light);
                                    RoomPage.UpdataStates(light);
                                    FunctionPage.UpdataStates(light);
                                    ClassificationPage.UpdataInfo(light);
                                    switch (light.functionType)
                                    {
                                        case FunctionType.Relay:
                                            RelayPage.UpdataState(light);
                                            break;
                                        case FunctionType.Dimmer:
                                            DimmerPage.UpdataStates(light);
                                            break;
                                    }
                                }
                            }
                            else
                            {
                                var e = FunctionList.List.electricals.Find((obj) => obj.bus_Data.SubnetID == subnetID && obj.bus_Data.DeviceID == deviceID && obj.bus_Data.loopId == i);
                                if (e != null)
                                {
                                    var fan = e as Fan;
                                    fan.trait_on_off.curValue = receiveBytes[2] == 0 ? "off" : "on";
                                    if (fan.trait_on_off.curValue.ToString() == "on")
                                    {
                                        fan.openLevel = receiveBytes[2];
                                        fan.lastState = Language.StringByID(StringId.Level) + " : " + receiveBytes[2];
                                    }
                                    HomePage.UpdataFunctionStates(fan);
                                    RoomPage.UpdataStates(fan);
                                    FunctionPage.UpdataStates(fan);
                                    ClassificationPage.UpdataInfo(fan);
                                    switch (fan.functionType)
                                    {
                                        case FunctionType.Fan:
                                            FanPage.UpdataState(fan);
                                            break;
                                        case FunctionType.Socket:
 
                                            break;
                                    }
                                }
                            }
                        }
                        break;
                    case Command.SetLogicLoopColorACK:
                    case Command.ReadLogicLoopColorACK:
                        foreach (var rgb in FunctionList.List.lights)
                        {
                            if (rgb.GetBusId() == subnetID + "_" + deviceID + "_" + receiveBytes[0])
                            {
                                if (rgb.functionType == FunctionType.RGB)
                                {
                                    rgb.trait_on_off.curValue = receiveBytes[1] > 0 ? "on" : "off";
                                    if (receiveBytes[1] > 0)
                                    {
                                        rgb.brightness = receiveBytes[1];
                                        rgb.lastState = Language.StringByID(StringId.Brightness) + " : " + receiveBytes[1] + "%";
                                    }
                                    rgb.redColor = receiveBytes[6];
                                    rgb.greenColor = receiveBytes[7];
                                    rgb.blueColor = receiveBytes[8];
 
                                    HomePage.UpdataFunctionStates(rgb);
                                    RoomPage.UpdataStates(rgb);
                                    FunctionPage.UpdataStates(rgb);
                                    ClassificationPage.UpdataInfo(rgb);
                                    RGBPage.UpdataStates(rgb);
                                }
                            }
                        }
                        break;
                    case Command.SetCurtainModelStutasACK:
                    case Command.ReadCurtainStutasACK:
                        foreach (var curtain in FunctionList.List.curtains)
                        {
                            if (curtain.bus_Data.SubnetID == subnetID && curtain.bus_Data.DeviceID == deviceID)
                            {
                                if (receiveBytes[0] == 17)
                                {
                                    if (receiveBytes[1] > 1)
                                    {
                                        curtain.trait_on_off.curValue = "on";
                                    }
                                    else
                                    {
                                        curtain.trait_on_off.curValue = "off";
                                    }
                                    curtain.percent = receiveBytes[1];
                                    curtain.lastState = Language.StringByID(StringId.Open) + curtain.percent + "%";
                                }
                                else
                                {
                                    if (curtain.bus_Data.loopId != receiveBytes[0])
                                        continue;
                                    switch (receiveBytes[1])
                                    {
                                        case 0:
                                            curtain.trait_on_off.curValue = "stop";
                                            break;
                                        case 1:
                                            curtain.trait_on_off.curValue = "on";
                                            curtain.lastState = Language.StringByID(StringId.Open);
                                            break;
                                        case 2:
                                            curtain.trait_on_off.curValue = "off";
                                            curtain.lastState = Language.StringByID(StringId.Close);
                                            break;
                                    }
                                }
                                RoomPage.UpdataStates(curtain);
                                FunctionPage.UpdataStates(curtain);
                                HomePage.UpdataFunctionStates(curtain);
                                ClassificationPage.UpdataInfo(curtain);
                                switch (curtain.functionType)
                                {
                                    case FunctionType.Curtain:
                                        CurtainModulePage.UpdataState(curtain);
                                        break;
                                    case FunctionType.MotorCurtain:
                                        MotorCurtainPage.UpdataState(curtain);
                                        break;
                                    case FunctionType.RollingShutter:
                                        RollingShutterPage.UpdataState(curtain);
                                        break;
                                }
                            }
                        }
                        break;
                    case Command.SetACModeACK:
                    case Command.ReadACModeACK:
                        foreach (var ac in FunctionList.List.aCs)
                        {
                            if (ac.GetBusId() == subnetID + "_" + deviceID + "_" + receiveBytes[0])
                            {
                                ac.curTempType = receiveBytes[1];
                                ac.indoorTemp = receiveBytes[2];
                                ac.trait_on_off.curValue = receiveBytes[8] == 1 ? "on" : "off";
                                ac.curModeIndex = receiveBytes[9];
                                ac.curFanIndex = receiveBytes[10];
                                ac.trait_temp.curValue = receiveBytes[11];
                                ac.lastState = "";
                                switch (ac.trait_mode.curValue.ToString())
                                {
                                    case "cool":
                                        ac.lastState = Language.StringByID(StringId.Cool);
                                        break;
                                    case "heat":
                                        ac.lastState = Language.StringByID(StringId.Heat);
                                        break;
                                    case "dry":
                                        ac.lastState = Language.StringByID(StringId.Dry);
                                        break;
                                    case "auto":
                                        ac.lastState = Language.StringByID(StringId.Auto);
                                        break;
                                    case "fan":
                                        ac.lastState = Language.StringByID(StringId.AirSupply);
                                        break;
                                }
                                switch (ac.trait_fan.curValue.ToString())
                                {
                                    case "high":
                                        ac.lastState += " " + Language.StringByID(StringId.HighWindSpeed);
                                        break;
                                    case "medium":
                                        ac.lastState += " " + Language.StringByID(StringId.MiddleWindSpeed);
                                        break;
                                    case "low":
                                        ac.lastState += " " + Language.StringByID(StringId.LowWindSpeed);
                                        break;
                                    case "auto":
                                        ac.lastState += " " + Language.StringByID(StringId.Auto);
                                        break;
                                }
                                ac.lastState += " " + ac.trait_temp.curValue + ac.tempUnitString;
                                RoomPage.UpdataStates(ac);
                                FunctionPage.UpdataStates(ac);
                                HomePage.UpdataFunctionStates(ac);
                                ClassificationPage.UpdataInfo(ac);
                                ACPage.UpdataStates(ac);
                            }
                        }
                        break;
                    case Command.ReadFloorHeatACK:
                    case Command.SetFloorHeatACK:
                        foreach (var fh in FunctionList.List.floorHeatings)
                        {
                            if (fh.GetBusId() == subnetID + "_" + deviceID + "_" + receiveBytes[0])
                            {
                                fh.curTempType = receiveBytes[2];
                                fh.trait_on_off.curValue = receiveBytes[1] % 2 == 0 ? "off" : "on";
                                fh.curModeIndex = receiveBytes[3];
                                if (fh.modeTemp.ContainsKey("normal"))
                                {
                                    fh.modeTemp["normal"] = receiveBytes[4];
                                }
                                else
                                {
                                    fh.modeTemp.Add("normal", receiveBytes[4]);
                                }
                                if (fh.modeTemp.ContainsKey("day"))
                                {
                                    fh.modeTemp["day"] = receiveBytes[5];
                                }
                                else
                                {
                                    fh.modeTemp.Add("day", receiveBytes[5]);
                                }
                                if (fh.modeTemp.ContainsKey("night"))
                                {
                                    fh.modeTemp["night"] = receiveBytes[6];
                                }
                                else
                                {
                                    fh.modeTemp.Add("night", receiveBytes[6]);
                                }
                                if (fh.modeTemp.ContainsKey("away"))
                                {
                                    fh.modeTemp["away"] = receiveBytes[7];
                                }
                                else
                                {
                                    fh.modeTemp.Add("away", receiveBytes[7]);
                                }
 
                                switch (fh.trait_mode.curValue)
                                {
                                    case "normal":
                                        fh.lastState = Language.StringByID(StringId.Normal);
                                        fh.trait_temp.curValue = receiveBytes[4];
                                        break;
                                    case "day":
                                        fh.lastState = Language.StringByID(StringId.Day);
                                        fh.trait_temp.curValue = receiveBytes[5];
                                        break;
                                    case "night":
                                        fh.lastState = Language.StringByID(StringId.Night);
                                        fh.trait_temp.curValue = receiveBytes[6];
                                        break;
                                    case "timer":
                                        fh.lastState = Language.StringByID(StringId.Auto);
                                        if (receiveBytes[8] == 0)
                                        {
                                            fh.timeFlag = 0;
                                            fh.trait_temp.curValue = receiveBytes[5];
                                        }
                                        else
                                        {
                                            fh.timeFlag = 1;
                                            fh.trait_temp.curValue = receiveBytes[6];
                                        }
                                        break;
                                    case "away":
                                        fh.trait_temp.curValue = receiveBytes[7];
                                        fh.lastState = Language.StringByID(StringId.Away);
                                        break;
                                }
                                fh.lastState += " " + fh.trait_temp.curValue + fh.tempUnitString;
                                RoomPage.UpdataStates(fh);
                                FunctionPage.UpdataStates(fh);
                                HomePage.UpdataFunctionStates(fh);
                                ClassificationPage.UpdataInfo(fh);
                                FloorHeatingPage.UpdataStates(fh);
                            }
                        }
                        break;
                    case Command.ReadDeviceLoopInfoACK:
                        FunctionType dt = (FunctionType)(11 * 256 + receiveBytes[1]);
                        string tag = receiveBytes[1] + "_" + subnetID + "_" + deviceID + "_" + receiveBytes[2];
                        foreach (var sensor in FunctionList.List.sensorsEnvironmentalScience)
                        {
                            if (sensor.bus_Data != null)
                            {
                                if ((int)sensor.functionType % 256 == receiveBytes[1] && sensor.bus_Data.SubnetID == subnetID &&
                                    sensor.bus_Data.DeviceID == deviceID && sensor.bus_Data.loopId == receiveBytes[2])
                                {
                                    switch (dt)
                                    {
                                        case FunctionType.Temp:
                                            byte[] tempBytes = new byte[] { receiveBytes[24], receiveBytes[25], receiveBytes[26], receiveBytes[27] };
                                            sensor.values = Math.Round(BitConverter.ToSingle(tempBytes, 0), 1);
                                            break;
                                        case FunctionType.Humidity:
                                            sensor.values = Convert.ToDouble(receiveBytes[24] * 256 + receiveBytes[25]) / 10;
                                            break;
                                        case FunctionType.TVOC:
                                            sensor.values = Convert.ToDouble(receiveBytes[24] * 256 + receiveBytes[25]) / 100;
                                            break;
                                        case FunctionType.PM25:
                                            sensor.values = Convert.ToInt32(receiveBytes[24] * 256 + receiveBytes[25]);
                                            break;
                                        case FunctionType.CO2:
                                            sensor.values = Convert.ToInt32(receiveBytes[24] * 256 + receiveBytes[25]);
                                            break;
                                    }
                                    EnvironmentalSciencePage.LoadEvent_UpdataStatus(sensor);
                                }
                            }
                        }
                        break;
                    case Command.New_Analog_Quantity_BROADCAST:
                        string tag1 = receiveBytes[1] + "_" + subnetID + "_" + deviceID + "_" + receiveBytes[2];
                        foreach (var sensor in FunctionList.List.sensorsEnvironmentalScience)
                        {
                            if (sensor.bus_Data != null)
                            {
                                if ((int)sensor.functionType % 256 == receiveBytes[1] && sensor.bus_Data.SubnetID == subnetID &&
                                    sensor.bus_Data.DeviceID == deviceID && sensor.bus_Data.loopId == receiveBytes[2])
                                {
                                    //0保留 1无符号4Byte整形  2有符号4Byte整形  3Float形(代±)
                                    switch (receiveBytes[3])
                                    {
                                        case 1:
                                            sensor.values = (receiveBytes[5] * 256 * 256 * 256) + (receiveBytes[6] * 256 * 256) + (receiveBytes[7] * 256) + receiveBytes[8];
                                            break;
                                        case 2:
                                            sensor.values = -1 * ((receiveBytes[5] * 256 * 256 * 256) + (receiveBytes[6] * 256 * 256) + (receiveBytes[7] * 256) + receiveBytes[8]);
                                            break;
                                        case 3:
                                            byte[] tempBytes = new byte[] { receiveBytes[5], receiveBytes[6], receiveBytes[7], receiveBytes[8] };
                                            sensor.values = Math.Round(BitConverter.ToSingle(tempBytes, 0), 1);
                                            break;
                                    }
                                    switch (receiveBytes[4])
                                    {
                                        case 2:
                                            if (receiveBytes[1] == 5)//TVOC需求除以100000
                                                sensor.values /= 100000;
                                            break;
                                    }
                                    EnvironmentalSciencePage.LoadEvent_UpdataStatus(sensor);
                                }
                            }
                        }
                        break;
                    case Command.InstructionPanelKeyACK:
                    case Command.ReadInstructionPanelKeyACK:
                        byte reACPanel = 0;
                        if (receiveBytes.Length == 2)
                        {
                            reACPanel = 1;
                        }
                        else if (receiveBytes.Length == 3)
                        {
                            reACPanel = receiveBytes[2];
                        }
                        else
                        {
                            break;
                        }
                        foreach (var ac in FunctionList.List.aCs)
                        {
                            if (ac.GetBusId() == subnetID + "_" + deviceID + "_" + reACPanel)
                            {
                                switch (receiveBytes[0])
                                {
                                    case 3://
                                        ac.trait_on_off.curValue = receiveBytes[1] == 1 ? "on" : "off";
                                        break;
                                    case 4:
                                    case 7:
                                    case 8:
                                    case 19:
                                        ac.trait_temp.curValue = receiveBytes[1];
                                        break;
                                    case 5:
                                        ac.curFanIndex = receiveBytes[1];
                                        break;
                                    case 6:
                                        ac.curModeIndex = receiveBytes[1];
                                        break;
 
                                }
                                ac.lastState = "";
                                ac.lastState += " " + ac.trait_temp.curValue + ac.tempUnitString;
                                RoomPage.UpdataStates(ac);
                                FunctionPage.UpdataStates(ac);
                                HomePage.UpdataFunctionStates(ac);
                                ClassificationPage.UpdataInfo(ac);
                                ACPage.UpdataStates(ac);
                            }
                        }
                        break;
                    case Command.ReadPanleTempACK://1944
                    case Command.PanleBroadcastTemp:
                        foreach (var ac in FunctionList.List.aCs)
                        {
                            if (ac.GetBusId() == subnetID + "_" + deviceID + "_" + receiveBytes[0])
                            {
                                ac.indoorTemp = receiveBytes[1];
                                FunctionPage.UpdataStates(ac);
                            }
                        }
                        break;
                    case Command.ReadGatewayACK:
                        var mac = ByteToHex16(receiveBytes[5]) + ByteToHex16(receiveBytes[6]) + ByteToHex16(receiveBytes[7]) + ByteToHex16(receiveBytes[8]) + ByteToHex16(receiveBytes[9]) + ByteToHex16(receiveBytes[10]) + ByteToHex16(receiveBytes[11]) + ByteToHex16(receiveBytes[12]);
                        var Name = Encoding.GetEncoding("gb2312").GetString(receiveBytes, 13, 20).Trim('\0'); ;
                        MainPage.Log($"name : {Name} ; mac : {mac}");
                        if (DB_ResidenceData.Instance.residenceGatewayMAC == mac)
                        {
                            DriverLayer.Control.Ins.GatewayOnline = true;
                            DriverLayer.Control.Ins.IsRemote = false;
                            DAL.Mqtt.MqttClient.DisConnectRemote();//断开mqtt
                        }
                        break;
                }
            }
            catch (Exception ex)
            {
                MainPage.Log($"Bus Rev Erorr : {ex.Message}");
            }
        }
        /// <summary>
        /// byte转16进制字符串
        /// </summary>
        /// <param name="b"></param>
        /// <returns></returns>
        string ByteToHex16(byte b)
        {
            string s = Convert.ToString(b, 16).ToUpper();
            if (s.Length <= 1)
            {
                return "0" + s;
            }
            return s;
        }
 
        /// <summary>
        /// 处理接收回来的数据
        /// </summary>
        void ManagerReceive(byte subnetID, byte deviceID, Command command, byte[] usefulBytes)
        {
            try
            {
                string receiveFlag = string.Format("{0},{1},{2},", subnetID, deviceID, (int)command);
 
                switch (command)
                {
                    case Command.SetSingleLightACK:
                        receiveFlag += string.Format("{0}", usefulBytes[0]);
                        break;
                    case Command.SetLogicLoopColorACK:
                        receiveFlag += string.Format("{0},{1},{2}", usefulBytes[0], usefulBytes[1], usefulBytes[2]);
                        break;
                    case Command.ReadLogicLoopColorACK:
                    case Command.ReadACModeACK:
                    case Command.SetACModeACK:
                    case Command.ReadFloorHeatACK:
                    case Command.SetFloorHeatACK:
                        receiveFlag += string.Format("{0}", usefulBytes[0]);
                        break;
                    case Command.ReadLightAllLoopBrightnessACK:
                        receiveFlag += "";
                        break;
                    case Command.ReadGatewayACK:
                        receiveFlag = string.Format("{0},{1}", usefulBytes[0], usefulBytes[1]);
                        break;
                    case Command.ReadDeviceLoopInfoACK:
                        if (usefulBytes[0] == 1)
                        {
                            receiveFlag += string.Format("{0},{1}", usefulBytes[0], usefulBytes[2]); ;
                        }
                        else
                            receiveFlag += string.Format("{0},{1},{2}", usefulBytes[0], usefulBytes[1], usefulBytes[2]);
                        break;
                    case Command.InstructionPanelKeyACK:
                    case Command.ReadInstructionPanelKeyACK:
                        receiveFlag += string.Format("{0},{1}", usefulBytes[0], usefulBytes[1]);
                        break;
                    case Command.ReadRemark:
                        DriverLayer.Control.Ins.myUdp.ReceiveReadRemark(usefulBytes);
                        break;
                    default:
                        break;
                }
                DriverLayer.Control.Ins.myUdp.ReceiveRepeatManager(receiveFlag);
            }
            catch (Exception ex)
            {
                MainPage.Log("ManagerReceive抛出异常:" + ex.ToString());
            }
        }
 
 
    }
}