1
wxr
2023-03-31 7e42cc13a14b7de31c9f5d5c61cdf24f3246335d
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
using System;
//using Java.Lang;
 
namespace Shared.SimpleControl.Phone
{
    public class TemporaryPassword : Dialog
    {
        FrameLayout mainFrameLayout = new FrameLayout { BackgroundColor = 0xFF3b3d3c };
        DoorLock doorLock;
        Button passwordText1;//临时密码6位数
        Button passwordText2;
        Button passwordText3;
        Button passwordText4;
        Button passwordText5;
        Button passwordText6;
        TemporaryPassword obj;
        private static System.Threading.Thread th;
 
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="door_lock">Door lock.</param>
        public TemporaryPassword (DoorLock door_lock)
        {
            this.doorLock = door_lock;
            AddChidren (mainFrameLayout);
            showSetTempPassword ();
            readStatus (door_lock);
 
            th = new System.Threading.Thread ((obj) => {
                ThFun ();
            });
            th.Start ();
        }
 
        /// <summary>
        /// 读门锁临时密码信息
        /// </summary>
        /// <param name="door_lock">Door lock.</param>
        void readStatus (DoorLock door_lock)
        {
            System.Threading.Tasks.Task.Run (() => {
                var randomNum = Control.ControlBytesSendHasReturn (Command.CreatConnection, door_lock.SubnetID, door_lock.DeviceID, new byte [] { 1 });
                if (randomNum != null) {
                    byte [] randomBytes = new byte [8];
                    System.Array.Copy (randomNum, 1, randomBytes, 0, 8);
                    byte [] inpuptBytes = { 0x1F, 0x52, door_lock.LoopID };
                    var encryptionBytes = Security.Encryption (randomBytes, inpuptBytes);
                    var resultBytes = Control.ControlBytesSendHasReturn (Command.SendEncryptionAfterConnected, door_lock.SubnetID, door_lock.DeviceID, encryptionBytes);
                    if (resultBytes == null) {
                        Application.RunOnMainThread (() => {
                            //可能是设备的ID被改变了,或者加密时间超过5s后没有反馈
                        });
                        return;
                    } else {
                        var ddBytes = Security.Encryption (randomBytes, resultBytes);
                        door_lock.Enable = ddBytes [3].ToString ();
                        door_lock.Times = ddBytes [4].ToString ();
                        door_lock.VailidTimeL = ddBytes [5].ToString ();
                        door_lock.VailidTimeH = ddBytes [6].ToString ();
                        door_lock.tempPasswordText1 = ddBytes [7].ToString ();
                        door_lock.tempPasswordText2 = ddBytes [8].ToString ();
                        door_lock.tempPasswordText3 = ddBytes [9].ToString ();
                        door_lock.tempPasswordText4 = ddBytes [10].ToString ();
                        door_lock.tempPasswordText5 = ddBytes [11].ToString ();
                        door_lock.tempPasswordText6 = ddBytes [12].ToString ();
                        door_lock.dynamicTempPassword = ddBytes [7].ToString () + ddBytes [8].ToString () + ddBytes [9].ToString () + ddBytes [10].ToString () + ddBytes [11].ToString () + ddBytes [12].ToString ();
                        int VailidTimeHH = Convert.ToInt32 (door_lock.VailidTimeH);
                        int VailidTimeLL = Convert.ToInt32 (door_lock.VailidTimeL);
                        door_lock.VailidTime = Convert.ToInt32 ((VailidTimeHH) * 256 + VailidTimeLL);
                    }
                    IO.FileUtils.SaveEquipmentMessage (door_lock, door_lock.LoopID.ToString ());
                    Application.RunOnMainThread (() => {
                        showSetTempPassword ();
                    });
                }
            });
        }
 
        void showSetTempPassword ()
        {
            mainFrameLayout.RemoveAll ();
 
            #region 标题
            var topView = new FrameLayout () {
                Y = Application.GetRealHeight (36),
                Height = Application.GetRealHeight (90),
                BackgroundColor = 0xff1e1f20,
            };
 
            mainFrameLayout.AddChidren (topView);
 
            var title = new Button () {
                TextAlignment = TextAlignment.Center,
                TextID = R.MyInternationalizationString.UserTempPassword,
                TextSize = 19,
            };
            topView.AddChidren (title);
 
            var logo = new Button () {
                Width = Application.GetRealWidth (154),
                Height = Application.GetRealHeight (90),
                X = Application.GetRealWidth (486),
                UnSelectedImagePath = MainPage.LogoString,
            };
            topView.AddChidren (logo);
            var back = new Button () {
                Height = Application.GetRealHeight (90),
                Width = Application.GetRealWidth (85),
                UnSelectedImagePath = "Item/Back.png",
                SelectedImagePath = "Item/BackSelected.png",
                Gravity = Gravity.CenterVertical,
            };
            topView.AddChidren (back);
            back.MouseUpEventHandler += (sender, e) => {
                Close ();
            };
 
            var btnl = new Button () {
                Height = 1,
                BackgroundColor = 0xFF000000,
                Y = topView.Height - 1
            };
            topView.AddChidren (btnl);
            #endregion
 
            #region bodyScrolView
            var bodyScrolView = new FrameLayout () {
                Height = Application.GetRealHeight (1136 - 126 - 98),
                Y = topView.Bottom,
            };
            mainFrameLayout.AddChidren (bodyScrolView);
 
            Button validTimeText = new Button () {
                X = Application.GetRealWidth (80),
                Y = Application.GetRealHeight (50),
                Width = Application.GetRealWidth (300),
                Height = Application.GetRealHeight (80),
                TextAlignment = TextAlignment.CenterLeft,
                Text = Language.StringByID (R.MyInternationalizationString.ValidTime),
            };
            bodyScrolView.AddChidren (validTimeText);
 
            var inputMinText = new EditText () {
                X = Application.GetRealWidth (80),
                Y = validTimeText.Bottom + Application.GetRealHeight (4),
                Width = Application.GetRealWidth (450),
                Height = Application.GetRealHeight (100),
                PlaceholderText = doorLock.VailidTime.ToString (),
                //Text = doorLock.VailidTime.ToString () ,
                PlaceholderTextColor = 0xffc1c1c1,
                TextAlignment = TextAlignment.Center,
                BorderWidth = 1,
                Radius = 5,
                BorderColor = SkinStyle.Current.BorderColor,
            };
            bodyScrolView.AddChidren (inputMinText);
 
            Button unitMin = new Button () {
                Height = Application.GetRealHeight (50),
                Width = Application.GetRealWidth (100),
                X = inputMinText.Right,
                Y = Application.GetRealHeight (190),
                Text = Language.StringByID (R.MyInternationalizationString.Minute),
                TextAlignment = TextAlignment.CenterLeft,
                TextSize = 12,
            };
            bodyScrolView.AddChidren (unitMin);
 
            var remindPic = new Button () {
                Height = Application.GetRealHeight (40),
                Width = Application.GetRealWidth (40),
                X = Application.GetRealWidth (80),
                Y = inputMinText.Bottom + Application.GetRealHeight (18),
                UnSelectedImagePath = "DoorLockPic/door_lock_alert.png",
            };
            bodyScrolView.AddChidren (remindPic);
 
            var maxMinuteText = new Button () {
                Width = Application.GetRealWidth (550),
                Height = Application.GetRealHeight (80),
                X = remindPic.Right + Application.GetRealWidth (8),
                Y = inputMinText.Bottom,
                Text = Language.StringByID (R.MyInternationalizationString.MaxValidTime),
                TextAlignment = TextAlignment.CenterLeft,
                TextSize = 10,
            };
            bodyScrolView.AddChidren (maxMinuteText);
 
            Button validCountText = new Button () {
                X = Application.GetRealWidth (80),
                Y = inputMinText.Bottom + Application.GetRealHeight (110),
                Width = Application.GetRealWidth (300),
                Height = Application.GetRealHeight (80),
                TextAlignment = TextAlignment.CenterLeft,
                Text = Language.StringByID (R.MyInternationalizationString.ValidCount),
            };
            bodyScrolView.AddChidren (validCountText);
 
            var inputCountText = new EditText () {
                Width = Application.GetRealWidth (450),
                Height = Application.GetRealHeight (100),
                X = Application.GetRealWidth (80),
                Y = validCountText.Bottom + Application.GetRealHeight (10),
                PlaceholderText = doorLock.Times,
                PlaceholderTextColor = 0xffc1c1c1,
                TextAlignment = TextAlignment.Center,
                BorderWidth = 1,
                Radius = 5,
                BorderColor = SkinStyle.Current.BorderColor,
            };
            bodyScrolView.AddChidren (inputCountText);
 
            Button unitCount = new Button () {
                Height = Application.GetRealHeight (50),
                Width = Application.GetRealWidth (100),
                X = inputCountText.Right + Application.GetRealWidth (2),
                Y = Application.GetRealHeight (490),
                Text = Language.StringByID (R.MyInternationalizationString.Ci),
                TextAlignment = TextAlignment.CenterLeft,
                TextSize = 12,
            };
            bodyScrolView.AddChidren (unitCount);
 
            var remindPic2 = new Button () {
                Height = Application.GetRealHeight (40),
                Width = Application.GetRealWidth (40),
                X = Application.GetRealWidth (80),
                Y = inputCountText.Bottom + Application.GetRealHeight (18),
                UnSelectedImagePath = "DoorLockPic/door_lock_alert.png",
            };
            bodyScrolView.AddChidren (remindPic2);
 
            var maxValidCount = new Button () {
                Width = Application.GetRealWidth (550),
                Height = Application.GetRealHeight (80),
                X = remindPic2.Right + Application.GetRealWidth (8),
                Y = inputCountText.Bottom,
                Text = Language.StringByID (R.MyInternationalizationString.MaxValicCount),
                TextAlignment = TextAlignment.CenterLeft,
                TextSize = 10,
            };
            bodyScrolView.AddChidren (maxValidCount);
 
            var remindFrameLayout = new FrameLayout () {
                Height = Application.GetRealHeight (350),
                Y = remindPic2.Bottom + Application.GetRealHeight (90),
            };
            bodyScrolView.AddChidren (remindFrameLayout);
 
            Button TempPasswordText = new Button () {
                X = Application.GetRealWidth (80),
                Y = Application.GetRealHeight (10),
                Width = Application.GetRealWidth (300),
                Height = Application.GetRealHeight (80),
                TextAlignment = TextAlignment.CenterLeft,
                Text = Language.StringByID (R.MyInternationalizationString.DynamicTempPassword),
            };
            remindFrameLayout.AddChidren (TempPasswordText);
 
            passwordText1 = new Button () {
                Height = Application.GetRealHeight (100),
                Width = Application.GetRealWidth (80),
                TextAlignment = TextAlignment.Center,
                X = Application.GetRealWidth (70),
                Y = TempPasswordText.Bottom + Application.GetRealHeight (10),
                Text = doorLock.tempPasswordText1,
                TextSize = 28,
            };
 
            remindFrameLayout.AddChidren (passwordText1);
 
            passwordText2 = new Button () {
                Height = Application.GetRealHeight (100),
                Width = Application.GetRealWidth (80),
                TextAlignment = TextAlignment.Center,
                Y = Application.GetRealHeight (100),
                X = passwordText1.Right + Application.GetRealWidth (2),
                Text = doorLock.tempPasswordText2,
                TextSize = 28,
            };
            remindFrameLayout.AddChidren (passwordText2);
 
            passwordText3 = new Button () {
                Height = Application.GetRealHeight (100),
                Width = Application.GetRealWidth (80),
                TextAlignment = TextAlignment.Center,
                Y = Application.GetRealHeight (100),
                X = passwordText2.Right + Application.GetRealWidth (2),
                Text = doorLock.tempPasswordText3,
                TextSize = 28,
            };
            remindFrameLayout.AddChidren (passwordText3);
 
            passwordText4 = new Button () {
                Height = Application.GetRealHeight (100),
                Width = Application.GetRealWidth (80),
                TextAlignment = TextAlignment.Center,
                Y = Application.GetRealHeight (100),
                X = passwordText3.Right + Application.GetRealWidth (2),
                Text = doorLock.tempPasswordText4,
                TextSize = 28,
            };
            remindFrameLayout.AddChidren (passwordText4);
 
            passwordText5 = new Button () {
                Height = Application.GetRealHeight (100),
                Width = Application.GetRealWidth (80),
                TextAlignment = TextAlignment.Center,
                Y = Application.GetRealHeight (100),
                X = passwordText4.Right + Application.GetRealWidth (2),
                Text = doorLock.tempPasswordText5,
                TextSize = 28,
            };
            remindFrameLayout.AddChidren (passwordText5);
 
            passwordText6 = new Button () {
                Height = Application.GetRealHeight (100),
                Width = Application.GetRealWidth (80),
                TextAlignment = TextAlignment.Center,
                Y = Application.GetRealHeight (100),
                X = passwordText5.Right + Application.GetRealWidth (2),
                Text = doorLock.tempPasswordText6,
                TextSize = 28,
            };
            remindFrameLayout.AddChidren (passwordText6);
            #endregion
 
            #region bottomFrameLayout
            var bottomFrameLayout = new FrameLayout () {
                Height = Application.GetRealHeight (98),
                Y = Application.GetRealHeight (1136 - 95),
            };
            mainFrameLayout.AddChidren (bottomFrameLayout);
            var bottomLine = new Button () {
                Height = Application.GetRealHeight (1),
                BackgroundColor = 0xFF000000,
            };
            bottomFrameLayout.AddChidren (bottomLine);
 
            Button generateTempPassword = new Button () {
                Y = bottomLine.Bottom,
                Height = Application.GetRealHeight (97),
                TextID = R.MyInternationalizationString.GenerateTemporaryPassword,
                TextSize = 15,
                BackgroundColor = 0xFF121212,
                SelectedBackgroundColor = 0xFFFC5806,
            };
            bottomFrameLayout.AddChidren (generateTempPassword);
 
            generateTempPassword.MouseUpEventHandler += (sender, e) => {
                var alert = new Alert ("", Language.StringByID (R.MyInternationalizationString.GenerateTemporaryDynamicPasswordAgain),
                                        Language.StringByID (R.MyInternationalizationString.cancel), Language.StringByID (R.MyInternationalizationString.confirm)) {
                };
                alert.ResultEventHandler += (sender1, e1) => {
                    if (e1) {
                        button1_Click (sender, e);
                        int passwrodValidTime;
                        int passwordValidCount;
                        if (string.IsNullOrEmpty (inputMinText.Text)) {
                            passwrodValidTime = Convert.ToInt32 (inputMinText.PlaceholderText);
                        } else {
                            passwrodValidTime = Convert.ToInt32 (inputMinText.Text);
                        }
                        if (string.IsNullOrEmpty (inputCountText.Text)) {
                            passwordValidCount = Convert.ToInt32 (inputCountText.PlaceholderText);
                        } else {
                            passwordValidCount = Convert.ToInt32 (inputCountText.Text);
                        }
 
                        if (passwordValidCount > 15 || passwordValidCount < 1) {
                            new Alert ("", Language.StringByID (R.MyInternationalizationString.MaxValicCount), Language.StringByID (R.MyInternationalizationString.Close)).Show ();
 
                            return;
                        }
 
                        System.Threading.Tasks.Task.Run (() => {
                            System.Threading.Thread.Sleep (6000);
                            var randomNumSet = Control.ControlBytesSendHasReturn (Command.CreatConnection, doorLock.SubnetID, doorLock.DeviceID, new byte [] { 1 });
                            if (randomNumSet != null) {
                                Console.WriteLine ("send:" + randomNumSet.ToString ());
                                byte [] randomBytes = new byte [8];
                                System.Array.Copy (randomNumSet, 1, randomBytes, 0, 8);
                                var sendBytes = new byte [13];
                                sendBytes [0] = 0x1F;
                                sendBytes [1] = 0x54;
                                sendBytes [2] = doorLock.LoopID;
                                sendBytes [3] = 1;//enable,0 : unable / 1:enable   / 失败 0xF5
                                sendBytes [4] = Convert.ToByte (passwordValidCount);//可用次数,0 ~ 15
                                sendBytes [5] = Convert.ToByte (passwrodValidTime % 256);//时间段低位
                                sendBytes [6] = Convert.ToByte (passwrodValidTime / 256);//时间段高位
                                sendBytes [7] = Convert.ToByte (passwordText1.Text); //0 ~ 9
                                sendBytes [8] = Convert.ToByte (passwordText2.Text);
                                sendBytes [9] = Convert.ToByte (passwordText3.Text);
                                sendBytes [10] = Convert.ToByte (passwordText4.Text);
                                sendBytes [11] = Convert.ToByte (passwordText5.Text);
                                sendBytes [12] = Convert.ToByte (passwordText6.Text);
                                var inpuptBytes = sendBytes;
                                var encryptionBytes = Security.Encryption (randomBytes, inpuptBytes);
                                var resultBytes = Control.ControlBytesSendHasReturn (Command.SendEncryptionAfterConnected, doorLock.SubnetID, doorLock.DeviceID, encryptionBytes);
                                if (resultBytes == null) {
                                    Application.RunOnMainThread (() => {
                                        new Tip () { MaxWidth = 150, Text = Language.StringByID (R.MyInternationalizationString.TargetsSetFail), Direction = AMPopTipDirection.Down, CloseTime = 2 }.Show ((View)sender);
                                    });
                                    return;
                                } else {
                                    //F8= 成功; F5 = 失败
                                    var ddBytes = Security.Encryption (randomBytes, resultBytes);
                                    var result = ddBytes [2];
                                    if (result == 0xF8) {
                                        Application.RunOnMainThread (() => {
                                            new Tip () { MaxWidth = 150, Text = Language.StringByID (R.MyInternationalizationString.SetSuccessfully), Direction = AMPopTipDirection.Down, CloseTime = 2 }.Show ((View)sender);
                                        });
                                        readStatus (doorLock);
                                    }
                                }
                            }
                        });
                    }
                };
                alert.Show ();
                #endregion
            };
 
        }
 
        /// <summary>
        /// 每隔一分钟自动刷新的方法
        /// </summary>
        //void updateStatus ()
        //{
        //    System.Threading.Tasks.Task.Run (() => {
        //        if (doorLock.LastUpdateTime.AddMinutes (Common.Time) <= DateTime.Now) {
        //            readStatus (doorLock);
        //        }  
        //      });
        //}
 
        /// <summary>
        /// 每隔一分钟自动刷新的方法
        /// </summary>
        private void ThFun ()
        {
            try {
                while (true) {
                    //执行方法()  ;  
                    //Console.WriteLine ("ssss333333");
                    System.Threading.Thread.Sleep (60000);//每1分钟刷新一次   以毫秒为单位  
                    readStatus (doorLock);
                }
            } catch { }
        }
 
        /// <summary>
        /// 生成随机数
        /// </summary>
        public int [] getRandomNum (int num, int minValue, int maxValue)
        {
            Random ra = new Random (unchecked((int)DateTime.Now.Ticks));
            int [] arrNum = new int [num];
            int tmp = 0;
            for (int i = 0; i <= num - 1; i++) {
                tmp = ra.Next (minValue, maxValue); //随机取数
                arrNum [i] = getNum (arrNum, tmp, minValue, maxValue, ra); //取出值赋到数组中
            }
            return arrNum;
        }
 
        public int getNum (int [] arrNum, int tmp, int minValue, int maxValue, Random ra)
        {
            int n = 0;
            while (n <= arrNum.Length - 1) {
                if (arrNum [n] == tmp) //利用循环判断是否有重复
                {
                    tmp = ra.Next (minValue, maxValue); //重新随机获取。
                    getNum (arrNum, tmp, minValue, maxValue, ra);//递归:如果取出来的数字和已取得的数字有重复就重新随机获取。
                }
                n++;
            }
            return tmp;
        }
 
        private void button1_Click (object sender, System.EventArgs e)
        {
            int [] arr = getRandomNum (6, 0, 10); //从0至10中取出6个互不相同的随机数
            int i = 0;
            string temp = "";
            while (i <= arr.Length - 1) {
                // temp = arr [i].ToString ();
                i++;
            }
            passwordText1.Text = arr [0].ToString (); //显示在label1中
            passwordText2.Text = arr [1].ToString ();
            passwordText3.Text = arr [2].ToString ();
            passwordText4.Text = arr [3].ToString ();
            passwordText5.Text = arr [4].ToString ();
            passwordText6.Text = arr [5].ToString ();
        }
    }
}