| | |
| | | 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 |
| | | { |
| | |
| | | { |
| | | MqttInfoConfig.Current.IfGetMqttInfoSuccess = false; |
| | | } |
| | | //Control.Ins.GatewayOnline_Cloud = Control.Ins.GatewayOnline_Local = false; |
| | | await DisConnectRemoteMqttClient(s); |
| | | } |
| | | |
| | |
| | | |
| | | await StartCloudMqtt(); |
| | | await SubscribeTopics(); |
| | | |
| | | } |
| | | catch { } |
| | | } |
| | |
| | | public static void InitState() |
| | | { |
| | | IfNeedReadAllDeviceStatus = true; |
| | | Common.ApiUtlis.Ins.hadInternet = true; |
| | | StartCloudMqtt(); |
| | | } |
| | | |
| | | static bool isSubscribeSuccess; |
| | | public static bool isSubscribeSuccess; |
| | | /// <summary> |
| | | /// 订阅主题 |
| | | /// </summary> |
| | |
| | | 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:住宅数据刷新通知--杨涛 |
| | |
| | | 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:房间数据刷新通知 |
| | |
| | | #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; |
| | |
| | | } |
| | | |
| | | /// <summary> |
| | | /// 订阅绑定第三方iot账号结果 |
| | | /// </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("订阅绑定第三方iot账号结果成功!"); |
| | | return true; |
| | | } |
| | | return false; |
| | | } |
| | | /// <summary> |
| | | /// 取消订阅绑定第三方iot账号结果 |
| | | /// </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("取消订阅绑定第三方iot账号结果成功!"); |
| | | return true; |
| | | } |
| | | return false; |
| | | } |
| | | /// <summary> |
| | | /// 绑定第三方平台接收回调事件 |
| | | /// </summary> |
| | | public static Action Bind3tyIotAction; |
| | | |
| | | /// <summary> |
| | | /// 启动远程Mqtt |
| | | /// </summary> |
| | | public static async Task StartCloudMqtt() |
| | | { |
| | | if (MainPage.InternetStatus == 0) |
| | | //没有网络的状态下尝试一下连接mqtt ,安卓的网络状态变化监听有异常,修改底层麻烦 |
| | | if (MainPage.InternetStatus == 0 && MainPage.LinkHdlMqttCount > 1 ) |
| | | { |
| | | return; |
| | | } |
| | | |
| | | |
| | | if (!Common.ApiUtlis.Ins.hadInternet) |
| | | { |
| | | return; |
| | | } |
| | | MainPage.LinkHdlMqttCount++; |
| | | |
| | | if (!UserInfo.Current.IsLogin) |
| | | { |
| | |
| | | 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 初始化远程Mqtt |
| | | RemoteMqttIsConnecting = true; |
| | | RemoteMqttClient = new MqttFactory().CreateMqttClient(); |
| | |
| | | try |
| | | { |
| | | var topic = e.ApplicationMessage.Topic; |
| | | //MainPage.Log($"收到mqtt主题:{topic}"); |
| | | MainPage.Log($"收到mqtt主题:{topic}"); |
| | | //一端口主题处理 |
| | | if (DB_ResidenceData.Instance.GatewayType == 0 && !DB_ResidenceData.Instance.CheckWhetherGatewayIdIsNull()) |
| | | { |
| | |
| | | //新挤下线主题方案 收到挤下线主题 |
| | | 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") |
| | | { |
| | |
| | | } |
| | | #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:房间数据刷新通知 |
| | |
| | | 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") |
| | |
| | | { |
| | | 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}"); |
| | |
| | | |
| | | if (temp != null) |
| | | { |
| | | Control_Udp.ReceiveRepeatManager(temp.id, null); |
| | | foreach (var updataSecurity in temp.objects) |
| | | { |
| | | var updataLocalSecurity = FunctionList.List.securities.Find((obj) => obj.sid == updataSecurity.sid); |
| | |
| | | } |
| | | 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; |
| | | //} |
| | | } |
| | | //一端口数据解析 |
| | | else |
| | |
| | | |
| | | 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 |
| | | { |
| | |
| | | { |
| | | IfNeedReadAllDeviceStatus = true; |
| | | Control.Ins.GatewayOnline_Cloud = true; |
| | | if(MainPage.InternetStatus == 0) |
| | | { |
| | | MainPage.InternetStatus = 1; |
| | | } |
| | | Utlis.WriteLine($"============>Mqtt远程连接成功"); |
| | | SendPushSignOut(); |
| | | }); |
| | |
| | | } |
| | | catch (Exception ex) |
| | | { |
| | | Utlis.WriteLine($"error:" + ex.Message); |
| | | Utlis.WriteLine($"mqtt连接异常 error:" + ex.Message); |
| | | //mqtt连接异常,清空本地mqtt信息,可能需要重新获取:wxr |
| | | MqttInfoConfig.Current.Refresh(); |
| | | } |
| | |
| | | switch (UserInfo.Current.userMobileInfo) |
| | | { |
| | | case "15626203746": |
| | | case "464027401@qq.com": |
| | | return; |
| | | } |
| | | |
| | |
| | | |
| | | |
| | | MqttInfoConfig.Current.mMqttInfo = mqttInfoRequestResult_Obj; |
| | | await MQTTConnectAsync(); |
| | | //await MQTTConnectAsync(); |
| | | //1.判断是否绑定了网关,获取网关远程连接的加密KEY |
| | | //if (DB_ResidenceData.Instance.CheckWhetherGatewayIsBound()) |
| | | { |
| | |
| | | //创建AES解密器对象 |
| | | 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 |
| | | |