From 09c653a3dc33474d26427090834481ef378b57bc Mon Sep 17 00:00:00 2001
From: wxr <wxr@hdlchina.com.cn>
Date: 星期一, 30 九月 2024 17:48:17 +0800
Subject: [PATCH] 离线保存文件
---
HDL_ON/DAL/Mqtt/MqttClient.cs | 288 +++++++++++++++++++++++++++++++++++++-------------------
1 files changed, 189 insertions(+), 99 deletions(-)
diff --git a/HDL_ON/DAL/Mqtt/MqttClient.cs b/HDL_ON/DAL/Mqtt/MqttClient.cs
index e617b29..9593aef 100644
--- a/HDL_ON/DAL/Mqtt/MqttClient.cs
+++ b/HDL_ON/DAL/Mqtt/MqttClient.cs
@@ -12,6 +12,9 @@
using HDL_ON.Entity;
using HDL_ON.UI;
using HDL_ON.DAL.Server;
+using HDL_ON;
+using Newtonsoft.Json;
+using System.Collections.Generic;
namespace HDL_ON.DAL.Mqtt
{
@@ -56,7 +59,7 @@
/// <summary>
/// 鎺ㄩ�佹爣璇�
/// </summary>
- static string PushSignStr = DateTime.Now.Ticks.ToString();
+ static string PushSignStr = OnAppConfig.Instance.PushSignStr;
/// <summary>
/// 鏂紑杩滅▼Mqtt鐨勯摼鎺�
@@ -110,6 +113,7 @@
{
MqttInfoConfig.Current.IfGetMqttInfoSuccess = false;
}
+ //Control.Ins.GatewayOnline_Cloud = Control.Ins.GatewayOnline_Local = false;
await DisConnectRemoteMqttClient(s);
}
@@ -143,6 +147,7 @@
await StartCloudMqtt();
await SubscribeTopics();
+
}
catch { }
}
@@ -156,10 +161,11 @@
public static void InitState()
{
IfNeedReadAllDeviceStatus = true;
+ Common.ApiUtlis.Ins.hadInternet = true;
StartCloudMqtt();
}
- static bool isSubscribeSuccess;
+ public static bool isSubscribeSuccess;
/// <summary>
/// 璁㈤槄涓婚
/// </summary>
@@ -204,6 +210,13 @@
Topic = $"/user/{DB_ResidenceData.Instance.CurrentRegion.id}/app/thing/event/irCodeStudyDone/up",
QualityOfServiceLevel = MQTTnet.Protocol.MqttQualityOfServiceLevel.AtMostOnce
};
+ //App璁㈤槄缇ゆ帶鐘舵�佷富棰�
+ var groupControlStatus = new MqttTopicFilter()
+ {
+ Topic = $"/user/{DB_ResidenceData.Instance.CurrentRegion.id}/app/device/group/control/property/send",
+ QualityOfServiceLevel = MQTTnet.Protocol.MqttQualityOfServiceLevel.AtMostOnce
+ };
+
#region 鏁版嵁鏇存柊鎺ㄩ�佷富棰�
//appHomeRefresh锛氫綇瀹呮暟鎹埛鏂伴�氱煡--鏉ㄦ稕
@@ -216,6 +229,12 @@
var residenceChange = new MqttTopicFilter()
{
Topic = $"/user/{DB_ResidenceData.Instance.CurrentRegion.id}/app/thing/event/appDeviceRefresh/up",
+ QualityOfServiceLevel = MQTTnet.Protocol.MqttQualityOfServiceLevel.AtMostOnce
+ };
+ //璁惧鍦ㄧ嚎绂荤嚎鐘舵�佹帹閫�
+ var deviceOnlinePush = new MqttTopicFilter()
+ {
+ Topic = $"/user/{DB_ResidenceData.Instance.CurrentRegion.id}/app/son/session/online",
QualityOfServiceLevel = MQTTnet.Protocol.MqttQualityOfServiceLevel.AtMostOnce
};
//appRoomRefresh锛氭埧闂存暟鎹埛鏂伴�氱煡
@@ -248,12 +267,18 @@
#endregion
- Utlis.WriteLine("寮�濮嬭闃咃紒");
- var result = await RemoteMqttClient.SubscribeAsync(new MqttTopicFilter[] {
+ Utlis.WriteLine("寮�濮嬭闃咃紒");
+ var result = await RemoteMqttClient.SubscribeAsync(new MqttTopicFilter[] {
pirStatus,pirStudy,
appDeviceRefresh,appHomeRefresh,appRoomRefresh,residenceChange,
topicFilterPush2, topicAlinkStatus ,mqttkeyChange,
+ deviceOnlinePush,
securityStatusChange});
+ //璁㈤槄缇ゆ帶鐘舵��
+ if (DB_ResidenceData.Instance.HomeGateway.isSupportGroupControl)
+ {
+ await RemoteMqttClient.SubscribeAsync(groupControlStatus);
+ }
if (result.Items[0].ResultCode == MQTTnet.Client.Subscribing.MqttClientSubscribeResultCode.GrantedQoS0)
{
isSubscribeSuccess = true;
@@ -268,20 +293,71 @@
}
/// <summary>
+ /// 璁㈤槄缁戝畾绗笁鏂筰ot璐﹀彿缁撴灉
+ /// </summary>
+ /// <returns></returns>
+ public static async Task<bool> SubscribeAsync3tyIotbind()
+ {
+ var topicFilter = new MqttTopicFilter()
+ {
+ Topic = $"/user/{UserInfo.Current.ID}/app/third/bind/send",
+ QualityOfServiceLevel = MQTTnet.Protocol.MqttQualityOfServiceLevel.AtMostOnce
+ };
+ var result = await RemoteMqttClient.SubscribeAsync(topicFilter);
+ if (result.Items[0].ResultCode == MQTTnet.Client.Subscribing.MqttClientSubscribeResultCode.GrantedQoS0)
+ {
+ Utlis.WriteLine("璁㈤槄缁戝畾绗笁鏂筰ot璐﹀彿缁撴灉鎴愬姛锛�");
+ return true;
+ }
+ return false;
+ }
+ /// <summary>
+ /// 鍙栨秷璁㈤槄缁戝畾绗笁鏂筰ot璐﹀彿缁撴灉
+ /// </summary>
+ /// <returns></returns>
+ public static async Task<bool> UnsubscribeAsync3tyIotbind()
+ {
+ string[] strs = new string[] {
+ $"/user/{UserInfo.Current.ID}/app/third/bind/send"
+ };
+
+ var result = await RemoteMqttClient.UnsubscribeAsync(strs);
+
+ if (result.Items[0].ReasonCode == MQTTnet.Client.Unsubscribing.MqttClientUnsubscribeResultCode.Success)
+ {
+ Utlis.WriteLine("鍙栨秷璁㈤槄缁戝畾绗笁鏂筰ot璐﹀彿缁撴灉鎴愬姛锛�");
+ return true;
+ }
+ return false;
+ }
+ /// <summary>
+ /// 缁戝畾绗笁鏂瑰钩鍙版帴鏀跺洖璋冧簨浠�
+ /// </summary>
+ public static Action Bind3tyIotAction;
+
+ /// <summary>
/// 鍚姩杩滅▼Mqtt
/// </summary>
public static async Task StartCloudMqtt()
{
- if (MainPage.InternetStatus == 0)
+ //娌℃湁缃戠粶鐨勭姸鎬佷笅灏濊瘯涓�涓嬭繛鎺qtt 锛屽畨鍗撶殑缃戠粶鐘舵�佸彉鍖栫洃鍚湁寮傚父锛屼慨鏀瑰簳灞傞夯鐑�
+ if (MainPage.InternetStatus == 0 && MainPage.LinkHdlMqttCount > 1 )
{
return;
}
- if (!UserInfo.Current.IsLogin)
+
+ if (!Common.ApiUtlis.Ins.hadInternet)
{
return;
}
- if (DB_ResidenceData.Instance.CurrentRegion == null || DB_ResidenceData.Instance.CurrentRegion.id == null)
+ MainPage.LinkHdlMqttCount++;
+
+ //if (!UserInfo.Current.IsLogin)
+ //{
+ // return;
+ //}
+ if (OnAppConfig.Instance.LastLoginUserId == ""|| DB_ResidenceData.Instance.CurrentRegion == null || DB_ResidenceData.Instance.CurrentRegion.id == null)
{
return;
}
@@ -297,6 +373,26 @@
await Task.Factory.StartNew((Func<Task>)(async () => {
try
{
+ try
+ {
+ //澧炲姞3绉掍簯鏈嶅姟鍣ㄨ繛鎺ユ娴嬶紝杩炰笉涓婃湇鍔″櫒鐨勬椂鍊欎笉鍒锋柊鏁版嵁
+ var dataList = new List<GlobalRegionListRes>();
+ var requestJson = HttpUtil.GetSignRequestJson(new GetRegionListObj() { regionMark = HttpUtil.RegionMark });
+ var revertObj = HttpUtil.RequestHttpsPost(NewAPI.API_POST_GlobalRegionList, requestJson, HttpUtil.GlobalRequestHttpsHost, "", 3);
+ if (revertObj == null || revertObj.Code != StateCode.SUCCESS)
+ {
+ Common.ApiUtlis.Ins.hadInternet = false;
+ return;
+ }
+ else
+ {
+ Common.ApiUtlis.Ins.hadInternet = true;
+ }
+ }
+ catch
+ {
+ }
+
#region 鍒濆鍖栬繙绋婱qtt
RemoteMqttIsConnecting = true;
RemoteMqttClient = new MqttFactory().CreateMqttClient();
@@ -310,7 +406,7 @@
try
{
var topic = e.ApplicationMessage.Topic;
- //MainPage.Log($"鏀跺埌mqtt涓婚:{topic}");
+ MainPage.Log($"鏀跺埌mqtt涓婚:{topic}");
//涓�绔彛涓婚澶勭悊
if (DB_ResidenceData.Instance.GatewayType == 0 && !DB_ResidenceData.Instance.CheckWhetherGatewayIdIsNull())
{
@@ -345,6 +441,38 @@
//鏂版尋涓嬬嚎涓婚鏂规 鏀跺埌鎸や笅绾夸富棰�
ReceiveNotifySqueezeAsync(mMes);
}
+ //缁戝畾绗笁鏂瑰钩鍙扮粨鏋滈�氱煡
+ else if(topic == $"/user/{UserInfo.Current.ID}/app/third/bind/send")
+ {
+ var revString = Encoding.UTF8.GetString(e.ApplicationMessage.Payload);
+ Bind3tyIotAction?.Invoke();
+ }
+ //璁惧鍦ㄧ嚎绂荤嚎鐘舵�佹帹閫�
+ else if (topic == $"/user/{DB_ResidenceData.Instance.CurrentRegion.id}/app/son/session/online")
+ {
+ try
+ {
+ //var mMes = Encoding.UTF8.GetString(e.ApplicationMessage.Payload);
+ //Newtonsoft.Json.Linq.JObject pairs = Newtonsoft.Json.Linq.JObject.Parse(mMes);
+ //var pairSid = pairs.GetValue("sid").ToString();
+ //var pairOnline = pairs.GetValue("online");
+ //if (!string.IsNullOrEmpty(pairSid.ToString()))
+ //{
+ // var function_online = FunctionList.List.Functions.Find((obj) => obj.sid == pairSid);
+ // if (function_online != null){
+ // function_online.online = (bool)pairOnline;
+ // HomePage.LoadEvent_RefreshDevcieOnline(function_online);
+ // FunctionPage.UpdataOnline(function_online);
+ // RoomPage.UpdataOnline(function_online);
+ // }
+ //}
+
+ }
+ catch (Exception ex)
+ {
+
+ }
+ }
//App璁㈤槄绾㈠瀹�/缃戝叧閬ユ帶鍣ㄦ坊鍔犳垚鍔熼�氱煡
else if (topic == $"/user/{DB_ResidenceData.Instance.CurrentRegion.id}/app/thing/topo/found")
{
@@ -361,10 +489,10 @@
}
#region 鏁版嵁鏇存柊鎺ㄩ�佷富棰�
//appHomeRefresh锛氫綇瀹呮暟鎹埛鏂伴�氱煡
- else if (topic == $"/user/{DB_ResidenceData.Instance.CurrentRegion.id}/app/thing/event/appHomeRefresh/up"
- || topic == $"/user/{DB_ResidenceData.Instance.CurrentRegion.id}/app/thing/event/appDeviceRefresh/up")
+ else if (topic == $"/user/{DB_ResidenceData.Instance.CurrentRegion.id}/app/thing/event/appHomeRefresh/up")
{
MainPage.Log("浣忓畢鏁版嵁鍒锋柊閫氱煡");
+ System.Threading.Thread.Sleep(2000);
new HttpServerRequest().GetHomePager();
}
//appRoomRefresh锛氭埧闂存暟鎹埛鏂伴�氱煡
@@ -393,48 +521,6 @@
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 (SPK.MusicSpkList().Contains(localFunction.spk))
- {
- 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")
@@ -450,7 +536,8 @@
{
try
{
- var securityBytes = Securitys.EncryptionService.AesDecryptPayload(e.ApplicationMessage.Payload, DB_ResidenceData.Instance.HomeGateway.aesKey);
+ var securityBytes = Securitys.EncryptionService.AesDecryptPayload(e.ApplicationMessage.Payload,
+ DB_ResidenceData.Instance.HomeGateway.aesKey);
var securityString = Encoding.UTF8.GetString(securityBytes);
var temp = Newtonsoft.Json.JsonConvert.DeserializeObject<SecurityStatusObj>(securityString);
MainPage.Log($"瀹夐槻鐘舵�佸彉鍖栵細{securityString}");
@@ -470,21 +557,36 @@
}
HomePage.LoadEvent_RefreshSecurityStatus();
}
- }catch(Exception ex)
+ }
+ catch (Exception ex)
{
MainPage.Log($"瀹夐槻杩滅▼淇℃伅寮傚父:{ex.Message}");
}
}
}
#endregion
+ //缇ゆ帶鐘舵��
+ else if (topic == $"/user/{DB_ResidenceData.Instance.CurrentRegion.id}/app/device/group/control/property/send")
+ {
+ var bytes = Securitys.EncryptionService.AesDecryptPayload(e.ApplicationMessage.Payload, tuyaEncryptKey);
+ var revString = Encoding.UTF8.GetString(bytes);
+ MainPage.Log($"mqtt 缇ゆ帶鐘舵�佹洿鏂�:{revString}");
+ Control.Ins.UpdataGroupControlStatus(revString, null, true);
+ }
//A缃戝叧璁惧鐘舵��-鍖呭惈娑傞甫璁惧
+ //Tag 缃戠粶鐘舵�佽В鏋�
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);
- MainPage.Log($"mqtt 鐘舵�佹洿鏂�:{revString}");
+ //MainPage.Log($"mqtt 鐘舵�佹洿鏂�:{revString}");
Control.Ins.UpdataFunctionStatus(revString, null, true);
+ //Control.Ins.MsgInfoList.Add($"mqtt 鐘舵�佹洿鏂�:{revString}");
//Control.Ins.MsgInfoList.Add(revString + "\r\n");
+ //if (FunctionList.List.OtherBrandFunction.Find((obj) => obj.sid == updateTemp.sid) == null)
+ //{
+ // return;
+ //}
}
//涓�绔彛鏁版嵁瑙f瀽
else
@@ -496,7 +598,8 @@
if (!string.IsNullOrEmpty(DB_ResidenceData.Instance.HomeGateway.aesKey))
{
- packet.Bytes = Securitys.EncryptionService.AesDecryptPayload(e.ApplicationMessage.Payload, DB_ResidenceData.Instance.HomeGateway.aesKey);
+ packet.Bytes = Securitys.EncryptionService.AesDecryptPayload(e.ApplicationMessage.Payload,
+ DB_ResidenceData.Instance.HomeGateway.aesKey);
}
else
{
@@ -525,6 +628,10 @@
{
IfNeedReadAllDeviceStatus = true;
Control.Ins.GatewayOnline_Cloud = true;
+ if(MainPage.InternetStatus == 0)
+ {
+ MainPage.InternetStatus = 1;
+ }
Utlis.WriteLine($"============>Mqtt杩滅▼杩炴帴鎴愬姛");
SendPushSignOut();
});
@@ -566,7 +673,7 @@
}
catch (Exception ex)
{
- Utlis.WriteLine($"error:" + ex.Message);
+ Utlis.WriteLine($"mqtt杩炴帴寮傚父 error:" + ex.Message);
//mqtt杩炴帴寮傚父锛屾竻绌烘湰鍦癿qtt淇℃伅锛屽彲鑳介渶瑕侀噸鏂拌幏鍙栵細wxr
MqttInfoConfig.Current.Refresh();
}
@@ -753,19 +860,8 @@
/// </summary>
static void ReceiveNotifySqueezeAsync(string mMes)
{
+ MainPage.Log($"鎺ㄩ�佹爣璇嗭細{mMes};鏈湴鏍囪瘑锛歿PushSignStr}");
if (mMes == PushSignStr) return;//鏄嚜宸辩殑鐧诲綍鎺ㄩ�佷笉澶勭悊//鎴栬�呭綋鍓嶄笉鏄繙绋嬮摼鎺ョ姸鎬�
- //娴嬭瘯璐﹀彿锛屼笉鎸や笅绾�
- switch (UserInfo.Current.userMobileInfo)
- {
- case "15626203746":
- case "464027401@qq.com":
- return;
- }
-
- if (!UserInfo.Current.IsLogin)
- {
- return;
- }
DisConnectRemoteMqttClient("鎸や笅绾�");
@@ -774,40 +870,26 @@
//寮圭獥鎻愮ず琚尋涓嬬嚎
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;
-
- // Utlis.WriteLine("ReceiveCheckGateway锛�");
-
- // //CheckIfNeedReadAllDeviceStatus ();
-
- // //var obj = Newtonsoft.Json.JsonConvert.DeserializeObject<ResponsePack>(mMes);
- // Control.Ins.GatewayOnline = true;
- //}
/// <summary>
/// 鎺ㄩ�佹尋涓嬬嚎涓婚
/// </summary>
static void SendPushSignOut()
{
+#if DEBUG
+ MainPage.Log("璋冭瘯妯″紡锛屼笉鍙戝竷鎸や笅绾夸富棰樸��");
+ return;
+#endif
+ //娴嬭瘯璐﹀彿锛屼笉鎸や笅绾�
+ switch (UserInfo.Current.AccountString)
+ {
+ case "15626203746":
+ case "13580507523":
+ case "18475593023":
+ return;
+ }
+
byte[] message = Encoding.UTF8.GetBytes(PushSignStr);
MqttRemoteSend(message, 4);
}
@@ -835,7 +917,6 @@
break;
case 4://鍙戝竷鏂版柟妗堢殑鎸や笅绾夸富棰�
topicName = $"/BusGateWayToClient/{UserInfo.Current.ID}" + PushNotifySqueeze;
- //message = Encoding.UTF8.GetBytes (PushSignStr);
await RemoteMqttClient.PublishAsync(new MqttApplicationMessage { Topic = topicName, Payload = message, Retain = false, QualityOfServiceLevel = MQTTnet.Protocol.MqttQualityOfServiceLevel.AtMostOnce });
break;
}
@@ -897,7 +978,7 @@
MqttInfoConfig.Current.mMqttInfo = mqttInfoRequestResult_Obj;
- await MQTTConnectAsync();
+ //await MQTTConnectAsync();
//1.鍒ゆ柇鏄惁缁戝畾浜嗙綉鍏筹紝鑾峰彇缃戝叧杩滅▼杩炴帴鐨勫姞瀵咾EY
//if (DB_ResidenceData.Instance.CheckWhetherGatewayIsBound())
{
@@ -1056,8 +1137,17 @@
//鍒涘缓AES瑙e瘑鍣ㄥ璞�
var cTransform = rm.CreateDecryptor();
+ byte[] reData = null;
//浣跨敤AES灏嗗瘑鏂囨祦杞垚鏄庢枃鐨勫瓧鑺傛暟缁�
- return cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
+ try
+ {
+ reData = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
+ }
+ catch (Exception ex)
+ {
+ MainPage.Log(ex.Message);
+ }
+ return reData;
}
#endregion
--
Gitblit v1.8.0