diff --git a/SensorHub.Noise/Noise.cs b/SensorHub.Noise/Noise.cs index 3a4501b..fd2db25 100644 --- a/SensorHub.Noise/Noise.cs +++ b/SensorHub.Noise/Noise.cs @@ -462,12 +462,11 @@ { // 如果有这个文件(参数是存储位置+文件名称,打开方式)打开文件末尾如果没有创建一个文件流的类并加入参数后实例化 string path = Common.GetWindowsServiceInstallPath(ConfigurationManager.AppSettings["ServiceName"]) + "\\Datas\\"; - FileStream fileStream = new FileStream(path + DateTime.Now.ToString("yyyyMMddHHmmss-") + obj + ".txt", FileMode.Append); + FileStream fileStream = new FileStream(path + DateTime.Now.ToString("yyyyMMddHHmmss-") + obj + ".noi", FileMode.Append); // 创建一个输入流的类并加入参数(文件路径和编码格式)实例化 StreamWriter write = new StreamWriter(fileStream, Encoding.Default); StringBuilder exdata = new StringBuilder();//这里是创建写入数据的StringBuilder对象 - exdata.Append("共").Append(AccelerationDatas.acclerationValueMap[(string)obj].Length).Append("行").AppendLine(); try { for (int i = 0; i < AccelerationDatas.acclerationValueMap[(string) obj].Length; i++) diff --git a/SensorHub.Noise/Noise.cs b/SensorHub.Noise/Noise.cs index 3a4501b..fd2db25 100644 --- a/SensorHub.Noise/Noise.cs +++ b/SensorHub.Noise/Noise.cs @@ -462,12 +462,11 @@ { // 如果有这个文件(参数是存储位置+文件名称,打开方式)打开文件末尾如果没有创建一个文件流的类并加入参数后实例化 string path = Common.GetWindowsServiceInstallPath(ConfigurationManager.AppSettings["ServiceName"]) + "\\Datas\\"; - FileStream fileStream = new FileStream(path + DateTime.Now.ToString("yyyyMMddHHmmss-") + obj + ".txt", FileMode.Append); + FileStream fileStream = new FileStream(path + DateTime.Now.ToString("yyyyMMddHHmmss-") + obj + ".noi", FileMode.Append); // 创建一个输入流的类并加入参数(文件路径和编码格式)实例化 StreamWriter write = new StreamWriter(fileStream, Encoding.Default); StringBuilder exdata = new StringBuilder();//这里是创建写入数据的StringBuilder对象 - exdata.Append("共").Append(AccelerationDatas.acclerationValueMap[(string)obj].Length).Append("行").AppendLine(); try { for (int i = 0; i < AccelerationDatas.acclerationValueMap[(string) obj].Length; i++) diff --git a/SensorHub.Servers/AepSdkCore.cs b/SensorHub.Servers/AepSdkCore.cs index 73a12c0..7d97e81 100644 --- a/SensorHub.Servers/AepSdkCore.cs +++ b/SensorHub.Servers/AepSdkCore.cs @@ -200,8 +200,11 @@ //设置https验证方式 if (url.StartsWith("https", StringComparison.OrdinalIgnoreCase)) { - ServicePointManager.ServerCertificateValidationCallback = - new RemoteCertificateValidationCallback(CertificateValidation); + ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CertificateValidation); + + // 尝试解决偶尔出现的 "请求被中止: 未能创建 SSL/TLS 安全通道" 异常 + ServicePointManager.Expect100Continue = true; + ServicePointManager.SecurityProtocol = (SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12 | SecurityProtocolType.Ssl3); } request = (HttpWebRequest)WebRequest.Create(url); diff --git a/SensorHub.Noise/Noise.cs b/SensorHub.Noise/Noise.cs index 3a4501b..fd2db25 100644 --- a/SensorHub.Noise/Noise.cs +++ b/SensorHub.Noise/Noise.cs @@ -462,12 +462,11 @@ { // 如果有这个文件(参数是存储位置+文件名称,打开方式)打开文件末尾如果没有创建一个文件流的类并加入参数后实例化 string path = Common.GetWindowsServiceInstallPath(ConfigurationManager.AppSettings["ServiceName"]) + "\\Datas\\"; - FileStream fileStream = new FileStream(path + DateTime.Now.ToString("yyyyMMddHHmmss-") + obj + ".txt", FileMode.Append); + FileStream fileStream = new FileStream(path + DateTime.Now.ToString("yyyyMMddHHmmss-") + obj + ".noi", FileMode.Append); // 创建一个输入流的类并加入参数(文件路径和编码格式)实例化 StreamWriter write = new StreamWriter(fileStream, Encoding.Default); StringBuilder exdata = new StringBuilder();//这里是创建写入数据的StringBuilder对象 - exdata.Append("共").Append(AccelerationDatas.acclerationValueMap[(string)obj].Length).Append("行").AppendLine(); try { for (int i = 0; i < AccelerationDatas.acclerationValueMap[(string) obj].Length; i++) diff --git a/SensorHub.Servers/AepSdkCore.cs b/SensorHub.Servers/AepSdkCore.cs index 73a12c0..7d97e81 100644 --- a/SensorHub.Servers/AepSdkCore.cs +++ b/SensorHub.Servers/AepSdkCore.cs @@ -200,8 +200,11 @@ //设置https验证方式 if (url.StartsWith("https", StringComparison.OrdinalIgnoreCase)) { - ServicePointManager.ServerCertificateValidationCallback = - new RemoteCertificateValidationCallback(CertificateValidation); + ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CertificateValidation); + + // 尝试解决偶尔出现的 "请求被中止: 未能创建 SSL/TLS 安全通道" 异常 + ServicePointManager.Expect100Continue = true; + ServicePointManager.SecurityProtocol = (SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12 | SecurityProtocolType.Ssl3); } request = (HttpWebRequest)WebRequest.Create(url); diff --git a/SensorHub.Servers/Common.cs b/SensorHub.Servers/Common.cs index 86233f2..7da8617 100644 --- a/SensorHub.Servers/Common.cs +++ b/SensorHub.Servers/Common.cs @@ -176,8 +176,8 @@ else { String strBase64Value = Convert.ToBase64String(afcrc); - int ret = SendNACommand(session, strBase64Value, source); - if (ret != 201) + int ret = SendAEPCommand(session, strBase64Value, source); + if (ret != 0) { session.Logger.Info("电信平台下发配置信息失败,返回的Http状态码:" + ret); } diff --git a/SensorHub.Noise/Noise.cs b/SensorHub.Noise/Noise.cs index 3a4501b..fd2db25 100644 --- a/SensorHub.Noise/Noise.cs +++ b/SensorHub.Noise/Noise.cs @@ -462,12 +462,11 @@ { // 如果有这个文件(参数是存储位置+文件名称,打开方式)打开文件末尾如果没有创建一个文件流的类并加入参数后实例化 string path = Common.GetWindowsServiceInstallPath(ConfigurationManager.AppSettings["ServiceName"]) + "\\Datas\\"; - FileStream fileStream = new FileStream(path + DateTime.Now.ToString("yyyyMMddHHmmss-") + obj + ".txt", FileMode.Append); + FileStream fileStream = new FileStream(path + DateTime.Now.ToString("yyyyMMddHHmmss-") + obj + ".noi", FileMode.Append); // 创建一个输入流的类并加入参数(文件路径和编码格式)实例化 StreamWriter write = new StreamWriter(fileStream, Encoding.Default); StringBuilder exdata = new StringBuilder();//这里是创建写入数据的StringBuilder对象 - exdata.Append("共").Append(AccelerationDatas.acclerationValueMap[(string)obj].Length).Append("行").AppendLine(); try { for (int i = 0; i < AccelerationDatas.acclerationValueMap[(string) obj].Length; i++) diff --git a/SensorHub.Servers/AepSdkCore.cs b/SensorHub.Servers/AepSdkCore.cs index 73a12c0..7d97e81 100644 --- a/SensorHub.Servers/AepSdkCore.cs +++ b/SensorHub.Servers/AepSdkCore.cs @@ -200,8 +200,11 @@ //设置https验证方式 if (url.StartsWith("https", StringComparison.OrdinalIgnoreCase)) { - ServicePointManager.ServerCertificateValidationCallback = - new RemoteCertificateValidationCallback(CertificateValidation); + ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CertificateValidation); + + // 尝试解决偶尔出现的 "请求被中止: 未能创建 SSL/TLS 安全通道" 异常 + ServicePointManager.Expect100Continue = true; + ServicePointManager.SecurityProtocol = (SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12 | SecurityProtocolType.Ssl3); } request = (HttpWebRequest)WebRequest.Create(url); diff --git a/SensorHub.Servers/Common.cs b/SensorHub.Servers/Common.cs index 86233f2..7da8617 100644 --- a/SensorHub.Servers/Common.cs +++ b/SensorHub.Servers/Common.cs @@ -176,8 +176,8 @@ else { String strBase64Value = Convert.ToBase64String(afcrc); - int ret = SendNACommand(session, strBase64Value, source); - if (ret != 201) + int ret = SendAEPCommand(session, strBase64Value, source); + if (ret != 0) { session.Logger.Info("电信平台下发配置信息失败,返回的Http状态码:" + ret); } diff --git a/SensorHub.Servers/TelecomAEPReceiveFilter.cs b/SensorHub.Servers/TelecomAEPReceiveFilter.cs index edc4ebd..93d33f3 100644 --- a/SensorHub.Servers/TelecomAEPReceiveFilter.cs +++ b/SensorHub.Servers/TelecomAEPReceiveFilter.cs @@ -79,12 +79,23 @@ } } //////////////////////////////////////////////////////////////////////// - // 对tag进行sm4解密 + // 对tag进行解密 + int encodeType = GetEncodeType(pduType); + byte[] dat = new byte[data.Length / 2 - 18]; Array.Copy(src, 16, dat, 0, data.Length / 2 - 18); - - string plainText = SM4Utils.sm4Decrypt(dat, deviceId); - settings = plainText.Substring(0, Convert.ToInt32(leng, 16) * 2 - 12 * 2); + + if (encodeType == 0) + { + // SM4加密方式 + string plainText = SM4Utils.sm4Decrypt(dat, deviceId); + settings = plainText.Substring(0, Convert.ToInt32(leng, 16) * 2 - 12 * 2); + } else if (encodeType == 1) + { + // TEA加密方式 + TEA.decrypt(ref dat, dat.Length); + settings = BitConverter.ToString(dat, 0, dat.Length).Replace("-", "").Substring(0, Convert.ToInt32(leng, 16) * 2 - 12 * 2); + } result = Common.getDeviceTypeByPdu(pduType); @@ -95,5 +106,27 @@ BasicRequestInfoParser m_Parser = new BasicRequestInfoParser(":", ","); return m_Parser.ParseRequestInfo(result); } + + private int GetEncodeType(string pduType) + { + int type = 0; // 默认为SM4加密方式 + string devType = Common.getDeviceTypeByPdu(pduType); + switch(devType) + { + case "Noise": + // SM4加密方式 + break; + + case "Well": + // 井盖 TEA加密方式 + type = 1; + break; + + default: + break; + } + + return type; + } } } diff --git a/SensorHub.Noise/Noise.cs b/SensorHub.Noise/Noise.cs index 3a4501b..fd2db25 100644 --- a/SensorHub.Noise/Noise.cs +++ b/SensorHub.Noise/Noise.cs @@ -462,12 +462,11 @@ { // 如果有这个文件(参数是存储位置+文件名称,打开方式)打开文件末尾如果没有创建一个文件流的类并加入参数后实例化 string path = Common.GetWindowsServiceInstallPath(ConfigurationManager.AppSettings["ServiceName"]) + "\\Datas\\"; - FileStream fileStream = new FileStream(path + DateTime.Now.ToString("yyyyMMddHHmmss-") + obj + ".txt", FileMode.Append); + FileStream fileStream = new FileStream(path + DateTime.Now.ToString("yyyyMMddHHmmss-") + obj + ".noi", FileMode.Append); // 创建一个输入流的类并加入参数(文件路径和编码格式)实例化 StreamWriter write = new StreamWriter(fileStream, Encoding.Default); StringBuilder exdata = new StringBuilder();//这里是创建写入数据的StringBuilder对象 - exdata.Append("共").Append(AccelerationDatas.acclerationValueMap[(string)obj].Length).Append("行").AppendLine(); try { for (int i = 0; i < AccelerationDatas.acclerationValueMap[(string) obj].Length; i++) diff --git a/SensorHub.Servers/AepSdkCore.cs b/SensorHub.Servers/AepSdkCore.cs index 73a12c0..7d97e81 100644 --- a/SensorHub.Servers/AepSdkCore.cs +++ b/SensorHub.Servers/AepSdkCore.cs @@ -200,8 +200,11 @@ //设置https验证方式 if (url.StartsWith("https", StringComparison.OrdinalIgnoreCase)) { - ServicePointManager.ServerCertificateValidationCallback = - new RemoteCertificateValidationCallback(CertificateValidation); + ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CertificateValidation); + + // 尝试解决偶尔出现的 "请求被中止: 未能创建 SSL/TLS 安全通道" 异常 + ServicePointManager.Expect100Continue = true; + ServicePointManager.SecurityProtocol = (SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12 | SecurityProtocolType.Ssl3); } request = (HttpWebRequest)WebRequest.Create(url); diff --git a/SensorHub.Servers/Common.cs b/SensorHub.Servers/Common.cs index 86233f2..7da8617 100644 --- a/SensorHub.Servers/Common.cs +++ b/SensorHub.Servers/Common.cs @@ -176,8 +176,8 @@ else { String strBase64Value = Convert.ToBase64String(afcrc); - int ret = SendNACommand(session, strBase64Value, source); - if (ret != 201) + int ret = SendAEPCommand(session, strBase64Value, source); + if (ret != 0) { session.Logger.Info("电信平台下发配置信息失败,返回的Http状态码:" + ret); } diff --git a/SensorHub.Servers/TelecomAEPReceiveFilter.cs b/SensorHub.Servers/TelecomAEPReceiveFilter.cs index edc4ebd..93d33f3 100644 --- a/SensorHub.Servers/TelecomAEPReceiveFilter.cs +++ b/SensorHub.Servers/TelecomAEPReceiveFilter.cs @@ -79,12 +79,23 @@ } } //////////////////////////////////////////////////////////////////////// - // 对tag进行sm4解密 + // 对tag进行解密 + int encodeType = GetEncodeType(pduType); + byte[] dat = new byte[data.Length / 2 - 18]; Array.Copy(src, 16, dat, 0, data.Length / 2 - 18); - - string plainText = SM4Utils.sm4Decrypt(dat, deviceId); - settings = plainText.Substring(0, Convert.ToInt32(leng, 16) * 2 - 12 * 2); + + if (encodeType == 0) + { + // SM4加密方式 + string plainText = SM4Utils.sm4Decrypt(dat, deviceId); + settings = plainText.Substring(0, Convert.ToInt32(leng, 16) * 2 - 12 * 2); + } else if (encodeType == 1) + { + // TEA加密方式 + TEA.decrypt(ref dat, dat.Length); + settings = BitConverter.ToString(dat, 0, dat.Length).Replace("-", "").Substring(0, Convert.ToInt32(leng, 16) * 2 - 12 * 2); + } result = Common.getDeviceTypeByPdu(pduType); @@ -95,5 +106,27 @@ BasicRequestInfoParser m_Parser = new BasicRequestInfoParser(":", ","); return m_Parser.ParseRequestInfo(result); } + + private int GetEncodeType(string pduType) + { + int type = 0; // 默认为SM4加密方式 + string devType = Common.getDeviceTypeByPdu(pduType); + switch(devType) + { + case "Noise": + // SM4加密方式 + break; + + case "Well": + // 井盖 TEA加密方式 + type = 1; + break; + + default: + break; + } + + return type; + } } } diff --git a/SensorHub.Well/AccelerationDatas.cs b/SensorHub.Well/AccelerationDatas.cs new file mode 100644 index 0000000..0e4aa48 --- /dev/null +++ b/SensorHub.Well/AccelerationDatas.cs @@ -0,0 +1,10 @@ +using System; +using System.Collections.Generic; + +namespace SensorHub.Well +{ + class AccelerationDatas + { + public static Dictionary acclerationValueMap = new Dictionary(); // double数组 + } +} diff --git a/SensorHub.Noise/Noise.cs b/SensorHub.Noise/Noise.cs index 3a4501b..fd2db25 100644 --- a/SensorHub.Noise/Noise.cs +++ b/SensorHub.Noise/Noise.cs @@ -462,12 +462,11 @@ { // 如果有这个文件(参数是存储位置+文件名称,打开方式)打开文件末尾如果没有创建一个文件流的类并加入参数后实例化 string path = Common.GetWindowsServiceInstallPath(ConfigurationManager.AppSettings["ServiceName"]) + "\\Datas\\"; - FileStream fileStream = new FileStream(path + DateTime.Now.ToString("yyyyMMddHHmmss-") + obj + ".txt", FileMode.Append); + FileStream fileStream = new FileStream(path + DateTime.Now.ToString("yyyyMMddHHmmss-") + obj + ".noi", FileMode.Append); // 创建一个输入流的类并加入参数(文件路径和编码格式)实例化 StreamWriter write = new StreamWriter(fileStream, Encoding.Default); StringBuilder exdata = new StringBuilder();//这里是创建写入数据的StringBuilder对象 - exdata.Append("共").Append(AccelerationDatas.acclerationValueMap[(string)obj].Length).Append("行").AppendLine(); try { for (int i = 0; i < AccelerationDatas.acclerationValueMap[(string) obj].Length; i++) diff --git a/SensorHub.Servers/AepSdkCore.cs b/SensorHub.Servers/AepSdkCore.cs index 73a12c0..7d97e81 100644 --- a/SensorHub.Servers/AepSdkCore.cs +++ b/SensorHub.Servers/AepSdkCore.cs @@ -200,8 +200,11 @@ //设置https验证方式 if (url.StartsWith("https", StringComparison.OrdinalIgnoreCase)) { - ServicePointManager.ServerCertificateValidationCallback = - new RemoteCertificateValidationCallback(CertificateValidation); + ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CertificateValidation); + + // 尝试解决偶尔出现的 "请求被中止: 未能创建 SSL/TLS 安全通道" 异常 + ServicePointManager.Expect100Continue = true; + ServicePointManager.SecurityProtocol = (SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12 | SecurityProtocolType.Ssl3); } request = (HttpWebRequest)WebRequest.Create(url); diff --git a/SensorHub.Servers/Common.cs b/SensorHub.Servers/Common.cs index 86233f2..7da8617 100644 --- a/SensorHub.Servers/Common.cs +++ b/SensorHub.Servers/Common.cs @@ -176,8 +176,8 @@ else { String strBase64Value = Convert.ToBase64String(afcrc); - int ret = SendNACommand(session, strBase64Value, source); - if (ret != 201) + int ret = SendAEPCommand(session, strBase64Value, source); + if (ret != 0) { session.Logger.Info("电信平台下发配置信息失败,返回的Http状态码:" + ret); } diff --git a/SensorHub.Servers/TelecomAEPReceiveFilter.cs b/SensorHub.Servers/TelecomAEPReceiveFilter.cs index edc4ebd..93d33f3 100644 --- a/SensorHub.Servers/TelecomAEPReceiveFilter.cs +++ b/SensorHub.Servers/TelecomAEPReceiveFilter.cs @@ -79,12 +79,23 @@ } } //////////////////////////////////////////////////////////////////////// - // 对tag进行sm4解密 + // 对tag进行解密 + int encodeType = GetEncodeType(pduType); + byte[] dat = new byte[data.Length / 2 - 18]; Array.Copy(src, 16, dat, 0, data.Length / 2 - 18); - - string plainText = SM4Utils.sm4Decrypt(dat, deviceId); - settings = plainText.Substring(0, Convert.ToInt32(leng, 16) * 2 - 12 * 2); + + if (encodeType == 0) + { + // SM4加密方式 + string plainText = SM4Utils.sm4Decrypt(dat, deviceId); + settings = plainText.Substring(0, Convert.ToInt32(leng, 16) * 2 - 12 * 2); + } else if (encodeType == 1) + { + // TEA加密方式 + TEA.decrypt(ref dat, dat.Length); + settings = BitConverter.ToString(dat, 0, dat.Length).Replace("-", "").Substring(0, Convert.ToInt32(leng, 16) * 2 - 12 * 2); + } result = Common.getDeviceTypeByPdu(pduType); @@ -95,5 +106,27 @@ BasicRequestInfoParser m_Parser = new BasicRequestInfoParser(":", ","); return m_Parser.ParseRequestInfo(result); } + + private int GetEncodeType(string pduType) + { + int type = 0; // 默认为SM4加密方式 + string devType = Common.getDeviceTypeByPdu(pduType); + switch(devType) + { + case "Noise": + // SM4加密方式 + break; + + case "Well": + // 井盖 TEA加密方式 + type = 1; + break; + + default: + break; + } + + return type; + } } } diff --git a/SensorHub.Well/AccelerationDatas.cs b/SensorHub.Well/AccelerationDatas.cs new file mode 100644 index 0000000..0e4aa48 --- /dev/null +++ b/SensorHub.Well/AccelerationDatas.cs @@ -0,0 +1,10 @@ +using System; +using System.Collections.Generic; + +namespace SensorHub.Well +{ + class AccelerationDatas + { + public static Dictionary acclerationValueMap = new Dictionary(); // double数组 + } +} diff --git a/SensorHub.Well/AccelerationTagHandler.cs b/SensorHub.Well/AccelerationTagHandler.cs new file mode 100644 index 0000000..105b5d8 --- /dev/null +++ b/SensorHub.Well/AccelerationTagHandler.cs @@ -0,0 +1,33 @@ +using System; + +namespace SensorHub.Servers.Commands.CASICCommands +{ + class AccelerationTagHandler : TagHandler + { + public override bool isThisTag(Tag tag) + { + if (!(tag is UploadTag)) + { + return false; + } + + UploadTag uploadTag = tag as UploadTag; + + return uploadTag.BizType == 21; // 海量加速度值 + } + + public override void resolve(Tag tag, CasicSession session) + { + // TODO LIST:解析数据 + UploadTag accelerationTag = tag as UploadTag; + Interval = accelerationTag.CollectInter; + CollecTime = accelerationTag.CollectTime; + string dataValue = accelerationTag.DataValue; + + session.Logger.Info("井盖海量加速度值数据上传TAG:oid:" + accelerationTag.Oid + " 采集间隔: " + Interval + "采集时间:" + CollecTime); + + Data = dataValue; + } + + } +} diff --git a/SensorHub.Noise/Noise.cs b/SensorHub.Noise/Noise.cs index 3a4501b..fd2db25 100644 --- a/SensorHub.Noise/Noise.cs +++ b/SensorHub.Noise/Noise.cs @@ -462,12 +462,11 @@ { // 如果有这个文件(参数是存储位置+文件名称,打开方式)打开文件末尾如果没有创建一个文件流的类并加入参数后实例化 string path = Common.GetWindowsServiceInstallPath(ConfigurationManager.AppSettings["ServiceName"]) + "\\Datas\\"; - FileStream fileStream = new FileStream(path + DateTime.Now.ToString("yyyyMMddHHmmss-") + obj + ".txt", FileMode.Append); + FileStream fileStream = new FileStream(path + DateTime.Now.ToString("yyyyMMddHHmmss-") + obj + ".noi", FileMode.Append); // 创建一个输入流的类并加入参数(文件路径和编码格式)实例化 StreamWriter write = new StreamWriter(fileStream, Encoding.Default); StringBuilder exdata = new StringBuilder();//这里是创建写入数据的StringBuilder对象 - exdata.Append("共").Append(AccelerationDatas.acclerationValueMap[(string)obj].Length).Append("行").AppendLine(); try { for (int i = 0; i < AccelerationDatas.acclerationValueMap[(string) obj].Length; i++) diff --git a/SensorHub.Servers/AepSdkCore.cs b/SensorHub.Servers/AepSdkCore.cs index 73a12c0..7d97e81 100644 --- a/SensorHub.Servers/AepSdkCore.cs +++ b/SensorHub.Servers/AepSdkCore.cs @@ -200,8 +200,11 @@ //设置https验证方式 if (url.StartsWith("https", StringComparison.OrdinalIgnoreCase)) { - ServicePointManager.ServerCertificateValidationCallback = - new RemoteCertificateValidationCallback(CertificateValidation); + ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CertificateValidation); + + // 尝试解决偶尔出现的 "请求被中止: 未能创建 SSL/TLS 安全通道" 异常 + ServicePointManager.Expect100Continue = true; + ServicePointManager.SecurityProtocol = (SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12 | SecurityProtocolType.Ssl3); } request = (HttpWebRequest)WebRequest.Create(url); diff --git a/SensorHub.Servers/Common.cs b/SensorHub.Servers/Common.cs index 86233f2..7da8617 100644 --- a/SensorHub.Servers/Common.cs +++ b/SensorHub.Servers/Common.cs @@ -176,8 +176,8 @@ else { String strBase64Value = Convert.ToBase64String(afcrc); - int ret = SendNACommand(session, strBase64Value, source); - if (ret != 201) + int ret = SendAEPCommand(session, strBase64Value, source); + if (ret != 0) { session.Logger.Info("电信平台下发配置信息失败,返回的Http状态码:" + ret); } diff --git a/SensorHub.Servers/TelecomAEPReceiveFilter.cs b/SensorHub.Servers/TelecomAEPReceiveFilter.cs index edc4ebd..93d33f3 100644 --- a/SensorHub.Servers/TelecomAEPReceiveFilter.cs +++ b/SensorHub.Servers/TelecomAEPReceiveFilter.cs @@ -79,12 +79,23 @@ } } //////////////////////////////////////////////////////////////////////// - // 对tag进行sm4解密 + // 对tag进行解密 + int encodeType = GetEncodeType(pduType); + byte[] dat = new byte[data.Length / 2 - 18]; Array.Copy(src, 16, dat, 0, data.Length / 2 - 18); - - string plainText = SM4Utils.sm4Decrypt(dat, deviceId); - settings = plainText.Substring(0, Convert.ToInt32(leng, 16) * 2 - 12 * 2); + + if (encodeType == 0) + { + // SM4加密方式 + string plainText = SM4Utils.sm4Decrypt(dat, deviceId); + settings = plainText.Substring(0, Convert.ToInt32(leng, 16) * 2 - 12 * 2); + } else if (encodeType == 1) + { + // TEA加密方式 + TEA.decrypt(ref dat, dat.Length); + settings = BitConverter.ToString(dat, 0, dat.Length).Replace("-", "").Substring(0, Convert.ToInt32(leng, 16) * 2 - 12 * 2); + } result = Common.getDeviceTypeByPdu(pduType); @@ -95,5 +106,27 @@ BasicRequestInfoParser m_Parser = new BasicRequestInfoParser(":", ","); return m_Parser.ParseRequestInfo(result); } + + private int GetEncodeType(string pduType) + { + int type = 0; // 默认为SM4加密方式 + string devType = Common.getDeviceTypeByPdu(pduType); + switch(devType) + { + case "Noise": + // SM4加密方式 + break; + + case "Well": + // 井盖 TEA加密方式 + type = 1; + break; + + default: + break; + } + + return type; + } } } diff --git a/SensorHub.Well/AccelerationDatas.cs b/SensorHub.Well/AccelerationDatas.cs new file mode 100644 index 0000000..0e4aa48 --- /dev/null +++ b/SensorHub.Well/AccelerationDatas.cs @@ -0,0 +1,10 @@ +using System; +using System.Collections.Generic; + +namespace SensorHub.Well +{ + class AccelerationDatas + { + public static Dictionary acclerationValueMap = new Dictionary(); // double数组 + } +} diff --git a/SensorHub.Well/AccelerationTagHandler.cs b/SensorHub.Well/AccelerationTagHandler.cs new file mode 100644 index 0000000..105b5d8 --- /dev/null +++ b/SensorHub.Well/AccelerationTagHandler.cs @@ -0,0 +1,33 @@ +using System; + +namespace SensorHub.Servers.Commands.CASICCommands +{ + class AccelerationTagHandler : TagHandler + { + public override bool isThisTag(Tag tag) + { + if (!(tag is UploadTag)) + { + return false; + } + + UploadTag uploadTag = tag as UploadTag; + + return uploadTag.BizType == 21; // 海量加速度值 + } + + public override void resolve(Tag tag, CasicSession session) + { + // TODO LIST:解析数据 + UploadTag accelerationTag = tag as UploadTag; + Interval = accelerationTag.CollectInter; + CollecTime = accelerationTag.CollectTime; + string dataValue = accelerationTag.DataValue; + + session.Logger.Info("井盖海量加速度值数据上传TAG:oid:" + accelerationTag.Oid + " 采集间隔: " + Interval + "采集时间:" + CollecTime); + + Data = dataValue; + } + + } +} diff --git a/SensorHub.Well/Well.cs b/SensorHub.Well/Well.cs index 3c95b15..c5b86c4 100644 --- a/SensorHub.Well/Well.cs +++ b/SensorHub.Well/Well.cs @@ -5,8 +5,10 @@ using SuperSocket.SocketBase.Protocol; using System; using System.Collections.Generic; -using System.Linq; +using System.IO; using System.Text; +using System.Threading; +using System.Configuration; namespace SensorHub.Well { @@ -37,11 +39,13 @@ //print the receving data String devType = "井盖状态监测仪"; String operType = Common.getOpeTypeByPdu(pduType); - session.Logger.Info("AD接收数据:" + requestInfo.Body); + // session.Logger.Info("AD接收数据:" + requestInfo.Body); + session.Logger.Info("设备编号:" + devCode); session.Logger.Info("设备类型:" + devType); session.Logger.Info("操作类型:" + operType); + session.Logger.Info("包序列号:" + seq); session.Logger.Info("会话:" + session.HubAddr + "," + session.SessionID); - + List tags = Common.getTags(settings, session); //具体业务处理 @@ -112,6 +116,39 @@ } } break; + + case 21: + // 海量加速度值 + TagHandler accelerationHandler = new AccelerationTagHandler(); + accelerationHandler.resolve(tag, session); + + string datas = (string)accelerationHandler.Data; + + session.Logger.Info("接收总数据长度:" + datas.Length / 2 + "字节,采样点:" + datas.Length / (2 * 2)); + // session.Logger.Info(datas); + + double[] realValue = new double[datas.Length / (2 * 2)]; // 每个采样点2个字节 + for (int i = 0; i < realValue.Length; i++) + { + string tempByteString = datas.Substring(i * 4, 4); + realValue[i] = HexToDouble(tempByteString); + // session.Logger.Info(i + ": " + tempByteString + "; " + HexToDouble(tempByteString)); + } + + AccelerationDatas.acclerationValueMap.Add(devCode, realValue); + + // 创建存盘线程 + Thread savethread = new Thread(SavetoFlie) + { + IsBackground = true//将线程设置为后台线程 + }; + savethread.Start(devCode); //开启存盘线程 + + byte[] responseFrame = BuildAccelerationDataReceived(devCode, new byte[2] { 0x05, 0x86 }); + string responseBase64 = Convert.ToBase64String(responseFrame); // 转成base64下发 + Common.SendAEPCommand(session, responseBase64, source); + return; + default: session.Logger.Info("未知业务类型!"); break; @@ -136,9 +173,120 @@ btPdu[1] = 0x86; - Common.sendConfig(session, devCode, routeFlag, source, btPdu); + // Common.sendConfig(session, devCode, routeFlag, source, btPdu); + Common.sendGPRSConfig(session, devCode, btPdu, source, "TEA"); } + } + + private double HexToDouble(string hexString) + { + if (hexString.Length != 4) + return 0; + + Int16 originInt = Convert.ToInt16(hexString); + originInt -= 2048; + return (double)originInt / 2048.0; + } + + private byte[] BuildAccelerationDataReceived(string devCode, byte[] btPdu) + { + byte[] frame = { 0xA3, 0x20, //帧头 - 固定 + 0x00, 0x26, //长度 - 固定 + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, //设备编号 + 0x03, //通信方式 - 固定 + 0x00, 0x00, //目标节点地址 + 0x00, 0x00, //PDUType + 0x01, //Seq - 不分包 固定 + 0x10, 0x00, 0x00, 0x51, 0x00, 0x06, //时间戳 + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x60, 0x00, 0x03, 0x00, 0x00, 0x08, //数据接收状态 + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 + }; + // 设备编号字段 + byte[] btDevCode = StrToHexByte(devCode); + btDevCode.CopyTo(frame, 4); + Array.Copy(btDevCode, 4, frame, 11, 2); // 目标节点地址 = 设备编号最后2个字节 + + // pduType字段 + btPdu.CopyTo(frame, 13); + + // 时间戳字段 + SystemTimeConfig sysTimeConfig = new SystemTimeConfig(null); + byte[] btConfig = sysTimeConfig.getConfig(new byte[0]); + btConfig.CopyTo(frame, 16); + + // TEA加密 + byte[] tag = new byte[frame.Length - 16]; + Array.Copy(frame, 16, tag, 0, tag.Length); // 需要加密的内容 + byte[] enTag; // 加密后的内容 + + int a = tag.Length % 8; + if (a != 0) + { + enTag = new byte[tag.Length + 8 - a]; + tag.CopyTo(enTag, 0); + for (int i = 0; i < 8 - a; i++) + { + enTag[tag.Length + i] = 0; + } + } + else + { + enTag = new byte[tag.Length]; + tag.CopyTo(enTag, 0); + } + + TEA.encrypt(ref enTag, enTag.Length); + + byte[] result = new byte[1 + 1 + 2 + 6 + 1 + 2 + 2 + 1 + enTag.Length]; + Array.Copy(frame, result, 16); + enTag.CopyTo(result, 16); + + byte[] afcrc = Common.CRC(result); + return afcrc; + } + + private void SavetoFlie(object obj)//存文件方法 + { + // 如果有这个文件(参数是存储位置+文件名称,打开方式)打开文件末尾如果没有创建一个文件流的类并加入参数后实例化 + string path = Common.GetWindowsServiceInstallPath(ConfigurationManager.AppSettings["ServiceName"]) + "\\Datas\\"; + FileStream fileStream = new FileStream(path + DateTime.Now.ToString("yyyyMMddHHmmss-") + obj + ".wll", FileMode.Append); + + // 创建一个输入流的类并加入参数(文件路径和编码格式)实例化 + StreamWriter write = new StreamWriter(fileStream, Encoding.Default); + StringBuilder exdata = new StringBuilder();//这里是创建写入数据的StringBuilder对象 + try + { + for (int i = 0; i < AccelerationDatas.acclerationValueMap[(string)obj].Length; i++) + { + exdata.Append(AccelerationDatas.acclerationValueMap[(string)obj][i]).AppendLine(); //添加StringBuilder字符串内容 + } + + write.Write(exdata); //向目标文件中写入数据 + } + catch (Exception ex) + { + Console.WriteLine("存储数据到文件时发生错误:" + ex.Message); + } + finally + { + // 关闭文件流 + write.Close(); + fileStream.Close(); + AccelerationDatas.acclerationValueMap.Remove((string)obj); + } + } + + private byte[] StrToHexByte(string hexString) + { + hexString = hexString.Replace(" ", ""); + if ((hexString.Length % 2) != 0) + hexString += "0"; + byte[] returnBytes = new byte[hexString.Length / 2]; + for (int i = 0; i < returnBytes.Length; i++) + returnBytes[i] = Convert.ToByte(hexString.Substring(i * 2, 2), 16); + return returnBytes; } } }