| | |
| | | {
|
| | | searchCount = 6;
|
| | | 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);
|
| | | }
|
| | | }
|
| | | }
|
| | | 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"); |
| | |
| | | getGatewayBaseInfo = new ZbGateway.GatewayBaseInfo |
| | | { |
| | | IpAddress = ipAddress, |
| | | Time = time, |
| | | GwName = gwName, |
| | | HomeId = homeID, |
| | | IsMainGateWay = isMainGateWay, |
| | |
| | | else |
| | | { |
| | | gateWay.PubKey = pubKey; |
| | | gateWay.getGatewayBaseInfo.Time = time; |
| | | gateWay.getGatewayBaseInfo.GwName = gwName; |
| | | gateWay.getGatewayBaseInfo.HomeId = homeID;
|
| | | await gateWay.StartLocalMqtt(ipAddress); |
| | |
| | | try |
| | | { |
| | | //定时检测远程连接情况 |
| | | await ZbGateway.StartRemoteMqtt();
|
| | | if (!ZbGateway.IsRemote) |
| | | { |
| | | System.Threading.Thread.Sleep(500); |
| | | continue; |
| | | } |
| | | var gateWayList = ZbGateway.GateWayList.FindAll(obj => obj.getGatewayBaseInfo.HomeId == Shared.Common.Config.Instance.HomeId); |
| | | |
| | | if (gateWayList.Count == 1) |
| | | { |
| | | gateWayList[0].getGatewayBaseInfo.IsMainGateWay = true; |
| | | } |
| | | else |
| | | { |
| | | if (gateWayList.Find((obj) => obj.getGatewayBaseInfo.IsMainGateWay) == null) |
| | | { |
| | | 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) |
| | | {
|
| | | for (int j = 0; j < gateWayList.Count; j++)
|
| | | {
|
| | | if (gateWayList[i].getGatewayBaseInfo.gwID == info.getGwData.GwId)
|
| | | {
|
| | | gateWayList[i].getGatewayBaseInfo.IsMainGateWay = true;
|
| | | }
|
| | | else
|
| | | {
|
| | | gateWayList[i].getGatewayBaseInfo.IsMainGateWay = false;
|
| | | }
|
| | | }
|
| | | break; |
| | | } |
| | | } |
| | | } |
| | | } |
| | | await ZbGateway.StartRemoteMqtt(); |
| | | } |
| | | catch { } |
| | | System.Threading.Thread.Sleep(2000); |
| | |
| | | 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> |