| | |
| | | { |
| | | 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 { } |
| | |
| | | { |
| | | try |
| | | { |
| | | if (FindGateWaySocket.busSocket == null || FindGateWaySocket.busSocket.Available <= 0) |
| | | if (FindGateWaySocket.udpClient == null || FindGateWaySocket.udpClient.Available <= 0) |
| | | { |
| | | System.Threading.Thread.Sleep(500); |
| | | continue; |
| | | } |
| | | var bytes = new byte[1024]; |
| | | var len = FindGateWaySocket.busSocket.Receive(bytes, bytes.Length, System.Net.Sockets.SocketFlags.None); |
| | | }
|
| | | var ipEndPoint = new System.Net.IPEndPoint(0, 0); |
| | | var bytes = FindGateWaySocket.udpClient.Receive(ref ipEndPoint); |
| | | if (bytes[43] == 0xA2) |
| | | { |
| | | //广播回复网关的基本信息处理 |
| | |
| | | HomeId = homeID, |
| | | IsMainGateWay = isMainGateWay, |
| | | GwId = id, |
| | | GatewayOnlineFlage = true, |
| | | PubKey = pubKey |
| | | }; |
| | | //通讯到网关列表 |
| | |
| | | {
|
| | | listReceiveIP.Add(ipAddress); |
| | | } |
| | | //2020.08.07追加:即使空的住宅id也建立链接,对应手动恢复网关出厂设置时,因为没有解绑,但是住宅id为空的问题 |
| | | else if (Shared.Phone.UserCenter.HdlGatewayLogic.Current.HomeIdIsEmpty(homeID) == true)
|
| | | {
|
| | | listReceiveIP.Add(ipAddress);
|
| | | } |
| | | } |
| | | //网关列表存储处理 |
| | | 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"); |
| | |
| | | } |
| | | } |
| | | } |
| | | catch { } |
| | | catch (Exception ex) |
| | | {
|
| | | //调试用
|
| | | if (Shared.Phone.UserCenter.UserCenterResourse.HideOption.WriteSocketReceiveDataToFile == 1)
|
| | | {
|
| | | Shared.Phone.UserCenter.HdlLogLogic.Current.WriteLog(3, "Receive异常\r\n" + ex.Message);
|
| | | } |
| | | } |
| | | } |
| | | }) |
| | | { IsBackground = true }.Start(); |
| | |
| | | //最后再断开mqtt连接 |
| | | for (int i = 0; i < list.Count; i++) |
| | | { |
| | | list[i].DisConnectLocalMqttClient("1"); |
| | | list[i]?.DisConnectLocalMqttClient("1"); |
| | | } |
| | | 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) |
| | |
| | | public static class FindGateWaySocket |
| | | { |
| | | //本地Socket |
| | | public static Socket busSocket; |
| | | public static UdpClient udpClient; |
| | | |
| | | /// <summary> |
| | | /// 启动Socket接收和发送功能 |
| | | /// </summary> |
| | | /// <param name="port"></param> |
| | | public static void Start(int port = 7624) |
| | | public static void Start() |
| | | { |
| | | if (IsRunning) |
| | | if (udpClient!=null) |
| | | { |
| | | return; |
| | | } |
| | | //定义网络类型,数据连接类型和网络协议UDP |
| | | busSocket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); |
| | | busSocket.EnableBroadcast = true; |
| | | busSocket.ReceiveTimeout = 1000; |
| | | busSocket.SendTimeout = 1000; |
| | | busSocket.Bind(new IPEndPoint(IPAddress.Any, port)); |
| | | udpClient = new UdpClient { EnableBroadcast = true }; |
| | | } |
| | | |
| | | /// <summary> |
| | |
| | | { |
| | | try |
| | | { |
| | | busSocket?.Close(); |
| | | udpClient?.Close(); |
| | | } |
| | | catch { } |
| | | busSocket = null; |
| | | udpClient = null; |
| | | |
| | | Console.WriteLine("BusSocket关闭成功!"); |
| | | } |
| | | |
| | | /// <summary> |
| | | /// 当前的Socket是否运行 |
| | | /// </summary> |
| | | public static bool IsRunning |
| | | { |
| | | get |
| | | { |
| | | return busSocket == null ? false : true; |
| | | } |
| | | } |
| | | |
| | | /// <summary> |
| | |
| | | { |
| | | try |
| | | { |
| | | if (IsRunning)
|
| | | if (udpClient == null)
|
| | | {
|
| | | busSocket.BeginSendTo(bytes, 0, bytes.Length, SocketFlags.None, iPEndPoint, new AsyncCallback(asyncEndSend), null);
|
| | | return;
|
| | | }
|
| | |
|
| | | 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) |
| | | {
|
| | |
| | | }
|
| | | } |
| | | } |
| | | |
| | | /// <summary> |
| | | /// 异步发送数据结束 |
| | | /// </summary> |
| | | /// <param name="iar"></param> |
| | | private static void asyncEndSend(IAsyncResult iar) |
| | | { |
| | | try |
| | | { |
| | | int bytesSent = busSocket.EndSendTo(iar); |
| | | } |
| | | catch (Exception ex)
|
| | | {
|
| | | //调试用
|
| | | if (Shared.Phone.UserCenter.UserCenterResourse.HideOption.WriteSocketReceiveDataToFile == 1)
|
| | | {
|
| | | Shared.Phone.UserCenter.HdlLogLogic.Current.WriteLog(3, "异步发送结束异常(asyncEndSend)\r\n" + ex.Message);
|
| | | } |
| | | } |
| | | } |
| | | } |
| | | |
| | | #endregion |
| | | } |
| | | } |
| | | } |