JLChen
2020-04-15 19545253f1977af3ef84b301183c2e12107983c9
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
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
using System;
using System.Collections.Generic;
using System.Text;
using Newtonsoft.Json.Linq;
using Shared.IO;
using Shared.Net;
using Shared.SimpleControl.Phone;
 
namespace Shared.SimpleControl
{
    public static class CommonPage
    {
        public static bool IsRemote;
        public static Action RefreshAir;
        public static bool FindGateway = false;
        public static bool FindGatewayChilren = false;
        public static string FindGatewayChilrenIPAddress = new Net.NetWiFi ().BroadcastIpAddress.ToString ();
        public static bool LocalPhoneFindDevice = false;
        private static string ip = "0.0.0.0";
        public static System.Net.IPEndPoint EndPoint {
            get {
                try {
                    if (FindGateway) {
                        return new System.Net.IPEndPoint (System.Net.IPAddress.Parse (new Net.NetWiFi ().BroadcastIpAddress.ToString ()), 6000);
                        //return new System.Net.IPEndPoint (System.Net.IPAddress.Parse ("224.0.168.188"), 6000);
                    } else if (FindGatewayChilren) {
                        try {
                            return new System.Net.IPEndPoint (System.Net.IPAddress.Parse (FindGatewayChilrenIPAddress), 6000);
                        } catch {
                            return new System.Net.IPEndPoint (System.Net.IPAddress.Parse ("224.0.168.188"), 6000);
                        }
                    } else {
                        return new System.Net.IPEndPoint (System.Net.IPAddress.Parse (new Net.NetWiFi ().BroadcastIpAddress.ToString ()), 6000);
                    }
                } catch {
                    //防止异常导致程序退出
                    return new System.Net.IPEndPoint (System.Net.IPAddress.Parse ("127.0.0.1"), 6000);
                }
            }
        }
 
        ///保存设备备注才用gb2312,其他情况用utf8
        public static Encoding MyEncodingUTF8 = Encoding.UTF8;//Get
        public static Encoding MyEncodingGB2312 {
            get {
                try {
                    if (System.Globalization.CultureInfo.InstalledUICulture.EnglishName.ToUpper().StartsWith("CZECH")) {
                        return System.Text.Encoding.GetEncoding (1250);
                    } else {
                        return Encoding.GetEncoding ("gb2312");
                    }
                }catch (Exception ex) {
                    Console.WriteLine ("MyEncodingGB2312 Erorr : "+ex.Message);
                    return Encoding.GetEncoding ("gb2312");
                }
            }
        }
        static bool isHttpListenerStart;
        public static DateTime dt;
 
        //public static byte currentSubnetID = 0;
        public static void InitHttpListener ()
        {
            if (isHttpListenerStart) {
                return;
            }
            HttpListener.Start (new NetWiFi ().IpAddress, 6001);
            HttpListener.EventHandler -= httpListener_EventHandler;
            HttpListener.EventHandler += httpListener_EventHandler;
            isHttpListenerStart = true;
        }
        public static void CloseHttpListener ()
        {
            //isHttpListenerStart = false;
            //return;
            if (!isHttpListenerStart)
                return;
            HttpListener.Close ();
            isHttpListenerStart = false;
        }
        /// <summary>
        /// 初始化处理socket接收的数据 
        /// </summary>
        public static void InitReceiveEvent ()
        {
            Packet.ReceiveEvent += Packet_ReceiveEvent;
        }
 
        public static float floatChange (byte b1, byte b2, byte b3, byte b4)
        {
            byte [] byteTemp = new byte [4] { b4, b3, b2, b1 };
            return BitConverter.ToSingle (byteTemp, 0);
        }
 
 
        static void Packet_ReceiveEvent (byte subnetID, byte deviceID, Command command, byte [] usefullBytes, string revGatewayIP)
        {
 
            try {
                List<byte> ButtonBkeyModelList = new List<byte> ();
                switch (command) {
                case Command.FreshAirReadACK:
                case Command.FreshAirControlACK:
                    foreach (var room in Room.Lists) {
                        var common = room.DeviceList.Find ((obj) => obj.SubnetID == subnetID && obj.DeviceID == deviceID && obj.LoopID == usefullBytes [0] && obj.Type == DeviceType.FreshAir);
                        if (common != null) {
                            var fresAirReceive = common as FreshAir;
                            fresAirReceive.SwitchStatus = usefullBytes [1];
                            fresAirReceive.WindSpeed = usefullBytes [2];
                            fresAirReceive.SetPattern = usefullBytes [3];
                            fresAirReceive.EnableValue1 = usefullBytes [4];
                            fresAirReceive.EnableValue2 = usefullBytes [5];
                            if (usefullBytes.Length > 6) {
                                fresAirReceive.InTemp = floatChange (usefullBytes [6], usefullBytes [7], usefullBytes [8], usefullBytes [9]);
                                fresAirReceive.OutTemp = floatChange (usefullBytes [10], usefullBytes [11], usefullBytes [12], usefullBytes [13]);
                                fresAirReceive.Humidity = floatChange (usefullBytes [14], usefullBytes [15], usefullBytes [16], usefullBytes [17]);
                                fresAirReceive.PM25 = floatChange (usefullBytes [18], usefullBytes [19], usefullBytes [20], usefullBytes [21]);
                                fresAirReceive.TVOC = floatChange (usefullBytes [22], usefullBytes [23], usefullBytes [24], usefullBytes [25]);
                                fresAirReceive.CO2 = floatChange (usefullBytes [26], usefullBytes [27], usefullBytes [28], usefullBytes [29]);
                            }
                            IO.FileUtils.SaveEquipmentMessage (fresAirReceive, fresAirReceive.LoopID.ToString ());
                            UserFresAirList.UpdateStatus (fresAirReceive);
                            UserRoom.UpdataDeviceStatus (fresAirReceive);
                            UserFreshAirPage.UpdateStatus (fresAirReceive);
                        }
                    }
                    break;
                case Command.SetArmACK:
                case Command.ReadArmACK:
                    if (Application.IsPad) { } else {
                        SecurityModul sm = new SecurityModul () {
                            SubnetID = subnetID,
                            DeviceID = subnetID,
                            LoopID = usefullBytes [0],
                            AreaType = usefullBytes [1],
                        };
                        UserSecurityAreaPage.UpdateStatus (sm);
                    }
                    break;  
                case Command.ReadLightEquipmentAllLoopBrightnessACK://18145611909
                    for (byte i = 1; i <= usefullBytes [0]; i++) {
                        bool updateLightStatus = true;
                        foreach (var room in Room.Lists) {
                            Common common = null;
                            var commons = room.DeviceList.FindAll ((obj) => obj.SubnetID == subnetID && obj.DeviceID == deviceID);
                            if (commons.Count > 0) {
                                foreach (var c in commons) {
                                    if (c.Type == DeviceType.LightMixDimming) {
                                        var rcuDimming = (c as LightMixDimming);
                                        if (rcuDimming.SubnetID == subnetID && rcuDimming.DeviceID == deviceID && rcuDimming.PhysicsLoopID == i) {
                                            if (rcuDimming.CurrentBrightness == usefullBytes [i]) {
                                                updateLightStatus = false;
                                            }
                                            rcuDimming.CurrentBrightness = usefullBytes [i];
                                            common = rcuDimming;
                                        }
                                    }
                                    if (c.Type == DeviceType.LightMixSwitch) {
                                        var rcuSwitch = (c as LightMixSwitch);
                                        if (rcuSwitch.SubnetID == subnetID && rcuSwitch.DeviceID == deviceID && rcuSwitch.PhysicsLoopID == i) {
                                            if (rcuSwitch.CurrentBrightness == usefullBytes [i]) {
                                                updateLightStatus = false;
                                            }
                                            rcuSwitch.CurrentBrightness = usefullBytes [i];
                                            common = rcuSwitch;
                                        }
                                    }
                                }
                            }
                            if (common == null)
                                common = room.DeviceList.Find ((obj) => obj.SubnetID == subnetID && obj.DeviceID == deviceID && obj.LoopID == i);
                            if (common != null) { 
                                //foreach (var common in room.DeviceList) {
                                //if (common.SubnetID != subnetID || common.DeviceID != deviceID || common.LoopID != i) {
                                //    continue;
                                //}
                                common.LastUpdateTime = DateTime.Now;
                                if (common.Type == DeviceType.LightSwitch || common.Type == DeviceType.LightSwitchSocket || common.Type == DeviceType.LightEnergySwitch) {
                                    if ((common as LightSwitch).CurrentBrightness == usefullBytes [i]) {
                                        updateLightStatus = false;
                                    }
                                    (common as LightSwitch).CurrentBrightness = usefullBytes [i];
                                } else if (common.Type == DeviceType.LightDimming) {
                                    if ((common as LightDimming).CurrentBrightness == usefullBytes [i]) {
                                        updateLightStatus = false;
                                    }
                                    (common as LightDimming).CurrentBrightness = usefullBytes [i];
                                } else if (common.Type == DeviceType.FanModule) {
                                    if ((common as FanModule).Switch == usefullBytes [i]) {
                                        updateLightStatus = false;
                                    }
                                    (common as FanModule).Switch = usefullBytes [i];
                                    if (usefullBytes [i] != 0)
                                        (common as FanModule).WindSpeed = usefullBytes [i];
                                } else if (common.Type == DeviceType.LightDALI) {
                                    if ((common as LightDALI).CurrentBrightness == usefullBytes [i]) {
                                        updateLightStatus = false;
                                    }
                                    (common as LightDALI).CurrentBrightness = usefullBytes [i];
                                }
                                string updateFlag = common.CommonLoopID;
                                if (Application.IsPad) {
                                    //Pad.UserLightPage.UpdateStatus (updateFlag, usefullBytes [i]);
                                    //Pad.UserDeviceToLight.UpdateStatus ();
                                    //Pad.UserDeviceToLight.UpdateAllLightsStatus (updateFlag, usefullBytes [i]);
                                    //Pad.UserDeviceToSocket.UpdateStatus (updateFlag, usefullBytes [i]);
                                    //Pad.UserFan.UpdataFanView (updateFlag, usefullBytes [i]);
                                } else {
                                    UserDeviceToLight.UpdateBrighingCount (updateFlag);
                                    if (updateLightStatus) {
                                        UserRoom.UpdataDeviceStatus (common);
                                        UserLightPage.UpdateStatus (updateFlag, usefullBytes [i]);
                                        UserDeviceToLight.UpdateStatus (updateFlag, usefullBytes [i]);
                                        UserFan.UpdataFanView (updateFlag, usefullBytes [i]);
                                        UserDeviceToFan.UpdataStatus (updateFlag, usefullBytes [i]);
                                        UserDeviceToSocket.UpdateStatus (updateFlag, usefullBytes [i]);
                                    }
                                }
                            }
                        }
                    }
                    break;
                case Command.ReadLightSingleLoopBrightnessACK:
                case Command.SetSingleLightACK:
                    Console.WriteLine ($"SetSingleLightACK {usefullBytes [2]}");
                    foreach (var room in Room.Lists) {
                        var common = room.DeviceList.Find ((obj) => obj.CommonLoopID == subnetID.ToString () + "_" + deviceID.ToString () + "_" + usefullBytes [0].ToString ());
                        if (common != null) {
                            //foreach (var common in room.DeviceList) {
                            if (common.CommonLoopID != subnetID.ToString () + "_" + deviceID.ToString () + "_" + usefullBytes [0].ToString ()) {
                                continue;
                            }
                            string updateFlag = common.CommonLoopID;
                            if (common.Type == DeviceType.LightRGB) {
                                break;
                            }
                            common.LastUpdateTime = DateTime.Now;
                            bool hadBeUpdate = true;
                            if (common.Type == DeviceType.LightSwitch || common.Type == DeviceType.LightSwitchSocket || common.Type == DeviceType.LightSwitchSocket) {
                                if ((common as LightSwitch).CurrentBrightness == usefullBytes [2])
                                    hadBeUpdate = false;
                                (common as LightSwitch).CurrentBrightness = usefullBytes [2];
                            } else if (common.Type == DeviceType.LightDimming) {
                                if ((common as LightDimming).CurrentBrightness == usefullBytes [2])
                                    hadBeUpdate = false;
                                (common as LightDimming).CurrentBrightness = usefullBytes [2];
                            } else if (common.Type == DeviceType.FanModule) {
                                if ((common as FanModule).Switch == usefullBytes [2])
                                    hadBeUpdate = false;
                                (common as FanModule).Switch = usefullBytes [2];
                                if (usefullBytes [2] != 0)
                                    (common as FanModule).WindSpeed = usefullBytes [2];
                            } else if (common.Type == DeviceType.LightMixSwitch) {
                                if ((common as LightMixSwitch).CurrentBrightness == usefullBytes [2])
                                    hadBeUpdate = false;
                                (common as LightMixSwitch).CurrentBrightness = usefullBytes [2];
                            } else if (common.Type == DeviceType.LightMixDimming) {
                                if ((common as LightMixDimming).CurrentBrightness == usefullBytes [2])
                                    hadBeUpdate = false;
                                (common as LightMixDimming).CurrentBrightness = usefullBytes [2];
                            } else if (common.Type == DeviceType.LightDALI) {
                                if ((common as LightDALI).CurrentBrightness == usefullBytes [2])
                                    hadBeUpdate = false;
                                (common as LightDALI).CurrentBrightness = usefullBytes [2];
                            }
 
 
                            if (Application.IsPad) {
                                //Pad.UserLightPage.UpdateStatus (updateFlag, usefullBytes [2]);
                                //Pad.UserDeviceToLight.UpdateStatus ();
                                //Pad.UserDeviceToLight.UpdateAllLightsStatus (updateFlag, usefullBytes [2]);
                                //Pad.UserDeviceToSocket.UpdateStatus (updateFlag, usefullBytes [2]);
                                //Pad.UserFan.UpdataFanView (updateFlag, usefullBytes [2]);
                                //break;
                            } else {
                                UserDeviceToLight.UpdateBrighingCount (updateFlag);
                                if (hadBeUpdate) {
                                    UserRoom.UpdataDeviceStatus (common);
                                    UserLightPage.UpdateStatus (updateFlag, usefullBytes [2]);
                                    UserDeviceToLight.UpdateStatus (updateFlag, usefullBytes [2]);
                                    UserFan.UpdataFanView (updateFlag, usefullBytes [2]);
                                    UserDeviceToFan.UpdataStatus (updateFlag, usefullBytes [2]);
                                    UserDeviceToSocket.UpdateStatus (updateFlag, usefullBytes [2]);
                                }
                                //break;
                            }
                        }
                    }
                    break;
                case Command.SetSceneACK:
                    /// [0]// 区号1-n, 255表示所有区(用于广播场景)     1byte                     /// [1]// 场号0-x                 1byte
                    /// [2]// 回路总数n                1byte
                    /// [3]// 回路状态:bit位 0关、1开,(byte:bit0-7对应1-8回路  ...n回路)     ((n+7)/8) byte
                    foreach (var room in Room.Lists) {
                        var commonList = room.DeviceList.FindAll ((obj) => obj.SubnetID == subnetID&& obj.DeviceID == deviceID);
                        if (commonList != null && commonList.Count > 0) {
                            foreach (var common in commonList) {
                                if (common.LoopID > usefullBytes [2]) {
                                    continue;
                                }
                                string updateFlag = common.CommonLoopID;
                                int useIndex = common.LoopID / 8;
                                int selfIndex = (common.LoopID -1) % 8;
                                var binaryString = Convert.ToString (usefullBytes [3 + useIndex], 2).PadLeft(8,'0').ToCharArray();
                                byte ackSceneBrightness = 100;
                                if (binaryString [7-selfIndex] == '0') {
                                    ackSceneBrightness = 0;
                                }
                                if (common.Type == DeviceType.LightRGB) {
                                    break;
                                }
                                common.LastUpdateTime = DateTime.Now;
                                bool hadBeUpdate = true;
                                if (common.Type == DeviceType.LightSwitch || common.Type == DeviceType.LightSwitchSocket || common.Type == DeviceType.LightSwitchSocket) {
                                    if ((common as LightSwitch).CurrentBrightness == ackSceneBrightness)
                                        hadBeUpdate = false;
                                    (common as LightSwitch).CurrentBrightness = ackSceneBrightness;
                                } else if (common.Type == DeviceType.LightDimming) {
                                    if ((common as LightDimming).CurrentBrightness == ackSceneBrightness)
                                        hadBeUpdate = false;
                                    (common as LightDimming).CurrentBrightness = ackSceneBrightness;
                                } else if (common.Type == DeviceType.FanModule) {
                                    if ((common as FanModule).Switch == ackSceneBrightness)
                                        hadBeUpdate = false;
                                    (common as FanModule).Switch = ackSceneBrightness;
                                    if (ackSceneBrightness != 0)
                                        (common as FanModule).WindSpeed = ackSceneBrightness;
                                } else if (common.Type == DeviceType.LightMixSwitch) {
                                    if ((common as LightMixSwitch).CurrentBrightness == ackSceneBrightness)
                                        hadBeUpdate = false;
                                    (common as LightMixSwitch).CurrentBrightness = ackSceneBrightness;
                                } else if (common.Type == DeviceType.LightMixDimming) {
                                    if ((common as LightMixDimming).CurrentBrightness == ackSceneBrightness)
                                        hadBeUpdate = false;
                                    (common as LightMixDimming).CurrentBrightness = ackSceneBrightness;
                                } else if (common.Type == DeviceType.LightDALI) {
                                    if ((common as LightDALI).CurrentBrightness == ackSceneBrightness)
                                        hadBeUpdate = false;
                                    (common as LightDALI).CurrentBrightness = ackSceneBrightness;
                                }
                                if (Application.IsPad) {
                                    
                                } else {
                                    UserDeviceToLight.UpdateBrighingCount (updateFlag);
                                    if (hadBeUpdate) {
                                        UserRoom.UpdataDeviceStatus (common);
                                        UserLightPage.UpdateStatus (updateFlag, ackSceneBrightness);
                                        UserDeviceToLight.UpdateStatus (updateFlag, ackSceneBrightness);
                                        UserFan.UpdataFanView (updateFlag, ackSceneBrightness);
                                        UserDeviceToFan.UpdataStatus (updateFlag, ackSceneBrightness);
                                        UserDeviceToSocket.UpdateStatus (updateFlag, ackSceneBrightness);
                                    }
                                    //break;
                                }
                            }
                        }
                    }                
                break;
                case Command.SetLogicLoopColorACK:
                case Command.ReadLogicLoopColorACK:
                    foreach (var room in Room.Lists) {
                        foreach (var common in room.DeviceList) {
                            if (common.SubnetID != subnetID || common.DeviceID != deviceID || common.LoopID != usefullBytes [0]) {
                                continue;
                            }
                            common.LastUpdateTime = DateTime.Now;
                            if (common.Type == DeviceType.LightRGB) {
                                (common as LightLogic).CurrentBrightness = usefullBytes [1];
                                string updateFlag = common.CommonLoopID + "RGB";
                                if (Application.IsPad) {
                                    UserLightPage.UpdateStatus (updateFlag, usefullBytes [1]);
                                    UserDeviceToLight.UpdateStatus (updateFlag, usefullBytes [1]);
                                } else {
                                    UserRoom.UpdataDeviceStatus (common);
                                    UserLightPage.UpdateStatus (updateFlag, usefullBytes [1]);
                                    UserDeviceToLight.UpdateStatus (updateFlag, usefullBytes [1]);
                                }
                            }
                        }
                    }
                    break;
                case Command.SetFoolHeatACK:
                case Command.ReadFoolHeatACK:
                case Command.Serverx_FH_CMD_ACK:
                    foreach (var room in Room.Lists) {
                        var fhCommon = room.DeviceList.Find ((obj) => obj.SubnetID == subnetID && obj.DeviceID == deviceID && obj.LoopID == usefullBytes [0]);
                        if (fhCommon != null) {
                            (fhCommon as FoolHeat).Serverx_FH_CMD (FoolHeat.CommandType.Null, usefullBytes);
                        }
                    }
                    break; 
                case Command.ReadCurtainStutasACK:
                case Command.UpdataCurtainModelStutasACK:
                    #region 窗帘
                    foreach (var room in Room.Lists) {
                        foreach (var common in room.DeviceList) {
                            if (common.SubnetID != subnetID || common.DeviceID != deviceID) {
                                continue;
                            }
                            common.LastUpdateTime = DateTime.Now;
                            if (common.Type == DeviceType.CurtainModel) {
                                if (common.LoopID == usefullBytes [0]) {
                                    if (usefullBytes [1] != 0) {
                                        (common as CurtainModel).Status = (CurtainStatus)usefullBytes [1];
                                        common.obj5 = usefullBytes [1];
                                        UserCurtainPage.UpdateStatus (common);
                                        UserDeviceToCurtains.UpdateStata (common);
                                    }
                                }
                            } else if (common.Type == DeviceType.CurtainRoller) {
                                if (usefullBytes [0] == 17) {
                                    (common as CurtainRoller).CurtainProress = usefullBytes [1];
                                    if (usefullBytes [1] > 0) {
                                        (common as CurtainRoller).Status = CurtainStatus.Open;
                                    } else {
                                        (common as CurtainRoller).Status = CurtainStatus.Close;
                                    }
                                } else {
                                    if (usefullBytes [1] != 0) {
                                        common.obj5 = usefullBytes [1];
                                        (common as CurtainRoller).Status = (CurtainStatus)usefullBytes [1];
                                    }
                                }
                                UserCurtainPage.UpdateStatus (common);
                                UserDeviceToCurtains.UpdateStata (common);
                            } else if (common.Type == DeviceType.CurtainTrietex) {
                                if (usefullBytes [0] == 17) {
                                    (common as CurtainTrietex).CurtainProress = usefullBytes [1];
                                    if (usefullBytes [1] > 0) {
                                        (common as CurtainTrietex).Status = CurtainStatus.Open;
                                    } else {
                                        (common as CurtainTrietex).Status = CurtainStatus.Close;
                                    }
                                } else {
                                    if (usefullBytes [1] != 0) {
                                        common.obj5 = usefullBytes [1];
                                        (common as CurtainTrietex).Status = (CurtainStatus)usefullBytes [1];
                                    }
                                }
                                UserCurtainPage.UpdateStatus (common);
                                UserDeviceToCurtains.UpdateStata (common);
                            }
                        }
                    }
                    break;
                #endregion
 
                case Command.ControlMusicModel1ACK:
                    #region 音乐
                    foreach (var m in Room.Lists) {
                        var device = m.DeviceList.Find ((v) => {
                            return v.Type == DeviceType.MusicModel && v.SubnetID == subnetID && v.DeviceID == deviceID;
                        });
                        if (device == null) {
                            continue;
                        }
                        MusicModel music = device as MusicModel;
 
                        //把接收到的数据转换为字符串
                        string backAllString = CommonPage.MyEncodingGB2312.GetString (usefullBytes);
 
                        //把当前字符串以,号分开
                        string [] backResult = backAllString.Split (',');
 
                        string commandString = backResult [0];
 
 
 
                        if (commandString.StartsWith ("#Z")) {
                            if (2 <= backResult.Length) {
                                //music.PlayStatus = backResult [1] == "ON" ? MusicModel.Status.Play : MusicModel.Status.Stop;
                            }
                            if (backResult.Length > 3) {
                                //设置音乐的区号,源号,音量值。
                                if (backResult [0].StartsWith ("#Z") && backResult [1] == "ON" && backResult [2].StartsWith ("SRC") && backResult [3].StartsWith ("VOL")) {
 
                                    music.ZoneID = backResult [0].Replace ("#Z", "");
                                    music.SourceID = backResult [2].Replace ("SRC", "");
                                    music.CurVol = backResult [3].Replace ("VOL", "").TrimEnd (new char [] { '\r', '\n' });
                                }
                            }
                        }
 
                        if (commandString.Length > 3) {
                            if (commandString.StartsWith ("#S")) {
                                //music_SourceID=commandString.Substring (2, 1);
                            }
 
                            commandString = commandString.Substring (3, commandString.Length - 3);
                            byte [] bytes = null;
                            switch (commandString) {
                            case "DISPLINE4":
                                //当前播放歌曲名
                                var len = 0;
                                for (var i = 14; i < usefullBytes.Length; i += 2) {
                                    if (usefullBytes [i] == 0x00 && usefullBytes [i + 1] == 0x03) {
                                        break;
                                    }
                                    len += 2;
                                }
                                bytes = new byte [len];
 
                                for (var j = 0; j < bytes.Length;) {
 
                                    bytes [j + 1] = usefullBytes [14 + j];
                                    bytes [j] = usefullBytes [14 + j + 1];
                                    j += 2;
                                }
 
                                music.curPlayMusicName = Encoding.Unicode.GetString (bytes);
                                break;
                            case "DISPLINE1":
                                //列表号、列表总数,返回格式: L:1/2
                                bytes = new byte [usefullBytes.Length - 4 - 14];
                                for (int i = 14, j = 0; j < bytes.Length;) {
                                    bytes [j + 1] = usefullBytes [i++];
                                    bytes [j] = usefullBytes [i++];
                                    j += 2;
                                }
                                string curPlayListNumberAndCount = Encoding.Unicode.GetString (bytes);
 
                                // mm1ACK.musicListNumber = "0";
 
                                int number = 0;
 
                                if (curPlayListNumberAndCount.StartsWith ("L:")) {
                                    curPlayListNumberAndCount = curPlayListNumberAndCount.Replace ("L:", "");
 
                                    if (curPlayListNumberAndCount.Contains ("/")) {
                                        number = int.Parse (curPlayListNumberAndCount.Split ('/') [0]);
                                        music.curListNumber = number < 10 ? "00" + number : (number < 100 ? "0" + number : number.ToString ());
 
                                    }
                                }
                                break;
                            case "DISPLINE2":
                                //当前列表名,返回格式: SOME.PLS
                                bytes = new byte [usefullBytes.Length - 4 - 14];
                                for (int i = 14, j = 0; j < bytes.Length;) {
                                    bytes [j + 1] = usefullBytes [i++];
                                    bytes [j] = usefullBytes [i++];
                                    j += 2;
                                }
                                string curPlayMusicListName = Encoding.Unicode.GetString (bytes);
 
                                string curMusicListName = curPlayMusicListName;
 
                                if (curPlayMusicListName.Contains (".PLS")) {
                                    curMusicListName = curPlayMusicListName.Split ('.') [0];
                                }
                                music.curMusicListName = curMusicListName;
 
                                //MusicStatusFunction.updateMusicStatus_PlayListName (subnetID, deviceID, music_SourceID, curMusicListName, iPEndPoint.Address.ToString (), iPEndPoint.Port);
                                break;
                            case "DISPLINE3":
                                //歌曲号、歌曲总数,返回格式: S:10/23
                                bytes = new byte [usefullBytes.Length - 4 - 14];
                                for (int i = 14, j = 0; j < bytes.Length;) {
                                    bytes [j + 1] = usefullBytes [i++];
                                    bytes [j] = usefullBytes [i++];
                                    j += 2;
                                }
                                string curPlayMusicNumberAndCount = System.Text.Encoding.Unicode.GetString (bytes);
 
                                int musicFileNumber = 0;
 
                                if (curPlayMusicNumberAndCount.StartsWith ("S:")) {
                                    curPlayMusicNumberAndCount = curPlayMusicNumberAndCount.Replace ("S:", "");
 
                                    if (curPlayMusicNumberAndCount.Contains ("/")) {
                                        musicFileNumber = int.Parse (curPlayMusicNumberAndCount.Split ('/') [0]);
                                    }
                                }
                                music.curSongNumber = musicFileNumber < 10 ? "00" + musicFileNumber : (musicFileNumber < 100 ? "0" + musicFileNumber : musicFileNumber.ToString ());
                                //MusicStatusFunction.updateMusicStatus_PlayMusicFileNumber (subnetID, deviceID, music_SourceID, musicFileNumber, iPEndPoint.Address.ToString (), iPEndPoint.Port);
                                break;
                            case "DISPINFO":
                                //当前播放歌曲的状态,返回格式: #SsDISPINFO,DUR1945,POS0,STATUS2
                                //DUR后数值:为歌曲播放总时间  (秒×10,实际时间的10倍)
                                //POS后数值:为歌曲已播放时间  (秒×10,实际时间的10倍)
                                //STATUS后数值:为1表示stop,为2表示play,为3表示pause
 
                                if (backResult [1].StartsWith ("DUR") && backResult [2].StartsWith ("POS") && backResult [3].StartsWith ("STATUS")) {
                                    music.SoundGenerator = backResult [0] [2].ToString ();
                                    music.musicTime = backResult [1].Replace ("DUR", "");
                                    music.musicPalyTime = backResult [2].Replace ("POS", "");
                                    music.musicCurPalyStatus = backResult [3] [6].ToString ();//.Replace ("STATUS", "")[0];
                                    music.PlayStatus = music.musicCurPalyStatus == "2" ? MusicModel.Status.Play : MusicModel.Status.Stop;
                                    music.LastDateTime = DateTime.Now;
                                }
                                // UserMusicModelRoom.UpdateControls(mm1ACK);
                                //UserMusicModel.UpdateRoomMusicStatus(mm1ACK);
                                break;
 
                            //播放模式
                            case "DISPMODE":
                                if (7 <= backResult [1].Length) {
                                    music.playMode = int.Parse (backResult [1].Substring (6, 1));
                                    System.Console.WriteLine ("DISPMODE=====" + music.playMode);
                                }
                                break;
                            }
                        }
                    }
                    break;
                #endregion
                #region 空调
                case Command.ReadPanelTempTypeACK:
                    foreach (var room in Room.Lists) {
                        foreach (var common in room.DeviceList) {
                            if (common.SubnetID != subnetID || common.DeviceID != deviceID || common.LoopID != usefullBytes [0]) {
                                continue;
                            }
                            common.LastUpdateTime = DateTime.Now;
                            if (common.Type == DeviceType.ACPanel ) {
                                var ac = common as ACPanel;
                                ac.TemperatureMode = usefullBytes [0];
                            }
                        }
                    }
                    break;
                case Command.ReadACModeACK:
                case Command.SetACModeACK:
                    foreach (var room in Room.Lists) {
                        foreach (var common in room.DeviceList) {
                            if (common.SubnetID != subnetID || common.DeviceID != deviceID || common.LoopID != usefullBytes [0]) {
                                continue;
                            }
                            common.LastUpdateTime = DateTime.Now;
                            if (common.Type == DeviceType.HVAC || common.Type == DeviceType.ACPanel || common.Type == DeviceType.ACInfrared)
                            {
                                var ac = common as AC;
                                ac.TemperatureMode = usefullBytes [1];
                                ac.IndoorTemperature = usefullBytes [2];
                                ac.CoolTemperature = usefullBytes [3];
                                ac.HeatTemperature = usefullBytes [4];
                                ac.AutoTemperature = usefullBytes [5];
                                ac.ChuShiTemperature = usefullBytes [6];
                                ac.RealModeAndFanSpeed = usefullBytes [7];
                                ac.Power = usefullBytes [8];
                                ac.SetMode = usefullBytes [9];
                                ac.SetFanSpeed = usefullBytes [10];
                                //if (usefullBytes [11] < 16 || usefullBytes [11] == 0) {
                                //    ac.SetTemperature = (byte)16;
                                //} else {
                                //    ac.SetTemperature = usefullBytes [11];
                                //}
                                ac.ShaoFanMode = usefullBytes [12];
                                //IO.F
                                if (Application.IsPad) {
                                    //Pad.UserACPage.UpdateStatus (ac);
                                } else {
                                    UserACPage.UpdateStatus (ac);
                                    UserDeviceToAC.UpdateStatus (ac);
                                    UserRoom.UpdataDeviceStatus (ac);
                                }
                            }
                        }
                    }
                    break;
                case Command.ReadPanleTempACK:
                case Command.PanleBroadcastTemp:
                    foreach (var room in Room.Lists) {
                        foreach (var common in room.DeviceList) {
                            if (common.Type == DeviceType.ACPanel) {
                                var tempAC = common as ACPanel;
                                tempAC.IndoorTemperature = usefullBytes [1];
                                UserACPage.UpdateIndoorTemp (common.CommonLoopID, usefullBytes [1]);
                            } else if (common.Type == DeviceType.FoolHeatPanel) {
                                var tempFH22 = common as FoolHeatPanel;
                                tempFH22.IndoorTemperature = usefullBytes [1];
                                UserFHPage.UpdateIndoorTemp (tempFH22.CommonLoopID, usefullBytes [1]);
                            }
                        }
                    }
                    break;
                case Command.InstructionPanelKeyACK:
                case Command.ReadInstructionPanelKeyACK:
                    byte reACPanel = 0;
                    if (usefullBytes.Length == 2) {
                        reACPanel = 1;
                    } else if (usefullBytes.Length == 3) {
                        reACPanel = usefullBytes [2];
                    } else {
                        break;
                    }
                    foreach (var room in Room.Lists) {
                        foreach (var common in room.DeviceList) {
                            if (common.SubnetID != subnetID || common.DeviceID != deviceID || common.LoopID != reACPanel) {
                                continue;
                            }
                            common.LastUpdateTime = DateTime.Now;
                            if (common.Type == DeviceType.ACPanel) {
                                var ac = common as ACPanel;
                                switch (usefullBytes [0]) {
                                case 3://
                                    ac.Power = usefullBytes [1];
                                    UserACPage.UpdatePower (ac.CommonLoopID, usefullBytes [1]);
                                    UserDeviceToAC.UpdatePower (ac.CommonLoopID, usefullBytes [1]);
                                    UserRoom.Update_ACPanel_Power (ac.Type.ToString () + "_" + ac.CommonLoopID, usefullBytes [1]);
                                    break;
                                case 4:
                                    ac.CoolTemperature = usefullBytes [1];
                                    if (ac.SetMode == 0) {
                                        ac.SetTemperature = usefullBytes [1];
                                        UserACPage.UpdateWorkingTemp (ac.CommonLoopID, usefullBytes [1]);
                                        UserDeviceToAC.UpdateWorkingTemp (ac.CommonLoopID, usefullBytes [1]);
                                        UserRoom.Update_ACPanel_WorkingTemp (ac.Type.ToString () + "_" + ac.CommonLoopID, usefullBytes [1], ac.TemperatureMode);
                                    }
                                    break;
                                case 5:
                                    ac.SetFanSpeed = usefullBytes [1];
                                    UserACPage.UpdateSetFanSpeed (ac.CommonLoopID, usefullBytes [1]);
                                    UserDeviceToAC.UpdateFanSpeed (ac.CommonLoopID, usefullBytes [1]);
                                    UserRoom.Update_ACPanel_FanIcon (ac.Type.ToString () + "_" + ac.CommonLoopID, usefullBytes [1]);
                                    break;
                                case 6:
                                    ac.SetMode = usefullBytes [1];
                                    UserACPage.UpdateSetMode (ac.CommonLoopID, usefullBytes [1]);
                                    UserDeviceToAC.UpdateSetMode (ac.CommonLoopID, usefullBytes [1]);
                                    UserRoom.Update_ACPanel_ModeIcon (ac.Type.ToString () + "_" + ac.CommonLoopID, usefullBytes [1]);
                                    break;
                                case 7:
                                    ac.HeatTemperature = usefullBytes [1];
                                    if (ac.SetMode == 1) {
                                        ac.SetTemperature = usefullBytes [1];
                                        UserACPage.UpdateWorkingTemp (ac.CommonLoopID, usefullBytes [1]);
                                        UserDeviceToAC.UpdateWorkingTemp (ac.CommonLoopID, usefullBytes [1]);
                                        UserRoom.Update_ACPanel_WorkingTemp (ac.Type.ToString () + "_" + ac.CommonLoopID, usefullBytes [1], ac.TemperatureMode);
                                    }
                                    break;
                                case 8:
                                    ac.AutoTemperature = usefullBytes [1];
 
                                     if (ac.SetMode == 3) {
                                        ac.SetTemperature = usefullBytes [1];
                                        UserACPage.UpdateWorkingTemp (ac.CommonLoopID, usefullBytes [1]);
                                        UserDeviceToAC.UpdateWorkingTemp (ac.CommonLoopID, usefullBytes [1]);
                                        UserRoom.Update_ACPanel_WorkingTemp (ac.Type.ToString () + "_" + ac.CommonLoopID, usefullBytes [1], ac.TemperatureMode);
                                    }
                                    break;
                                case 19:
                                    ac.ChuShiTemperature = usefullBytes [1];
                                    if (ac.SetMode == 4) {
                                        ac.SetTemperature = usefullBytes [1];
                                        UserACPage.UpdateWorkingTemp (ac.CommonLoopID, usefullBytes [1]);
                                        UserDeviceToAC.UpdateWorkingTemp (ac.CommonLoopID, usefullBytes [1]);
                                        UserRoom.Update_ACPanel_WorkingTemp (ac.Type.ToString () + "_" + ac.CommonLoopID, usefullBytes [1], ac.TemperatureMode);
                                    }
                                    break;
                                }
                                if (Application.IsPad) {
                                    //Pad.UserACPage.UpdateStatus (ac);
                                } else {
                                    //UserACPage.UpdateStatus (ac);
                                    //UserDeviceToAC.UpdateStatus (ac);
                                    //UserRoom.UpdataDeviceStatus (ac);
                                }
                            } else if (common.Type == DeviceType.FoolHeatPanel) {
                                var fh = common as FoolHeatPanel;
 
                                switch (usefullBytes [0]) {
                                case 20://
                                    fh.Status = usefullBytes [1];
                                    UserDeviceToFH.UpdatePower (fh.CommonLoopID, usefullBytes [1]);
                                    UserFHPage.UpdatePower (fh.CommonLoopID, usefullBytes [1]);
                                    UserRoom.Update_FoolHeatPanel_Power (fh.Type.ToString () + "_" + fh.CommonLoopID, usefullBytes [1]);
                                    break;
                                case 21:
                                    fh.TemperatureType = usefullBytes [1];
                                    UserFHPage.UpdateWorkingMode (fh.CommonLoopID, usefullBytes [1]);
                                    if (usefullBytes [1] == 5) {
                                        if (fh.Timer == 0)//时间模式的时间段标志 (0:白天,1:夜晚) (1byte)     //20110112加时间段标志
                                            Control.ControlBytesSend (Command.ReadInstructionPanelKey, fh.SubnetID, fh.DeviceID, new byte [] { 26, 2, fh.LoopID }, SendCount.Zero);
                                        else
                                            Control.ControlBytesSend (Command.ReadInstructionPanelKey, fh.SubnetID, fh.DeviceID, new byte [] { 27, 3, fh.LoopID }, SendCount.Zero);
                                    } else
                                        Control.ControlBytesSend (Command.ReadInstructionPanelKey, fh.SubnetID, fh.DeviceID, new byte [] { (byte)(usefullBytes [1] + 24), fh.LoopID }, SendCount.Zero);
                                    break;
                                case 25:
                                    fh.NormalTemperature = usefullBytes [1];
                                    if (fh.TemperatureType == 1) {
                                        fh.WorkingTemperature = usefullBytes [1];
                                        UserDeviceToFH.UpdateWorkingTemp (fh.CommonLoopID, usefullBytes [1]);
                                        UserFHPage.UpdateWorkingTemp (fh.CommonLoopID, usefullBytes [1]);
                                        UserRoom.Update_FoolHeatPanel_WorkingTemp (fh.Type.ToString () + "_" + fh.CommonLoopID, usefullBytes [1]);
                                    }
                                    break;
                                case 26:
                                    fh.DayTemperature = usefullBytes [1];
                                    if (fh.TemperatureType == 2) {
                                        fh.WorkingTemperature = usefullBytes [1];
                                        UserDeviceToFH.UpdateWorkingTemp (fh.CommonLoopID, usefullBytes [1]);
                                        UserFHPage.UpdateWorkingTemp (fh.CommonLoopID, usefullBytes [1]);
                                        UserRoom.Update_FoolHeatPanel_WorkingTemp (fh.Type.ToString () + "_" + fh.CommonLoopID, usefullBytes [1]);
                                    }
                                    break;
                                case 27:
                                    fh.NightTemperature = usefullBytes [1];
                                    if (fh.TemperatureType == 3) {
                                        fh.WorkingTemperature = usefullBytes [1];
                                        UserDeviceToFH.UpdateWorkingTemp (fh.CommonLoopID, usefullBytes [1]);
                                        UserFHPage.UpdateWorkingTemp (fh.CommonLoopID, usefullBytes [1]);
                                        UserRoom.Update_FoolHeatPanel_WorkingTemp (fh.Type.ToString () + "_" + fh.CommonLoopID, usefullBytes [1]);
                                    }
                                    break;
                                case 28:
                                    fh.AwayTemperature = usefullBytes [1];
                                    if (fh.TemperatureType == 4) {
                                        fh.WorkingTemperature = usefullBytes [1];
                                        UserDeviceToFH.UpdateWorkingTemp (fh.CommonLoopID, usefullBytes [1]);
                                        UserFHPage.UpdateWorkingTemp (fh.CommonLoopID, usefullBytes [1]);
                                        UserRoom.Update_FoolHeatPanel_WorkingTemp (fh.Type + "_" + fh.CommonLoopID, usefullBytes [1]);
                                    }
                                    break;
                                }
                                if (Application.IsPad) {
                                    //SimpleControl.Pad.UserFHPage.UpdateStatus (fh);
                                } else {
                                    //UserDeviceToFH.UpdateStatus (fh);
                                    //UserFHPage.UpdateStatus (fh);
                                    //UserRoom.UpdataDeviceStatus (fh);
                                }
                            }
                        }
                    }
 
                    break;
                case Command.Read_Air_Condition_Set_ack:
                    byte acpanelLoopId = 0;
                    if (usefullBytes.Length == 10) {
                        acpanelLoopId = 1;
                    } else if (usefullBytes.Length == 11) {
                        acpanelLoopId = usefullBytes [10];
                    }
                    foreach (var room in Room.Lists) {
                        var common = room.DeviceList.Find ((obj) => obj.SubnetID == subnetID && obj.DeviceID == deviceID && obj.LoopID == usefullBytes [1] && obj.Type == DeviceType.ACPanel);
                        if (common == null)
                            continue;
                        common.LastUpdateTime = DateTime.Now;
                        var ac = common as ACPanel;
                        ac.Power = usefullBytes [0];
                        ac.CoolTemperature = usefullBytes [1];
                        ac.SetMode = (byte)(usefullBytes [2] >> 4);//模式和风速  
                        ac.SetFanSpeed = (byte)(usefullBytes [2] & 0x0F);
                        //空调锁 
                        ac.SetTemperature = usefullBytes [4];//当前温度
                        ac.HeatTemperature = usefullBytes [5];//制热温度 
                                                              //空调实际运行模式和风速 
                        ac.AutoTemperature = usefullBytes [7];//自动温度 
                        ac.ChuShiTemperature = usefullBytes [8];//抽湿温度 
                        ac.ShaoFanMode = usefullBytes [9];//空调是否扫风(低四位)
                        if (Application.IsPad) {
 
                        } else {
                            UserACPage.UpdateStatus (ac);
                            UserDeviceToAC.UpdateStatus (ac);
                            UserRoom.UpdataDeviceStatus (ac);
                        }
                    }
                    break;
                case Command.Set_Air_State_New_ack:
                    if (usefullBytes.Length == 15) {
                        /// [0]//MODIFY_SUCCESS                                          1byte
                        /// [1]//空调页号(0-8)                                          1byte
                        /// [2]//空调开关(1开0关)                                       1byte       
                        /// [3]//制冷控制温度(摄氏0-30,华氏32-86)                       1byte   
                        /// [4]//制热控制温度(摄氏0-30,华氏32-86)                       1byte
                        /// [5]//自动控制温度(摄氏0-30,华氏32-86)                       1byte                                                                                           
                        /// [6]//除湿控制温度(摄氏0-30,华氏32-86)                       1byte
                        /// [7]//设置模式(0制冷,1制热,2通风,3自动,4除湿)              1byte
                        /// [8]//设置风速(0自动,1高风,2中风,3低风)                     1byte //注意:当模式为除湿或模式为通风及风速为自动时,风速视为小风为准           
                        /// [9]//设置扫风(0不扫风,1扫风)                                1byte
                        /// [10]//温度模式(摄氏Celsius:0,华氏Fahrenheit:1)               1byte 
                        /// [11]//模式实际工作状态(0制冷,1制热,2通风,3自动,4除湿)      1byte 
                        /// [12]//风速实际工作状态(0关闭,1高风,2中风,3低风)             1byte      
                        /// [13]//扫风实际工作状态(0不扫风,1扫风)                      1byte
                        /// [14]//环境温度(摄氏0-40,华氏32-99)                          1byte  
                        foreach (var room in Room.Lists) {
                            //foreach (var common in room.DeviceList) {
                            var common = room.DeviceList.Find ((obj) => obj.SubnetID == subnetID && obj.DeviceID == deviceID && obj.LoopID == usefullBytes [1] && obj.Type == DeviceType.ACPanel);
                            if (common == null)
                                continue;
                            //if (common.SubnetID != subnetID || common.DeviceID != deviceID || common.LoopID != usefullBytes[1]) {
                            //    continue;
                            //}
                            common.LastUpdateTime = DateTime.Now;
                            var ac = common as ACPanel;
                            ac.Power = usefullBytes [2];
                            ac.CoolTemperature = usefullBytes [3];
                            ac.SetMode = usefullBytes [7];//模式和风速  
                            ac.SetFanSpeed = (byte)(usefullBytes [2] & 0x0F);
                            //空调锁 
                            ac.SetTemperature = usefullBytes [4];//当前温度
                            ac.HeatTemperature = usefullBytes [4];//制热温度 
                                                                  //空调实际运行模式和风速 
                            ac.AutoTemperature = usefullBytes [5];//自动温度 
                            ac.ChuShiTemperature = usefullBytes [6];//抽湿温度 
                            ac.ShaoFanMode = usefullBytes [9];//空调是否扫风(低四位)
                            ac.IndoorTemperature = usefullBytes [14];
                            if (Application.IsPad) {
 
                            } else {
                                UserACPage.UpdateStatus (ac);
                                UserDeviceToAC.UpdateStatus (ac);
                                UserRoom.UpdataDeviceStatus (ac);
                            }
                            //}
                        }
                    }
                    break;
                #endregion
 
                #region 地热
                case Command.Read_Floor_Heat_State_ack:
                    foreach (var room in Room.Lists) {
                        byte fhLoopId = 1;
                        if (usefullBytes.Length == 11) {
                            fhLoopId = usefullBytes [10];
                        }
 
                        var common = room.DeviceList.Find ((obj) => obj.SubnetID == subnetID && obj.DeviceID == deviceID && obj.LoopID == fhLoopId && obj.Type == DeviceType.FoolHeatPanel);
                        if (common == null)
                            continue;
                        /// 1温度模式(摄氏Celsius:0,华氏Fahrenheit:1)                         (1byte)
                        /// 2温度源温度(最高位0:正,1:负.后7bit为值)                             (1byte)
                        /// 3地热地冷开关::0关、1开                                           (1byte)
                        /// 4工作温度模式:1:普通,2:白天,3:夜晚,4:离开,5:时间     (1-5)             (1byte) 
                        /// 5普通温度                       (摄氏0-99,华氏32-210)              (1byte)  
                        /// 6白天温度                       (摄氏0-99,华氏32-210)              (1byte) 
                        /// 7夜晚温度                       (摄氏0-99,华氏32-210)              (1byte)
                        /// 8离开温度                       (摄氏0-99,华氏32-210)              (1byte)
                        /// 9时间模式的时间段标志           (0:白天,1:夜晚)                      (1byte)  //20110112加时间段标志
                        /// 10模式:0地热模式、1地冷模式、2自动                                 (1byte)    //20120802加//20140805加自动
                        /// 11地热页号0-8                                       (1byte)//彩屏加入20150714
                        var th = common as FoolHeatPanel;
                        th.IndoorTemperature = usefullBytes [1] > 128 ? (byte)(0 - (usefullBytes [1] - 128)) : usefullBytes [1];
                        th.Status = usefullBytes [2];
                        th.TemperatureType = usefullBytes [3];
                        th.NormalTemperature = usefullBytes [4];
                        th.DayTemperature = usefullBytes [5];
                        th.NightTemperature = usefullBytes [6];
                        th.AwayTemperature = usefullBytes [7];
                        switch (usefullBytes [3]) {
                        case 1://普通
                            th.WorkingTemperature = th.NormalTemperature;
                            break;
                        case 2://白天
                            th.WorkingTemperature = th.DayTemperature;
                            break;
                        case 3://夜晚
                            th.WorkingTemperature = th.NightTemperature;
                            break;
                        case 4://离开
                            th.WorkingTemperature = th.AwayTemperature;
                            break;
                        case 5://时间模式
                            th.Timer = usefullBytes [9];
                            if (usefullBytes [9] == 1)//时间模式的时间段标志 (0:白天,1:夜晚) (1byte)     //20110112加时间段标志
                                th.WorkingTemperature = th.DayTemperature;
                            else
                                th.WorkingTemperature = th.NightTemperature;
                            break;
                        }
                        UserDeviceToFH.UpdateStatus (th);
                        UserFHPage.UpdateStatus (th,fhLoopId);
                        UserRoom.UpdataDeviceStatus (th);
                    }
                    break;
                case Command.Set_Floor_Heat_State_ack:
                    foreach (var room in Room.Lists) {
                        byte fhLoopId = 1;
                        if (usefullBytes.Length == 12) {
                            fhLoopId = usefullBytes [11];
                        }
 
                        var common = room.DeviceList.Find ((obj) => obj.SubnetID == subnetID && obj.DeviceID == deviceID && obj.LoopID == fhLoopId && obj.Type == DeviceType.FoolHeatPanel);
                        if (common == null)
                            continue;
                        /// 1 MODIFY_SUCCESS                          1 byte
                        /// 2温度模式(摄氏Celsius:0,华氏Fahrenheit:1)                         (1byte) 
                        /// 3地热地冷开关::0关、1开                                           (1byte)
                        /// 4工作温度模式:1:普通,2:白天,3:夜晚,4:离开,5:时间     (1-5)            (1byte) 
                        /// 5普通温度                       (摄氏0-99,华氏32-210)             (1byte)  
                        /// 6白天温度                       (摄氏0-99,华氏32-210)             (1byte) 
                        /// 7夜晚温度                       (摄氏0-99,华氏32-210)             (1byte)
                        /// 8离开温度                       (摄氏0-99,华氏32-210)             (1byte)
                        /// 9时间模式的时间段标志           (0:白天,1:夜晚)                     (1byte)  //20120802加
                        /// 10工作模式:0地热、1地冷、2自动                                     (1byte)     //20120802加//20140805加自动   
                        var th = common as FoolHeatPanel;
                        th.Status = usefullBytes [2];
                        th.TemperatureType = usefullBytes [3];
                        th.NormalTemperature = usefullBytes [4];
                        th.DayTemperature = usefullBytes [5];
                        th.NightTemperature = usefullBytes [6];
                        th.AwayTemperature = usefullBytes [7];
                        switch (usefullBytes [3]) {
                        case 1://普通
                            th.WorkingTemperature = th.NormalTemperature;
                            break;
                        case 2://白天
                            th.WorkingTemperature = th.DayTemperature;
                            break;
                        case 3://夜晚
                            th.WorkingTemperature = th.NightTemperature;
                            break;
                        case 4://离开
                            th.WorkingTemperature = th.AwayTemperature;
                            break;
                        case 5://时间模式
                            th.Timer = usefullBytes [9];
                            if (usefullBytes [9] == 1)//时间模式的时间段标志 (0:白天,1:夜晚) (1byte)     //20110112加时间段标志
                                th.WorkingTemperature = th.DayTemperature;
                            else
                                th.WorkingTemperature = th.NightTemperature;
                            break;
                        }
                        UserDeviceToFH.UpdateStatus (th);
                        UserFHPage.UpdateStatus (th,fhLoopId);
                        UserRoom.UpdataDeviceStatus (th);
                    }
                    break;
                #endregion
                }
            } catch (Exception ex) {
                Console.WriteLine ("Packet_ReceiveEvent:" + ex.ToString ());
            }
 
        }
        /// <summary>
        /// byte 变字符串
        /// </summary>
        /// <returns>The to hex16.</returns>
        /// <param name="b">The blue component.</param>
        public static string byteToHex16 (byte b)
        {
            string s = Convert.ToString (b, 16).ToUpper (); 
            if (s.Length <= 1) {
                return "0" + s;
            }
            return s;//
        }
 
        static void httpListener_EventHandler (System.Collections.Specialized.NameValueCollection nameValueCollection, System.IO.Stream outputStream, System.IO.Stream inputStream)
        {
            //if (!isHttpListenerStart) {
            //    return;
            //}
            try {
                //System.Net.WebClient webClient = new System.Net.WebClient ();
                //byte []bytes= webClient.DownloadData("http://ip:6001/GetAllFiles");
                //               System.Text.Encoding.UTF8.GetString (bytes);
                //List<string> list = new List<string> ();
                //foreach(string s in list) { 
                //    byte [] bytes = webClient.DownloadData ("http://ip:6001/Get"+s);
                //    FileUtils.WriteFileByBytes (s, bytes);
                //}
 
                if (nameValueCollection ["Command"] != null && nameValueCollection ["Command"].StartsWith ("Get")) {
                    string tempFileName = nameValueCollection ["Command"].Replace ("Get", "");
                    if ("AllFiles" == tempFileName) {
                        byte [] bytes = System.Text.Encoding.UTF8.GetBytes (Newtonsoft.Json.JsonConvert.SerializeObject (IO.FileUtils.ReadFiles ()));
                        outputStream.Write (bytes, 0, bytes.Length);
                        outputStream.Flush ();
                    } else {
                        byte [] bytes;
                        if (!IO.FileUtils.Exists (tempFileName)) {
                            bytes = System.Text.Encoding.UTF8.GetBytes ("文件名不存在!");
                            outputStream.Write (bytes, 0, bytes.Length);
                            outputStream.Flush ();
                            return;
                        }
                        System.IO.FileStream fs = new System.IO.FileStream (Application.RootPath + tempFileName, System.IO.FileMode.Open);
                        bytes = new byte [fs.Length];
                        fs.Read (bytes, 0, bytes.Length);
                        fs.Close ();
                        outputStream.Write (bytes, 0, bytes.Length);
                        outputStream.Flush ();
                    }
                } else if (nameValueCollection ["Command"].StartsWith ("Upload")) {
                    string path = Application.RootPath + nameValueCollection ["Command"].Replace ("Upload", "");
                    string dePath = nameValueCollection ["Command"].Replace ("Upload", "");
                    if (dePath.Contains ("Room_")) {
                        new Room ().Add (dePath);
                    }
                    if (dePath.Split ('_').Length == 5) {
                        if (dePath.Split ('_') [1] == (typeof (OnePortBus).Name) || dePath.Split ('_') [1] == (typeof (OnePortWirelessFR).Name)) {
                            UserConfig.Instance.RemoteModeFile = dePath;
                            UserConfig.Instance.SaveUserConfig ();
                        }
                        if (dePath.Split ('_') [1] == (typeof (SecurityModul).Name)) {
                            if (UserConfig.Instance.HideDeviceTypes.Contains ((R.MyInternationalizationString.Security))) {
                                UserConfig.Instance.HideDeviceTypes.Remove (R.MyInternationalizationString.Security);
                                UserConfig.Instance.SaveUserConfig ();
                            }
                        }
                    }
 
                    if (dePath.Split ('_').Length == 2 && dePath.StartsWith ("GlobalScene")) {
                        var sceneFileList = Newtonsoft.Json.JsonConvert.DeserializeObject<List<string>> (System.Text.Encoding.UTF8.GetString (IO.FileUtils.ReadFile (Scene.GlobalSceneFilePath)));
                        if (!sceneFileList.Contains (dePath)) {
                            sceneFileList.Add (dePath);
                            IO.FileUtils.WriteFileByBytes (Scene.GlobalSceneFilePath, CommonPage.MyEncodingUTF8.GetBytes (Newtonsoft.Json.JsonConvert.SerializeObject (sceneFileList)));
                        }
                    }
                    FileUtils.WriteFileByInputStream (path, inputStream);
                    Common common = Newtonsoft.Json.JsonConvert.DeserializeObject<Common> (CommonPage.MyEncodingUTF8.GetString (IO.FileUtils.ReadFile (path)));
                    if (common.DeviceTextID == R.MyInternationalizationString.ElectricalControl) {
                        UserConfig.Instance.SocketList.Add (common.CommonLoopID);
                    }
                } else {
                    byte [] bytes = System.Text.Encoding.UTF8.GetBytes ("请求命令无效!");
                    outputStream.Write (bytes, 0, bytes.Length);
                    outputStream.Flush ();
                }
            } catch (Exception ex) {
                Console.WriteLine ("httpListener_EventHandler" + ex.ToString ());
            }
        }
 
        /// <summary>
        /// 随机数高位
        /// </summary>
        public static byte RandomHigh;
        /// <summary>
        /// 随机数低位
        /// </summary>
        public static byte RandomLow;
 
        /// <summary>
        /// 设备列表
        /// </summary>
        public static System.Collections.Generic.List<Common> DeviceList = new System.Collections.Generic.List<Common> ();
        /// <summary>
        /// 网关设备列表
        /// </summary>
        public static System.Collections.Generic.List<Common> GateWayList = new System.Collections.Generic.List<Common> ();
        /// <summary>
        /// 设备对应回路列表
        /// </summary>
        public static System.Collections.Generic.List<Common> DeviceLoopList = new System.Collections.Generic.List<Common> ();
 
        /// <summary>
        /// Updates Device's remake.
        /// </summary>
        public static void UpdateRemark (byte subnetID, byte deviceID, string remark)
        {
            byte [] updateBytes = new byte [20];
            byte [] remakeBytes = MyEncodingGB2312.GetBytes (remark);
            Array.Copy (remakeBytes, 0, updateBytes, 0, remakeBytes.Length < 20 ? remakeBytes.Length : 20);
            Control.ControlBytesSend (Command.UpdataRemake, subnetID, deviceID, updateBytes);
        }
 
        /// <summary>
        /// 监听请求的Http端口
        /// </summary>
        public static int Port = 5555;
        static void httpListener_EventHandler (string rawUrl, System.IO.Stream outputStream, System.IO.Stream inputStream)
        {
            foreach (var musicInfo in MusicInfo.MusicInfoList) {
                if ("audio-item-" + musicInfo.ID == rawUrl.TrimStart ('/')) {
                    var file = new System.IO.FileStream (musicInfo.Data, System.IO.FileMode.Open, System.IO.FileAccess.Read);
                    byte [] bytes = new byte [1024];
                    try {
                        while (file.CanRead) {
                            int len = file.Read (bytes, 0, bytes.Length);
                            if (len == 0) {
                                break;
                            }
                            outputStream.Write (bytes, 0, len);
                        }
                    } catch { }
                    file.Close ();
                    break;
                }
            }
        }
    }
}