xm
2019-07-16 b910cb79c9b5bcc204022a3cf9e6950f0a64dfbd
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
using System;
using System.Collections.Generic;
using System.Text;
using ZigBee.Device;
 
namespace Shared.Phone.UserCenter.Safety
{
    /// <summary>
    /// 添加报警目标指定设备类型的设备列表
    /// </summary>
    public class AddDeviceAlarmTargetListForm : UserCenterCommonForm
    {
        /// <summary>
        /// 列表控件
        /// </summary>
        private VerticalScrolViewLayout listView = null;
        /// <summary>
        /// 防区ID(这个东西似乎是唯一的)
        /// </summary>
        private int zoonID = 0;
        /// <summary>
        /// 设备列表
        /// </summary>
        private List<uDeviceInfo> listAllDevice = null;
        /// <summary>
        /// 现存的报警目标
        /// </summary>
        private Dictionary<string, Safeguard.CatActionResponseObj> dicAlarmDevice = null;
        /// <summary>
        /// 需要添加的报警设备
        /// </summary>
        private Dictionary<string, TaskInfoData> dicSaveDevice = new Dictionary<string, TaskInfoData>();
 
        /// <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<uDeviceInfo> i_listDevice)
        {
            this.zoonID = i_zoonID;
            this.listAllDevice = i_listDevice;
 
            //设置头部信息
            base.SetTitleText(i_deviceText);
 
            //完成
            var btnfinish = new TopLayoutFinshView();
            topFrameLayout.AddChidren(btnfinish);
            btnfinish.MouseUpEventHandler += (sender, e) =>
            {
                //保存选择的设备
                this.DoSaveSelectDeviceAsync();
            };
 
            //初始化中部信息
            this.InitMiddleFrame(i_deviceText);
        }
 
        /// <summary>
        /// 初始化中部信息
        /// </summary>
        /// <param name="titleText">列表头部名称</param>
        private void InitMiddleFrame(string titleText)
        {
            //XXX列表
            var btnTitle = new TitleViewControl();
            btnTitle.TextColor = UserCenterColor.Current.TextGrayColor;
            btnTitle.Y = Application.GetRealHeight(60);
            btnTitle.Text = titleText + Language.StringByID(R.MyInternationalizationString.uList);
            bodyFrameLayout.AddChidren(btnTitle);
 
            this.listView = new VerticalScrolViewLayout();
            this.listView.Y = btnTitle.Bottom;
            this.listView.Height = bodyFrameLayout.Height - btnTitle.Bottom;
            bodyFrameLayout.AddChidren(this.listView);
 
            //设置中部信息
            this.SetMiddleFrameInfo();
        }
 
        /// <summary>
        /// 设置中部信息
        /// </summary>
        private void SetMiddleFrameInfo()
        {
            new System.Threading.Thread(() =>
            {
                //获取现存的报警设备
                var listData = Common.LocalSafeguard.Current.GetLocalAlarmTargetInfoByZoneId(this.zoonID);
                dicAlarmDevice = new Dictionary<string, Safeguard.CatActionResponseObj>();
                foreach (var data in listData)
                {
                    if (data.Type == 1)
                    {
                        //不需要场景
                        continue;
                    }
                    string mainkeys = Common.LocalDevice.Current.GetDeviceMainKeys(data.DeviceAddr, data.Epoint);
                    dicAlarmDevice[mainkeys] = data;
                }
 
                foreach (var deviceInfo in listAllDevice)
                {
                    //如果那个设备已经添加了,则不再显示
                    if (Common.LocalSafeguard.Current.IsAlarmDeviceExist(this.zoonID, deviceInfo.Device) == true)
                    {
                        continue;
                    }
                    Application.RunOnMainThread(() =>
                    {
                        //添加行目标
                        this.AddRowLayout(deviceInfo);
                    });
                }
            })
            { IsBackground = true }.Start();
        }
 
        /// <summary>
        /// 添加行目标
        /// </summary>
        /// <param name="deviceInfo"></param>
        private void AddRowLayout(uDeviceInfo deviceInfo)
        {
            var row = new DeviceRoomViewRow(this.listView, deviceInfo.Device, deviceInfo.listRoomName);
            //添加向右的图标
            row.AddRightIconControl();
 
            //状态显示
            var btnStatu = new RowSecondRightTextView();
            row.AddChidren(btnStatu);
 
            string mainKeys = Common.LocalDevice.Current.GetDeviceMainKeys(deviceInfo.Device);
            List<Safeguard.TaskListInfo> listTaskinfo = null;
            if (this.dicAlarmDevice.ContainsKey(mainKeys) == true)
            {
                //显示现存的状态文本
                listTaskinfo = this.dicAlarmDevice[mainKeys].TaskList;
                btnStatu.Text = SafeguardLogic.GetLightAlarmStatuText(listTaskinfo);
                btnStatu.TextColor = UserCenterColor.Current.Green;
            }
            else
            {
                //无动作
                btnStatu.TextID = R.MyInternationalizationString.uNotAction;
            }
            row.MouseUpEvent += (sender, e) =>
            {
                var form = new LightAlarmSettionForm();
                this.AddForm(form, deviceInfo.Device, listTaskinfo);
                form.formCloseEvent += (statuText, listInfo) =>
                {
                    btnStatu.Text = statuText;
                    if (listInfo == null)
                    {
                        //指定为无动作模式
                        if (this.dicSaveDevice.ContainsKey(mainKeys) == true)
                        {
                            this.dicSaveDevice.Remove(mainKeys);
                        }
                        btnStatu.TextColor = UserCenterColor.Current.TextColor;
                    }
                    else
                    {
                        //确认添加动作
                        var data = new TaskInfoData();
                        this.dicSaveDevice[mainKeys] = data;
                        data.MacAddress = deviceInfo.Device.DeviceAddr;
                        data.Epoint = deviceInfo.Device.DeviceEpoint;
                        data.listInfos = listInfo;
 
                        btnStatu.TextColor = UserCenterColor.Current.Green;
                    }
                };
            };
        }
 
        /// <summary>
        /// 保存选择的设备
        /// </summary>
        private async void DoSaveSelectDeviceAsync()
        {
            if (this.dicSaveDevice.Count == 0)
            {
                Application.RunOnMainThread(() =>
                {
                    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);
            }
            //打开进度条
            this.ShowProgressBar();
 
            //添加报警目标到安防
            bool success = await Common.LocalSafeguard.Current.AddAlarmTagetToSafety(this.zoonID, listAction);
            //关闭进度条
            this.CloseProgressBar();
 
            if (success == true)
            {
                Application.RunOnMainThread(() =>
                {
                    //刷新一览画面
                    this.LoadFormMethodByName("AlarmTargetSettionForm", "SetMiddleInfo");
                    //关闭自身
                    this.CloseForm();
                });
            }
        }
 
        /// <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>();
        }
    }
}