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.Lamphouse { public class LamphouseConfig : 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); LamphouseConfigJson configJson = JsonConvert.DeserializeObject<LamphouseConfigJson>(mBody); byte[] btPdu = new byte[2]; //2个字节 btPdu[1] = 0x90; if (configJson.bType.Contains("Query")) { btPdu[0] = 0x01; Common.dispatchQueryMessage(session, "Lamphouse", devCode, configJson.concentratorCode, btPdu); } else if (configJson.bType.Contains("Config")) { LamphouseConfigItemsJson configItems = configJson.configItems; byte[] configTag = getConfigTag(configItems); btPdu[0] = 0x03; Common.dispatchConfigMessage(session, "Lamphouse", devCode, configJson.netType, configJson.concentratorCode, btPdu, configTag); } else { session.Logger.Info("业务类型bType无法识别:" + configJson.bType); } } //获取配置tag private byte[] getConfigTag(LamphouseConfigItemsJson configItems) { byte[] result = new byte[128]; int flag = 0; if (configItems.acqStart != null) { //采集开始时间 byte[] tag = { 0x10, 0x00, 0x01, 0x04, 0x00, 0x02, 0x00, 0x00 }; getTimeTag(configItems.acqStart, 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.thresh != 0) { //电流阈值 byte[] tag = { 0x10, 0x00, 0x09, 0x00, 0x00, 0x02, 0x00,0x00 }; byte[] value = BitConverter.GetBytes(configItems.thresh); value.CopyTo(tag, 6); tag.CopyTo(result, flag); flag += 8; } if (configItems.Channel1TurnOnTime != null) { //开启时间 byte[] tag = { 0x10, 0x00, 0x02, 0x01, 0x00, 0x02, 0x00, 0x00 }; getTimeTag(configItems.acqStart, tag).CopyTo(result, flag); flag += 8; } if (configItems.Channel1TurnOffTime != null) { //关闭时间 byte[] tag = { 0x10, 0x00, 0x02, 0x02, 0x00, 0x02, 0x00, 0x00 }; getTimeTag(configItems.acqStart, tag).CopyTo(result, flag); flag += 8; } return result.Take(flag).ToArray(); } private byte[] getTimeTag(String time, byte[] tag) { Int16 stime = (Int16)(Convert.ToInt16(time.Substring(0, 2)) * 60 + Convert.ToInt16(time.Substring(3, 2))); byte[] value = BitConverter.GetBytes(stime); value.CopyTo(tag, 6); return tag; } } }