| | |
| | | using System.Security.Cryptography; |
| | | using HDL_ON.DriverLayer; |
| | | using HDL_ON.Entity; |
| | | using HDL_ON.UI; |
| | | |
| | | namespace HDL_ON.DAL.Mqtt |
| | | { |
| | |
| | | /// </summary> |
| | | static string mqttEncryptKey = ""; |
| | | //static string checkGatewayTopicBase64 = ""; |
| | | |
| | | static bool hadGateway = true; |
| | | /// <summary> |
| | | /// 挤下线主题 |
| | | /// </summary> |
| | |
| | | /// <summary> |
| | | /// 外网的MQTT是否正在连接 |
| | | /// </summary> |
| | | public static bool remoteMqttIsConnecting; |
| | | public static bool RemoteMqttIsConnecting; |
| | | static bool remoteIsConnected; |
| | | |
| | | static MqttClient() |
| | |
| | | static void InitMqtt() |
| | | { |
| | | new System.Threading.Thread(async () => { |
| | | while (true) |
| | | while (hadGateway) |
| | | { |
| | | try |
| | | { |
| | | System.Threading.Thread.Sleep(500); |
| | | if (!Control.Ins.IsRemote) continue; |
| | | //进入后台不处理 |
| | | if (MainPage.IsEnterBackground) continue; |
| | | |
| | | await StartCloudMqtt(); |
| | | await SubscribeTopics(); |
| | |
| | | |
| | | var topicAlinkStatus = new MqttTopicFilter() |
| | | { |
| | | Topic = $"/user/{MqttInfoConfig.Current.HomeGatewayInfo.gatewayId}/app/thing/property/#", |
| | | Topic = $"/user/{MqttInfoConfig.Current.HomeGatewayInfo.gatewayId}/app/thing/property/send", |
| | | QualityOfServiceLevel = MQTTnet.Protocol.MqttQualityOfServiceLevel.AtMostOnce |
| | | }; |
| | | |
| | |
| | | /// </summary> |
| | | public static async Task StartCloudMqtt() |
| | | { |
| | | |
| | | if (OnAppConfig.Instance.internetStatus == 0) |
| | | if (MainPage.InternetStatus == 0) |
| | | { |
| | | return; |
| | | } |
| | |
| | | } |
| | | |
| | | //追加:没有远程连接的权限 |
| | | if (remoteMqttIsConnecting || remoteIsConnected) |
| | | if (RemoteMqttIsConnecting || remoteIsConnected) |
| | | { |
| | | return; |
| | | } |
| | |
| | | try |
| | | { |
| | | #region 初始化远程Mqtt |
| | | |
| | | RemoteMqttIsConnecting = true; |
| | | RemoteMqttClient = new MqttFactory().CreateMqttClient(); |
| | | |
| | | |
| | |
| | | else |
| | | { |
| | | SetGatewayOnlineResetCheck(); |
| | | if (Entity.DB_ResidenceData.residenceData.GatewayType == 0) |
| | | var bytes = Securitys.EncryptionService.AesDecryptPayload(e.ApplicationMessage.Payload, DB_ResidenceData.Instance.HomeGateway.aesKey); |
| | | |
| | | //bus数据解析 |
| | | if (DB_ResidenceData.Instance.GatewayType == 0) |
| | | { |
| | | var packet = new Packet(); |
| | | |
| | |
| | | else |
| | | { |
| | | //A协议数据处理 |
| | | var revString = Encoding.UTF8.GetString(bytes); |
| | | Control.Ins.UpdataFunctionStatus(revString,null); |
| | | } |
| | | } |
| | | } |
| | |
| | | if (RemoteMqttClient.ConnectedHandler == null) |
| | | { |
| | | RemoteMqttClient.UseConnectedHandler(async (e) => { |
| | | if (DB_ResidenceData.Instance.HomeGateway.gatewayStatus) |
| | | { |
| | | Control.Ins.GatewayOnline = true; |
| | | } |
| | | else |
| | | { |
| | | //new System.Threading.Thread(() => { |
| | | // while(true) |
| | | // { |
| | | // System.Threading.Thread.Sleep(1000); |
| | | // if (DB_ResidenceData.Instance.HomeGateway.GatewayOnline) |
| | | // { |
| | | // Control.Ins.GatewayOnline = true; |
| | | // break; |
| | | // } |
| | | // } |
| | | //}) { IsBackground = true }.Start(); |
| | | } |
| | | IfNeedReadAllDeviceStatus = true; |
| | | Utlis.WriteLine($"============>Mqtt远程连接成功"); |
| | | SendPushSignOut(); |
| | | |
| | | //if (Control.Ins.IsRemote) { |
| | | // //Utlis.ShowAppLinkStatus (AppLinkStatus.CloudLink); |
| | | // MainPage.AddTip (Language.StringByID (SimpleControl.R.MyInternationalizationString.LinkSuccess)); |
| | | //} |
| | | }); |
| | | } |
| | | #endregion |
| | |
| | | finally |
| | | { |
| | | //最终要释放连接状态 |
| | | remoteMqttIsConnecting = false; |
| | | RemoteMqttIsConnecting = false; |
| | | |
| | | Utlis.WriteLine($"StartCloudMqtt: 结束"); |
| | | } |
| | |
| | | { |
| | | try |
| | | { |
| | | if (MqttInfoConfig.Current.HomeGatewayInfo != null && MqttInfoConfig.Current.HomeGatewayInfo.mac == Entity.DB_ResidenceData.residenceData.residenceGatewayMAC) |
| | | if (MqttInfoConfig.Current.HomeGatewayInfo != null && MqttInfoConfig.Current.HomeGatewayInfo.mac == Entity.DB_ResidenceData.Instance.residenceGatewayMAC) |
| | | { |
| | | await MQTTConnectAsync(); |
| | | } |
| | |
| | | /// </summary> |
| | | static void ReceiveNotifySqueezeAsync(string mMes) |
| | | { |
| | | |
| | | #if DEBUG |
| | | return; |
| | | #endif |
| | | if (mMes == PushSignStr) return;//是自己的登录推送不处理 |
| | | |
| | | //断开远程连接 |
| | |
| | | |
| | | DisConnectRemoteMqttClient("挤下线"); |
| | | |
| | | UserInfo.Current.LastTime = DateTime.MinValue; |
| | | UserInfo.Current.SaveUserInfo(); |
| | | HDLCommon.Current.CheckLogout(); |
| | | |
| | | Application.RunOnMainThread(() => { |
| | | MainPage.GoLoginPage(UserInfo.Current); |
| | | //弹窗提示被挤下线 |
| | | }); |
| | | //UserInfo.Current.LastTime = DateTime.MinValue; |
| | | //UserInfo.Current.SaveUserInfo(); |
| | | |
| | | //Application.RunOnMainThread(() => { |
| | | // MainPage.GoLoginPage(UserInfo.Current); |
| | | // //弹窗提示被挤下线 |
| | | //}); |
| | | |
| | | //2020-08-11 删除推送数据 |
| | | //HDLRequest.Current.PushserivceSignOut (); |
| | |
| | | { |
| | | if (Control.Ins.IsRemote) |
| | | { |
| | | Control.Ins.GatewayOnline = true; |
| | | if (!Control.Ins.GatewayOnline) |
| | | Control.Ins.GatewayOnline = true; |
| | | } |
| | | } |
| | | |
| | |
| | | MqttInfoConfig.Current.mMqttInfo = mqttInfoRequestResult_Obj; |
| | | await MQTTConnectAsync(); |
| | | //1.判断是否绑定了网关,获取网关远程连接的加密KEY |
| | | if (DB_ResidenceData.residenceData.CheckWhetherGatewayIsBound()) |
| | | if (DB_ResidenceData.Instance.CheckWhetherGatewayIsBound()) |
| | | { |
| | | //2.找出是否存在匹配当前住宅的mac,存在再进行远程。 |
| | | MqttInfoConfig.Current.HomeGatewayInfo = DB_ResidenceData.residenceData.HomeGateway; |
| | | MqttInfoConfig.Current.HomeGatewayInfo = DB_ResidenceData.Instance.HomeGateway; |
| | | //3.开始连接 |
| | | await MQTTConnectAsync(); |
| | | } |
| | | else |
| | | { |
| | | Utlis.WriteLine("============>还没绑定网关"); |
| | | hadGateway = false; |
| | | } |
| | | } |
| | | } |