using Ems.CollectService.Common; using Ems.CollectService.Entity; using Ems.CollectService.Entity.config; using Ems.CollectService.Entity.dto; using Ems.CollectService.SqlSugarCore; using NetTaste; using NLog; using SqlSugar; using System; using System.Data.SqlTypes; using System.Linq; using System.Net.Sockets; using System.Text; using System.Threading.Tasks; using TouchSocket.Sockets; namespace Ems.CollectService.Analysis { public sealed class BufferAnalysis { private Logger logger = LogManager.GetCurrentClassLogger(); private MsgUtil msgUtil = MsgUtil.Instance; private AppConfig appConfig = AppConfig.Instance; //private SqlSugarClient baseService = SqlGenerator.GetMySqlInstance(); private static readonly Lazy lazy = new Lazy(() => new BufferAnalysis()); public static BufferAnalysis Instance { get { return lazy.Value; } } private BufferAnalysis() { logger.Info($"版本更新,修复连接ID重复,主动断开原有连接信息建立新连接"); } /// /// 登录指令 /// /// /// /// /// public bool LoginMessageHandling(SocketClient client, byte[] buffer, int count,TcpService tcpService) { byte[] array = new byte[2]; var flag = ""; try { string DeviceType = ""; byte[] bDeviceType = new byte[1]; byte[] bDeviceID = new byte[2]; Array.Copy(buffer, 1, bDeviceType, 0, 1); Array.Copy(buffer, 2, bDeviceID, 0, 2); DeviceType = Encoding.ASCII.GetString(buffer, 1, 1); DeviceType += msgUtil.ConverToString(bDeviceID); flag = msgUtil.ConverToString(bDeviceType); flag += msgUtil.ConverToString(bDeviceID); MessagePack messagePack = new MessagePack(); messagePack.m_EnergyType = buffer[1]; Array.Copy(buffer, 2, messagePack.m_Meteraddr, 0, 2); Array.Copy(buffer, 4, messagePack.m_Msta, 0, 2); messagePack.m_StartFlag = buffer[6]; messagePack.m_MessageType = 161; Array.Copy(buffer, 2, array, 0, 2); string text = Encoding.ASCII.GetString(buffer, 1, 1) + msgUtil.ConverToString(array); string clientIdStr = flag.ToString(); if (clientIdStr.Contains("45")) { var clients = tcpService.GetClients(); var info = clients.Where(x => x.ID == clientIdStr).ToList(); if (info.Count > 0) { logger.Trace($"客户端:{client.ID};地址:{client.GetIPPort()};已存在连接信息,主动断开已有连接"); foreach (var item in info) { item.Close(); logger.Trace($"客户端:{item.ID};地址:{item.GetIPPort()};已断开"); } } if (client.ID != clientIdStr) { client.ResetID(clientIdStr); } if (SendMessageToClient(client, messagePack)) { logger.Info($"向客户端:{client.ID};地址:{client.GetIPPort()};回复登录指令成功"); if(client.ID.Length >= 4 ) { string clientId = string.Format("E{0}", client.ID.ToString().Substring(2, client.ID.ToString().Length - 2)); UpdateCollectDeviceOnLineState(clientId, 1); } return true; } } return false; } catch (Exception ex) { logger.Error($"LoginMessageHandling登录指令解析异常:{ex.Message}"); client.Close(); return false; } } /// /// 心跳指令 /// /// /// /// /// public bool HeartMessageHandlingAsync(SocketClient client, byte[] buffer, int count) { try { var flag = ""; byte[] SendMessage = new byte[12]; string DeviceType = ""; byte[] bDeviceType = new byte[1]; byte[] bDeviceID = new byte[2]; Array.Copy(buffer, 1, bDeviceType, 0, 1); Array.Copy(buffer, 2, bDeviceID, 0, 2); DeviceType = Encoding.ASCII.GetString(buffer, 1, 1); DeviceType += msgUtil.ConverToString(bDeviceID); flag = msgUtil.ConverToString(bDeviceType); flag += msgUtil.ConverToString(bDeviceID); logger.Debug($"收到客户端:{client.ID};地址:{client.GetIPPort()}心跳指令"); MessagePack SendMessagePackInfo = new MessagePack(); SendMessagePackInfo.m_EnergyType = buffer[1]; Array.Copy(buffer, 2, SendMessagePackInfo.m_Meteraddr, 0, 2); Array.Copy(buffer, 4, SendMessagePackInfo.m_Msta, 0, 2); SendMessagePackInfo.m_StartFlag = buffer[6]; SendMessagePackInfo.m_MessageType = (byte)CallbackSendData._HeartBeat; if(SendMessageAsync(client, SendMessagePackInfo)) { logger.Debug($"向客户端:{client.ID};地址:{client.GetIPPort()};回复心跳指令成功"); //bool isRes = SendTimeSyncToClient(client, SendMessagePackInfo); //logger.Info($"向客户端:{client.ID};地址:{client.GetIPPort()};发送校时指令{(isRes ? "成功":"失败")}"); return true; } else { logger.Debug($"向客户端:{client.ID};地址:{client.GetIPPort()};回复心跳指令失败"); return false; } } catch (Exception ex) { logger.Error($"HeartMessageHandlingAsync心跳指令解析异常:{ex.Message}"); return false; } } /// /// 电力仪表 /// /// /// /// /// public void EDataMessageHandlingAsync(SocketClient socketClient, byte[] buffer, int count, int collectType) { int DataLength = 58 + 12; var flag = "";//发送指定客户端标记 string DeviceType = ""; byte[] bDeviceType = new byte[1]; byte[] bDeviceID = new byte[2]; string m_BarcodeGroupCount = "";//表长度16进制字符串 byte[] b_BarcodeGroupCount = new byte[2]; int i_BarcodeGroupCount = 0; try { //发送电力仪表应答帧 MessagePack SendMessagePackInfo = new MessagePack(); SendMessagePackInfo.m_EnergyType = buffer[1]; Array.Copy(buffer, 2, SendMessagePackInfo.m_Meteraddr, 0, 2); Array.Copy(buffer, 4, SendMessagePackInfo.m_Msta, 0, 2); SendMessagePackInfo.m_StartFlag = buffer[6]; SendMessagePackInfo.m_MessageType = (byte)CallbackSendData._ERealFlag; SendMessageAsync(socketClient, SendMessagePackInfo); Array.Copy(buffer, 1, bDeviceType, 0, 1); Array.Copy(buffer, 2, bDeviceID, 0, 2); DeviceType = Encoding.ASCII.GetString(buffer, 1, 1); flag = msgUtil.ConverToString(bDeviceType) + msgUtil.ConverToString(bDeviceID); //LogInfo.Debug("》》》收到采集终端[" + DeviceType + "]下属电表数据指令!\r\n"); Array.Copy(buffer, 8, b_BarcodeGroupCount, 0, 2); m_BarcodeGroupCount = Convert.ToString(msgUtil.bytesToHexStr(b_BarcodeGroupCount, b_BarcodeGroupCount.Length));//获取仪表数量 i_BarcodeGroupCount = Convert.ToInt32("0x" + m_BarcodeGroupCount, 16); int iFirstMeterID = 10; uint u_Control; //控制码 byte[] b_MeterID = new byte[2]; byte[] b_UA = new byte[4]; byte[] b_UB = new byte[4]; byte[] b_UC = new byte[4]; byte[] b_IA = new byte[4]; byte[] b_IB = new byte[4]; byte[] b_IC = new byte[4]; byte[] b_ZXYGZ = new byte[4]; byte[] b_GLYS = new byte[4]; float f_UA; float f_UB; float f_UC; float f_IA; float f_IB; float f_IC; float f_ZXYGZ; float f_GLYS; #region 有功功率、无功功率参数定义 Add By Wenjy 2023-03-23 byte[] b_YGGL = new byte[4]; float f_YGGL; byte[] b_WGGL = new byte[4]; float f_WGGL; #endregion for (int i = 0; i < i_BarcodeGroupCount / DataLength; i++) { int nParamCount = 11; //表参数个数 RecordDnbInstant recordDnbInstant = new RecordDnbInstant(); Array.Copy(buffer, iFirstMeterID, b_MeterID, 0, b_MeterID.Length); var collectDeviceId = Encoding.ASCII.GetString(buffer, 1, 1) + msgUtil.ConverToString(bDeviceID); var MeterID_1 = "00" + Convert.ToInt32(b_MeterID[0]).ToString(); MeterID_1 = MeterID_1.Substring(MeterID_1.Length - 2, 2); var MeterID_2 = "00" + Convert.ToInt32(b_MeterID[1]).ToString(); MeterID_2 = MeterID_2.Substring(MeterID_2.Length - 2, 2); DeviceType = collectDeviceId + "_" + MeterID_1 + MeterID_2; //LogInfo.Debug("MeterID_1:" + MeterID_1 + "b_MeterID[0]:" + Convert.ToInt32(b_MeterID[0]) + ";" + "MeterID_2:" + MeterID_2 + "b_MeterID[1]" + Convert.ToInt32(b_MeterID[1])); logger.Info("》》》循环解析[" + (i + 1) + "]个电力仪表[" + DeviceType + "]电力数据!\r\n"); int i_pointer = 12 + i * DataLength; recordDnbInstant.monitorId = DeviceType; if (i_pointer == 0) { return; } string logInfoStr = ""; while (i_pointer < 10 + (i + 1) * DataLength) { u_Control = (uint)(buffer[i_pointer] * 0x100 + buffer[i_pointer + 1]); logInfoStr += $"仪表编号:{recordDnbInstant.monitorId}"; switch (u_Control) { case EMS_COMM_PARAMS.AU: //A相电压 Array.Copy(buffer, i_pointer + 2 + 2, b_UA, 0, 2); Array.Copy(buffer, i_pointer + 2, b_UA, 2, 2); f_UA = BitConverter.ToSingle(b_UA.Reverse().ToArray(), 0); logInfoStr += $";电力仪表的A相电压:{f_UA}"; if (double.IsNaN(f_UA)) { //此处判断a为NaN recordDnbInstant.vA = appConfig.virtualValue; } else { recordDnbInstant.vA = Convert.ToDecimal(f_UA); } i_pointer += 6; nParamCount--; break; case EMS_COMM_PARAMS.BU: //B相电压 Array.Copy(buffer, i_pointer + 2 + 2, b_UB, 0, 2); Array.Copy(buffer, i_pointer + 2, b_UB, 2, 2); f_UB = BitConverter.ToSingle(b_UB.Reverse().ToArray(), 0); logInfoStr += $";电力仪表的B相电压:{f_UB}"; if (double.IsNaN(f_UB)) { //此处判断a为NaN recordDnbInstant.vB = 0; } else { recordDnbInstant.vB = Convert.ToDecimal(f_UB); } i_pointer += 6; nParamCount--; break; case EMS_COMM_PARAMS.CU: //C相电压 Array.Copy(buffer, i_pointer + 2 + 2, b_UC, 0, 2); Array.Copy(buffer, i_pointer + 2, b_UC, 2, 2); f_UC = BitConverter.ToSingle(b_UC.Reverse().ToArray(), 0); logInfoStr += $";电力仪表的C相电压:{f_UC}"; if (double.IsNaN(f_UC)) { //此处判断a为NaN recordDnbInstant.vC = 0; } else { recordDnbInstant.vC = Convert.ToDecimal(f_UC); } i_pointer += 6; nParamCount--; break; case EMS_COMM_PARAMS.AI: //A相电流 Array.Copy(buffer, i_pointer + 2 + 2, b_IA, 0, 2); Array.Copy(buffer, i_pointer + 2, b_IA, 2, 2); f_IA = BitConverter.ToSingle(b_IA.Reverse().ToArray(), 0); logInfoStr += $";电力仪表的A相电流:{f_IA}"; if (double.IsNaN(f_IA)) { //此处判断a为NaN recordDnbInstant.iA = 0; } else { recordDnbInstant.iA = Convert.ToDecimal(f_IA); } i_pointer += 6; nParamCount--; break; case EMS_COMM_PARAMS.BI: //B相电流 Array.Copy(buffer, i_pointer + 2 + 2, b_IB, 0, 2); Array.Copy(buffer, i_pointer + 2, b_IB, 2, 2); f_IB = BitConverter.ToSingle(b_IB.Reverse().ToArray(), 0); logInfoStr += $";电力仪表的B相电流:{f_IB}"; if (double.IsNaN(f_IB)) { //此处判断a为NaN recordDnbInstant.iB = 0; } else { recordDnbInstant.iB = Convert.ToDecimal(f_IB); } i_pointer += 6; nParamCount--; break; case EMS_COMM_PARAMS.CI: //C相电流 Array.Copy(buffer, i_pointer + 2 + 2, b_IC, 0, 2); Array.Copy(buffer, i_pointer + 2, b_IC, 2, 2); f_IC = BitConverter.ToSingle(b_IC.Reverse().ToArray(), 0); logInfoStr += $";电力仪表的C相电流:{f_IC}"; if (double.IsNaN(f_IC)) { //此处判断a为NaN recordDnbInstant.iC = 0; } else { recordDnbInstant.iC = Convert.ToDecimal(f_IC); } i_pointer += 6; nParamCount--; break; case EMS_COMM_PARAMS.ZXYGZ: //正向有功总 Array.Copy(buffer, i_pointer + 2 + 2, b_ZXYGZ, 0, 2); Array.Copy(buffer, i_pointer + 2, b_ZXYGZ, 2, 2); f_ZXYGZ = BitConverter.ToSingle(b_ZXYGZ.Reverse().ToArray(), 0); logInfoStr += $";电力仪表的正向有功总:{f_ZXYGZ}"; if (double.IsNaN(f_ZXYGZ)) { //此处判断a为NaN recordDnbInstant.zxyg = 0; } else { recordDnbInstant.zxyg = Convert.ToDecimal(f_ZXYGZ); } i_pointer += 6; nParamCount--; break; case EMS_COMM_PARAMS.GLYS: //功率因数 Array.Copy(buffer, i_pointer + 2 + 2, b_GLYS, 0, 2); Array.Copy(buffer, i_pointer + 2, b_GLYS, 2, 2); f_GLYS = BitConverter.ToSingle(b_GLYS.Reverse().ToArray(), 0); logInfoStr += $";电力仪表的功率因数:{f_GLYS}"; if (double.IsNaN(f_GLYS)) { //此处判断a为NaN recordDnbInstant.glys = 0; } else { recordDnbInstant.glys = Convert.ToDecimal(f_GLYS); } i_pointer += 6; nParamCount--; break; case EMS_COMM_PARAMS.YGGL: Array.Copy(buffer, i_pointer + 2 + 2, b_YGGL, 0, 2); Array.Copy(buffer, i_pointer + 2, b_YGGL, 2, 2); f_YGGL = BitConverter.ToSingle(b_YGGL.Reverse().ToArray(), 0); logInfoStr += $";电力仪表的有功功率:{f_YGGL}"; if (double.IsNaN(f_YGGL)) { //此处判断a为NaN recordDnbInstant.activePower = 0; } else { recordDnbInstant.activePower = Convert.ToDecimal(f_YGGL / 1000); } i_pointer += 6; nParamCount--; break; case EMS_COMM_PARAMS.WGGL: Array.Copy(buffer, i_pointer + 2 + 2, b_WGGL, 0, 2); Array.Copy(buffer, i_pointer + 2, b_WGGL, 2, 2); f_WGGL = BitConverter.ToSingle(b_WGGL.Reverse().ToArray(), 0); logInfoStr += $";电力仪表的无功功率:{f_WGGL}"; if (double.IsNaN(f_WGGL)) { //此处判断a为NaN recordDnbInstant.reactivePower = 0; } else { recordDnbInstant.reactivePower = Convert.ToDecimal(f_WGGL); } i_pointer += 6; nParamCount--; break; case EMS_COMM_PARAMS.CJSJ: //采集时间 string strDateTime = "20" + buffer[i_pointer + 2 + 5].ToString("x2") + "-" + buffer[i_pointer + 2 + 4].ToString("x2") + "-" + buffer[i_pointer + 2 + 3].ToString("x2") + " " + buffer[i_pointer + 2 + 2].ToString("x2") + ":" + buffer[i_pointer + 2 + 1].ToString("x2") + ":" + buffer[i_pointer + 2].ToString("x2"); logInfoStr += $";终端时间:{strDateTime}"; recordDnbInstant.collectTime = Convert.ToDateTime(strDateTime); i_pointer += 8; nParamCount--; break; } } if (nParamCount == 0) { logger.Info($"仪表:{recordDnbInstant.monitorId};解析完成数据:{logInfoStr}"); recordDnbInstant.collectType = collectType; recordDnbInstant.recordTime = DateTime.Now; if (appConfig.virtualFlag) { if (recordDnbInstant.vA != appConfig.virtualValue) { var info = SqlSugarHelper.Db.Insertable(recordDnbInstant).ExecuteCommand(); if (info > 0) { logger.Info($"仪表:{recordDnbInstant.monitorId}数据保存成功"); } } else { logger.Info($"仪表:{recordDnbInstant.monitorId}存在FFFF值不保存该条数据"); } } else { recordDnbInstant.vA = recordDnbInstant.vA == appConfig.virtualValue ? 0 : recordDnbInstant.vA; var info = SqlSugarHelper.Db.Insertable(recordDnbInstant).ExecuteCommand(); if (info > 0) { logger.Info($"仪表:{recordDnbInstant.monitorId}数据保存成功"); } } } iFirstMeterID += 70; }//end for } catch (Exception ex) { logger.Error($"EDataMessageHandlingAsync电力仪表数据解析异常:{ex.Message}"); } } /// /// 水表数据 /// /// /// /// /// public void SDataMessageHandling(SocketClient socketClient, byte[] buffer, int length, int collectType) { int DataLength = 58; var flag = "";//发送指定客户端标记 string DeviceType = ""; byte[] bDeviceType = new byte[1]; byte[] bDeviceID = new byte[2]; string m_BarcodeGroupCount = "";//表长度16进制字符串 byte[] b_BarcodeGroupCount = new byte[2]; int i_BarcodeGroupCount = 0; try { //发送蒸汽、压缩空气、水仪表应答帧 MessagePack SendMessagePackInfo = new MessagePack(); SendMessagePackInfo.m_EnergyType = buffer[1]; Array.Copy(buffer, 2, SendMessagePackInfo.m_Meteraddr, 0, 2); Array.Copy(buffer, 4, SendMessagePackInfo.m_Msta, 0, 2); SendMessagePackInfo.m_StartFlag = buffer[6]; SendMessagePackInfo.m_MessageType = (byte)CallbackSendData._SRealFlag; SendMessageAsync(socketClient, SendMessagePackInfo); Array.Copy(buffer, 1, bDeviceType, 0, 1); Array.Copy(buffer, 2, bDeviceID, 0, 2); DeviceType = Encoding.ASCII.GetString(buffer, 1, 1); flag = msgUtil.ConverToString(bDeviceType) + msgUtil.ConverToString(bDeviceID); //LogInfo.Debug("》》》收到采集终端[" + DeviceType + "]下属电表数据指令!\r\n"); Array.Copy(buffer, 8, b_BarcodeGroupCount, 0, 2); m_BarcodeGroupCount = Convert.ToString(msgUtil.bytesToHexStr(b_BarcodeGroupCount, b_BarcodeGroupCount.Length));//获取仪表数量 i_BarcodeGroupCount = Convert.ToInt32("0x" + m_BarcodeGroupCount, 16); int iFirstMeterID = 10; uint u_Control; //控制码 byte[] b_MeterID = new byte[2]; byte[] b_Press = new byte[4]; byte[] b_Temperature = new byte[4]; byte[] b_Frequency = new byte[4]; byte[] b_FluxInstantValue = new byte[4]; byte[] b_FluxEyeableTotalValue = new byte[4]; byte[] b_HeatInstantValue = new byte[4]; byte[] b_HeatToftalValue = new byte[4]; byte[] b_Density = new byte[4]; float f_Press; float f_Temperature; float f_Frequency; float f_FluxInstantValue; float f_FluxEyeableTotalValue; float f_HeatInstantValue; float f_HeatToftalValue; float f_Density; for (int i = 0; i < i_BarcodeGroupCount / DataLength; i++) { int nParamCount = 9; //表参数个数 RecordWaterInstant recordWaterInstant = new RecordWaterInstant(); Array.Copy(buffer, iFirstMeterID, b_MeterID, 0, b_MeterID.Length); var collectDeviceId = Encoding.ASCII.GetString(buffer, 1, 1) + msgUtil.ConverToString(bDeviceID); var MeterID_1 = "00" + Convert.ToInt32(b_MeterID[0]).ToString(); MeterID_1 = MeterID_1.Substring(MeterID_1.Length - 2, 2); var MeterID_2 = "00" + Convert.ToInt32(b_MeterID[1]).ToString(); MeterID_2 = MeterID_2.Substring(MeterID_2.Length - 2, 2); DeviceType = collectDeviceId + "_" + MeterID_1 + MeterID_2; logger.Info("》》》循环解析第[" + (i + 1) + "]个蒸汽、压缩空气、水仪表[" + DeviceType + "]数据!\r\n"); int i_pointer = 12 + i * DataLength; recordWaterInstant.monitorId = DeviceType; if (i_pointer == 0) { return; } string logInfoStr = ""; while (i_pointer < 10 + (i + 1) * DataLength) { u_Control = (uint)(buffer[i_pointer] * 0x100 + buffer[i_pointer + 1]); logInfoStr+=$"仪表编号:{recordWaterInstant.monitorId}"; switch (u_Control) { case EMS_COMM_PARAMS.Press: //压力值 Array.Copy(buffer, i_pointer + 2 + 2, b_Press, 0, 2); Array.Copy(buffer, i_pointer + 2, b_Press, 2, 2); f_Press = BitConverter.ToSingle(b_Press.Reverse().ToArray(), 0); logInfoStr += $";压力值:{f_Press}"; if (float.IsNaN(f_Press)) { //recordWaterInstant.press = 0; } else { //recordWaterInstant.press = Convert.ToDecimal(f_Press); } i_pointer += 6; nParamCount--; break; case EMS_COMM_PARAMS.STemperature: //温度值 Array.Copy(buffer, i_pointer + 2 + 2, b_Temperature, 0, 2); Array.Copy(buffer, i_pointer + 2, b_Temperature, 2, 2); f_Temperature = BitConverter.ToSingle(b_Temperature.Reverse().ToArray(), 0); logInfoStr += $";温度值:{f_Temperature}"; if (float.IsNaN(f_Temperature)) { //recordWaterInstant.temperature = 0; } else { //recordWaterInstant.temperature = Convert.ToDecimal(f_Temperature); } i_pointer += 6; nParamCount--; break; case EMS_COMM_PARAMS.Frequency: //频率值 Array.Copy(buffer, i_pointer + 2 + 2, b_Frequency, 0, 2); Array.Copy(buffer, i_pointer + 2, b_Frequency, 2, 2); f_Frequency = BitConverter.ToSingle(b_Frequency.Reverse().ToArray(), 0); logInfoStr += $";频率值:{f_Frequency}"; if (float.IsNaN(f_Frequency)) { //recordWaterInstant.frequency = 0; } else { //recordWaterInstant.frequency = Convert.ToDecimal(f_Frequency); } i_pointer += 6; nParamCount--; break; case EMS_COMM_PARAMS.FluxInstantValue: //瞬时流值 Array.Copy(buffer, i_pointer + 2 + 2, b_FluxInstantValue, 0, 2); Array.Copy(buffer, i_pointer + 2, b_FluxInstantValue, 2, 2); f_FluxInstantValue = BitConverter.ToSingle(b_FluxInstantValue.Reverse().ToArray(), 0); logInfoStr += $";瞬时流值:{f_FluxInstantValue}"; if (float.IsNaN(f_FluxInstantValue)) { recordWaterInstant.fluxFlow = appConfig.virtualValue; } else { recordWaterInstant.fluxFlow = Convert.ToDecimal(f_FluxInstantValue); } i_pointer += 6; nParamCount--; break; case EMS_COMM_PARAMS.FluxEyeableTotalValue: //累计流量值 Array.Copy(buffer, i_pointer + 2 + 2, b_FluxEyeableTotalValue, 0, 2); Array.Copy(buffer, i_pointer + 2, b_FluxEyeableTotalValue, 2, 2); f_FluxEyeableTotalValue = BitConverter.ToSingle(b_FluxEyeableTotalValue.Reverse().ToArray(), 0); logInfoStr += $";累计流量值:{f_FluxEyeableTotalValue}"; if (float.IsNaN(f_FluxEyeableTotalValue)) { recordWaterInstant.waterFlow = 0; } else { recordWaterInstant.waterFlow = Convert.ToDecimal(f_FluxEyeableTotalValue); } i_pointer += 6; nParamCount--; break; case EMS_COMM_PARAMS.HeatInstantValue: //瞬时热量 Array.Copy(buffer, i_pointer + 2 + 2, b_HeatInstantValue, 0, 2); Array.Copy(buffer, i_pointer + 2, b_HeatInstantValue, 2, 2); f_HeatInstantValue = BitConverter.ToSingle(b_HeatInstantValue.Reverse().ToArray(), 0); logInfoStr += $";瞬时热量:{f_HeatInstantValue}"; if (float.IsNaN(f_HeatInstantValue)) { //recordWaterInstant.heatInstantValue = 0; } else { //recordWaterInstant.heatInstantValue = Convert.ToDecimal(f_HeatInstantValue); } i_pointer += 6; nParamCount--; break; case EMS_COMM_PARAMS.HeatToftalValue: //累计热量值 Array.Copy(buffer, i_pointer + 2 + 2, b_HeatToftalValue, 0, 2); Array.Copy(buffer, i_pointer + 2, b_HeatToftalValue, 2, 2); f_HeatToftalValue = BitConverter.ToSingle(b_HeatToftalValue.Reverse().ToArray(), 0); logInfoStr += $";累计热量值:{f_HeatToftalValue}"; if (float.IsNaN(f_HeatToftalValue)) { //recordWaterInstant.heatToftalValue = 0; } else { //recordWaterInstant.heatToftalValue = Convert.ToDecimal(f_HeatToftalValue); } i_pointer += 6; nParamCount--; break; case EMS_COMM_PARAMS.Density: //密度值 Array.Copy(buffer, i_pointer + 2 + 2, b_Density, 0, 2); Array.Copy(buffer, i_pointer + 2, b_Density, 2, 2); f_Density = BitConverter.ToSingle(b_Density.Reverse().ToArray(), 0); logInfoStr += $";密度值:{f_Density}"; if (float.IsNaN(f_Density)) { //recordWaterInstant.density = 0; } else { //recordWaterInstant.density = Convert.ToDecimal(f_Density); } i_pointer += 6; nParamCount--; break; case EMS_COMM_PARAMS.CJSJ: //采集时间 string strDateTime = "20" + buffer[i_pointer + 2 + 5].ToString("x2") + "-" + buffer[i_pointer + 2 + 4].ToString("x2") + "-" + buffer[i_pointer + 2 + 3].ToString("x2") + " " + buffer[i_pointer + 2 + 2].ToString("x2") + ":" + buffer[i_pointer + 2 + 1].ToString("x2") + ":" + buffer[i_pointer + 2].ToString("x2"); logInfoStr += $";终端时间:{strDateTime}"; recordWaterInstant.collectTime = Convert.ToDateTime(strDateTime); i_pointer += 8; nParamCount--; break; } } if (nParamCount == 0) { logger.Info($"仪表:{recordWaterInstant.monitorId};解析完成数据:{logInfoStr}"); recordWaterInstant.collectType = collectType; recordWaterInstant.recordTime = DateTime.Now; if (appConfig.virtualFlag) { if (recordWaterInstant.fluxFlow != appConfig.virtualValue) { var info = SqlSugarHelper.Db.Insertable(recordWaterInstant).ExecuteCommand(); if (info > 0) { logger.Info($"仪表:{recordWaterInstant.monitorId}数据保存成功"); } } else { logger.Info($"仪表:{recordWaterInstant.monitorId}存在FFFF值不保存该条数据"); } } else { recordWaterInstant.fluxFlow = recordWaterInstant.fluxFlow == appConfig.virtualValue ? 0 : recordWaterInstant.fluxFlow; var info = SqlSugarHelper.Db.Insertable(recordWaterInstant).ExecuteCommand(); if (info > 0) { logger.Info($"仪表:{recordWaterInstant.monitorId}数据保存成功"); } } } iFirstMeterID += 58; }//end for } catch (Exception ex) { logger.Info($"SDataMessageHandling水表数据解析异常:{ex}"); } } /// /// 修改终端连接状态 /// /// 终端编号 /// 终端状态:0-离线;1-在线 public void UpdateCollectDeviceOnLineState(string collectId, int onLineFlag) { try { int result = SqlSugarHelper.Db.Updateable() .SetColumns(it => new BaseCollectDeviceInfo() { OnlineState = onLineFlag, UpdateTime = DateTime.Now }).Where(it => it.CollectDeviceId == collectId).ExecuteCommand(); if (result > 0) { logger.Trace(string.Format("》》》终端:{0};状态修改成功:{1}", collectId, onLineFlag == 1 ? "在线" : "离线")); } else { logger.Trace(string.Format("终端:{0};状态修改失败", collectId)); } } catch (Exception ex) { logger.Error(string.Format("UpdateCollectDeviceOnLineState修改终端状态异常:{0}", ex.Message)); } } public bool SendMessageToClient(SocketClient client, MessagePack pMessagePack) { ushort num = 0; try { byte[] SendBuffer = new byte[12]; SendBuffer[num] = pMessagePack.m_BeginChar; num = (ushort)(num + 1); SendBuffer[num] = pMessagePack.m_EnergyType; num = (ushort)(num + 1); Array.Copy(pMessagePack.m_Meteraddr, 0, SendBuffer, num, pMessagePack.m_Meteraddr.Length); num = (ushort)(num + 2); Array.Copy(pMessagePack.m_Msta, 0, SendBuffer, num, pMessagePack.m_Msta.Length); num = (ushort)(num + 2); SendBuffer[num] = pMessagePack.m_StartFlag; num = (ushort)(num + 1); SendBuffer[num] = pMessagePack.m_MessageType; num = (ushort)(num + 1); Array.Copy(pMessagePack.m_PackLen, 0, SendBuffer, num, pMessagePack.m_PackLen.Length); num = (ushort)(num + 2); pMessagePack.m_Verify = MsgUtil.CalculateVerify(SendBuffer, SendBuffer.Length - 1)[0]; SendBuffer[num] = pMessagePack.m_Verify; num = (ushort)(num + 1); SendBuffer[num] = pMessagePack.m_EndChar; logger.Info($"向客户端:{client.ID};地址:{client.GetIPPort()};发送终端消息:{msgUtil.bytesToHexStr(SendBuffer, SendBuffer.Length)}"); client.SendAsync(SendBuffer); return true; } catch (Exception ex) { logger.Error($"SendMessageToClient异常:{ex.Message}"); return false; } } public bool SendMessageAsync(SocketClient client, MessagePack pMessagePack) { UInt16 iPos = 0; string flag = ""; try { byte[] SendBuffer = new byte[12]; SendBuffer[iPos] = pMessagePack.m_BeginChar; iPos += 1; SendBuffer[iPos] = pMessagePack.m_EnergyType; iPos += 1; Array.Copy(pMessagePack.m_Meteraddr, 0, SendBuffer, iPos, pMessagePack.m_Meteraddr.Length); iPos += 2; Array.Copy(pMessagePack.m_Msta, 0, SendBuffer, iPos, pMessagePack.m_Msta.Length); iPos += 2; SendBuffer[iPos] = pMessagePack.m_StartFlag; iPos += 1; SendBuffer[iPos] = pMessagePack.m_MessageType; iPos += 1; Array.Copy(pMessagePack.m_PackLen, 0, SendBuffer, iPos, pMessagePack.m_PackLen.Length); iPos += 2; pMessagePack.m_Verify = MsgUtil.CalculateVerify(SendBuffer, SendBuffer.Length - 1)[0]; SendBuffer[iPos] = pMessagePack.m_Verify; iPos += 1; SendBuffer[iPos] = pMessagePack.m_EndChar; flag = pMessagePack.m_EnergyType.ToString("X2"); flag += msgUtil.ConverToString(pMessagePack.m_Meteraddr); client.Send(SendBuffer); logger.Info($"向客户端:{client.ID};地址:{client.GetIPPort()};发送终端消息:{msgUtil.bytesToHexStr(SendBuffer, SendBuffer.Length)}"); return true; } catch (Exception ex) { logger.Error($"SendMessageAsync异常:{ex.Message}"); return false; } } public bool SendTimeSyncToClient(SocketClient client, MessagePack pMessagePack) { ushort num = 0; try { byte[] SendBuffer = new byte[24]; SendBuffer[num] = pMessagePack.m_BeginChar; num = (ushort)(num + 1); SendBuffer[num] = pMessagePack.m_EnergyType; num = (ushort)(num + 1); Array.Copy(pMessagePack.m_Meteraddr, 0, SendBuffer, num, pMessagePack.m_Meteraddr.Length); num = (ushort)(num + 2); Array.Copy(pMessagePack.m_Msta, 0, SendBuffer, num, pMessagePack.m_Msta.Length); num = (ushort)(num + 2); SendBuffer[num] = pMessagePack.m_StartFlag; num = (ushort)(num + 1); SendBuffer[num] = pMessagePack.m_MessageType; num = (ushort)(num + 1); Array.Copy(pMessagePack.m_PackLen, 0, SendBuffer, num, pMessagePack.m_PackLen.Length); num = (ushort)(num + 2); DateTime currentTime = DateTime.Now; byte[] timeBuffer = new byte[] { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, (byte)currentTime.Second, (byte)currentTime.Minute, (byte)currentTime.Hour, (byte)(currentTime.Year%100), (byte)currentTime.Month, (byte)currentTime.Day, }; Array.Copy(timeBuffer, 0, SendBuffer, num, timeBuffer.Length); num = (ushort)(num + 12); pMessagePack.m_Verify = MsgUtil.CalculateVerify(SendBuffer, SendBuffer.Length - 1)[0]; SendBuffer[num] = pMessagePack.m_Verify; num = (ushort)(num + 1); SendBuffer[num] = pMessagePack.m_EndChar; logger.Info($"向客户端:{client.ID};地址:{client.GetIPPort()};发送终端消息:{msgUtil.bytesToHexStr(SendBuffer, SendBuffer.Length)}"); client.SendAsync(SendBuffer); return true; } catch (Exception ex) { logger.Error($"SendMessageToClient异常:{ex.Message}"); return false; } } } }