CrabtreeOn,印度客户定制APP,迁移2.0平台版本
1
wxr
2023-03-31 d53e6af2c5f17838fa79659614b15a2a1f383399
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
using System;
using System.Threading;
 
namespace Shared.SimpleControl.Phone
{
    /// <summary>
    /// 空调设备房间
    /// </summary>
    public class UserDeviceToAC : FrameLayout
    {
        /// <summary>
        /// 当前界面
        /// </summary>
        static UserDeviceToAC curView;
        VerticalScrolViewLayout equipmentListScrolView;
        static ACMethod acBLL;
 
        /// <summary>
        /// 构造函数
        /// </summary>
        public UserDeviceToAC ()
        {
            curView = this;
            BackgroundColor = SkinStyle.Current.MainColor;
            readAllStatus ();
        }
 
        /// <summary>
        /// 更新当前空调
        /// </summary>
        public static void UpdateStatus (AC ac)
        {
            #region 更新用户主界面灯光点亮总数
            int acOponeCount = 0;
            foreach (var room in Room.Lists) {
                if (string.IsNullOrEmpty(room.Name)) {
                    continue;
                }
                foreach (var acTemp in room.DeviceList) {
                    if (acTemp.Type == DeviceType.HVAC || acTemp.Type == DeviceType.ACInfrared || acTemp.Type == DeviceType.ACPanel )
                        if ((acTemp as AC).Power == 1) {
                            acOponeCount++;
                        }
                }
            }
            UserDeviceView.UpdataDeviceCountNumber (acOponeCount, Language.StringByID (R.MyInternationalizationString.AC));
            #endregion
            Application.RunOnMainThread (() => {
                if (curView == null) {
                    return;
                }
                for (int i = 0; i < curView.equipmentListScrolView.ChildrenCount; i++) {
                    if (curView.equipmentListScrolView.GetChildren (i).GetType () == typeof (RowLayout)) {
                        var acRow = (RowLayout)curView.equipmentListScrolView.GetChildren (i);
 
                        if (acRow.Tag != ac) {
                            continue;
                        }
                        //注意全局变量只能针对同一个空调,这里是多个空调,所以变量不能用全局变量
                        for (int j = 0; j < acRow.ChildrenCount; j++) {
                            if (acRow.GetChildren (j).GetType () == typeof (Button)) {
                                var btn = (Button)acRow.GetChildren (j);
                                if (btn.Tag == null) {
                                    continue;
                                }
                                if (btn.Tag.ToString () == "Switch") {
                                    if (ac.Power == 0) {
                                        btn.IsSelected = false;
                                    } else {
                                        btn.IsSelected = true;
                                    }
                                } else if (btn.Tag.ToString () == "Temperature") {
                                    acBLL.UpdataACModeTemperature (ac, ac.SetMode, btn);
                                } else if (btn.Tag.ToString () == "Mode") {
                                   
                                        acBLL.UpdataACHostModeIcon (ac.SetMode, btn);
 
                                } else if (btn.Tag.ToString () == "Wind") {
                                   
                                        acBLL.UpdataACHostWindIcon (ac.SetFanSpeed, btn);
 
                                }
                            }
                        }
                    }
                }
            });
        }
 
        /// <summary>
        /// 显示房间的所有空调
        /// </summary>
        public void showAllRoomAC ()
        {
            acBLL = new ACMethod ();
 
            #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.AC),
                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;
                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
            };
            powerView.AddChidren (btnAllON);
            var btnAllOff = new Button () {
                Width = Application.GetRealWidth (320),
                X = btnAllON.Right,
                TextSize = 16,
                TextID = R.MyInternationalizationString.ALLOFF,
                TextColor = SkinStyle.Current.TextColor1
            };
            powerView.AddChidren (btnAllOff);
            var btnLine = new Button () {
                Width = 1,
                BackgroundColor = SkinStyle.Current.White20Transparent,
                X = btnAllON.Right,
            };
            powerView.AddChidren (btnLine);
           
            #endregion
 
            equipmentListScrolView = new VerticalScrolViewLayout () {
                Width = LayoutParams.MatchParent,
                Height = Application.GetRealHeight (1136 - 126 - 110),
                Y = powerView.Bottom,
                BackgroundColor = SkinStyle.Current.ViewColor
            };
            AddChidren (equipmentListScrolView);
 
            #region 显示全部的空调
            int devcieCount = 0;
            foreach (var room in Room.Lists) {
                if (string.IsNullOrEmpty(room.Name)) {
                    continue;
                }
                foreach (var common in room.DeviceList.FindAll ((obj) => { return obj.Type == DeviceType.HVAC || obj.Type == DeviceType.ACPanel || obj.Type == DeviceType.ACInfrared || obj.Type == DeviceType.CustomAC;})) {
                    var ac = common as AC;
                    EventHandler<MouseEventArgs> openACControlPageEvent = (button, mouseEventArgs) => {
                        var userACView = new UserACPage (ac, room);
                        UserMiddle.DevicePageView.AddChidren (userACView);
                        userACView.showRoomAC ();
                        UserMiddle.DevicePageView.PageIndex = 2;
                    };
                    RowLayout acView = new RowLayout () {
                        Height = Application.GetRealHeight (140),
                        Tag = ac
                    };
                    equipmentListScrolView.AddChidren (acView);
                    acView.MouseUpEventHandler += openACControlPageEvent;
                    Button btnACViewBottomNull = new Button () {
                        Y = Application.GetRealHeight (135),
                        Height = Application.GetRealHeight (5),
                        BackgroundColor = SkinStyle.Current.MainColor,
                        SelectedBackgroundColor = SkinStyle.Current.MainColor,
                    };
                    acView.AddChidren (btnACViewBottomNull);
 
                    var tempDeviceName = new Button () {
                        X = Application.GetRealWidth (30),
                        Height = Application.GetRealHeight (60),
                        TextAlignment = TextAlignment.CenterLeft,
                        Text = room.Name + " - " + ac.Name,
                        TextColor = SkinStyle.Current.TextColor1
                    };
                    acView.AddChidren (tempDeviceName);
                    tempDeviceName.MouseUpEventHandler += openACControlPageEvent;
 
                    Button btnMode = new Button () {
                        X = Application.GetRealWidth (40),
                        Y = Application.GetRealHeight (40),
                        Width = Application.GetMinRealAverage (100),
                        Height = Application.GetMinRealAverage (100),
                        Tag = "Mode"
                    };
                    acView.AddChidren (btnMode);
                    acBLL.UpdataACHostModeIcon (ac.SetMode, btnMode);
                    btnMode.MouseUpEventHandler += openACControlPageEvent;
 
                    Button btnWind = new Button () {
                        X = btnMode.Right + Application.GetRealWidth (20),
                        Y = Application.GetRealHeight (40),
                        Width = Application.GetMinRealAverage (100),
                        Height = Application.GetMinRealAverage (100),
                        Tag = "Wind"
                    };
                    acView.AddChidren (btnWind);
                    acBLL.UpdataACHostWindIcon (ac.SetFanSpeed, btnWind);
                    btnWind.MouseUpEventHandler += openACControlPageEvent;
 
                    var btnSetTemperature = new Button () {
                        Width = Application.GetRealWidth (310),
                        X = Application.GetRealWidth (280),
                        Y = Application.GetRealHeight (20),
                        TextSize = 26,
                        TextAlignment = TextAlignment.Center,
                        Tag = "Temperature",
                        TextColor = SkinStyle.Current.TextColor1
                    };
                    acView.AddChidren (btnSetTemperature);
                    acBLL.UpdataACModeTemperature (ac, ac.SetMode, btnSetTemperature);
                    btnSetTemperature.MouseUpEventHandler += openACControlPageEvent;
 
                    var tempSiwtch = new Button () {
                        Width = Application.GetMinRealAverage (90),
                        Height = Application.GetMinRealAverage (53),
                        X = Application.GetRealWidth (640 - 90 - 20),
                        Y = Application.GetRealHeight (67),
                        UnSelectedImagePath = "Item/SwitchClose.png",
                        SelectedImagePath = "Item/SwitchOpen.png",
                        Tag = "Switch"
                    };
                    acView.AddChidren (tempSiwtch);
                    tempSiwtch.MouseUpEventHandler += (sender3, e3) => {
 
                        //tempSiwtch.IsSelected = !tempSiwtch.IsSelected;
                        //ac.Power = tempSiwtch.IsSelected ? (byte)1 : (byte)0;
                        byte bPower = tempSiwtch.IsSelected ? (byte)0 : (byte)1;
                        if (ac.Type == DeviceType.CustomAC) {
                            var acCommon = common as CustomAC;
                            acCommon.Power = bPower;
                            var controlCommand = acCommon.customACControlLists.Find ((obj) => obj.UniversalModeType == acCommon.Power);
                            if (controlCommand != null) {
                                Control.ControlBytesSend (Command.SetCommonSwitch, acCommon.SubnetID, acCommon.DeviceID, new byte [] { controlCommand.UniversalID, 255 }, SendCount.Zero);
                                IO.FileUtils.SaveEquipmentMessage (acCommon, acCommon.LoopID.ToString ());
                            }
                        } else {
                            //acBLL.ControlAC (ac);
                            SendControlAc (ac, bPower);
                        }
                    };
 
                    if (0 < ac.Power) {
                        tempSiwtch.IsSelected = true;
                    }
                    devcieCount++;
                }
            }
 
           
            if (devcieCount > 0) {
                btnAllON.MouseUpEventHandler += (sender, e) => {
                    btnAllON.TextColor = SkinStyle.Current.TextColor1;
                    Alert alert = new Alert ("",
                                    "Open All AC",
                                   Language.StringByID (R.MyInternationalizationString.Cancel),
                                   Language.StringByID (R.MyInternationalizationString.Confrim));
                    alert.Show ();
                    alert.ResultEventHandler += (sender2, e2) => {
                        if (e2) {
 
                            controlAllAC (1);
                        }
                    };
                    //controlAllAC (1);
                };
                btnAllON.MouseDownEventHandler += (ss, dd) => {
                    btnAllON.TextColor = SkinStyle.Current.SelectedColor;
                };
                powerView.AddChidren (btnAllOff);
                btnAllOff.MouseUpEventHandler += (sender, e) => {
                    btnAllOff.TextColor = SkinStyle.Current.TextColor1;
                    //controlAllAC (0);
                    Alert alert = new Alert ("",
                              "Close All AC",
                             Language.StringByID (R.MyInternationalizationString.Cancel),
                             Language.StringByID (R.MyInternationalizationString.Confrim));
                    alert.Show ();
                    alert.ResultEventHandler += (sender2, e2) => {
                        if (e2) {
 
                            controlAllAC (0);
                        }
                    };
                };
                btnAllOff.MouseDownEventHandler += (ss, dd) => {
                    btnAllOff.TextColor = SkinStyle.Current.SelectedColor;
                };
            }
            #endregion
        }
 
        /// <summary>
        /// 控制所有空调
        /// </summary>
        void controlAllAC (byte b)
        {
            if (CommonPage.IsRemote) {
                if (!SmartHome.MqttCommon.IsGatewayOnline) {
                    new Alert ("", "Gateway offline" + ErrorCode.ControlFailure, "Close").Show ();
                    return;
                }
            }
            MainPage.Loading.Start ("Sending...");
 
            System.Threading.Tasks.Task.Run (() => {
                foreach (var room in Room.Lists) {
                    if (room == null)
                        continue;
                    if (string.IsNullOrEmpty(room.Name)) {
                        continue;
                    }
                    var list = room.DeviceList.FindAll ((obj) => {
                        return obj.Type == DeviceType.ACPanel || obj.Type == DeviceType.ACInfrared || obj.Type == DeviceType.HVAC;
                    });
                    foreach (var common in list) {
                        //2020-02-10
                        byte [] returnBytes = null;
                        var acCommon = common as AC;
                        acCommon.Power = b;
                        //acBLL.ControlAC (acCommon);
 
                        if (acCommon.Type == DeviceType.ACPanel) {
                            returnBytes = Control.ControlBytesSendHasReturn (Command.InstructionPanelKey, acCommon.SubnetID, acCommon.DeviceID, new byte [] { 3, acCommon.Power, acCommon.LoopID });
                        } else if (acCommon.Type == DeviceType.ACDevice || acCommon.Type == DeviceType.HVAC || acCommon.Type == DeviceType.ACInfrared) {
                            returnBytes = Control.ControlBytesSendHasReturn (Command.SetACMode, acCommon.SubnetID, acCommon.DeviceID,
                                new byte [] {acCommon.LoopID,acCommon.TemperatureMode,acCommon.IndoorTemperature,acCommon.CoolTemperature,
                                    acCommon.HeatTemperature,acCommon.AutoTemperature,acCommon.ChuShiTemperature,acCommon.RealModeAndFanSpeed,
                                    acCommon.Power,acCommon.SetMode,acCommon.SetFanSpeed,acCommon.SetTemperature,acCommon.ShaoFanMode});
                        } else {
                            continue;
                        }
 
                        if (returnBytes == null) {
                            Application.RunOnMainThread (() => {
                                string tipString = Language.StringByID (R.MyInternationalizationString.ControlFailure);
                                var btnTip = new Button () {
                                    Height = Application.GetRealHeight (140),
                                    Text = acCommon.Name + tipString,// + "..." + controlDeviceIndex.ToString () + @"/" + sceneDeviceCount.ToString (),
                                    BackgroundColor = 0xFFD7D7D7,
                                };
                                MainPage.MainFrameLayout.AddChidren (btnTip);
                                btnTip.MouseUpEventHandler += (sender, e) => {
                                    btnTip.RemoveFromParent ();
                                };
 
                                System.Threading.Tasks.Task.Run (() => {
                                    System.Threading.Thread.Sleep (3000);
                                    Application.RunOnMainThread (() => {
                                        btnTip.RemoveFromParent ();
                                    });
                                });
 
                            });
                        }
 
                    }
                    var listCustomAC = room.DeviceList.FindAll ((obj) => {
                        return obj.Type == DeviceType.CustomAC;
                    });
                    foreach (var common in listCustomAC) {
                        var acCommon = common as CustomAC;
                        var controlCommand = acCommon.customACControlLists.Find ((obj) => obj.UniversalModeType == acCommon.Power);
                        if (controlCommand != null) {
                            Control.ControlBytesSend (Command.SetCommonSwitch, acCommon.SubnetID, acCommon.DeviceID, new byte [] { controlCommand.UniversalID, 255 }, SendCount.Zero);
                            IO.FileUtils.SaveEquipmentMessage (acCommon, acCommon.LoopID.ToString ());
                        }
                    }
                }
 
                Application.RunOnMainThread (() => {
                    MainPage.Loading.Hide ();
                });
            });
        }
 
 
        /// <summary>
        /// 读取标志
        /// </summary>
        static bool IsReadingAll = false;
 
        static System.Threading.Thread thread;
 
        /// <summary>
        /// 读取设备状态
        /// </summary>
        public static void readAllStatus (bool isRead = false, bool isCheckIsReadingAll = false)
        {
            if (isCheckIsReadingAll) {
                if (IsReadingAll && !isRead) {
                    Utlis.WriteLine ("IsReadingAll AC 读取中");
                    return;
                }
            }
 
            if (isRead) {
                foreach (Room room in Room.Lists) {
                    if (string.IsNullOrEmpty (room.Name)) {
                        continue;
                    }
                    var list = room.DeviceList.FindAll ((obj) => {
                        return obj.Type == DeviceType.HVAC || obj.Type == DeviceType.ACPanel || obj.Type == DeviceType.ACInfrared;
                    });
                    foreach (Common common in list) {
                        common.LastUpdateTime = DateTime.MinValue;
                    }
                }
            }
            if (thread != null) {
                thread.Abort ();
            }
            thread = new Thread (() => {
                try {
                    IsReadingAll = true;
                    System.Collections.Generic.List<string> readList = new System.Collections.Generic.List<string> ();
                    foreach (Room room in Room.Lists) {
                        var list = room.DeviceList.FindAll ((obj) => {
                            return obj.LastUpdateTime.AddMinutes (Common.Time) <= DateTime.Now && (obj.Type == DeviceType.HVAC || obj.Type == DeviceType.ACPanel || obj.Type == DeviceType.ACInfrared);
                        });
                        foreach (Common ac in list) {
                            var s = ac.Type + "_" + ac.SubnetID + "_" + ac.DeviceID + "_" + ac.LoopID;
                            //已经读取过当前设备就不再读取
                            if (readList.Contains (s)) {
                                continue;
                            }
                            readList.Add (s); if (ac.Type == DeviceType.ACPanel) {
                                Control.ControlBytesSend (Command.ReadInstructionPanelKey, ac.SubnetID, ac.DeviceID, new byte [] { 3, ac.LoopID, ac.LoopID }, SendCount.Zero);
                                Control.ControlBytesSend (Command.ReadInstructionPanelKey, ac.SubnetID, ac.DeviceID, new byte [] { 4, ac.LoopID, ac.LoopID }, SendCount.Zero);
                                Control.ControlBytesSend (Command.ReadInstructionPanelKey, ac.SubnetID, ac.DeviceID, new byte [] { 5, ac.LoopID, ac.LoopID }, SendCount.Zero);
                                Control.ControlBytesSend (Command.ReadInstructionPanelKey, ac.SubnetID, ac.DeviceID, new byte [] { 6, ac.LoopID, ac.LoopID }, SendCount.Zero);
                                Control.ControlBytesSend (Command.ReadInstructionPanelKey, ac.SubnetID, ac.DeviceID, new byte [] { 7, ac.LoopID, ac.LoopID }, SendCount.Zero);
                                Control.ControlBytesSend (Command.ReadInstructionPanelKey, ac.SubnetID, ac.DeviceID, new byte [] { 8, ac.LoopID, ac.LoopID }, SendCount.Zero);
                                Control.ControlBytesSend (Command.ReadInstructionPanelKey, ac.SubnetID, ac.DeviceID, new byte [] { 19, ac.LoopID, ac.LoopID }, SendCount.Zero);
                                Control.ControlBytesSend (Command.ReadPanleTemp, ac.SubnetID, ac.DeviceID, new byte [] { ac.LoopID }, SendCount.Zero);
                            }
                        }
 
                    }
                    IsReadingAll = false;
                } catch {
                    IsReadingAll = false;
                } finally {
                    thread = null;
                }
            });
            thread.Start ();
        }
 
 
        /// <summary>
        /// 空调控制,带返回结果
        /// </summary>
        /// <param name="mAcData"></param>
        /// <param name="bPower"></param>
        void SendControlAc (AC mAcData, byte bPower)
        {
            MainPage.Loading.Start ("Sending...");
            System.Threading.Tasks.Task.Run (() => {
                byte [] returnBytes = null;
                if (mAcData.Type == DeviceType.ACPanel) {
                    returnBytes = Control.ControlBytesSendHasReturn (Command.InstructionPanelKey, mAcData.SubnetID, mAcData.DeviceID, new byte [] { 3, bPower, mAcData.LoopID });
                } else if (mAcData.Type == DeviceType.ACDevice || mAcData.Type == DeviceType.HVAC || mAcData.Type == DeviceType.ACInfrared) {
 
                    returnBytes = Control.ControlBytesSendHasReturn (Command.SetACMode, mAcData.SubnetID, mAcData.DeviceID, new [] { mAcData.LoopID, mAcData.TemperatureMode, mAcData.IndoorTemperature, mAcData.CoolTemperature, mAcData.HeatTemperature, mAcData.AutoTemperature, mAcData.ChuShiTemperature, mAcData.RealModeAndFanSpeed, bPower, mAcData.SetMode, mAcData.SetFanSpeed, mAcData.SetTemperature, mAcData.ShaoFanMode });
                }
 
                Application.RunOnMainThread (() => {
                    if (returnBytes == null) {
                        new Alert ("", mAcData.Name + ErrorCode.ControlFailure, "Close").Show ();
                    }
                    MainPage.Loading.Hide ();
                });
            });
        }
    }
}