| | |
| | | /// 是否搜索本地网关成功 |
| | | /// </summary> |
| | | public bool IsSearchLocalGatewaySuccessful = false; |
| | | /// <summary> |
| | | /// 是否本地加密,目前只对A网关有用 |
| | | /// </summary> |
| | | public bool IsLocalEncrypt; |
| | | /// <summary> |
| | | /// 判断是否本地加密并且加密key不为空 |
| | | /// </summary> |
| | | public bool IsLocalEncryptAndGetAesKey { |
| | | get { |
| | | return IsLocalEncrypt && (!string.IsNullOrEmpty(DB_ResidenceData.Instance.CurrentRegion.localSecret)); |
| | | } |
| | | } |
| | | |
| | | bool _GatewayOnline_Local = false; |
| | | /// <summary> |
| | |
| | | /// <summary> |
| | | /// 转换发送数据 |
| | | /// </summary> |
| | | public byte[] ConvertSendBodyData(string topic, string bodyDataString) |
| | | /// <param name="topic">主题</param> |
| | | /// <param name="bodyDataString">body内容数据</param> |
| | | /// <param name="isEncryption">是否要对body加密</param> |
| | | /// <returns></returns> |
| | | public byte[] ConvertSendBodyData(string topic, string bodyDataString, bool isEncryption = true) |
| | | { |
| | | string topicString = "Topic:" + topic + "\r\n"; |
| | | byte[] bodyBytes = Encoding.ASCII.GetBytes(bodyDataString); |
| | | string lengthString = "Length:" + bodyBytes.Length.ToString() + "\r\n" + "\r\n"; |
| | | //string topicString = "Topic:" + topic + "\r\n"; |
| | | //byte[] bodyBytes = Encoding.ASCII.GetBytes(bodyDataString); |
| | | //string lengthString = "Length:" + bodyBytes.Length.ToString() + "\r\n" + "\r\n"; |
| | | |
| | | string sendDataString = topicString + lengthString + bodyDataString; |
| | | byte[] sendDataBytes = Encoding.ASCII.GetBytes(sendDataString); |
| | | MainPage.Log($"转换HDL-Link数据\r\n{sendDataString}\r\n"); |
| | | //string sendDataString = topicString + lengthString + bodyDataString; |
| | | //byte[] sendDataBytes = Encoding.ASCII.GetBytes(sendDataString); |
| | | //MainPage.Log($"转换HDL-Link数据\r\n{sendDataString}\r\n"); |
| | | |
| | | //*************************************************************** |
| | | //2021-09-23 增加本地通信加密处理 |
| | | //1.拼接头 |
| | | string topicString = "Topic:" + topic + "\r\n"; |
| | | //2.Body字符串转为byte数组 |
| | | byte[] bodyBytes = Encoding.ASCII.GetBytes(bodyDataString); |
| | | //判断是否需加密Body数据 |
| | | if (isEncryption && IsLocalEncryptAndGetAesKey) |
| | | { |
| | | bodyBytes = Securitys.EncryptionService.AesEncryptPayload(bodyBytes, DB_ResidenceData.Instance.CurrentRegion.localSecret); |
| | | //bodyDataString = Encoding.UTF8.GetString(bodyBytes); |
| | | //MainPage.Log($"转换HDL-Link数据 加密key:" + DB_ResidenceData.Instance.CurrentRegion.localSecret); |
| | | } |
| | | //3.拼接body的Length长度数据 |
| | | string lengthString = "Length:" + bodyBytes.Length.ToString() + "\r\n" + "\r\n"; |
| | | string topicAndLengthString = topicString + lengthString; |
| | | byte[] topicAndLengthBytes = Encoding.ASCII.GetBytes(topicAndLengthString); |
| | | //4.拼接合并 Topic 和 body的byte数组数据 |
| | | byte[] sendDataBytes = new byte[topicAndLengthBytes.Length + bodyBytes.Length]; |
| | | topicAndLengthBytes.CopyTo(sendDataBytes, 0); |
| | | bodyBytes.CopyTo(sendDataBytes, topicAndLengthBytes.Length); |
| | | |
| | | //var sendDataString = Encoding.UTF8.GetString(sendDataBytes); |
| | | //MainPage.Log($"转换HDL-Link数据\r\n{sendDataString}\r\n"); |
| | | //*************************************************************** |
| | | |
| | | return sendDataBytes; |
| | | } |
| | |
| | | public void ConvertReceiveData(byte[] receiveBytes) |
| | | { |
| | | var reString = Encoding.UTF8.GetString(receiveBytes); |
| | | AnalysisReceiveData(reString); |
| | | AnalysisReceiveData(reString, receiveBytes); |
| | | } |
| | | /// <summary> |
| | | /// 转换接收到的数据 |
| | | /// </summary> |
| | | /// <param name="receiveString">转String后的数据</param> |
| | | /// <param name="originalReceiveBytes"原始Bytes数据</param> |
| | | /// <returns></returns> |
| | | public LocalCommunicationData AnalysisReceiveData(string receiveString) |
| | | public LocalCommunicationData AnalysisReceiveData(string receiveString, byte[] originalReceiveBytes) |
| | | { |
| | | LocalCommunicationData receiveObj = new LocalCommunicationData(); |
| | | |
| | | MainPage.Log($"局域网信息: \r\n{receiveString}"); |
| | | |
| | | var res = receiveString.Split("\r\n\r\n"); |
| | | |
| | |
| | | } |
| | | } |
| | | |
| | | MainPage.Log($"局域网信息: {receiveObj.Topic} : 内容: {res[1]}"); |
| | | |
| | | //MainPage.Log($"局域网信息: {receiveObj.Topic} : 内容: {res[1]}"); |
| | | |
| | | //验证有效数据长度 |
| | | //if (res[1].Length != receiveObj.Length) |
| | |
| | | // return receiveObj; |
| | | //} |
| | | receiveObj.BodyDataString = res[1]; |
| | | |
| | | //2021-09-23 过滤不需要解密的主题 目前搜索网关主题不加密 |
| | | if (receiveObj.Topic != CommunicationTopic.SearchLoaclGatewayReply) |
| | | { |
| | | //判断当前网关是否开启了本地加密 |
| | | if (IsLocalEncryptAndGetAesKey) |
| | | { |
| | | MainPage.Log($"局域网信息 开始解密"); |
| | | if (originalReceiveBytes != null) |
| | | { |
| | | //拿到原始Bytes数据去解密 |
| | | byte[] topicBytes = Encoding.UTF8.GetBytes(res[0]); |
| | | byte[] bodyBytes = new byte[receiveObj.Length]; |
| | | Array.Copy(originalReceiveBytes, topicBytes.Length + 4, bodyBytes, 0, receiveObj.Length); |
| | | byte[] receiveBytes = Securitys.EncryptionService.AesDecryptPayload(bodyBytes, DB_ResidenceData.Instance.CurrentRegion.localSecret); |
| | | var revString = Encoding.UTF8.GetString(receiveBytes); |
| | | receiveObj.BodyDataString = revString; |
| | | MainPage.Log($"局域网信息: 解密后:" + receiveObj.BodyDataString); |
| | | } |
| | | else |
| | | { |
| | | //目前不拿原始Bytes数据 解密不了 |
| | | //byte[] receiveBytes = Encoding.UTF8.GetBytes(res[1]); |
| | | //MainPage.Log($"局域网信息 receiveBytes {receiveBytes.Length}"); |
| | | //receiveBytes = Securitys.EncryptionService.AesDecryptPayload(receiveBytes, DB_ResidenceData.Instance.CurrentRegion.localSecret); |
| | | //MainPage.Log($"局域网信息 解密后:receiveBytes {receiveBytes.Length}"); |
| | | //var revString = Encoding.UTF8.GetString(receiveBytes); |
| | | //receiveObj.BodyDataString = revString; |
| | | //MainPage.Log($"局域网信息: 解密后:" + receiveObj.BodyDataString); |
| | | } |
| | | |
| | | } |
| | | } |
| | | |
| | | |
| | | if (receiveObj.Topic == CommunicationTopic.SearchLoaclGatewayReply || receiveObj.Topic == CommunicationTopic.GatewayBroadcast) |
| | | { |
| | |
| | | Ins.GatewayId = device.device_mac; |
| | | } |
| | | reportIp = device.ip_address;//主播地址也能控制设备//"239.0.168.188";// |
| | | //2021-09-23 新增获取当前网关是否本地加密 |
| | | Ins.IsLocalEncrypt = device.isLocalEncrypt; |
| | | //MainPage.Log("网关本地加密状态:" + device.local_encrypt.ToString()); |
| | | } |
| | | } |
| | | else if (receiveObj.Topic == CommunicationTopic.ct.ReadStatus + "_reply" || |