JLChen
2020-06-04 6d55af8792cf8fbef0055e677b900fc352dba9a2
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
using System;
using System.Collections.Generic;
using System.Threading;
using System.Net.NetworkInformation;
using System.Net;
using System.Text;
 
namespace Shared.SimpleControl.Phone
{
    public class SystemMqttGatewaySetting : FrameLayout
    {
        static byte TYPEStandardMQTT = 5;
        static byte TYPEHdlMQTT = 4;
        Common gatewayDeicve;
 
        public SystemMqttGatewaySetting()
        {
            BackgroundColor = SkinStyle.Current.MainColor;
        }
 
        public override void RemoveFromParent()
        {
            base.RemoveFromParent();
        }
 
        public void ShowView(Common GatewayCommon)
        {
            gatewayDeicve = GatewayCommon;
 
            #region 标题
            var topView = new FrameLayout()
            {
                Y = Application.GetRealHeight(36),
                Height = Application.GetRealHeight(90),
            };
            AddChidren(topView);
 
            var title = new Button()
            {
                TextAlignment = TextAlignment.Center,
                Text = GatewayCommon.Name,
                TextSize = 19,
                TextColor = SkinStyle.Current.TextColor1
            };
            topView.AddChidren(title);
 
            var logo = new Button()
            {
                Width = Application.GetRealWidth(154),
                Height = Application.GetRealHeight(90),
                X = Application.GetRealWidth(486),
                UnSelectedImagePath = MainPage.LogoString,
                Gravity = Gravity.CenterVertical,
            };
            topView.AddChidren(logo);
            var back = new Button()
            {
                Height = Application.GetRealHeight(90),
                Width = Application.GetRealWidth(85),
                UnSelectedImagePath = "Item/Back.png",
                Gravity = Gravity.CenterVertical,
            };
            topView.AddChidren(back);
            back.MouseUpEventHandler += (sender, e) => {
                UserMiddle.SettingPageView.PageIndex = UserMiddle.SettingPageView.ChildrenCount - 2;
            };
            #endregion
 
            var BodyView = new FrameLayout () {
                Width = LayoutParams.MatchParent,
                Height = Application.GetRealHeight (Application.DesignHeight - 126 - 90),
                Y = topView.Bottom,
                BackgroundColor = SkinStyle.Current.ViewColor,
            };
            AddChidren (BodyView);
 
            var TipView = new Button () {
                Y = topView.Bottom,
                Width = LayoutParams.MatchParent,
                Height = Application.GetRealHeight (50),
                BackgroundColor = 0xFFFFFFE0,
                TextSize = 11,
                //Text = "一键配置网关远程连接参数",
                TextID = R.MyInternationalizationString.OneKeyConfigurationGateway,
                TextColor = 0xFF1F1F1F,
                IsMoreLines = true
 
            };
            AddChidren (TipView);
 
 
 
            //var bottomView = new FrameLayout () {
            //    Y = Application.GetRealHeight (846 + 90 - UserMiddle.userMenuItemHeight),
            //    Height = Application.GetRealHeight (UserMiddle.userMenuItemHeight + 3),
            //    BackgroundColor = SkinStyle.Current.MainColor,
            //};
            //AddChidren (bottomView);
 
      
 
            Button SettingButton = new Button () {
                Y = Application.GetRealHeight (Application.DesignHeight - 90),
                Height = Application.GetRealHeight (90),
                TextColor = SkinStyle.Current.TextColor1,
                BackgroundColor = SkinStyle.Current.MainColor,
                TextID = R.MyInternationalizationString.OneKeyConfiguration,
                TextAlignment = TextAlignment.Center
            };
            AddChidren (SettingButton);
 
 
            SettingButton.MouseUpEventHandler += (sender, e) => {
                RemoteSettingWithHDLMQTT ();
                //SetGateWayLocalEncryption (gatewayDeicve, false);
            };
        }
 
 
 
 
        /// <summary>
        /// 远程配置 HDL MQTT模式
        /// </summary>
        void RemoteSettingWithHDLMQTT ()
        {
 
            MainPage.Loading.Start (Language.StringByID (R.MyInternationalizationString.disposition) + "...");
            System.Threading.Tasks.Task.Run (() => {
                try {
                    //1.修改用户名信息 和修改连接模式
                    if (SetGateWayModelInfo ()) {
                        //2.修改管理信息
                        if (SetGateWayAdminInfo ()) {
                            Application.RunOnMainThread (() => {
                                new Alert ("", Language.StringByID (R.MyInternationalizationString.ConfigurationSucceeded), Language.StringByID (R.MyInternationalizationString.Close)).Show ();
                            });
                        }
                    }
 
                } catch (Exception ex) {
                    Console.WriteLine (ex.Message);
                    Application.RunOnMainThread (() => {
                        //修改网关连接模式失败
                        new Alert ("", Language.StringByID (R.MyInternationalizationString.ConfigurationFailed), Language.StringByID (R.MyInternationalizationString.Close)).Show ();
                    });
                    bool canRemove = false;
                } finally {
                    Application.RunOnMainThread (() => {
                        MainPage.Loading.Hide ();
                        //this.RemoveAll ();
                    });
                }
            });
            //Room.InitAllRoom ();
        }
 
        bool SetGateWayModelInfo ()
        {
            byte [] gatewayBytes = new byte [60];
            //gatewayDeicve.Remote_GroupName = MainPage.LoginUser.AccountString;//UserConfig.Instance.CurrentRegion.RegionName;
            //gatewayDeicve.Remote_ProjectName = gatewayDeicve.MAC.Replace (".", "");//UserConfig.Instance.CurrentRegion.RegionName;
            //gatewayDeicve.Remote_UserName = "Admin";
            //gatewayDeicve.Remote_Password = "c" + MainPage.LoginUser.ID;
            ////gatewayDeicve.Remote_Password = "85521566";
            //byte [] ggn = new byte [20];
            //byte [] b1 = CommonPage.MyEncodingGB2312.GetBytes (gatewayDeicve.Remote_GroupName);
            //gatewayDeicve.Remote_GroupName = CommonPage.MyEncodingGB2312.GetString (b1);
            //Array.Copy (b1, 0, ggn, 0, 20 < b1.Length ? 20 : b1.Length);
 
            //byte [] gpn = new byte [20];
            //byte [] b2 = CommonPage.MyEncodingGB2312.GetBytes (gatewayDeicve.Remote_ProjectName);
            //Array.Copy (b2, 0, gpn, 0, 20 < b2.Length ? 20 : b2.Length);
 
            //byte [] gun = new byte [8];
            //byte [] b3 = CommonPage.MyEncodingGB2312.GetBytes (gatewayDeicve.Remote_UserName);
            //Array.Copy (b3, 0, gun, 0, 8 < b3.Length ? 8 : b3.Length);
 
            //byte [] gpw = new byte [8];
            //byte [] b4 = CommonPage.MyEncodingGB2312.GetBytes (gatewayDeicve.Remote_Password);
            //Array.Copy (b4, 0, gpw, 0, 8 < b4.Length ? 8 : b4.Length);
 
            var Remote_GroupName = MainPage.LoginUser.AccountString;//UserConfig.Instance.CurrentRegion.RegionName;
            var Remote_ProjectName = gatewayDeicve.MAC.Replace (".", "");//UserConfig.Instance.CurrentRegion.RegionName;
            var Remote_UserName = "Admin";
            var Remote_Password = "c" + MainPage.LoginUser.ID;
            //gatewayDeicve.Remote_Password = "85521566";
            byte [] ggn = new byte [20];
            byte [] b1 = CommonPage.MyEncodingGB2312.GetBytes (Remote_GroupName);
            //Remote_GroupName = CommonPage.MyEncodingGB2312.GetString (b1);
            Array.Copy (b1, 0, ggn, 0, 20 < b1.Length ? 20 : b1.Length);
 
            byte [] gpn = new byte [20];
            byte [] b2 = CommonPage.MyEncodingGB2312.GetBytes (Remote_ProjectName);
            Array.Copy (b2, 0, gpn, 0, 20 < b2.Length ? 20 : b2.Length);
 
            byte [] gun = new byte [8];
            byte [] b3 = CommonPage.MyEncodingGB2312.GetBytes (Remote_UserName);
            Array.Copy (b3, 0, gun, 0, 8 < b3.Length ? 8 : b3.Length);
 
            byte [] gpw = new byte [8];
            byte [] b4 = CommonPage.MyEncodingGB2312.GetBytes (Remote_Password);
            Array.Copy (b4, 0, gpw, 0, 8 < b4.Length ? 8 : b4.Length);
 
 
            gatewayBytes [0] = TYPEHdlMQTT;//HDL MQTT
            Array.Copy (ggn, 0, gatewayBytes, 1, 20);
            Array.Copy (gpn, 0, gatewayBytes, 21, 20);
            Array.Copy (gun, 0, gatewayBytes, 41, 8);
            Array.Copy (gpw, 0, gatewayBytes, 49, 8);
            byte [] backBytes = Control.ControlBytesSendHasReturn (Command.SetGateWayModelInfo, gatewayDeicve.SubnetID, gatewayDeicve.DeviceID, gatewayBytes);
            string error1 = Language.StringByID (R.MyInternationalizationString.ConfigurationFailed) + "(1)";
            return CheckIsSuccessfulWithBytes (backBytes, error1);
        }
 
        ///// <summary>
        ///// 修改管理员信息
        ///// </summary>
        ///// <param name="adminBytes"></param>
        ///// <returns></returns>
        //bool SetGateWayAdminInfo (byte [] adminBytes)
        //{
        //    byte [] name = CommonPage.MyEncodingGB2312.GetBytes (MainPage.LoginUser.AccountString);
        //    byte [] backBytes;
        //    if (adminBytes != null && adminBytes.Length >= 73) {//返回数据正常,只修改账号信息
        //        Array.Copy (name, 0, adminBytes, 0, 36 < name.Length ? 36 : name.Length);
        //        backBytes = Control.ControlBytesSendHasReturn (Command.SetGateWayAdminInfo, gatewayDeicve.SubnetID, gatewayDeicve.DeviceID, adminBytes);
 
        //    } else {
        //        byte [] currentRegionIdBytes = CommonPage.MyEncodingGB2312.GetBytes (UserConfig.Instance.CurrentRegion.RegionID.ToString ());
        //        //byte [] currentRegionIdBytes = CommonPage.MyEncodingGB2312.GetBytes (UserConfig.Instance.CurrentRegion.RegionID);
        //        //var currentRegionIdBytes = Utlis.IntToByteArray (UserConfig.Instance.CurrentRegion.RegionID);
        //        adminBytes = new byte [73];
        //        adminBytes [36] = 1;//住宅标志位
        //        Array.Copy (name, 0, adminBytes, 0, 36 < name.Length ? 36 : name.Length);
        //        Array.Copy (currentRegionIdBytes, 0, adminBytes, 37, 36 < currentRegionIdBytes.Length ? 36 : currentRegionIdBytes.Length);
        //        backBytes = Control.ControlBytesSendHasReturn (Command.SetGateWayAdminInfo, gatewayDeicve.SubnetID, gatewayDeicve.DeviceID, adminBytes);
 
        //    }
        //    return CheckIsSuccessfulWithBytes (backBytes);
        //}
 
        /// <summary>
        /// 修改管理员信息
        /// </summary>
        /// <param name="adminBytes"></param>
        /// <returns></returns>
        bool SetGateWayAdminInfo ()
        {
            byte [] name = CommonPage.MyEncodingGB2312.GetBytes (MainPage.LoginUser.AccountString);
            //byte [] currentRegionIdBytes = CommonPage.MyEncodingGB2312.GetBytes (UserConfig.Instance.CurrentRegion.RegionID);
            byte [] currentRegionIdBytes = CommonPage.MyEncodingGB2312.GetBytes (UserConfig.Instance.CurrentRegion.RegionID.ToString ());
            //var currentRegionIdBytes = Utlis.IntToByteArray (UserConfig.Instance.CurrentRegion.RegionID);
            byte [] adminBytes = new byte [73];
            adminBytes [36] = 1;//住宅标志位
            Array.Copy (name, 0, adminBytes, 0, 36 < name.Length ? 36 : name.Length);
            Array.Copy (currentRegionIdBytes, 0, adminBytes, 37, 36 < currentRegionIdBytes.Length ? 36 : currentRegionIdBytes.Length);
 
            byte [] backBytes = Control.ControlBytesSendHasReturn (Command.SetGateWayAdminInfo, gatewayDeicve.SubnetID, gatewayDeicve.DeviceID, adminBytes);
            string error1 = Language.StringByID (R.MyInternationalizationString.ConfigurationFailed) + "(2)";
            return CheckIsSuccessfulWithBytes (backBytes, error1);
        }
 
        /// <summary>
        /// 判断是否修复成功 F8 = 成功; F5=失败
        /// </summary>
        /// <param name="backBytes"></param>
        /// <returns></returns>
        private bool CheckIsSuccessfulWithBytes (byte [] backBytes, string errorStr)
        {
            if (backBytes == null) {
                Application.RunOnMainThread (() => {
                    new Alert ("", Language.StringByID (R.MyInternationalizationString.GatewayNoResponse),
                               Language.StringByID (R.MyInternationalizationString.Close)).Show ();
                });
                return false;
            } else if (backBytes [0] == 0xF8) {//F8 = 成功; F5=失败
                return true;
            } else {
                Application.RunOnMainThread (() => {
                    new Alert ("", errorStr,
                               Language.StringByID (R.MyInternationalizationString.Close)).Show ();
                });
                return false;
            }
        }
 
 
        ///// <summary>
        ///// 写配置参数成功后执行下一步操作
        ///// </summary>
        //void GatewaySettingSucceeded ()
        //{
 
        //    //var currentRegionIdBytes = Utlis.IntToByteArray (UserConfig.Instance.CurrentRegion.RegionID);
        //    //byte [] currentRegionIdBytes = CommonPage.MyEncodingGB2312.GetBytes (UserConfig.Instance.CurrentRegion.RegionID);
        //    byte [] currentRegionIdBytes = CommonPage.MyEncodingGB2312.GetBytes (UserConfig.Instance.CurrentRegion.RegionID.ToString ());
        //    byte [] sendBytes = new byte [36];
        //    Array.Copy (currentRegionIdBytes, 0, sendBytes, 0, 36 < currentRegionIdBytes.Length ? 36 : currentRegionIdBytes.Length);
 
        //    //var currentRegionIdBytes = Utlis.IntToByteArray (UserConfig.Instance.CurrentRegion.RegionID);
        //    var bindReginIdStatus = Control.ControlBytesSendHasReturn (Command.Write_APP_Data_STORE_1D5E_CMD, gatewayDeicve.SubnetID, gatewayDeicve.DeviceID, sendBytes);
        //    if (bindReginIdStatus == null) {
        //        Application.RunOnMainThread (() => {
        //            new Alert ("", Language.StringByID (R.MyInternationalizationString.GatewayNoResponse), Language.StringByID (R.MyInternationalizationString.Close)).Show ();
        //        });
        //        return;
        //    } else if (bindReginIdStatus [0] != 0xF8) {
        //        //new Alert ("", "网关绑定住宅失败!", Language.StringByID (R.MyInternationalizationString.Close)).Show ();
        //        string error3 = Language.StringByID (R.MyInternationalizationString.ConfigurationFailed) + "(3)";
        //        Application.RunOnMainThread (() => {
        //            new Alert ("", error3, Language.StringByID (R.MyInternationalizationString.Close)).Show ();
        //        });
        //        return;
        //    }
 
 
        //    Application.RunOnMainThread (() => {
        //        new Alert ("", Language.StringByID (R.MyInternationalizationString.ConfigurationSucceeded), Language.StringByID (R.MyInternationalizationString.Close)).Show ();
        //    });
 
 
        //}
 
 
        ////static byte [] AseKeyBytes;
        ///// <summary>
        ///// 修改网关本地加密
        ///// </summary>
        ///// <param name="gatewayDeicve"></param>
        ///// <param name="isEncrypt"></param>
        ///// <returns></returns>
        //bool SetGateWayLocalEncryption (GatewayBase gatewayDeicve, bool isEncrypt = true)
        //{
        //    if (UserConfig.Instance.CurrentRegion == null) return false;
 
        //    byte [] aseKey = new byte [16];
        //    //生成加密Key
        //    //byte [] AseKeyBytes;
        //    byte [] homeId = System.Text.Encoding.ASCII.GetBytes (UserConfig.Instance.CurrentRegion.RegionID.ToString());
        //    Array.Copy (homeId, 0, aseKey, 0, 16 < homeId.Length ? 16 : homeId.Length);
        
        //    byte [] sendBytes = new byte [17];
        //    sendBytes [0] = isEncrypt ? (byte)1 : (byte)0; //0:不加密 1:加密
        //    Array.Copy (aseKey, 0, sendBytes, 1, 16);
        //    byte [] backBytes = Control.ControlBytesSendHasReturn (Command.SetGateWayLocalEncryption, gatewayDeicve.SubnetID, gatewayDeicve.DeviceID, sendBytes);
        //    return CheckIsSuccessfulWithBytes (backBytes,"ddd");
        //}
 
    }
}