黄学彪
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
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using ZigBee.Device;
 
namespace Shared.Phone
{
    /// <summary>
    /// 窗帘的逻辑
    /// </summary>
    public class HdlDeviceCurtainLogic
    {
        #region ■ 变量声明___________________________
 
        /// <summary>
        /// 窗帘的逻辑
        /// </summary>
        private static HdlDeviceCurtainLogic m_Current = null;
        /// <summary>
        /// 窗帘的逻辑
        /// </summary>
        public static HdlDeviceCurtainLogic Current
        {
            get
            {
                if (m_Current == null)
                {
                    m_Current = new HdlDeviceCurtainLogic();
                }
                return m_Current;
            }
        }
 
        #endregion
 
        #region ■ 手拉控制___________________________
 
        /// <summary>
        /// 设置手拉控制状态值 ☆☆☆☆☆
        /// </summary>
        /// <param name="device">窗帘对象</param>
        /// <param name="bolStatu">手拉控制状态值</param>
        /// <returns></returns>
        public bool SetHandPullControl(CommonDevice device, bool bolStatu)
        {
            //如果是虚拟住宅
            if (Common.Config.Instance.Home.IsVirtually == true)
            {
                //添加缓存
                HdlTemplateDeviceDataLogic.Current.SetCurtainHandPullControl(device, bolStatu, null);
                return true;
            }
            //获取发送的命令字符
            var sendData = this.GetHandPullControlText(device.DeviceAddr, device.DeviceEpoint, bolStatu);
            //发送给网关
            var result = HdlDeviceCommonLogic.Current.SendJobjectDataToGateway(device, "SetWritableValue", sendData, "SetWritableValue_Respon");
            if (result.ErrorMsg != null)
            {
                this.ShowTipMsg(result.ErrorMsg);
                return false;
            }
            if (result.ErrorMsgDiv == 0)
            {
                //切换手拉控制失败
                string msg = Language.StringByID(R.MyInternationalizationString.uChangeHandPullControlFail);
                //拼接上【网关回复超时】的Msg
                msg = HdlCommonLogic.Current.CombineGatewayTimeOutMsg(msg, result);
                this.ShowTipMsg(msg);
                return false;
            }
 
            var data = JsonConvert.DeserializeObject<CommonDevice.SetWritableValueResponData>(result.ReceiptData);
            if (data.Status != 0)
            {
                //切换手拉控制失败
                string msg = Language.StringByID(R.MyInternationalizationString.uChangeHandPullControlFail);
                this.ShowTipMsg(msg);
                return false;
            }
            //添加缓存
            HdlTemplateDeviceDataLogic.Current.SetCurtainHandPullControl(device, bolStatu, result.JsonData[0]);
 
            return true;
        }
 
        /// <summary>
        /// 获取更改手拉控制状态的字符串命令 ☆☆☆☆☆
        /// </summary>
        /// <param name="DeviceAddr">DeviceAddr</param>
        /// <param name="DeviceEpoint">DeviceEpoint</param>
        /// <param name="bolStatu">手拉控制状态值</param>
        /// <returns></returns>
        public string GetHandPullControlText(string DeviceAddr, int DeviceEpoint, bool bolStatu)
        {
            int bit2 = bolStatu == true ? 1 : 0;
            int attributeData = bit2 * 4;
            var jObject = new JObject { { "DeviceAddr", DeviceAddr }, { "Epoint", DeviceEpoint }, { "Cluster_ID", 258 }, { "Command", 120 } };
            var data = new JObject { { "Undivided", 0 }, { "AttributeId", 23 }, { "AttributeDataType", 24 }, { "AttributeData", attributeData } };
            jObject.Add("Data", data);
            return jObject.ToString();
        }
 
        #endregion
 
        #region ■ 窗帘方向配置_______________________
 
        /// <summary>
        /// 配置窗帘的方向
        /// </summary>
        /// <param name="rollershade">窗帘对象</param>
        /// <param name="bolStatu">false:电机方向正向;true:电机方向反向</param>
        /// <returns></returns>
        public bool SetCurtainDirection(CommonDevice rollershade, bool bolStatu)
        {
            var result = this.SetCurtainDirectionAsync(rollershade, bolStatu);
            //检测网关返回的共通错误状态码
            string error = HdlCheckLogic.Current.CheckGatewayErrorCode(result);
            if (error != null)
            {
                this.ShowTipMsg(error);
                return false;
            }
            if (result == null || result.setWritableValueResponData == null)
            {
                //窗帘方向设置失败
                string msg = Language.StringByID(R.MyInternationalizationString.uSetCurtainDirectionFail);
                //拼接上【网关回复超时】的Msg
                msg = HdlCommonLogic.Current.CombineGatewayTimeOutMsg(msg, result);
                this.ShowTipMsg(msg);
                return false;
            }
            if (result.setWritableValueResponData.Status != 0)
            {
                //窗帘方向设置失败
                string msg = Language.StringByID(R.MyInternationalizationString.uSetCurtainDirectionFail);
                this.ShowTipMsg(msg);
                return false;
            }
 
            return true;
        }
 
        /// <summary>
        /// 设置开合帘电机方向
        /// <para>isDirectionReversed (false:电机方向正向;true:电机方向反向;)</para> .
        /// </summary>
        private CommonDevice.SetWritableValueResponAllData SetCurtainDirectionAsync(CommonDevice device, bool isDirectionReversed)
        {
            //如果当前是虚拟住宅
            if (Common.Config.Instance.Home.IsVirtually == true)
            {
                return HdlTemplateDeviceDataLogic.Current.SetCurtainDirection(device, isDirectionReversed, null);
            }
            //获取编辑窗帘方向的命令字符
            var sendData = this.GetCurtainDirectionCommadText(device.DeviceAddr, device.DeviceEpoint, isDirectionReversed);
            var result = HdlDeviceCommonLogic.Current.SendJobjectDataToGateway(device, "SetWritableValue", sendData, "SetWritableValue_Respon");
            if (result.ErrorMsg != null || result.ErrorMsgDiv == 0)
            {
                return null;
            }
 
            //添加缓存
            HdlTemplateDeviceDataLogic.Current.SetCurtainDirection(device, isDirectionReversed, result.JsonData[0]);
 
            var tempData = JsonConvert.DeserializeObject<CommonDevice.SetWritableValueResponData>(result.ReceiptData);
            return new CommonDevice.SetWritableValueResponAllData { setWritableValueResponData = tempData };
        }
 
        /// <summary>
        /// 获取编辑窗帘方向的命令字符
        /// </summary>
        /// <param name="DeviceAddr"></param>
        /// <param name="DeviceEpoint"></param>
        /// <param name="isDirectionReversed"></param>
        /// <returns></returns>
        public string GetCurtainDirectionCommadText(string DeviceAddr,int DeviceEpoint, bool isDirectionReversed)
        {
            int bit0 = isDirectionReversed == true ? 1 : 0;
            int attributeData = bit0 * 1;
            var jObject = new JObject { { "DeviceAddr", DeviceAddr }, { "Epoint", DeviceEpoint }, { "Cluster_ID", 258 }, { "Command", 120 } };
            var data = new JObject { { "Undivided", 0 }, { "AttributeId", 23 }, { "AttributeDataType", 24 }, { "AttributeData", attributeData } };
            jObject.Add("Data", data);
 
            return jObject.ToString();
        }
 
        #endregion
 
        #region ■ 开合帘限位配置_____________________
 
        /// <summary>
        /// 设置开合帘限位
        /// </summary>
        /// <param name="rollershade"></param>
        /// <param name="upLimit">开限位</param>
        /// <param name="downLimit">合限位</param>
        /// <param name="curtainLength">导轨长度(这个是给虚拟设备用的)</param>
        /// <returns></returns>
        public bool SetAutoOpenCurtainLimitPoint(CommonDevice rollershade, int upLimit, int downLimit, int curtainLength)
        {
            //如果当前是虚拟住宅
            if (Common.Config.Instance.Home.IsVirtually == true)
            {
                return HdlTemplateDeviceDataLogic.Current.SetCurtainLimitPoint(rollershade, upLimit, downLimit, curtainLength, null);
            }
            //获取编辑开合帘限位的命令字符
            var sendData = this.GetAutoOpenCurtainLimitPointCommandText(rollershade.DeviceAddr, rollershade.DeviceEpoint, upLimit, downLimit);
            var result = HdlDeviceCommonLogic.Current.SendJobjectDataToGateway(rollershade, "SetWritableValue", sendData, "SetWritableValue_Respon");
            if (result.ErrorMsg != null || result.ErrorMsgDiv == 0)
            {
                //设置窗帘限位点失败
                string msg = Language.StringByID(R.MyInternationalizationString.uCommitCurtainLimitFail);
                //拼接上【网关回复超时】的Msg
                msg = HdlCommonLogic.Current.CombineGatewayTimeOutMsg(msg, result);
                this.ShowTipMsg(msg);
                return false;
            }
            var tempData = Newtonsoft.Json.JsonConvert.DeserializeObject<CommonDevice.SetWritableValueResponData>(result.ReceiptData);
            if (tempData.Status != 0)
            {
                //设置窗帘限位点失败
                string msg = Language.StringByID(R.MyInternationalizationString.uCommitCurtainLimitFail);
                this.ShowTipMsg(msg);
                return false;
            }
 
            //添加缓存
            HdlTemplateDeviceDataLogic.Current.SetCurtainLimitPoint(rollershade, upLimit, downLimit, curtainLength, result.JsonData[0]);
 
            return true;
        }
 
 
        /// <summary>
        /// 获取编辑开合帘限位的命令字符
        /// </summary>
        /// <param name="DeviceAddr"></param>
        /// <param name="DeviceEpoint"></param>
        /// <param name="upLimit"></param>
        /// <param name="downLimit"></param>
        /// <returns></returns>
        public string GetAutoOpenCurtainLimitPointCommandText(string DeviceAddr, int DeviceEpoint, int upLimit, int downLimit)
        {
            //这个是开合帘 "2,0x00ff,0x00ff"格式 合限位在前,开限位在后
            string convertData = "\"2,0x" + Convert.ToString(downLimit, 16).PadLeft(4, '0');
            convertData += ",0x" + Convert.ToString(upLimit, 16).PadLeft(4, '0') + "\"";
            //然后将它们转为ASK码数值,然后再转为16进制
            //总计长度为17(这里是16进制)
            string sendData = "11";
            foreach (char c in convertData)
            {
                sendData += Convert.ToString((int)c, 16).PadLeft(2, '0');
            }
 
            var jObject = new JObject { { "DeviceAddr", DeviceAddr }, { "Epoint", DeviceEpoint }, { "Cluster_ID", 258 }, { "Command", 120 } };
            var data = new JObject { { "Undivided", 0 }, { "AttributeId", 24 }, { "AttributeDataType", 65 }, { "AttributeData", sendData } };
            jObject.Add("Data", data);
 
            return jObject.ToString();
        }
 
        #endregion
 
        #region ■ 窗帘限位配置_______________________
 
        /// <summary>
        /// 执行确认及覆盖窗帘限位点
        /// </summary>
        /// <param name="rollershade">窗帘对象</param>
        /// <param name="limiType">确认及覆盖上限位还是下限位</param>
        /// <param name="upLimit">上限位的值(长度值,不是百分比)</param>
        /// <param name="downLimit">下限位的值(长度值,不是百分比)</param>
        /// <returns></returns>
        public async Task<bool> CommitCurtainLimitPoint(Rollershade rollershade, Rollershade.CurtainPrivateInstalledLimi limiType, int upLimit, int downLimit)
        {
            CommonDevice.SetWritableValueResponAllData result = null;
            if (upLimit == -1 && downLimit == -1)
            {
                //这个是卷帘
                result = await rollershade.SetCurtainInstalledLimitAsync(limiType);
            }
            else
            {
                //这个是开合帘 "2,0x00ff,0x00ff"格式 合限位在前,开限位在后
                string convertData = "\"2,0x" + Convert.ToString(downLimit, 16).PadLeft(4, '0');
                convertData += ",0x" + Convert.ToString(upLimit, 16).PadLeft(4, '0') + "\"";
                //然后将它们转为ASK码数值,然后再转为16进制
                //总计长度为17(这里是16进制)
                string sendData = "11";
                foreach (char c in convertData)
                {
                    sendData += Convert.ToString((int)c, 16).PadLeft(2, '0');
                }
                //result = await HdlDeviceAttributeLogic.Current.WriteDeviceAttribute(rollershade, 258, 24, 65, sendData);
            }
            //检测网关返回的共通错误状态码
            string error = HdlCheckLogic.Current.CheckGatewayErrorCode(result);
            if (error != null)
            {
                this.ShowTipMsg(error);
                return false;
            }
            if (result == null || result.setWritableValueResponData == null)
            {
                //设置窗帘限位点失败
                string msg = Language.StringByID(R.MyInternationalizationString.uCommitCurtainLimitFail);
                //拼接上【网关回复超时】的Msg
                msg = HdlCommonLogic.Current.CombineGatewayTimeOutMsg(msg, result);
                this.ShowTipMsg(msg);
                return false;
            }
            if (result.setWritableValueResponData.Status != 0)
            {
                //设置窗帘限位点失败
                string msg = Language.StringByID(R.MyInternationalizationString.uCommitCurtainLimitFail);
                this.ShowTipMsg(msg);
                return false;
            }
 
            return true;
        }
 
        /// <summary>
        /// 删除限位点
        /// </summary>
        /// <param name="rollershade">窗帘对象</param>
        /// <param name="limiType">删除上限位还是删除下限位</param>
        /// <returns></returns>
        public async Task<bool> DeleteCurtainLimitPoint(Rollershade rollershade, Rollershade.LimiType limiType)
        {
            var result = await rollershade.DeleteCurtainLimitsAsync(limiType);
            //检测网关返回的共通错误状态码
            string error = HdlCheckLogic.Current.CheckGatewayErrorCode(result);
            if (error != null)
            {
                this.ShowTipMsg(error);
                return false;
            }
            if (result == null || result.setWritableValueResponData == null)
            {
                //重置窗帘限位点失败
                string msg = Language.StringByID(R.MyInternationalizationString.uDeleteCurtainLimitFail);
                //拼接上【网关回复超时】的Msg
                msg = HdlCommonLogic.Current.CombineGatewayTimeOutMsg(msg, result);
                this.ShowTipMsg(msg);
                return false;
            }
            if (result.setWritableValueResponData.Status != 0)
            {
                //重置窗帘限位点失败
                string msg = Language.StringByID(R.MyInternationalizationString.uDeleteCurtainLimitFail);
                this.ShowTipMsg(msg);
                return false;
            }
            return true;
        }
 
        #endregion
 
        #region ■ 获取窗帘限位配置___________________
 
        /// <summary>
        /// 发送获取窗帘限位配置的命令
        /// </summary>
        /// <param name="rollershade">窗帘对象</param>
        public void SetGetCurtainLimitSettionComand(Rollershade rollershade)
        {
            var jObject = new Newtonsoft.Json.Linq.JObject
            {
                { "DeviceAddr",rollershade.DeviceAddr },
                { "Epoint", rollershade.DeviceEpoint },
                { "Cluster_ID", (int)Cluster_ID.WindowCovering },
                { "Command", 108 }
            };
            var attriBute = new Newtonsoft.Json.Linq.JArray
            {
               new Newtonsoft.Json.Linq.JObject
               {
                 //开合帘方向
                 { "AttriButeId", (int)AttriButeId.WindowCoveringMode}
               },
               new Newtonsoft.Json.Linq.JObject
               {
                 //开合帘总长
                 { "AttriButeId", (int)AttriButeId.WindowCoveringLength}
               },
               new Newtonsoft.Json.Linq.JObject
               {
                 //开限位
                 { "AttriButeId", (int)AttriButeId.InstalledOpenLimitLift}
               },
               new Newtonsoft.Json.Linq.JObject
               {
                 //合限位
                 { "AttriButeId", (int)AttriButeId.InstalledClosedLimitLift}
               }
            };
            var data = new Newtonsoft.Json.Linq.JObject { { "AttriBute", attriBute } };
            jObject.Add("Data", data);
            rollershade.Gateway?.Send(("GetDeviceStatus"), jObject.ToString());
        }
 
        #endregion
 
        #region ■ 重置窗帘___________________________
 
        /// <summary>
        /// 重置窗帘
        /// </summary>
        /// <param name="rollershade">窗帘对象</param>
        /// <returns></returns>
        public async Task<bool> RestoreCurtain(Rollershade rollershade, ShowErrorMode showError = ShowErrorMode.YES)
        {
            var result = await rollershade.RestoreCurtainLimitAsync();
            //检测网关返回的共通错误状态码
            string error = HdlCheckLogic.Current.CheckGatewayErrorCode(result);
            if (error != null)
            {
                if (showError == ShowErrorMode.YES)
                {
                    this.ShowTipMsg(error);
                }
                return false;
            }
            if (result == null || result.setWritableValueResponData == null)
            {
                //重置窗帘失败
                string msg = Language.StringByID(R.MyInternationalizationString.uRestoreCurtainFail);
                //拼接上【网关回复超时】的Msg
                msg = HdlCommonLogic.Current.CombineGatewayTimeOutMsg(msg, result);
                if (showError == ShowErrorMode.YES)
                {
                    this.ShowTipMsg(msg);
                }
                return false;
            }
            if (result.setWritableValueResponData.Status != 0)
            {
                //重置窗帘失败
                string msg = Language.StringByID(R.MyInternationalizationString.uRestoreCurtainFail);
                if (showError == ShowErrorMode.YES)
                {
                    this.ShowTipMsg(msg);
                }
                return false;
            }
            return true;
        }
 
        #endregion
 
        #region ■ 一般方法___________________________
 
        /// <summary>
        /// 显示错误信息窗口
        /// </summary>
        /// <param name="msg"></param>
        private void ShowErrorMsg(string msg)
        {
            Application.RunOnMainThread(() =>
            {
                var contr = new ShowMsgControl(ShowMsgType.Error, msg);
                contr.Show();
            });
        }
 
        /// <summary>
        /// 显示Tip信息窗口
        /// </summary>
        /// <param name="msg"></param>
        private void ShowTipMsg(string msg)
        {
            Application.RunOnMainThread(() =>
            {
                var contr = new ShowMsgControl(ShowMsgType.Tip, msg);
                contr.Show();
            });
        }
 
        #endregion
    }
}