From 98c998ca98ee014266f65a517d672df0cd97f244 Mon Sep 17 00:00:00 2001
From: WJC <wjc@hdlchina.com.cn>
Date: 星期三, 25 三月 2020 17:51:18 +0800
Subject: [PATCH] 2020-03-25-1
---
ZigbeeApp/Shared/Phone/ZigBee/Common/Application.cs | 204 ++++++++++++++++++++++++++++++--------------------
1 files changed, 122 insertions(+), 82 deletions(-)
diff --git a/ZigbeeApp/Shared/Phone/ZigBee/Common/Application.cs b/ZigbeeApp/Shared/Phone/ZigBee/Common/Application.cs
index 3c400c1..89ec837 100755
--- a/ZigbeeApp/Shared/Phone/ZigBee/Common/Application.cs
+++ b/ZigbeeApp/Shared/Phone/ZigBee/Common/Application.cs
@@ -3,15 +3,16 @@
using System.Net;
using System.Net.Sockets;
using System.Text;
+
using Shared.Common;
using ZigBee.Device;
-using static ZigBee.Device.ZbGateway;
+
namespace ZigBee.Common
{
public static class Application
{
- static bool isInited;
+ public static bool isInited;
static bool isEncry;
/// <summary>
/// 鏄惁姝e湪鎼滅储缃戝叧
@@ -21,6 +22,10 @@
/// 鏂板彂鐜扮殑缃戝叧閫氱煡
/// </summary>
public static Action<ZigBee.Device.ZbGateway> NewGateWayAction;
+ /// <summary>
+ /// 涓婁竴娆$殑浣忓畢ID
+ /// </summary>
+ private static string oldHomeID = string.Empty;
public static void Init()
{
@@ -29,11 +34,11 @@
return;
}
isInited = true;
+
new System.Threading.Thread(async () =>
{
var gateWayList = new List<ZbGateway> { };
- var searchCount = 2;
- var oldHomeID = string.Empty;
+ var searchCount = 6;
var broadBytes = new byte[44];// byteHomeId[0] ,//H
broadBytes[0] = 0xfe;
broadBytes[1] = 0x29;
@@ -48,7 +53,9 @@
try
{
if (string.IsNullOrEmpty(Shared.Common.Config.Instance.HomeId))
- {
+ {
+ //浣忓畢ID涓虹┖鍙湁涓�绉嶅彲鑳藉氨鏄��鍑轰簡鐧诲綍锛岃繖閲岀殑涓婁竴娆′綇瀹匢D瑕佹竻绌�
+ oldHomeID = "?";
System.Threading.Thread.Sleep(1000);
continue;
}
@@ -57,13 +64,13 @@
{
//鍥犱负閭d竴鐬棿锛屾湁鍙兘mqtt浼氬姞鍥炴潵,鎵�浠ュ厛鍔犵紦瀛�
var list = new List<ZbGateway>();
- list.AddRange(GateWayList);
+ list.AddRange(ZbGateway.GateWayList);
//鐒跺悗娓呯┖鎺�
- GateWayList.Clear();
+ ZbGateway.GateWayList.Clear();
//鏈�鍚庡啀鏂紑mqtt杩炴帴
for (int i = 0; i < list.Count; i++)
{
- list[i].DisConnect("1");
+ list[i].DisConnectLocalMqttClient("1");
}
list.Clear();
oldHomeID = Shared.Common.Config.Instance.HomeId;
@@ -88,43 +95,48 @@
}
}
- var broadcastIpAddress = new Shared.Net.NetWiFi().BroadcastIpAddress;
-
- //缃戝叧閫氳澶勭悊
-
- if (0 < gateWayList.Count)
- {
- searchCount = 2;
- gateWayList.Clear();
- var aa = GateWayList;
-
- //鍏抽棴杩滅▼鏈嶅姟鍣�
- if (RemoteMqttClient!=null&& RemoteMqttClient.IsConnected==true)
- {
- await RemoteMqttClient?.DisconnectAsync();
- System.Console .WriteLine ($"鏀跺埌缃戝叧杩滅▼杩炴帴涓诲姩鏂紑_{System.DateTime.Now.ToString()}");
- }
- }
- else
+ var broadcastIpAddress = new Shared.Net.NetWiFi().BroadcastIpAddress;
+
+
+ if (Shared.Application.IsWifi)
{
- if (searchCount <= 0)
+ if (0 < gateWayList.Count)
{
- //杩滅▼閫氳杩炴帴锛岃繛鎺ヤ簯绔湇鍔″櫒
- await StartCloudMqtt();
- }
- }
+ searchCount = 6;
+ ZbGateway.IsRemote = false;
+ }
+ else
+ {
+ if (searchCount < 0)
+ {
+ ZbGateway.IsRemote = true;
+ }
+ }
+ }
+ else
+ {
+ ZbGateway.IsRemote = true;
+ for (int i = 0; i < ZbGateway.GateWayList.Count; i++)
+ {
+ ZbGateway.GateWayList[i].DisConnectLocalMqttClient("1");
+ }
+ }
+
searchCount--;
- //姣�0.5绉掑箍鎾彂鐜颁竴娆$綉鍏筹紝鍏�2s
- int count = 4;
+ #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++){
+ for (int i = 0; i < gateWayList.Count; i++)
+ {
FindGateWaySocket.BeginSend(new System.Net.IPEndPoint(System.Net.IPAddress.Parse(gateWayList[i].getGatewayBaseInfo.IpAddress), 7624), broadBytes);
}
//骞挎挱鍙戦��
@@ -133,9 +145,10 @@
FindGateWaySocket.BeginSend(new System.Net.IPEndPoint(broadcastIpAddress, 7624), broadBytes);
}
}
- catch (Exception e)
- {
- System.Console.WriteLine("===" + e.Message);
+ catch
+ {
+ //鍏抽棴Socket,涓嬫鍙戦�佷細鑷姩杩炴帴
+ FindGateWaySocket.Stop();
}
System.Threading.Thread.Sleep(500);
}
@@ -171,7 +184,6 @@
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
{
getGatewayBaseInfo = new ZbGateway.GatewayBaseInfo
@@ -192,25 +204,25 @@
//缃戝叧鍖归厤褰撳墠浣忓畢涓埌缃戝叧
if (Shared.Common.Config.Instance.HomeId == homeID)
{
- gateWayList.Add(zbGateWay);
+ gateWayList.Add(zbGateWay);
}
//UI鐣岄潰姝e湪鎼滅储锛屼笉蹇呴厤褰撳墠浣忓畢鍒板埌缃戝叧姝ゆ椂涔熼�氳
else if (IsSearchingGateway)
{
- gateWayList.Add(zbGateWay);
+ gateWayList.Add(zbGateWay);
}
- //缃戝叧涓埌浣忓畢ID涓虹┖姝ゆ椂涔熼�氳
- else if (homeID == string.Empty)
- {
- gateWayList.Add(zbGateWay);
- }
+ //缃戝叧涓埌浣忓畢ID涓虹┖姝ゆ椂涔熼�氳 2020.01.14鍙樻洿锛氭病杩欎釜蹇呰,涓婇潰閭d釜鍙橀噺灏辫兘澶勭悊
+ //else if (homeID == string.Empty)
+ //{
+ // gateWayList.Add(zbGateWay);
+ //}
}
//缃戝叧鍒楄〃瀛樺偍澶勭悊
- var gateWay = GateWayList.Find(obj => obj.getGatewayBaseInfo != null && obj.getGatewayBaseInfo.gwID == zbGateWay.getGatewayBaseInfo.gwID);
+ var gateWay = ZbGateway.GateWayList.Find(obj => obj.getGatewayBaseInfo != null && obj.getGatewayBaseInfo.gwID == zbGateWay.getGatewayBaseInfo.gwID);
if (gateWay == null)
{
- await zbGateWay.StartLocalMqtt(ipAddress);
- GateWayList.Add(zbGateWay);
+ ZbGateway.GateWayList.Add(zbGateWay);
+ await zbGateWay.StartLocalMqtt(ipAddress);
NewGateWayAction?.Invoke(zbGateWay);
}
else
@@ -220,11 +232,11 @@
gateWay.GatewayOnlineFlage = true;
if (gateWay.getGatewayBaseInfo.IpAddress != ipAddress)
{
- gateWay.DisConnect("2");
- GateWayList.Remove(gateWay);
+ await gateWay.DisConnectLocalMqttClient("2");
+ ZbGateway.GateWayList.Remove(gateWay);
gateWay = zbGateWay;
- await zbGateWay.StartLocalMqtt(ipAddress);
- GateWayList.Add(gateWay);
+ ZbGateway.GateWayList.Add(gateWay);
+ await zbGateWay.StartLocalMqtt(ipAddress);
}
else
{
@@ -232,15 +244,15 @@
gateWay.getGatewayBaseInfo.Time = time;
gateWay.getGatewayBaseInfo.GwName = gwName;
gateWay.getGatewayBaseInfo.HomeId = homeID;
- await gateWay.StartLocalMqtt(ipAddress);
+ await gateWay.StartLocalMqtt(ipAddress);
}
//涓荤綉鍏宠缃�
if (isMainGateWay && oldHomeID == gateWay.getGatewayBaseInfo.HomeId)
{
- for (int i = 0; i < GateWayList.Count; i++)
+ for (int i = 0; i < ZbGateway.GateWayList.Count; i++)
{
- var gw = GateWayList[i];
+ var gw = ZbGateway.GateWayList[i];
//缃戝叧ID涓嶆槸褰撳墠鐨勭綉鍏筹紝鍒欐妸缃戝叧鍒楄〃涓叾浠栫綉鍏虫爣璁颁负涓嶆槸涓荤綉鍏�
if (gw.getGatewayBaseInfo.gwID != id && oldHomeID == gw.getGatewayBaseInfo.HomeId)
{
@@ -250,22 +262,35 @@
//鏍囪褰撳墠缃戝叧鏄富缃戝叧
gateWay.getGatewayBaseInfo.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 (Exception ex)
+ catch
{
- var mess = ex.Message;
+ //鍏抽棴Socket,涓嬫鍙戦�佷細鑷姩杩炴帴
+ FindGateWaySocket.Stop();
}
}
+ #endregion
}
- catch (Exception ex)
- {
- var mess = ex.Message;
- }
+ catch { }
System.Threading.Thread.Sleep(500);
-
}
})
{ IsBackground = true }.Start();
@@ -277,12 +302,14 @@
{
try
{
+ //瀹氭椂妫�娴嬭繙绋嬭繛鎺ユ儏鍐�
+ await ZbGateway.StartRemoteMqtt();
if (!ZbGateway.IsRemote)
{
System.Threading.Thread.Sleep(500);
continue;
}
- var gateWayList = GateWayList.FindAll(obj => obj.getGatewayBaseInfo.HomeId == Shared.Common.Config.Instance.HomeId);
+ var gateWayList = ZbGateway.GateWayList.FindAll(obj => obj.getGatewayBaseInfo.HomeId == Shared.Common.Config.Instance.HomeId);
if (gateWayList.Count == 1)
{
@@ -290,28 +317,33 @@
}
else
{
- for (int i = 0; i < gateWayList.Count; i++)
+ if (gateWayList.Find((obj) => obj.getGatewayBaseInfo.IsMainGateWay) == null)
{
- var gateWay = gateWayList[i];
- var info = await gateWay.GetZbGwInfoAsync();
- if (info == null || info.getGwData == null)
+ for (int i = 0; i < gateWayList.Count; i++)
{
- continue;
- }
- if (info.getGwData.IsDominant == 1)
- {
- gateWay.getGatewayBaseInfo.IsMainGateWay = true;
- for (int j = i + 1; j < gateWayList.Count; j++)
+ var gateWay = gateWayList[i];
+ var info = await gateWay.GetZbGwInfoAsync();
+ if (info == null || info.getGwData == null)
{
- gateWayList[j].getGatewayBaseInfo.IsMainGateWay = false;
- }
- break;
+ 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;
+ }
}
}
- }
- if (gateWayList.Find(obj => obj.getGatewayBaseInfo.IsMainGateWay == true) != null)
- {
- break;
}
}
catch { }
@@ -321,6 +353,14 @@
{ IsBackground = true }.Start();
}
+ /// <summary>
+ /// 閲嶆柊鎼滅储,娉ㄦ剰锛氳皟鐢ㄨ鏂规硶,鍒欎績浣垮叏閮ㄧ綉鍏崇殑mqtt鍏ㄩ儴鏂紑,鐒跺悗閲嶆柊鎼滅储(鍙兘浼氭湁1绉掑欢杩�)
+ /// </summary>
+ public static void ReSearch()
+ {
+ //涓轰簡閭d箞澶氬皯涓囧垎涔嬩竴鐨勫嚑鐜�,杩欓噷璁剧疆鎴愬埆鐨勫彟涓�绫荤殑鍊�
+ oldHomeID = "**";
+ }
/// <summary>
/// 鎺ユ敹澶勭悊UDP鏁版嵁鍖�
--
Gitblit v1.8.0