using Newtonsoft.Json; using SensorHub.Servers; using SensorHub.Servers.Commands.CASICCommands; using SensorHub.Servers.JsonFormat; using SuperSocket.SocketBase.Command; using SuperSocket.SocketBase.Protocol; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace SensorHub.CorrRate { public class CorrRateConfig : CommandBase<ThirdpartySession, StringRequestInfo> { public override void ExecuteCommand(ThirdpartySession session, StringRequestInfo requestInfo) { String mType = requestInfo.Parameters[0]; String devCode = requestInfo.Parameters[1]; String mBody = requestInfo.Parameters[2]; String ts = requestInfo.Parameters[3]; session.Logger.Info("接收下发配置数据如下:"); session.Logger.Info("消息类型:" + mType); session.Logger.Info("设备编号:" + devCode); session.Logger.Info("配置参数:" + mBody); session.Logger.Info("时间戳:" + ts); CorrRateConfigJson configJson = JsonConvert.DeserializeObject<CorrRateConfigJson>(mBody); if (configJson.bType.Contains("Query")) { byte[] btPdu = new byte[2]; //2个字节 btPdu[0] = 0x01; btPdu[1] = 0x8B; Common.dispatchQueryMessage(session, "CorrRate", devCode, configJson.concentratorCode, btPdu); } else if (configJson.bType.Contains("Config")) { CorrRateConfigItemsJson configItems = configJson.configItems; byte[] configTag = getConfigTag(configItems); byte[] btPdu = new byte[2]; //2个字节 btPdu[0] = 0x03; btPdu[1] = 0x8B; Common.dispatchConfigMessage(session, "CorrRate", devCode, configJson.netType, configJson.concentratorCode, btPdu, configTag); } else { session.Logger.Info("业务类型bType无法识别:" + configJson.bType); } } //获取配置tag private byte[] getConfigTag(CorrRateConfigItemsJson configItems) { byte[] result = new byte[128]; int flag = 0; if (configItems.acqStart != null) { //采集开始时间 byte[] tag = { 0x10, 0x00, 0x01, 0x04, 0x00, 0x02, 0x00, 0x00 }; Int16 stime = (Int16)(Convert.ToInt16(configItems.acqStart.Substring(0, 2)) * 60 + Convert.ToInt16(configItems.acqStart.Substring(3, 2))); byte[] value = BitConverter.GetBytes(stime); value.CopyTo(tag, 6); tag.CopyTo(result, flag); flag += 8; } if (configItems.interval != 0) { //采集间隔 byte[] tag = { 0x10, 0x00, 0x01, 0x05, 0x00, 0x02, 0x00, 0x00 }; byte[] value = BitConverter.GetBytes(configItems.interval); value.CopyTo(tag, 6); tag.CopyTo(result, flag); flag += 8; } if (configItems.times != 0) { //采集次数 byte[] tag = { 0x10, 0x00, 0x01, 0x06, 0x00, 0x02, 0x00, 0x00 }; byte[] value = BitConverter.GetBytes(configItems.times); value.CopyTo(tag, 6); tag.CopyTo(result, flag); flag += 8; } if (configItems.repeat != 0) { //重试次数 byte[] tag = { 0x10, 0x00, 0x00, 0x0A, 0x00, 0x01, 0x00 }; byte value = Convert.ToByte(configItems.repeat); tag[6] = value; tag.CopyTo(result, flag); flag += 7; } if (configItems.ip != null && configItems.port != 0) { byte[] tag = Common.getIpPortTag(configItems.ip, configItems.port); tag.CopyTo(result, flag); flag += tag.Length; } return result.Take(flag).ToArray(); } } }