HDL Home App 第二版本 旧平台金堂用 正在使用
黄学彪
2019-11-13 8b9ce384b26c414db32f98e94e088f5334869c2d
ZigbeeApp/Shared/Phone/UserCenter/CommonBase/Logic/HdlGatewayLogic.cs
@@ -15,11 +15,11 @@
        #region ■ 变量声明___________________________
        /// <summary>
        /// 备份业务的逻辑
        /// 网关业务的逻辑类
        /// </summary>
        private static HdlGatewayLogic m_Current = null;
        /// <summary>
        /// 备份业务的逻辑
        /// 网关业务的逻辑类
        /// </summary>
        public static HdlGatewayLogic Current
        {
@@ -179,20 +179,34 @@
        #region ■ 添加网关___________________________
        /// <summary>
        /// 添加新网关(仅限追加新的网关  1:正常  -1:异常  0:当前的网关绑定在了当前账号下的不同住宅里面)
        /// 添加新网关(仅限追加新的网关)
        /// </summary>
        /// <param name="zbGateway">网关</param>
        /// <param name="mode">是否显示错误</param>
        public async Task<int> AddNewGateway(ZbGateway zbGateway, ShowErrorMode mode)
        public async Task<bool> AddNewGateway(ZbGateway zbGateway, ShowErrorMode mode)
        {
            //执行添加网关到内存
            var result = await this.DoAddGatewayToMemory(zbGateway, mode);
            //前的网关绑定在了当前账号下的不同住宅里面
            if (result == 0)
            {
                if (mode == ShowErrorMode.YES)
                {
                    //网关绑定在当前账号下的其他住宅里\r\n请解除绑定后再试
                    string msg = Language.StringByID(R.MyInternationalizationString.uTheGatewayInOtherResidenceMsg);
                    if (msg.Contains("{0}") == true)
                    {
                        msg = string.Format(msg, "\r\n");
                    }
                    this.ShowTipMsg(msg);
                }
                return false;
            }
            if (result != 1)
            {
                return result;
                return false;
            }
            return 1;
            return true;
        }
        /// <summary>
@@ -1013,6 +1027,198 @@
        #endregion
        #region ■ 获取协调器当前信道_________________
        /// <summary>
        /// 获取协调器当前信道(会有等待延迟,返回-1代表错误)
        /// </summary>
        /// <param name="zbGateway"></param>
        /// <returns></returns>
        public int GetGatewayChannelId(ZbGateway zbGateway)
        {
            ZbGateway realWay = null;
            if (this.GetRealGateway(ref realWay, zbGateway) == false)
            {
                //错误:网关对象丢失
                this.ShowTipMsg(Language.StringByID(R.MyInternationalizationString.uErrorGatewayLostMsg));
                return -1;
            }
            ChannelIdInfo data = null;
            Action<string, string> action = (topic, message) =>
            {
                var gatewayID = topic.Split('/')[0];
                if (topic == gatewayID + "/" + "ZbGw/GetChannel_Respon")
                {
                    var jobject = Newtonsoft.Json.Linq.JObject.Parse(message);
                    data = Newtonsoft.Json.JsonConvert.DeserializeObject<ChannelIdInfo>(jobject["Data"].ToString());
                }
            };
            realWay.Actions += action;
            var jObject = new Newtonsoft.Json.Linq.JObject { { "Cluster_ID", 64512 }, { "Command", 8 } };
            realWay.Send("ZbGw/GetChannel", jObject.ToString());
            int TimeOut = 0;
            while (data == null && TimeOut < 30)
            {
                System.Threading.Thread.Sleep(100);
                TimeOut++;
            }
            realWay.Actions -= action;
            if (data == null)
            {
                //获取协调器信道失败
                string msg = Language.StringByID(R.MyInternationalizationString.uGetGatewayChannelIdFail);
                msg = UserCenterLogic.CombineGatewayTimeOutMsg(msg, null, "回复超时");
                this.ShowTipMsg(msg);
                return -1;
            }
            return data.Channel;
        }
        /// <summary>
        /// 网关信道信息
        /// </summary>
        private class ChannelIdInfo
        {
            /// <summary>
            /// 网关信道
            /// </summary>
            public int Channel = -1;
        }
        #endregion
        #region ■ 获取协调器MAC______________________
        /// <summary>
        /// 获取协调器MAC地址(会有等待延迟,返回null代表错误)
        /// </summary>
        /// <param name="zbGateway"></param>
        /// <returns></returns>
        public string GetGatewayCoordinatorMac(ZbGateway zbGateway)
        {
            ZbGateway realWay = null;
            if (this.GetRealGateway(ref realWay, zbGateway) == false)
            {
                //错误:网关对象丢失
                this.ShowTipMsg(Language.StringByID(R.MyInternationalizationString.uErrorGatewayLostMsg));
                return null;
            }
            CoordinatorMacInfo data = null;
            Action<string, string> action = (topic, message) =>
            {
                var gatewayID = topic.Split('/')[0];
                if (topic == gatewayID + "/" + "ZbGw/GetMac_Respon")
                {
                    var jobject = Newtonsoft.Json.Linq.JObject.Parse(message);
                    data = Newtonsoft.Json.JsonConvert.DeserializeObject<CoordinatorMacInfo>(jobject["Data"].ToString());
                }
            };
            realWay.Actions += action;
            var jObject = new Newtonsoft.Json.Linq.JObject { { "Cluster_ID", 64512 }, { "Command", 13 } };
            realWay.Send("ZbGw/GetMac", jObject.ToString());
            int TimeOut = 0;
            while (data == null && TimeOut < 30)
            {
                System.Threading.Thread.Sleep(100);
                TimeOut++;
            }
            realWay.Actions -= action;
            if (data == null)
            {
                //获取协调器Mac失败
                string msg = Language.StringByID(R.MyInternationalizationString.uGetGatewayCoordinatorMacFail);
                msg = UserCenterLogic.CombineGatewayTimeOutMsg(msg, null, "回复超时");
                this.ShowTipMsg(msg);
                return null;
            }
            return data.MacAddr;
        }
        /// <summary>
        /// 网关协调器Mac信息
        /// </summary>
        private class CoordinatorMacInfo
        {
            /// <summary>
            /// 调器Mac
            /// </summary>
            public string MacAddr = string.Empty;
        }
        #endregion
        #region ■ 获取协调器PanID____________________
        /// <summary>
        /// 获取协调器PanID(会有等待延迟,返回-1代表错误)
        /// </summary>
        /// <param name="zbGateway"></param>
        /// <returns></returns>
        public int GetGatewayPanId(ZbGateway zbGateway)
        {
            ZbGateway realWay = null;
            if (this.GetRealGateway(ref realWay, zbGateway) == false)
            {
                //错误:网关对象丢失
                this.ShowTipMsg(Language.StringByID(R.MyInternationalizationString.uErrorGatewayLostMsg));
                return -1;
            }
            PanIdInfo data = null;
            Action<string, string> action = (topic, message) =>
            {
                var gatewayID = topic.Split('/')[0];
                if (topic == gatewayID + "/" + "ZbGw/GetPanId_Respon")
                {
                    var jobject = Newtonsoft.Json.Linq.JObject.Parse(message);
                    data = Newtonsoft.Json.JsonConvert.DeserializeObject<PanIdInfo>(jobject["Data"].ToString());
                }
            };
            realWay.Actions += action;
            var jObject = new Newtonsoft.Json.Linq.JObject { { "Cluster_ID", 64512 }, { "Command", 12 } };
            realWay.Send("ZbGw/GetPanId", jObject.ToString());
            int TimeOut = 0;
            while (data == null && TimeOut < 30)
            {
                System.Threading.Thread.Sleep(100);
                TimeOut++;
            }
            realWay.Actions -= action;
            if (data == null)
            {
                //获取协调器PanID失败
                string msg = Language.StringByID(R.MyInternationalizationString.uGetGatewayPanIDFail);
                msg = UserCenterLogic.CombineGatewayTimeOutMsg(msg, null, "回复超时");
                this.ShowTipMsg(msg);
                return -1;
            }
            return data.PANID;
        }
        /// <summary>
        /// 网关PanId信息
        /// </summary>
        private class PanIdInfo
        {
            /// <summary>
            /// PanId
            /// </summary>
            public int PANID = -1;
        }
        #endregion
        #region ■ 获取网关GwInfo里面的属性___________
        /// <summary>
@@ -1044,44 +1250,6 @@
            {
                //获取本地的属性 
                objValue = localWay.getGwInfo.GetType().InvokeMember(attributeName, System.Reflection.BindingFlags.GetField, null, localWay.getGwInfo, null);
            }
            if (objValue == null)
            {
                return defult;
            }
            return objValue;
        }
        /// <summary>
        /// 获取网关GatewayBaseInfo里面的属性
        /// </summary>
        /// <param name="zbGateway">网关对象</param>
        /// <param name="attributeName">GatewayBaseInfo里面属性的名字</param>
        /// <param name="defult">如果获取不到时,设置的默认值</param>
        /// <returns></returns>
        public object GetGatewayBaseInfoAttribute(ZbGateway zbGateway, string attributeName, string defult = "")
        {
            string gwID = this.GetGatewayId(zbGateway);
            var localWay = this.GetLocalGateway(gwID);
            object objValue = null;
            if (localWay == null || localWay.getGatewayBaseInfo == null)
            {
                //本地没有记录有这个东西,则直接返回参数的数据
                if (zbGateway.getGatewayBaseInfo != null)
                {
                    objValue = zbGateway.getGatewayBaseInfo.GetType().InvokeMember(attributeName, System.Reflection.BindingFlags.GetField, null, zbGateway.getGatewayBaseInfo, null);
                }
                else
                {
                    return defult;
                }
            }
            else
            {
                //获取本地属性
                objValue = localWay.getGatewayBaseInfo.GetType().InvokeMember(attributeName, System.Reflection.BindingFlags.GetField, null, localWay.getGatewayBaseInfo, null);
            }
            if (objValue == null)
@@ -1966,6 +2134,74 @@
        #endregion
        #region ■ 设置网关经纬度_____________________
        /// <summary>
        /// 设置网关经纬度
        /// </summary>
        /// <param name="gateway">网关对象</param>
        /// <param name="Longitude">经度</param>
        /// <param name="Latitude">维度</param>
        /// <returns></returns>
        public bool SetGatewaySite(ZbGateway gateway, double Longitude, double Latitude)
        {
            ZbGateway realWay = null;
            if (this.GetRealGateway(ref realWay, gateway) == false)
            {
                //错误:网关对象丢失
                string msg = Language.StringByID(R.MyInternationalizationString.uErrorGatewayLostMsg);
                this.ShowTipMsg(msg);
                return false;
            }
            int result = -1;
            Action<string, string> action = (topic, message) =>
            {
                var gatewayID = topic.Split('/')[0];
                if (topic == gatewayID + "/" + "Logic/SetSite_Respon")
                {
                    var jobject = Newtonsoft.Json.Linq.JObject.Parse(message);
                    result = Convert.ToInt32(jobject["Data"]["Result"].ToString());
                }
            };
            realWay.Actions += action;
            //两位小数
            Longitude = Math.Round(Longitude, 2);
            Latitude = Math.Round(Latitude, 2);
            int intLongitude = Convert.ToInt32(Longitude.ToString().Replace(".", string.Empty));
            int intLatitude = Convert.ToInt32(Latitude.ToString().Replace(".", string.Empty));
            var jObject = new Newtonsoft.Json.Linq.JObject { { "Cluster_ID", 0 }, { "Command", 2013 } };
            var data = new Newtonsoft.Json.Linq.JObject { { "Longitude", intLongitude }, { "Latitude", intLatitude } };
            jObject.Add("Data", data);
            realWay.Send("Logic/SetSite", jObject.ToString());
            int TimeOut = 0;
            while (result == -1 && TimeOut < 30)
            {
                System.Threading.Thread.Sleep(100);
                TimeOut++;
            }
            realWay.Actions -= action;
            if (result != 0)
            {
                //设置网关经纬度失败
                string msg = Language.StringByID(R.MyInternationalizationString.uSetGatewaySiteFail);
                if (result == -1)
                {
                    msg = UserCenterLogic.CombineGatewayTimeOutMsg(msg, null, "回复超时");
                }
                this.ShowTipMsg(msg);
                return false;
            }
            return true;
        }
        #endregion
        #region ■ 解绑云端网关_______________________
        /// <summary>
@@ -2120,6 +2356,7 @@
        {
            //获取从云端那里得来的全部文件
            var listBackFile = HdlAutoBackupLogic.GetFileFromDirectory(UserCenterLogic.CombinePath(backDirectory));
            listBackFile.Sort();
            //添加附加情报:还原设备配置
            ProgressBar.SetAppendText(Language.StringByID(R.MyInternationalizationString.uRecoverDeviceSettion));
            ProgressBar.SetMaxValue(listBackFile.Count);
@@ -2276,12 +2513,62 @@
            }
            else if (backType == GatewayBackupEnum.A窗帘方向)
            {
                var statu = Newtonsoft.Json.JsonConvert.DeserializeObject<bool>(System.Text.Encoding.UTF8.GetString(byteData));
                result = await HdlDeviceCurtainLogic.Current.SetCurtainDirection((Rollershade)device, statu);
            }
            else if (backType == GatewayBackupEnum.A窗帘手拉控制)
            {
                var statu = Newtonsoft.Json.JsonConvert.DeserializeObject<bool>(System.Text.Encoding.UTF8.GetString(byteData));
                result = await HdlDeviceCurtainLogic.Current.SetHandPullControl((Rollershade)device, statu);
            }
            else if (backType == GatewayBackupEnum.A窗帘上下限位)
            {
                var curtainDevice = (Rollershade)device;
                var recoverData = Newtonsoft.Json.JsonConvert.DeserializeObject<Newtonsoft.Json.Linq.JObject>(System.Text.Encoding.UTF8.GetString(byteData));
                int upLimit = Convert.ToInt32(recoverData["upLimit"]);
                int downLimit = Convert.ToInt32(recoverData["downLimit"]);
                //先重置窗帘
                result = await HdlDeviceCurtainLogic.Current.RestoreCurtain(curtainDevice);
                if (result == false) { return -1; }
                await Task.Delay(5000);
                //重置上限位
                result = await HdlDeviceCurtainLogic.Current.DeleteCurtainLimitPoint(curtainDevice, Rollershade.LimiType.UpLimit);
                if (result == false) { return -1; }
                //将窗帘调整到指定百分比
                curtainDevice.WcdGoToTiltValue(upLimit);
                await Task.Delay(3000);
                //执行确认及覆盖窗帘限位点
                result = await HdlDeviceCurtainLogic.Current.CommitCurtainLimitPoint(curtainDevice, Rollershade.CurtainPrivateInstalledLimi.UpLimit, -1, -1);
                if (result == false) { return -1; }
                await Task.Delay(2000);
                //重置下限位
                result = await HdlDeviceCurtainLogic.Current.DeleteCurtainLimitPoint(curtainDevice, Rollershade.LimiType.DownLimit);
                if (result == false) { return -1; }
                //将窗帘调整到指定百分比
                curtainDevice.WcdGoToTiltValue(downLimit);
                await Task.Delay(3000);
                //执行确认及覆盖窗帘限位点
                result = await HdlDeviceCurtainLogic.Current.CommitCurtainLimitPoint(curtainDevice, Rollershade.CurtainPrivateInstalledLimi.DownLimit, -1, -1);
            }
            else if (backType == GatewayBackupEnum.A空调自定义模式)
            {
                var data = Newtonsoft.Json.JsonConvert.DeserializeObject<int>(System.Text.Encoding.UTF8.GetString(byteData));
                result = await HdlDeviceAirConditionerLogic.Current.SetAcModeSupport((AC)device, data);
                if (result == true)
                {
                    //转换为二进制
                    var value = Convert.ToString(data, 2).PadLeft(16, '0');
                    //这五个设置是放在后面的
                    var fixValue = value.Substring(0, value.Length - 5);
                    var reportValue = value.Substring(fixValue.Length);
                    //更改缓存
                    for (int i = 0; i < reportValue.Length; i++)
                    {
                        ((AC)device).listSupportMode[i] = Convert.ToInt32(reportValue[i]);
                    }
                    device.ReSave();
                }
            }
            return result == true ? 1 : -1;
        }