| | |
| | | using HDL_ON.UI; |
| | | using HDL_ON.DAL.Server; |
| | | using HDL_ON; |
| | | using Newtonsoft.Json; |
| | | using System.Collections.Generic; |
| | | |
| | | namespace HDL_ON.DAL.Mqtt |
| | | { |
| | |
| | | /// <summary> |
| | | /// 推送标识 |
| | | /// </summary> |
| | | static string PushSignStr = DateTime.Now.Ticks.ToString(); |
| | | static string PushSignStr = OnAppConfig.Instance.PushSignStr; |
| | | |
| | | /// <summary> |
| | | /// 断开远程Mqtt的链接 |
| | |
| | | { |
| | | MqttInfoConfig.Current.IfGetMqttInfoSuccess = false; |
| | | } |
| | | Control.Ins.GatewayOnline_Cloud = Control.Ins.GatewayOnline_Local = false; |
| | | //Control.Ins.GatewayOnline_Cloud = Control.Ins.GatewayOnline_Local = false; |
| | | await DisConnectRemoteMqttClient(s); |
| | | } |
| | | |
| | |
| | | new System.Threading.Thread(async () => { |
| | | while (true) |
| | | { |
| | | if (!UserInfo.Current.IsLogin) |
| | | { |
| | | return; |
| | | } |
| | | try |
| | | { |
| | | System.Threading.Thread.Sleep(2000); |
| | |
| | | |
| | | await StartCloudMqtt(); |
| | | await SubscribeTopics(); |
| | | |
| | | } |
| | | catch { } |
| | | } |
| | |
| | | public static void InitState() |
| | | { |
| | | IfNeedReadAllDeviceStatus = true; |
| | | Common.ApiUtlis.Ins.hadInternet = true; |
| | | StartCloudMqtt(); |
| | | } |
| | | |
| | |
| | | public static async Task StartCloudMqtt() |
| | | { |
| | | //没有网络的状态下尝试一下连接mqtt ,安卓的网络状态变化监听有异常,修改底层麻烦 |
| | | if (MainPage.InternetStatus == 0 && MainPage.LinkHdlMqttCount > 1) |
| | | if (MainPage.InternetStatus == 0 && MainPage.LinkHdlMqttCount > 1 ) |
| | | { |
| | | return; |
| | | } |
| | | |
| | | |
| | | if (!Common.ApiUtlis.Ins.hadInternet) |
| | | { |
| | | return; |
| | | } |
| | | MainPage.LinkHdlMqttCount++; |
| | | |
| | | if (!UserInfo.Current.IsLogin) |
| | | { |
| | | return; |
| | | } |
| | | if (DB_ResidenceData.Instance.CurrentRegion == null || DB_ResidenceData.Instance.CurrentRegion.id == null) |
| | | //if (!UserInfo.Current.IsLogin) |
| | | //{ |
| | | // return; |
| | | //} |
| | | if (OnAppConfig.Instance.LastLoginUserId == ""|| DB_ResidenceData.Instance.CurrentRegion == null || DB_ResidenceData.Instance.CurrentRegion.id == null) |
| | | { |
| | | return; |
| | | } |
| | |
| | | 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(); |
| | |
| | | { |
| | | 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"); |
| | |
| | | /// </summary> |
| | | static void ReceiveNotifySqueezeAsync(string mMes) |
| | | { |
| | | #if DEBUG |
| | | //return; |
| | | #endif |
| | | MainPage.Log($"推送标识:{mMes};本地标识:{PushSignStr}"); |
| | | if (mMes == PushSignStr) return;//是自己的登录推送不处理//或者当前不是远程链接状态 |
| | | //测试账号,不挤下线 |
| | | switch (UserInfo.Current.userMobileInfo) |
| | | { |
| | | case "18316672920": |
| | | case "15626203746": |
| | | case "464027401@qq.com": |
| | | return; |
| | | } |
| | | |
| | | if (!UserInfo.Current.IsLogin) |
| | | { |
| | | return; |
| | | } |
| | | |
| | | DisConnectRemoteMqttClient("挤下线"); |
| | | |
| | |
| | | //弹窗提示被挤下线 |
| | | 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() |
| | | { |
| | | byte[] message = Encoding.UTF8.GetBytes(PushSignStr); |
| | | #if DEBUG |
| | | MqttRemoteSend(message, 4); |
| | | MainPage.Log("调试模式,不发布挤下线主题。"); |
| | | return; |
| | | #endif |
| | | //测试账号,不挤下线 |
| | | switch (UserInfo.Current.AccountString) |
| | | { |
| | | case "15626203746": |
| | | case "13580507523": |
| | | case "18475593023": |
| | | return; |
| | | } |
| | | |
| | | byte[] message = Encoding.UTF8.GetBytes(PushSignStr); |
| | | MqttRemoteSend(message, 4); |
| | | } |
| | | |
| | | /// <summary> |
| | |
| | | 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; |
| | | } |
| | |
| | | |
| | | |
| | | MqttInfoConfig.Current.mMqttInfo = mqttInfoRequestResult_Obj; |
| | | await MQTTConnectAsync(); |
| | | //await MQTTConnectAsync(); |
| | | //1.判断是否绑定了网关,获取网关远程连接的加密KEY |
| | | //if (DB_ResidenceData.Instance.CheckWhetherGatewayIsBound()) |
| | | { |