| | |
| | | .WithTcpServer(brokerName, 1883)
|
| | | .WithKeepAlivePeriod(TimeSpan.FromSeconds(20))
|
| | | .WithCleanSession()
|
| | | .WithCommunicationTimeout(TimeSpan.FromSeconds(4))
|
| | | //.WithCommunicationTimeout(TimeSpan.FromSeconds(10))
|
| | | .WithCredentials("", "")
|
| | | .Build();
|
| | | await localMqttClient.ConnectAsync(options, CancellationToken.None);
|
| | |
| | | { |
| | | DebugPrintLog($"远程连接断开"); |
| | | await DisConnectRemoteMqttClient("StartRemoteMqtt.DisconnectedHandler"); |
| | | await StartRemoteMqtt(); |
| | | //await StartRemoteMqtt(); |
| | | }); |
| | | } |
| | | if (RemoteMqttClient.ConnectedHandler == null) |
| | |
| | | |
| | | try |
| | | {
|
| | | var jsonData = new Dictionary<string, object> { ["RequestVersion"] = "RequestVersion", ["RequestSource"] = 0, ["LoginAccessToken"] = Config.Instance.Token, ["RequestProtocolType"] = 0 };
|
| | | var bytes = System.Text.Encoding.UTF8.GetBytes(Newtonsoft.Json.JsonConvert.SerializeObject(jsonData)); |
| | | |
| | | var result = await CommonPage.Instance.DoRequestZigbeeHttpsInterface("https://global.hdlcontrol.com/HangZhouHdlCloudApi/EmqMqtt/GetConnMqttInfo", bytes, Config.Instance.Token); |
| | | //{ |
| | | //"ResponseVersion": "RequestVersion1", |
| | | //"StateCode": "Success", |
| | | //"ErrorInfo": null, |
| | | //"ResponseData": { < !--如下信息只能使用一次,即便你连接失败!-- > |
| | | // "ConnEmqDomainPort": "tcp://developer.hdlcontrol.com:1883", |
| | | // "ConnEmqClientId": "ZigbeeClientTcp_33094de8-34ba-4e38-93c2-ac8da16d0e68_Zigbee", |
| | | // "ConnEmqUserName": "33094de8-34ba-4e38-93c2-ac8da16d0e68", |
| | | // "ConnEmqPwd": "4a265b65-d6cc-4a" |
| | | //} |
| | | //} |
| | | byte[] result = null;
|
| | | if (Config.Instance.Home.IsOthreShare == false)
|
| | | {
|
| | | //主人
|
| | | var jsonData = new Dictionary<string, object>
|
| | | {
|
| | | ["RequestVersion"] = CommonPage.RequestVersion,
|
| | | ["RequestSource"] = 0,
|
| | | ["LoginAccessToken"] = Config.Instance.Token,
|
| | | ["RequestProtocolType"] = 0
|
| | | };
|
| | | var bytes = System.Text.Encoding.UTF8.GetBytes(Newtonsoft.Json.JsonConvert.SerializeObject(jsonData));
|
| | | result = await CommonPage.Instance.DoRequestZigbeeHttpsInterface("https://global.hdlcontrol.com/HangZhouHdlCloudApi/EmqMqtt/GetConnMqttInfo", bytes, Config.Instance.Token);
|
| | | } |
| | | else
|
| | | {
|
| | | //分享的
|
| | | var jsonData = new Dictionary<string, object>
|
| | | {
|
| | | ["RequestVersion"] = CommonPage.RequestVersion,
|
| | | ["RequestSource"] = 0,
|
| | | ["LoginAccessToken"] = Config.Instance.Token,
|
| | | ["RequestProtocolType"] = 0,
|
| | | ["MainUserDistributedMark"] = Config.Instance.Home.MainUserDistributedMark,
|
| | | ["HomeId"] = Config.Instance.Home.Id
|
| | | };
|
| | | var bytes = System.Text.Encoding.UTF8.GetBytes(Newtonsoft.Json.JsonConvert.SerializeObject(jsonData));
|
| | | result = await CommonPage.Instance.DoRequestZigbeeHttpsInterface("https://global.hdlcontrol.com/HangZhouHdlCloudApi/EmqMqtt/ShareMemberConnMqttInfo", bytes, Config.Instance.Token);
|
| | | } |
| | | if (result != null) |
| | | { |
| | | var jobject = Newtonsoft.Json.Linq.JObject.Parse(Encoding.UTF8.GetString(result)); |
| | |
| | | .WithCredentials(connEmqUserName, connEmqPwd)
|
| | | .WithKeepAlivePeriod(TimeSpan.FromSeconds(20))
|
| | | .WithCleanSession()
|
| | | .WithCommunicationTimeout(TimeSpan.FromSeconds(6))
|
| | | //.WithCommunicationTimeout(TimeSpan.FromSeconds(10))
|
| | | .Build();
|
| | | await DisConnectRemoteMqttClient("StartRemoteMqtt");
|
| | | await RemoteMqttClient.ConnectAsync(options, CancellationToken.None);
|
| | |
| | | //文件流不用加密 |
| | | if (topic != "FileTransfer/SendFile") |
| | | { |
| | | topic = ZigBee.Common.SecuritySet.AesEncrypt(System.Text.Encoding.UTF8.GetBytes(topic), Password);
|
| | | topic = topic.Replace("/", "[[$-MQTT_TILT_SYMBOL_REPLACE-$]]").Replace("+", "[[$-MQTT_PLUS_SYMBOL_REPLACE-$]]"); |
| | | message = System.Text.Encoding.UTF8.GetBytes(SecuritySet.AesEncrypt(message, password)); |
| | | message = SecuritySet.AesEncryptBytes(message, password); |
| | | } |
| | | }
|
| | | if (localIsConnected) |
| | |
| | | }
|
| | | catch(Exception e)
|
| | | { |
| | | DebugPrintLog($"Local主动断开_{e.Message}");
|
| | | await DisConnectLocalMqttClient(e.Message); |
| | | await StartLocalMqtt("ReConnect");
|
| | | if (localIsConnected)
|
| | |
| | | { |
| | | try |
| | | { |
| | | var topic = e.ApplicationMessage.Topic.TrimStart('/');
|
| | | topic = topic.Replace("[[$-MQTT_TILT_SYMBOL_REPLACE-$]]", "/").Replace("[[$-MQTT_PLUS_SYMBOL_REPLACE-$]]", "+"); |
| | | var topic = e.ApplicationMessage.Topic.TrimStart('/'); |
| | | var payload = e.ApplicationMessage.Payload; |
| | | |
| | | var message = string.Empty; |
| | |
| | | { |
| | | return; |
| | | } |
| | | if (topics[2]== "NotifyGateWayInfoChagne") { |
| | | if (topics[2]== "NotifyGateWayInfoChange") { |
| | | initGateWayBaseInfomation(); |
| | | return; |
| | | } |
| | |
| | | { |
| | | try |
| | | { |
| | | var topic = e.ApplicationMessage.Topic;
|
| | | topic = topic.Replace("[[$-MQTT_TILT_SYMBOL_REPLACE-$]]", "/").Replace("[[$-MQTT_PLUS_SYMBOL_REPLACE-$]]", "+"); |
| | | var message = System.Text.Encoding.UTF8.GetString(e.ApplicationMessage.Payload); |
| | | var topic = e.ApplicationMessage.Topic; |
| | | string payloadString = ""; |
| | | |
| | | if (IsEncry) |
| | | { |
| | | //主题 |
| | | topic = Common.SecuritySet.AesDecrypt(System.Text.Encoding.UTF8.GetBytes(topic), Password); |
| | | //下载的字节流不需要解密 |
| | | if (topic.Split('/')[0] + "/" + topic.Split('/')[1] == topic.Split('/')[0] + "/" + "FileTransfer") |
| | | { |
| | | if (topic.Split('/')[2] != "DownloadFile") |
| | | { |
| | | message = Common.SecuritySet.AesDecrypt(e.ApplicationMessage.Payload, Password); |
| | | payloadString = System.Text.Encoding.UTF8.GetString(Common.SecuritySet.AesDecryptBytes(e.ApplicationMessage.Payload, Password)); |
| | | } |
| | | } |
| | | else if (topic == topic.Split('/')[0] + "/" + "SendAESKey_Respon") { }//回复主题是秘文,数据是明文 |
| | | else |
| | | { |
| | | message = Common.SecuritySet.AesDecrypt(e.ApplicationMessage.Payload, Password); |
| | | payloadString = System.Text.Encoding.UTF8.GetString(Common.SecuritySet.AesDecryptBytes(e.ApplicationMessage.Payload, Password)); |
| | | } |
| | | } |
| | | |
| | | #if DEBUG |
| | | DebugPrintLog($"网关返回的主题:{topic}_网关返回的负载:{message}_{System.DateTime.Now.ToString()}"); |
| | | #endif |
| | | ReceiveMessage(topic, message, e.ApplicationMessage.Payload); |
| | | else
|
| | | {
|
| | | payloadString = System.Text.Encoding.UTF8.GetString(e.ApplicationMessage.Payload);
|
| | | } |
| | | DebugPrintLog($"网关返回的主题:{topic}_网关返回的负载:{payloadString}"); |
| | | ReceiveMessage(topic, payloadString, e.ApplicationMessage.Payload); |
| | | } |
| | | catch (Exception ex) |
| | | { |
| | | #if DEBUG |
| | | DebugPrintLog($"接收网关数据异常:{ex.Message}"); |
| | | #endif |
| | | } |
| | | } |
| | | |