黄学彪
2020-12-17 9f326f4000847e6167d8166fa2f6a66f53cb3734
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 System;
using System.Collections.Generic;
using System.Text;
using ZigBee.Device;
 
namespace Shared.Phone.UserCenter.Residence
{
    /// <summary>
    /// 查看房间配置的界面
    /// </summary>
    public class LookRoomSettionForm : EditorCommonForm
    {
        #region ■ 变量声明___________________________
 
        /// <summary>
        /// 查看的房间对象
        /// </summary>
        private Common.Room lookRoom = null;
        /// <summary>
        /// 桌布控件
        /// </summary>
        private NormalFrameLayout frameTable = null;
        /// <summary>
        /// 设备列表
        /// </summary>
        private List<CommonDevice> listDevice = new List<CommonDevice>();
 
        #endregion
 
        #region ■ 初始化_____________________________
 
        /// <summary>
        /// 画面显示(底层会固定调用此方法,借以完成画面创建)
        /// </summary>
        /// <param name="i_room">查看的房间对象</param>
        public void ShowForm(Common.Room i_room)
        {
            this.lookRoom = i_room;
 
            //设置头部信息
            base.SetTitleText(lookRoom.Name);
 
            //初始化中部信息
            this.InitMiddleFrame();
        }
 
        /// <summary>
        /// 初始化中部信息
        /// </summary>
        private void InitMiddleFrame()
        {
            //清空bodyFrame
            this.ClearBodyFrame();
 
            //初始化桌布控件
            this.frameTable = new NormalFrameLayout();
            frameTable.Y = Application.GetRealHeight(187);
            frameTable.Height = bodyFrameLayout.Height - Application.GetRealHeight(187);
            bodyFrameLayout.AddChidren(frameTable);
 
            //初始化分支控件
            this.InitSwitchControl();
        }
 
        /// <summary>
        /// 初始化分支控件
        /// </summary>
        private void InitSwitchControl()
        {
            var tabControl = new SceneFunctionSwitchControl();
            tabControl.Y = Application.GetRealHeight(40);
            bodyFrameLayout.AddChidren(tabControl);
            tabControl.SelectTabEvent += (tabIndex) =>
            {
                if (tabIndex == 0)
                {
                    //显示场景列表
                    this.ShowSceneList();
                }
                else
                {
                    //显示功能列表
                    this.ShowFunctionList();
                }
            };
            var listTitle = new List<string>();
            //场景,功能
            listTitle.Add(Language.StringByID(R.MyInternationalizationString.uScence));
            listTitle.Add(Language.StringByID(R.MyInternationalizationString.uFunction));
            tabControl.InitControl(listTitle);
        }
 
        #endregion
 
        #region ■ 场景显示___________________________
 
        /// <summary>
        /// 显示场景列表
        /// </summary>
        private void ShowSceneList()
        {
            //清空桌布
            this.frameTable.RemoveAll();
            HdlThreadLogic.Current.RunMainInThread(() =>
            {
                if (lookRoom.ListSceneId.Count == 0)
                {
                    //还没有添加场景哦
                    this.ShowNotDataImage(frameTable, Language.StringByID(R.MyInternationalizationString.uDoNotHadAddScenceMsg));
                    return;
                }
                var listView = new VerticalFrameControl(3);
                listView.Height = frameTable.Height;
                frameTable.AddChidren(listView);
 
                foreach (var dataId in lookRoom.ListSceneId)
                {
                    var data = HdlSceneLogic.Current.GetSceneUIBySceneId(dataId);
                    if (data == null)
                    {
                        continue;
                    }
                    //场景控件
                    var frameContr = new ScenePictrueControl();
                    listView.AddChidren(frameContr);
                    frameContr.InitControl(data);
                }
            });
        }
 
        #endregion
 
        #region ■ 功能显示___________________________
 
        /// <summary>
        /// 显示功能列表
        /// </summary>
        private void ShowFunctionList()
        {
            //清空桌布
            this.frameTable.RemoveAll();
 
            HdlThreadLogic.Current.RunMainInThread(() =>
            {
                //获取分组后的设备列表
                var dicGroupDevice = this.GetAllGroupDevice();
                if (dicGroupDevice.Count == 0)
                {
                    //还没有添加设备哦
                    this.ShowNotDataImage(frameTable, Language.StringByID(R.MyInternationalizationString.uDoNotHadAddDeviceMsg));
                    return;
                }
 
                var frameBack = new FrameLayout();
                frameBack.X = HdlControlResourse.XXLeft;
                frameBack.BackgroundColor = UserCenterColor.Current.White;
                frameBack.Width = bodyFrameLayout.Width;
                frameBack.Height = Application.GetRealHeight(1650);
                frameBack.Radius = (uint)Application.GetRealHeight(58);
                frameTable.AddChidren(frameBack);
 
                var listView = new VerticalListControl(23);
                listView.Y = Application.GetRealHeight(23);
                listView.Height = Application.GetRealHeight(1549 - 23);
                frameBack.AddChidren(listView);
 
                var dicData = new Dictionary<string, DeviceRowInfo>();
                foreach (var strText in dicGroupDevice.Keys)
                {
                    //添加设备行
                    this.AddDeviceRow(dicGroupDevice[strText], strText, listView);
                    foreach (var deviceKey in dicGroupDevice[strText].listDeviceKeys)
                    {
                        var device = HdlDeviceCommonLogic.Current.GetDevice(deviceKey);
                        //以回路作为索引去指定对象
                        dicData[deviceKey] = dicGroupDevice[strText];
                    }
                }
                listView.AdjustRealHeight(Application.GetRealHeight(23));
                //如果不是虚拟住宅
                if (Common.Config.Instance.Home.IsVirtually == false)
                {
                    //开启设备在线监测
                    this.StartCheckDeviceOnline(listView, dicData);
                }
            });
        }
 
        #endregion
 
        #region ■ 添加设备行_________________________
 
        /// <summary>
        /// 添加设备行
        /// </summary>
        /// <param name="rowInfo"></param>
        /// <param name="strText"></param>
        /// <param name="listView"></param>
        private void AddDeviceRow(DeviceRowInfo rowInfo, string strText, VerticalListControl listView)
        {
            var frameRow = new FrameRowControl(listView.rowSpace / 2);
            frameRow.LeftOffset = Application.GetRealWidth(46) - HdlControlResourse.XXLeft;
            frameRow.RightOffset = -HdlControlResourse.XXLeft;
            listView.AddChidren(frameRow);
            //图标
            var btnIcon = frameRow.AddLeftIcon(81);
            btnIcon.UnSelectedImagePath = rowInfo.IconPath;
            //名称
            var btnView = frameRow.AddLeftCaption(string.Empty, 600);
            btnView.Text = strText;
            btnView.TextSize = 15;
            //右箭头
            frameRow.AddRightArrow();
            //在线数
            var btnOnline = frameRow.AddMostRightView(rowInfo.OnlineCount + "/" + rowInfo.listDeviceKeys.Count, 300);
            rowInfo.btnOnline = btnOnline;
            //底线
            frameRow.AddBottomLine();
            frameRow.ButtonClickEvent += (sender, e) =>
            {
                var form = new LookRoomDeviceListForm();
                form.AddForm(lookRoom.Name, strText, rowInfo.listDeviceKeys);
            };
        }
 
        #endregion
 
        #region ■ 整合设备___________________________
 
        /// <summary>
        /// 获取分组后的设备
        /// </summary>
        /// <returns></returns>
        private Dictionary<string, DeviceRowInfo> GetAllGroupDevice()
        {
            //全部的设备
            this.listDevice = HdlRoomLogic.Current.GetRoomListDevice(lookRoom);
            var dic = new Dictionary<string, DeviceRowInfo>();
            foreach (var device in listDevice)
            {
                var typeInfo = HdlDeviceCommonLogic.Current.GetDeviceBelongEnumInfo(device);
                //按所属ID分组
                if (dic.ContainsKey(typeInfo.BeloneText) == false)
                {
                    dic[typeInfo.BeloneText] = new DeviceRowInfo();
                    string path1 = string.Empty;
                    string path2 = string.Empty;
                    //获取图片
                    HdlDeviceCommonLogic.Current.GetDeviceFunctionTypeMenuIcon(typeInfo, ref path1, ref path2);
                    dic[typeInfo.BeloneText].IconPath = path1;
                }
                dic[typeInfo.BeloneText].listDeviceKeys.Add(HdlDeviceCommonLogic.Current.GetDeviceMainKeys(device));
            }
            return dic;
        }
 
        #endregion
 
        #region ■ 设备在线___________________________
 
        /// <summary>
        /// 开启设备在线监测
        /// </summary>
        /// <param name="listView">判断退出时使用</param>
        /// <param name="dicData">以回路作为索引去指定对象</param>
        private void StartCheckDeviceOnline(VerticalListControl listView, Dictionary<string, DeviceRowInfo> dicData)
        {
            HdlThreadLogic.Current.RunThread(() =>
            {
                var dicCheck = new Dictionary<string, bool>();
                for (int i = 0; i < this.listDevice.Count; i++)
                {
                    if (listView.Parent == null)
                    {
                        return;
                    }
                    var localDevice = this.listDevice[i];
                    if (dicCheck.ContainsKey(localDevice.DeviceAddr) == false)
                    {
                        //只需要检测一路
                        var deviceInfo = HdlDeviceCommonLogic.Current.ReadDeviceEpointDeviceInfo(localDevice);
                        if (deviceInfo != null)
                        {
                            localDevice.IsOnline = deviceInfo.IsOnline;
                            dicCheck[localDevice.DeviceAddr] = HdlDeviceCommonLogic.Current.CheckDeviceIsOnline(localDevice, false);
                        }
                    }
                    if (dicCheck.ContainsKey(localDevice.DeviceAddr) == false
                      || dicCheck[localDevice.DeviceAddr] == false)
                    {
                        //网关没有回复,或者不在线
                        continue;
                    }
                    string mainkeys = HdlDeviceCommonLogic.Current.GetDeviceMainKeys(localDevice);
                    if (dicData.ContainsKey(mainkeys) == true)
                    {
                        HdlThreadLogic.Current.RunMain(() =>
                        {
                            dicData[mainkeys].OnlineCount += 1;
                            dicData[mainkeys].btnOnline.Text = dicData[mainkeys].OnlineCount + "/" + dicData[mainkeys].listDeviceKeys.Count;
 
                        }, ShowErrorMode.NO);
                    }
                    System.Threading.Thread.Sleep(30);
                }
            }, ShowErrorMode.NO);
        }
 
        #endregion
 
        #region ■ 结构体_____________________________
 
        /// <summary>
        /// 设备行信息
        /// </summary>
        private class DeviceRowInfo
        {
            /// <summary>
            /// 图标
            /// </summary>
            public string IconPath = string.Empty;
            /// <summary>
            /// 设备回路主键
            /// </summary>
            public List<string> listDeviceKeys = new List<string>();
            /// <summary>
            /// 设备在线数
            /// </summary>
            public int OnlineCount = 0;
            /// <summary>
            /// 在线控件
            /// </summary>
            public NormalViewControl btnOnline = null;
        }
 
        #endregion
    }
}