xm
2020-04-16 6fa9d69da922c8049f5acfcbb9ce9fd26811024c
ZigbeeApp/Shared/Phone/UserCenter/CommonBase/Logic/HdlDeviceCurtainLogic.cs
New file
@@ -0,0 +1,366 @@
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using ZigBee.Device;
namespace Shared.Phone.UserCenter
{
    /// <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="rollershade">窗帘对象</param>
        /// <param name="bolStatu">手拉控制状态值</param>
        /// <returns></returns>
        public async Task<bool> SetHandPullControl(Rollershade rollershade, bool bolStatu)
        {
            var result = await rollershade.SetCurtianMaintenanceAsync(bolStatu);
            //检测网关返回的共通错误状态码
            string error = HdlCheckLogic.Current.CheckCommonErrorCode(result);
            if (error != null)
            {
                this.ShowTipMsg(error);
                return false;
            }
            if (result == null || result.setWritableValueResponData == null)
            {
                //切换手拉控制失败
                string msg = Language.StringByID(R.MyInternationalizationString.uChangeHandPullControlFail);
                //拼接上【网关回复超时】的Msg
                msg = UserCenterLogic.CombineGatewayTimeOutMsg(msg, result);
                this.ShowTipMsg(msg);
                return false;
            }
            if (result.setWritableValueResponData.Status != 0)
            {
                //切换手拉控制失败
                string msg = Language.StringByID(R.MyInternationalizationString.uChangeHandPullControlFail);
                this.ShowTipMsg(msg);
                return false;
            }
            //备份属性
            await HdlGatewayLogic.Current.UpLoadDeviceBackupDataToGateway(rollershade, GatewayBackupEnum.A窗帘手拉控制, bolStatu);
            return true;
        }
        #endregion
        #region ■ 窗帘方向配置_______________________
        /// <summary>
        /// 配置窗帘的方向
        /// </summary>
        /// <param name="rollershade">窗帘对象</param>
        /// <param name="bolStatu">false:电机方向正向;true:电机方向反向</param>
        /// <returns></returns>
        public async Task<bool> SetCurtainDirection(Rollershade rollershade, bool bolStatu)
        {
            var result = await rollershade.SetCurtainDirectionAsync(bolStatu);
            //检测网关返回的共通错误状态码
            string error = HdlCheckLogic.Current.CheckCommonErrorCode(result);
            if (error != null)
            {
                this.ShowTipMsg(error);
                return false;
            }
            if (result == null || result.setWritableValueResponData == null)
            {
                //窗帘方向设置失败
                string msg = Language.StringByID(R.MyInternationalizationString.uSetCurtainDirectionFail);
                //拼接上【网关回复超时】的Msg
                msg = UserCenterLogic.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;
            }
            //备份设备
            await this.BackupCurtainDirection(rollershade, bolStatu);
            return true;
        }
        /// <summary>
        /// 备份窗帘方向
        /// </summary>
        /// <param name="rollershade">窗帘对象</param>
        /// <param name="bolStatu">false:电机方向正向;true:电机方向反向</param>
        /// <returns></returns>
        public async Task<bool> BackupCurtainDirection(Rollershade rollershade, bool bolStatu)
        {
            //备份设备
            return await HdlGatewayLogic.Current.UpLoadDeviceBackupDataToGateway(rollershade, GatewayBackupEnum.A窗帘方向, bolStatu);
        }
        #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.CheckCommonErrorCode(result);
            if (error != null)
            {
                this.ShowTipMsg(error);
                return false;
            }
            if (result == null || result.setWritableValueResponData == null)
            {
                //设置窗帘限位点失败
                string msg = Language.StringByID(R.MyInternationalizationString.uCommitCurtainLimitFail);
                //拼接上【网关回复超时】的Msg
                msg = UserCenterLogic.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;
            }
            if (upLimit != -1 && downLimit != -1)
            {
                //备份设备
                await this.BackupCurtainLimitPoint(rollershade, upLimit, downLimit);
            }
            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.CheckCommonErrorCode(result);
            if (error != null)
            {
                this.ShowTipMsg(error);
                return false;
            }
            if (result == null || result.setWritableValueResponData == null)
            {
                //重置窗帘限位点失败
                string msg = Language.StringByID(R.MyInternationalizationString.uDeleteCurtainLimitFail);
                //拼接上【网关回复超时】的Msg
                msg = UserCenterLogic.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;
        }
        /// <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
        #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)
        {
            var result = await rollershade.RestoreCurtainLimitAsync();
            //检测网关返回的共通错误状态码
            string error = HdlCheckLogic.Current.CheckCommonErrorCode(result);
            if (error != null)
            {
                this.ShowTipMsg(error);
                return false;
            }
            if (result == null || result.setWritableValueResponData == null)
            {
                //重置窗帘失败
                string msg = Language.StringByID(R.MyInternationalizationString.uRestoreCurtainFail);
                //拼接上【网关回复超时】的Msg
                msg = UserCenterLogic.CombineGatewayTimeOutMsg(msg, result);
                this.ShowTipMsg(msg);
                return false;
            }
            if (result.setWritableValueResponData.Status != 0)
            {
                //重置窗帘失败
                string msg = Language.StringByID(R.MyInternationalizationString.uRestoreCurtainFail);
                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
    }
}