diff --git a/Ems.CollectService.Analysis/BufferAnalysis.cs b/Ems.CollectService.Analysis/BufferAnalysis.cs index 3db84b3..d45b6ba 100644 --- a/Ems.CollectService.Analysis/BufferAnalysis.cs +++ b/Ems.CollectService.Analysis/BufferAnalysis.cs @@ -154,11 +154,6 @@ namespace Ems.CollectService.Analysis if(SendMessageAsync(client, SendMessagePackInfo)) { logger.Debug($"向客户端:{client.ID};地址:{client.GetIPPort()};回复心跳指令成功"); - Thread.Sleep(1000); - SendMessagePackInfo.m_MessageType = (byte)CallbackSendData._SetTime; - SendMessagePackInfo.m_PackLen = new byte[] {0x00, 0x0D}; - bool isRes = SendTimeSyncToClient(client, SendMessagePackInfo); - logger.Info($"向客户端:{client.ID};地址:{client.GetIPPort()};发送校时指令{(isRes ? "成功":"失败")}"); return true; } else @@ -173,6 +168,58 @@ namespace Ems.CollectService.Analysis return false; } } + + /// + /// 校时指令处理 + /// + /// + /// + /// + /// + public bool CheckTimeHandlingAsync(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._SetTime; + SendMessagePackInfo.m_PackLen = new byte[] {0x00, 0x06}; + bool isRes = SendTimeSyncToClient(client, SendMessagePackInfo); + if(isRes) + { + logger.Info($"向客户端:{client.ID};地址:{client.GetIPPort()};发送校时指令{(isRes ? "成功":"失败")}"); + return true; + } + else + { + logger.Debug($"向客户端:{client.ID};地址:{client.GetIPPort()};回复校时指令失败"); + return false; + } + } + catch (Exception ex) + { + logger.Error($"CheckTimeHandlingAsync校时指令解析异常:{ex.Message}"); + return false; + } + } /// /// 电力仪表 @@ -917,7 +964,7 @@ namespace Ems.CollectService.Analysis ushort num = 0; try { - byte[] SendBuffer = new byte[25]; + byte[] SendBuffer = new byte[18]; SendBuffer[num] = pMessagePack.m_BeginChar; num = (ushort)(num + 1); SendBuffer[num] = pMessagePack.m_EnergyType; @@ -936,13 +983,6 @@ namespace Ems.CollectService.Analysis DateTime currentTime = DateTime.Now; byte[] timeBuffer = new byte[] { - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, HexStrTorbytes(currentTime.ToString("ss"))[0], HexStrTorbytes(currentTime.ToString("mm"))[0], HexStrTorbytes(currentTime.ToString("HH"))[0], @@ -952,7 +992,7 @@ namespace Ems.CollectService.Analysis }; Array.Copy(timeBuffer, 0, SendBuffer, num, timeBuffer.Length); - num = (ushort)(num + 13); + num = (ushort)(num + 6); pMessagePack.m_Verify = MsgUtil.CalculateVerify(SendBuffer, SendBuffer.Length - 1)[0]; SendBuffer[num] = pMessagePack.m_Verify; diff --git a/Ems.CollectService.TouchSocket/MyFixedHeaderRequestInfo.cs b/Ems.CollectService.TouchSocket/MyFixedHeaderRequestInfo.cs index e4afc56..a28f2ab 100644 --- a/Ems.CollectService.TouchSocket/MyFixedHeaderRequestInfo.cs +++ b/Ems.CollectService.TouchSocket/MyFixedHeaderRequestInfo.cs @@ -58,7 +58,7 @@ namespace Ems.CollectService.TouchSocket byteBlock.Pos = 0; byteBlock.Read(out m_sync, 10); Array.Copy(header, 8, m_length, 0, 2); - if (header[7] == 0X01 || header[7] == 0X24) + if (header[7] == 0X01 || header[7] == 0X24 || header[7] == 0x08) { this.m_bodyLength = 2;//先把crc校验和end都获取。 return true; diff --git a/Ems.CollectService.TouchSocket/TcpServer.cs b/Ems.CollectService.TouchSocket/TcpServer.cs index 35d3885..013c811 100644 --- a/Ems.CollectService.TouchSocket/TcpServer.cs +++ b/Ems.CollectService.TouchSocket/TcpServer.cs @@ -128,6 +128,11 @@ namespace Ems.CollectService.TouchSocket logger.Info($"{client.ID}心跳指令,Header:{header};Body:{body}"); bufferAnalysis.HeartMessageHandlingAsync(client, buffer, requestInfo.Header.Length + requestInfo.Body.Length); break; + case 0X08: + //请求校时指令 + logger.Info($"{client.ID}请求校时指令,Header:{header};Body:{body}"); + bufferAnalysis.CheckTimeHandlingAsync(client, buffer, requestInfo.Header.Length + requestInfo.Body.Length); + break; case 131: //电表数据 //db.StringSet("DnbMessage:" + clientId, body, TimeSpan.FromMinutes(10));