HDL Home App 第二版本 旧平台金堂用 正在使用
黄学彪
2020-05-19 e747e6d8524e3146bb48dc304a713f309966b1c6
ZigbeeApp/Shared/Phone/UserCenter/CommonBase/Logic/HdlDeviceCurtainLogic.cs
@@ -1,4 +1,7 @@
using System;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Shared.Phone.ModelData;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
@@ -37,22 +40,30 @@
        #region ■ 手拉控制___________________________
        /// <summary>
        /// 设置手拉控制状态值
        /// 设置手拉控制状态值 ☆☆☆☆☆
        /// </summary>
        /// <param name="rollershade">窗帘对象</param>
        /// <param name="device">窗帘对象</param>
        /// <param name="bolStatu">手拉控制状态值</param>
        /// <returns></returns>
        public async Task<bool> SetHandPullControl(Rollershade rollershade, bool bolStatu)
        public bool SetHandPullControl(CommonDevice device, bool bolStatu)
        {
            var result = await rollershade.SetCurtianMaintenanceAsync(bolStatu);
            //检测网关返回的共通错误状态码
            string error = HdlCheckLogic.Current.CheckCommonErrorCode(result);
            if (error != null)
            //如果是虚拟住宅
            if (Common.Config.Instance.Home.IsVirtually == true)
            {
                this.ShowTipMsg(error);
                //添加缓存
                DeviceModelDataLogic.Current.SetCurtainHandPullControl(device, bolStatu, "SetWritableValue");
                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 == null || result.setWritableValueResponData == null)
            if (result.ErrorMsgDiv == 0)
            {
                //切换手拉控制失败
                string msg = Language.StringByID(R.MyInternationalizationString.uChangeHandPullControlFail);
@@ -61,16 +72,33 @@
                this.ShowTipMsg(msg);
                return false;
            }
            if (result.setWritableValueResponData.Status != 0)
            var data = JsonConvert.DeserializeObject<CommonDevice.SetWritableValueResponData>(result.ReceiptData);
            if (data.Status != 0)
            {
                //切换手拉控制失败
                string msg = Language.StringByID(R.MyInternationalizationString.uChangeHandPullControlFail);
                this.ShowTipMsg(msg);
                return false;
            }
            //备份属性
            await HdlGatewayLogic.Current.UpLoadDeviceBackupDataToGateway(rollershade, GatewayBackupEnum.A窗帘手拉控制, bolStatu);
            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
@@ -83,9 +111,9 @@
        /// <param name="rollershade">窗帘对象</param>
        /// <param name="bolStatu">false:电机方向正向;true:电机方向反向</param>
        /// <returns></returns>
        public async Task<bool> SetCurtainDirection(Rollershade rollershade, bool bolStatu)
        public bool SetCurtainDirection(CommonDevice rollershade, bool bolStatu)
        {
            var result = await rollershade.SetCurtainDirectionAsync(bolStatu);
            var result = this.SetCurtainDirectionAsync(rollershade, bolStatu);
            //检测网关返回的共通错误状态码
            string error = HdlCheckLogic.Current.CheckCommonErrorCode(result);
            if (error != null)
@@ -109,22 +137,121 @@
                this.ShowTipMsg(msg);
                return false;
            }
            //备份设备
            await this.BackupCurtainDirection(rollershade, bolStatu);
            return true;
        }
        /// <summary>
        /// 备份窗帘方向
        /// 设置开合帘电机方向
        /// <para>isDirectionReversed (false:电机方向正向;true:电机方向反向;)</para> .
        /// </summary>
        /// <param name="rollershade">窗帘对象</param>
        /// <param name="bolStatu">false:电机方向正向;true:电机方向反向</param>
        /// <returns></returns>
        public async Task<bool> BackupCurtainDirection(Rollershade rollershade, bool bolStatu)
        private CommonDevice.SetWritableValueResponAllData SetCurtainDirectionAsync(CommonDevice device, bool isDirectionReversed)
        {
            //备份设备
            return await HdlGatewayLogic.Current.UpLoadDeviceBackupDataToGateway(rollershade, GatewayBackupEnum.A窗帘方向, bolStatu);
            //如果当前是虚拟住宅
            if (Common.Config.Instance.Home.IsVirtually == true)
            {
                return ModelData.DeviceModelDataLogic.Current.SetCurtainDirection(device, isDirectionReversed, "SetWritableValue");
            }
            //获取编辑窗帘方向的命令字符
            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;
            }
            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 ModelData.DeviceModelDataLogic.Current.SetCurtainLimitPoint(rollershade, upLimit, downLimit, curtainLength, "SetWritableValue");
            }
            //获取编辑开合帘限位的命令字符
            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 = UserCenterLogic.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;
            }
            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
@@ -159,7 +286,7 @@
                {
                    sendData += Convert.ToString((int)c, 16).PadLeft(2, '0');
                }
                result = await HdlDeviceAttributeLogic.Current.WriteDeviceAttribute(rollershade, 258, 24, 65, sendData);
                //result = await HdlDeviceAttributeLogic.Current.WriteDeviceAttribute(rollershade, 258, 24, 65, sendData);
            }
            //检测网关返回的共通错误状态码
            string error = HdlCheckLogic.Current.CheckCommonErrorCode(result);
@@ -183,12 +310,6 @@
                string msg = Language.StringByID(R.MyInternationalizationString.uCommitCurtainLimitFail);
                this.ShowTipMsg(msg);
                return false;
            }
            if (upLimit != -1 && downLimit != -1)
            {
                //备份设备
                await this.BackupCurtainLimitPoint(rollershade, upLimit, downLimit);
            }
            return true;
@@ -227,25 +348,6 @@
                return false;
            }
            return true;
        }
        /// <summary>
        /// 备份窗帘限位点
        /// </summary>
        /// <param name="rollershade">窗帘对象</param>
        /// <param name="upLimit">上限位的值,百分比(备份用)</param>
        /// <param name="downLimit">下限位的值,百分比(备份用)</param>
        /// <returns></returns>
        public async Task<bool> BackupCurtainLimitPoint(Rollershade rollershade, int upLimit, int downLimit)
        {
            //备份设备
            var backData = new Newtonsoft.Json.Linq.JObject
            {
                { "upLimit",upLimit },
                { "downLimit", downLimit }
            };
            //备份设备
           return await HdlGatewayLogic.Current.UpLoadDeviceBackupDataToGateway(rollershade, GatewayBackupEnum.A窗帘上下限位, backData);
        }
        #endregion