From 9f326f4000847e6167d8166fa2f6a66f53cb3734 Mon Sep 17 00:00:00 2001
From: 黄学彪 <hxb@hdlchina.com.cn>
Date: 星期四, 17 十二月 2020 09:07:13 +0800
Subject: [PATCH] 新云端Ver1.3
---
ZigbeeApp/Shared/Phone/ZigBee/Common/Application.cs | 301 +++++++++++++++++++++++++++----------------------
1 files changed, 164 insertions(+), 137 deletions(-)
diff --git a/ZigbeeApp/Shared/Phone/ZigBee/Common/Application.cs b/ZigbeeApp/Shared/Phone/ZigBee/Common/Application.cs
index b3f93e1..e549d19 100755
--- a/ZigbeeApp/Shared/Phone/ZigBee/Common/Application.cs
+++ b/ZigbeeApp/Shared/Phone/ZigBee/Common/Application.cs
@@ -18,7 +18,11 @@
/// <summary>
/// 鏄惁姝e湪鎼滅储缃戝叧
/// </summary>
- public static bool IsSearchingGateway = false;
+ public static bool IsSearchingGateway = false;
+ /// <summary>
+ /// 鍙戦�佺殑鏃堕棿闂撮殧(鍗曚綅:绉�)
+ /// </summary>
+ public static int SendTime = 3;
/// <summary>
/// 涓婁竴娆$殑浣忓畢ID
/// </summary>
@@ -64,78 +68,68 @@
{
new System.Threading.Thread(() =>
{
- var searchCount = 3;
- 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;
+ var broadBytes = new byte[44];
+ var waitCount = 0;
+
while (true)
{
try
- {
- //妫�娴嬭兘鍚﹀彂閫佸箍鎾�
- if (CheckCanSendBroadcast(broadBytes) == false)
- {
- continue;
+ {
+ //妫�娴嬭兘鍚﹀彂閫佸箍鎾� 0:涓嶈兘鍙戦�佸箍鎾� 1:鍙互鍙戦�佸箍鎾� 2:浣忓畢ID鍙樻洿
+ int result = CheckCanSendBroadcast();
+ if (result == 0)
+ {
+ continue;
+ }
+ if (result == 2)
+ {
+ //閲嶆柊鍒濆鍖栧箍鎾暟鎹�
+ broadBytes = new byte[44];
+ broadBytes[0] = 0xfe;
+ broadBytes[1] = 0x29;
+ broadBytes[2] = 0x00;
+ broadBytes[3] = 0x00;
+ broadBytes[4] = 0x00;
+ broadBytes[5] = 0x00;
+ broadBytes[6] = 0x00;
+ broadBytes[43] = 0x02;
+ var tempBytes = System.Text.Encoding.UTF8.GetBytes(oldHomeID);
+ System.Array.Copy(tempBytes, 0, broadBytes, 7, 36 < tempBytes.Length ? 36 : tempBytes.Length);
}
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(SendTime * 1000);
+
+ if (listReceiveIP.Count > 0)
+ {
+ waitCount = 0;
+ //灞�鍩熺綉鑳藉骞挎挱寰楀埌缃戝叧
+ ZbGateway.IsRemote = false;
+ //褰撶綉鍏崇殑杩炴帴鏂瑰紡鏀瑰彉鏃�,璁板綍褰撳墠鐨勮繛鎺ユ柟寮�
+ Shared.Phone.HdlGatewayLogic.Current.CheckGatewayByConnectChanged(Shared.Phone.GatewayConnectMode.WIFI);
}
else
{
+ waitCount++;
+ if (waitCount >= 2)
+ {
+ //涓ゆ閮芥悳涓嶅埌,鎵嶆爣璁颁负杩滅▼
+ continue;
+ }
+ waitCount = 0;
+ //灞�鍩熺綉骞挎挱涓嶅埌缃戝叧
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");
- }
- //闈瀢ifi鎯呭喌涓�,涓嶉渶瑕佸箍鎾�
- 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);
+ Shared.Phone.HdlGatewayLogic.Current.CheckGatewayByConnectChanged(Shared.Phone.GatewayConnectMode.Remote);
}
}
catch { }
@@ -167,7 +161,7 @@
}
var ipEndPoint = new System.Net.IPEndPoint(0, 0);
var bytes = FindGateWaySocket.udpClient.Receive(ref ipEndPoint);
- if (bytes[43] == 0xA2)
+ if (bytes.Length > 43 && bytes[43] == 0xA2)
{
//骞挎挱鍥炲缃戝叧鐨勫熀鏈俊鎭鐞�
var ipAddress = $"{bytes[3]}.{bytes[4]}.{bytes[5]}.{bytes[6]}";
@@ -190,7 +184,6 @@
HomeId = homeID,
IsMainGateWay = isMainGateWay,
GwId = id,
- GatewayOnlineFlage = true,
PubKey = pubKey
};
//閫氳鍒扮綉鍏冲垪琛�
@@ -206,19 +199,28 @@
{
listReceiveIP.Add(ipAddress);
}
+ //2020.08.07杩藉姞:鍗充娇绌虹殑浣忓畢id涔熷缓绔嬮摼鎺�,瀵瑰簲鎵嬪姩鎭㈠缃戝叧鍑哄巶璁剧疆鏃�,鍥犱负娌℃湁瑙g粦,浣嗘槸浣忓畢id涓虹┖鐨勯棶棰�
+ else if (Shared.Phone.HdlGatewayLogic.Current.HomeIdIsEmpty(homeID) == true)
+ {
+ listReceiveIP.Add(ipAddress);
+ }
}
//缃戝叧鍒楄〃瀛樺偍澶勭悊
var gateWay = ZbGateway.GateWayList.Find(obj => obj.GwId == zbGateWay.GwId);
if (gateWay == null)
{
+ //鍒锋柊缃戝叧鐨勫湪绾挎椂闂寸偣
+ Shared.Phone.HdlGatewayLogic.Current.RefreshGatewayOnlineTime(zbGateWay.GwId);
+
ZbGateway.GateWayList.Add(zbGateWay);
await zbGateWay.StartLocalMqtt(ipAddress);
}
else
{
- gateWay.IsVirtual = false;
- //灏嗚缃戝叧鏍囪瘑涓恒�愬彲鎼滅储鍒帮紝鍗筹細鍦ㄧ嚎銆�
- gateWay.GatewayOnlineFlage = true;
+ gateWay.IsVirtual = false;
+ //鍒锋柊缃戝叧鐨勫湪绾挎椂闂寸偣
+ Shared.Phone.HdlGatewayLogic.Current.RefreshGatewayOnlineTime(gateWay.GwId);
+
if (gateWay.GwIP != ipAddress)
{
await gateWay.DisConnectLocalMqttClient("2");
@@ -253,23 +255,23 @@
}
//娴嬭瘯鑳藉惁骞挎挱寰楀埌缃戝叧,閫氬父鎯呭喌涓嬩笉妫�娴�
- if (Shared.Phone.UserCenter.UserCenterResourse.HideOption.CheckCanReceiveGateway == 1)
+ if (Shared.Phone.HdlUserCenterResourse.HideOption.CheckCanReceiveGateway == 1)
{
- if (Shared.Phone.UserCenter.UserCenterResourse.DicReceiveGatewayTest == null)
+ if (Shared.Phone.HdlGatewayResourse.DicReceiveGatewayTest == null)
{
//闃叉寮傚父,铏界劧鍑犵巼寰堜綆
- Shared.Phone.UserCenter.UserCenterResourse.HideOption.CheckCanReceiveGateway = 0;
+ Shared.Phone.HdlUserCenterResourse.HideOption.CheckCanReceiveGateway = 0;
continue;
}
- if (Shared.Phone.UserCenter.UserCenterResourse.DicReceiveGatewayTest.ContainsKey(id) == false)
+ if (Shared.Phone.HdlGatewayResourse.DicReceiveGatewayTest.ContainsKey(id) == false)
{
if (gateWay == null)
{
- Shared.Phone.UserCenter.UserCenterResourse.DicReceiveGatewayTest[id] = zbGateWay;
+ Shared.Phone.HdlGatewayResourse.DicReceiveGatewayTest[id] = zbGateWay;
}
else
{
- Shared.Phone.UserCenter.UserCenterResourse.DicReceiveGatewayTest[id] = gateWay;
+ Shared.Phone.HdlGatewayResourse.DicReceiveGatewayTest[id] = gateWay;
}
}
}
@@ -277,19 +279,20 @@
else
{
//璋冭瘯鐢�
- if (Shared.Phone.UserCenter.UserCenterResourse.HideOption.WriteSocketReceiveDataToFile == 1)
+ if (Shared.Phone.HdlUserCenterResourse.HideOption.WriteSocketReceiveDataToFile == 1)
{
var data33 = Encoding.UTF8.GetString(bytes).Replace("锟�)", string.Empty).TrimStart('\0').TrimEnd('\0');
- Shared.Phone.UserCenter.HdlLogLogic.Current.WriteLog(3, data33);
+ Shared.Phone.HdlLogLogic.Current.WriteLog(3, data33);
}
- }
+ }
+ await System.Threading.Tasks.Task.Delay(20);
}
catch (Exception ex)
{
//璋冭瘯鐢�
- if (Shared.Phone.UserCenter.UserCenterResourse.HideOption.WriteSocketReceiveDataToFile == 1)
+ if (Shared.Phone.HdlUserCenterResourse.HideOption.WriteSocketReceiveDataToFile == 1)
{
- Shared.Phone.UserCenter.HdlLogLogic.Current.WriteLog(3, "Receive寮傚父\r\n" + ex.Message);
+ Shared.Phone.HdlLogLogic.Current.WriteLog(3, "Receive寮傚父\r\n" + ex.Message);
}
}
}
@@ -333,57 +336,60 @@
{
//涓轰簡閭d箞澶氬皯涓囧垎涔嬩竴鐨勫嚑鐜�,杩欓噷璁剧疆鎴愬埆鐨勫彟涓�绫荤殑鍊�
oldHomeID = "**";
- }
-
- /// <summary>
- /// 妫�娴嬭兘鍚﹀彂閫佸箍鎾�
- /// </summary>
- /// <returns></returns>
- private static bool CheckCanSendBroadcast(byte[] broadBytes)
- {
- if (string.IsNullOrEmpty(Config.Instance.HomeId))
- {
- //浣忓畢ID涓虹┖鍙湁涓�绉嶅彲鑳藉氨鏄��鍑轰簡鐧诲綍锛岃繖閲岀殑涓婁竴娆′綇瀹匢D瑕佹竻绌�
- oldHomeID = "?";
- System.Threading.Thread.Sleep(1000);
- return false;
- }
- //棣栨杩涘叆缃戝叧锛屽拰鍒囨崲浣忓畢浼氭竻闄ょ綉鍏冲垪琛紝閲嶆柊鎼滅储瀛樺偍
- if (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 = Config.Instance.HomeId;
- ZbGateway.GateWayList.Add(gateWay);
- }
- }
- }
- return true;
+ }
+
+ /// <summary>
+ /// 妫�娴嬭兘鍚﹀彂閫佸箍鎾�(0:涓嶈兘鍙戦�佸箍鎾� 1:鍙互鍙戦�佸箍鎾� 2:浣忓畢ID鍙樻洿)
+ /// </summary>
+ /// <returns></returns>
+ private static int CheckCanSendBroadcast()
+ {
+ if (string.IsNullOrEmpty(Config.Instance.HomeId))
+ {
+ //浣忓畢ID涓虹┖鍙湁涓�绉嶅彲鑳藉氨鏄��鍑轰簡鐧诲綍锛岃繖閲岀殑涓婁竴娆′綇瀹匢D瑕佹竻绌�
+ oldHomeID = "?";
+ System.Threading.Thread.Sleep(1000);
+ return 0;
+ }
+ //棣栨杩涘叆缃戝叧锛屽拰鍒囨崲浣忓畢浼氭竻闄ょ綉鍏冲垪琛紝閲嶆柊鎼滅储瀛樺偍
+ if (Config.Instance.HomeId == oldHomeID)
+ {
+ return 1;
+ }
+
+ //鍥犱负閭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();
+
+ //浣忓畢涓凡缁忓瓨鍦ㄧ殑缃戝叧濡傛灉灞�鍩熺綉涓嶅瓨鍦紝闇�瑕佸湪褰撳墠浣忓畢涓櫄鎷熶竴涓綉鍏矷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 = Config.Instance.HomeId;
+ ZbGateway.GateWayList.Add(gateWay);
+ }
+ }
+ //浣忓畢ID鍙樻洿
+ oldHomeID = Config.Instance.HomeId;
+
+ return 2;
}
#endregion
@@ -403,13 +409,24 @@
/// 鍚姩Socket鎺ユ敹鍜屽彂閫佸姛鑳�
/// </summary>
/// <param name="port"></param>
- public static void Start()
+ public static bool Start()
{
- if (udpClient!=null)
+ if (udpClient != null)
{
- return;
+ return true;
}
- udpClient = new UdpClient { EnableBroadcast = true };
+
+ //鑾峰彇鎵嬫満鐨剋ifi鍦板潃
+ var netwifi = new Shared.Net.NetWiFi();
+ string phoneIp = netwifi.GetIPAddress();
+ //"127.0.0.1"鏄簳灞傚簱閲岄潰璁剧疆鐨勫垵濮嬮粯璁ゅ��
+ if (phoneIp != "127.0.0.1")
+ {
+ udpClient = new UdpClient(new IPEndPoint(IPAddress.Parse(phoneIp), 7624));
+ udpClient.EnableBroadcast = true;
+ return true;
+ }
+ return false;
}
/// <summary>
@@ -431,22 +448,32 @@
/// 寮傛鍙戦�佹暟鎹�
/// </summary>
/// <param name="tempPacket"></param>
- public static void BeginSend(System.Net.IPEndPoint iPEndPoint, byte[] bytes)
+ public static void BeginSend(IPEndPoint iPEndPoint, byte[] bytes)
{
try
{
- if (udpClient == null)
+ if (Start() == false)
{
+ //璋冭瘯鐢�
+ if (Shared.Phone.HdlUserCenterResourse.HideOption.WriteSocketReceiveDataToFile == 1)
+ {
+ Shared.Phone.HdlLogLogic.Current.WriteLog(3, "udp New涓嶈捣鏉�");
+ }
return;
}
- udpClient.Send(bytes, bytes.Length, iPEndPoint);
+ int value = udpClient.Send(bytes, bytes.Length, iPEndPoint);
+ //璋冭瘯鐢�
+ if (Shared.Phone.HdlUserCenterResourse.HideOption.WriteSocketReceiveDataToFile == 1)
+ {
+ Shared.Phone.HdlLogLogic.Current.WriteLog(3, "鍙戦�侀暱搴�(BeginSendTo)锛�" + value + " 鍙戦�佸唴瀹�:" + System.Text.Encoding.UTF8.GetString(bytes));
+ }
}
catch (Exception ex)
{
//璋冭瘯鐢�
- if (Shared.Phone.UserCenter.UserCenterResourse.HideOption.WriteSocketReceiveDataToFile == 1)
+ if (Shared.Phone.HdlUserCenterResourse.HideOption.WriteSocketReceiveDataToFile == 1)
{
- Shared.Phone.UserCenter.HdlLogLogic.Current.WriteLog(3, "鍙戦�佸紓甯�(BeginSendTo)\r\n" + ex.Message);
+ Shared.Phone.HdlLogLogic.Current.WriteLog(3, "鍙戦�佸紓甯�(BeginSendTo)\r\n" + ex.Message);
}
}
}
--
Gitblit v1.8.0