| | |
| | | /// <value><c>true</c> if is remote; otherwise, <c>false</c>.</value> |
| | | public static bool IsRemote; |
| | | |
| | | /// <summary>
|
| | | /// 是否拥有远程连接的权限
|
| | | /// </summary> |
| | | [Newtonsoft.Json.JsonIgnore] |
| | | public static bool AllowRemoteCtrl = true; |
| | | |
| | | /// <summary> |
| | | /// 网关保存路径 |
| | | /// </summary> |
| | |
| | | var jObject = new JObject { { "Cluster_ID", 0 }, { "Command", 82 } }; |
| | | var data = new JObject { { "HomeId", homeId }, { "AccountId", accountId } }; |
| | | jObject.Add("Data", data); |
| | | Send("GwSetHomeId", jObject.ToString()); |
| | | //住宅ID的设置,固定使用局域网,不存在远程的说法 |
| | | SendLocation("GwSetHomeId", System.Text.Encoding.UTF8.GetBytes(jObject.ToString())); |
| | | } |
| | | catch { } |
| | | |
| | |
| | | /// <returns></returns> |
| | | static async System.Threading.Tasks.Task initGateWayBaseInfomation() |
| | | {
|
| | | if (AllowRemoteCtrl == false)
|
| | | {
|
| | | //没有远程连接的权限
|
| | | return;
|
| | | }
|
| | | string loginToken = Config.Instance.Token;
|
| | | if (Config.Instance.Home.IsOthreShare == true)
|
| | | {
|
| | |
| | | var statuCode = jobject["StateCode"].ToString();
|
| | | if (statuCode != "Success")
|
| | | {
|
| | | //Log出力
|
| | | Shared.Phone.UserCenter.HdlLogLogic.Current.WriteLog(-1, "接口访问失败★:App/GetSingleHomeGatewayPagger " + statuCode);
|
| | | //序列化对象
|
| | | var requestJson = Newtonsoft.Json.JsonConvert.SerializeObject(pra2);
|
| | | Shared.Phone.UserCenter.HdlLogLogic.Current.WriteLog(-1, "参数:\r\n" + requestJson);
|
| | | //Log出力
|
| | | string errorMsg = "接口访问失败★:App/GetSingleHomeGatewayPagger " + statuCode + "\r\n";
|
| | | errorMsg += "参数:\r\n" + requestJson;
|
| | | Shared.Phone.UserCenter.HdlLogLogic.Current.WriteLog(-1, errorMsg);
|
| | | return;
|
| | | }
|
| | |
|
| | | var responseData = jobject["ResponseData"];
|
| | |
| | | /// <param name="brokerName">Broker name.</param> |
| | | public static async System.Threading.Tasks.Task StartRemoteMqtt() |
| | | {
|
| | | if (remoteMqttIsConnecting
|
| | | || !Shared.Common.Config.Instance.IsLogin
|
| | | || Shared.Common.Config.Instance.HomeId == ""
|
| | | || remoteIsConnected)
|
| | | //追加:没有远程连接的权限
|
| | | if (AllowRemoteCtrl == false
|
| | | || remoteMqttIsConnecting
|
| | | || !Shared.Common.Config.Instance.IsLogin
|
| | | || Shared.Common.Config.Instance.HomeId == ""
|
| | | || remoteIsConnected)
|
| | | {
|
| | | return;
|
| | | } |
| | |
| | | RemoteMqttClient.UseConnectedHandler(async (e) =>
|
| | | {
|
| | | DebugPrintLog($"远程连接成功");
|
| | |
|
| | | if (Config.Instance.Home.IsOthreShare == true)
|
| | | {
|
| | | //订阅一个成员被删除的主题
|
| | | string myGuid = Config.Instance.Guid;
|
| | | await RemoteMqttClient.SubscribeAsync("/ZigbeeGateWayToClient/" + myGuid + "/Push/Deleted");
|
| | | //订阅一个分享数据已经变更的主题
|
| | | await RemoteMqttClient.SubscribeAsync("/ZigbeeGateWayToClient/" + myGuid + "/Push/DeletedShareData");
|
| | | }
|
| | |
|
| | | await initGateWayBaseInfomation(); |
| | | Shared.Phone.UserCenter.HdlGatewayLogic.Current.CheckGatewayByConnectChanged(Shared.Phone.UserCenter.GatewayConnectMode.Remote);
|
| | | //没有主网关时主动读取,获取主网关信息
|
| | |
| | | if (result != null) |
| | | { |
| | | var jobject = Newtonsoft.Json.Linq.JObject.Parse(Encoding.UTF8.GetString(result)); |
| | | if (jobject["StateCode"].ToString() == "NotAllowRemoteCtrl")
|
| | | {
|
| | | //没有远程连接的权限
|
| | | AllowRemoteCtrl = false;
|
| | | return;
|
| | | } |
| | | var responseData = jobject["ResponseData"]; |
| | | if (responseData != null) |
| | | { |
| | |
| | | {
|
| | | localIsConnected = false;
|
| | | DebugPrintLog($"Local主动断开_{s}");
|
| | | await localMqttClient.DisconnectAsync(new MQTTnet.Client.Disconnecting.MqttClientDisconnectOptions { }, CancellationToken.None);
|
| | | //await localMqttClient.DisconnectAsync(new MQTTnet.Client.Disconnecting.MqttClientDisconnectOptions { }, CancellationToken.None);
|
| | | await localMqttClient.DisconnectAsync(); |
| | | }
|
| | | } |
| | | catch (Exception ex) |
| | |
| | | {
|
| | | remoteIsConnected = false;
|
| | | DebugPrintLog($"Remote主动断开_{s}");
|
| | | await RemoteMqttClient.DisconnectAsync(new MQTTnet.Client.Disconnecting.MqttClientDisconnectOptions { }, CancellationToken.None);
|
| | | //await RemoteMqttClient.DisconnectAsync(new MQTTnet.Client.Disconnecting.MqttClientDisconnectOptions { }, CancellationToken.None);
|
| | | await RemoteMqttClient.DisconnectAsync();
|
| | | }
|
| | | }
|
| | | catch(Exception e) {
|
| | |
| | | await StartLocalMqtt("ReConnect");
|
| | | if (localIsConnected)
|
| | | {
|
| | | DebugPrintLog($"局域网——二次发送到网关的主题:{topic}_发送到网关的数据:{System.Text.Encoding.UTF8.GetString(message)}_当前网关{CurrentGateWayId} 是否加密:{IsEncry}");
|
| | | await localMqttClient.PublishAsync(new MqttApplicationMessage { Topic = topic, Payload = message, QualityOfServiceLevel = MQTTnet.Protocol.MqttQualityOfServiceLevel.ExactlyOnce, Retain = retain });
|
| | | } |
| | | }
|
| | |
| | | return; |
| | | } |
| | | await Send(topic, System.Text.Encoding.UTF8.GetBytes(message), retain); |
| | | }
|
| | |
|
| | | /// <summary> |
| | | /// 强制指定使用本地局域网发送消息到服务器 |
| | | /// </summary> |
| | | /// <returns></returns> |
| | | /// <param name="topic"></param> |
| | | /// <param name="message"></param> |
| | | /// <param name="retain"></param> |
| | | public async Task SendLocation(string topic, byte[] message, bool retain = false) |
| | | { |
| | | try |
| | | { |
| | | if (Shared.Common.Config.Instance.HomeId == "") |
| | | { |
| | | return; |
| | | }
|
| | |
|
| | | DebugPrintLog($"局域网——发送到网关的主题:{topic}_发送到网关的数据:{System.Text.Encoding.UTF8.GetString(message)}_当前网关{CurrentGateWayId} 是否加密:{IsEncry}");
|
| | |
|
| | | if (IsEncry)
|
| | | {
|
| | | //文件流不用加密
|
| | | if (topic != "FileTransfer/SendFile")
|
| | | {
|
| | | message = SecuritySet.AesEncryptBytes(message, password);
|
| | | }
|
| | | }
|
| | | if (localIsConnected)
|
| | | {
|
| | | try
|
| | | {
|
| | | await localMqttClient.PublishAsync(new MqttApplicationMessage { Topic = topic, Payload = message, QualityOfServiceLevel = MQTTnet.Protocol.MqttQualityOfServiceLevel.ExactlyOnce, Retain = retain });
|
| | | }
|
| | | catch (Exception e)
|
| | | {
|
| | | DebugPrintLog($"Local主动断开_{e.Message}");
|
| | | await DisConnectLocalMqttClient(e.Message);
|
| | | await StartLocalMqtt("ReConnect");
|
| | | if (localIsConnected)
|
| | | {
|
| | | DebugPrintLog($"局域网——二次发送到网关的主题:{topic}_发送到网关的数据:{System.Text.Encoding.UTF8.GetString(message)}_当前网关{CurrentGateWayId} 是否加密:{IsEncry}");
|
| | | await localMqttClient.PublishAsync(new MqttApplicationMessage { Topic = topic, Payload = message, QualityOfServiceLevel = MQTTnet.Protocol.MqttQualityOfServiceLevel.ExactlyOnce, Retain = retain });
|
| | | }
|
| | | }
|
| | | } |
| | | } |
| | | catch (Exception ex) |
| | | { |
| | | DebugPrintLog($"Send:{ex.Message}"); |
| | | } |
| | | } |
| | | |
| | | |
| | | [Serializable] |
| | | public class CloudTimeResponse |
| | |
| | | #region 设备请求APP获取升级数据 |
| | | else if (topic == gatewayID + "/" + "ZbDataPassthrough") |
| | | { |
| | | var gatewayTemp = new ZbGateway() { DataID = jobject.Value<int>("Data_ID") }; |
| | | gatewayTemp.clientDataPassthroughResponseData = Newtonsoft.Json.JsonConvert.DeserializeObject<ClientDataPassthroughResponseData>(jobject["Data"].ToString()); |
| | | if (gatewayTemp.clientDataPassthroughResponseData == null) |
| | | { |
| | | return; |
| | | } |
| | | |
| | | //上报类型通知 |
| | | if (gwa.ReportAction != null) |
| | | { |
| | | DebugPrintLog("DeviceRequestAcUpdateData"); |
| | | gwa.ReportAction("DeviceRequestAcUpdateData", gatewayTemp.clientDataPassthroughResponseData); |
| | | {
|
| | | var clientDataPassthrough = Newtonsoft.Json.JsonConvert.DeserializeObject<ClientDataPassthroughResponseData>(jobject["Data"].ToString());
|
| | | if (clientDataPassthrough != null)
|
| | | {
|
| | | DebugPrintLog("DeviceRequestAcUpdateData");
|
| | | gwa.ReportAction("DeviceRequestAcUpdateData", clientDataPassthrough);
|
| | | } |
| | | } |
| | | } |
| | | #endregion |
| | | DebugPrintLog("网关返回数据通知"); |
| | | } |
| | | catch (Exception ex) |
| | | { |