| | |
| | | /* |
| | | 更新了EMQ连接方式 |
| | | */ |
| | | using System.Collections.Generic; |
| | | using System; |
| | | using MQTTnet.Client; |
| | | using System.Threading.Tasks; |
| | |
| | | using MQTTnet; |
| | | using System.Text; |
| | | using System.Security.Cryptography; |
| | | using System.IO; |
| | | using HDL_ON.DriverLayer; |
| | | using HDL_ON.Entity; |
| | | using HDL_ON.UI; |
| | | |
| | | namespace HDL_ON.DAL.Mqtt |
| | | { |
| | | public static class MqttCommon |
| | | public static class MqttClient |
| | | { |
| | | /// <summary> |
| | | /// 加密通讯KEY |
| | |
| | | /// </summary> |
| | | public static IMqttClient RemoteMqttClient = new MqttFactory().CreateMqttClient(); |
| | | |
| | | |
| | | /// <summary> |
| | | /// 推送标识 |
| | | /// </summary> |
| | | static string PushSignStr = System.DateTime.Now.Ticks.ToString(); |
| | | static string PushSignStr = DateTime.Now.Ticks.ToString(); |
| | | |
| | | /// <summary>
|
| | | /// 断开远程Mqtt的链接
|
| | |
| | | |
| | | if (Control.Ins.IsRemote) |
| | | { |
| | | //不是无网络 |
| | | if (OnAppConfig.Instance.internetStatus != 0) |
| | | { |
| | | Control.Ins.GatewayOnline = false; |
| | | } |
| | | Control.Ins.GatewayOnline = false; |
| | | } |
| | | }
|
| | | } |
| | |
| | | { |
| | | try |
| | | { |
| | | //if (remoteIsConnected) { |
| | | remoteIsConnected = false; |
| | | isSubscribeSuccess = false; |
| | | Utlis.WriteLine($"RemoteStart主动断开_{s}"); |
| | | await RemoteMqttClient.DisconnectAsync(); |
| | | |
| | | //} |
| | | } |
| | | catch (Exception e) |
| | | { |
| | | Utlis.WriteLine($"RemoteStart断开通讯连接出异常:{e.Message}");
|
| | | } |
| | | |
| | | } |
| | | |
| | | |
| | | /// <summary> |
| | | /// 断开mqtt连接 |
| | |
| | | { |
| | | if (reset) |
| | | { |
| | | bNeedStartTip = true; |
| | | bNeedConnectTip = true; |
| | | MqttInfoConfig.Current.IfGetMqttInfoSuccess = false; |
| | | } |
| | | await DisConnectRemoteMqttClient(s); |
| | | } |
| | | |
| | | //static DateTime dateTime = DateTime.MinValue; |
| | | /// <summary> |
| | | /// 外网的MQTT是否正在连接 |
| | | /// </summary> |
| | | public static bool remoteMqttIsConnecting; |
| | | static bool remoteIsConnected; |
| | | |
| | | static MqttCommon() |
| | | static MqttClient() |
| | | { |
| | | InitMqtt(); |
| | | } |
| | |
| | | try |
| | | { |
| | | System.Threading.Thread.Sleep(500); |
| | | //if (!UserInfo.Current.IsLogin) { |
| | | // continue; |
| | | //} |
| | | |
| | | if (!Control.Ins.IsRemote) continue; |
| | | |
| | | //if (BusSocket.IsEnterBackground) continue; |
| | | |
| | | await StartCloudMqtt(); |
| | | await SubscribeTopics(); |
| | |
| | | { |
| | | try |
| | | { |
| | | |
| | | //var topicFilterPush = new TopicFilter { QualityOfServiceLevel = MQTTnet.Protocol.MqttQualityOfServiceLevel.ExactlyOnce, |
| | | // Topic = $"/BusGateWayToClient/{MqttInfoConfig.Instance.CurRemoteMACInfo.clientId}/Push/NotifySqueeze" }; |
| | | |
| | | //2020-05-14 订阅主题质量改为0 |
| | | var topicFilterBusGateWayToClient = new MqttTopicFilter() |
| | | { |
| | | Topic = $"/BusGateWayToClient/{MqttInfoConfig.Current.HomeGatewayInfo.id}/#", |
| | | Topic = $"/BusGateWayToClient/{MqttInfoConfig.Current.HomeGatewayInfo.gatewayId}/#", |
| | | QualityOfServiceLevel = MQTTnet.Protocol.MqttQualityOfServiceLevel.AtMostOnce |
| | | //QualityOfServiceLevel = MQTTnet.Protocol.MqttQualityOfServiceLevel.ExactlyOnce |
| | | }; |
| | |
| | | //QualityOfServiceLevel = MQTTnet.Protocol.MqttQualityOfServiceLevel.ExactlyOnce |
| | | }; |
| | | |
| | | var topicAlinkStatus = new MqttTopicFilter() |
| | | { |
| | | Topic = $"/user/{MqttInfoConfig.Current.HomeGatewayInfo.gatewayId}/app/thing/property/#", |
| | | QualityOfServiceLevel = MQTTnet.Protocol.MqttQualityOfServiceLevel.AtMostOnce |
| | | }; |
| | | |
| | | Utlis.WriteLine("开始订阅!"); |
| | | var result = await RemoteMqttClient.SubscribeAsync(new MqttTopicFilter[] { topicFilterBusGateWayToClient, topicFilterPush2 }); |
| | | var result = await RemoteMqttClient.SubscribeAsync(new MqttTopicFilter[] { topicFilterBusGateWayToClient, topicFilterPush2 , topicAlinkStatus }); |
| | | if (result.Items[0].ResultCode == MQTTnet.Client.Subscribing.MqttClientSubscribeResultCode.GrantedQoS0) |
| | | { |
| | | isSubscribeSuccess = true; |
| | |
| | | MqttRemoteSend(new byte[] { 0 }, 3); |
| | | |
| | | //连接成功后检测是否需要通过远程获取Key |
| | | CheckIfNeedGetLocalPasswordFromRemote(); |
| | | //CheckIfNeedGetLocalPasswordFromRemote(); |
| | | } |
| | | |
| | | |
| | | } |
| | | catch (Exception ex) |
| | | { |
| | |
| | | } |
| | | } |
| | | |
| | | static DateTime mFlagDateTime; |
| | | |
| | | //static readonly object SendLocker = new object (); |
| | | /// <summary> |
| | | /// 启动远程Mqtt |
| | | /// </summary> |
| | |
| | | //(1)当[连接云端的Mqtt成功后]或者[以及后面App通过云端Mqtt转发数据给网关成功后],处理接收到云端数据包响应时在mqttServerClient_ApplicationMessageReceived这个方法处理 |
| | | if (RemoteMqttClient.ApplicationMessageReceivedHandler == null) |
| | | { |
| | | //处理接收到的数据 |
| | | RemoteMqttClient.UseApplicationMessageReceivedHandler((e) => { |
| | | try |
| | | { |
| | |
| | | //新挤下线主题方案 收到挤下线主题 |
| | | ReceiveNotifySqueezeAsync(mMes); |
| | | } |
| | | else if (topic == $"/BusGateWayToClient/{MqttInfoConfig.Current.HomeGatewayInfo.id}/NotifyBusGateWayInfoChange") |
| | | else if (topic == $"/BusGateWayToClient/{MqttInfoConfig.Current.HomeGatewayInfo.gatewayId}/NotifyBusGateWayInfoChange") |
| | | {//网关上线,需要更新aeskey |
| | | //收到网关上线消息主题 |
| | | ReceiveNotifyBusGateWayInfoChange(); |
| | | } |
| | | else if (topic == $"/BusGateWayToClient/{MqttInfoConfig.Current.HomeGatewayInfo.id}/NotifyGateWayOffline") |
| | | {//网关掉线 //----第二步:读取账号下面的网关列表 |
| | | else if (topic == $"/BusGateWayToClient/{MqttInfoConfig.Current.HomeGatewayInfo.gatewayId}/NotifyGateWayOffline") |
| | | {//网关掉线 |
| | | //----第二步:读取账号下面的网关列表 |
| | | ReceiveNotifyGateWayOffline(); |
| | | } |
| | | else if (topic == $"/BusGateWayToClient/{MqttInfoConfig.Current.HomeGatewayInfo.id}/Common/CheckGateway") |
| | | else if (topic == $"/BusGateWayToClient/{MqttInfoConfig.Current.HomeGatewayInfo.gatewayId}/Common/CheckGateway") |
| | | { |
| | | var ss = Encoding.UTF8.GetString(e.ApplicationMessage.Payload); |
| | | ReceiveCheckGateway(ss); |
| | | } |
| | | else |
| | | { |
| | | |
| | | SetGatewayOnlineResetCheck(); |
| | | |
| | | var packet = new Packet(); |
| | | |
| | | if (!string.IsNullOrEmpty(mqttEncryptKey)) |
| | | if (Entity.DB_ResidenceData.residenceData.GatewayType == 0) |
| | | { |
| | | packet.Bytes = Securitys.EncryptionService.AesDecryptPayload(e.ApplicationMessage.Payload, mqttEncryptKey); |
| | | var packet = new Packet(); |
| | | |
| | | if (!string.IsNullOrEmpty(mqttEncryptKey)) |
| | | { |
| | | packet.Bytes = Securitys.EncryptionService.AesDecryptPayload(e.ApplicationMessage.Payload, mqttEncryptKey); |
| | | } |
| | | else |
| | | { |
| | | packet.Bytes = e.ApplicationMessage.Payload; |
| | | } |
| | | packet.Manager(); |
| | | } |
| | | else |
| | | { |
| | | packet.Bytes = e.ApplicationMessage.Payload; |
| | | //A协议数据处理 |
| | | var revString = System.Text.Encoding.UTF8.GetString(e.ApplicationMessage.Payload); |
| | | var temp = Newtonsoft.Json.JsonConvert.DeserializeObject<GatewayAlinkControlObj>(revString); |
| | | if (temp != null) |
| | | { |
| | | var allLocalFuntion = FunctionList.List.GetAllDeviceFunctionList(); |
| | | foreach (var updateTemp in temp.objects) |
| | | { |
| | | bool hadChange = false;//状态有变化再更新界面 |
| | | var updataObj = allLocalFuntion.Find((obj) => obj.sid == updateTemp.sid); |
| | | if (updataObj != null) |
| | | { |
| | | foreach (var attr in updateTemp.status) |
| | | { |
| | | foreach (var locatAttr in updataObj.attributes) |
| | | { |
| | | if (attr.key == locatAttr.key) |
| | | { |
| | | if (locatAttr.curValue.ToString() == attr.value) |
| | | { |
| | | continue; |
| | | } |
| | | locatAttr.curValue = attr.value; |
| | | hadChange = true; |
| | | } |
| | | } |
| | | } |
| | | if(!hadChange)//状态有变化再更新界面 |
| | | { |
| | | continue; |
| | | } |
| | | //更新界面状态 |
| | | switch (updataObj.spk) |
| | | { |
| | | case SPK.LightSwitch: |
| | | RelayPage.UpdataState(updataObj as Light); |
| | | break; |
| | | case SPK.LightDimming: |
| | | var dimming = updataObj as Light; |
| | | dimming.lastState = Language.StringByID(StringId.Brightness) + " : " + dimming.brightness + "%"; |
| | | DimmerPage.UpdataStates(dimming); |
| | | break; |
| | | case SPK.ElectricFan: |
| | | var fan = updataObj as Fan; |
| | | fan.lastState = Language.StringByID(StringId.Level) + " : " + fan.openLevel; |
| | | FanPage.UpdataState(fan); |
| | | break; |
| | | case SPK.LightRGB: |
| | | var rgb = updataObj as Light; |
| | | rgb.lastState = Language.StringByID(StringId.Brightness) + " : " + rgb.brightness + "%"; |
| | | RGBPage.UpdataStates(rgb); |
| | | break; |
| | | case SPK.LightRGBW: |
| | | case SPK.LightCCT: |
| | | break; |
| | | case SPK.CurtainSwitch: |
| | | var curtain = updataObj as Curtain; |
| | | curtain.lastState = curtain.trait_on_off.curValue.ToString() == "on" ? Language.StringByID(StringId.Open) : Language.StringByID(StringId.Close); |
| | | CurtainModulePage.UpdataState(curtain); |
| | | break; |
| | | case SPK.CurtainTrietex: |
| | | var trietex = updataObj as Curtain; |
| | | trietex.lastState = Language.StringByID(StringId.Open) + trietex.percent + "%"; |
| | | MotorCurtainPage.UpdataState(trietex); |
| | | break; |
| | | case SPK.CurtainRoller: |
| | | var roller = updataObj as Curtain; |
| | | roller.lastState = Language.StringByID(StringId.Open) + roller.percent + "%"; |
| | | RollingShutterPage.UpdataState(roller); |
| | | break; |
| | | case SPK.CurtainShades: |
| | | break; |
| | | case SPK.AcStandard: |
| | | var ac = updataObj as AC; |
| | | ac.lastState = ""; |
| | | switch (ac.trait_mode.curValue.ToString()) |
| | | { |
| | | case "cool": |
| | | ac.lastState = Language.StringByID(StringId.Cool); |
| | | break; |
| | | case "heat": |
| | | ac.lastState = Language.StringByID(StringId.Heat); |
| | | break; |
| | | case "dry": |
| | | ac.lastState = Language.StringByID(StringId.Dry); |
| | | break; |
| | | case "auto": |
| | | ac.lastState = Language.StringByID(StringId.Auto); |
| | | break; |
| | | case "fan": |
| | | ac.lastState = Language.StringByID(StringId.AirSupply); |
| | | break; |
| | | } |
| | | switch (ac.trait_fan.curValue.ToString()) |
| | | { |
| | | case "high": |
| | | ac.lastState += " " + Language.StringByID(StringId.HighWindSpeed); |
| | | break; |
| | | case "medium": |
| | | ac.lastState += " " + Language.StringByID(StringId.MiddleWindSpeed); |
| | | break; |
| | | case "low": |
| | | ac.lastState += " " + Language.StringByID(StringId.LowWindSpeed); |
| | | break; |
| | | case "auto": |
| | | ac.lastState += " " + Language.StringByID(StringId.Auto); |
| | | break; |
| | | } |
| | | ac.lastState += " " + ac.trait_temp.curValue + ac.tempUnitString; |
| | | ACPage.UpdataStates(ac); |
| | | break; |
| | | case SPK.FloorHeatStandard: |
| | | var fh = updataObj as FloorHeating; |
| | | switch (fh.trait_mode.curValue) |
| | | { |
| | | case "normal": |
| | | fh.lastState = Language.StringByID(StringId.Normal); |
| | | break; |
| | | case "day": |
| | | fh.lastState = Language.StringByID(StringId.Day); |
| | | break; |
| | | case "night": |
| | | fh.lastState = Language.StringByID(StringId.Night); |
| | | break; |
| | | case "timer": |
| | | fh.lastState = Language.StringByID(StringId.Auto); |
| | | break; |
| | | case "away": |
| | | fh.lastState = Language.StringByID(StringId.Away); |
| | | break; |
| | | } |
| | | fh.lastState += " " + fh.trait_temp.curValue + fh.tempUnitString; |
| | | FloorHeatingPage.UpdataStates(fh); |
| | | break; |
| | | case SPK.SensorPm25: |
| | | case SPK.SensorCO2: |
| | | case SPK.SensorTVOC: |
| | | case SPK.SensorTemperature: |
| | | case SPK.SensorHumidity: |
| | | var sensor = updataObj as Sensor; |
| | | EnvironmentalSciencePage.LoadEvent_UpdataStatus(sensor); |
| | | break; |
| | | case SPK.ElectricSocket: |
| | | break; |
| | | case SPK.ElectricTV: |
| | | break; |
| | | default: |
| | | break; |
| | | } |
| | | HomePage.UpdataFunctionStates(updataObj); |
| | | RoomPage.UpdataStates(updataObj); |
| | | FunctionPage.UpdataStates(updataObj); |
| | | ClassificationPage.UpdataInfo(updataObj); |
| | | } |
| | | } |
| | | } |
| | | } |
| | | packet.IsLocal = false; |
| | | packet.Manager(); |
| | | } |
| | | } |
| | | catch { } |
| | |
| | | Utlis.WriteLine($"远程连接断开"); |
| | | isSubscribeSuccess = false; |
| | | await DisConnectRemoteMqttClient("UseDisconnectedHandler"); |
| | | |
| | | }); |
| | | } |
| | | //(3)ConnectedHandler |
| | |
| | | IfNeedReadAllDeviceStatus = true; |
| | | Utlis.WriteLine($"============>Mqtt远程连接成功"); |
| | | SendPushSignOut(); |
| | | |
| | | //if (Control.Ins.IsRemote) { |
| | | // //Utlis.ShowAppLinkStatus (AppLinkStatus.CloudLink); |
| | | // MainPage.AddTip (Language.StringByID (SimpleControl.R.MyInternationalizationString.LinkSuccess)); |
| | | //} |
| | | FunctionList.List.ReadAllFunctionStatus(); |
| | | }); |
| | | } |
| | | #endregion |
| | |
| | | |
| | | if (MqttInfoConfig.Current.HomeGatewayInfo != null && MqttInfoConfig.Current.mMqttInfo != null) |
| | | { |
| | | if (bNeedConnectTip) |
| | | { |
| | | bNeedConnectTip = false; |
| | | MainPage.LoadingTipShow(Language.StringByID(SimpleControl.R.MyInternationalizationString.GetSuccessfullyStartConnecting)); |
| | | } |
| | | |
| | | try |
| | | { |
| | | |
| | |
| | | |
| | | //url = HttpUtil.GetProxyEMQUrl (url); |
| | | //#if DEBUG |
| | | // url = HttpUtil.GetProxyEMQUrl (url); |
| | | |
| | | //url = HttpUtil.GetProxyEMQUrl (url); |
| | | //#endif |
| | | var clientId = MqttInfoConfig.Current.mMqttInfo.clientId; |
| | | var username = MqttInfoConfig.Current.mMqttInfo.userName; |
| | |
| | | { |
| | | remoteIsConnected = true; |
| | | IsDisConnectingWithSendCatch = false; |
| | | UnsupportedProtocolVersionCount = 0; |
| | | } |
| | | else |
| | | { |
| | |
| | | catch (Exception ex) |
| | | { |
| | | |
| | | if (ex.Message == MqttCommunicationTimedOutException) |
| | | { |
| | | Console.WriteLine("Connect error TimedOut: " + ex.Message); |
| | | } |
| | | else |
| | | { |
| | | //重新中心服务器获取参数标记 |
| | | MqttInfoConfig.Current.IfGetMqttInfoSuccess = false; |
| | | Console.WriteLine("Connect error: " + ex.Message); |
| | | } |
| | | |
| | | //Console.WriteLine ("Connect error: " + ex.Message); |
| | | if (IfDEBUG) |
| | | { |
| | | MainPage.ShowAlertOnMainThread("Connect error: " + ex.Message); |
| | | } |
| | | //重新中心服务器获取参数标记 |
| | | MqttInfoConfig.Current.IfGetMqttInfoSuccess = false; |
| | | Console.WriteLine("Connect error: " + ex.Message); |
| | | } |
| | | finally |
| | | { |
| | |
| | | switch (optionType) |
| | | { |
| | | case 0: |
| | | topicName = $"/ClientToBusGateWay/{MqttInfoConfig.Current.HomeGatewayInfo.id}/Common/ON"; |
| | | topicName = $"/ClientToBusGateWay/{MqttInfoConfig.Current.HomeGatewayInfo.gatewayId}/Common/ON"; |
| | | if (!string.IsNullOrEmpty(mqttEncryptKey)) |
| | | { |
| | | message = Securitys.EncryptionService.AesEncryptPayload(message, mqttEncryptKey); |
| | |
| | | await RemoteMqttClient.PublishAsync(new MqttApplicationMessage { Topic = topicName, Payload = message, Retain = false, QualityOfServiceLevel = MQTTnet.Protocol.MqttQualityOfServiceLevel.AtMostOnce }); |
| | | break; |
| | | case 3: |
| | | topicName = $"/ClientToBusGateWay/{MqttInfoConfig.Current.HomeGatewayInfo.id}/Common/CheckGateway"; |
| | | topicName = $"/ClientToBusGateWay/{MqttInfoConfig.Current.HomeGatewayInfo.gatewayId}/Common/CheckGateway"; |
| | | Utlis.WriteLine("CheckGateway"); |
| | | await RemoteMqttClient.PublishAsync(new MqttApplicationMessage { Topic = topicName, Retain = false, QualityOfServiceLevel = MQTTnet.Protocol.MqttQualityOfServiceLevel.AtMostOnce }); |
| | | break; |
| | |
| | | /// <returns></returns> |
| | | static async Task GetMqttInfoAndMQTTConnectAsync() |
| | | { |
| | | var mqttInfoRequestResult_Obj =new Server.HttpServerRequest().GetMqttRemoteInfo(GetRandomKey()); |
| | | var mqttInfoRequestResult_Obj = new Server.HttpServerRequest().GetMqttRemoteInfo(GetRandomKey()); |
| | | if (mqttInfoRequestResult_Obj != null) |
| | | { |
| | | MqttInfoConfig.Current.mMqttInfo = mqttInfoRequestResult_Obj; |
| | | if (UserConfig.Instance.GatewayList != null && UserConfig.Instance.GatewayList.Count > 0) |
| | | await MQTTConnectAsync(); |
| | | //1.判断是否绑定了网关,获取网关远程连接的加密KEY |
| | | if (DB_ResidenceData.residenceData.CheckWhetherGatewayIsBound()) |
| | | { |
| | | //----第二步找出是否存在匹配当前住宅的mac,存在再进行远程。 |
| | | MqttInfoConfig.Current.HomeGatewayInfo = UserConfig.Instance.GatewayList[0]; |
| | | if (MqttInfoConfig.Current.HomeGatewayInfo != null) |
| | | { |
| | | //----第三步 开始连接 |
| | | await MQTTConnectAsync(); |
| | | } |
| | | //2.找出是否存在匹配当前住宅的mac,存在再进行远程。 |
| | | MqttInfoConfig.Current.HomeGatewayInfo = DB_ResidenceData.residenceData.HomeGateway; |
| | | //3.开始连接 |
| | | await MQTTConnectAsync(); |
| | | } |
| | | else |
| | | { |
| | | Utlis.WriteLine("============>还没绑定网关"); |
| | | } |
| | | |
| | | } |
| | | else |
| | | { |
| | | Utlis.WriteLine("============>MqttInfo null"); |
| | | } |
| | | |
| | | |
| | | |
| | | } |
| | | |
| | | |
| | | } |
| | | } |