黄学彪
2020-09-22 ade5917841b0fdcb1df7353ef7c56b1a1bdc9282
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
using System;
using System.Collections.Generic;
using System.Text;
using ZigBee.Device;
 
namespace Shared.Phone.UserCenter.Safety
{
    /// <summary>
    /// 添加报警目标(设备)的列表界面
    /// </summary>
    public class AlarmTargetAddDeviceForm : EditorCommonForm
    {
        #region ■ 变量声明___________________________
 
        /// <summary>
        /// 列表控件
        /// </summary>
        private VerticalListControl listView = null;
        /// <summary>
        /// 防区ID(这个东西似乎是唯一的)
        /// </summary>
        private int zoonID = 0;
        /// <summary>
        /// 设备列表
        /// </summary>
        private List<CommonDevice> listAllDevice = null;
        /// <summary>
        /// 需要添加的报警设备
        /// </summary>
        private Dictionary<string, TaskInfoData> dicSaveDevice = new Dictionary<string, TaskInfoData>();
 
        #endregion
 
        #region ■ 初始化_____________________________
 
        /// <summary>
        /// 画面显示(底层会固定调用此方法,借以完成画面创建)
        /// </summary>
        /// <param name="i_zoonID">防区ID</param>
        /// <param name="i_deviceText">设备类型的名称</param>
        /// <param name="i_listDevice">设备信息</param>
        public void ShowForm(int i_zoonID, string i_deviceText, List<CommonDevice> i_listDevice)
        {
            this.zoonID = i_zoonID;
            this.listAllDevice = i_listDevice;
 
            //设置头部信息
            base.SetTitleText(i_deviceText);
 
            //初始化中部信息
            this.InitMiddleFrame(i_deviceText);
        }
 
        /// <summary>
        /// 初始化中部信息
        /// </summary>
        /// <param name="titleText">列表头部名称</param>
        private void InitMiddleFrame(string titleText)
        {
            //清空bodyFrame
            this.ClearBodyFrame();
 
            var frameBack = new FrameLayout();
            frameBack.BackgroundColor = UserCenterColor.Current.White;
            frameBack.Height = Application.GetRealHeight(8);
            bodyFrameLayout.AddChidren(frameBack);
 
            this.listView = new VerticalListControl(12);
            listView.Y = frameBack.Bottom;
            listView.BackgroundColor = UserCenterColor.Current.White;
            listView.Height = bodyFrameLayout.Height - frameBack.Bottom;
            bodyFrameLayout.AddChidren(listView);
 
            //完成
            var btnfinish = new BottomClickButton();
            btnfinish.TextID = R.MyInternationalizationString.uFinish;
            bodyFrameLayout.AddChidren(btnfinish);
            btnfinish.ButtonClickEvent += (sender, e) =>
            {
                //保存选择的设备
                this.DoSaveSelectDeviceAsync();
            };
 
            HdlThreadLogic.Current.RunMainInThread(() =>
            {
                int count = listAllDevice.Count - 1;
                for (int i = 0; i < listAllDevice.Count; i++)
                {
                    //如果那个设备已经添加了,则不再显示
                    if (HdlSafeguardLogic.Current.IsAlarmDeviceExist(this.zoonID, listAllDevice[i]) == true)
                    {
                        continue;
                    }
                    //添加行目标
                    this.AddRowLayout(listAllDevice[i], i != count);
                }
                //调整真实高度
                listView.AdjustRealHeightByBottomButton(Application.GetRealHeight(23));
            });
        }
 
        #endregion
 
        #region ■ 添加行目标_________________________
 
        /// <summary>
        /// 添加行目标
        /// </summary>
        /// <param name="device"></param>
        /// <param name="addLine"></param>
        private void AddRowLayout(CommonDevice device, bool addLine)
        {
            string mainKeys = Common.LocalDevice.Current.GetDeviceMainKeys(device);
 
            var rowContr = new FrameRowControl(listView.rowSpace / 2);
            listView.AddChidren(rowContr);
 
            //图标
            var btnIcon = rowContr.AddLeftIcon(81);
            Common.LocalDevice.Current.SetDeviceIconToControl(btnIcon, device);
            //设备名
            var btnName = rowContr.AddLeftCaption(Common.LocalDevice.Current.GetDeviceEpointName(device), 700);
            btnName.TextSize = 15;
            //右箭头
            rowContr.AddRightArrow();
            if (addLine == true)
            {
                //底线
                rowContr.AddBottomLine();
            }
            //状态显示
            var btnStatu = rowContr.AddMostRightView("", 500);
            rowContr.ButtonClickEvent += (sender, e) =>
            {
                List<Safeguard.TaskListInfo> listTaskinfo = null;
                if (dicSaveDevice.ContainsKey(mainKeys) == true)
                {
                    //取缓存中还未保存的数据
                    listTaskinfo = dicSaveDevice[mainKeys].listInfos;
                }
 
                if (device.Type == DeviceType.DimmableLight//调光器
                    || device.Type == DeviceType.ColorDimmableLight)//彩灯
                {
                    var form = new AlarmTargetStatuSelectLightForm();
                    form.AddForm(device, listTaskinfo);
                    form.FinishSelectEvent += (statuText, listInfo) =>
                    {
                        btnStatu.Text = statuText;
                        //将新的报警目标添加入缓存
                        this.AddAlarmSettionDataToMemory(device, listInfo);
                    };
                }
                else if (device.Type == DeviceType.WindowCoveringDevice)//窗帘
                {
                    var form = new AlarmTargetStatuSelectCurtainForm();
                    form.AddForm(device, listTaskinfo);
                    form.FinishSelectEvent += (statuText, listInfo) =>
                    {
                        btnStatu.Text = statuText;
                        //将新的报警目标添加入缓存
                        this.AddAlarmSettionDataToMemory(device, listInfo);
                    };
                }
                else
                {
                    //其他直接归为开关类
                    var form = new AlarmTargetStatuSelectSwitchForm();
                    form.AddForm(device, listTaskinfo);
                    form.FinishSelectEvent += (statuText, listInfo) =>
                    {
                        btnStatu.Text = statuText;
                        //将新的报警目标添加入缓存
                        this.AddAlarmSettionDataToMemory(device, listInfo);
                    };
                }
            };
        }
 
        /// <summary>
        /// 将新的报警目标添加入缓存
        /// </summary>
        /// <param name="deviceInfo"></param>
        /// <param name="listInfo"></param>
        private void AddAlarmSettionDataToMemory(CommonDevice device, List<Safeguard.TaskListInfo> listInfo)
        {
            string mainKeys = Common.LocalDevice.Current.GetDeviceMainKeys(device);
            if (listInfo == null || listInfo.Count == 0)
            {
                //指定为无动作模式
                if (this.dicSaveDevice.ContainsKey(mainKeys) == true)
                {
                    this.dicSaveDevice.Remove(mainKeys);
                }
            }
            else
            {
                //确认添加动作
                var data = new TaskInfoData();
                this.dicSaveDevice[mainKeys] = data;
                data.MacAddress = device.DeviceAddr;
                data.Epoint = device.DeviceEpoint;
                data.listInfos.AddRange(listInfo);
            }
        }
 
        #endregion
 
        #region ■ 保存选择的设备_____________________
 
        /// <summary>
        /// 保存选择的设备
        /// </summary>
        private async void DoSaveSelectDeviceAsync()
        {
            if (this.dicSaveDevice.Count == 0)
            {
                this.CloseForm();
                return;
            }
 
            var listAction = new List<Safeguard.AlarmActionObj>();
            foreach (var data in this.dicSaveDevice.Values)
            {
                var actionObj = new Safeguard.AlarmActionObj();
                actionObj.DeviceAddr = data.MacAddress;
                actionObj.Epoint = data.Epoint;
                actionObj.Type = 0;
                actionObj.TaskList = data.listInfos;
                listAction.Add(actionObj);
            }
            //添加报警目标到安防
            bool success = await HdlSafeguardLogic.Current.AddAlarmTagetToSafety(this.zoonID, listAction);
            if (success == true)
            {
                //关闭自身
                this.CloseForm();
            }
        }
 
        #endregion
 
        #region ■ 结构体_____________________________
 
        /// <summary>
        /// 报警目标数据
        /// </summary>
        private class TaskInfoData
        {
            /// <summary>
            /// MAC地址
            /// </summary>
            public string MacAddress = string.Empty;
            /// <summary>
            /// 端口号
            /// </summary>
            public int Epoint = 0;
            /// <summary>
            /// 报警动作
            /// </summary>
            public List<Safeguard.TaskListInfo> listInfos = new List<Safeguard.TaskListInfo>();
        }
 
        #endregion
    }
}