黄学彪
2020-12-16 0d9f64668fd7350d6a21fd157e32009a96d98134
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
using System;
using System.Collections.Generic;
using System.Text;
 
namespace Shared.Phone.UserCenter.DevicePirSensor
{
    /// <summary>
    /// PIR传感器的触发目标选择界面
    /// </summary>
    public class PirSensorTargetSelectForm : EditorCommonForm
    {
        #region ■ 变量声明___________________________
 
        /// <summary>
        /// 结束选择的事件(设备主键)
        /// </summary>
        public Action<List<string>> FinishSelectEvent = null;
        /// <summary>
        /// 当前选择的楼层ID
        /// </summary>
        private string nowSelectFloorId = string.Empty;
        /// <summary>
        /// 桌布控件
        /// </summary>
        private NormalFrameLayout frameTable = null;
        /// <summary>
        /// 房间对象
        /// </summary>
        private Dictionary<string, List<Common.Room>> dicRoom = new Dictionary<string, List<Common.Room>>();
        /// <summary>
        /// 已经存在的绑定设备
        /// </summary>
        private List<string> listEsixtDevice = new List<string>();
 
        #endregion
 
        #region ■ 初始化_____________________________
 
        /// <summary>
        /// 画面显示(底层会固定调用此方法,借以完成画面创建)
        /// </summary>
        /// <param name="i_listEsixtDevice">存在的设备</param>
        public void ShowForm(List<string> i_listEsixtDevice)
        {
            this.listEsixtDevice.AddRange(i_listEsixtDevice);
 
            //设置头部信息
            base.SetTitleText(Language.StringByID(R.MyInternationalizationString.uTriggerTarget));
 
            //初始化右上角的控件
            this.InitTopRightMenuControl();
 
            //初始化中部信息
            this.InitMiddleFrame();
        }
 
        /// <summary>
        /// 初始化中部信息
        /// </summary>
        private void InitMiddleFrame()
        {
            //清空bodyFrame
            this.ClearBodyFrame();
 
            //房间的容器控件
            var frameBack = new FrameLayout();
            frameBack.Height = Application.GetRealHeight(204);
            bodyFrameLayout.AddChidren(frameBack);
 
            //桌布控件
            this.frameTable = new NormalFrameLayout();
            frameTable.Y = frameBack.Bottom;
            frameTable.Height = bodyFrameLayout.Height - frameBack.Height;
            bodyFrameLayout.AddChidren(frameTable);
 
            //其他
            var tempRoom = new Common.Room();
            tempRoom.Name = Language.StringByID(R.MyInternationalizationString.uOther);
            foreach (var mainKeys in this.listEsixtDevice)
            {
                var device = HdlDeviceCommonLogic.Current.GetDevice(mainKeys);
                if (device == null)
                {
                    continue;
                }
                //这个手机弄弄,那个手机弄弄的情况下,
                //采用后备操作 -> 新建一个临时房间对象出来
                if (HdlRoomLogic.Current.GetRoomByDevice(device) == null)
                {
                    tempRoom.ListDevice.Add(HdlDeviceCommonLogic.Current.GetDeviceMainKeys(device));
                }
            }
 
            HdlThreadLogic.Current.RunMainInThread(() =>
            {
                if (dicRoom.ContainsKey(nowSelectFloorId) == true && dicRoom[nowSelectFloorId].Count > 0)
                {
                    //初始化房间控件
                    var listRoom = new List<Common.Room>();
                    listRoom.AddRange(dicRoom[nowSelectFloorId]);
                    if (tempRoom.ListDevice.Count > 0)
                    {
                        //显示新建的临时房间对象
                        listRoom.Add(tempRoom);
                    }
                    var roomContr = new RoomDeviceGroupMenuControl(listRoom);
                    frameBack.AddChidren(roomContr);
                    roomContr.SelectRoomEvent += (myRoom) =>
                    {
                        //初始化设备行
                        this.InitDeviceControl(myRoom);
                    };
                    roomContr.InitControl();
 
                    //确定按钮
                    var btnOk = new BottomClickButton();
                    btnOk.TextID = R.MyInternationalizationString.uConfirm1;
                    bodyFrameLayout.AddChidren(btnOk);
                    btnOk.ButtonClickEvent += (sender, e) =>
                    {
                        //调用回调函数
                        this.FinishSelectEvent?.Invoke(listEsixtDevice);
                        //界面关闭
                        this.CloseForm();
                    };
                    //查看模板时,不允许编辑
                    if (Common.Config.Instance.Home.IsShowTemplate == true)
                    {
                        btnOk.CanClick = false;
                    }
                }
                else
                {
                    //没有可以添加的目标
                    this.ShowNotDataImage(bodyFrameLayout, Language.StringByID(R.MyInternationalizationString.uNotHadAddTarget));
                }
            });
        }
 
        #endregion
 
        #region ■ 初始化右上角的控件_________________
 
        /// <summary>
        /// 初始化右上角的控件
        /// </summary>
        private void InitTopRightMenuControl()
        {
            //房间分组
            var listRoom = HdlRoomLogic.Current.GetAllListRooms();
            foreach (var room in listRoom)
            {
                //检测该房间能否显示
                if (this.CheckCanShowRow(room) == false)
                {
                    continue;
                }
                if (dicRoom.ContainsKey(room.FloorId) == false)
                {
                    dicRoom[room.FloorId] = new List<Common.Room>();
                }
                dicRoom[room.FloorId].Add(room);
            }
            if (dicRoom.Count == 0)
            {
                //默认一个空的东西
                dicRoom[string.Empty] = new List<Common.Room>();
            }
 
            //获取楼层
            var dicFloor = HdlRoomLogic.Current.GetFloorSortList();
            if (dicFloor.Count == 0)
            {
                return;
            }
            int indexTemp = 1;
            foreach (var floorId in dicRoom.Keys)
            {
                if (dicFloor.ContainsKey(floorId) == false)
                {
                    //这是多台手机瞎搞之后出现的后备补救措施
                    dicFloor[floorId] = Language.StringByID(R.MyInternationalizationString.uOther) + indexTemp;
                    indexTemp++;
                }
            }
 
            var btnIconContr = new MostRightIconControl(69, 69);
            btnIconContr.UnSelectedImagePath = "Item/Drop_Down.png";
            topFrameLayout.AddChidren(btnIconContr);
            btnIconContr.InitControl();
 
            var btnFloor = new NormalViewControl(300, 69, true);
            btnFloor.Gravity = Gravity.CenterVertical;
            btnFloor.X = btnIconContr.X + btnIconContr.btnIcon.X - Application.GetRealWidth(300);
            btnFloor.TextAlignment = TextAlignment.CenterRight;
            topFrameLayout.AddChidren(btnFloor);
 
            foreach (var floorId in dicFloor.Keys)
            {
                //第一个楼层
                this.nowSelectFloorId = floorId;
                btnFloor.Text = dicFloor[floorId];
                break;
            }
 
            btnIconContr.ButtonClickEvent += (sender, e) =>
            {
                //楼层菜单
                var contr = new TopRightMenuControl(dicFloor.Count, 2, Language.StringByID(R.MyInternationalizationString.SelectFloor));
                foreach (var floorId in dicFloor.Keys)
                {
                    contr.AddRowMenu(dicFloor[floorId], "Floor/Floor.png", "Floor/FloorSelected.png", () =>
                    {
                        //记录起选择的ID
                        this.nowSelectFloorId = floorId;
                        btnFloor.Text = dicFloor[this.nowSelectFloorId];
                        //初始化中部信息
                        this.InitMiddleFrame();
                    });
                }
            };
        }
        #endregion
 
        #region ■ 初始化设备控件_____________________
 
        /// <summary>
        /// 初始化设备控件
        /// </summary>
        /// <param name="room"></param>
        private void InitDeviceControl(Common.Room room)
        {
            //先清空
            this.frameTable.RemoveAll();
 
            var frameBack = new FrameLayout();
            frameBack.Height = Application.GetRealHeight(11);
            frameBack.BackgroundColor = UserCenterColor.Current.White;
            frameTable.AddChidren(frameBack);
 
            var listView = new VerticalListControl(12);
            listView.Y = frameBack.Bottom;
            listView.Height = frameTable.Height - frameBack.Height;
            listView.BackgroundColor = UserCenterColor.Current.White;
            frameTable.AddChidren(listView);
 
            var listDevice = new List<ZigBee.Device.CommonDevice>();
            foreach (var deviceKeys in room.ListDevice)
            {
                var device = HdlDeviceCommonLogic.Current.GetDevice(deviceKeys);
                //检测设备
                if (this.CheckCanShowDevice(device) == true)
                {
                    listDevice.Add(device);
                }
            }
 
            for (int i = 0; i < listDevice.Count; i++)
            {
                var device = listDevice[i];
                string mainKey = HdlDeviceCommonLogic.Current.GetDeviceMainKeys(device);
 
                var rowDevice = new FrameRowControl(listView.rowSpace / 2);
                listView.AddChidren(rowDevice);
                //图标
                var btnIcon = rowDevice.AddLeftIcon(81);
                HdlDeviceCommonLogic.Current.SetDeviceIconToControl(btnIcon, device);
                //设备名称
                var btnName = rowDevice.AddLeftCaption(HdlDeviceCommonLogic.Current.GetDeviceEpointName(device), 700);
                btnName.TextSize = 15;
                //选择
                var btnSelect = rowDevice.AddMostRightEmptyIcon(58, 58);
                if (listEsixtDevice.Contains(mainKey) == false)
                {
                    btnSelect.Visible = false;
                }
                btnSelect.UnSelectedImagePath = "Item/ItemSelected.png";
                if (i != listDevice.Count - 1)
                {
                    //底线
                    rowDevice.AddBottomLine();
                }
 
                rowDevice.ButtonClickEvent += (sender, e) =>
                {
                    btnSelect.Visible = !btnSelect.Visible;
                    if (btnSelect.Visible == true)
                    {
                        listEsixtDevice.Add(mainKey);
                    }
                    else
                    {
                        listEsixtDevice.Remove(mainKey);
                    }
                };
                //查看模板时,不允许编辑
                if (Common.Config.Instance.Home.IsShowTemplate == true)
                {
                    rowDevice.CanClick = false;
                }
            }
            listDevice = null;
            //调整控件真实高度
            listView.AdjustRealHeight(Application.GetRealHeight(23));
            //借用这个东西进行检测
            var btnTemp = new BottomClickButton();
            //如果真实高度已经超过了确定按键
            if (listView.Bottom + frameTable.Y > btnTemp.Yaxis)
            {
                listView.Height = frameTable.Height - frameBack.Height;
                var frameTemp = new FrameLayout();
                frameTemp.Height = bodyFrameLayout.Height - btnTemp.Yaxis;
                listView.AddChidren(frameTemp);
            }
        }
 
        #endregion
 
        #region ■ 一般方法___________________________
 
        /// <summary>
        /// 检测该房间能否显示
        /// </summary>
        /// <param name="room"></param>
        /// <returns></returns>
        private bool CheckCanShowRow(Common.Room room)
        {
            if (room.ListDevice.Count == 0)
            {
                return false;
            }
            if (room.IsLove == true)
            {
                return false;
            }
            foreach (var deviceKeys in room.ListDevice)
            {
                //检测该设备能否显示
                var device = HdlDeviceCommonLogic.Current.GetDevice(deviceKeys);
                if (this.CheckCanShowDevice(device) == false)
                {
                    continue;
                }
 
                //存在设备的话,此房间可以显示
                return true;
            }
            return false;
        }
 
        /// <summary>
        /// 检测该设备能否显示
        /// </summary>
        /// <param name="device"></param>
        /// <returns></returns>
        private bool CheckCanShowDevice(ZigBee.Device.CommonDevice device)
        {
            if (device == null)
            {
                return false;
            }
            //如果是传感器,或者是没有开关簇的话(这里判断的是输入簇)
            if ((device.Type == ZigBee.Device.DeviceType.IASZone) || HdlDeviceCommonLogic.Current.InDeviceIsCanOnOff(device) == false)
            {
                return false;
            }
            return true;
        }
 
        #endregion
 
        #region ■ 界面关闭___________________________
 
        /// <summary>
        /// 界面关闭
        /// </summary>
        public override void CloseFormBefore()
        {
            this.FinishSelectEvent = null;
 
            base.CloseFormBefore();
        }
 
        #endregion
    }
}