黄学彪
2019-11-18 16604a593202f2f87adf71abd57d036fe7da3b52
ZigbeeApp/Shared/Phone/ZigBee/Device/ZbGateway.cs
@@ -221,7 +221,6 @@
        /// <para>第一个参数:如果为 DDevice/IsGetEpointInfo:有新设备加入zigbee网络反馈</para>设备请求APP获取升级数据
        /// <para>第一个参数:如果为 Device/DeviceJoinZbNet:获取新设备所有端点信息是否成功反馈</para>
        /// <para>第一个参数:如果为 DeviceRequestAcUpdateData: 设备请求空调发升级数据</para>
        /// "已经通知");//已经通知");
        /// </summary>
        [Newtonsoft.Json.JsonIgnore]
        public Action<string, object> ReportAction;
@@ -3205,6 +3204,9 @@
            {
                try
                {
                    if (!Shared.Common.Config.Instance.IsLogin) {
                        return;
                    }
                    if (remoteMqttIsConnecting || Shared.Common.Config.Instance.HomeId == "")
                    {
                        return;
@@ -3388,13 +3390,6 @@
        /// </summary>
        async System.Threading.Tasks.Task SendRemoteMsg(string topicName, byte[] message, MQTTnet.Core.Protocol.MqttQualityOfServiceLevel qosLevel = MQTTnet.Core.Protocol.MqttQualityOfServiceLevel.ExactlyOnce, bool retain = false)
        {
            //#region MD5加密 token
            //var result = System.Text.Encoding.UTF8.GetBytes(Shared.Common.Config.Instance.Password);
            //var md5 = new System.Security.Cryptography.MD5CryptoServiceProvider();
            //var output = md5.ComputeHash(result);
            //var md5Password = BitConverter.ToString(output).Replace("-", "").ToUpper();
            //#endregion
            //(1)生成[用户名:密码]
            var userNameAndPwdStr = string.Concat(Shared.Common.Config.Instance.Guid, RemoteOperatorConsts.CAP_NUMBER, Shared.Common.Config.Instance.MD5PWD);
            DebugPrintLog($"userNameAndPwdStr:{userNameAndPwdStr} ");
@@ -3405,11 +3400,10 @@
            //(3)将(2)继续生成格式[Base64(用户名:密码):Timestamp]
            var userNameAndPwdStrBaseSexFourAndTimestamp = string.Concat(userNameAndPwdStrBaseSexFour, RemoteOperatorConsts.CAP_NUMBER, CurrentTimeStamp.ToString());
            ///[Zigbee.Buspro]App/id(帐号Id)/Base64(DES[Base64(用户名:密码):Timestamp ]/网关唯一Id/转发给网关的主题/XX/XX
            //[Zigbee.Buspro]App/id(帐号Id)/Base64(DES[Base64(用户名:密码):Timestamp ]/网关唯一Id/转发给网关的主题/XX/XX
            //(4)生成上报主题,主题模式需要为  /ZigbeeApp/id(主子调试帐号Id)/Base64(DES[Base64(用户名:密码):Timestamp ]/网关唯一Id/topicName "000204022022"   GateWayId  000204022022
            var reportTopic = string.Format("/{0}/{1}/{2}/{3}/{4}", RemoteOperatorConsts.ZIGBEE_APP_STARTS_WITH, Shared.Common.Config.Instance.Guid, userNameAndPwdStrBaseSexFourAndTimestamp, this.getGatewayBaseInfo?.gwID, topicName);
            var aa = System.Text.Encoding.UTF8.GetString(message);
            DebugPrintLog($"AAAA发送到云端主题明文:{reportTopic}___BBBBB发送到云端内容明文:{aa}");
            string topicEncStr = reportTopic;
            //文件流不用加密
@@ -3417,7 +3411,6 @@
            topicEncStr = topicEncStr.Replace("/", "[[$-MQTT_TILT_SYMBOL_REPLACE-$]]").Replace("+", "[[$-MQTT_PLUS_SYMBOL_REPLACE-$]]");
            message = System.Text.Encoding.UTF8.GetBytes(SecuritySet.AesEncrypt(message, Shared.Common.Config.Instance.MqttKey));
            var bb = System.Text.Encoding.UTF8.GetString(message);
            DebugPrintLog($"CCCC发送到云端主题秘文:{topicEncStr}___DDDD发送到云端内容秘文:{bb}");
            //(6)构建Mqtt需要发布的数据包,发布给云端的MqttBroker
            await RemoteMqttClient.PublishAsync(new MQTTnet.Core.MqttApplicationMessage(topicEncStr, message, qosLevel, retain));
@@ -3532,7 +3525,7 @@
                    //云端中的当前连接被挤下线,需要通知app
                    //全局接收网关推送的的逻辑(为了执行速度,尽可能的别加耗时的操作)
                    message = "BeingSqueezedOffline";
                    Shared.Phone.UserCenter.HdlGatewayReceiveLogic.GatewayOverallMsgReceive("", topic, null);
                    Shared.Phone.UserCenter.HdlGatewayReceiveLogic.Current.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;
                }
@@ -3728,7 +3721,7 @@
                }
                //全局接收网关推送的的逻辑(为了执行速度,尽可能的别加耗时的操作)
                Shared.Phone.UserCenter.HdlGatewayReceiveLogic.GatewayOverallMsgReceive(gatewayID, topic, jobject);
                Shared.Phone.UserCenter.HdlGatewayReceiveLogic.Current.GatewayOverallMsgReceive(gatewayID, topic, reportStatus, jobject);
                #region 云端通知
                var cloudMqttResult = Newtonsoft.Json.JsonConvert.DeserializeObject<ZigBee.Common.CloudMqttResponsePack>(message);
@@ -3743,7 +3736,7 @@
                                gwa.CloudErrorAction("AppNoLogin", "登录过期,请重新登录");
                            }
                            //全局接收网关推送的的逻辑(为了执行速度,尽可能的别加耗时的操作)
                            Shared.Phone.UserCenter.HdlGatewayReceiveLogic.GatewayOverallMsgReceive(gatewayID, "AppNoLogin", jobject);
                            Shared.Phone.UserCenter.HdlGatewayReceiveLogic.Current.GatewayOverallMsgReceive(gatewayID, "AppNoLogin", reportStatus, jobject);
                            break;
                        case "AppTimeOut":
                            if (gwa.CloudErrorAction != null)
@@ -3771,17 +3764,15 @@
                        if (gwData != null)
                        {
                            var gwList = GateWayList.FindAll(obj => obj.getGatewayBaseInfo.HomeId == Shared.Common.Config.Instance.HomeId);
                            for (int i = 0; i < gwList.Count; i++)
                            {
                                var gwTemp = gwList[i];
                                if (gwData.IsDominant == 1)
                                {
                                    gwTemp.getGatewayBaseInfo.IsMainGateWay = true;
                                    for (int j = i + 1; j < gwList.Count; j++)
                                    {
                                        gwList[j].getGatewayBaseInfo.IsMainGateWay = false;
                                    }
                                if (gwList[i].getGatewayBaseInfo.gwID == gatewayID)
                                {
                                    gwList[i].getGatewayBaseInfo.IsMainGateWay = true;
                                }
                                else
                                {
                                    gwList[i].getGatewayBaseInfo.IsMainGateWay = false;
                                }
                            }
                        }
@@ -3922,6 +3913,54 @@
                    UpdateDeviceInfo(tempDevice, "DeviceStatusReport");
                }
                #endregion
                #region 门锁操作事件通知
                else if (topic == gatewayID + "/" + "DoorLock/DoorLockOperatingEventNotificationCommand")
                {
                    var deviceID = jobject.Value<int>("Device_ID");
                    switch ((DeviceType)(deviceID))
                    {
                        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 };
                            var OperatingEventNotificationDatad = Newtonsoft.Json.JsonConvert.DeserializeObject<ZigBee.Device.DoorLock.DoorLockOperatingEventNotificationCommand>(jobject["Data"].ToString());
                            if (OperatingEventNotificationDatad != null)
                            {
                                doorLock.doorLockOperatingEventNotificationCommand = OperatingEventNotificationDatad;
                            }
                            if (gwa.ReportAction != null)
                            {
                                DebugPrintLog("DoorLockProgrammingEventNotificationCommand已经通知");
                                gwa.ReportAction("DoorLockProgrammingEventNotificationCommand", doorLock);
                            }
                            UpdateDeviceStatus(doorLock);
                            UpdateDeviceInfo(doorLock, "DoorLockProgrammingEventNotificationCommand");
                            break;
                    }
                }
                #endregion
                #region 门锁编程事件通知
                else if (topic == gatewayID + "/" + "DoorLock/DoorLockProgrammingEventNotificationCommand")
                {
                    var deviceID = jobject.Value<int>("Device_ID");
                    switch ((DeviceType)(deviceID))
                    {
                        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 };
                            var ProgrammingEventNotificationData = Newtonsoft.Json.JsonConvert.DeserializeObject<ZigBee.Device.DoorLock.DoorLockProgrammingEventNotificationCommand>(jobject["Data"].ToString());
                            if (ProgrammingEventNotificationData != null)
                            {
                                doorLock.doorLockProgrammingEventNotificationCommand = ProgrammingEventNotificationData;
                            }
                            if (gwa.ReportAction != null)
                            {
                                DebugPrintLog("DoorLockProgrammingEventNotificationCommand已经通知");
                                gwa.ReportAction("DoorLockProgrammingEventNotificationCommand", doorLock);
                            }
                            UpdateDeviceStatus(doorLock);
                            UpdateDeviceInfo(doorLock, "DoorLockProgrammingEventNotificationCommand");
                            break;
                    }
                }
                #endregion
                #region IAS安防信息上报
                else if (topic == gatewayID + "/" + "IASInfoReport")
                {