gxc
2020-02-28 66a9965c44ecc32a6696abca876ab9d1cd091584
ZigbeeApp/Shared/Phone/ZigBee/Device/ZbGateway.cs
@@ -60,6 +60,12 @@
        /// <value><c>true</c> if is remote; otherwise, <c>false</c>.</value>
        public static bool IsRemote;
        /// <summary>
        /// 是否拥有远程连接的权限
        /// </summary>
        [Newtonsoft.Json.JsonIgnore]
        public static bool AllowRemoteCtrl = true;
        /// <summary>
        /// 网关保存路径
        /// </summary>
@@ -95,7 +101,7 @@
            }
        }
        #region variable
        #region variable
        /// <summary>
        /// 远程连接成功时的时间
        /// </summary>
@@ -218,6 +224,7 @@
        /// <para>第一个参数:如果为 DDevice/IsGetEpointInfo:有新设备加入zigbee网络反馈</para>设备请求APP获取升级数据
        /// <para>第一个参数:如果为 Device/DeviceJoinZbNet:获取新设备所有端点信息是否成功反馈</para>
        /// <para>第一个参数:如果为 DeviceRequestAcUpdateData: 设备请求空调发升级数据</para>
        /// "已经通知");//已经通知");
        /// </summary>
        [Newtonsoft.Json.JsonIgnore]
        public Action<string, object> ReportAction;
@@ -3229,6 +3236,11 @@
        /// <returns></returns>
        static async System.Threading.Tasks.Task initGateWayBaseInfomation()
        {
            if (AllowRemoteCtrl == false)
            {
                //没有远程连接的权限
                return;
            }
            string loginToken = Config.Instance.Token;
            if (Config.Instance.Home.IsOthreShare == true)
            {
@@ -3271,11 +3283,13 @@
                    var statuCode = jobject["StateCode"].ToString();
                    if (statuCode != "Success")
                    {
                        //Log出力
                        Shared.Phone.UserCenter.HdlLogLogic.Current.WriteLog(-1, "接口访问失败★:App/GetSingleHomeGatewayPagger " + statuCode);
                        //序列化对象
                        var requestJson = Newtonsoft.Json.JsonConvert.SerializeObject(pra2);
                        Shared.Phone.UserCenter.HdlLogLogic.Current.WriteLog(-1, "参数:\r\n" + requestJson);
                        //Log出力
                        string errorMsg = "接口访问失败★:App/GetSingleHomeGatewayPagger " + statuCode + "\r\n";
                        errorMsg += "参数:\r\n" + requestJson;
                        Shared.Phone.UserCenter.HdlLogLogic.Current.WriteLog(-1, errorMsg);
                        return;
                    }
                    var responseData = jobject["ResponseData"];
@@ -3311,7 +3325,7 @@
        /// 远程MqttClient
        /// </summary>
        public static IMqttClient RemoteMqttClient= new MqttFactory().CreateMqttClient();
        static bool remoteIsConnected;
        static bool remoteIsConnected;
        private int IsLogin = 0;
        [Newtonsoft.Json.JsonIgnore]
        static Action actionTemp;
@@ -3323,10 +3337,12 @@
        /// <param name="brokerName">Broker name.</param>
        public static async System.Threading.Tasks.Task StartRemoteMqtt()
        {
            if (remoteMqttIsConnecting
                       || !Shared.Common.Config.Instance.IsLogin
                       || Shared.Common.Config.Instance.HomeId == ""
                       || remoteIsConnected)
            //追加:没有远程连接的权限
            if (AllowRemoteCtrl == false
               || remoteMqttIsConnecting
               || !Shared.Common.Config.Instance.IsLogin
               || Shared.Common.Config.Instance.HomeId == ""
               || remoteIsConnected)
            {
                return;
            }
@@ -3367,6 +3383,18 @@
                            RemoteMqttClient.UseConnectedHandler(async (e) =>
                            {
                                DebugPrintLog($"远程连接成功");
                                if (Config.Instance.Home.IsOthreShare == true)
                                {
                                    //订阅一个成员被删除的主题
                                    string myGuid = Config.Instance.Guid;
                                    await RemoteMqttClient.SubscribeAsync("/ZigbeeGateWayToClient/" + myGuid + "/Push/Deleted");
                                    //订阅一个分享数据已经变更的主题
                                    await RemoteMqttClient.SubscribeAsync("/ZigbeeGateWayToClient/" + myGuid + "/Push/DeletedShareData");
                                }
                                //订阅一个挤下线的主题
                                await RemoteMqttClient.SubscribeAsync("/ZigbeeGateWayToClient/" + Config.Instance.ConnEmqClientId + "/Push/NotifySqueeze");
                                await initGateWayBaseInfomation();
                                Shared.Phone.UserCenter.HdlGatewayLogic.Current.CheckGatewayByConnectChanged(Shared.Phone.UserCenter.GatewayConnectMode.Remote);
                                //没有主网关时主动读取,获取主网关信息
@@ -3444,6 +3472,12 @@
                        if (result != null)
                        {
                            var jobject = Newtonsoft.Json.Linq.JObject.Parse(Encoding.UTF8.GetString(result));
                            if (jobject["StateCode"].ToString() == "NotAllowRemoteCtrl")
                            {
                                //没有远程连接的权限
                                AllowRemoteCtrl = false;
                                return;
                            }
                            var responseData = jobject["ResponseData"];
                            if (responseData != null)
                            {
@@ -3451,6 +3485,8 @@
                                var connEmqClientId = responseData["ConnEmqClientId"]?.ToString();
                                var connEmqUserName = responseData["ConnEmqUserName"]?.ToString();
                                var connEmqPwd = responseData["ConnEmqPwd"]?.ToString();
                                //记录起当前的客户端ID
                                Config.Instance.ConnEmqClientId = connEmqClientId;
                                var connEmqDomainPorts = connEmqDomainPort.Replace("//", "").Split(':');
                                var domain = connEmqDomainPorts[1];
@@ -3496,7 +3532,8 @@
                {
                    localIsConnected = false;
                    DebugPrintLog($"Local主动断开_{s}");
                    await localMqttClient.DisconnectAsync(new MQTTnet.Client.Disconnecting.MqttClientDisconnectOptions {  }, CancellationToken.None);
                    //await localMqttClient.DisconnectAsync(new MQTTnet.Client.Disconnecting.MqttClientDisconnectOptions {  }, CancellationToken.None);
                    await localMqttClient.DisconnectAsync();
                }
            }
            catch (Exception ex)
@@ -3516,10 +3553,11 @@
                {
                    remoteIsConnected = false;
                    DebugPrintLog($"Remote主动断开_{s}");
                    await RemoteMqttClient.DisconnectAsync(new MQTTnet.Client.Disconnecting.MqttClientDisconnectOptions { }, CancellationToken.None);
                    //await RemoteMqttClient.DisconnectAsync(new MQTTnet.Client.Disconnecting.MqttClientDisconnectOptions { }, CancellationToken.None);
                    await RemoteMqttClient.DisconnectAsync();
                }
            }
            catch(Exception e) {
            catch(Exception e) {
                DebugPrintLog($"Remote断开通讯连接出异常:{e.Message}");
            }
        }
@@ -3851,7 +3889,10 @@
                    epoint = topic.Split('/')[3];
                    cluID = topic.Split('/')[4];
                    attrId = topic.Split('/')[5];
                }
                }
                //全局接收网关推送的的逻辑(为了执行速度,尽可能的别加耗时的操作)
                Shared.Phone.UserCenter.HdlGatewayReceiveLogic.Current.GatewayOverallMsgReceive(gatewayID, topic, reportStatus, message);
                var gwa = GateWayList.Find(obj => obj.getGatewayBaseInfo.gwID == gatewayID);
                if (gwa == null)
@@ -3883,9 +3924,6 @@
                {
                    jobject = Newtonsoft.Json.Linq.JObject.Parse(message);
                }
                //全局接收网关推送的的逻辑(为了执行速度,尽可能的别加耗时的操作)
                Shared.Phone.UserCenter.HdlGatewayReceiveLogic.Current.GatewayOverallMsgReceive(gatewayID, topic, reportStatus, jobject);
                #region 远程,主网关上报通知
                if (IsRemote)
@@ -4329,22 +4367,18 @@
                #region 设备请求APP获取升级数据
                else if (topic == gatewayID + "/" + "ZbDataPassthrough")
                {
                    var gatewayTemp = new ZbGateway() { DataID = jobject.Value<int>("Data_ID") };
                    gatewayTemp.clientDataPassthroughResponseData = Newtonsoft.Json.JsonConvert.DeserializeObject<ClientDataPassthroughResponseData>(jobject["Data"].ToString());
                    if (gatewayTemp.clientDataPassthroughResponseData == null)
                    {
                        return;
                    }
                    //上报类型通知
                    if (gwa.ReportAction != null)
                    {
                        DebugPrintLog("DeviceRequestAcUpdateData");
                        gwa.ReportAction("DeviceRequestAcUpdateData", gatewayTemp.clientDataPassthroughResponseData);
                    {
                        var clientDataPassthrough = Newtonsoft.Json.JsonConvert.DeserializeObject<ClientDataPassthroughResponseData>(jobject["Data"].ToString());
                        if (clientDataPassthrough != null)
                        {
                            DebugPrintLog("DeviceRequestAcUpdateData");
                            gwa.ReportAction("DeviceRequestAcUpdateData", clientDataPassthrough);
                        }
                    }
                }
                #endregion
                DebugPrintLog("网关返回数据通知");
            }
            catch (Exception ex)
            {