| | |
| | | { |
| | | public static class MqttCommon |
| | | { |
| | | /// <summary> |
| | | /// 是否提示 远程连接的错误信息 |
| | | /// </summary> |
| | | public static bool IfDEBUG = false; |
| | | |
| | | static string mqttEncryptKey = ""; |
| | | static string checkGatewayTopicBase64 = ""; |
| | | static RemoteMACInfo CurRemoteMACInfo = null; |
| | |
| | | new System.Threading.Thread (async () => { |
| | | while (true) { |
| | | try { |
| | | System.Threading.Thread.Sleep (100); |
| | | System.Threading.Thread.Sleep (200); |
| | | if (!CommonPage.IsRemote) continue; |
| | | |
| | | await StartCloudMqtt (); |
| | |
| | | } |
| | | }) { IsBackground = true }.Start (); |
| | | } |
| | | |
| | | public static void InitState () |
| | | { |
| | | IfNeedReadAllDeviceStatus = true; |
| | | bNeedStartTip = true; |
| | | IsGatewayOnline = false; |
| | | |
| | | } |
| | | |
| | | |
| | | /// <summary> |
| | | /// 是否订阅成功 |
| | |
| | | if (CommonPage.IsRemote) { |
| | | Utlis.ShowAppLinkStatus (AppLinkStatus.CloudUnlink); |
| | | } |
| | | Utlis.WriteLine ($"============>MqttRemoteDisConnectRemoteMqttClient"); |
| | | //Utlis.WriteLine ($"============>MqttRemoteDisConnectRemoteMqttClient"); |
| | | |
| | | } |
| | | } catch (Exception e) { |
| | |
| | | /// <summary> |
| | | /// 外网的MQTT是否正在连接 |
| | | /// </summary> |
| | | static bool remoteMqttIsConnecting; |
| | | public static bool remoteMqttIsConnecting; |
| | | static bool remoteIsConnected; |
| | | /// <summary> |
| | | /// 启动远程Mqtt |
| | |
| | | } |
| | | if (RemoteMqttClient.ConnectedHandler == null) { |
| | | RemoteMqttClient.UseConnectedHandler (async (e) => { |
| | | IfNeedReadAllDeviceStatus = true; |
| | | //IfNeedReadAllDeviceStatus = true; |
| | | mCheckGatewayTime = DateTime.Now; |
| | | CheckGatewayCount = 0; |
| | | bNeedStartTip = true; |
| | |
| | | } |
| | | |
| | | } catch (Exception ex) { |
| | | Utlis.WriteLine ($"============>Mqtt 远程连接通讯连接出异常:{ex.Message}"); |
| | | //Utlis.WriteLine ($"============>Mqtt 远程连接通讯连接出异常:{ex.Message}"); |
| | | |
| | | MainPage.LoadingSetText ("Connect error, trying to reconnect."); |
| | | |
| | | if (IfDEBUG) { |
| | | MainPage.ShowAlertOnMainThread ("error: " + ex.Message); |
| | | } |
| | | |
| | | } finally { |
| | | //最终要释放连接状态 |
| | | remoteMqttIsConnecting = false; |
| | | |
| | | MainPage.LoadingHide (); |
| | | } |
| | | }); |
| | | } |
| | |
| | | //----第三步 开始连接 |
| | | await MQTTConnectAsync (); |
| | | } else { |
| | | Utlis.WriteLine ("============>Mqtt CurRemoteMACInfo null"); |
| | | //Utlis.WriteLine ("============>Mqtt CurRemoteMACInfo null"); |
| | | |
| | | MainPage.LoadingSetText ("Please wait, failed to get gateway parameters."); |
| | | } |
| | | } |
| | | |
| | | } else { |
| | | Utlis.WriteLine ("============>Mqtt mqttInfoRequestResult_Obj null"); |
| | | //Utlis.WriteLine ("============>Mqtt mqttInfoRequestResult_Obj null"); |
| | | |
| | | MainPage.LoadingSetText ("Please wait, failed to get user parameters."); |
| | | //MainPage.LoadingSetText ("Please wait, failed to get parameters."); |
| | | } |
| | | } catch { } |
| | | } catch { |
| | | |
| | | } |
| | | } else { |
| | | Utlis.WriteLine ("============>Mqtt GetInfo 失败"); |
| | | //Utlis.WriteLine ("============>Mqtt GetInfo 失败"); |
| | | MainPage.AddTip ("Please wait, failed to get parameters."); |
| | | //MainPage.LoadingSetText ("Please wait, failed to get parameters."); |
| | | } |
| | | |
| | | } |
| | |
| | | mMqttInfo = mqttInfoRequestResult_info; |
| | | //--第二步:获取当前住分享宅网关信息并连接MQTT |
| | | await GetSingleHomeGatewayPaggerAndMQTTConnectAsync (); |
| | | } else { |
| | | MainPage.LoadingSetText ("Please wait, failed to get user parameters."); |
| | | } |
| | | } |
| | | } |
| | |
| | | |
| | | |
| | | //获取参数成功,开始连接... |
| | | MainPage.AddTip ("Successfully obtained parameters, starting to connect..."); |
| | | //MainPage.AddTip ("Successfully obtained parameters, starting to connect..."); |
| | | |
| | | MainPage.LoadingStart ("Please wait, connecting remotely..."); |
| | | |
| | | //try { |
| | | // var url = mMqttInfo.connEmqDomainPort; |
| | | // var clientId = mMqttInfo.connEmqClientId; |
| | |
| | | // Console.WriteLine ("Connect error: " + ex.Message); |
| | | //} |
| | | |
| | | string mes = ""; |
| | | //string mes = ""; |
| | | try { |
| | | |
| | | var url = mMqttInfo.connEmqDomainPort; |
| | |
| | | var username = mMqttInfo.connEmqUserName; |
| | | var passwordRemote = mMqttInfo.connEmqPwd; |
| | | |
| | | mes = "step1.Set Data"; |
| | | //mes = "step1.Set Data"; |
| | | CurRemoteMACInfo.LoginAccessToken = MainPage.LoginUser.LoginTokenString; |
| | | mqttEncryptKey = CurRemoteMACInfo.aesKey; |
| | | var options1 = new MQTTnet.Client.Options.MqttClientOptionsBuilder () |
| | |
| | | // .WithCommunicationTimeout (new TimeSpan (0, 0, 20)) |
| | | .Build (); |
| | | |
| | | mes = "step2.DisConnect"; |
| | | //mes = "step2.DisConnect"; |
| | | await DisConnectRemoteMqttClient ("StartRemoteMqtt"); |
| | | //MainPage.AddTip ("Start to connect..."); |
| | | mes = "step3.Start to connect"; |
| | | //mes = "step3.Start to connect"; |
| | | await RemoteMqttClient.ConnectAsync (options1); |
| | | mes = "step4.Connected Success"; |
| | | //mes = "step4.Connected Success"; |
| | | remoteIsConnected = true; |
| | | IsDisConnectingWithSendCatch = false; |
| | | |
| | | MainPage.LoadingSetText ("Remote connection succeeded."); |
| | | |
| | | //MainPage.AddTip ("Connection succeeded!"); |
| | | //await MqttRemoteSend (new byte [] { 0 }, 1); |
| | |
| | | //await MqttRemoteSend (new byte [] { 0 }, 4); |
| | | |
| | | } catch (Exception ex) { |
| | | mes = ex.Message; |
| | | //mes = ex.Message; |
| | | //options1 异常提示 |
| | | //Console.WriteLine("Connect error: " + ex.Message); |
| | | //MainPage.ShowAlertOnMainThread ("Connect error: " + ex.Message); |
| | | |
| | | MainPage.LoadingSetText ("Connect error."); |
| | | |
| | | if (IfDEBUG) { |
| | | MainPage.ShowAlertOnMainThread ("Connect error: " + ex.Message); |
| | | } |
| | | } finally { |
| | | MainPage.ShowAlertOnMainThread ("finally: " + mes); |
| | | //MainPage.ShowAlertOnMainThread ("finally: " + mes); |
| | | //MainPage.AddTip ("finally: " + mes); |
| | | } |
| | | |
| | |
| | | if (bNeedStartTip) { |
| | | bNeedStartTip = false; |
| | | if (CommonPage.IsRemote) { |
| | | MainPage.AddTip ("Getting remote connection parameters..."); |
| | | //MainPage.AddTip ("Getting remote connection parameters..."); |
| | | MainPage.LoadingStart ("Please wait, getting connection parameters..."); |
| | | } |
| | | } |
| | | } |
| | |
| | | if (mGatewayRes != null) { |
| | | CurRemoteMACInfo.aesKey = mGatewayRes.AesKey; |
| | | mqttEncryptKey = CurRemoteMACInfo.aesKey; |
| | | } else { |
| | | MainPage.LoadingSetText ("Please wait, failed to get gateway parameters."); |
| | | } |
| | | |
| | | } |