From d4973876384be55df64de45db8a511d1e0330872 Mon Sep 17 00:00:00 2001
From: wei <kaede@kaededeMacBook-Air.local>
Date: 星期三, 23 六月 2021 17:06:20 +0800
Subject: [PATCH] 房间小图列表
---
HDL_ON/DAL/Mqtt/MqttClient.cs | 569 ++++++++++++++++++++++++++++++++++++--------------------
1 files changed, 368 insertions(+), 201 deletions(-)
diff --git a/HDL_ON/DAL/Mqtt/MqttClient.cs b/HDL_ON/DAL/Mqtt/MqttClient.cs
index f65e6ce..93d0ae7 100644
--- a/HDL_ON/DAL/Mqtt/MqttClient.cs
+++ b/HDL_ON/DAL/Mqtt/MqttClient.cs
@@ -1,7 +1,6 @@
锘�/*
鏇存柊浜咵MQ杩炴帴鏂瑰紡
*/
-using System.Collections.Generic;
using System;
using MQTTnet.Client;
using System.Threading.Tasks;
@@ -9,8 +8,10 @@
using MQTTnet;
using System.Text;
using System.Security.Cryptography;
-using System.IO;
using HDL_ON.DriverLayer;
+using HDL_ON.Entity;
+using HDL_ON.UI;
+using HDL_ON.DAL.Server;
namespace HDL_ON.DAL.Mqtt
{
@@ -20,8 +21,9 @@
/// 鍔犲瘑閫氳KEY
/// </summary>
static string mqttEncryptKey = "";
+ static string tuyaEncryptKey = "";
//static string checkGatewayTopicBase64 = "";
-
+ static bool hadGateway = true;
/// <summary>
/// 鎸や笅绾夸富棰�
/// </summary>
@@ -32,18 +34,18 @@
/// </summary>
static string RandomKey = "";
+ /// <summary>
+ /// 闅忔満鐢熸垚瀛楃
+ /// </summary>
+ /// <returns></returns>
static string GetRandomKey()
{
if (string.IsNullOrEmpty(RandomKey))
{
//闅忔満2浣嶅瓧绗︿覆
- Random random = new Random(Guid.NewGuid().GetHashCode());
- int num = random.Next(65, 91);
- RandomKey = Convert.ToChar(num).ToString();
+ RandomKey = Utlis.CreateRandomString(2);
}
-
return RandomKey;
-
}
/// <summary>
@@ -51,65 +53,50 @@
/// </summary>
public static IMqttClient RemoteMqttClient = new MqttFactory().CreateMqttClient();
-
/// <summary>
/// 鎺ㄩ�佹爣璇�
/// </summary>
- static string PushSignStr = System.DateTime.Now.Ticks.ToString();
+ static string PushSignStr = DateTime.Now.Ticks.ToString();
- /// <summary>
- /// 鏂紑杩滅▼Mqtt鐨勯摼鎺�
+ /// <summary>
+ /// 鏂紑杩滅▼Mqtt鐨勯摼鎺�
/// </summary>
- static async Task DisConnectRemoteMqttClient(string s = "")
- {
+ static async Task DisConnectRemoteMqttClient(string s = "")
+ {
try
- {
+ {
if (remoteIsConnected)
{
remoteIsConnected = false;
isSubscribeSuccess = false;
- Utlis.WriteLine($"Remote涓诲姩鏂紑_{s}");
- //await RemoteMqttClient.DisconnectAsync(new MQTTnet.Client.Disconnecting.MqttClientDisconnectOptions { }, CancellationToken.None);
+ Utlis.WriteLine($"Remote涓诲姩鏂紑_{s}");
+ //await RemoteMqttClient.DisconnectAsync(new MQTTnet.Client.Disconnecting.MqttClientDisconnectOptions { }, CancellationToken.None);
await RemoteMqttClient.DisconnectAsync();
-
- if (Control.Ins.IsRemote)
- {
- //涓嶆槸鏃犵綉缁�
- if (OnAppConfig.Instance.internetStatus != 0)
- {
- Control.Ins.GatewayOnline = false;
- }
- }
- }
+ }
}
catch (Exception e)
{
- Utlis.WriteLine($"Remote鏂紑閫氳杩炴帴鍑哄紓甯�:{e.Message}");
- }
+ Utlis.WriteLine($"Remote鏂紑閫氳杩炴帴鍑哄紓甯�:{e.Message}");
+ }
}
- /// <summary>
- /// 鏂紑杩滅▼Mqtt鐨勯摼鎺�
+ /// <summary>
+ /// 鏂紑杩滅▼Mqtt鐨勯摼鎺�
/// </summary>
- static async Task DisConnectRemoteMqttClientWhenStart(string s = "")
+ static async Task DisConnectRemoteMqttClientWhenStart(string s = "")
{
try
{
- //if (remoteIsConnected) {
remoteIsConnected = false;
isSubscribeSuccess = false;
Utlis.WriteLine($"RemoteStart涓诲姩鏂紑_{s}");
await RemoteMqttClient.DisconnectAsync();
-
- //}
}
catch (Exception e)
{
- Utlis.WriteLine($"RemoteStart鏂紑閫氳杩炴帴鍑哄紓甯�:{e.Message}");
+ Utlis.WriteLine($"RemoteStart鏂紑閫氳杩炴帴鍑哄紓甯�:{e.Message}");
}
-
}
-
/// <summary>
/// 鏂紑mqtt杩炴帴
@@ -126,11 +113,10 @@
await DisConnectRemoteMqttClient(s);
}
- //static DateTime dateTime = DateTime.MinValue;
/// <summary>
/// 澶栫綉鐨凪QTT鏄惁姝e湪杩炴帴
/// </summary>
- public static bool remoteMqttIsConnecting;
+ public static bool RemoteMqttIsConnecting;
static bool remoteIsConnected;
static MqttClient()
@@ -147,14 +133,13 @@
{
try
{
- System.Threading.Thread.Sleep(500);
- //if (!UserInfo.Current.IsLogin) {
- // continue;
- //}
-
- if (!Control.Ins.IsRemote) continue;
-
- //if (BusSocket.IsEnterBackground) continue;
+ System.Threading.Thread.Sleep(2000);
+ //杩涘叆鍚庡彴涓嶅鐞�
+ if (MainPage.IsEnterBackground) continue;
+ if (MqttInfoConfig.Current.HomeGatewayInfo == null)
+ {
+ continue;
+ }
await StartCloudMqtt();
await SubscribeTopics();
@@ -175,45 +160,90 @@
}
static bool isSubscribeSuccess;
+ /// <summary>
+ /// 璁㈤槄涓婚
+ /// </summary>
+ /// <returns></returns>
static async Task SubscribeTopics()
{
if (remoteIsConnected && !isSubscribeSuccess)
{
try
{
-
- //var topicFilterPush = new TopicFilter { QualityOfServiceLevel = MQTTnet.Protocol.MqttQualityOfServiceLevel.ExactlyOnce,
- // Topic = $"/BusGateWayToClient/{MqttInfoConfig.Instance.CurRemoteMACInfo.clientId}/Push/NotifySqueeze" };
-
- //2020-05-14 璁㈤槄涓婚璐ㄩ噺鏀逛负0
- var topicFilterBusGateWayToClient = new MqttTopicFilter()
+ if (DB_ResidenceData.Instance.GatewayType == 0 && !DB_ResidenceData.Instance.CheckWhetherGatewayIdIsNull())
{
- Topic = $"/BusGateWayToClient/{MqttInfoConfig.Current.HomeGatewayInfo.id}/#",
- QualityOfServiceLevel = MQTTnet.Protocol.MqttQualityOfServiceLevel.AtMostOnce
- //QualityOfServiceLevel = MQTTnet.Protocol.MqttQualityOfServiceLevel.ExactlyOnce
- };
-
+ Utlis.WriteLine("寮�濮嬭闃呬竴绔彛閫氱敤涓婚锛�");
+ //2020-05-14 璁㈤槄涓婚璐ㄩ噺鏀逛负0
+ var topicFilterBusGateWayToClient = new MqttTopicFilter()
+ {
+ Topic = $"/BusGateWayToClient/{MqttInfoConfig.Current.HomeGatewayInfo.gatewayId}/#",
+ QualityOfServiceLevel = MQTTnet.Protocol.MqttQualityOfServiceLevel.AtMostOnce
+ };
+ await RemoteMqttClient.SubscribeAsync(topicFilterBusGateWayToClient);
+ }
+ //鎸や笅绾夸富棰�
var topicFilterPush2 = new MqttTopicFilter
{
Topic = $"/BusGateWayToClient/{UserInfo.Current.ID}/#",
QualityOfServiceLevel = MQTTnet.Protocol.MqttQualityOfServiceLevel.AtMostOnce,
- //QualityOfServiceLevel = MQTTnet.Protocol.MqttQualityOfServiceLevel.ExactlyOnce
+ };
+ var topicAlinkStatus = new MqttTopicFilter()
+ {
+ Topic = $"/user/{DB_ResidenceData.Instance.CurrentRegion.id}/app/thing/property/send",
+ QualityOfServiceLevel = MQTTnet.Protocol.MqttQualityOfServiceLevel.AtMostOnce
+ };
+ //App璁㈤槄绾㈠瀹� / 缃戝叧閬ユ帶鍣ㄦ坊鍔犳垚鍔熼�氱煡
+ var pirStatus = new MqttTopicFilter()
+ {
+ Topic = $"/user/{DB_ResidenceData.Instance.CurrentRegion.id}/app/thing/topo/found",
+ QualityOfServiceLevel = MQTTnet.Protocol.MqttQualityOfServiceLevel.AtMostOnce
+ };
+ //App璁㈤槄閬ユ帶鍣ㄨ嚜瀛︽寜閿涔犳垚鍔熼�氱煡
+ var pirStudy = new MqttTopicFilter()
+ {
+ Topic = $"/user/{DB_ResidenceData.Instance.CurrentRegion.id}/app/thing/event/irCodeStudyDone/up",
+ QualityOfServiceLevel = MQTTnet.Protocol.MqttQualityOfServiceLevel.AtMostOnce
};
+ #region 鏁版嵁鏇存柊鎺ㄩ�佷富棰�
+ //appHomeRefresh锛氫綇瀹呮暟鎹埛鏂伴�氱煡
+ var appHomeRefresh = new MqttTopicFilter()
+ {
+ Topic = $"/user/{DB_ResidenceData.Instance.CurrentRegion.id}/app/thing/event/appHomeRefresh/up",
+ QualityOfServiceLevel = MQTTnet.Protocol.MqttQualityOfServiceLevel.AtMostOnce
+ };
+ //appRoomRefresh锛氭埧闂存暟鎹埛鏂伴�氱煡
+ var appRoomRefresh = new MqttTopicFilter()
+ {
+ Topic = $"/user/{DB_ResidenceData.Instance.CurrentRegion.id}/app/thing/event/appRoomRefresh/up",
+ QualityOfServiceLevel = MQTTnet.Protocol.MqttQualityOfServiceLevel.AtMostOnce
+ };
+ //appDeviceRefresh锛氳澶囨暟鎹埛鏂伴�氱煡
+ var appDeviceRefresh = new MqttTopicFilter()
+ {
+ Topic = $"/user/{DB_ResidenceData.Instance.CurrentRegion.id}/app/thing/event/appDeviceRefresh/up",
+ QualityOfServiceLevel = MQTTnet.Protocol.MqttQualityOfServiceLevel.AtMostOnce
+ };
+ //涓�绔彛瀵嗛挜鏇存柊閫氱煡
+ var mqttkeyChange = new MqttTopicFilter()
+ {
+ Topic = $"/user/{MqttInfoConfig.Current.HomeGatewayInfo.gatewayId}/custom/mqtt/secret/change",
+ QualityOfServiceLevel = MQTTnet.Protocol.MqttQualityOfServiceLevel.AtMostOnce
+ };
+
+ #endregion
+
+
Utlis.WriteLine("寮�濮嬭闃咃紒");
- var result = await RemoteMqttClient.SubscribeAsync(new MqttTopicFilter[] { topicFilterBusGateWayToClient, topicFilterPush2 });
+ var result = await RemoteMqttClient.SubscribeAsync(new MqttTopicFilter[] {
+ pirStatus,pirStudy,
+ appDeviceRefresh,appHomeRefresh,appRoomRefresh,
+ topicFilterPush2, topicAlinkStatus ,mqttkeyChange});
if (result.Items[0].ResultCode == MQTTnet.Client.Subscribing.MqttClientSubscribeResultCode.GrantedQoS0)
{
isSubscribeSuccess = true;
Utlis.WriteLine("璁㈤槄鎴愬姛锛�");
-
- MqttRemoteSend(new byte[] { 0 }, 3);
-
- //杩炴帴鎴愬姛鍚庢娴嬫槸鍚﹂渶瑕侀�氳繃杩滅▼鑾峰彇Key
- CheckIfNeedGetLocalPasswordFromRemote();
}
-
-
}
catch (Exception ex)
{
@@ -222,16 +252,12 @@
}
}
- static DateTime mFlagDateTime;
-
- //static readonly object SendLocker = new object ();
/// <summary>
/// 鍚姩杩滅▼Mqtt
/// </summary>
public static async Task StartCloudMqtt()
{
-
- if (OnAppConfig.Instance.internetStatus == 0)
+ if (MainPage.InternetStatus == 0)
{
return;
}
@@ -240,55 +266,188 @@
{
return;
}
+ if (DB_ResidenceData.Instance.CurrentRegion == null || DB_ResidenceData.Instance.CurrentRegion.id == null)
+ {
+ return;
+ }
//杩藉姞锛氭病鏈夎繙绋嬭繛鎺ョ殑鏉冮檺
- if (remoteMqttIsConnecting || remoteIsConnected)
+ if (RemoteMqttIsConnecting || remoteIsConnected)
{
return;
}
Utlis.WriteLine($"StartCloudMqtt: 寮�濮�");
- await Task.Factory.StartNew(async () => {
+ await Task.Factory.StartNew((Func<Task>)(async () => {
try
{
#region 鍒濆鍖栬繙绋婱qtt
-
+ RemoteMqttIsConnecting = true;
RemoteMqttClient = new MqttFactory().CreateMqttClient();
//(1)褰揫杩炴帴浜戠鐨凪qtt鎴愬姛鍚嶿鎴栬�匸浠ュ強鍚庨潰App閫氳繃浜戠Mqtt杞彂鏁版嵁缁欑綉鍏虫垚鍔熷悗],澶勭悊鎺ユ敹鍒颁簯绔暟鎹寘鍝嶅簲鏃跺湪mqttServerClient_ApplicationMessageReceived杩欎釜鏂规硶澶勭悊
if (RemoteMqttClient.ApplicationMessageReceivedHandler == null)
{
- RemoteMqttClient.UseApplicationMessageReceivedHandler((e) => {
+ //澶勭悊鎺ユ敹鍒扮殑鏁版嵁
+ RemoteMqttClient.UseApplicationMessageReceivedHandler((Action<MqttApplicationMessageReceivedEventArgs>)((e) => {
try
{
var topic = e.ApplicationMessage.Topic;
+ //MainPage.Log($"鏀跺埌mqtt涓婚:{topic}");
+ //涓�绔彛涓婚澶勭悊
+ if (DB_ResidenceData.Instance.GatewayType == 0 && !DB_ResidenceData.Instance.CheckWhetherGatewayIdIsNull())
+ {
+ if (topic == $"/BusGateWayToClient/{MqttInfoConfig.Current.HomeGatewayInfo.gatewayId}/NotifyBusGateWayInfoChange")
+ {
+ //缃戝叧涓婄嚎锛岄渶瑕佹洿鏂癮eskey
+ //鏀跺埌缃戝叧涓婄嚎娑堟伅涓婚
+ ReceiveNotifyBusGateWayInfoChange();
+ return;
+ }
+ else if (topic == $"/BusGateWayToClient/{MqttInfoConfig.Current.HomeGatewayInfo.gatewayId}/NotifyGateWayOffline")
+ {
+ //缃戝叧鎺夌嚎
+ //----绗簩姝ワ細璇诲彇璐﹀彿涓嬮潰鐨勭綉鍏冲垪琛�
+ ReceiveNotifyGateWayOffline();
+ return;
+ }
+ else if (topic == $"/BusGateWayToClient/{MqttInfoConfig.Current.HomeGatewayInfo.gatewayId}/Common/CheckGateway")
+ {
+ var ss = Encoding.UTF8.GetString(e.ApplicationMessage.Payload);
+ //ReceiveCheckGateway(ss);
+ return;
+ }
+ }
+
+ //涓�浜涚壒娈婄殑涓婚澶勭悊(涓轰簡鎵ц閫熷害,灏藉彲鑳界殑鍒姞鑰楁椂鐨勬搷浣� true:鎵ц浜嗙壒娈婂鐞� false:娌℃湁鎵ц鐗规畩澶勭悊)
+ if (Stan.HdlCloudReceiveLogic.Current.CloudOverallMsgReceiveEx(topic, e.ApplicationMessage.Payload, mqttEncryptKey, tuyaEncryptKey) == true)
+ {
+ return;
+ }
+
if (topic == $"/BusGateWayToClient/{UserInfo.Current.ID}" + PushNotifySqueeze)
{
var mMes = Encoding.UTF8.GetString(e.ApplicationMessage.Payload);
//鏂版尋涓嬬嚎涓婚鏂规 鏀跺埌鎸や笅绾夸富棰�
ReceiveNotifySqueezeAsync(mMes);
}
- else if (topic == $"/BusGateWayToClient/{MqttInfoConfig.Current.HomeGatewayInfo.id}/NotifyBusGateWayInfoChange")
- {//缃戝叧涓婄嚎锛岄渶瑕佹洿鏂癮eskey
- //鏀跺埌缃戝叧涓婄嚎娑堟伅涓婚
- ReceiveNotifyBusGateWayInfoChange();
- }
- else if (topic == $"/BusGateWayToClient/{MqttInfoConfig.Current.HomeGatewayInfo.id}/NotifyGateWayOffline")
- {//缃戝叧鎺夌嚎 //----绗簩姝ワ細璇诲彇璐﹀彿涓嬮潰鐨勭綉鍏冲垪琛�
- ReceiveNotifyGateWayOffline();
- }
- else if (topic == $"/BusGateWayToClient/{MqttInfoConfig.Current.HomeGatewayInfo.id}/Common/CheckGateway")
+ //App璁㈤槄绾㈠瀹�/缃戝叧閬ユ帶鍣ㄦ坊鍔犳垚鍔熼�氱煡
+ else if (topic == $"/user/{DB_ResidenceData.Instance.CurrentRegion.id}/app/thing/topo/found")
{
- var ss = Encoding.UTF8.GetString(e.ApplicationMessage.Payload);
- ReceiveCheckGateway(ss);
+ var bytes = Securitys.EncryptionService.AesDecryptPayload(e.ApplicationMessage.Payload, tuyaEncryptKey);
+ var revString = Encoding.UTF8.GetString(bytes);
+ HDL_ON.UI.UI2.PersonalCenter.PirDevice.PirMethod.controldata = revString;
}
+ //App璁㈤槄閬ユ帶鍣ㄨ嚜瀛︽寜閿涔犳垚鍔熼�氱煡
+ else if (topic == $"/user/{DB_ResidenceData.Instance.CurrentRegion.id}/app/thing/event/irCodeStudyDone/up")
+ {
+ var bytes = Securitys.EncryptionService.AesDecryptPayload(e.ApplicationMessage.Payload, tuyaEncryptKey);
+ var revString = Encoding.UTF8.GetString(bytes);
+ UI.UI2.PersonalCenter.PirDevice.PirMethod.buttondata = revString;
+ }
+ #region 鏁版嵁鏇存柊鎺ㄩ�佷富棰�
+ //appHomeRefresh锛氫綇瀹呮暟鎹埛鏂伴�氱煡
+ else if (topic == $"/user/{DB_ResidenceData.Instance.CurrentRegion.id}/app/thing/event/appHomeRefresh/up")
+ {
+ MainPage.Log("浣忓畢鏁版嵁鍒锋柊閫氱煡");
+ new HttpServerRequest().GetHomePager();
+ }
+ //appRoomRefresh锛氭埧闂存暟鎹埛鏂伴�氱煡
+ else if (topic == $"/user/{DB_ResidenceData.Instance.CurrentRegion.id}/app/thing/event/appRoomRefresh/up")
+ {
+ MainPage.Log("鎴块棿鏁版嵁鍒锋柊閫氱煡");
+ var roomResult = new HttpServerRequest().GetRoomList();
+ if (roomResult.Code == StateCode.SUCCESS)
+ {
+ MainPage.Log($"璇诲彇鎴块棿淇℃伅鎴愬姛");
+ var revData = Newtonsoft.Json.JsonConvert.DeserializeObject<SpatialApiPack>(roomResult.Data.ToString());
+ if (revData == null)
+ {
+ revData = new SpatialApiPack();
+ }
+ {
+ SpatialInfo.CurrentSpatial.UpdateSpatialList(revData.list);
+ }
+ }
+ else
+ {
+ MainPage.Log($"璇诲彇鎴块棿鏁版嵁澶辫触:Code:{roomResult.Code}; msg:{roomResult.message}");
+ }
+ }
+ //appDeviceRefresh锛氳澶囨暟鎹埛鏂伴�氱煡
+ else if (topic == $"/user/{DB_ResidenceData.Instance.CurrentRegion.id}/app/thing/event/appDeviceRefresh/up")
+ {
+ MainPage.Log("璁惧鏁版嵁鍒锋柊閫氱煡");
+ var deviceResult = new HttpServerRequest().GetDeviceList();
+ if (deviceResult.Code == StateCode.SUCCESS)
+ {
+ MainPage.Log($"璇诲彇璁惧淇℃伅鎴愬姛");
+ var deviceList = Newtonsoft.Json.JsonConvert.DeserializeObject<DevcieApiPack>(deviceResult.Data.ToString());
+ if (deviceList == null)
+ {
+ deviceList = new DevcieApiPack();
+ }
+ string delFile = "";
+ if (FunctionList.List.GetDeviceFunctionList().Count > 0)
+ {
+ for (int i = 0; i < FunctionList.List.GetDeviceFunctionList().Count;)
+ {
+ var localFunction = FunctionList.List.GetDeviceFunctionList()[i];
+ if (localFunction.Spk_Prefix == FunctionCategory.Music || string.IsNullOrEmpty(localFunction.Spk_Prefix))
+ {
+ i++;
+ continue;
+ }
+ var newFunction = deviceList.list.Find((obj) => obj.deviceId == localFunction.deviceId);
+
+ if (delFile == localFunction.savePath)
+ {
+ i++;
+ continue;
+ }
+ delFile = localFunction.savePath;
+ FunctionList.List.DeleteFunction(localFunction);
+ }
+ }
+ //澶勭悊鍓╀笅鐨勬柊澧炲姛鑳�
+ foreach (var newFunction in deviceList.list)
+ {
+ newFunction.SaveFunctionFile();
+ FunctionList.List.IniFunctionList(newFunction.savePath);
+ }
+ }
+ else
+ {
+ MainPage.Log($"璇诲彇浜戠璁惧鏁版嵁澶辫触:Code:{deviceResult.Code}; Msg:{deviceResult.message}");
+ }
+ }
+ //缃戝叧瀵嗛挜鍙樺寲
+ else if (topic == $"/user/{DB_ResidenceData.Instance.CurrentRegion.id}/custom/mqtt/secret/change")
+ {
+ var pm = new HttpServerRequest();
+ pm.GetHomeGatewayList();
+ MainPage.Log($"缃戝叧瀵嗛挜鍙樻洿");
+ MainPage.Log($"鏃у瘑閽ワ細{mqttEncryptKey}");
+ mqttEncryptKey = MqttInfoConfig.Current.HomeGatewayInfo.aesKey;
+ MainPage.Log($"鏂板瘑閽ワ細{mqttEncryptKey}");
+ }
+
+ #endregion
+ //A缃戝叧璁惧鐘舵��-鍖呭惈娑傞甫璁惧
+ else if (topic == $"/user/{DB_ResidenceData.Instance.CurrentRegion.id}/app/thing/property/send")
+ {
+ var bytes = Securitys.EncryptionService.AesDecryptPayload(e.ApplicationMessage.Payload, tuyaEncryptKey);
+ var revString = Encoding.UTF8.GetString(bytes);
+ Control.Ins.UpdataFunctionStatus(revString, null, true);
+ }
+ //涓�绔彛鏁版嵁瑙f瀽
else
{
-
- SetGatewayOnlineResetCheck();
-
+ //SetGatewayOnlineResetCheck();
+ //var bytes = Securitys.EncryptionService.AesDecryptPayload(e.ApplicationMessage.Payload, mqttEncryptKey);
+ //bus鏁版嵁瑙f瀽
var packet = new Packet();
if (!string.IsNullOrEmpty(mqttEncryptKey))
@@ -303,7 +462,7 @@
}
}
catch { }
- });
+ }));
}
//(2)DisconnectedHandler
@@ -313,25 +472,20 @@
Utlis.WriteLine($"杩滅▼杩炴帴鏂紑");
isSubscribeSuccess = false;
await DisConnectRemoteMqttClient("UseDisconnectedHandler");
-
});
}
//(3)ConnectedHandler
if (RemoteMqttClient.ConnectedHandler == null)
{
- RemoteMqttClient.UseConnectedHandler(async (e) => {
+ RemoteMqttClient.UseConnectedHandler(async (e) =>
+ {
IfNeedReadAllDeviceStatus = true;
+ Control.Ins.GatewayOnline_Cloud = true;
Utlis.WriteLine($"============>Mqtt杩滅▼杩炴帴鎴愬姛");
SendPushSignOut();
-
- //if (Control.Ins.IsRemote) {
- // //Utlis.ShowAppLinkStatus (AppLinkStatus.CloudLink);
- // MainPage.AddTip (Language.StringByID (SimpleControl.R.MyInternationalizationString.LinkSuccess));
- //}
});
}
#endregion
- //}
//(4)===========寮�濮嬭繛鎺ヨ繃绋�==========
//涔嬪墠宸茬粡鑾峰彇鍙傛暟鎴愬姛杩�
@@ -350,16 +504,18 @@
catch (Exception ex)
{
Utlis.WriteLine($"error:" + ex.Message);
+ //mqtt杩炴帴寮傚父锛屾竻绌烘湰鍦癿qtt淇℃伅锛屽彲鑳介渶瑕侀噸鏂拌幏鍙栵細wxr
+ MqttInfoConfig.Current.Refresh();
}
finally
{
//鏈�缁堣閲婃斁杩炴帴鐘舵��
- remoteMqttIsConnecting = false;
+ RemoteMqttIsConnecting = false;
Utlis.WriteLine($"StartCloudMqtt: 缁撴潫");
}
- });
+ }));
}
@@ -384,7 +540,7 @@
{
try
{
- if (MqttInfoConfig.Current.HomeGatewayInfo != null && MqttInfoConfig.Current.HomeGatewayInfo.mac == Entity.DB_ResidenceData.residenceData.residenceGatewayMAC)
+ if (MqttInfoConfig.Current.HomeGatewayInfo != null && MqttInfoConfig.Current.HomeGatewayInfo.mac == Entity.DB_ResidenceData.Instance.residenceGatewayMAC)
{
await MQTTConnectAsync();
}
@@ -407,11 +563,6 @@
/// <returns></returns>
static async Task StartMQTTGetInfo()
{
- if (!Control.Ins.IsRemote)
- {
- return;
- }
-
await GetMqttInfoAndMQTTConnectAsync();
////--鍒ゆ柇鏄綋鍓嶆槸鍚﹀垎浜殑浣忓畢
@@ -430,13 +581,9 @@
/// </summary>
static async Task MQTTConnectAsync()
{
-
- if (!Control.Ins.IsRemote)
- {
- return;
- }
-
- if (MqttInfoConfig.Current.HomeGatewayInfo != null && MqttInfoConfig.Current.mMqttInfo != null)
+ //if (MqttInfoConfig.Current.HomeGatewayInfo != null && MqttInfoConfig.Current.mMqttInfo != null)
+ //娌℃湁缃戝叧鎯呭喌涓嬶紝涔熼渶瑕佽繛鎺qtt锛屾秱楦︾涓夋柟璁惧涓嶉渶瑕佺綉鍏�
+ if (MqttInfoConfig.Current.mMqttInfo != null)
{
try
{
@@ -445,8 +592,7 @@
//url = HttpUtil.GetProxyEMQUrl (url);
//#if DEBUG
- // url = HttpUtil.GetProxyEMQUrl (url);
-
+ //url = HttpUtil.GetProxyEMQUrl (url);
//#endif
var clientId = MqttInfoConfig.Current.mMqttInfo.clientId;
var username = MqttInfoConfig.Current.mMqttInfo.userName;
@@ -455,7 +601,18 @@
MqttInfoConfig.Current.IfGetMqttInfoSuccess = true;
MqttInfoConfig.Current.Save();
+
+
mqttEncryptKey = MqttInfoConfig.Current.HomeGatewayInfo.aesKey;
+ //瑙e瘑瀵嗛挜瑙勫垯锛氬凡鐜版湁鐨勪綇瀹匢D涓哄熀鍑�,浠庡彸杈逛竴涓�鑾峰彇鍊�,鏈�鍚庡鏋滀笉澶�16浣�,鍒欏線鍙宠ˉ闆�
+ string aesKey = string.Empty;
+ for (int i = DB_ResidenceData.Instance.CurrentRegion.id.Length - 1; i >= 0; i--)
+ {
+ aesKey += DB_ResidenceData.Instance.CurrentRegion.id[i].ToString();
+ if (aesKey.Length == 16) { break; }
+ }
+ aesKey = aesKey.PadRight(16, '0');
+ tuyaEncryptKey = aesKey;
var options1 = new MQTTnet.Client.Options.MqttClientOptionsBuilder()
.WithClientId(clientId)
@@ -476,7 +633,6 @@
{
remoteIsConnected = true;
IsDisConnectingWithSendCatch = false;
- UnsupportedProtocolVersionCount = 0;
}
else
{
@@ -488,22 +644,11 @@
catch (Exception ex)
{
- if (ex.Message == MqttCommunicationTimedOutException)
- {
- Console.WriteLine("Connect error TimedOut: " + ex.Message);
- }
- else
- {
- //閲嶆柊涓績鏈嶅姟鍣ㄨ幏鍙栧弬鏁版爣璁�
- MqttInfoConfig.Current.IfGetMqttInfoSuccess = false;
- Console.WriteLine("Connect error: " + ex.Message);
- }
-
- //Console.WriteLine ("Connect error: " + ex.Message);
- if (IfDEBUG)
- {
- MainPage.ShowAlertOnMainThread("Connect error: " + ex.Message);
- }
+ //閲嶆柊涓績鏈嶅姟鍣ㄨ幏鍙栧弬鏁版爣璁�
+ MqttInfoConfig.Current.IfGetMqttInfoSuccess = false;
+ Console.WriteLine("Connect error: " + ex.Message);
+ //mqtt杩炴帴寮傚父锛屾竻绌烘湰鍦癿qtt淇℃伅锛屽彲鑳介渶瑕侀噸鏂拌幏鍙栵細wxr
+ MqttInfoConfig.Current.Refresh();
}
finally
{
@@ -525,12 +670,11 @@
{
try
{
- SetGatewayOnlineResetCheck();
- if (Control.Ins.IsRemote)
+ //SetGatewayOnlineResetCheck();
+ if (Control.Ins.GatewayOnline_Cloud)
{
CheckIfNeedReadAllDeviceStatus();
}
-
}
catch { }
}
@@ -540,23 +684,36 @@
/// </summary>
static void ReceiveNotifyGateWayOffline()
{
- if (Control.Ins.IsRemote)
- {
- Control.Ins.GatewayOnline = false;
- }
+ Control.Ins.GatewayOnline_Cloud = false;
}
-
/// <summary>
/// 鏀跺埌鎸や笅绾挎帹閫�
/// </summary>
static void ReceiveNotifySqueezeAsync(string mMes)
{
+ if (mMes == PushSignStr) return;//鏄嚜宸辩殑鐧诲綍鎺ㄩ�佷笉澶勭悊//鎴栬�呭綋鍓嶄笉鏄繙绋嬮摼鎺ョ姸鎬�
+ //娴嬭瘯璐﹀彿锛屼笉鎸や笅绾�
+ switch (UserInfo.Current.userMobileInfo)
+ {
+ case "13415629083":
+ case "18316120654":
+ case "15622703419":
+ case "18824864143":
+ case "464027401@qq.com":
+ case "2791308028@qq.com":
+ case "13697499568":
+ case "18666455392":
+ case "13375012446":
+ case "13602944661":
+ case "18778381374":
+ case "18316672920":
+ case "15971583093":
+ case "15626203746":
+ case "551775569@qq.com":
+ return;
+ }
- if (mMes == PushSignStr) return;//鏄嚜宸辩殑鐧诲綍鎺ㄩ�佷笉澶勭悊
-
- //鏂紑杩滅▼杩炴帴
- Control.Ins.IsRemote = false;
if (!UserInfo.Current.IsLogin)
{
return;
@@ -564,32 +721,39 @@
DisConnectRemoteMqttClient("鎸や笅绾�");
- UserInfo.Current.LastTime = DateTime.MinValue;
- UserInfo.Current.SaveUserInfo();
-
- Application.RunOnMainThread(() => {
- MainPage.GoLoginPage(UserInfo.Current);
+ Application.RunOnMainThread(() =>
+ {
//寮圭獥鎻愮ず琚尋涓嬬嚎
+ HDLCommon.Current.CheckLogout();
});
+
+
+ //UserInfo.Current.LastTime = DateTime.MinValue;
+ //UserInfo.Current.SaveUserInfo();
+
+ //Application.RunOnMainThread(() => {
+ // MainPage.GoLoginPage(UserInfo.Current);
+ // //寮圭獥鎻愮ず琚尋涓嬬嚎
+ //});
//2020-08-11 鍒犻櫎鎺ㄩ�佹暟鎹�
//HDLRequest.Current.PushserivceSignOut ();
}
- /// <summary>
- /// 鏀跺埌CheckGateway涓婚
- /// </summary>
- static void ReceiveCheckGateway(string mMes)
- {
- if (!Control.Ins.IsRemote) return;
+ ///// <summary>
+ ///// 鏀跺埌CheckGateway涓婚
+ ///// </summary>
+ //static void ReceiveCheckGateway(string mMes)
+ //{
+ // if (!Control.Ins.IsRemote) return;
- Utlis.WriteLine("ReceiveCheckGateway锛�");
+ // Utlis.WriteLine("ReceiveCheckGateway锛�");
- //CheckIfNeedReadAllDeviceStatus ();
+ // //CheckIfNeedReadAllDeviceStatus ();
- //var obj = Newtonsoft.Json.JsonConvert.DeserializeObject<ResponsePack>(mMes);
- Control.Ins.GatewayOnline = true;
- }
+ // //var obj = Newtonsoft.Json.JsonConvert.DeserializeObject<ResponsePack>(mMes);
+ // Control.Ins.GatewayOnline = true;
+ //}
/// <summary>
/// 鎺ㄩ�佹尋涓嬬嚎涓婚
@@ -608,25 +772,18 @@
/// <returns></returns>
public static async Task MqttRemoteSend(byte[] message, int optionType = 0)
{
- //return;
-
try
{
string topicName;
switch (optionType)
{
case 0:
- topicName = $"/ClientToBusGateWay/{MqttInfoConfig.Current.HomeGatewayInfo.id}/Common/ON";
+ topicName = $"/ClientToBusGateWay/{MqttInfoConfig.Current.HomeGatewayInfo.gatewayId}/Common/ON";
if (!string.IsNullOrEmpty(mqttEncryptKey))
{
message = Securitys.EncryptionService.AesEncryptPayload(message, mqttEncryptKey);
}
await RemoteMqttClient.PublishAsync(new MqttApplicationMessage { Topic = topicName, Payload = message, Retain = false, QualityOfServiceLevel = MQTTnet.Protocol.MqttQualityOfServiceLevel.AtMostOnce });
- break;
- case 3:
- topicName = $"/ClientToBusGateWay/{MqttInfoConfig.Current.HomeGatewayInfo.id}/Common/CheckGateway";
- Utlis.WriteLine("CheckGateway");
- await RemoteMqttClient.PublishAsync(new MqttApplicationMessage { Topic = topicName, Retain = false, QualityOfServiceLevel = MQTTnet.Protocol.MqttQualityOfServiceLevel.AtMostOnce });
break;
case 4://鍙戝竷鏂版柟妗堢殑鎸や笅绾夸富棰�
topicName = $"/BusGateWayToClient/{UserInfo.Current.ID}" + PushNotifySqueeze;
@@ -655,18 +812,28 @@
/// 鏄惁闇�瑕佽鍙栦竴娆℃墍鏈夎澶囩姸鎬�
/// </summary>
static bool IfNeedReadAllDeviceStatus = true;
- //public static bool IsGatewayOnline = true;
- /// <summary>
- /// 璁剧疆缃戝叧鍦ㄧ嚎鏍囧織锛屽苟閲嶇疆CheckGateway鍙傛暟
- /// </summary>
- static void SetGatewayOnlineResetCheck()
- {
- if (Control.Ins.IsRemote)
- {
- Control.Ins.GatewayOnline = true;
- }
- }
+ ///// <summary>
+ ///// 璁剧疆缃戝叧鍦ㄧ嚎鏍囧織锛屽苟閲嶇疆CheckGateway鍙傛暟
+ ///// </summary>
+ //static void SetGatewayOnlineResetCheck()
+ //{
+ // if (Control.Ins.IsRemote)
+ // {
+ // if (!Control.Ins.GatewayOnline)
+ // {
+ // try
+ // {
+ // if (DB_ResidenceData.Instance.HomeGateway != null)
+ // {
+ // DB_ResidenceData.Instance.HomeGateway.gatewayStatus = true;
+ // }
+ // Control.Ins.GatewayOnline = true;
+ // }
+ // catch { }
+ // }
+ // }
+ //}
/// <summary>
@@ -675,30 +842,30 @@
/// <returns></returns>
static async Task GetMqttInfoAndMQTTConnectAsync()
{
- var mqttInfoRequestResult_Obj =new Server.HttpServerRequest().GetMqttRemoteInfo(GetRandomKey());
+ var mqttInfoRequestResult_Obj = new HttpServerRequest().GetMqttRemoteInfo(GetRandomKey());
if (mqttInfoRequestResult_Obj != null)
{
- MqttInfoConfig.Cur.mMqttInfo = mqttInfoRequestResult_Obj;
- if (UserConfig.Instance.GatewayList != null && UserConfig.Instance.GatewayList.Count > 0)
+ MainPage.Log($"鑾峰彇mqtt info 鎴愬姛 /r/n clientId:{mqttInfoRequestResult_Obj.clientId}/r/n passWord:{mqttInfoRequestResult_Obj.passWord} /r/n url:{mqttInfoRequestResult_Obj.url}/r/n userName:{mqttInfoRequestResult_Obj.userName}");
+
+
+ MqttInfoConfig.Current.mMqttInfo = mqttInfoRequestResult_Obj;
+ await MQTTConnectAsync();
+ //1.鍒ゆ柇鏄惁缁戝畾浜嗙綉鍏筹紝鑾峰彇缃戝叧杩滅▼杩炴帴鐨勫姞瀵咾EY
+ //if (DB_ResidenceData.Instance.CheckWhetherGatewayIsBound())
{
- //----绗簩姝ユ壘鍑烘槸鍚﹀瓨鍦ㄥ尮閰嶅綋鍓嶄綇瀹呯殑mac锛屽瓨鍦ㄥ啀杩涜杩滅▼銆�
- MqttInfoConfig.Current.HomeGatewayInfo = UserConfig.Instance.GatewayList[0];
- if (MqttInfoConfig.Current.HomeGatewayInfo != null)
- {
- //----绗笁姝� 寮�濮嬭繛鎺�
- await MQTTConnectAsync();
- }
+ //2.鎵惧嚭鏄惁瀛樺湪鍖归厤褰撳墠浣忓畢鐨刴ac锛屽瓨鍦ㄥ啀杩涜杩滅▼銆�
+ MqttInfoConfig.Current.HomeGatewayInfo = DB_ResidenceData.Instance.HomeGateway;
+ //3.寮�濮嬭繛鎺�
+ await MQTTConnectAsync();
}
- else
- {
- Utlis.WriteLine("============>杩樻病缁戝畾缃戝叧");
- }
- }
- else
- {
- Utlis.WriteLine("============>MqttInfo null");
+ //else
+ //{
+ // Utlis.WriteLine("============>杩樻病缁戝畾缃戝叧");
+ // hadGateway = false;
+ //}
}
}
+
}
}
@@ -849,4 +1016,4 @@
}
-}
+}
\ No newline at end of file
--
Gitblit v1.8.0