From 4f0ab0ad21ce450b7856d50f98322a7899361386 Mon Sep 17 00:00:00 2001
From: 黄学彪 <hxb@hdlchina.com.cn>
Date: 星期二, 22 九月 2020 10:30:01 +0800
Subject: [PATCH] 不要下载这个备份
---
ZigbeeApp/Shared/Phone/UserCenter/CommonBase/Logic/HdlWifiLogic.cs | 429 ++++++++++++++++++++++++++++++++++++++++++++++++++++-
1 files changed, 420 insertions(+), 9 deletions(-)
diff --git a/ZigbeeApp/Shared/Phone/UserCenter/CommonBase/Logic/HdlWifiLogic.cs b/ZigbeeApp/Shared/Phone/UserCenter/CommonBase/Logic/HdlWifiLogic.cs
index d78f64a..c7bc442 100755
--- a/ZigbeeApp/Shared/Phone/UserCenter/CommonBase/Logic/HdlWifiLogic.cs
+++ b/ZigbeeApp/Shared/Phone/UserCenter/CommonBase/Logic/HdlWifiLogic.cs
@@ -1,5 +1,6 @@
锘縰sing System;
using System.Collections.Generic;
+using System.Security.Cryptography;
using System.Text;
namespace Shared.Phone.UserCenter
@@ -31,6 +32,40 @@
}
/// <summary>
+ /// 褰撳墠鏄惁鏄綉鍏崇儹鐐�
+ /// </summary>
+ public bool IsGatewayHotspot
+ {
+ get
+ {
+ string myId = this.SSID;
+
+ if (myId != null && myId.StartsWith("hdlZigbeeGW") == true)
+ {
+ return true;
+ }
+ return false;
+ }
+ }
+
+ /// <summary>
+ /// 褰撳墠鏄惁鏄疢ini缃戝叧鐑偣
+ /// </summary>
+ public bool IsMiniGatewayHotspot
+ {
+ get
+ {
+ string myId = this.SSID;
+
+ if (myId != null && myId.StartsWith("hdlZigbeeGW-") == true)
+ {
+ return true;
+ }
+ return false;
+ }
+ }
+
+ /// <summary>
/// 褰撳墠wifi鐨凷SID,涔熷彨Wifi鐨勫悕瀛�(鍙栦笉鍒版椂,浼氳繑鍥瀗ull)
/// </summary>
public string SSID
@@ -38,7 +73,12 @@
get
{
#if iOS
- return string.Empty;
+ string ssiD = Shared.WiimuUPnP.SSID;
+ if (string.IsNullOrEmpty(ssiD) == false && ssiD.StartsWith("\"") && ssiD.EndsWith("\""))
+ {
+ ssiD = ssiD.Substring(1, ssiD.Length - 2);
+ }
+ return ssiD;
#endif
#if Android
string ssiD = null;
@@ -54,9 +94,9 @@
{
System.Threading.Thread.Sleep(50);
count++;
- if (count == 60)
+ if (count == 20)
{
- //3绉掕秴鏃�
+ //1绉掕秴鏃�
return null;
}
}
@@ -74,6 +114,376 @@
/// </summary>
private Com.Mediatek.Elian.ElianNative hdlWiFi = null;
#endif
+ /// <summary>
+ /// TCP鍗忚
+ /// </summary>
+ private System.Net.Sockets.TcpClient tcpClient = null;
+ /// <summary>
+ /// 閾炬帴娴佸璞�
+ /// </summary>
+ private System.Net.Sockets.NetworkStream networkStream = null;
+ /// <summary>
+ /// Ap妯″紡缃戝叧杩斿洖鐨勭閽�
+ /// </summary>
+ private string randDesKey = null;
+ /// <summary>
+ /// 鏄惁宸茬粡娣诲姞缃戠粶鍙樺寲鐩戝惉
+ /// </summary>
+ private bool hadAddListenNetWork = false;
+
+ /// <summary>
+ /// 鑳藉惁璁块棶http
+ /// </summary>
+ private bool m_CanAccessHttp = true;
+ /// <summary>
+ /// 涓婁竴娆¤闂甴ttp鐨勬椂闂�
+ /// </summary>
+ private DateTime oldAccessHttpTime = DateTime.Now;
+ /// <summary>
+ /// 鑳藉惁璁块棶http
+ /// </summary>
+ public bool CanAccessHttp
+ {
+ set
+ {
+ m_CanAccessHttp = value;
+ oldAccessHttpTime = DateTime.Now;
+ }
+ get
+ {
+ //8绉掑悗鍏佽鍐嶆璁块棶
+ if (m_CanAccessHttp == true || (DateTime.Now - oldAccessHttpTime).TotalMilliseconds > 8 * 1000)
+ {
+ oldAccessHttpTime = DateTime.Now;
+ return true;
+ }
+ return false;
+ }
+ }
+ /// <summary>
+ /// 鍙樻洿缃戠粶鏃剁殑浜嬩欢
+ /// </summary>
+ private string changedNetworkTime = string.Empty;
+ /// <summary>
+ /// 褰撳墠 wifi 鐨�
+ /// </summary>
+ private string oldSsid = string.Empty;
+
+ #endregion
+
+ #region 鈻� Ap妯″紡鐨凾CP閾炬帴____________________
+
+ /// <summary>
+ /// 鎵撳紑Ap妯″紡鐨凾CP閾炬帴(鏈夐敊璇椂锛岀洿鎺ヨ繑鍥為敊璇秷鎭�傛病鏈夐敊璇椂锛岃繑鍥瀗ull)
+ /// </summary>
+ /// <returns></returns>
+ public string StartApTcpConnection()
+ {
+ try
+ {
+ //TCP杩炴帴
+ tcpClient = new System.Net.Sockets.TcpClient();
+ tcpClient.Connect("192.168.8.1", 5000);
+ networkStream = tcpClient.GetStream();
+ networkStream.ReadTimeout = 1000;
+ //鍔犲瘑鏁版嵁
+ string sendDataMsg = "hdl1985.";
+ string DefaultdesKey = "hdl1985.";
+ //44bytes 鍔犲瘑鏁版嵁 DES鍔犲瘑 base64瀵嗘枃
+ var sendData = Encoding.UTF8.GetBytes(DesEncrypt(sendDataMsg, DefaultdesKey));
+ //灏嗘暟鎹啓鍏ョ綉缁滄祦
+ networkStream.Write(sendData, 0, sendData.Length);
+
+ System.Threading.Thread.Sleep(500);
+ //2.鎺ユ敹鐘舵��,闀垮害<1024瀛楄妭
+ byte[] bytes = new Byte[1024];
+ int length = networkStream.Read(bytes, 0, bytes.Length);
+ if (length == 0)
+ {
+ //閫氫俊閾炬帴澶辫触
+ return Language.StringByID(R.MyInternationalizationString.uCommunicationLinkFail);
+ }
+ string data = Encoding.UTF8.GetString(bytes, 0, length);
+ //缃戝叧杩斿洖鐨勭閽�
+ this.randDesKey = DESDecrypt(data, DefaultdesKey);
+
+ return null;
+ }
+ catch (Exception ex)
+ {
+ HdlLogLogic.Current.WriteLog(ex);
+ this.CloseApTcpConnection();
+ //閫氫俊閾炬帴澶辫触
+ return Language.StringByID(R.MyInternationalizationString.uCommunicationLinkFail);
+ }
+ }
+
+ /// <summary>
+ /// Ap妯″紡 鍙戦�佸瘑鐮佸埌瀹跺涵鐨勮矾鐢卞櫒
+ /// </summary>
+ /// <param name="ssid"></param>
+ /// <param name="password"></param>
+ /// <returns></returns>
+ public bool SendApHomeWifiPassword(string ssid, string password)
+ {
+ try
+ {
+ //鍔犲瘑鏁版嵁
+ string data2 = "ssid:" + ssid + "," + "pwd:" + password;
+ var sendData2 = Encoding.UTF8.GetBytes(DesEncrypt(data2, randDesKey));
+ //灏嗘暟鎹啓鍏ョ綉缁滄祦
+ networkStream.Write(sendData2, 0, sendData2.Length);
+ return true;
+ }
+ catch (Exception ex)
+ {
+ HdlLogLogic.Current.WriteLog(ex);
+ return false;
+ }
+ }
+
+ /// <summary>
+ /// 鍏抽棴Ap妯″紡鐨則cp閾炬帴
+ /// </summary>
+ public void CloseApTcpConnection()
+ {
+ try
+ {
+ //鍏抽棴瀹㈡埛绔�
+ tcpClient?.Close();
+ tcpClient = null;
+ networkStream = null;
+ randDesKey = null;
+ }
+ catch (Exception ex)
+ {
+ HdlLogLogic.Current.WriteLog(ex);
+ }
+ }
+
+ #endregion
+
+ #region 鈻� 鍔犲瘑瑙e瘑___________________________
+
+ /// <summary>
+ /// //鍔犲瘑鏁版嵁
+ /// </summary>
+ /// <param name="pToDesEncrypt"></param>
+ /// <param name="sKey">DES绉橀挜</param>
+ /// <returns></returns>
+ private string DesEncrypt(string pToDesEncrypt, string sKey)
+ {
+ // 鍒涘缓des鍔犲瘑娌�
+ using (DESCryptoServiceProvider des = new DESCryptoServiceProvider())
+ {
+ // 寰楀埌UTF-8鐨勬暟鎹簮
+ byte[] inputByteArray = Encoding.UTF8.GetBytes(pToDesEncrypt);
+ // 璁剧疆key鍜宨v瀵嗛挜
+ des.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
+ des.IV = ASCIIEncoding.ASCII.GetBytes(sKey);
+ //鐢变簬java鏄娇鐢ㄩ粯璁ゅ姞瀵嗘ā寮忥紝C#榛樿鏄疌BC锛岄渶瑕佷慨鏀逛负ECB
+ des.Mode = CipherMode.CBC;
+ des.Padding = PaddingMode.PKCS7;
+ // 鍒涘缓鏁版嵁娴佸苟杩涜鍔犲瘑
+ var ms = new System.IO.MemoryStream();
+ using (CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write))
+ {
+ cs.Write(inputByteArray, 0, inputByteArray.Length);
+ cs.FlushFinalBlock();
+ cs.Close();
+ }
+ // 鍏抽棴鏁版嵁娴�
+ ms.Close();
+ return Convert.ToBase64String(ms.ToArray());//base64瀵嗘枃
+ }
+ }
+
+ /// <summary>
+ /// 瑙e瘑
+ /// </summary>
+ /// <param name="pToDecrypt"></param>
+ /// <param name="key">瑙e瘑瀵嗗寵</param>
+ /// <returns></returns>
+ private string DESDecrypt(string pToDecrypt, string key = null)
+ {
+ byte[] inputByteArray = Convert.FromBase64String(pToDecrypt);
+ using (DESCryptoServiceProvider des = new DESCryptoServiceProvider())
+ {
+
+ des.Key = ASCIIEncoding.ASCII.GetBytes(key);
+ des.IV = ASCIIEncoding.ASCII.GetBytes(key);
+
+ var ms = new System.IO.MemoryStream();
+ using (CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write))
+ {
+ cs.Write(inputByteArray, 0, inputByteArray.Length);
+ cs.FlushFinalBlock();
+ cs.Close();
+ }
+ string str = Encoding.UTF8.GetString(ms.ToArray());
+ ms.Close();
+ return str;
+ }
+ }
+
+ #endregion
+
+ #region 鈻� 鐩戝惉缃戠粶鍙樺寲_______________________
+
+ /// <summary>
+ /// 寮�鍚洃鍚綉缁�
+ /// </summary>
+ public void StartListenNetWork()
+ {
+ if (hadAddListenNetWork == true)
+ {
+ return;
+ }
+ hadAddListenNetWork = true;
+
+#if Android
+ //寮�鍚洃鍚畨鍗撶綉缁滃彉鍖�
+ BaseActivity.NetworkStateChanged += (value) =>
+ {
+ //缃戠粶鍙樺寲
+ this.NetworkStateChanged(value);
+ };
+#endif
+
+#if iOS
+ //寮�鍚洃鍚琁OS缃戠粶鍙樺寲
+ GateWay.Ios.Reachability.ReachabilityChanged += (sender, e) =>
+ {
+ var internetStatus = GateWay.Ios.Reachability.InternetConnectionStatus();
+ if (internetStatus == GateWay.Ios.NetworkStatus.NotReachable)//娌℃湁缃戠粶杩炴帴 0
+ {
+ //缃戠粶鍙樺寲
+ this.NetworkStateChanged(0);
+ }
+ else if (internetStatus == GateWay.Ios.NetworkStatus.ReachableViaCarrierDataNetwork)//3,4G鐨勭綉缁滆繛鎺� 1
+ {
+ //缃戠粶鍙樺寲
+ this.NetworkStateChanged(1);
+ }
+ else if (internetStatus == GateWay.Ios.NetworkStatus.ReachableViaWiFiNetwork) //wifi鐨勭綉缁滆繛鎺� 2
+ {
+ //缃戠粶鍙樺寲
+ this.NetworkStateChanged(2);
+ }
+ };
+#endif
+ }
+
+ /// <summary>
+ /// 鐩戝惉瀹夊崜缃戠粶鍙樺寲
+ /// </summary>
+ /// <param name="value">0:娌℃湁缃戠粶 1:4G 2:wifi</param>
+ public void NetworkStateChanged(int value)
+ {
+ if (Common.Config.Instance.HomeId == string.Empty)
+ {
+ //鍦ㄧ櫥褰曠晫闈笉澶勭悊
+ return;
+ }
+#if iOS
+ var nowSsid = this.SSID;
+ if (this.oldSsid == nowSsid)
+ {
+ //濡傛灉褰撳墠 wifi 鐨勫悕瀛椾竴鏍凤紝鍒欎笉澶勭悊
+ ZigBee.Common.Application.FindGateWaySocket.Start();
+ return;
+ }
+ this.oldSsid = nowSsid;
+#endif
+ //娌℃湁缃戠粶
+ if (value == 0)
+ {
+ this.CanAccessHttp = false;
+ //鍏抽棴Socket
+ ZigBee.Common.Application.FindGateWaySocket.Stop();
+ }
+ //WIFI鎴栬��4G鏃�
+ if (value == 1 || value == 2)
+ {
+ //鍏佽鑱旂綉
+ this.CanAccessHttp = true;
+ this.changedNetworkTime = DateTime.Now.ToString("yyyyMMdd HH.mm.ss");
+ //褰撳墠鎵ц姝ょ嚎绋嬬殑鏃堕棿
+ string thisThreadTime = this.changedNetworkTime;
+
+ HdlThreadLogic.Current.RunThread(() =>
+ {
+ //涓嶇濡備綍,閮借鏄剧ず杞湀
+ ProgressBar.Show();
+
+ //妫�娴嬫槸鍚﹀凡缁忓畬鎴愯处鍙蜂俊鎭垵濮嬪寲
+ if (UserCenterResourse.UserInfo.InitUserInfoSuccess == false
+ && Common.Config.Instance.HomeId != string.Empty)
+ {
+ //閲嶆柊鍒濆鍖栬处鍙蜂俊鎭�
+ UserCenterLogic.ReInitUserAccoutInfo();
+ //鍥犱负涓婇潰杩欎釜涓滆タ鑰楁椂铔箙鐨�,鎵�浠ュ鐞嗗畬涔嬪悗杩涜鍒ゆ柇
+ if (thisThreadTime != this.changedNetworkTime)
+ {
+ //鍦ㄥ鐞嗙殑杩囩▼涓�,鍒囨崲浜嗙綉缁�,鎶婁笅闈㈢殑澶勭悊浜ょ粰鍙﹀鐨勭嚎绋�
+ return;
+ }
+ }
+ //閲嶆柊鍒濆鍖朣ocket
+ ZigBee.Common.Application.FindGateWaySocket.Stop();
+ System.Threading.Thread.Sleep(100);
+
+ //鏂帀鏈湴杩炴帴
+ HdlGatewayLogic.Current.ClearAllRealGatewayConection(true);
+ //鏂帀杩滅▼
+ ZigBee.Device.ZbGateway.DisConnectRemoteMqttClient();
+
+ //濡傛灉鏄痺ifi
+ if (value == 2)
+ {
+ ZigBee.Common.Application.FindGateWaySocket.Start();
+ //鍊熺敤涓�涓嬭繖涓彉閲�(妫�娴嬭兘鍚﹀箍鎾埌缃戝叧)
+ UserCenterResourse.DicReceiveGatewayTest = new Dictionary<string, ZigBee.Device.ZbGateway>();
+ UserCenterResourse.HideOption.CheckCanReceiveGateway = 1;
+ //鍥哄畾绛変釜3绉掑惂
+ System.Threading.Thread.Sleep(2500);
+ if (thisThreadTime != this.changedNetworkTime)
+ {
+ //鍙樻洿浜嗙綉缁�,浜ょ敱鍏朵粬绾跨▼澶勭悊
+ return;
+ }
+ UserCenterResourse.HideOption.CheckCanReceiveGateway = 0;
+ System.Threading.Thread.Sleep(500);
+
+ bool canReceiveGw = false;
+ foreach (var gateway in UserCenterResourse.DicReceiveGatewayTest.Values)
+ {
+ if (gateway.HomeId == Common.Config.Instance.Home.Id)
+ {
+ //鑳藉鎼滅储寰楀埌缃戝叧
+ canReceiveGw = true;
+ break;
+ }
+ }
+ UserCenterResourse.DicReceiveGatewayTest = null;
+ //璁剧疆杩滅▼杩炴帴鐨勫垵濮嬪��
+ ZigBee.Device.ZbGateway.IsRemote = canReceiveGw == false;
+ if (canReceiveGw == false)
+ {
+ //濡傛灉鏄繙绋嬬殑璇�,杩藉姞绛夊緟鏃堕棿(鎬诲叡5绉�)
+ System.Threading.Thread.Sleep(2000);
+ }
+ }
+ else if (value == 1)
+ {
+ //濡傛灉鏄�4G鐨勮瘽,鐩存帴绛夊緟
+ System.Threading.Thread.Sleep(4500);
+ }
+ ProgressBar.Close();
+
+ }, ShowErrorMode.NO);
+ }
+ }
#endregion
@@ -87,14 +497,14 @@
/// <param name="wifiPsw">wifi瀵嗙爜</param>
/// <param name="p2">鎴戜篃涓嶇煡閬撹繖涓槸浠�涔堜笢瑗�</param>
/// <returns></returns>
- public bool StartSmartConnection(string wifiName, string wifiPsw, string p2 = "1")
+ public bool StartSmartConnection(string wifiName, string wifiPsw, string p2 = "")
{
if (hdlWiFi == null)
{
- Com.Mediatek.Elian.ElianNative.LoadLib();
+ var result2 = Com.Mediatek.Elian.ElianNative.LoadLib();
//鍒濆鍖朩i-Fi閰嶇綉
this.hdlWiFi = new Com.Mediatek.Elian.ElianNative();
- hdlWiFi.InitSmartConnection(null, 1, 1);
+ var value = hdlWiFi.InitSmartConnection(null, 1, 1);
}
//寮�濮媁i-Fi閰嶇綉
var result = hdlWiFi.StartSmartConnection(wifiName, wifiPsw, p2);
@@ -109,7 +519,8 @@
if (hdlWiFi != null)
{
//鍋滄Wi-Fi閰嶇綉
- hdlWiFi.StopSmartConnection();
+ var result = hdlWiFi.StopSmartConnection();
+ hdlWiFi = null;
}
}
#endif
@@ -121,10 +532,10 @@
/// <param name="wifiPsw">wifi瀵嗙爜</param>
/// <param name="p2">鎴戜篃涓嶇煡閬撹繖涓槸浠�涔堜笢瑗�</param>
/// <returns></returns>
- public bool StartSmartConnection(string wifiName, string wifiPsw, string p2 = "1")
+ public bool StartSmartConnection(string wifiName, string wifiPsw, string p2 = "")
{
//鍒濆鍖朩i-Fi杩炴帴
- Com.Mediatek.Elian.ElianNative.InitSmartConnection(null, 1, 1);
+ Com.Mediatek.Elian.ElianNative.InitSmartConnection("1", 1, 1);
//寮�濮媁i-Fi杩炴帴
var result = Com.Mediatek.Elian.ElianNative.StartSmartConnection(wifiName, wifiPsw, p2);
return true;
--
Gitblit v1.8.0