xm
2021-12-01 6d73bf6e816570291865674bef8bce8972e4de3f
HDL_ON/DAL/DriverLayer/Control.cs
@@ -38,6 +38,11 @@
                return _control;
            }
        }
        /// <summary>
        /// 记录接收到的消息,方便zb的工程师调试他们的设备
        /// </summary>
        public List<string> MsgInfoList = new List<string>();
        int _msg_id = 1;
        /// <summary>
        /// 通讯ID
@@ -279,6 +284,7 @@
                        else if (DB_ResidenceData.Instance.GatewayType == 1)
                        {
                            new Control_Udp().SearchLocalGateway();
                            new Control_Udp().SearchLocalGateway(true);
                        }
                        System.Threading.Thread.Sleep(500);
                    }
@@ -353,7 +359,7 @@
        /// <param name="function"></param>
        /// <param name="useRemote">是否直接使用远程发送</param>
        /// <returns></returns>
        public void SendWriteCommand(Function function, Dictionary<string, string> commandDictionary, bool useRemote = false)
        public void SendWriteCommand(Function function, Dictionary<string, string> commandDictionary, bool useRemote = false,int resend = 3)
        {
            function.controlCounter++;
            function.refreshTime = DateTime.Now;
@@ -407,9 +413,12 @@
            switch (function.spk)
            {
                case SPK.ElectricTuyaAirCleaner:
                case SPK.ElectricTuyaAirCleaner2:
                case SPK.ElectricTuyaFan:
                case SPK.ElectricTuyaFan2:
                case SPK.ElectricTuyaWaterValve:
                case SPK.ElectricTuyaWeepRobot:
                case SPK.ElectricTuyaWeepRobot2:
                    useRemote = true;
                    break;
            }
@@ -447,7 +456,7 @@
                        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);
                        new Control_Udp().SendLocalHdlLinkData(sendBytes, functionControlDataObj.id,resend);
                        MainPage.Log($"本地通讯 发送HDL-Link数据:{functionControlDataJson}");
                    }
                }
@@ -615,9 +624,9 @@
                        {
                            id = Ins.msg_id.ToString(),
                            objects = new List<Dictionary<string, string>>()
                        {
                            readKey
                        },
                                        {
                                            readKey
                                        },
                            time_stamp = Utlis.GetTimestamp()
                        };
                        var functionControlDataJson = Newtonsoft.Json.JsonConvert.SerializeObject(readDataObj);
@@ -695,9 +704,24 @@
                };
                var aLinkJson = Newtonsoft.Json.JsonConvert.SerializeObject(aLinkData);
                var sendBytes = Ins.ConvertSendBodyData(CommunicationTopic.ct.ControlScene, aLinkJson);
                new Control_Udp().SendLocalHdlLinkData(sendBytes, aLinkData.id);
                new Control_Udp().SendLocalHdlLinkData(sendBytes, aLinkData.id,0);
            }
        }
        /// <summary>
        /// 网关进入配网模式
        /// </summary>
        public void AuthGateway()
        {
            var objects1 = new { spk = "", time = "180" };
            //{"objects":[{"spk":"","time":"180"}],"id":"8","time_stamp":"1635241216669"}
            var sendId = Ins.msg_id.ToString();
            var sendObj = new { objects = objects1, id = sendId, time_stamp = Utlis.GetTimestamp() };
            var aLinkJson = Newtonsoft.Json.JsonConvert.SerializeObject(sendObj);
            var sendBytes = Ins.ConvertSendBodyData(CommunicationTopic.ct.AuthGateway, aLinkJson);
            new Control_Udp().SendLocalHdlLinkData(sendBytes, Ins.msg_id.ToString());
        }
        /// <summary>
        /// 转换发送数据
        /// </summary>
@@ -720,7 +744,7 @@
            //1.拼接头
            string topicString = "Topic:" + topic + "\r\n";
            //2.Body字符串转为byte数组
            byte[] bodyBytes = Encoding.ASCII.GetBytes(bodyDataString);
            byte[] bodyBytes = Encoding.UTF8.GetBytes(bodyDataString);
            //判断是否需加密Body数据
            if (isEncryption && IsLocalEncryptAndGetAesKey)
            {
@@ -731,7 +755,7 @@
            //3.拼接body的Length长度数据
            string lengthString = "Length:" + bodyBytes.Length.ToString() + "\r\n" + "\r\n";
            string topicAndLengthString = topicString + lengthString;
            byte[] topicAndLengthBytes = Encoding.ASCII.GetBytes(topicAndLengthString);
            byte[] topicAndLengthBytes = Encoding.UTF8.GetBytes(topicAndLengthString);
            //4.拼接合并 Topic 和 body的byte数组数据
            byte[] sendDataBytes = new byte[topicAndLengthBytes.Length + bodyBytes.Length];
            topicAndLengthBytes.CopyTo(sendDataBytes, 0);
@@ -747,10 +771,10 @@
        /// 转换接收到的数据
        /// </summary>
        /// <returns></returns>
        public void ConvertReceiveData(byte[] receiveBytes)
        public void ConvertReceiveData(byte[] receiveBytes,string ip)
        {
            var reString = Encoding.UTF8.GetString(receiveBytes);
            AnalysisReceiveData(reString, receiveBytes);
            AnalysisReceiveData(reString, receiveBytes,ip);
        }
        /// <summary>
        /// 转换接收到的数据
@@ -758,7 +782,7 @@
        /// <param name="receiveString">转String后的数据</param>
        /// <param name="originalReceiveBytes"原始Bytes数据</param>
        /// <returns></returns>
        public LocalCommunicationData AnalysisReceiveData(string receiveString, byte[] originalReceiveBytes)
        public LocalCommunicationData AnalysisReceiveData(string receiveString, byte[] originalReceiveBytes , string sIp = null)
        {
            LocalCommunicationData receiveObj = new LocalCommunicationData();
@@ -811,6 +835,10 @@
                            var revString = Encoding.UTF8.GetString(receiveBytes);
                            receiveObj.BodyDataString = revString;
                            MainPage.Log($"局域网信息: 解密后:" + receiveObj.BodyDataString);
                            if(receiveObj.Topic.EndsWith("/thing/property/up"))
                            {
                                MsgInfoList.Add(revString + "\r\n");
                            }
                        }
                        else
                        {
@@ -851,6 +879,10 @@
                        {
                            Ins.GatewayId = device.device_mac;
                        }
                        if (!string.IsNullOrEmpty(sIp))
                        {
                            device.ip_address = sIp;
                        }
                        reportIp = device.ip_address;//主播地址也能控制设备//"239.0.168.188";//
                        //2021-09-23 新增获取当前网关是否本地加密
                        Ins.IsLocalEncrypt = device.isLocalEncrypt;
@@ -859,7 +891,8 @@
                }
                else if (receiveObj.Topic == CommunicationTopic.ct.ReadStatus + "_reply" ||
                    receiveObj.Topic == CommunicationTopic.ct.ControlFunctionTopic + "_reply" ||
                    receiveObj.Topic == CommunicationTopic.ct.GatewayUpStatus)
                    receiveObj.Topic == CommunicationTopic.ct.GatewayUpStatus ||
                    receiveObj.Topic.Contains( CommunicationTopic.ct.GatewayUpSortTopic))
                {
                    //TODO 暂时不传正确的数据上去,如果后面要优化前面这些代码
                    UpdataFunctionStatus(receiveObj.BodyDataString, null);
@@ -961,6 +994,10 @@
                        {
                            case SPK.AirSwitch:
                                AirSwitchPage.UpdataState(localFunction);
                                if(localFunction.GetAttribute(FunctionAttributeKey.Power)!=null)//如果是带电量的空开也要更新能源界面
                                {
                                    EnergyMainPage.UpdataStatus(localFunction);
                                }
                                break;
                            case SPK.ElectricEnergy:
                                EnergyMainPage.UpdataStatus(localFunction);
@@ -974,6 +1011,7 @@
                                DimmerPage.UpdataStates(localFunction);
                                break;
                            case SPK.ElectricFan:
                            case SPK.HvacFan:
                                localFunction.lastState = Language.StringByID(StringId.Level) + " : " +
                                   localFunction.GetAttrState(FunctionAttributeKey.OpenLevel);
                                FanPage.UpdataState(localFunction);
@@ -1003,6 +1041,8 @@
                            case SPK.CurtainShades:
                                break;
                            case SPK.AcStandard:
                            case SPK.HvacAC:
                            case SPK.AcIr:
                                Stan.HdlDeviceStatuPushLogic.Current.UpdateDeviceStatu(updateTemp.sid, updateTemp.status);
                                if (localFunction != null)
                                {
@@ -1041,9 +1081,10 @@
                                            break;
                                    }
                                    localFunction.lastState += " " + localFunction.GetAttrState(FunctionAttributeKey.SetTemp) + new AC().GetTempUnitString(localFunction);
                                    ACPage.UpdataStates(localFunction);
                                }
                                break;
                            case SPK.HvacFloorHeat:
                            case SPK.FloorHeatStandard:
                                localFunction.lastState = "";
                                switch (localFunction.GetAttrState(FunctionAttributeKey.Mode))
@@ -1072,6 +1113,7 @@
                            case SPK.SensorTVOC:
                            case SPK.SensorTemperature:
                            case SPK.SensorHumidity:
                            case SPK.SensorHcho:
                                if(localFunction.spk == SPK.SensorTemperature)
                                {
                                    HomePage.LoadEvent_RefreshEnvirIndoorTemp();
@@ -1098,22 +1140,29 @@
                                //A_EnvironmentalDataCenter.LoadEvent_UpdataStatus(localFunction);
                                break;
                            case SPK.ElectricSocket:
                            case SPK.PanelSocket:
                                SocketPage.UpdataState(localFunction);
                                break;
                            case SPK.ElectricTV:
                                break;
                            case SPK.ElectricTuyaAirCleaner:
                            case SPK.ElectricTuyaAirCleaner2:
                            case SPK.ElectricTuyaFan:
                            case SPK.ElectricTuyaFan2:
                            case SPK.ElectricTuyaWeepRobot:
                            case SPK.ElectricTuyaWeepRobot2:
                            case SPK.ElectricTuyaWaterValve:
                            case SPK.ElectricTuyaWaterValve2:
                            case SPK.SensorPir:
                            case SPK.SensorDoorWindow:
                            case SPK.SensorSmoke:
                            case SPK.SensorWater:
                            case SPK.ClothesHanger:
                            case SPK.AcIr:
                            case SPK.SenesorMegahealth:
                            case SPK.SenesorMegahealth2:
                            case SPK.AirFreshStandard:
                            case SPK.HvacAirFresh:
                            case SPK.SensorGas:
                                //设备状态推送
                                //状态更新
                                Stan.HdlDeviceStatuPushLogic.Current.UpdateDeviceStatu(updateTemp.sid, updateTemp.status);