wxr
2020-03-13 171bf03f3664226eeff2b20ee9bd2e914b63a17d
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
using System;
using System.Text;
using HDL_ON.DAL.Net;
using HDL_ON.Entity;
using HDL_ON.UI;
using Shared;
using Shared.Net;
 
namespace HDL_ON.DAL
{
    public static class CommonPage
    {
 
        public static bool IsRemote;
        public static Action RefreshAir;
        public static string FindGatewayChilrenIPAddress = new NetWiFi().BroadcastIpAddress.ToString();
        private static string ip = "0.0.0.0";
        public static System.Net.IPEndPoint EndPoint
        {
            get
            {
                try
                {
                    return new System.Net.IPEndPoint(System.Net.IPAddress.Parse(new 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 Encoding.GetEncoding(1250);
                    }
                    else
                    {
                        return Encoding.GetEncoding("gb2312");
                    }
                }
                catch (Exception ex)
                {
                    MainPage.Log("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 = { b4, b3, b2, b1 };
            return BitConverter.ToSingle(byteTemp, 0);
        }
 
 
        static void Packet_ReceiveEvent(byte subnetID, byte deviceID, Command command, byte[] receiveBytes, string revGatewayIP)
        {
            try
            {
                switch (command)
                {
                    case Command.ReadLightSingleLoopBrightnessACK:
                    case Command.SetSingleLightACK:
                        foreach (var function in DB_ResidenceData.residenceData.functionList.lights)
                        {
                            if (function.GetBusId() == subnetID + "_" + deviceID + "_" + receiveBytes[0])
                            {
                                if (function.functionCategory == FunctionType.Light)
                                {
                                    var light = function as Light;
                                    if (receiveBytes[2] == 0 && light.state == 0)
                                    {
                                        break;
                                    }
                                    if (receiveBytes[2] == 100 && light.state == 1)
                                    {
                                        break;
                                    }
                                    if (function.functionType == "Relay")
                                    {
                                        var relay = function as Light;
                                        relay.state = receiveBytes[2] == 0 ? 0 : 1;
                                        HomePage.UpdataLightView(relay);
                                    }
                                    else if (function.functionType == "brightness")
                                    {
                                        var dimmer = function as Light;
                                        dimmer.state = receiveBytes[2] == 0 ? 0 : 1;
                                        dimmer.dicPropert["brightness"] = receiveBytes[1];
                                        //dimmer.brightness = receiveBytes[1];
                                        if (receiveBytes[2] > 0)
                                            dimmer.lastState = Language.StringByID(StringId.Brightness) + " : " + receiveBytes[2] + "%";
                                        HomePage.UpdataLightView(dimmer);
                                    }
                                }
                            }
                        }
                        break;
                }
            }
            catch (Exception ex)
            {
                MainPage.Log($"Bus Rev Erorr : {ex.Message}");
            }
        }
        /// <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)
        {
            try
            {
                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(FileUtils.ReadFiles()));
                        outputStream.Write(bytes, 0, bytes.Length);
                        outputStream.Flush();
                    }
                    else
                    {
                        byte[] bytes;
                        if (!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", "");
                }
                else
                {
                    byte[] bytes = System.Text.Encoding.UTF8.GetBytes("请求命令无效!");
                    outputStream.Write(bytes, 0, bytes.Length);
                    outputStream.Flush();
                }
            }
            catch (Exception ex)
            {
                MainPage.Log("httpListener_EventHandler" + ex.ToString());
            }
        }
 
        /// <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;
                }
            }
        }
    }
}