| | |
| | | /// </summary> |
| | | static string RandomKey = ""; |
| | | |
| | | /// <summary> |
| | | /// 随机生成字符 |
| | | /// </summary> |
| | | /// <returns></returns> |
| | | static string GetRandomKey() |
| | | { |
| | | if (string.IsNullOrEmpty(RandomKey)) |
| | | { |
| | | //随机2位字符串 |
| | | Random random = new Random(Guid.NewGuid().GetHashCode()); |
| | | int num = random.Next(65, 91); |
| | | RandomKey = Convert.ToChar(num).ToString(); |
| | | RandomKey = Utlis.CreateRandomString(2); |
| | | } |
| | | |
| | | return RandomKey; |
| | | |
| | | } |
| | | |
| | | /// <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 (hadGateway) |
| | | while (true) |
| | | { |
| | | try |
| | | { |
| | | System.Threading.Thread.Sleep(500); |
| | | if (!Control.Ins.IsRemote) continue; |
| | | //进入后台不处理 |
| | | if (MainPage.IsEnterBackground) continue; |
| | | |
| | | await StartCloudMqtt(); |
| | | await SubscribeTopics(); |
| | |
| | | } |
| | | |
| | | static bool isSubscribeSuccess; |
| | | /// <summary> |
| | | /// 订阅主题 |
| | | /// </summary> |
| | | /// <returns></returns> |
| | | static async Task SubscribeTopics() |
| | | { |
| | | if (remoteIsConnected && !isSubscribeSuccess) |
| | |
| | | Topic = $"/user/{MqttInfoConfig.Current.HomeGatewayInfo.gatewayId}/app/thing/property/send", |
| | | QualityOfServiceLevel = MQTTnet.Protocol.MqttQualityOfServiceLevel.AtMostOnce |
| | | }; |
| | | //var topicBusStatus = new MqttTopicFilter() |
| | | //{ |
| | | // Topic = $"/BusGateWayToClient/{}/Common", |
| | | // QualityOfServiceLevel = MQTTnet.Protocol.MqttQualityOfServiceLevel.AtMostOnce |
| | | //}; |
| | | |
| | | Utlis.WriteLine("开始订阅!"); |
| | | var result = await RemoteMqttClient.SubscribeAsync(new MqttTopicFilter[] { topicFilterBusGateWayToClient, topicFilterPush2 , topicAlinkStatus }); |
| | | var result = await RemoteMqttClient.SubscribeAsync(new MqttTopicFilter[] { topicFilterBusGateWayToClient, topicFilterPush2, topicAlinkStatus }); |
| | | if (result.Items[0].ResultCode == MQTTnet.Client.Subscribing.MqttClientSubscribeResultCode.GrantedQoS0) |
| | | { |
| | | Control.Ins.GatewayOnline = true; |
| | | isSubscribeSuccess = true; |
| | | Utlis.WriteLine("订阅成功!"); |
| | | |
| | |
| | | } |
| | | |
| | | //追加:没有远程连接的权限 |
| | | if (remoteMqttIsConnecting || remoteIsConnected) |
| | | if (RemoteMqttIsConnecting || remoteIsConnected) |
| | | { |
| | | return; |
| | | } |
| | |
| | | try |
| | | { |
| | | #region 初始化远程Mqtt |
| | | remoteMqttIsConnecting = true; |
| | | RemoteMqttIsConnecting = true; |
| | | RemoteMqttClient = new MqttFactory().CreateMqttClient(); |
| | | |
| | | |
| | |
| | | } |
| | | else if (topic == $"/BusGateWayToClient/{MqttInfoConfig.Current.HomeGatewayInfo.gatewayId}/Common/CheckGateway") |
| | | { |
| | | |
| | | var ss = Encoding.UTF8.GetString(e.ApplicationMessage.Payload); |
| | | ReceiveCheckGateway(ss); |
| | | } |
| | |
| | | { |
| | | packet.Bytes = e.ApplicationMessage.Payload; |
| | | } |
| | | #if DEBUG |
| | | //string ddd = ""; |
| | | //foreach (var bb in packet.Bytes) |
| | | //{ |
| | | // ddd += bb + ","; |
| | | //} |
| | | //MainPage.Log($"mqtt bus data:{ddd}"); |
| | | #endif |
| | | packet.Manager(); |
| | | } |
| | | else |
| | | { |
| | | MainPage.Log($"mqtt A-Link data"); |
| | | //A协议数据处理 |
| | | var revString = Encoding.UTF8.GetString(bytes); |
| | | Control.Ins.UpdataFunctionStatus(revString); |
| | | Control.Ins.UpdataFunctionStatus(revString,null); |
| | | } |
| | | } |
| | | } |
| | |
| | | //(3)ConnectedHandler |
| | | if (RemoteMqttClient.ConnectedHandler == null) |
| | | { |
| | | RemoteMqttClient.UseConnectedHandler(async (e) => { |
| | | Control.Ins.GatewayOnline = true; |
| | | RemoteMqttClient.UseConnectedHandler(async (e) => |
| | | { |
| | | IfNeedReadAllDeviceStatus = true; |
| | | Utlis.WriteLine($"============>Mqtt远程连接成功"); |
| | | SendPushSignOut(); |
| | | //FunctionList.List.ReadAllFunctionStatus(); |
| | | }); |
| | | } |
| | | #endregion |
| | |
| | | finally |
| | | { |
| | | //最终要释放连接状态 |
| | | remoteMqttIsConnecting = false; |
| | | RemoteMqttIsConnecting = false; |
| | | |
| | | Utlis.WriteLine($"StartCloudMqtt: 结束"); |
| | | } |
| | |
| | | /// </summary> |
| | | static void ReceiveNotifySqueezeAsync(string mMes) |
| | | { |
| | | #if DEBUG |
| | | return; |
| | | #endif |
| | | if (mMes == PushSignStr) return;//是自己的登录推送不处理 |
| | | |
| | | //断开远程连接 |
| | |
| | | |
| | | DisConnectRemoteMqttClient("挤下线"); |
| | | |
| | | HDLCommon.Current.CheckLogout(); |
| | | Application.RunOnMainThread(() => |
| | | { |
| | | //弹窗提示被挤下线 |
| | | HDLCommon.Current.CheckLogout(); |
| | | }); |
| | | |
| | | |
| | | //UserInfo.Current.LastTime = DateTime.MinValue; |
| | | //UserInfo.Current.SaveUserInfo(); |
| | |
| | | /// <returns></returns> |
| | | public static async Task MqttRemoteSend(byte[] message, int optionType = 0) |
| | | { |
| | | //return; |
| | | |
| | | try |
| | | { |
| | | string topicName; |
| | |
| | | /// 是否需要读取一次所有设备状态 |
| | | /// </summary> |
| | | static bool IfNeedReadAllDeviceStatus = true; |
| | | //public static bool IsGatewayOnline = true; |
| | | |
| | | /// <summary> |
| | | /// 设置网关在线标志,并重置CheckGateway参数 |
| | |
| | | if (Control.Ins.IsRemote) |
| | | { |
| | | if (!Control.Ins.GatewayOnline) |
| | | Control.Ins.GatewayOnline = true; |
| | | { |
| | | try |
| | | { |
| | | if (DB_ResidenceData.Instance.HomeGateway != null) |
| | | { |
| | | DB_ResidenceData.Instance.HomeGateway.gatewayStatus = true; |
| | | } |
| | | Control.Ins.GatewayOnline = true; |
| | | } |
| | | catch { } |
| | | } |
| | | } |
| | | } |
| | | |