From 412e0af11df2b1d7986adf002e7997d82344ab3e Mon Sep 17 00:00:00 2001
From: xm <1271024303@qq.com>
Date: 星期一, 13 七月 2020 09:47:40 +0800
Subject: [PATCH] 请合并最新版代码
---
ZigbeeApp/Shared/Phone/ZigBee/Common/Application.cs | 408 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1 files changed, 408 insertions(+), 0 deletions(-)
diff --git a/ZigbeeApp/Shared/Phone/ZigBee/Common/Application.cs b/ZigbeeApp/Shared/Phone/ZigBee/Common/Application.cs
new file mode 100644
index 0000000..e229871
--- /dev/null
+++ b/ZigbeeApp/Shared/Phone/ZigBee/Common/Application.cs
@@ -0,0 +1,408 @@
+锘縰sing System;
+using System.Collections.Generic;
+using System.Net;
+using System.Net.Sockets;
+using System.Text;
+
+using Shared.Common;
+using ZigBee.Device;
+
+namespace ZigBee.Common
+{
+ public static class Application
+ {
+ public static bool isInited;
+ /// <summary>
+ /// 鏄惁姝e湪鎼滅储缃戝叧
+ /// </summary>
+ public static bool IsSearchingGateway = false;
+ /// <summary>
+ /// 鏂板彂鐜扮殑缃戝叧閫氱煡
+ /// </summary>
+ public static Action<ZigBee.Device.ZbGateway> NewGateWayAction;
+ /// <summary>
+ /// 涓婁竴娆$殑浣忓畢ID
+ /// </summary>
+ private static string oldHomeID = string.Empty;
+
+ public static void Init()
+ {
+ if (isInited)
+ {
+ return;
+ }
+ isInited = true;
+
+ new System.Threading.Thread(async () =>
+ {
+ var gateWayList = new List<ZbGateway> { };
+ var searchCount = 6;
+ var broadBytes = new byte[44];// byteHomeId[0] ,//H
+ broadBytes[0] = 0xfe;
+ broadBytes[1] = 0x29;
+ broadBytes[2] = 0x00;
+ broadBytes[3] = 0x00;
+ broadBytes[4] = 0x00;
+ broadBytes[5] = 0x00;
+ broadBytes[6] = 0x00;
+ broadBytes[43] = 0x02;
+ while (true)
+ {
+ try
+ {
+ if (string.IsNullOrEmpty(Shared.Common.Config.Instance.HomeId))
+ {
+ //浣忓畢ID涓虹┖鍙湁涓�绉嶅彲鑳藉氨鏄��鍑轰簡鐧诲綍锛岃繖閲岀殑涓婁竴娆′綇瀹匢D瑕佹竻绌�
+ oldHomeID = "?";
+ System.Threading.Thread.Sleep(1000);
+ continue;
+ }
+ //棣栨杩涘叆缃戝叧锛屽拰鍒囨崲浣忓畢浼氭竻闄ょ綉鍏冲垪琛紝閲嶆柊鎼滅储瀛樺偍
+ if (Shared.Common.Config.Instance.HomeId != oldHomeID)
+ {
+ //鍥犱负閭d竴鐬棿锛屾湁鍙兘mqtt浼氬姞鍥炴潵,鎵�浠ュ厛鍔犵紦瀛�
+ var list = new List<ZbGateway>();
+ list.AddRange(ZbGateway.GateWayList);
+ //鐒跺悗娓呯┖鎺�
+ ZbGateway.GateWayList.Clear();
+ //鏈�鍚庡啀鏂紑mqtt杩炴帴
+ for (int i = 0; i < list.Count; i++)
+ {
+ 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);
+ //浣忓畢涓凡缁忓瓨鍦ㄧ殑缃戝叧濡傛灉灞�鍩熺綉涓嶅瓨鍦紝闇�瑕佸湪褰撳墠浣忓畢涓櫄鎷熶竴涓綉鍏矷D鐩稿悓鐨勭綉鍏�
+ var gateWayFileList = Global.FileListByHomeId().FindAll(obj => obj.StartsWith("Gateway_"));
+ foreach (var filePath in gateWayFileList)
+ {
+ var paths = filePath.Split('_');
+ if (paths.Length < 3)
+ continue;
+
+ var gateWay = ZbGateway.GateWayList.Find(obj => (obj != null) && (obj.GwId == paths[2]));
+ if (gateWay == null)
+ {
+ gateWay = new ZbGateway { IsVirtual = true };
+ gateWay.GwId = paths[2];
+ gateWay.HomeId = Shared.Common.Config.Instance.HomeId;
+ ZbGateway.GateWayList.Add(gateWay);
+ }
+ }
+ }
+
+ var broadcastIpAddress = new Shared.Net.NetWiFi().BroadcastIpAddress;
+
+ if (Shared.Application.IsWifi)
+ {
+ if (0 < gateWayList.Count)
+ {
+ 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");
+ }
+ }
+
+ searchCount--;
+
+ #region 1绉掓悳绱竴娆$綉鍏�
+ //姣�0.5绉掑箍鎾彂鐜颁竴娆$綉鍏筹紝鍏�1s
+ int count = 2;
+ new System.Threading.Thread(() =>
+ {
+ gateWayList.Clear();
+ while (0 < count--)
+ {
+ try
+ {
+ //鐐瑰鐐瑰彂閫�(鍏堝彂涓�鏉″凡鏈夌殑鐐规挱锛屽洖澶嶅嚑鐜囬珮涓�鐐癸級
+ for (int i = 0; i < gateWayList.Count; i++)
+ {
+ FindGateWaySocket.BeginSend(new System.Net.IPEndPoint(System.Net.IPAddress.Parse(gateWayList[i].GwIP), 7624), broadBytes);
+ }
+ //骞挎挱鍙戦��
+ if (broadcastIpAddress.ToString() != "0.0.0.0")
+ {
+ FindGateWaySocket.BeginSend(new System.Net.IPEndPoint(broadcastIpAddress, 7624), broadBytes);
+ }
+ }
+ catch
+ {
+ //鍏抽棴Socket,涓嬫鍙戦�佷細鑷姩杩炴帴
+ FindGateWaySocket.Stop();
+ }
+ System.Threading.Thread.Sleep(500);
+ }
+ })
+ { IsBackground = true }.Start();
+
+ while (0 < count)
+ {
+ try
+ {
+ if (FindGateWaySocket.busSocket == null)
+ {
+ System.Threading.Thread.Sleep(100);
+ continue;
+ }
+ var bytes = new byte[1024];
+
+ var len = FindGateWaySocket.busSocket.Receive(bytes, bytes.Length, System.Net.Sockets.SocketFlags.None);
+
+ if (bytes[43] == 0xA2)
+ {
+ //骞挎挱鍥炲缃戝叧鐨勫熀鏈俊鎭鐞�
+ var ipAddress = $"{bytes[3]}.{bytes[4]}.{bytes[5]}.{bytes[6]}";
+ var homeID = System.Text.Encoding.UTF8.GetString(bytes, 7, 36);
+ homeID = homeID.Replace('\0', ' ').Trim();
+ var isMainGateWay = bytes[44] == 1;
+ var time = (bytes[48] & 0xff) << 24 | (bytes[47] & 0xff) << 16 | (bytes[46] & 0xff) << 8 | (bytes[45] & 0xff);
+ var gwNameLength = bytes[49];
+ var gwName = System.Text.Encoding.UTF8.GetString(bytes, 50, gwNameLength);
+ var gwIdLength = bytes[49 + gwNameLength + 1];
+ var id = Encoding.UTF8.GetString(bytes, 49 + gwNameLength + 2, gwIdLength);
+ var pubKeyLengthByte0 = bytes[49 + gwNameLength + 1 + gwIdLength + 1];
+ var pubKeyLengthByte1 = bytes[49 + gwNameLength + 1 + gwIdLength + 2];
+ int pubKeyLength = ((pubKeyLengthByte1 & 0xff) << 8 | (pubKeyLengthByte0 & 0xff));
+ var pubKey = Encoding.UTF8.GetString(bytes, 49 + gwNameLength + 1 + gwIdLength + 2 + 1, pubKeyLength);
+ var zbGateWay = new ZbGateway
+ {
+ GwIP = ipAddress,
+ GwName = gwName,
+ HomeId = homeID,
+ IsMainGateWay = isMainGateWay,
+ GwId = id,
+ GatewayOnlineFlage = true,
+ PubKey = pubKey
+ };
+ //閫氳鍒扮綉鍏冲垪琛�
+ if (gateWayList.Find(obj => obj.GwId == zbGateWay.GwId) == null)
+ {
+ //缃戝叧鍖归厤褰撳墠浣忓畢涓埌缃戝叧
+ if (Shared.Common.Config.Instance.HomeId == homeID)
+ {
+ gateWayList.Add(zbGateWay);
+ }
+ //UI鐣岄潰姝e湪鎼滅储锛屼笉蹇呴厤褰撳墠浣忓畢鍒板埌缃戝叧姝ゆ椂涔熼�氳
+ else if (IsSearchingGateway)
+ {
+ gateWayList.Add(zbGateWay);
+ }
+ //缃戝叧涓埌浣忓畢ID涓虹┖姝ゆ椂涔熼�氳 2020.01.14鍙樻洿锛氭病杩欎釜蹇呰,涓婇潰閭d釜鍙橀噺灏辫兘澶勭悊
+ //else if (homeID == string.Empty)
+ //{
+ // gateWayList.Add(zbGateWay);
+ //}
+ }
+ //缃戝叧鍒楄〃瀛樺偍澶勭悊
+ var gateWay = ZbGateway.GateWayList.Find(obj => obj.GwId == zbGateWay.GwId);
+ if (gateWay == null)
+ {
+ ZbGateway.GateWayList.Add(zbGateWay);
+ await zbGateWay.StartLocalMqtt(ipAddress);
+ NewGateWayAction?.Invoke(zbGateWay);
+ }
+ else
+ {
+ gateWay.IsVirtual = false;
+ //灏嗚缃戝叧鏍囪瘑涓恒�愬彲鎼滅储鍒帮紝鍗筹細鍦ㄧ嚎銆�
+ gateWay.GatewayOnlineFlage = true;
+ if (gateWay.GwIP != ipAddress)
+ {
+ await gateWay.DisConnectLocalMqttClient("2");
+ ZbGateway.GateWayList.Remove(gateWay);
+ gateWay = zbGateWay;
+ ZbGateway.GateWayList.Add(gateWay);
+ await zbGateWay.StartLocalMqtt(ipAddress);
+ }
+ else
+ {
+ gateWay.PubKey = pubKey;
+ gateWay.GwName = gwName;
+ gateWay.HomeId = homeID;
+ await gateWay.StartLocalMqtt(ipAddress);
+ }
+
+ //涓荤綉鍏宠缃�
+ if (isMainGateWay && oldHomeID == gateWay.HomeId)
+ {
+ for (int i = 0; i < ZbGateway.GateWayList.Count; i++)
+ {
+ var gw = ZbGateway.GateWayList[i];
+ //缃戝叧ID涓嶆槸褰撳墠鐨勭綉鍏筹紝鍒欐妸缃戝叧鍒楄〃涓叾浠栫綉鍏虫爣璁颁负涓嶆槸涓荤綉鍏�
+ if (gw.GwId != id && oldHomeID == gw.HomeId)
+ {
+ gw.IsMainGateWay = false;
+ }
+ }
+ //鏍囪褰撳墠缃戝叧鏄富缃戝叧
+ gateWay.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
+ {
+ //鍏抽棴Socket,涓嬫鍙戦�佷細鑷姩杩炴帴
+ FindGateWaySocket.Stop();
+ }
+ }
+ #endregion
+ }
+ catch { }
+ System.Threading.Thread.Sleep(500);
+ }
+ })
+ { IsBackground = true }.Start();
+
+ ///杩滅▼涓荤綉鍏虫洿鏂�
+ new System.Threading.Thread(async () =>
+ {
+ while (true)
+ {
+ try
+ {
+ //瀹氭椂妫�娴嬭繙绋嬭繛鎺ユ儏鍐�
+ await ZbGateway.StartRemoteMqtt();
+ }
+ catch { }
+ System.Threading.Thread.Sleep(2000);
+ }
+ })
+ { IsBackground = true }.Start();
+ }
+
+ /// <summary>
+ /// 閲嶆柊鎼滅储,娉ㄦ剰锛氳皟鐢ㄨ鏂规硶,鍒欎績浣垮叏閮ㄧ綉鍏崇殑mqtt鍏ㄩ儴鏂紑,鐒跺悗閲嶆柊鎼滅储(鍙兘浼氭湁1绉掑欢杩�)
+ /// </summary>
+ public static void ReSearch()
+ {
+ //涓轰簡閭d箞澶氬皯涓囧垎涔嬩竴鐨勫嚑鐜�,杩欓噷璁剧疆鎴愬埆鐨勫彟涓�绫荤殑鍊�
+ oldHomeID = "**";
+ }
+
+ /// <summary>
+ /// 鎺ユ敹澶勭悊UDP鏁版嵁鍖�
+ /// </summary>
+ /// <typeparam name="T"></typeparam>
+ public static class FindGateWaySocket
+ {
+ //鏈湴Socket
+ public static Socket busSocket;
+
+ /// <summary>
+ /// 鍚姩Socket鎺ユ敹鍜屽彂閫佸姛鑳�
+ /// </summary>
+ /// <param name="port"></param>
+ public static void Start(int port = 7624)
+ {
+ if (IsRunning)
+ {
+ 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));
+ }
+
+ /// <summary>
+ /// 鍋滄Socket
+ /// </summary>
+ public static void Stop()
+ {
+ if (!IsRunning)
+ {
+ return;
+ }
+ try
+ {
+ busSocket.Close();
+ }
+ catch { }
+ busSocket = null;
+
+ Console.WriteLine("BusSocket鍏抽棴鎴愬姛!");
+ }
+
+ /// <summary>
+ /// 褰撳墠鐨凷ocket鏄惁杩愯
+ /// </summary>
+ public static bool IsRunning
+ {
+ get
+ {
+ return busSocket == null ? false : true;
+ }
+ }
+
+ /// <summary>
+ /// 寮傛鍙戦�佹暟鎹�
+ /// </summary>
+ /// <param name="tempPacket"></param>
+ public static void BeginSend(System.Net.IPEndPoint iPEndPoint, byte[] bytes)
+ {
+ try
+ {
+ Start(7624);
+ busSocket.BeginSendTo(bytes, 0, bytes.Length, SocketFlags.None, iPEndPoint, new AsyncCallback(asyncEndSend), null);
+ }
+ catch { }
+ }
+
+ /// <summary>
+ /// 寮傛鍙戦�佹暟鎹粨鏉�
+ /// </summary>
+ /// <param name="iar"></param>
+ private static void asyncEndSend(IAsyncResult iar)
+ {
+ try
+ {
+ int bytesSent = busSocket.EndSendTo(iar);
+ }
+ catch { }
+ }
+ }
+ }
+}
--
Gitblit v1.8.0