| | |
| | | |
| | | using Shared.Common; |
| | | using ZigBee.Device; |
| | | using static ZigBee.Device.ZbGateway; |
| | | |
| | | |
| | | namespace ZigBee.Common |
| | | { |
| | |
| | | /// 新发现的网关通知 |
| | | /// </summary> |
| | | public static Action<ZigBee.Device.ZbGateway> NewGateWayAction; |
| | | /// <summary>
|
| | | /// 上一次的住宅ID
|
| | | /// </summary> |
| | | private static string oldHomeID = string.Empty; |
| | | |
| | | public static void Init() |
| | | { |
| | |
| | | new System.Threading.Thread(async () => |
| | | { |
| | | var gateWayList = new List<ZbGateway> { }; |
| | | var searchCount = 0; |
| | | var oldHomeID = string.Empty; |
| | | var searchCount = 6; |
| | | var broadBytes = new byte[44];// byteHomeId[0] ,//H |
| | | broadBytes[0] = 0xfe; |
| | | broadBytes[1] = 0x29; |
| | |
| | | try |
| | | { |
| | | if (string.IsNullOrEmpty(Shared.Common.Config.Instance.HomeId)) |
| | | { |
| | | {
|
| | | //住宅ID为空只有一种可能就是退出了登录,这里的上一次住宅ID要清空
|
| | | oldHomeID = "?"; |
| | | System.Threading.Thread.Sleep(1000); |
| | | continue; |
| | | } |
| | |
| | | { |
| | | //因为那一瞬间,有可能mqtt会加回来,所以先加缓存 |
| | | var list = new List<ZbGateway>(); |
| | | list.AddRange(GateWayList); |
| | | list.AddRange(ZbGateway.GateWayList); |
| | | //然后清空掉 |
| | | GateWayList.Clear(); |
| | | ZbGateway.GateWayList.Clear(); |
| | | //最后再断开mqtt连接 |
| | | for (int i = 0; i < list.Count; i++) |
| | | { |
| | | list[i].DisConnect("1"); |
| | | list[i].DisConnectLocalMqttClient("1"); |
| | | } |
| | | list.Clear(); |
| | | oldHomeID = Shared.Common.Config.Instance.HomeId; |
| | |
| | | } |
| | | } |
| | | |
| | | var broadcastIpAddress = new Shared.Net.NetWiFi().BroadcastIpAddress; |
| | | System.Console.WriteLine($"Wifi{Shared.Application.IsWifi}"); |
| | | if (Shared.Application.IsWifi) |
| | | { |
| | | if (0 < gateWayList.Count) |
| | | { |
| | | searchCount = 4; |
| | | |
| | | //关闭远程服务器 |
| | | if (RemoteMqttClient != null && RemoteMqttClient.IsConnected == true) |
| | | { |
| | | await RemoteMqttClient?.DisconnectAsync(); |
| | | System.Console.WriteLine($"收到网关远程连接主动断开_{System.DateTime.Now.ToString()}"); |
| | | } |
| | | } |
| | | else |
| | | var broadcastIpAddress = new Shared.Net.NetWiFi().BroadcastIpAddress;
|
| | |
|
| | |
|
| | | if (Shared.Application.IsWifi)
|
| | | {
|
| | | if (0 < gateWayList.Count)
|
| | | {
|
| | | if (searchCount <= 0) |
| | | searchCount = 6;
|
| | | ZbGateway.IsRemote = false;
|
| | | //当网关的连接方式改变时,记录当前的连接方式
|
| | | Shared.Phone.UserCenter.HdlGatewayLogic.Current.CheckGatewayByConnectChanged(Shared.Phone.UserCenter.GatewayConnectMode.WIFI);
|
| | | }
|
| | | else
|
| | | {
|
| | | if (searchCount < 0)
|
| | | {
|
| | | System.Console.WriteLine($"=====================3{System.DateTime.Now.ToString()}"); |
| | | gateWayList.Clear(); |
| | | //远程通讯连接,连接云端服务器
|
| | | await StartCloudMqtt(); |
| | | } |
| | | } |
| | | } |
| | | else |
| | | { |
| | | if (searchCount <= 0) |
| | | { |
| | | System.Console.WriteLine($"=====================4{System.DateTime.Now.ToString()}"); |
| | | gateWayList.Clear(); |
| | | //远程通讯连接,连接云端服务器 |
| | | await StartCloudMqtt(); |
| | | } |
| | | } |
| | | ZbGateway.IsRemote = true;
|
| | | //当网关的连接方式改变时,记录当前的连接方式
|
| | | Shared.Phone.UserCenter.HdlGatewayLogic.Current.CheckGatewayByConnectChanged(Shared.Phone.UserCenter.GatewayConnectMode.Remote);
|
| | | }
|
| | | }
|
| | | }
|
| | | else
|
| | | {
|
| | | ZbGateway.IsRemote = true;
|
| | | //当网关的连接方式改变时,记录当前的连接方式
|
| | | Shared.Phone.UserCenter.HdlGatewayLogic.Current.CheckGatewayByConnectChanged(Shared.Phone.UserCenter.GatewayConnectMode.Remote);
|
| | |
|
| | | for (int i = 0; i < ZbGateway.GateWayList.Count; i++) |
| | | {
|
| | | ZbGateway.GateWayList[i].DisConnectLocalMqttClient("1"); |
| | | }
|
| | | }
|
| | | |
| | | searchCount--; |
| | | |
| | | #region 2秒搜索一次网关 |
| | | //每0.5秒广播发现一次网关,共2s |
| | | int count = 4; |
| | | #region 1秒搜索一次网关 |
| | | //每0.5秒广播发现一次网关,共1s |
| | | int count = 2; |
| | | new System.Threading.Thread(() => |
| | | { |
| | | {
|
| | | gateWayList.Clear(); |
| | | while (0 < count--) |
| | | { |
| | | try |
| | |
| | | FindGateWaySocket.BeginSend(new System.Net.IPEndPoint(broadcastIpAddress, 7624), broadBytes); |
| | | } |
| | | } |
| | | catch (Exception e) |
| | | { |
| | | System.Console.WriteLine("===" + e.Message); |
| | | catch
|
| | | { |
| | | //关闭Socket,下次发送会自动连接
|
| | | FindGateWaySocket.Stop();
|
| | | } |
| | | System.Threading.Thread.Sleep(500); |
| | | } |
| | |
| | | getGatewayBaseInfo = new ZbGateway.GatewayBaseInfo |
| | | { |
| | | IpAddress = ipAddress, |
| | | Time = time, |
| | | GwName = gwName, |
| | | HomeId = homeID, |
| | | IsMainGateWay = isMainGateWay, |
| | |
| | | {
|
| | | gateWayList.Add(zbGateWay); |
| | | } |
| | | //网关中到住宅ID为空此时也通讯 |
| | | else if (homeID == string.Empty) |
| | | {
|
| | | gateWayList.Add(zbGateWay); |
| | | } |
| | | //网关中到住宅ID为空此时也通讯 2020.01.14变更:没这个必要,上面那个变量就能处理 |
| | | //else if (homeID == string.Empty) |
| | | //{
|
| | | // gateWayList.Add(zbGateWay); |
| | | //} |
| | | } |
| | | //网关列表存储处理 |
| | | var gateWay = GateWayList.Find(obj => obj.getGatewayBaseInfo != null && obj.getGatewayBaseInfo.gwID == zbGateWay.getGatewayBaseInfo.gwID); |
| | | var gateWay = ZbGateway.GateWayList.Find(obj => obj.getGatewayBaseInfo != null && obj.getGatewayBaseInfo.gwID == zbGateWay.getGatewayBaseInfo.gwID); |
| | | if (gateWay == null) |
| | | {
|
| | | ZbGateway.GateWayList.Add(zbGateWay); |
| | | await zbGateWay.StartLocalMqtt(ipAddress); |
| | | GateWayList.Add(zbGateWay); |
| | | NewGateWayAction?.Invoke(zbGateWay); |
| | | } |
| | | else |
| | |
| | | gateWay.GatewayOnlineFlage = true; |
| | | if (gateWay.getGatewayBaseInfo.IpAddress != ipAddress) |
| | | { |
| | | gateWay.DisConnect("2"); |
| | | GateWayList.Remove(gateWay); |
| | | await gateWay.DisConnectLocalMqttClient("2"); |
| | | ZbGateway.GateWayList.Remove(gateWay); |
| | | gateWay = zbGateWay;
|
| | | ZbGateway.GateWayList.Add(gateWay); |
| | | await zbGateWay.StartLocalMqtt(ipAddress); |
| | | GateWayList.Add(gateWay); |
| | | } |
| | | else |
| | | { |
| | | gateWay.PubKey = pubKey; |
| | | gateWay.getGatewayBaseInfo.Time = time; |
| | | gateWay.getGatewayBaseInfo.GwName = gwName; |
| | | gateWay.getGatewayBaseInfo.HomeId = homeID;
|
| | | await gateWay.StartLocalMqtt(ipAddress); |
| | |
| | | //主网关设置 |
| | | if (isMainGateWay && oldHomeID == gateWay.getGatewayBaseInfo.HomeId) |
| | | { |
| | | for (int i = 0; i < GateWayList.Count; i++) |
| | | for (int i = 0; i < ZbGateway.GateWayList.Count; i++) |
| | | { |
| | | var gw = GateWayList[i]; |
| | | var gw = ZbGateway.GateWayList[i]; |
| | | //网关ID不是当前的网关,则把网关列表中其他网关标记为不是主网关 |
| | | if (gw.getGatewayBaseInfo.gwID != id && oldHomeID == gw.getGatewayBaseInfo.HomeId) |
| | | { |
| | |
| | | //标记当前网关是主网关 |
| | | gateWay.getGatewayBaseInfo.IsMainGateWay = true; |
| | | } |
| | | }
|
| | |
|
| | | //测试能否广播得到网关,通常情况下不检测
|
| | | if (Shared.Phone.UserCenter.UserCenterResourse.HideOption.CheckCanReceiveGateway == 1)
|
| | | {
|
| | | if (Shared.Phone.UserCenter.UserCenterResourse.DicReceiveGatewayTest.ContainsKey(id) == false)
|
| | | {
|
| | | if (gateWay == null)
|
| | | {
|
| | | Shared.Phone.UserCenter.UserCenterResourse.DicReceiveGatewayTest[id] = zbGateWay;
|
| | | }
|
| | | else
|
| | | {
|
| | | Shared.Phone.UserCenter.UserCenterResourse.DicReceiveGatewayTest[id] = gateWay;
|
| | | }
|
| | | }
|
| | | } |
| | | |
| | | } |
| | | } |
| | | catch (Exception ex) |
| | | catch |
| | | { |
| | | var mess = ex.Message; |
| | | //关闭Socket,下次发送会自动连接 |
| | | FindGateWaySocket.Stop(); |
| | | } |
| | | } |
| | | #endregion |
| | | #endregion |
| | | } |
| | | catch (Exception ex) |
| | | { |
| | | var mess = ex.Message; |
| | | } |
| | | catch { } |
| | | System.Threading.Thread.Sleep(500); |
| | | |
| | | } |
| | | }) |
| | | { IsBackground = true }.Start(); |
| | |
| | | { |
| | | try |
| | | { |
| | | if (!ZbGateway.IsRemote) |
| | | { |
| | | System.Threading.Thread.Sleep(500); |
| | | continue; |
| | | } |
| | | var gateWayList = GateWayList.FindAll(obj => obj.getGatewayBaseInfo.HomeId == Shared.Common.Config.Instance.HomeId); |
| | | |
| | | if (gateWayList.Count == 1) |
| | | { |
| | | gateWayList[0].getGatewayBaseInfo.IsMainGateWay = true; |
| | | } |
| | | else |
| | | { |
| | | for (int i = 0; i < gateWayList.Count; i++) |
| | | { |
| | | var gateWay = gateWayList[i]; |
| | | var info = await gateWay.GetZbGwInfoAsync(); |
| | | if (info == null || info.getGwData == null) |
| | | { |
| | | continue; |
| | | } |
| | | if (info.getGwData.IsDominant == 1) |
| | | { |
| | | gateWay.getGatewayBaseInfo.IsMainGateWay = true; |
| | | for (int j = i + 1; j < gateWayList.Count; j++) |
| | | { |
| | | gateWayList[j].getGatewayBaseInfo.IsMainGateWay = false; |
| | | }
|
| | | break; |
| | | } |
| | | } |
| | | } |
| | | if (gateWayList.Find(obj => obj.getGatewayBaseInfo.IsMainGateWay == true) != null) |
| | | { |
| | | break; |
| | | } |
| | | //定时检测远程连接情况 |
| | | await ZbGateway.StartRemoteMqtt(); |
| | | } |
| | | catch { } |
| | | System.Threading.Thread.Sleep(2000); |
| | |
| | | { IsBackground = true }.Start(); |
| | | } |
| | | |
| | | /// <summary>
|
| | | /// 重新搜索,注意:调用该方法,则促使全部网关的mqtt全部断开,然后重新搜索(可能会有1秒延迟)
|
| | | /// </summary> |
| | | public static void ReSearch()
|
| | | {
|
| | | //为了那么多少万分之一的几率,这里设置成别的另一类的值
|
| | | oldHomeID = "**";
|
| | | } |
| | | |
| | | /// <summary> |
| | | /// 接收处理UDP数据包 |
| | |
| | | public static class FindGateWaySocket |
| | | { |
| | | //本地Socket |
| | | public static System.Net.Sockets.Socket busSocket; |
| | | public static Socket busSocket; |
| | | /// <summary>
|
| | | /// 计时器
|
| | | /// </summary> |
| | | private static int timeCount = 0; |
| | | |
| | | /// <summary> |
| | | /// 启动Socket接收和发送功能 |
| | |
| | | { |
| | | get |
| | | { |
| | | return null == busSocket ? false : true; |
| | | return busSocket == null ? false : true; |
| | | } |
| | | } |
| | | |
| | |
| | | { |
| | | try |
| | | { |
| | | //检测连接状态 |
| | | CheckConnectiton(); |
| | | |
| | | Start(7624); |
| | | busSocket.BeginSendTo(bytes, 0, bytes.Length, SocketFlags.None, iPEndPoint, new AsyncCallback(asyncEndSend), null); |
| | | } |
| | | catch { } |
| | | } |
| | | |
| | | /// <summary>
|
| | | /// 检测连接状态
|
| | | /// </summary> |
| | | private static void CheckConnectiton()
|
| | | {
|
| | | if (busSocket == null) { return; }
|
| | | timeCount++;
|
| | | if (timeCount < 20)
|
| | | {
|
| | | //每10秒检测一次
|
| | | return;
|
| | | }
|
| | | timeCount = 0;
|
| | |
|
| | | bool blockingState = busSocket.Blocking;
|
| | | try
|
| | | {
|
| | | byte[] tmp = new byte[1];
|
| | |
|
| | | busSocket.Blocking = false;
|
| | | busSocket.Send(tmp, 0, 0);
|
| | | //tcp还在连接着
|
| | | busSocket.Blocking = blockingState;
|
| | | }
|
| | | catch (SocketException e)
|
| | | {
|
| | | // 10035 == WSAEWOULDBLOCK
|
| | | if (e.NativeErrorCode.Equals(10035))
|
| | | {
|
| | | //Still Connected, but the Send would block
|
| | | }
|
| | | else
|
| | | {
|
| | | //tcp已经断开了连接
|
| | | Stop();
|
| | | }
|
| | | }
|
| | | } |
| | | |
| | | /// <summary> |
| | | /// 异步发送数据结束 |
| | | /// </summary> |