| | |
| | | /// <summary> |
| | | /// 主网关 |
| | | /// </summary> |
| | | /// <value>The main gate way.</value> |
| | | [Newtonsoft.Json.JsonIgnore] |
| | | public static ZbGateway MainGateWay |
| | | { |
| | | get |
| | | { |
| | | return GateWayList.Find((obj) => { return obj.HomeId == Shared.Common.Config.Instance.HomeId && obj.IsMainGateWay; }); |
| | | {
|
| | | ZbGateway mainWay = null; |
| | | for (int i = 0; i < GateWayList.Count; i++)
|
| | | {
|
| | | if (GateWayList[i].HomeId == Config.Instance.HomeId)
|
| | | {
|
| | | //2020.07.16变更:别管那么多,如果住宅ID一样,先确定就是它了(不然有时候经常返回null,有可能是刷新不到)
|
| | | mainWay = GateWayList[i];
|
| | | if (mainWay.IsMainGateWay == true)
|
| | | {
|
| | | //然后如果它确实是主网关,直接break
|
| | | break;
|
| | | }
|
| | | }
|
| | | }
|
| | | return mainWay; |
| | | } |
| | | } |
| | | |
| | |
| | | /// </summary> |
| | | static Guid currentGuid = Guid.NewGuid(); |
| | | |
| | | public async System.Threading.Tasks.Task SendAesKey()
|
| | | public async Task SendAesKey()
|
| | | {
|
| | | if (PubKey != null)
|
| | | {
|
| | |
| | | } |
| | | } |
| | | |
| | | public async System.Threading.Tasks.Task StartLocalMqtt(string brokerName) |
| | | public async Task StartLocalMqtt(string brokerName) |
| | | {
|
| | | if (localMqttIsConnecting
|
| | | || !Shared.Common.Config.Instance.IsLogin
|
| | | || Shared.Common.Config.Instance.HomeId == ""
|
| | | || localIsConnected)
|
| | | || Shared.Common.Config.Instance.HomeId == ""
|
| | | || localIsConnected)
|
| | | {
|
| | | return;
|
| | | } |
| | |
| | | /// 获取当前帐号所有的网关信息 |
| | | /// </summary> |
| | | /// <returns></returns> |
| | | static async System.Threading.Tasks.Task initGateWayBaseInfomation() |
| | | static async Task initGateWayBaseInfomation() |
| | | {
|
| | | if (AllowRemoteCtrl == false)
|
| | | {
|
| | |
| | | public static IMqttClient RemoteMqttClient = new MqttFactory().CreateMqttClient(); |
| | | static bool remoteIsConnected; |
| | | |
| | | /// <summary>
|
| | | /// 远程开始连接的时间点
|
| | | /// </summary> |
| | | private static DateTime RemoteConnectTime = DateTime.Now; |
| | | /// <summary> |
| | | /// 启动远程Mqtt |
| | | /// </summary> |
| | | /// <returns>The start.</returns> |
| | | /// <param name="brokerName">Broker name.</param> |
| | | public static async System.Threading.Tasks.Task StartRemoteMqtt() |
| | | public static async Task StartRemoteMqtt() |
| | | {
|
| | | //追加:没有远程连接的权限
|
| | | if (AllowRemoteCtrl == false
|
| | | || remoteMqttIsConnecting
|
| | | || !Shared.Common.Config.Instance.IsLogin
|
| | | || Shared.Common.Config.Instance.HomeId == ""
|
| | | || remoteIsConnected)
|
| | | {
|
| | | return;
|
| | | } |
| | | await System.Threading.Tasks.Task.Factory.StartNew(async () => |
| | | //如果远程还在连接中 |
| | | if (remoteMqttIsConnecting == true)
|
| | | {
|
| | | //如果这个变量一直处于连接中的状态,但是已经过去了10秒了,还是true的话,说明这里是有点问题的,需要重新创建
|
| | | if ((DateTime.Now - RemoteConnectTime).TotalMilliseconds < 10 * 1000)
|
| | | {
|
| | | return;
|
| | | }
|
| | | } |
| | | //记录起这次远程连接的时间点 |
| | | RemoteConnectTime = DateTime.Now; |
| | | |
| | | await Task.Factory.StartNew(async () => |
| | | { |
| | | try |
| | | { |
| | |
| | | //订阅一个挤下线的主题
|
| | | await RemoteMqttClient.SubscribeAsync("/ZigbeeGateWayToClient/" + Config.Instance.ConnEmqClientId + "/Push/NotifySqueeze");
|
| | |
|
| | | await initGateWayBaseInfomation(); |
| | |
|
| | | //没有主网关时主动读取,获取主网关信息
|
| | | var gateWayList = GateWayList.FindAll(obj => obj.HomeId == Shared.Common.Config.Instance.HomeId);
|
| | | if (gateWayList.Find(obj => obj.IsMainGateWay == true) == null)
|
| | | //如果这个函数卡久了的话,会接收到云端推送的挤下线主题,不知道为什么
|
| | | new Thread(async () =>
|
| | | {
|
| | | if (gateWayList.Count == 1)
|
| | | await initGateWayBaseInfomation();
|
| | |
|
| | | //没有主网关时主动读取,获取主网关信息
|
| | | var gateWayList = GateWayList.FindAll(obj => obj.HomeId == Shared.Common.Config.Instance.HomeId);
|
| | | if (gateWayList.Find(obj => obj.IsMainGateWay == true) == null)
|
| | | {
|
| | | gateWayList[0].IsMainGateWay = true;
|
| | | }
|
| | | else
|
| | | {
|
| | | for (int i = 0; i < gateWayList.Count; i++)
|
| | | if (gateWayList.Count == 1)
|
| | | {
|
| | | var gateWay = gateWayList[i];
|
| | | var info = await gateWay.GetZbGwInfoAsync();
|
| | | if (info == null || info.getGwData == null)
|
| | | gateWayList[0].IsMainGateWay = true;
|
| | | }
|
| | | else
|
| | | {
|
| | | for (int i = 0; i < gateWayList.Count; i++)
|
| | | {
|
| | | continue;
|
| | | }
|
| | | if (info.getGwData.IsDominant == 1)
|
| | | {
|
| | | for (int j = 0; j < gateWayList.Count; j++)
|
| | | var gateWay = gateWayList[i];
|
| | | var info = await gateWay.GetZbGwInfoAsync();
|
| | | if (info == null || info.getGwData == null)
|
| | | {
|
| | | if (gateWayList[i].GwId == info.getGwData.GwId)
|
| | | {
|
| | | gateWayList[i].IsMainGateWay = true;
|
| | | }
|
| | | else
|
| | | {
|
| | | gateWayList[i].IsMainGateWay = false;
|
| | | }
|
| | | continue;
|
| | | }
|
| | | break;
|
| | | if (info.getGwData.IsDominant == 1)
|
| | | {
|
| | | for (int j = 0; j < gateWayList.Count; j++)
|
| | | {
|
| | | if (gateWayList[i].GwId == info.getGwData.GwId)
|
| | | {
|
| | | gateWayList[i].IsMainGateWay = true;
|
| | | }
|
| | | else
|
| | | {
|
| | | gateWayList[i].IsMainGateWay = false;
|
| | | }
|
| | | }
|
| | | break;
|
| | | }
|
| | | }
|
| | | }
|
| | | }
|
| | | }
|
| | | })
|
| | | { IsBackground = true }.Start();
|
| | | |
| | | }); |
| | | } |
| | | #endregion |
| | |
| | | AllowRemoteCtrl = false;
|
| | | return;
|
| | | } |
| | | //虽然不知道对挤下线那一块有没有用,不过先这么弄 |
| | | await System.Threading.Tasks.Task.Delay(1500); |
| | | |
| | | var responseData = jobject["ResponseData"]; |
| | | if (responseData != null) |
| | |
| | | /// <summary> |
| | | /// 断开服务器连接 |
| | | /// </summary> |
| | | public async System.Threading.Tasks.Task DisConnectLocalMqttClient(string s) |
| | | public async Task DisConnectLocalMqttClient(string s) |
| | | { |
| | | try |
| | | { |
| | | if (localIsConnected)
|
| | | {
|
| | | localIsConnected = false;
|
| | | //这个东西也要弄
|
| | | localMqttIsConnecting = false;
|
| | | DebugPrintLog($"Local主动断开_{s}");
|
| | | //await localMqttClient.DisconnectAsync(new MQTTnet.Client.Disconnecting.MqttClientDisconnectOptions { }, CancellationToken.None);
|
| | | await localMqttClient.DisconnectAsync();
|
| | |
| | | { |
| | | DebugPrintLog($"Local断开通讯连接出异常:{ex.Message}"); |
| | | } |
| | | }
|
| | |
|
| | | /// <summary> |
| | | /// 强制断开本地的网关连接 |
| | | /// </summary> |
| | | public async Task CloseLocalConnectionOnForce() |
| | | { |
| | | try |
| | | {
|
| | | await localMqttClient.DisconnectAsync();
|
| | | } |
| | | catch { } |
| | | finally |
| | | {
|
| | | localIsConnected = false;
|
| | | //这个东西也要弄
|
| | | localMqttIsConnecting = false;
|
| | | } |
| | | } |
| | | |
| | | /// <summary>
|
| | | /// 断开远程Mqtt的链接
|
| | | /// </summary> |
| | | public static async System.Threading.Tasks.Task DisConnectRemoteMqttClient(string s = "")
|
| | | public static async Task DisConnectRemoteMqttClient(string s = "")
|
| | | {
|
| | | try
|
| | | {
|
| | | if (remoteIsConnected)
|
| | | {
|
| | | remoteIsConnected = false;
|
| | | //这个东西也要弄
|
| | | remoteMqttIsConnecting = false;
|
| | | DebugPrintLog($"Remote主动断开_{s}");
|
| | | //await RemoteMqttClient.DisconnectAsync(new MQTTnet.Client.Disconnecting.MqttClientDisconnectOptions { }, CancellationToken.None);
|
| | | await RemoteMqttClient.DisconnectAsync();
|
| | |
| | | catch (Exception e)
|
| | | { |
| | | DebugPrintLog($"Remote断开通讯连接出异常:{e.Message}");
|
| | | }
|
| | | } |
| | | |
| | | /// <summary>
|
| | | /// 强制断开远程Mqtt的链接
|
| | | /// </summary>
|
| | | /// <returns></returns> |
| | | public static async Task CloseRemoteConnectionOnForce()
|
| | | {
|
| | | try
|
| | | {
|
| | | await RemoteMqttClient.DisconnectAsync();
|
| | | }
|
| | | catch { }
|
| | | finally
|
| | | {
|
| | | remoteIsConnected = false;
|
| | | //这个东西也要弄
|
| | | remoteMqttIsConnecting = false;
|
| | | }
|
| | | } |
| | | |
| | |
| | | /// </summary> |
| | | public void ReSave() |
| | | { |
| | | if (Shared.Common.Config.Instance.Home.IsShowTemplate == true) |
| | | { |
| | | //展示模板时,不允许保存文件(防止属性上报用的) |
| | | return; |
| | | } |
| | | Global.WriteFileByBytesByHomeId(FilePath, System.Text.Encoding.UTF8.GetBytes(Newtonsoft.Json.JsonConvert.SerializeObject(this))); |
| | | } |
| | | #endregion |
| | |
| | | #if DEBUG |
| | | if (flage == true) |
| | | { |
| | | //if (msg.Contains("DeviceStatusReport") == false) |
| | | //if (msg.Contains("DeviceInfoRespon") == true) |
| | | { |
| | | System.Console.WriteLine(msg + " " + System.DateTime.Now.ToLongTimeString() + " " + System.DateTime.Now.Millisecond); |
| | | } |