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
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
using System;
using System.Collections.Generic;
using System.Threading;
 
namespace Shared.SimpleControl.Phone
{
    /// <summary>
    /// 通过灯光设备类型,显示出所有房间的灯光
    /// </summary>
    public class UserDeviceToSocket : FrameLayout
    {
        /// <summary>
        /// 当前设备界面
        /// </summary>
        static UserDeviceToSocket curView;
        VerticalScrolViewLayout bodyScrolView;
 
        /// <summary>
        /// 构造函数
        /// </summary>
        public UserDeviceToSocket ()
        {
            curView = this;
            BackgroundColor = SkinStyle.Current.MainColor;
            readAllStatus ();
        }
 
        Thread readEnergyThead;
        List<Common> EnergtLightList = new List<Common> ();
 
        public static void UpdataEnergy (string updataFlag, int energy)
        {
            Application.RunOnMainThread (() => {
                if (curView == null) {
                    return;
                }
                if (curView.bodyScrolView.Decelerating) {
                    return;
                }
                for (int i = 0; i < curView.bodyScrolView.ChildrenCount; i++) {
                    if (curView.bodyScrolView.GetChildren (i).GetType () == typeof (FrameLayout)) {
                        var lightRow = (FrameLayout)curView.bodyScrolView.GetChildren (i);
                        var o = lightRow.Tag;
                        if (o != null && o.ToString () == updataFlag) {
                            for (int j = 0; j < lightRow.ChildrenCount; j++) {
                                if (lightRow.GetChildren (j).GetType () == typeof (Button)) {
                                    var btn = (Button)lightRow.GetChildren (j);
                                    if (btn.Tag != null && btn.Tag.ToString () == DeviceType.LightEnergySwitch.ToString ()) {
                                        btn.Text = energy.ToString () + " W";
                                    }
                                }
                            }
                        }
                    }
                }
            });
        }
 
        /// <summary>
        /// 更新当前灯光
        /// </summary>
        public static void UpdateStatus (string updataFlag, byte brightness)
        {
            #region 更新用户主界面灯光点亮总数
            Application.RunOnMainThread (() => {
                int brighingCount = 0;
                foreach (var room in Room.Lists) {
                    if (string.IsNullOrEmpty(room.Name)) {
                        continue;
                    }
                    foreach (var light in room.DeviceList) {
                        if (light.Type == DeviceType.LightSwitchSocket) {
                            if ((light as LightSwitch).CurrentBrightness == 100)
                                brighingCount++;
                        }
                    }
                }
                UserDeviceView.UpdataDeviceCountNumber (brighingCount, R.MyInternationalizationString.ElectricalControl);
            });
            #endregion
           new System.Threading.Thread (() => {
               Application.RunOnMainThread (() => {
                   if (curView == null || curView.bodyScrolView.Decelerating) {
                       return;
                   }
                   for (int i = 0; i < curView.bodyScrolView.ChildrenCount; i++) {
                       if (curView.bodyScrolView.GetChildren (i).GetType () == typeof (FrameLayout)) {
                           var lightRow = (FrameLayout)curView.bodyScrolView.GetChildren (i);
                           var o = lightRow.Tag;
                           if (o != null && o.ToString () == updataFlag) {
                               for (int j = 0; j < lightRow.ChildrenCount; j++) {
                                   if (lightRow.GetChildren (j).GetType () == typeof (Button)) {
                                       var btn = (Button)lightRow.GetChildren (j);
                                       if (brightness == 0) {
                                           btn.IsSelected = false;
                                       } else {
                                           btn.IsSelected = true;
                                       }
                                   }
                               }
                           }
                       }
                   }
                   curView.updateLigthOnCount ();
               });
           }) { IsBackground = true}.Start();
        }
        /// <summary>
        /// 显示当前多少个灯是亮的
        /// </summary>
        void updateLigthOnCount ()
        {
            Application.RunOnMainThread (() => {
                if (curView == null)
                    return;
                for (int i = 0; i < curView.bodyScrolView.ChildrenCount; i++) {
                    if (curView.bodyScrolView.GetChildren (i).GetType () == typeof (RowLayout)) {
                        var roomRow = (RowLayout)curView.bodyScrolView.GetChildren (i);
                        if (roomRow.Tag != null) {
                            foreach (var room in Room.Lists) {
                                if (string.IsNullOrEmpty(room.Name)) {
                                    continue;
                                }
                                if (roomRow.Tag.ToString () == room.Name) {
                                    int oCount = 0;
                                    foreach (var light in room.DeviceList) {
                                        if (light.Type == DeviceType.LightSwitchSocket) {
                                            if ((light as LightSwitch).CurrentBrightness == 100)
                                                oCount++;
                                        }
                                    }
 
                                    for (int j = 0; j < roomRow.ChildrenCount; j++) {
                                        if (roomRow.GetChildren (j).GetType () == typeof (EditText)) {
                                            var et = (EditText)roomRow.GetChildren (j);
                                            et.Text = oCount.ToString ();
                                            if (oCount > 0) {
                                                et.TextColor = SkinStyle.Current.SelectedColor;
                                            } else
                                                et.TextColor = SkinStyle.Current.TextColor1;
                                        }
                                        if (roomRow.GetChildren (j).GetType () == typeof (Button)) {
                                            var btn = (Button)roomRow.GetChildren (j);
                                            if (oCount > 0) {
                                                btn.IsSelected = true;
                                            } else {
                                                btn.IsSelected = false;
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            });
        }
        bool showTip = true;
        /// <summary>
        /// 控制所有的灯
        /// </summary>
        /// <param name="b">The blue component.</param>
        void controlAllLight (byte b)
        {
            showTip = true;
 
            System.Threading.Tasks.Task.Run (() => {
                Application.RunOnMainThread (() => {
                    MainPage.Loading.Start ("Sending...");
                });
                foreach (var room in Room.Lists) {
                    if (string.IsNullOrEmpty(room.Name)) {
                        continue;
                    }
                    foreach (var common in room.DeviceList) {
                        byte [] returnBytes = null;
                        if (common.Type == DeviceType.LightSwitchSocket || common.Type == DeviceType.LightEnergySwitch) {
                            returnBytes = Control.ControlBytesSendHasReturn (Command.SetSingleLight, common.SubnetID, common.DeviceID, new byte [] { common.LoopID, b, 0, 0 });
                            if (showTip) {
                                Application.RunOnMainThread (() => {
                                    if (returnBytes == null) {
                                        string showTipText = common.Name + Language.StringByID (R.MyInternationalizationString.ControlFailure);
                                        MainPage.AddTip (showTipText);
                                    }
                                });
                            }
                        }
                    }
                }
 
                Application.RunOnMainThread (() => {
                    MainPage.Loading.Hide ();
                });
            });
        }
        /// <summary>
        /// 通过设备类型方式选择设备
        /// </summary>
        public void ShowAllRoomLights ()
        {
            #region 标题
            var topView = new FrameLayout () {
                Y = Application.GetRealHeight(36),
                Height = Application.GetRealHeight (90),
            };
            AddChidren (topView);
 
            var title = new Button () {
                TextAlignment = TextAlignment.Center,
                Text = Language.StringByID (R.MyInternationalizationString.ElectricalControl),
                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,
            };
            topView.AddChidren (logo);
            var back = new Button () {
                Height = Application.GetRealHeight (90),
                Width = Application.GetRealWidth (85),
                UnSelectedImagePath = "Item/Back.png",
                SelectedImagePath = "Item/BackSelected.png",
            };
            topView.AddChidren (back);
            back.MouseUpEventHandler += (sender, e) => {
                (Parent as PageLayout).PageIndex -= 1;
                if (readEnergyThead != null)
                    readEnergyThead.Abort ();
                curView = null;
            };
            #endregion
 
            #region 全开-全关
            var powerView = new FrameLayout () {
                Height = Application.GetRealHeight (110),
                BackgroundColor = SkinStyle.Current.TitileView,
                Y = topView.Bottom
            };
            AddChidren (powerView);
            var btnAllON = new Button () {
                Width = Application.GetRealWidth (320),
                TextSize = 16,
                TextID = R.MyInternationalizationString.ALLON,
                TextColor = SkinStyle.Current.TextColor1,
                SelectedTextColor = SkinStyle.Current.SelectedColor,
            };
            powerView.AddChidren (btnAllON);
            var btnAllOff = new Button () {
                Width = Application.GetRealWidth (320),
                X = btnAllON.Right,
                TextSize = 16,
                TextID = R.MyInternationalizationString.ALLOFF,
                TextColor = SkinStyle.Current.TextColor1,
                SelectedTextColor = SkinStyle.Current.SelectedColor,
            };
            var btnLine = new Button () {
                Width = 1,
                BackgroundColor = SkinStyle.Current.White20Transparent,
                X = btnAllON.Right,
                Y = 1,
            };
            powerView.AddChidren (btnLine);
            btnAllON.MouseUpEventHandler += (sender, e) => {
                btnAllON.TextColor = SkinStyle.Current.SelectedColor;
                btnAllOff.TextColor = SkinStyle.Current.TextColor1;
                Alert alert = new Alert (Language.StringByID (R.MyInternationalizationString.Tip),
                                         Language.StringByID (R.MyInternationalizationString.SureOpenAllSocket),
                                         Language.StringByID (R.MyInternationalizationString.Cancel),
                                         Language.StringByID (R.MyInternationalizationString.Confrim));
                alert.Show ();
                alert.ResultEventHandler += (sender2, e2) => {
                    if (e2) {
                        controlAllLight (100);
                    }
                };
            };
            powerView.AddChidren (btnAllOff);
            btnAllOff.MouseUpEventHandler += (sender, e) => {
                btnAllON.TextColor = SkinStyle.Current.TextColor1;
                btnAllOff.TextColor = SkinStyle.Current.SelectedColor;
                Alert alert = new Alert (Language.StringByID (R.MyInternationalizationString.Tip),
                                         Language.StringByID (R.MyInternationalizationString.SureCloseAllSocket),
                                         Language.StringByID (R.MyInternationalizationString.Cancel),
                                         Language.StringByID (R.MyInternationalizationString.Confrim));
                alert.Show ();
                alert.ResultEventHandler += (sender2, e2) => {
                    if (e2) {
                        controlAllLight (0);
                    }
                };
            };
            #endregion
 
            bodyScrolView = new VerticalScrolViewLayout () {
                Height = Application.GetRealHeight (Application.DesignHeight - 126 - 110),
                Y = powerView.Bottom,
                BackgroundColor = SkinStyle.Current.ViewColor
            };
            AddChidren (bodyScrolView);
 
            updateLigthOnCount ();
 
            #region 显示全部的灯光
            int LightCount = 0;
            foreach (var room in Room.Lists) {
                if (string.IsNullOrEmpty(room.Name)) {
                    continue;
                }
                foreach (var common in room.DeviceList) {
                    if (common.Type == DeviceType.LightSwitchSocket || common.Type == DeviceType.LightEnergySwitch) {
                        LightCount++;
                        var light = common as LightSwitch;
                        var lightView = new FrameLayout () {
                            Height = Application.GetRealHeight (130),
                            Tag = common.CommonLoopID
                        };
                        bodyScrolView.AddChidren (lightView);
 
                        //灯名
                        var tempDeviceName = new Button () {
                            X = Application.GetRealWidth (20),
                            TextAlignment = TextAlignment.CenterLeft,
                            Text = room.Name + "-" + light.Name,
                            TextColor = SkinStyle.Current.TextColor1,
                            SelectedTextColor = SkinStyle.Current.TextColor1,
                            Enable = false,
                        };
                        lightView.AddChidren (tempDeviceName);
 
                        var tempSiwtch = new Button () {
                            Width = Application.GetMinRealAverage (90),
                            Height = Application.GetMinRealAverage (53),
                            X = Application.GetRealWidth (640 - 90 - 20),
                            Gravity = Gravity.CenterVertical,
                            UnSelectedImagePath = "Item/SwitchClose.png",
                            SelectedImagePath = "Item/SwitchOpen.png",
                            IsSelected = light.CurrentBrightness == 100 ? true : false
                        };
                        lightView.AddChidren (tempSiwtch);
                        tempSiwtch.MouseUpEventHandler += (sender3, e3) => {
                            tempSiwtch.IsSelected = !tempSiwtch.IsSelected;
                            if (tempSiwtch.IsSelected) {
                                light.CurrentBrightness = 100;
                            } else {
                                light.CurrentBrightness = 0;
                            }
                            Control.ControlBytesSend (Command.SetSingleLight, light.SubnetID, light.DeviceID, new byte [] { light.LoopID, light.CurrentBrightness, light.DelayTimeHeight, light.DelayTimeLow });
                        };
 
                        if (light.CurrentBrightness == 100) {
                            tempSiwtch.IsSelected = true;
                        }
                        if (common.Type == DeviceType.LightEnergySwitch) {
                            Button btnEnergy = new Button () {
                                Width = Application.GetRealWidth (90),
                                Height = Application.GetRealHeight (30),
                                X = tempSiwtch.X,
                                Y = tempSiwtch.Bottom,
                                TextColor = SkinStyle.Current.TextColor1,
                                Tag = DeviceType.LightEnergySwitch.ToString (),
                                Text = "0 W"
                            };
                            lightView.AddChidren (btnEnergy);
                            EnergtLightList.Add (common);
                        }
 
                        Button btnEnergyNull = new Button () {
                            Y = Application.GetRealHeight (127),
                            Height = Application.GetRealHeight (3),
                            BackgroundColor = SkinStyle.Current.MainColor,
                            SelectedBackgroundColor = SkinStyle.Current.MainColor,
                        };
                        lightView.AddChidren (btnEnergyNull);
                    }
                }
            }
 
            readEnergyThead = new Thread ((obj) => {
                while (true) {
                    foreach (var c in EnergtLightList) {
                        Control.ControlBytesSend (Command.ReadAnalogValue, c.SubnetID, c.DeviceID, new byte [] { 10, c.LoopID }, SendCount.Zero);
                    }
                    System.Threading.Thread.Sleep (10000);
                }
            });
            readEnergyThead.Start ();
            #endregion
        }
 
        static System.Threading.Thread thread;
        /// <summary>
        /// 读取设备状态
        /// </summary>
        public static void readAllStatus (bool isRead = false)
        {
            if (isRead) {
                foreach (Room room in Room.Lists) {
                    if (room == null) {
                        continue;
                    }
                    var list = room.DeviceList.FindAll ((obj) => {
                        return obj.Type == DeviceType.LightSwitchSocket;
                    });
                    foreach (Common common in list) {
                        common.LastUpdateTime = DateTime.MinValue;
                    }
                }
            }
            if (thread != null) {
                thread.Abort ();
            }
            thread = new Thread (() => {
                try {
                    System.Collections.Generic.List<string> readList = new System.Collections.Generic.List<string> ();
                    foreach (Room room in Room.Lists) {
                        if (room == null) {
                            continue;
                        }
                        var list = room.DeviceList.FindAll ((obj) => {
                            return obj.LastUpdateTime.AddMinutes (Common.Time) <= DateTime.Now && obj.Type == DeviceType.LightSwitchSocket;
                        });
                        foreach (Common common in list) {
                            var s = common.Type + "_" + common.SubnetID + "_" + common.DeviceID;
                            //已经读取过当前设备就不再读取
                            if (readList.Contains (s)) {
                                continue;
                            }
                            Thread.Sleep (500);
                            readList.Add (s);
                            Control.ControlBytesSendHasReturn (Command.ReadLightEquipmentAllLoopBrightness, common.SubnetID, common.DeviceID, new byte [] { });
                        }
                    }
                } catch (Exception xe) {
                    Console.WriteLine (xe.Message);
                } finally {
                    thread = null;
                }
            });
            thread.Start ();
        }
    }
}