| | |
| | | using MQTTnet; |
| | | using System.Text; |
| | | using System.Security.Cryptography; |
| | | using System.Collections.Generic; |
| | | |
| | | namespace SmartHome |
| | | { |
| | |
| | | ///// </summary> |
| | | //static readonly string TopicToApp = "/Push/NotifySqueeze"; |
| | | |
| | | /// <summary> |
| | | /// 是否获取过密钥 |
| | | /// </summary> |
| | | static bool isGetGatewaySecretKey = false; |
| | | |
| | | /// <summary> |
| | | /// 随机Key |
| | |
| | | /// <summary> |
| | | /// 断开远程Mqtt的链接 |
| | | /// </summary> |
| | | static async System.Threading.Tasks.Task DisConnectRemoteMqttClient (string s = "") |
| | | public static async System.Threading.Tasks.Task DisConnectRemoteMqttClient (string s = "") |
| | | { |
| | | try { |
| | | if (remoteIsConnected) { |
| | |
| | | bNeedStartTip = true; |
| | | bNeedConnectTip = true; |
| | | CommonConfig.Current.IfGetMqttInfoSuccess = false; |
| | | isGetGatewaySecretKey = false;//获取密钥标记为false |
| | | } |
| | | await DisConnectRemoteMqttClient (s); |
| | | } |
| | |
| | | //QualityOfServiceLevel = MQTTnet.Protocol.MqttQualityOfServiceLevel.ExactlyOnce |
| | | }; |
| | | |
| | | //当设备MQTT报文加密密钥发生变更时,云端通过该主题通知其他客户端刷新密钥,其它和该网关交互的客户端自行订阅该主题,该主题包不加密 |
| | | var topicFilterPush3 = new MqttTopicFilter { |
| | | Topic = $"/user/{CommonConfig.Current.HomeGatewayInfo.gatewayId}/custom/mqtt/secret/change", |
| | | QualityOfServiceLevel = MQTTnet.Protocol.MqttQualityOfServiceLevel.AtMostOnce, |
| | | }; |
| | | |
| | | |
| | | |
| | | Utlis.WriteLine ("开始订阅!"); |
| | | var result = await RemoteMqttClient.SubscribeAsync (new MqttTopicFilter [] { topicFilterBusGateWayToClient, topicFilterPush2 }); |
| | | var result = await RemoteMqttClient.SubscribeAsync (new MqttTopicFilter [] { topicFilterBusGateWayToClient, topicFilterPush2, topicFilterPush3 }); |
| | | if (result.Items [0].ResultCode == MQTTnet.Client.Subscribing.MqttClientSubscribeResultCode.GrantedQoS0) { |
| | | isSubscribeSuccess = true; |
| | | Utlis.WriteLine ("订阅成功!"); |
| | |
| | | } else if (topic == $"/BusGateWayToClient/{CommonConfig.Current.HomeGatewayInfo.gatewayId}/NotifyBusGateWayInfoChange") {//网关上线,需要更新aeskey |
| | | //收到网关上线消息主题 |
| | | ReceiveNotifyBusGateWayInfoChange (); |
| | | } else if (topic == $"/user/{CommonConfig.Current.HomeGatewayInfo.gatewayId}/custom/mqtt/secret/change") {//设备密钥更新通知,需要更新aeskey |
| | | //设备密钥更新通知 |
| | | HDLUtils.WriteLine ("设备密钥更新通知"); |
| | | isGetGatewaySecretKey = false;//获取密钥标记为false |
| | | GetGatewaySecretKey (); |
| | | } else if (topic == $"/BusGateWayToClient/{CommonConfig.Current.HomeGatewayInfo.gatewayId}/NotifyGateWayOffline") {//网关掉线 //----第二步:读取账号下面的网关列表 |
| | | ReceiveNotifyGateWayOffline (); |
| | | } else if (topic == $"/BusGateWayToClient/{CommonConfig.Current.HomeGatewayInfo.gatewayId}/Common/CheckGateway") { |
| | |
| | | //} |
| | | |
| | | //(4)===========开始连接过程========== |
| | | //之前已经获取参数成功过 |
| | | if (CommonConfig.Current.IfGetMqttInfoSuccess) { |
| | | //判断是否需要重新获取 |
| | | await CheckMQTTConnectAsync (); |
| | | } else { |
| | | //开始获取远程连接参数 |
| | | await StartMQTTGetInfo (); |
| | | //4.1 如果没获取过密钥则重新获取一次, 获取一次最新的密钥 |
| | | if (isGetGatewaySecretKey || GetGatewaySecretKey ()) { |
| | | //之前已经获取参数成功过 |
| | | if (CommonConfig.Current.IfGetMqttInfoSuccess) { |
| | | //判断是否需要重新获取 |
| | | await CheckMQTTConnectAsync (); |
| | | } else { |
| | | //开始获取远程连接参数 |
| | | await StartMQTTGetInfo (); |
| | | } |
| | | } |
| | | |
| | | } catch (Exception ex) { |
| | |
| | | //#endif |
| | | CheckIfNeedReadAllDeviceStatus (); |
| | | } |
| | | |
| | | GetSingleHomeGatewayPaggerAndMQTTConnectAsync (false); |
| | | |
| | | } catch { } |
| | | } |
| | | |
| | |
| | | /// </summary> |
| | | static void SendPushSignOut () |
| | | { |
| | | #if DEBUG |
| | | return; |
| | | #endif |
| | | |
| | | byte [] message = CommonPage.MyEncodingUTF8.GetBytes (PushSignStr); |
| | | MqttRemoteSend (message, 4); |
| | | } |
| | |
| | | case 0: |
| | | topicName = $"/ClientToBusGateWay/{CommonConfig.Current.HomeGatewayInfo.gatewayId}/Common/ON"; |
| | | if (!string.IsNullOrEmpty (mqttEncryptKey)) { |
| | | //Utlis.WriteLine ("mqttEncryptKey:" + mqttEncryptKey); |
| | | message = Shared.Securitys.EncryptionService.AesEncryptPayload (message, mqttEncryptKey); |
| | | } |
| | | await RemoteMqttClient.PublishAsync (new MqttApplicationMessage { Topic = topicName, Payload = message, Retain = false, QualityOfServiceLevel = MQTTnet.Protocol.MqttQualityOfServiceLevel.AtMostOnce }); |
| | |
| | | var mqttInfoRequestResult_Obj = HttpServerRequest.Current.GetMqttRemoteInfo (GetRandomKey ()); |
| | | if (mqttInfoRequestResult_Obj != null) { |
| | | CommonConfig.Current.mMqttInfo = mqttInfoRequestResult_Obj; |
| | | if (UserConfig.Instance.CheckHomeGatewaysNotEmpty()) { |
| | | if (UserConfig.Instance.CheckHomeGatewaysNotEmpty ()) { |
| | | //----第二步找出是否存在匹配当前住宅的mac,存在再进行远程。 |
| | | CommonConfig.Current.HomeGatewayInfo = UserConfig.Instance.HomeGateway; |
| | | if (CommonConfig.Current.HomeGatewayInfo != null) { |
| | |
| | | } |
| | | |
| | | |
| | | |
| | | /// <summary> |
| | | /// 分享住宅 获取当前住宅网关信息并且连接MQTT 或者刷新 |
| | | /// 2020-03-17 |
| | | /// 设备密钥更新通知,重新拿密钥 |
| | | /// </summary> |
| | | static async Task GetSingleHomeGatewayPaggerAndMQTTConnectAsync (bool bNeedConnect = true) |
| | | /// <returns></returns> |
| | | static bool GetGatewaySecretKey () |
| | | { |
| | | |
| | | bool isSuccess = false; |
| | | try { |
| | | if (string.IsNullOrEmpty (UserConfig.Instance.CurrentRegion.Id)) return false; |
| | | var nowhomeId = UserConfig.Instance.CurrentRegion.Id; |
| | | var requestJson = HttpUtil.GetSignRequestJson (new HomeIdObj () { homeId = nowhomeId }); |
| | | var revertObj = HttpUtil.RequestHttpsPostFroHome (NewAPI.API_POST_GetGatewayList, requestJson); |
| | | if (revertObj.Code == StateCode.SUCCESS) { |
| | | var mHomeGatewayRes = Newtonsoft.Json.JsonConvert.DeserializeObject<List<HomeGatewayInfo>> (revertObj.Data.ToString ()); |
| | | if (nowhomeId == UserConfig.Instance.CurrentRegion.Id) { |
| | | if (mHomeGatewayRes != null) { |
| | | if (mHomeGatewayRes.Count > 0) { |
| | | //只取第一个住宅 |
| | | var info = mHomeGatewayRes [0]; |
| | | UserConfig.Instance.HomeGateway = info; |
| | | UserConfig.Instance.SaveUserConfig (); |
| | | CommonConfig.Current.HomeGatewayInfo = info; |
| | | //更新网关信息和加密Key |
| | | mqttEncryptKey = info.aesKey; |
| | | CommonConfig.Current.Save (); |
| | | isSuccess = true; |
| | | isGetGatewaySecretKey = true;//获取密钥标记为true |
| | | |
| | | } else { |
| | | //打印错误 |
| | | HDLUtils.WriteLine ("gateway is not bound"); |
| | | } |
| | | } |
| | | } |
| | | } else { |
| | | //打印错误 |
| | | HDLUtils.WriteLine ("Failed to get gateway info"); |
| | | |
| | | } |
| | | return isSuccess; |
| | | } catch { |
| | | //return ""; |
| | | return isSuccess; |
| | | } |
| | | } |
| | | |
| | | |
| | |
| | | public partial class EncryptionService |
| | | { |
| | | |
| | | #region 加密 |
| | | #region 加密 |
| | | /// <summary> |
| | | /// 加密主题为Base64 |
| | | /// </summary> |
| | |
| | | //使用AES将明文流转成密文字节数组 |
| | | return cTransform.TransformFinalBlock (toEncryptArray, 0, toEncryptArray.Length); |
| | | } |
| | | #endregion |
| | | #endregion |
| | | |
| | | |
| | | #region 解密 |
| | | #region 解密 |
| | | /// <summary> |
| | | /// 解密主题数据 |
| | | /// </summary> |
| | |
| | | //使用AES将密文流转成明文的字节数组 |
| | | return cTransform.TransformFinalBlock (toEncryptArray, 0, toEncryptArray.Length); |
| | | } |
| | | #endregion |
| | | #endregion |
| | | |
| | | |
| | | |