wxr
2023-06-06 592974441a4df95fffd9167c90192da1a390b1c2
HDL_ON/DAL/DriverLayer/Control.cs
@@ -83,8 +83,10 @@
        /// <summary>
        /// 判断是否本地加密并且加密key不为空
        /// </summary>
        public bool IsLocalEncryptAndGetAesKey {
            get {
        public bool IsLocalEncryptAndGetAesKey
        {
            get
            {
                return IsLocalEncrypt && (!string.IsNullOrEmpty(DB_ResidenceData.Instance.CurrentRegion.localSecret));
            }
        }
@@ -136,45 +138,51 @@
            }
            set
            {
                if (_GatewayOnline_Cloud != value)
                try
                {
                    _GatewayOnline_Cloud = value;
                    if(GatewayOnline_Local)
                    if (_GatewayOnline_Cloud != value)
                    {
                        return;
                    }
                    if (value)
                    {
                        if (DB_ResidenceData.Instance.HomeGateway == null)
                        _GatewayOnline_Cloud = value;
                        if (GatewayOnline_Local)
                        {
                            return;
                        }
                        if (!DB_ResidenceData.Instance.HomeGateway.gatewayStatus)//远程情况下,网关未链接服务器不能修改主页网关状态
                        if (value)
                        {
                            if (DB_ResidenceData.Instance.HomeGateway == null)
                            {
                                return;
                            }
                            if (!DB_ResidenceData.Instance.HomeGateway.gatewayStatus)//远程情况下,网关未链接服务器不能修改主页网关状态
                            {
                                new System.Threading.Thread(() =>
                                {
                                    System.Threading.Thread.Sleep(3000);
                                    var pm = new DAL.Server.HttpServerRequest();
                                    pm.GetGatewayInfo();
                                })
                                { IsBackground = true }.Start();
                                return;
                            }
                            //修改主页连接状态
                            HomePage.LoadEvent_CheckLinkStatus();
                            MainPage.Log($"网关云端在线,刷新设备状态");
                            new System.Threading.Thread(() =>
                            {
                                System.Threading.Thread.Sleep(3000);
                                var pm = new DAL.Server.HttpServerRequest();
                                pm.GetGatewayInfo();
                                System.Threading.Thread.Sleep(1000);
                                FunctionList.List.ReadAllFunctionStatus();
                            })
                            { IsBackground = true }.Start();
                            return;
                            { IsBackground = true, Priority = System.Threading.ThreadPriority.AboveNormal }.Start();
                        }
                        //修改主页连接状态
                        HomePage.LoadEvent_CheckLinkStatus();
                        MainPage.Log($"网关云端在线,刷新设备状态");
                        new System.Threading.Thread(() =>
                        else
                        {
                            System.Threading.Thread.Sleep(1000);
                            FunctionList.List.ReadAllFunctionStatus();
                        })
                        { IsBackground = true, Priority = System.Threading.ThreadPriority.AboveNormal }.Start();
                            //修改主页连接状态
                            HomePage.LoadEvent_CheckLinkStatus();
                        }
                    }
                    else
                    {
                        //修改主页连接状态
                        HomePage.LoadEvent_CheckLinkStatus();
                    }
                }catch(Exception ex)
                {
                    MainPage.Log($"Set GatewayCloudOnline Error:{ex.Message}");
                }
            }
        }
@@ -249,7 +257,8 @@
        public void SearchLoaclGateway()
        {
            //2021-01-15 : 住宅没有绑定网关的时候不用搜索,并且不能链接mqtt
            if(DB_ResidenceData.Instance.HomeGateway == null || string.IsNullOrEmpty(DB_ResidenceData.Instance.HomeGateway.gatewayId))
            if (DB_ResidenceData.Instance.HomeGateway == null || string.IsNullOrEmpty(DB_ResidenceData.Instance.HomeGateway.gatewayId) ||
                    !UserInfo.Current.IsLogin)
            {
                return;
            }
@@ -308,9 +317,10 @@
        /// </summary>
        public void LoginGateway()
        {
            if(loginGatewayThread== null)
            if (loginGatewayThread == null)
            {
                loginGatewayThread = new System.Threading.Thread(() => {
                loginGatewayThread = new System.Threading.Thread(() =>
                {
                    while (!Ins.myTcpClient.isConnected)
                    {
                        if (Ins.GatewayOnline_Local && myTcpClient.isConnected)
@@ -337,7 +347,8 @@
                try
                {
                    loginGatewayThread?.Abort();
                }catch (Exception ex)
                }
                catch (Exception ex)
                {
                    MainPage.Log($"重启登录网关线程异常:{ex.Message}");
                }
@@ -347,7 +358,7 @@
                    LoginGateway();
                }
            }
        }
@@ -357,9 +368,9 @@
        public void ControlScene(Scene scene)
        {
            //体验模式
            if(MainPage.NoLoginMode)
            if (MainPage.NoLoginMode)
            {
                foreach(var sceneFunction in scene.functions)
                foreach (var sceneFunction in scene.functions)
                {
                    var revString = "";
                    var upDataObj = new AlinkFunctionStatusObj();
@@ -406,7 +417,7 @@
        /// <param name="function"></param>
        /// <param name="useRemote">是否直接使用远程发送</param>
        /// <returns></returns>
        public bool SendWriteCommand(Function function, Dictionary<string, string> commandDictionary, bool useRemote = false,int resend = 3)
        public bool SendWriteCommand(Function function, Dictionary<string, string> commandDictionary, bool useRemote = false, int resend = 3)
        {
            function.controlCounter++;
            function.refreshTime = DateTime.Now;
@@ -453,7 +464,6 @@
                UpdataFunctionStatus(revString, null, true);
                return true;
            }
            //MainPage.Log($"发送数据:{Newtonsoft.Json.JsonConvert.SerializeObject(commandDictionary)}");
@@ -504,8 +514,9 @@
                        var functionControlDataObj = function.GetGatewayAlinkControlData(commandDictionary);
                        var functionControlDataJson = Newtonsoft.Json.JsonConvert.SerializeObject(functionControlDataObj);
                        var sendBytes = Ins.ConvertSendBodyData(CommunicationTopic.ct.ControlFunctionTopic, functionControlDataJson);
                        new Control_Udp().SendLocalHdlLinkData(sendBytes, functionControlDataObj.id,resend);
                        new Control_Udp().SendLocalHdlLinkData(sendBytes, functionControlDataObj.id, resend);
                        MainPage.Log($"本地通讯 发送HDL-Link数据:{functionControlDataJson}");
                            Ins.myTcpClient.SendMessage(sendBytes);
                    }
                }
                //远程通讯
@@ -522,7 +533,7 @@
                                commandDictionary.Add(FunctionAttributeKey.OnOff, commandDictionary[FunctionAttributeKey.Brightness] == "0" ? "off" : "on");
                            }
                        }
                        if(function.spk == SPK.LightCCT)
                        if (function.spk == SPK.LightCCT)
                        {
                            if (!commandDictionary.ContainsKey(FunctionAttributeKey.CCT))
                            {
@@ -546,10 +557,10 @@
        /// <summary>
        /// 全开全关功能
        /// </summary>
        public void SwtichFunctions(bool open,List<Function> functions)
        public void SwtichFunctions(bool open, List<Function> functions)
        {
            //dome模式控制
            if(MainPage.NoLoginMode)
            if (MainPage.NoLoginMode)
            {
                new System.Threading.Thread(() =>
                {
@@ -557,7 +568,7 @@
                    {
                        Dictionary<string, string> d1 = new Dictionary<string, string>();
                        d1.Add(FunctionAttributeKey.OnOff, open ? "on" : "off");
                         if (function.spk == SPK.PanelSocket)
                        if (function.spk == SPK.PanelSocket)
                        {
                            if (function.attributes.Find((obj) => obj.key == "on_off_usb") != null)
                            {
@@ -623,16 +634,16 @@
        {
            var pm = new DAL.Server.HttpServerRequest();
            var pack = pm.GetDeviceInfoList(functionIds);
            if(pack!= null&& pack.Data!=null)
            if (pack != null && pack.Data != null)
            {
                //待测试2021-03-04
                var ddd = Newtonsoft.Json.JsonConvert.DeserializeObject<List<AlinkStatusData>>(pack.Data.ToString());
                if(ddd!= null)
                if (ddd != null)
                {
                    foreach(var function in ddd)
                    foreach (var function in ddd)
                    {
                        var temp = FunctionList.List.GetDeviceFunctionList().Find((obj) => obj.deviceId == function.deviceId);
                        if(temp!= null)
                        if (temp != null)
                        {
                            if (SPK.Get3tySpk(SPK.BrandType.Tuya).Contains(temp.spk))
                            {
@@ -648,7 +659,7 @@
        /// 发送读取命令
        /// 自动判断是否为A协议设备
        /// </summary>
        public void SendReadCommand(Function function ,bool forceRemote = false)
        public void SendReadCommand(Function function, bool forceRemote = false)
        {
            function.refreshTime = DateTime.Now;
            if (forceRemote)
@@ -732,7 +743,7 @@
                            {
                                busClient.ReadBusData(function);
                            }
                        }
                        catch (Exception ex)
                        {
@@ -776,7 +787,7 @@
        /// <summary>
        /// 安防控制
        /// </summary>
        public void ControlSecurity(SecurityAlarm securityAlarm,string state)
        public void ControlSecurity(SecurityAlarm securityAlarm, string state)
        {
            if (!Ins.GatewayOnline_Local)//网关本地不在线
            {
@@ -834,7 +845,7 @@
                };
                var aLinkJson = Newtonsoft.Json.JsonConvert.SerializeObject(aLinkData);
                var sendBytes = Ins.ConvertSendBodyData(CommunicationTopic.ct.ControlScene, aLinkJson);
                new Control_Udp().SendLocalHdlLinkData(sendBytes, aLinkData.id,0);
                new Control_Udp().SendLocalHdlLinkData(sendBytes, aLinkData.id, 0);
            }
        }
@@ -928,10 +939,10 @@
        /// 转换接收到的数据
        /// </summary>
        /// <returns></returns>
        public void ConvertReceiveData(byte[] receiveBytes,string ip)
        public void ConvertReceiveData(byte[] receiveBytes, string ip)
        {
            var reString = Encoding.UTF8.GetString(receiveBytes);
            AnalysisReceiveData(reString, receiveBytes,ip);
            AnalysisReceiveData(reString, receiveBytes, ip);
        }
        /// <summary>
        /// 转换接收到的数据
@@ -939,7 +950,7 @@
        /// <param name="receiveString">转String后的数据</param>
        /// <param name="originalReceiveBytes"原始Bytes数据</param>
        /// <returns></returns>
        public LocalCommunicationData AnalysisReceiveData(string receiveString, byte[] originalReceiveBytes , string sIp = null)
        public LocalCommunicationData AnalysisReceiveData(string receiveString, byte[] originalReceiveBytes, string sIp = null)
        {
            LocalCommunicationData receiveObj = new LocalCommunicationData();
@@ -1078,7 +1089,8 @@
                    }
                }
                else if (receiveObj.Topic == CommunicationTopic.ct.ReadStatus + "_reply" ||
                    receiveObj.Topic == CommunicationTopic.ct.ControlFunctionTopic + "_reply" ||
                    receiveObj.Topic == CommunicationTopic.ct.ControlFunctionTopic + "_reply" ||
                    receiveObj.Topic == CommunicationTopic.ct.ControlGroupControlTopic + "_reply" ||
                    receiveObj.Topic == CommunicationTopic.ct.GatewayUpStatus ||
                    receiveObj.Topic.Contains(CommunicationTopic.ct.GatewayUpSortTopic))
                {
@@ -1118,7 +1130,8 @@
                        MainPage.Log($"安防局域网异常:{ex.Message}");
                    }
                }
                else if (receiveObj.Topic == $"/user/{GatewayId}/custom/gateway/login_reply") {
                else if (receiveObj.Topic == $"/user/{GatewayId}/custom/gateway/login_reply")
                {
                    var temp = Newtonsoft.Json.JsonConvert.DeserializeObject<JObject>(receiveObj.BodyDataString);
                    if (temp.GetValue("objects").ToString().Contains("success"))
                    {
@@ -1161,7 +1174,7 @@
            }
            else
            {
                if(processedDataList.Count> 50)
                if (processedDataList.Count > 50)
                {
                    processedDataList.RemoveAt(0);
                }
@@ -1170,12 +1183,63 @@
        }
        /// <summary>
        /// 更新设备状态
        /// A协议数据
        /// </summary>
        /// <param name="updateBytes"></param>
        public void UpdataFunctionStatus(string revString, byte[] usefulBytes,bool isCloudData = false)
        public void UpdataGroupControlStatus(string revString, byte[] usefulBytes, bool isCloudData = false)
        {
            var temp = JsonConvert.DeserializeObject<AlinkFunctionStatusObj>(revString);
            if (temp != null)
            {
                Control_Udp.ReceiveRepeatManager(temp.id, usefulBytes);
                var allLocalFuntion = FunctionList.List.groupControls;
                foreach (var updateTemp in temp.objects)
                {
                    try
                    {
                        var localFunction = allLocalFuntion.Find((obj) => obj.sid == updateTemp.sid);
                        if (localFunction == null)
                        {
                            continue;
                        }
                        MainPage.Log($"收到数据:{revString}");
                        foreach (var attr in updateTemp.status)
                        {
                            localFunction.time_stamp = temp.time_stamp;
                            localFunction.SetAttrState(attr.key, attr.value);
                        }
                        //更新界面状态
                        switch (localFunction.spk)
                        {
                            case SPK.ElectricEnergy:
                                EnergyMainPage.UpdataStatus(localFunction);
                                break;
                        }
                        HomePage.UpdataFunctionStates(localFunction);
                        RoomPage.UpdataStates(localFunction);
                        FunctionPage.UpdataStates(localFunction);
                        ClassificationPage.UpdataInfo(localFunction);
                    }
                    catch (Exception ex)
                    {
                        MainPage.Log($"A协议更新状态异常:{ex.Message}");
                    }
                }
            }
        }
        /// <summary>
        /// 更新设备状态
        /// A协议数据
        /// </summary>
        public void UpdataFunctionStatus(string revString, byte[] usefulBytes, bool isCloudData = false)
        {
            ////test 云端连接成功时,不适用本地数据更新
            //if (Ins.GatewayOnline_Cloud && !isCloudData)
@@ -1218,7 +1282,7 @@
                        if (Ins.GatewayOnline_Local && isCloudData)//本地链接,除了涂鸦设备数据之外的云端数据不处理
                        {
                            if (!SPK.Get3tySpk(SPK.BrandType.All3tyBrand).Contains(localFunction.spk) && !SPK.MusicSpkList().Contains( localFunction.spk))
                            if (!SPK.Get3tySpk(SPK.BrandType.All3tyBrand).Contains(localFunction.spk) && !SPK.MusicSpkList().Contains(localFunction.spk))
                            {
                                //MainPage.Log($"A协议更新状态:本地链接,除了涂鸦设备数据之外的云端数据不处理........");
@@ -1235,12 +1299,11 @@
                        //        return;
                        //    }
                        //}
                        MainPage.Log($"音乐收到数据:{revString}");
                        MainPage.Log($"收到数据:{revString}");
                        foreach (var attr in updateTemp.status)
                        {
                            localFunction.time_stamp = temp.time_stamp;
                            localFunction.SetAttrState(attr.key, attr.value);
                            Console.WriteLine("音乐播放器添加缓存数据=======" + revString);
                        }
@@ -1256,7 +1319,7 @@
                                break;
                            case SPK.AirSwitch:
                                AirSwitchPage.UpdataState(localFunction);
                                if(localFunction.GetAttribute(FunctionAttributeKey.Power)!=null)//如果是带电量的空开也要更新能源界面
                                if (localFunction.GetAttribute(FunctionAttributeKey.Power) != null)//如果是带电量的空开也要更新能源界面
                                {
                                    EnergyMainPage.UpdataStatus(localFunction);
                                }
@@ -1295,7 +1358,7 @@
                                CurtainModulePage.UpdataState(localFunction);
                                break;
                            case SPK.CurtainDream:
                                if(localFunction.GetAttrState(FunctionAttributeKey.Percent) == "0")
                                if (localFunction.GetAttrState(FunctionAttributeKey.Percent) == "0")
                                {
                                    localFunction.SetAttrState(FunctionAttributeKey.OnOff, "off");
                                }
@@ -1388,7 +1451,7 @@
                            case SPK.SensorTemperature:
                            case SPK.SensorHumidity:
                            case SPK.SensorHcho:
                                if(localFunction.spk == SPK.SensorTemperature)
                                if (localFunction.spk == SPK.SensorTemperature)
                                {
                                    HomePage.LoadEvent_RefreshEnvirIndoorTemp();
                                }
@@ -1490,7 +1553,7 @@
                            case SPK.AvMusic:
                            case SPK.MusicStandard:
                                //UI.Music.MusicMain.mMusicMain.RefreshView(updateTemp);
                                Console.WriteLine("音乐播放器显示状态======="+ revString);
                                Console.WriteLine("音乐播放器显示状态=======" + revString);
                                //wjc
                                break;
                            case SPK.Inverter:
@@ -1524,12 +1587,12 @@
        /// <summary>
        /// //一键(远程)开锁
        /// </summary>
        public void OneKeyUnlocking(Function doorlock,string extStr)
        public void OneKeyUnlocking(Function doorlock, string extStr)
        {
            if (Ins.GatewayOnline_Local)
            {
                Dictionary<string, string> dic = new Dictionary<string, string>();
                var functionControlDataObj = doorlock.GetGatewayAlinkDoorlockData(UserInfo.Current.ID,extStr);
                var functionControlDataObj = doorlock.GetGatewayAlinkDoorlockData(UserInfo.Current.ID, extStr);
                var functionControlDataJson = Newtonsoft.Json.JsonConvert.SerializeObject(functionControlDataObj);
                var sendBytes = Ins.ConvertSendBodyData($"/base/{Control.Ins.GatewayId}/thing/service/doorRemoteOpen/down", functionControlDataJson);
                new Control_Udp().SendLocalHdlLinkData(sendBytes, functionControlDataObj.id, 0);
@@ -1544,7 +1607,7 @@
        /// </summary>
        /// <param name="functionId"></param>
        /// <param name="pwd"></param>
        public void ConfirmUnlocking(string functionId,string pwd)
        public void ConfirmUnlocking(string functionId, string pwd)
        {
            Loading loading = new Loading();
            MainPage.BaseView.AddChidren(loading);
@@ -1580,8 +1643,10 @@
                        //{
                        //    return false;
                        //}
                        if (pack.Code != StateCode.SUCCESS) {
                            Application.RunOnMainThread(() => {
                        if (pack.Code != StateCode.SUCCESS)
                        {
                            Application.RunOnMainThread(() =>
                            {
                                new PublicAssmebly().TipMsg(Language.StringByID(StringId.Tip), pack.message);
                                UserInfo.Current.doorPasswordString = "";//
                                UserInfo.Current.LastTimeOpenDoor = DateTime.MinValue;
@@ -1590,8 +1655,9 @@
                    }
                    if (pack.Code != StateCode.SUCCESS)
                    {
                        Application.RunOnMainThread(() => {
                            if(string.IsNullOrEmpty(pack.message))
                        Application.RunOnMainThread(() =>
                        {
                            if (string.IsNullOrEmpty(pack.message))
                            {
                                new PublicAssmebly().TipMsg(Language.StringByID(StringId.Tip),
                                    Language.StringByID(StringId.OperationFailed));