HDL Home App 第二版本 旧平台金堂用 正在使用
黄学彪
2019-09-30 404cdc88627f942df7944af04ee05b9d527752d6
ZigbeeApp/Shared/Phone/ZigBee/Device/ZbGateway.cs
old mode 100644 new mode 100755
@@ -106,7 +106,7 @@
        /// <summary>
        /// 同步到远程的当前时间戳
        /// </summary>
        /// </summary>Re
        public static ulong CurrentTimeStamp
        {
            get
@@ -2513,7 +2513,8 @@
                    }
                }
                catch { }
                finally{
                finally
                {
                    Actions -= action;
                }
                DebugPrintLog("Security/ChangeAdminPassword_Actions 退出" + System.DateTime.Now.ToString());
@@ -3127,20 +3128,20 @@
                                 DebugPrintLog($" 本地连接成功_网关IP:{brokerName}_网关是否加:{IsEncry}_当前密码:{Password}_{System.DateTime.Now.ToString()}");
#endif
                                 //if (PubKey != null)
                                 //{
                                 //    var rsaString = ZigBee.Common.SecuritySet.RSAEncrypt(PubKey, Password);
                                 //    var resultVerityfy = await SendAesKeyAsync(rsaString);
                                 //    if (resultVerityfy == null)
                                 //    {
                                 //        resultVerityfy = await SendAesKeyAsync(rsaString);
                                 //    }
                                 //    if (resultVerityfy != null && resultVerityfy.Result == 0)
                                 //    {
                                 //        IsEncry = true;
                                 //    }
                                 //}
                                 Shared.Common.LocalGateway.Current.CheckGatewayByConnectChanged(Shared.Phone.UserCenter.GatewayConnectMode.WIFI);
                                if (PubKey != null)
                                {
                                    var rsaString = ZigBee.Common.SecuritySet.RSAEncrypt(PubKey, Password);
                                    var resultVerityfy = await SendAesKeyAsync(rsaString);
                                    if (resultVerityfy == null)
                                    {
                                        resultVerityfy = await SendAesKeyAsync(rsaString);
                                    }
                                    if (resultVerityfy != null && resultVerityfy.Result == 0)
                                    {
                                        IsEncry = true;
                                    }
                                }
                                Shared.Phone.UserCenter.HdlGatewayLogic.Current.CheckGatewayByConnectChanged(Shared.Phone.UserCenter.GatewayConnectMode.WIFI);
                                 System.Console.WriteLine($"当前是网关Wi-Fi在通讯_{System.DateTime.Now.ToString()}");
                             };
@@ -3204,7 +3205,7 @@
            {
                try
                {
                    if (remoteMqttIsConnecting)
                    if (remoteMqttIsConnecting || Shared.Common.Config.Instance.HomeId == "")
                    {
                        return;
                    }
@@ -3213,6 +3214,7 @@
                    var url = Shared.Common.Config.Instance.ZigbeeMqttBrokerLoadSubDomain;//.Replace("6688","6689");
                    if (string.IsNullOrEmpty(url) || !url.StartsWith("tcp://") || url.Split(':').Length != 3)
                    {
                        remoteMqttIsConnecting = false;
                        return;
                    }
                    if (RemoteMqttClient == null)
@@ -3239,7 +3241,7 @@
#if DEBUG
                             DebugPrintLog($"远程连接成功_{System.DateTime.Now.ToString()}");
#endif
                             Shared.Common.LocalGateway.Current.CheckGatewayByConnectChanged(Shared.Phone.UserCenter.GatewayConnectMode.Remote);
                            Shared.Phone.UserCenter.HdlGatewayLogic.Current.CheckGatewayByConnectChanged(Shared.Phone.UserCenter.GatewayConnectMode.Remote);
                             //没有主网关时主动读取,获取主网关信息
                             var gateWayList = GateWayList.FindAll(obj => obj.getGatewayBaseInfo.HomeId == Shared.Common.Config.Instance.HomeId);
                             if (gateWayList.Find(obj => obj.getGatewayBaseInfo.IsMainGateWay == true) == null)
@@ -3314,7 +3316,9 @@
                        KeepAlivePeriod = new TimeSpan(0, 5, 0),//设置心跳时间(最大值,2^16-1 = 65535秒 = 18小时。最小值可以为0,表示客户端不断开。一般设为几分钟,比如微信心跳周期为300秒。)
                        DefaultCommunicationTimeout = new TimeSpan(0, 0, 100),//设置超时时间
                    };
                    //远程通讯连接,连接云端服务器
                    await RemoteMqttClient.ConnectAsync(connectCloudMqttClientOptions);
                }
                catch (Exception ex)
                {
@@ -3424,12 +3428,18 @@
        {
            try
            {
                if (Shared.Common.Config.Instance.HomeId == "")
                {
                    return;
                }
                if (IsRemote)
                {
#if DEBUG
                    DebugPrintLog($"远程 ——发送到网关的主题:{topic}_发送到网关的数据:{System.Text.Encoding.UTF8.GetString(message)}_当前网关{CurrentGateWayId}_{System.DateTime.Now.ToString()}");
#endif
                    await SendRemoteMsg(topic, message, qosLevel, retain);
#if DEBUG
                    DebugPrintLog($"远程 ——发送到网关的主题:{topic}_发送到网关的数据:{System.Text.Encoding.UTF8.GetString(message)}_当前网关{CurrentGateWayId}_{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:fff")}");//{System.DateTime.Now.ToString()}");// DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:fff")
#endif
                }
                else
                {
@@ -3478,7 +3488,8 @@
        /// <param name="retain"></param>
        public async System.Threading.Tasks.Task Send(string topic, string message, MQTTnet.Core.Protocol.MqttQualityOfServiceLevel qosLevel = MQTTnet.Core.Protocol.MqttQualityOfServiceLevel.ExactlyOnce, bool retain = false)
        {
            if(string.IsNullOrEmpty(message)){
            if (string.IsNullOrEmpty(message))
            {
                return;
            }
            await Send(topic, System.Text.Encoding.UTF8.GetBytes(message), qosLevel, retain);
@@ -3509,6 +3520,15 @@
                {
                    message = System.Text.Encoding.UTF8.GetString(e.ApplicationMessage.Payload);
                }
                else if (topic == "BeingSqueezedOffline")
                {
                    //云端中的当前连接被挤下线,需要通知app
                    //全局接收网关推送的的逻辑(为了执行速度,尽可能的别加耗时的操作)
                    message = "BeingSqueezedOffline";
                    Shared.Phone.UserCenter.HdlGatewayReceiveLogic.GatewayOverallMsgReceive("", topic, null);
                    DebugPrintLog($"被挤下线通知:{ topic}_远程返回的数据_{message}_{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:fff")}");//{System.DateTime.Now.ToString()}");// DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:fff")
                    return;
                }
                //重新登录:帐号或者共享连接中的[子帐号分布式Id$Share$分布式住宅Id=XXX尚未登录,请重新登录后再连接后再进行发布操作,如果您当前是别人共享给你的连接,则请重新请求获取连接信息一下,再初始化连接再次发布远程控制即可!!
                else if (topic == "AppNoLogin")
                {
@@ -3526,7 +3546,7 @@
                    }
                }
#if DEBUG
                DebugPrintLog($"远程返回的主题:{ topic}_远程返回的数据_{message}_{System.DateTime.Now.ToString()}");
                DebugPrintLog($"远程返回的主题:{ topic}_远程返回的数据_{message}_{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:fff")}");//{System.DateTime.Now.ToString()}");// DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:fff")
#endif
                var cloudMqttResult = Newtonsoft.Json.JsonConvert.DeserializeObject<ZigBee.Common.CloudMqttResponsePack>(message);
                if (cloudMqttResult != null)
@@ -3536,7 +3556,7 @@
                        RemoteTimeStamp = ulong.Parse(cloudMqttResult.CloudTimestamp);
                        LoginRemoteDateTime = System.DateTime.Now;
#if DEBUG
                        DebugPrintLog($"远程返回主题:{topic}_当前时间戳:_+{CurrentTimeStamp} + _+ {System.DateTime.Now.ToString()}");
                        DebugPrintLog($"远程返回主题:{topic}_当前时间戳:_+{CurrentTimeStamp} + _+{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:fff")}");//{System.DateTime.Now.ToString()}");// DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:fff"   // {System.DateTime.Now.ToString()}");
#endif
                        return;
                    }
@@ -3598,7 +3618,6 @@
            {
                var topic = e.ApplicationMessage.Topic;
                var message = System.Text.Encoding.UTF8.GetString(e.ApplicationMessage.Payload);
               // DebugPrintLog($"网关返回的主题(秘文):{topic}_网关返回的负载(秘文):{message}_{System.DateTime.Now.ToString()}");
                if (IsEncry)
                {
@@ -3647,10 +3666,11 @@
            {
                if(string .IsNullOrEmpty(message))
                {
                    return;
                    message = "{}";
                }
                var gatewayID = topic.Split('/')[0];//网关返回的网关ID
                var reportStatus = topic.Split('/')[1];//主题为设备上报的主题
                var reportStatus = "";
                reportStatus = topic.Split('/')[1];//主题为设备上报的主题
                string addr = "";//上报的设备addr
                string epoint = "";//上报的设备epoint
                string cluID = "";//上报的设备cluID
@@ -3677,6 +3697,14 @@
                if (gwa.GwResDataAction != null)
                {
                    gwa.GwResDataAction(topic, message);
                    if (topic == $"{gatewayID}/DoorLock/DoorLockOperatingEventNotificationCommand")
                    {
                        Console.WriteLine($"111_DoorLockOperatingEventNotificationCommand:{message}");
                    }
                    if (topic == $"{gatewayID}/DoorLock/DoorLockProgrammingEventNotificationCommand")
                    {
                        Console.WriteLine($"111_DoorLockProgrammingEventNotificationCommand:{message}");
                    }
                }
                gwa.CurrentGateWayId = gatewayID;
@@ -3700,6 +3728,9 @@
                    jobject = Newtonsoft.Json.Linq.JObject.Parse(message);
                }
                //全局接收网关推送的的逻辑(为了执行速度,尽可能的别加耗时的操作)
                Shared.Phone.UserCenter.HdlGatewayReceiveLogic.GatewayOverallMsgReceive(gatewayID, topic, jobject);
                #region 云端通知
                var cloudMqttResult = Newtonsoft.Json.JsonConvert.DeserializeObject<ZigBee.Common.CloudMqttResponsePack>(message);
                if (cloudMqttResult != null)
@@ -3712,6 +3743,8 @@
                                DebugPrintLog("AppNoLogin已经通知");
                                gwa.CloudErrorAction("AppNoLogin", "登录过期,请重新登录");
                            }
                            //全局接收网关推送的的逻辑(为了执行速度,尽可能的别加耗时的操作)
                            Shared.Phone.UserCenter.HdlGatewayReceiveLogic.GatewayOverallMsgReceive(gatewayID, "AppNoLogin", jobject);
                            break;
                        case "AppTimeOut":
                            if (gwa.CloudErrorAction != null)
@@ -3896,6 +3929,17 @@
                                gwa.ReportAction("DeviceInComingRespon", transverter.getNewDeviceInfo);
                            }
                            UpdateDeviceInfo(transverter, "DeviceInComingRespon");
                            UpdateDeviceStatus(gwa);
                            break;
                        case DeviceType.DoorLock:
                            var doorLock = new DoorLock() { DeviceID = jobject.Value<int>("Device_ID"), DeviceAddr = jobject.Value<string>("DeviceAddr"), DeviceEpoint = jobject.Value<int>("Epoint"), DataID = jobject.Value<int>("Data_ID"), CurrentGateWayId = gwa.getGatewayBaseInfo.gwID };
                            doorLock.getNewDeviceInfo = Newtonsoft.Json.JsonConvert.DeserializeObject<DoorLock.NewDeviceInfoData>(jobject["Data"].ToString());
                            if (gwa.ReportAction != null)
                            {
                                DebugPrintLog("DeviceInComingRespon已经通知");
                                gwa.ReportAction("DeviceInComingRespon", doorLock.getNewDeviceInfo);
                            }
                            UpdateDeviceInfo(doorLock, "DeviceInComingRespon");
                            UpdateDeviceStatus(gwa);
                            break;
                    }
@@ -4134,6 +4178,31 @@
                                UpdateDeviceStatus(transverterInfo);
                            }
                            break;
                        case DeviceType.DoorLock:
                            var doorLock = new DoorLock() { DeviceID = jobject.Value<int>("Device_ID"), DeviceAddr = jobject.Value<string>("DeviceAddr"), DeviceEpoint = jobject.Value<int>("Epoint"), DataID = jobject.Value<int>("Data_ID"), CurrentGateWayId = gwa.getGatewayBaseInfo.gwID };
                            doorLock.IsOnline = Newtonsoft.Json.JsonConvert.DeserializeObject<int>(jobject["Data"]["IsOnline"].ToString());
                            if (gwa.ReportAction != null)
                            {
                                DebugPrintLog("OnlineStatusChange已经通知");
                                gwa.ReportAction("OnlineStatusChange", doorLock);
                            }
                            var doorLockInfo = gwa.DeviceList.Find(obj => obj.DeviceID == doorLock.DeviceID && obj.DeviceAddr == doorLock.DeviceAddr);
                            if (doorLockInfo == null)
                            {
                                gwa.DeviceList.Add(doorLockInfo);
                                UpdateDeviceInfo(doorLockInfo, "OnlineStatusChange");
                                UpdateDeviceStatus(gwa);
                            }
                            else
                            {
                                if (doorLockInfo.DeviceInfo != null)
                                {
                                    doorLockInfo.DeviceInfo.IsOnline = doorLockInfo.IsOnline;
                                }
                                UpdateDeviceInfo(doorLockInfo, "OnlineStatusChange");
                                UpdateDeviceStatus(doorLockInfo);
                            }
                            break;
                    }
                }
                #endregion
@@ -4343,6 +4412,27 @@
                                                }
                                            }
                                            break;
                                        case DeviceType.DoorLock:
                                            var doorLock = new DoorLock() { DeviceAddr = jobject.Value<string>("DeviceAddr"), DataID = jobject.Value<int>("Data_ID"), CurrentGateWayId = gwa.getGatewayBaseInfo.gwID };
                                            doorLock.removeDeviceResponseData = Newtonsoft.Json.JsonConvert.DeserializeObject<DoorLock.RemoveDeviceResponseData>(jobject["Data"].ToString());
                                            if (doorLock.removeDeviceResponseData == null)
                                            {
                                                return;
                                            }
                                            else
                                            {
                                                UpdateDeviceStatus(doorLock);
                                                UpdateDeviceInfo(doorLock, "RemoveDeviceRespon");
                                                if (doorLock.removeDeviceResponseData.Result == 0)
                                                {
                                                    var info = gwa.DeviceList.Find((CommonDevice obj) => obj.DeviceID == doorLock.DeviceID && obj.DeviceAddr == doorLock.DeviceAddr && obj.DeviceEpoint == doorLock.DeviceEpoint);
                                                    if (info != null)
                                                    {
                                                        gwa.DeviceList.Remove(info);
                                                    }
                                                }
                                            }
                                            break;
                                    }
                                }
                            }
@@ -4418,10 +4508,16 @@
                            break;
                        case DeviceType.Transverter :
                            var transverter = new Transverter { DeviceID = deviceID, DeviceAddr = deviceAddr, DeviceEpoint = tempEpoint, DataID = dataId };
                            transverter.DeviceStatusReport = Newtonsoft.Json.JsonConvert.DeserializeObject<AC.DeviceStatusReportData>(jobject["Data"].ToString());
                            transverter.DeviceStatusReport = Newtonsoft.Json.JsonConvert.DeserializeObject<Transverter.DeviceStatusReportData>(jobject["Data"].ToString());
                            UpdateDeviceStatus(transverter);
                            UpdateDeviceInfo(transverter, "DeviceStatusReport");
                            break;
                        case DeviceType.DoorLock:
                            var doorLock = new DoorLock { DeviceID = deviceID, DeviceAddr = deviceAddr, DeviceEpoint = tempEpoint, DataID = dataId };
                            doorLock.DeviceStatusReport = Newtonsoft.Json.JsonConvert.DeserializeObject<DoorLock.DeviceStatusReportData>(jobject["Data"].ToString());
                            UpdateDeviceStatus(doorLock);
                            UpdateDeviceInfo(doorLock, "DeviceStatusReport");
                            break;
                    }
                }
                #endregion