1.1
wxr
2022-12-09 88b0bfe8a27e15717395e643db5814f34e3fc22f
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
using System;
using Shared.Net;
using Shared.IO;
using System.Text;
using System.Collections.Generic;
using Shared.SimpleControl.Phone;
//using service.hdlcontrol.com_WebServiceAirQuality;
 
namespace Shared.SimpleControl
{
    public static class CommonPage
    {
        //public static AirQuality AirQuality;
        //public static Action RefreshAir;           
        public static bool IsRemote = false;
        public static bool FindGateway = false;
        public static bool FindGatewayChilren = false;
        public static string FindGatewayChilrenIPAddress = new Net.NetWiFi ().BroadcastIpAddress.ToString ();
        public static bool LocalPhoneFindDevice = false;
        public static int searchTotal = 0;
        public static int newDevice = 0;
        private static string ip= "115.29.251.24";
        public static System.Net.IPEndPoint EndPoint {
            get {
                try {
                    if (IsRemote) {
                        //127.0.0.1
                        if (ip == "0.0.0.0") {
                            try {
                                ip = "115.29.251.24";
                            } catch { }
                        }
                        return new System.Net.IPEndPoint (System.Net.IPAddress.Parse (ip), 9999);
                    } else {
                        if (FindGateway) {
                            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 = Encoding.GetEncoding ("gb2312");
        static bool isHttpListenerStart;
        public static DateTime dt;
 
        /// <summary>
        /// 初始化处理socket接收的数据 
        /// </summary>
        public static void InitReceiveEvent ()
        {
            Packet.ReceiveEvent += Packet_ReceiveEvent;
        }
 
        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 object lockobj = new object ();
 
        static void Packet_ReceiveEvent (byte subnetID, byte deviceID, Command command, byte [] usefullBytes,int deviceTypeNumber)
        {
            try {
                List<byte> ButtonBkeyModelList = new List<byte> ();
                switch (command) {
                //读取网关设备回复
                case Command.ReadGatewayACK:
                    if (usefullBytes [0] != RandomHigh | usefullBytes [1] != RandomLow) {
                        return;
                    }
                    lock (GateWayList) {
                        if (GateWayList.Find ((l) => { return l.SubnetID == subnetID && l.DeviceID == deviceID; }) != null) {
                            return;
                        }
                        DeviceType deviceType = (DeviceType)(usefullBytes [2] * 256 + usefullBytes [3]);
                        if (usefullBytes [2] == 254 && usefullBytes [3] < 8) {
                            GatewayBase gatewayBase = new GatewayBase ();
                            gatewayBase.DeviceTypeNumber = deviceTypeNumber;
                            gatewayBase.SubnetID = subnetID;
                            gatewayBase.DeviceID = deviceID; gatewayBase.Type = deviceType;
                            gatewayBase.LoopID = usefullBytes [4];
                            gatewayBase.MAC = byteToHex16 (usefullBytes [5]) + "." + byteToHex16 (usefullBytes [6]) + "." + byteToHex16 (usefullBytes [7]) + "." + byteToHex16 (usefullBytes [8]) + "." + byteToHex16 (usefullBytes [9]) + "." + byteToHex16 (usefullBytes [10]) + "." + byteToHex16 (usefullBytes [11]) + "." + byteToHex16 (usefullBytes [12]);
                            gatewayBase.Name = MyEncodingGB2312.GetString (usefullBytes, 13, 20).Trim ('\0');//=============
                            gatewayBase.IPAddress = usefullBytes [33] + "." + usefullBytes [34] + "." + usefullBytes [35] + "." + usefullBytes [36];
                            gatewayBase.RouteIPAddress = usefullBytes [37] + "." + usefullBytes [38] + "." + usefullBytes [39] + "." + usefullBytes [40];
                            gatewayBase.IPMAC = usefullBytes [41] + "." + usefullBytes [42] + "." + usefullBytes [43] + "." + usefullBytes [44] + "." + usefullBytes [45] + "." + usefullBytes [46];
                            gatewayBase.SubnetMask = usefullBytes [47] + "." + usefullBytes [48] + "." + usefullBytes [49] + "." + usefullBytes [50];
                            gatewayBase.DHCP = usefullBytes [51] == 0 ? false : true;
                            GateWayList.Add (gatewayBase);
                            if (gatewayBase.Name == "") {
                                gatewayBase.Name = gatewayBase.CommonLoopID.ToString ();
                            }
                        }
                    }
                    break;
                case Command.WriteWirelessPanelButtonKey:
                    //01 01 59(+30) 02 0D 03 64 00 01 
                    break;
                case Command.AssignedAddressACK:
                    Console.WriteLine (usefullBytes [0].ToString ());
                    if (usefullBytes [0] == 3) {
                        dt = DateTime.Now;
                    } else if (usefullBytes [0] == 4) {
                        dt = System.DateTime.MinValue;
                    }
                    break;
                case Command.Remote3thACK:
                    Console.WriteLine (usefullBytes [0]);
                    break;
                }
            } 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;//
        }
 
 
        /// <summary>
        /// 随机数高位
        /// </summary>
        public static byte RandomHigh;
        /// <summary>
        /// 随机数低位
        /// </summary>
        public static byte RandomLow;
 
        /// <summary>
        /// 网关设备列表
        /// </summary>
        public static List<GatewayBase> GateWayList = new List<GatewayBase> ();
 
    }
}