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
using Shared.SimpleControl.Phone;
using System;
namespace Shared.SimpleControl.Pad
{
    public class UserDeviceToCurtains : Shared.Dialog
    {
        VerticalScrolViewLayout RoomListScrolView;
        /// <summary>
        /// 当前界面
        /// </summary>
        static UserDeviceToCurtains curView;
        UserCurtainPage ControlBodyView;
        /// <summary>
        ///构造函数
        /// </summary>
        public UserDeviceToCurtains ()
        {
            curView = this;
            showAllRoomCurtain ();
            readAllStatus ();
        }
 
        /// <summary>
        /// 通过设备类型方式选择设备
        /// </summary>
        void showAllRoomCurtain ()
        {
            #region 标题
            FrameLayout topBackView = new FrameLayout () {
                Height = Application.GetRealHeight (150),
                BackgroundColor = 0xFF2f2f2f
            };
            AddChidren (topBackView);
            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.Curtains),
                TextSize = 19,
            };
            topView.AddChidren (title);
 
            var back = new Button () {
                Height = Application.GetRealHeight (120),
                Width = Application.GetRealWidth (95),
                UnSelectedImagePath = "Item/Back.png",
                SelectedImagePath = "Item/BackSelected.png",
                Gravity = Gravity.CenterVertical,
            };
            topView.AddChidren (back);
            back.MouseUpEventHandler += (sender, e) => {
 
                Close ();
                curView = null;
            };
            var logo = new Button () {
               Width = Application.GetRealWidth (154),
                Height = Application.GetRealHeight (90),
                X = Application.GetRealWidth (486),
                UnSelectedImagePath = MainPage.LogoString,
            };
            topView.AddChidren (logo);
 
            var btnl = new Button () {
                Height = 1,
                BackgroundColor = 0xFF2f2f2f,
                Y = topView.Height - 1
            };
            topView.AddChidren (btnl);
            #endregion
 
            FrameLayout bordorView = new FrameLayout () {
                Width = Application.GetRealWidth (480),
                Height = Application.GetRealHeight (Application.DesignHeight - 150),
                Y = Application.GetRealHeight (150),
                BackgroundColor = 0xFF2f2f2f
            };
 
            AddChidren (bordorView);
 
            #region 全开-全关
            var powerView = new FrameLayout () {
                Height = Application.GetRealHeight (140),
                Width = Application.GetRealWidth (480),
                BackgroundColor = SkinStyle.Current.MainColor,
            };
            bordorView.AddChidren (powerView);
            var btnAllON = new Button () {
                Width = Application.GetRealWidth (240),
                TextSize = 16,
                TextID = R.MyInternationalizationString.ALLON,
                SelectedTextColor = SkinStyle.Current.SelectedColor,
                TextColor = SkinStyle.Current.TextColor1,
            };
            powerView.AddChidren (btnAllON);
            var btnAllOff = new Button () {
                Width = Application.GetRealWidth (240),
                X = btnAllON.Right,
                TextSize = 16,
                TextID = R.MyInternationalizationString.ALLOFF,
                SelectedTextColor = SkinStyle.Current.SelectedColor,
                TextColor = SkinStyle.Current.TextColor1,
            };
            var btnLine = new Button () {
                Width = 1,
                BackgroundColor = 0xFF2f2f2f,
                X = btnAllON.Right,
            };
            powerView.AddChidren (btnLine);
            var btnLine1 = new Button () {
                Height = 1,
                Y = powerView.Height - 1,
                BackgroundColor = 0xFF2f2f2f,
            };
            powerView.AddChidren (btnLine1);
            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.SureOpenAllCurtian),
                                        Language.StringByID (R.MyInternationalizationString.Cancel),
                                        Language.StringByID (R.MyInternationalizationString.Confrim));
                alert.Show ();
                alert.ResultEventHandler += (sender2, e2) => {
                    if (e2) {
 
                        controlAllCurtain (1);
                    }
                };
            };
            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.SureCloseAllCurtian),
                                         Language.StringByID (R.MyInternationalizationString.Cancel),
                                         Language.StringByID (R.MyInternationalizationString.Confrim));
                alert.Show ();
                alert.ResultEventHandler += (sender2, e2) => {
                    if (e2) {
                        controlAllCurtain (2);
                    }
                };
            };
            #endregion
 
            RoomListScrolView = new VerticalScrolViewLayout () {
                Width = LayoutParams.MatchParent,
                Height = Application.GetRealHeight (Application.DesignHeight - 150 - 140),
                Y = powerView.Bottom,
            };
            bordorView.AddChidren (RoomListScrolView);
            #region 显示全部的窗帘
 
 
            int devcieCount = 0;
            foreach (var room in Room.Lists) {
                if (room.Name == "") {
                    continue;
                }
                var curtainFindList = room.DeviceList.FindAll ((obj) => {
                    return obj.Type == DeviceType.CurtainModel ||
                              obj.Type == DeviceType.CurtainRoller ||
                              obj.Type == DeviceType.CurtainTrietex;
                });
                foreach (var common in curtainFindList) {
                    EventHandler<MouseEventArgs> openCurtainControlPageEvent = (button, mouseEventArgs) => {
                        for (int i = 0; i < RoomListScrolView.ChildrenCount; i++) {
                            var view = RoomListScrolView.GetChildren (i);
                            if (view.Tag == common) {
                                for (int j = 0; j < (view as FrameLayout).ChildrenCount; j++) {
                                    var btn = (view as FrameLayout).GetChildren (j);
                                    (btn as Button).IsSelected = true;
                                }
                            } else {
                                for (int j = 0; j < (view as FrameLayout).ChildrenCount; j++) {
                                    var btn = (view as FrameLayout).GetChildren (j);
                                    (btn as Button).IsSelected = false;
                                }
                            }
                        }
                        if (ControlBodyView != null) {
                            ControlBodyView.RemoveFromParent ();
                        }
                        ControlBodyView = new UserCurtainPage (common, room) {
                            Width = Application.GetRealWidth (Application.DesignWidth) - bordorView.Width,
                            Height = Application.GetRealHeight (Application.DesignHeight - 150),
                            X = bordorView.Right,
                            Y = bordorView.Y,
                            BackgroundColor = SkinStyle.Current.MainColor
 
                        };
                        AddChidren (ControlBodyView);
                        ControlBodyView.showCurtainsRoom (common, room);
                    };
                    FrameLayout RoomView = new FrameLayout () {
                        Height = Application.GetRealHeight (140),
                    };
                    RoomView.Tag = common;
                    RoomListScrolView.AddChidren (RoomView);
                    RoomView.MouseUpEventHandler += openCurtainControlPageEvent;
 
                    //窗名
                    var tempDeviceName = new Button () {
                        Padding = new Padding (0, 35, 0, 0),
                        Text = room.Name + " - " + common.Name,
                        Height = Application.GetRealHeight (140),
                        TextAlignment = TextAlignment.CenterLeft,
                        SelectedTextColor = SkinStyle.Current.SelectedColor,
                        TextColor = SkinStyle.Current.TextColor1,
                        BackgroundColor = SkinStyle.Current.Transparent,
                        SelectedBackgroundColor = SkinStyle.Current.MainColor
                    };
                    RoomView.AddChidren (tempDeviceName);
                    tempDeviceName.MouseUpEventHandler += openCurtainControlPageEvent;
 
 
                    Button btnPoint = new Button () {
                        Width = Application.GetRealWidth (15),
                        Height = Application.GetRealHeight (15),
                        X = Application.GetRealWidth (45),
                        Gravity = Gravity.CenterVertical,
                        UnSelectedImagePath = "Item/Point.png",
                        SelectedImagePath = "Item/PointSelected.png",
                    };
                    RoomView.AddChidren (btnPoint);
 
                    Button btnNull = new Button () {
                        Height = Application.GetRealHeight (5),
                        Y = Application.GetRealHeight (138),
                        BackgroundColor = SkinStyle.Current.MainColor,
                    };
                    RoomView.AddChidren (btnNull);
 
 
                    Button btnSelected = new Button () {
                        X = RoomView.Width - 5,
                        Width = 5,
                        SelectedBackgroundColor = SkinStyle.Current.SelectedColor,
                        BackgroundColor = SkinStyle.Current.Transparent,
                    };
                    RoomView.AddChidren (btnSelected);
                    tempDeviceName.MouseUpEventHandler += openCurtainControlPageEvent;
                    if (devcieCount == 0) {
                        ControlBodyView = new UserCurtainPage (common, room) {
                            Width = Application.GetRealWidth (Application.DesignWidth) - bordorView.Width,
                            Height = Application.GetRealHeight (Application.DesignHeight - 150),
                            X = bordorView.Right,
                            Y = bordorView.Y,
                            BackgroundColor = SkinStyle.Current.MainColor
                        };
                        AddChidren (ControlBodyView);
                        ControlBodyView.showCurtainsRoom (common, room);
                        tempDeviceName.IsSelected = true;
                        btnPoint.IsSelected = true;
                        btnSelected.IsSelected = true;
                        //btnLastTempDeviceName = tempDeviceName;
                        //btnLastPoint = btnPoint;
                    }
                    devcieCount++;
                }
            }
            #endregion
        }
 
        /// <summary>
        /// 控制所有窗帘
        /// </summary>
        /// <param name="status">1:开,2:关.</param>
        void controlAllCurtain (byte status)
        {
            System.Threading.Tasks.Task.Run (() => {
                foreach (var room in Room.Lists) {
                    if (room == null)
                        continue;
                    if (room.Name == "") {
                        continue;
                    }
                    foreach (var common in room.DeviceList) {
                        if (common.Type == DeviceType.CurtainModel) {
                            var curtainmodel = common as CurtainModel;
                            Control.ControlBytesSendHasReturn (Command.UpdataCurtainModelStutas, curtainmodel.SubnetID, curtainmodel.DeviceID, new byte [] { curtainmodel.LoopID, status });
                        } else if (common.Type == DeviceType.CurtainRoller) {
                            var curtainroller = common as CurtainRoller;
                            Control.ControlBytesSendHasReturn (Command.UpdataCurtainModelStutas, curtainroller.SubnetID, curtainroller.DeviceID, new byte [] { curtainroller.LoopID, status });
                        } else if (common.Type == DeviceType.CurtainTrietex) {
                            var curtaintrietex = common as CurtainTrietex;
                            Control.ControlBytesSendHasReturn (Command.UpdataCurtainModelStutas, curtaintrietex.SubnetID, curtaintrietex.DeviceID, new byte [] { curtaintrietex.LoopID, status });
                        }
                    }
                }
            });
        }
 
        /// <summary>
        /// 读取设备状态
        /// </summary>
        static void readAllStatus ()
        {
            System.Threading.Tasks.Task.Run (() => {
                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.CurtainModel || obj.Type == DeviceType.CurtainRoller || obj.Type == DeviceType.CurtainTrietex);
                    });
                    foreach (Common common in list) {
                        var s = common.Type + "_" + common.SubnetID + "_" + common.DeviceID + "_" + common.LoopID;
                        //已经读取过当前设备就不再读取
                        if (readList.Contains (s)) {
                            continue;
                        }
 
                        if (common.Type == DeviceType.CurtainModel) {
                            var curtainModel = common as CurtainModel;
                            readList.Add (s);
                            Control.ControlBytesSend (Command.ReadCurtainStatus, curtainModel.SubnetID, curtainModel.DeviceID, new byte [] { curtainModel.LoopID });
                        }
                        if (common.Type == DeviceType.CurtainRoller) {
                            var curtainroller = common as CurtainRoller;
                            readList.Add (s);
                            Control.ControlBytesSend (Command.ReadCurtainStatus, curtainroller.SubnetID, curtainroller.DeviceID, new byte [] { 17 });
                        }
                        if (common.Type == DeviceType.CurtainTrietex) {
                            var curtaintrietex = common as CurtainTrietex;
                            readList.Add (s);
                            Control.ControlBytesSend (Command.ReadCurtainStatus, curtaintrietex.SubnetID, curtaintrietex.DeviceID, new byte [] { 17 });
                        }
                    }
                }
            });
        }
    }
}