| | |
| | | { |
| | | new System.Threading.Thread(() => |
| | | { |
| | | var searchCount = 3; |
| | | var broadBytes = new byte[44];// byteHomeId[0] ,//H |
| | | broadBytes[0] = 0xfe; |
| | | broadBytes[1] = 0x29; |
| | |
| | | } |
| | | |
| | | var broadcastIpAddress = new Shared.Net.NetWiFi().BroadcastIpAddress;
|
| | | if (Shared.Application.IsWifi)
|
| | | |
| | | //清空接收到的IP |
| | | listReceiveIP.Clear(); |
| | | if (broadcastIpAddress.ToString() != "0.0.0.0") |
| | | {
|
| | | if (0 < listReceiveIP.Count)
|
| | | {
|
| | | searchCount = 3;
|
| | | ZbGateway.IsRemote = false;
|
| | | //当网关的连接方式改变时,记录当前的连接方式
|
| | | Shared.Phone.UserCenter.HdlGatewayLogic.Current.CheckGatewayByConnectChanged(Shared.Phone.UserCenter.GatewayConnectMode.WIFI);
|
| | | }
|
| | | else
|
| | | {
|
| | | if (searchCount < 0)
|
| | | {
|
| | | ZbGateway.IsRemote = true;
|
| | | //当网关的连接方式改变时,记录当前的连接方式
|
| | | Shared.Phone.UserCenter.HdlGatewayLogic.Current.CheckGatewayByConnectChanged(Shared.Phone.UserCenter.GatewayConnectMode.Remote);
|
| | | }
|
| | | }
|
| | | //广播出去 |
| | | FindGateWaySocket.BeginSend(new System.Net.IPEndPoint(broadcastIpAddress, 7624), broadBytes); |
| | | } |
| | | //等待3秒,下面的接收方法会去接收广播 |
| | | System.Threading.Thread.Sleep(3000); |
| | | |
| | | if (listReceiveIP.Count > 0)
|
| | | {
|
| | | //局域网能够广播得到网关
|
| | | ZbGateway.IsRemote = false;
|
| | | //当网关的连接方式改变时,记录当前的连接方式
|
| | | Shared.Phone.UserCenter.HdlGatewayLogic.Current.CheckGatewayByConnectChanged(Shared.Phone.UserCenter.GatewayConnectMode.WIFI);
|
| | | }
|
| | | 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"); |
| | | }
|
| | | //非wifi情况下,不需要广播
|
| | | System.Threading.Thread.Sleep(1000);
|
| | | continue;
|
| | | }
|
| | | searchCount--; |
| | | |
| | | //每秒广播发现一次网关 |
| | | int count = 2; |
| | | listReceiveIP.Clear(); |
| | | while (0 < count--) |
| | | { |
| | | //点对点发送(先发一条已有的点播,回复几率高一点) |
| | | for (int i = 0; i < listReceiveIP.Count; i++)
|
| | | { |
| | | FindGateWaySocket.BeginSend(new System.Net.IPEndPoint(System.Net.IPAddress.Parse(listReceiveIP[i]), 7624), broadBytes); |
| | | } |
| | | //广播发送 |
| | | if (broadcastIpAddress.ToString() != "0.0.0.0") |
| | | { |
| | | FindGateWaySocket.BeginSend(new System.Net.IPEndPoint(broadcastIpAddress, 7624), broadBytes); |
| | | } |
| | | System.Threading.Thread.Sleep(1000); |
| | | } |
| | | } |
| | | catch { } |
| | |
| | | HomeId = homeID, |
| | | IsMainGateWay = isMainGateWay, |
| | | GwId = id, |
| | | GatewayOnlineFlage = true, |
| | | PubKey = pubKey |
| | | }; |
| | | //通讯到网关列表 |
| | |
| | | var gateWay = ZbGateway.GateWayList.Find(obj => obj.GwId == zbGateWay.GwId); |
| | | if (gateWay == null) |
| | | {
|
| | | zbGateWay.OnlineTime = DateTime.Now; |
| | | ZbGateway.GateWayList.Add(zbGateWay); |
| | | await zbGateWay.StartLocalMqtt(ipAddress); |
| | | } |
| | |
| | | { |
| | | gateWay.IsVirtual = false; |
| | | //将该网关标识为【可搜索到,即:在线】 |
| | | gateWay.GatewayOnlineFlage = true; |
| | | gateWay.OnlineTime = DateTime.Now; |
| | | if (gateWay.GwIP != ipAddress) |
| | | { |
| | | await gateWay.DisConnectLocalMqttClient("2"); |
| | |
| | | } |
| | | list.Clear(); |
| | | oldHomeID = Shared.Common.Config.Instance.HomeId; |
| | | |
| | | var tempBytes = System.Text.Encoding.UTF8.GetBytes(Shared.Common.Config.Instance.HomeId); |
| | | System.Array.Copy(tempBytes, 0, broadBytes, 7, 36 < tempBytes.Length ? 36 : tempBytes.Length); |
| | | |
| | | //住宅中已经存在的网关如果局域网不存在,需要在当前住宅中虚拟一个网关ID相同的网关 |
| | | var gateWayFileList = Global.FileListByHomeId().FindAll(obj => obj.StartsWith("Gateway_")); |
| | | foreach (var filePath in gateWayFileList) |
| | |
| | | {
|
| | | return;
|
| | | }
|
| | | udpClient.Send(bytes, bytes.Length, iPEndPoint);
|
| | | int value = udpClient.Send(bytes, bytes.Length, iPEndPoint);
|
| | | //调试用 |
| | | if (Shared.Phone.UserCenter.UserCenterResourse.HideOption.WriteSocketReceiveDataToFile == 1) |
| | | { |
| | | Shared.Phone.UserCenter.HdlLogLogic.Current.WriteLog(3, "发送长度(BeginSendTo):" + value + " 发送内容:" + System.Text.Encoding.UTF8.GetString(bytes)); |
| | | } |
| | | } |
| | | catch (Exception ex) |
| | | {
|