黄学彪
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
using System;
using System.Collections.Generic;
using System.Text;
using ZigBee.Device;
 
namespace Shared.Phone
{
    /// <summary>
    /// 设备功能类型的自定义行控件
    /// </summary>
    public class DeviceFunctionTypeRowControl : FrameCaptionViewControl
    {
        #region ■ 变量声明___________________________
 
        /// <summary>
        /// 结束选择的事件 value:索引(从0开始)  文本信息请用Text获取
        /// </summary>
        public Action<int> FinishSelectEvent = null;
        /// <summary>
        /// 判断该控件能否显示
        /// </summary>
        public bool CanShowRow
        {
            get { return this.CheckCanShowRow(); }
        }
 
        private bool m_SetCanSelect = true;
        /// <summary>
        /// 强制设置能否进行选择(只对继电器和空气开关有效,个别界面需要这种操作)
        /// </summary>
        public bool SetCanSelect
        {
            set 
            {
                m_SetCanSelect = value;
            }
        }
        /// <summary>
        /// 设备对象
        /// </summary>
        private CommonDevice device = null;
        /// <summary>
        /// 当前选择的功能类型索引 -1:还没有设置过 0:不指定 1:开关 2:灯光 3:插座
        /// </summary>
        private int nowSelectNo = -1;
 
        #endregion
 
        #region ■ 初始化_____________________________
 
        /// <summary>
        /// 设备功能类型的自定义行控件(选择功能之后,无条件直接变更类型)
        /// </summary>
        /// <param name="i_device">设备的对象</param>
        /// <param name="i_ChidrenYaxis">子控件Y轴偏移量(【列表控件的rowSpace/2】即可,不懂默认为0即可)</param>
        public DeviceFunctionTypeRowControl(CommonDevice i_device, int i_ChidrenYaxis = 0) : base("", "", i_ChidrenYaxis)
        {
            this.device = i_device;
            this.UseClickStatu = false;
        }
 
        /// <summary>
        /// 初始化
        /// </summary>
        public override void InitControl()
        {
            //初始化初始数据
            this.InitDefultData();
            //初始化底层数据
            base.InitControl();
 
            //空气开关和继电器可以选择功能类型
            if (this.device.Type == DeviceType.AirSwitch
                || this.device.Type == DeviceType.OnOffOutput)
            {
                //没被强制干涉的话
                if (m_SetCanSelect == true && Common.Config.Instance.Home.IsShowTemplate == false)
                {
                    this.UseClickStatu = true;
                    //右箭头
                    this.AddRightArrow();
 
                    //读取设备功能类型
                    this.ReadDeviceFunctionType();
 
                    this.ButtonClickEvent += (sender, e) =>
                    {
                        //显示选择设备功能类型的界面
                        this.ShowSelectDeviceFunctionListForm();
                    };
                }
            }
        }
 
        #endregion
 
        #region ■ 显示选择设备功能类型_______________
 
        /// <summary>
        /// 显示选择设备功能类型的界面
        /// </summary>
        private void ShowSelectDeviceFunctionListForm()
        {
            //显示列表
            var listText = new List<string>();
            listText.Add(Language.StringByID(R.MyInternationalizationString.uSwitch));//开关
            listText.Add(Language.StringByID(R.MyInternationalizationString.uLight));//灯光
            listText.Add(Language.StringByID(R.MyInternationalizationString.uSocket1));//插座
            //标题:选择功能类型
            var title = Language.StringByID(R.MyInternationalizationString.uSelectFunctionType);
 
            var form = new BottomItemSelectForm();
            form.CancelCallEvent = true;//允许取消
            form.AddForm(title, listText, null, nowSelectNo - 1);
            form.FinishSelectEvent += (selectNo) =>
            {
                if (selectNo == nowSelectNo - 1)
                {
                    //选择的是相同的
                    return;
                }
                //-1:选择取消
                this.Text = selectNo == -1 ? string.Empty : listText[selectNo];
                nowSelectNo = selectNo + 1;
 
                this.CanClick = false;
                HdlThreadLogic.Current.RunThread(() =>
                {
                    //设置功能类型到网关
                    var result = HdlDeviceCommonLogic.Current.SendDeviceFunctionTypeToGateway(this.device, (DeviceFunctionType)nowSelectNo);
                    
                    HdlThreadLogic.Current.RunMain(() =>
                    {
                        if (result == true)
                        {
                            //记录起当前选择的功能类型
                            this.RefreshDfunctionType();
 
                            //设备改变功能类型的话,主页需要重新刷新
                            UserView.UserPage.Instance.RefreshAllForm = true;
 
                            //调用回调函数
                            this.FinishSelectEvent?.Invoke(nowSelectNo);
                        }
                        else
                        {
                            //设置设备功能类型失败
                            var alert = new ShowMsgControl(ShowMsgType.Tip, Language.StringByID(R.MyInternationalizationString.uSetDeviceFunctionTypeFail));
                            alert.Show();
                        }
                        this.CanClick = true;
                    });
                });
            };
        }
        #endregion
 
        #region ■ 初始化初始数据_____________________
 
        /// <summary>
        /// 初始化初始数据
        /// </summary>
        private void InitDefultData()
        {
            //标题:功能类型
            this.btnCaption.Text = Language.StringByID(R.MyInternationalizationString.uFunctionType);
 
            var myFunType = this.device.DfunctionType;
            //功能类型的翻译名字
            string strType = string.Empty;
 
            if (this.device.DfunctionType == DeviceFunctionType.A不指定)
            {
                nowSelectNo = 0;
            }
            else if (this.device.DfunctionType == DeviceFunctionType.A开关)
            {
                strType = Language.StringByID(R.MyInternationalizationString.uSwitch);
                nowSelectNo = 1;
            }
            else if (myFunType == DeviceFunctionType.A灯光)
            {
                strType = Language.StringByID(R.MyInternationalizationString.uLight);
                nowSelectNo = 2;
            }
            else if (this.device.DfunctionType == DeviceFunctionType.A插座)
            {
                strType = Language.StringByID(R.MyInternationalizationString.uSocket1);
                nowSelectNo = 3;
            }
 
            if (this.device.Type == DeviceType.ColorDimmableLight
                || this.device.Type == DeviceType.ColorTemperatureLight
                || this.device.Type == DeviceType.DimmableLight)
            {
                //灯光类固定为 灯光
                var infoContent = HdlDeviceCommonLogic.Current.GetDeviceModelIdNameInfo("A418");
                strType = infoContent != null ? infoContent.A官方名字 : string.Empty;
                nowSelectNo = 2;
            }
            else if (this.device.Type == DeviceType.WindowCoveringDevice)
            {
                //窗帘固定为 遮阳
                var infoContent = HdlDeviceCommonLogic.Current.GetDeviceModelIdNameInfo("A400");
                strType = infoContent != null ? infoContent.A官方名字 : string.Empty;
                nowSelectNo = -1;
            }
            else if (this.device.Type == DeviceType.Thermostat)
            {
                //空调固定为 空调
                var infoContent = HdlDeviceCommonLogic.Current.GetDeviceModelIdNameInfo("A406");
                strType = infoContent != null ? infoContent.A官方名字 : string.Empty;
                nowSelectNo = -1;
            }
            else if (this.device.Type == DeviceType.DoorLock)
            {
                //门锁固定为 门锁
                var infoContent = HdlDeviceCommonLogic.Current.GetDeviceModelIdNameInfo("A405");
                strType = infoContent != null ? infoContent.A官方名字 : string.Empty;
                nowSelectNo = -1;
            }
            else if (this.device.Type == DeviceType.FreshAir)
            {
                //新风固定为 新风
                strType = Language.StringByID(R.MyInternationalizationString.FreshAir);
                nowSelectNo = -1;
            }
            else if (this.device.Type == DeviceType.PMSensor)
            {
                //PM2.5传感器固定为 空气质量
                strType = Language.StringByID(R.MyInternationalizationString.AirQuality);
                nowSelectNo = -1;
            }
 
            //显示文本
            this.txtView.Text = strType;
        }
 
        #endregion
 
        #region ■ 检测能否显示_______________________
 
        /// <summary>
        /// 检测能否显示
        /// </summary>
        /// <returns></returns>
        private bool CheckCanShowRow()
        {
            if (this.device == null) { return true; }
 
            if (this.device.Type == DeviceType.AirSwitch//空气开关
                || this.device.Type == DeviceType.ColorDimmableLight//彩灯
                || this.device.Type == DeviceType.DimmableLight//调光灯
                || this.device.Type == DeviceType.DoorLock//门锁
                || this.device.Type == DeviceType.OnOffOutput//继电器
                || this.device.Type == DeviceType.Thermostat//空调
                || this.device.Type == DeviceType.FreshAir//新风
                || this.device.Type == DeviceType.PMSensor //PM2.5
                || this.device.Type == DeviceType.ColorTemperatureLight //色温灯
                || this.device.Type == DeviceType.WindowCoveringDevice)//窗帘
            {
                return true;
            }
            return false;
        }
 
        #endregion
 
        #region ■ 控件摧毁___________________________
 
        /// <summary>
        /// 控件摧毁
        /// </summary>
        public override void RemoveFromParent()
        {
            this.FinishSelectEvent = null;
 
            base.RemoveFromParent();
        }
 
        #endregion
 
        #region ■ 一般方法___________________________
 
        /// <summary>
        /// 刷新设备功能类型
        /// </summary>
        private void RefreshDfunctionType()
        {
            if (this.nowSelectNo == 1)
            {
                this.device.DfunctionType = DeviceFunctionType.A开关;
                if (this.device.IsCustomizeImage == false)
                {
                    //重新设置图片
                    this.device.IconPath = "Device/Switch.png";
                }
            }
            else if (this.nowSelectNo == 2)
            {
                this.device.DfunctionType = DeviceFunctionType.A灯光;
                if (this.device.IsCustomizeImage == false)
                {
                    //重新设置图片
                    this.device.IconPath = "Device/Light.png";
                }
            }
            else if (this.nowSelectNo == 3)
            {
                this.device.DfunctionType = DeviceFunctionType.A插座;
                if (this.device.IsCustomizeImage == false)
                {
                    //重新设置图片
                    this.device.IconPath = "Device/Socket1.png";
                }
            }
            else
            {
                this.device.DfunctionType = DeviceFunctionType.A不指定;
                if (this.device.IsCustomizeImage == false)
                {
                    //重新设置图片
                    if (this.device.Type == DeviceType.AirSwitch)
                    {
                        //空气开关
                        this.device.IconPath = "Device/Switch.png";
                    }
                    else if (this.device.Type == DeviceType.OnOffOutput)
                    {
                        //继电器
                        this.device.IconPath = "Device/RelayEpoint.png";
                    }
                }
            }
            this.device.ReSave();
        }
 
        #endregion
 
        #region ■ 读取设备功能类型___________________
 
        /// <summary>
        /// 读取设备功能类型
        /// </summary>
        private void ReadDeviceFunctionType()
        {
            HdlThreadLogic.Current.RunThread(() =>
            {
                //读取设备功能类型
                var info = HdlDeviceCommonLogic.Current.ReadDeviceEpointDeviceInfo(this.device);
                if (info != null && ((int)this.device.DfunctionType) != info.FunctionType)
                {
                    this.device.DfunctionType = (DeviceFunctionType)info.FunctionType;
                    HdlThreadLogic.Current.RunMain(() =>
                    {
                        //刷新控件
                        this.InitDefultData();
                        //刷新设备功能类型
                        this.RefreshDfunctionType();
                    });
                }
            });
        }
 
        #endregion
    }
}