陈嘉乐
2021-04-01 802f200c004348defca5cefd047ad843c74abadd
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
using Shared;
using HDL_ON.Stan;
using HDL_ON.UI.CSS;
using HDL_ON.Entity;
using System.Collections.Generic;
using System;
using HDL_ON.DriverLayer;
 
namespace HDL_ON.UI
{
    /// <summary>
    /// 晾衣架界面
    /// </summary>
    public class ClothesHangerPage : DeviceFunctionCardCommonForm
    {
        #region ■ 变量声明___________________________
 
        /// <summary>
        /// 图标
        /// </summary>
        private Button btnIcon = null;
        /// <summary>
        /// 上升
        /// </summary>
        Button btnUp;
        /// <summary>
        /// 停止
        /// </summary>
        Button btnStop;
        /// <summary>
        /// 下降
        /// </summary>
        Button btnDown;
        /// <summary>
        /// 烘干控件
        /// </summary>
        ClothesHangerControl hotDryView;
        /// <summary>
        /// 风干控件
        /// </summary>
        ClothesHangerControl windDryView;
        /// <summary>
        /// 消毒控件
        /// </summary>
        ClothesHangerControl disinfectView;
        /// <summary>
        /// 照明控件
        /// </summary>
        ClothesHangerControl lightingView;
 
 
        #endregion
 
        #region ■ 初始化_____________________________
 
        /// <summary>
        /// 初始化白色区域的内容
        /// </summary>
        public override void InitFrameWhiteContent()
        {
            base.ShowColltionButton = false;
            base.SetTitleText(Language.StringByID(StringId.Electric));
 
            //初始化第一个索引页的内容
            this.InitFrameWhiteContent1();
 
            //刷新界面状态
            this.RefreshFormStatu();
            //读取状态
            new System.Threading.Thread(() =>
            {
                Control.Ins.SendReadCommand(device);
            })
            { IsBackground = true }.Start();
        }
 
        /// <summary>
        /// 初始化第一个索引页的内容
        /// </summary>
        private void InitFrameWhiteContent1()
        {
            btnIcon = new Button()
            {
                Gravity = Gravity.CenterHorizontal,
                Y = Application.GetRealHeight(85),
                Width = Application.GetRealWidth(195),
                Height = Application.GetRealHeight(121),
                UnSelectedImagePath = "FunctionIcon/Electrical/ClothesHanger/ClothesHangerBgIcon.png",
                SelectedImagePath = "FunctionIcon/Electrical/ClothesHanger/ClothesHangerBgIconOn.png",
            };
            FrameWhiteCentet1.AddChidren(btnIcon);
 
            #region 上升 停止 下降
            btnUp = new Button()
            {
                X = Application.GetRealWidth(42),
                Y = Application.GetRealHeight(236),
                Width = Application.GetRealWidth(28),
                Height = Application.GetRealWidth(28),
                UnSelectedImagePath = "FunctionIcon/Electrical/ClothesHanger/ClothesHangerUpIcon.png",
                SelectedImagePath = "FunctionIcon/Electrical/ClothesHanger/ClothesHangerUpIconOn.png",
            };
            FrameWhiteCentet1.AddChidren(btnUp);
 
            btnStop = new Button()
            {
                Gravity = Gravity.CenterHorizontal,
                Y = Application.GetRealHeight(236),
                Width = Application.GetRealWidth(28),
                Height = Application.GetRealWidth(28),
                UnSelectedImagePath = "FunctionIcon/Electrical/ClothesHanger/ClothesHangerStopIcon.png",
                SelectedImagePath = "FunctionIcon/Electrical/ClothesHanger/ClothesHangerStopIconOn.png",
            };
            FrameWhiteCentet1.AddChidren(btnStop);
 
            btnDown = new Button()
            {
                X = Application.GetRealWidth(257),
                Y = Application.GetRealHeight(236),
                Width = Application.GetRealWidth(28),
                Height = Application.GetRealWidth(28),
                UnSelectedImagePath = "FunctionIcon/Electrical/ClothesHanger/ClothesHangerDownIcon.png",
                SelectedImagePath = "FunctionIcon/Electrical/ClothesHanger/ClothesHangerDownIconOn.png",
            };
            FrameWhiteCentet1.AddChidren(btnDown);
 
            #region 控制
            btnUp.MouseDownEventHandler = (sender, e) => {
                btnUp.IsSelected = true;
            };
            btnUp.MouseUpEventHandler = (sender, e) => {
                new System.Threading.Thread(() =>
                {
                    System.Threading.Thread.Sleep(2000);
                    Application.RunOnMainThread(() =>
                    {
                        btnUp.IsSelected = false;
                    });
                })
                { IsBackground = true }.Start();
                Dictionary<string, string> d = new Dictionary<string, string>();
                d.Add(FunctionAttributeKey.Position, "up");
                Control.Ins.SendWriteCommand(device, d);
            };
            //----
            btnStop.MouseDownEventHandler = (sender, e) => {
                btnStop.IsSelected = true;
            };
            btnStop.MouseUpEventHandler = (sender, e) => {
                new System.Threading.Thread(() =>
                {
                    System.Threading.Thread.Sleep(2000);
                    Application.RunOnMainThread(() =>
                    {
                        btnStop.IsSelected = false;
                    });
                })
                { IsBackground = true }.Start();
                Dictionary<string, string> d = new Dictionary<string, string>();
                d.Add(FunctionAttributeKey.Position, "stop");
                Control.Ins.SendWriteCommand(device, d);
            };
            //-----
            btnDown.MouseDownEventHandler = (sender, e) => {
                btnDown.IsSelected = true;
            };
            btnDown.MouseUpEventHandler = (sender, e) => {
                new System.Threading.Thread(() =>
                {
                    System.Threading.Thread.Sleep(2000);
                    Application.RunOnMainThread(() =>
                    {
                        btnDown.IsSelected = false;
                    });
                })
                { IsBackground = true }.Start();
                Dictionary<string, string> d = new Dictionary<string, string>();
                d.Add(FunctionAttributeKey.Position, "down");
                Control.Ins.SendWriteCommand(device, d);
            };
            #endregion
 
            #endregion
 
            #region 菜单
            var menu = new List<string>() {
                FunctionAttributeKey.HotDry,
                FunctionAttributeKey.WindDry,
                FunctionAttributeKey.Disinfect,
                FunctionAttributeKey.OnOff
            };
 
            int index = 0;
            List<string> attrs = device.GetAttributes();
 
            hotDryView = new ClothesHangerControl(
                            "FunctionIcon/Electrical/ClothesHanger/ClothesHangerHotDryIcon.png",
                            "FunctionIcon/Electrical/ClothesHanger/ClothesHangerHotDryIconOn.png",
                            Language.StringByID(StringId.HotDry),
                            device.GetAttrState(FunctionAttributeKey.HotDryTimeLeft));
            hotDryView.X = 0;
            hotDryView.Y = Application.GetRealHeight(304);
            hotDryView.Width = Application.GetRealWidth(96);
            hotDryView.Height = Application.GetRealHeight(74);//28 29 14 3
            if (attrs.Contains(menu[0]))
            {
                FrameWhiteCentet1.AddChidren(hotDryView);
 
                EventHandler<MouseEventArgs> eventHandler = (sender, e) =>
                {
                    string onoff = hotDryView.Lighting ? "false" : "true";
                    device.SetAttrState(FunctionAttributeKey.HotDry, onoff);
                    Dictionary<string, string> d = new Dictionary<string, string>();
                    d.Add(FunctionAttributeKey.HotDry, onoff);
                    Control.Ins.SendWriteCommand(device, d);
                };
                hotDryView.SetThouchEvent(eventHandler);
 
                index++;
            }
 
            windDryView = new ClothesHangerControl(
                                "FunctionIcon/Electrical/ClothesHanger/ClothesHangerWindDryIcon.png",
                                "FunctionIcon/Electrical/ClothesHanger/ClothesHangerWindDryIconOn.png",
                                Language.StringByID(StringId.WindDry),
                                device.GetAttrState(FunctionAttributeKey.WindDryTimeLeft));
            windDryView.Y = Application.GetRealHeight(304);
            windDryView.Width = Application.GetRealWidth(96);
            windDryView.Height = Application.GetRealHeight(74);
            if (attrs.Contains(menu[1]))
            {
                if (index == 1)
                {
                    windDryView.Gravity = Gravity.CenterHorizontal;
                }
                FrameWhiteCentet1.AddChidren(windDryView);
 
                EventHandler<MouseEventArgs> eventHandler = (sender, e) =>
                {
                    string onoff = windDryView.Lighting ? "false" : "true";
                    device.SetAttrState(FunctionAttributeKey.WindDry, onoff);
                    Dictionary<string, string> d = new Dictionary<string, string>();
                    d.Add(FunctionAttributeKey.WindDry, onoff);
                    Control.Ins.SendWriteCommand(device, d);
                };
                windDryView.SetThouchEvent(eventHandler);
 
                index++;
            }
 
 
 
            disinfectView = new ClothesHangerControl(
                                "FunctionIcon/Electrical/ClothesHanger/ClothesHangerDisinfectIcon.png",
                                "FunctionIcon/Electrical/ClothesHanger/ClothesHangerDisinfectIconOn.png",
                                Language.StringByID(StringId.Disinfect),
                                device.GetAttrState(FunctionAttributeKey.DisinfectTimeLeft));
            disinfectView.Y = Application.GetRealHeight(304);
            disinfectView.Width = Application.GetRealWidth(96);
            disinfectView.Height = Application.GetRealHeight(74);
            if (attrs.Contains(menu[2]))
            {
                if (index == 1)
                {
                    disinfectView.Gravity = Gravity.CenterHorizontal;
                }
                else if (index == 2)
                {
                    disinfectView.X = Application.GetRealWidth(266 - 33);
                }
                FrameWhiteCentet1.AddChidren(disinfectView);
 
                EventHandler<MouseEventArgs> eventHandler = (sender, e) =>
                {
                    string onoff = disinfectView.Lighting ? "false" : "true";
                    device.SetAttrState(FunctionAttributeKey.Disinfect, onoff);
                    Dictionary<string, string> d = new Dictionary<string, string>();
                    d.Add(FunctionAttributeKey.Disinfect, onoff);
                    Control.Ins.SendWriteCommand(device, d);
                };
                disinfectView.SetThouchEvent(eventHandler);
 
                index++;
            }
 
            lightingView = new ClothesHangerControl(
                                "FunctionIcon/Electrical/ClothesHanger/ClothesHangerLightIcon.png",
                                "FunctionIcon/Electrical/ClothesHanger/ClothesHangerLightIconOn.png",
                                Language.StringByID(StringId.Lighting),
                                "");
            lightingView.Width = Application.GetRealWidth(96);
            lightingView.Height = Application.GetRealHeight(74);
            if (attrs.Contains(menu[3]))
            {
                if (index > 2)
                {
                    lightingView.Y = Application.GetRealHeight(409);
                }
                else
                {
                    lightingView.Y = Application.GetRealHeight(304);
                    if (index == 1)
                    {
                        lightingView.Gravity = Gravity.CenterHorizontal;
                    }
                    else if (index == 2)
                    {
                        lightingView.X = Application.GetRealWidth(266 - 33);
                    }
                }
                FrameWhiteCentet1.AddChidren(lightingView);
 
                EventHandler<MouseEventArgs> eventHandler = (sender, e) =>
                {
                    string onoff = lightingView.Lighting ? "off" : "on";
                    device.SetAttrState(FunctionAttributeKey.OnOff, onoff);
                    Dictionary<string, string> d = new Dictionary<string, string>();
                    d.Add(FunctionAttributeKey.OnOff, onoff);
                    Control.Ins.SendWriteCommand(device, d);
                };
                lightingView.SetThouchEvent(eventHandler);
 
                index++;
            }
 
            #endregion
 
 
            #region 时间设置
            var timeSetView = new ClothesHangerControl(
                                "FunctionIcon/Electrical/ClothesHanger/ClothesHangerTimeIcon.png",
                                "FunctionIcon/Electrical/ClothesHanger/ClothesHangerTimeIconOn.png",
                                Language.StringByID(StringId.Time),
                                "");
            timeSetView.Width = Application.GetRealWidth(96);
            timeSetView.Height = Application.GetRealHeight(74);
            timeSetView.Y = Application.GetRealHeight(409);
            timeSetView.X = Application.GetRealWidth(266 - 33);
 
 
            EventHandler<MouseEventArgs> eventHandler_timeSetView = (sender, e) =>
            {
                var setTimePage = new ClothesHangerSetTimePage(device);
                MainPage.BasePageView.AddChidren(setTimePage);
                setTimePage.LoadPage();
                MainPage.BasePageView.PageIndex = MainPage.BasePageView.ChildrenCount - 1;
            };
            timeSetView.SetThouchEvent(eventHandler_timeSetView);
 
            FrameWhiteCentet1.AddChidren(timeSetView);
 
            #endregion
        }
        #endregion
 
        #region ■ 设备状态反馈_______________________
 
        /// <summary>
        /// 设备状态反馈
        /// </summary>
        /// <param name="i_LocalDevice"></param>
        public override void DeviceStatuPush(Function i_LocalDevice)
        {
            //不是同一个东西
            if (this.device.sid != i_LocalDevice.sid) { return; }
 
            //刷新界面状态
            this.RefreshFormStatu();
        }
 
        #endregion
 
        #region ■ 发送各种命令_______________________
 
        #endregion
 
        #region ■ 刷新界面状态_______________________
 
        /// <summary>
        /// 刷新界面状态
        /// </summary>
        private void RefreshFormStatu()
        {
            Application.RunOnMainThread(() =>
            {
                btnIcon.IsSelected = true;
                #region 点亮文本
                var hotDryTemp = device.attributes.Find((sta) => sta.key == FunctionAttributeKey.HotDry);
                if (hotDryTemp != null)
                {
                    hotDryView.SetViewStatus(hotDryTemp.state == "true");
                }
                var windDryTemp = device.attributes.Find((sta) => sta.key == FunctionAttributeKey.WindDry);
                if (windDryTemp != null)
                {
                    windDryView.SetViewStatus(windDryTemp.state == "true");
                }
                var disinfectTemp = device.attributes.Find((sta) => sta.key == FunctionAttributeKey.Disinfect);
                if (disinfectTemp != null)
                {
                    disinfectView.SetViewStatus(disinfectTemp.state == "true");
                }
                var lightTemp = device.attributes.Find((sta) => sta.key == FunctionAttributeKey.OnOff);
                if (lightTemp != null)
                {
                    lightingView.SetViewStatus(lightTemp.state == "on");
                }
                MainPage.Log($"时间戳:{device.time_stamp}  时间:{device.GeteTime()} 烘干:{hotDryTemp.state} 风干:{windDryTemp.state} 消毒:{disinfectTemp.state} 灯光状态:{lightTemp.state}");
                #endregion
 
                #region 修改时间
                var hotDryTimeLeftTemp = device.attributes.Find((sta) => sta.key == FunctionAttributeKey.HotDryTimeLeft);
                if (hotDryTimeLeftTemp != null)
                {
                    hotDryView.ChangeTime(hotDryTimeLeftTemp.state);
                }
                var windDryTimeLeftTemp = device.attributes.Find((sta) => sta.key == FunctionAttributeKey.WindDryTimeLeft);
                if (windDryTimeLeftTemp != null)
                {
                    windDryView.ChangeTime(windDryTimeLeftTemp.state);
                }
                var disinfectTimeLeftTemp = device.attributes.Find((sta) => sta.key == FunctionAttributeKey.DisinfectTimeLeft);
                if (disinfectTimeLeftTemp != null)
                {
                    disinfectView.ChangeTime(disinfectTimeLeftTemp.state);
                }
                #endregion
 
 
            });
        }
 
        #endregion
 
        #region ■ 一般方法___________________________
 
        #endregion
    }
 
    public class ClothesHangerControl : FrameLayout
    {
        private Button btnIcon;
        private Button btnTitle;
        private Button btnTime;
        /// <summary>
        /// 倒计时时间
        /// </summary>
        private int leftTime = 0;
        /// <summary>
        /// 控件是否点亮
        /// </summary>
        public bool Lighting = false;
 
        public ClothesHangerControl(string iconPath1, string iconPath2, string title, string time)
        {
            btnIcon = new Button()
            {
                Gravity = Gravity.CenterHorizontal,
                Width = Application.GetRealWidth(28),
                Height = Application.GetRealWidth(28),
                UnSelectedImagePath = iconPath1,
                SelectedImagePath = iconPath2,
            };
            this.AddChidren(btnIcon);
 
            btnTitle = new Button()
            {
                Gravity = Gravity.Center,
                Text = title,
                Height = Application.GetRealHeight(42),
                TextColor = CSS_Color.TextualColor,
                SelectedTextColor = CSS_Color.MainColor,
                TextSize = CSS_FontSize.PromptFontSize_FirstLevel,
            };
            this.AddChidren(btnTitle);
 
            btnTime = new Button()
            {
                Gravity = Gravity.CenterHorizontal,
                Y = Application.GetRealHeight(74-32),
                Height = Application.GetRealHeight(32),
                Text = time,
                TextColor = 0x00000000,
                SelectedTextColor = CSS_Color.MainColor,
                TextSize = CSS_FontSize.PromptFontSize_SecondaryLevel,
            };
            this.AddChidren(btnTime);
        }
        /// <summary>
        /// 修改显示时间
        /// </summary>
        /// <param name="newTime"></param>
        public void ChangeTime(string newTime)
        {
            int.TryParse(newTime, out leftTime);
            if (leftTime > 0)
            {
                Application.RunOnMainThread(() =>
                {
                    btnTime.Text = new TimeSpan(0, leftTime, 0).ToString().Remove(5, 3);
                });
                leftTime--;
            }
            else if (leftTime == 0)
            {
                Application.RunOnMainThread(() =>
                {
                    btnTime.Text = "";
                });
            }
            if (countdownThread == null)
            {
                countdownThread = new System.Threading.Thread(() =>
                {
 
                    while (true)
                    {
                        if (leftTime > 0)
                        {
                            Application.RunOnMainThread(() =>
                            {
                                btnTime.Text = new TimeSpan(0, leftTime, 0).ToString().Remove(5,3) ;
                            });
                            leftTime--;
                        }
                        else if(leftTime == 0)
                        {
                            Application.RunOnMainThread(() =>
                            {
                                btnTime.Text = "";
                            });
                        }
                        System.Threading.Thread.Sleep(60000);
                    }
                })
                { IsBackground = true };
                countdownThread.Start();
            }
        }
 
        public void SetViewStatus(bool state)
        {
            btnIcon.IsSelected = btnTitle.IsSelected = btnTime.IsSelected = Lighting = state;
        }
 
        /// <summary>
        /// 设置点击事件
        /// </summary>
        public void SetThouchEvent(EventHandler<MouseEventArgs> eventHandler)
        {
            btnIcon.MouseUpEventHandler = eventHandler;
            btnTime.MouseUpEventHandler = eventHandler;
            btnTitle.MouseUpEventHandler = eventHandler;
        }
 
        /// <summary>
        /// 倒计时线程
        /// </summary>
        System.Threading.Thread countdownThread;
    }
}