wei
2020-12-22 d07d62be7111d78b2aaa44c9cdee23a7d22b4dda
HDL_ON/DAL/DriverLayer/Control.cs
@@ -25,6 +25,8 @@
    public class Control
    {
        static Control _control;
        public static Control Ins
        {
@@ -58,6 +60,14 @@
            return t.ToString();
        }
        /// <summary>
        /// 是否搜索本地网关成功
        /// </summary>
        public bool IsSearchLocalGatewaySuccessful = false;
        /// <summary>
        ///
        /// </summary>
        bool _gatewayOnline = false;
        /// <summary>
        /// 网关是否在线
@@ -168,14 +178,6 @@
            }
        }
        ///// <summary>
        ///// 改变通讯方式
        ///// </summary>
        //public void ChangeCommunicationMode(CommunicationMode communicationMode)
        //{
        //    this.communicationMode = communicationMode;
        //}
        /// <summary>
        /// 搜索本地网关列表
        /// </summary>
@@ -183,31 +185,55 @@
        {
            MainPage.Log($"搜索本地网关列表,网关类型:{DB_ResidenceData.Instance.GatewayType}");
            if (MainPage.InternetStatus == 0)
                return;
            OpenUdp(DB_ResidenceData.Instance.GatewayType == 0 ? 6000 : 8585);
            new System.Threading.Thread(() =>
            {
                for (int i = 0; i < 5; i++)
                {
                    if (GatewayOnline)
                        break;
                    if (DB_ResidenceData.Instance.GatewayType == 0)
                    {
                        new Control_Udp().ControlBytesSend(Command.ReadGateway, 255, 255, new byte[] { (byte)new Random().Next(255), (byte)new Random().Next(255) });
                    }
                    else if (DB_ResidenceData.Instance.GatewayType == 1)
                    {
                        new Control_Udp().SearchLocalGateway();
                    }
                    System.Threading.Thread.Sleep(500);
                }
                if (!GatewayOnline)//网关不在线的时候才可尝试远程连接
                Ins.GatewayOnline = false;
                return;
            }
            else if (MainPage.InternetStatus == 1)
            {
                if (!Ins.IsRemote)
                {
                    Ins.IsRemote = true;
                    DAL.Mqtt.MqttClient.InitState();
                }
            })
            { IsBackground = true }.Start();
            }
            else if (MainPage.InternetStatus == 2)
            {
                OpenUdp(DB_ResidenceData.Instance.GatewayType == 0 ? 6000 : 8585);
                //重置搜索网关标志
                IsSearchLocalGatewaySuccessful = false;
                new System.Threading.Thread(() =>
                {
                    for (int i = 0; i < 5; i++)
                    {
                        if (IsSearchLocalGatewaySuccessful)
                            break;
                        if (DB_ResidenceData.Instance.GatewayType == 0)
                        {
                            new Control_Udp().ControlBytesSend(Command.ReadGateway, 255, 255, new byte[] { (byte)new Random().Next(255), (byte)new Random().Next(255) });
                        }
                        else if (DB_ResidenceData.Instance.GatewayType == 1)
                        {
                            new Control_Udp().SearchLocalGateway();
                        }
                        System.Threading.Thread.Sleep(500);
                    }
                    if (!IsSearchLocalGatewaySuccessful)
                    {
                        //本地搜索不到网关
                        if (!Ins.IsRemote)
                        {
                            //连接mqtt前,先断开--2020-12-21
                            DAL.Mqtt.MqttClient.DisConnectRemote();
                            Ins.IsRemote = true;
                            DAL.Mqtt.MqttClient.InitState();
                        }
                    }
                })
                { IsBackground = true }.Start();
            }
        }
        /// <summary>
@@ -240,6 +266,28 @@
        {
            function.usageCount++;
            function.refreshTime = DateTime.Now;
            //如果是控制调光的开时,亮度值不能为0
            if (commandDictionary.Count > 2)
            {
                if (commandDictionary.ContainsKey(FunctionAttributeKey.OnOff) && commandDictionary.ContainsKey(FunctionAttributeKey.Brightness))
                {
                    if (commandDictionary[FunctionAttributeKey.OnOff] == "on")
                    {
                        if (commandDictionary[FunctionAttributeKey.Brightness] == "0")
                        {
                            commandDictionary[FunctionAttributeKey.Brightness] = "100";
                        }
                    }
                    if (commandDictionary[FunctionAttributeKey.OnOff] == "off")
                    {
                        if (commandDictionary[FunctionAttributeKey.Brightness] != "0")
                        {
                            commandDictionary[FunctionAttributeKey.Brightness] = "0";
                        }
                    }
                }
            }
            //远程通讯
            if (Ins.IsRemote)
@@ -274,6 +322,7 @@
                    var functionControlDataJson = Newtonsoft.Json.JsonConvert.SerializeObject(functionControlDataObj);
                    var sendBytes = Ins.ConvertSendBodyData(CommunicationTopic.ct.ControlFunctionTopic, functionControlDataJson);
                    new Control_Udp().SendLocalHdlLinkData(sendBytes, functionControlDataObj.id);
                    MainPage.Log($"发送HDL-Link数据:{functionControlDataJson}");
                }
            }
        }
@@ -325,6 +374,7 @@
                    };
                    var functionControlDataJson = Newtonsoft.Json.JsonConvert.SerializeObject(readDataObj);
                    var sendBytes = Ins.ConvertSendBodyData(CommunicationTopic.ct.ReadStatus, functionControlDataJson);
                    MainPage.Log($"发送HDL-Link数据:{functionControlDataJson}");
                    new Control_Udp().SendLocalHdlLinkData(sendBytes, readDataObj.id);
                }
            }
@@ -385,7 +435,7 @@
            string sendDataString = topicString + lengthString + bodyDataString;
            byte[] sendDataBytes = Encoding.ASCII.GetBytes(sendDataString);
            MainPage.Log($"转换HDL-Link数据\r\n{sendDataString}\r\n");
            //MainPage.Log($"转换HDL-Link数据\r\n{sendDataString}\r\n");
            return sendDataBytes;
        }
@@ -425,6 +475,10 @@
                            break;
                    }
                }
                MainPage.Log($"time 1 :{receiveObj.Topic}");
                //验证有效数据长度
                //if (res[1].Length != receiveObj.Length)
                //{
@@ -443,6 +497,8 @@
                    var device = Newtonsoft.Json.JsonConvert.DeserializeObject<DeviceModule>(bodyJObj.objects.ToString());
                    if (device.device_mac == DB_ResidenceData.Instance.residenceGatewayMAC)
                    {
                        //本地搜索网关成功
                        Control.Ins.IsSearchLocalGatewaySuccessful = true;
                        Ins.GatewayOnline = true;
                        if (!string.IsNullOrEmpty(device.gatewayId))
                        {
@@ -457,6 +513,7 @@
                    }
                }
                else if (receiveObj.Topic == CommunicationTopic.ct.ReadStatus + "_reply" ||
                    receiveObj.Topic == CommunicationTopic.ct.ControlFunctionTopic + "_reply" ||
                    receiveObj.Topic == CommunicationTopic.ct.GatewayUpStatus)
                {
                    //TODO 暂时不传正确的数据上去,如果后面要优化前面这些代码
@@ -512,7 +569,7 @@
                                                localAttr.curValue = attr.value;
                                            }
                                        }
                                        MainPage.Log($"{localSwitch.trait_on_off.curValue}: ");
                                        //rgb.lastState = Language.StringByID(StringId.Brightness) + " : " + rgb.brightness + "%";
                                        RelayPage.UpdataState(localSwitch);
                                    }
                                    break;
@@ -583,7 +640,7 @@
                                            }
                                        }
                                        lightCCT.lastState = Language.StringByID(StringId.Brightness) + " : " + lightCCT.brightness + "%";
                                        DimmerPage.UpdataStates(lightCCT);
                                        ColorTureLampPage.UpdataStates(lightCCT);
                                    }
                                    break;
                                case SPK.CurtainSwitch: