using Newtonsoft.Json; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace SensorHub.Config { public class ConfigBuild { public static byte[] buildQueryFrame(String devCode) { byte[] frame = { 0xA3, 0x20, 0x00, 0x10, //长度 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, //设备编号 0x00, //通信方式 0x00, 0x00, //目标节点地址 0x01, 0x00, //PDUType 0x01, //Seq 0x20, 0x00, 0x00, 0x01 //服务器实时查询数据指令 }; byte[] btDevCode = strToHexByte(devCode); btDevCode.CopyTo(frame, 4); Array.Copy(btDevCode, 4, frame, 11, 2); frame[14] = getDevTypeByDevCode(devCode); return frame; } public static byte[] build433ConfigFrame(String devCode, String configItem) { byte[] frame = { 0xA3, 0x20, 0x00, 0x00, //长度 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, //设备编号 0x00, //通信方式 0x00, 0x00, //目标节点地址 0x03, 0x00, //PDUType 0x01 //Seq //0x10, 0x00, 0x00, 0x51, 0x00, 0x06, //时间戳 //0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; byte[] btDevCode = strToHexByte(devCode); btDevCode.CopyTo(frame, 4); Array.Copy(btDevCode, 4, frame, 11, 2); byte[] configTag = getConfigTag(configItem); int length = frame.Length + configTag.Length; byte[] config = new byte[length]; frame.CopyTo(config, 0); configTag.CopyTo(config, frame.Length); //config[10] = routeflag;//通信方式 //config[15] = 0x01;//Seq config[14] = getDevTypeByDevCode(devCode); byte[] btLens = new byte[2];//数据帧长度 byte[] btlens0 = BitConverter.GetBytes(length - 4); btLens[0] = btlens0[1]; btLens[1] = btlens0[0]; btLens.CopyTo(config, 2); return config; } public static byte[] buildConfigFrame(String devCode, String configItem) { byte[] frame = { 0xA3, 0x20, 0x00, 0x00, //长度 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, //设备编号 0x00, //通信方式 0x00, 0x00, //目标节点地址 0x03, 0x00, //PDUType 0x01, //Seq 0x10, 0x00, 0x00, 0x51, 0x00, 0x06, //时间戳 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; byte[] btDevCode = strToHexByte(devCode); btDevCode.CopyTo(frame, 4); Array.Copy(btDevCode, 4, frame, 11, 2); byte[] configTag = getConfigTag(configItem); int length = frame.Length + configTag.Length; byte[] config = new byte[length]; frame.CopyTo(config, 0); configTag.CopyTo(config, frame.Length); byte[] btLens = new byte[2];//数据帧长度 byte[] btlens0 = BitConverter.GetBytes(length - 4); btLens[0] = btlens0[1]; btLens[1] = btlens0[0]; btLens.CopyTo(config, 2); return config; } //获取配置tag private static byte[] getConfigTag(String configItem) { byte[] result = new byte[256]; int flag = 0; if (string.IsNullOrEmpty(configItem)) { return new byte[0]; } ConfigItems configItems = JsonConvert.DeserializeObject<ConfigItems>(configItem); if (configItems.thresh != 0.0f) { //阈值0x10000901 byte[] tag = { 0x10, 0x00, 0x09, 0x01, 0x00, 0x04, 0x00, 0x00, 0x00, 0x00 }; byte[] value = BitConverter.GetBytes(configItems.thresh); value.CopyTo(tag, 6); tag.CopyTo(result, flag); flag += 10; } if (configItems.height != 0.0f) { //高度 byte[] tag = { 0x10, 0x00, 0x00, 0x60, 0x00, 0x04, 0x00, 0x00, 0x00, 0x00 }; byte[] value = BitConverter.GetBytes(configItems.height); value.CopyTo(tag, 6); tag.CopyTo(result, flag); flag += 10; } //if (configItems.period != 0) //{ // //上传周期 // byte[] tag = { 0x10, 0x00, 0x00, 0x62, // 0x00, 0x02, // 0x00, 0x00 }; // byte[] value = BitConverter.GetBytes(configItems.period); // tag[6] = value[1]; // tag[7] = value[0]; // 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.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); tag[6] = value[1]; tag[7] = value[0]; tag.CopyTo(result, flag); flag += 8; } if (configItems.period != 0) { //上传周期 byte[] tag = { 0x10, 0x00, 0x00, 0x62, 0x00, 0x02, 0x00, 0x00 }; byte[] value = BitConverter.GetBytes(configItems.period); tag[6] = value[1]; tag[7] = value[0]; 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); tag[6] = value[1]; tag[7] = value[0]; tag.CopyTo(result, flag); flag += 8; } if (configItems.elecThresh != 0.0f) { //电流阈值 byte[] tag = { 0x10, 0x00, 0x09, 0x00, 0x00, 0x04, 0x00,0x00,0x00,0x00 }; byte[] value = BitConverter.GetBytes(configItems.elecThresh); value.CopyTo(tag, 6); tag.CopyTo(result, flag); flag += 10; } if (configItems.liquidDataThreshold != 0.0f) { //电流阈值 byte[] tag = { 0x10, 0x00, 0x09, 0x02, 0x00, 0x04, 0x00,0x00,0x00,0x00 }; byte[] value = BitConverter.GetBytes(configItems.liquidDataThreshold); value.CopyTo(tag, 6); tag.CopyTo(result, flag); flag += 10; } if (configItems.sleepStartTime != null) { //休眠开始时间 byte[] tag = { 0x10, 0x00, 0x02, 0x02, 0x00, 0x02, 0x00, 0x00 }; getTimeTag(configItems.sleepStartTime, tag).CopyTo(result, flag); flag += 8; } if (configItems.limitDig != 0) { //开挖报警阈值 byte[] tag = { 0x10, 0x00, 0x09, 0x00, 0x00, 0x01, 0x00 }; byte value = Convert.ToByte(configItems.limitDig); tag[6] = value; tag.CopyTo(result, flag); flag += 7; } if (configItems.Channel1TurnOnTime != null) { //路灯1开启时间 byte[] tag = { 0x10, 0x00, 0x02, 0x01, 0x00, 0x02, 0x00, 0x00 }; getTimeTag(configItems.Channel1TurnOnTime, tag).CopyTo(result, flag); flag += 8; } if (configItems.Channel1TurnOffTime != null) { //路灯1关闭时间 byte[] tag = { 0x10, 0x00, 0x02, 0x02, 0x00, 0x02, 0x00, 0x00 }; getTimeTag(configItems.Channel1TurnOffTime, tag).CopyTo(result, flag); flag += 8; } if (configItems.Channel2TurnOnTime != null) { //路灯2开启时间 byte[] tag = { 0x10, 0x00, 0x02, 0x03, 0x00, 0x02, 0x00, 0x00 }; getTimeTag(configItems.Channel2TurnOnTime, tag).CopyTo(result, flag); flag += 8; } if (configItems.Channel2TurnOffTime != null) { //路灯2关闭时间 byte[] tag = { 0x10, 0x00, 0x02, 0x04, 0x00, 0x02, 0x00, 0x00 }; getTimeTag(configItems.Channel2TurnOffTime, tag).CopyTo(result, flag); flag += 8; } #region 多功能 if (configItems.acqStartFlow != null) { getMultiLeakTimeTag(configItems.acqStartFlow, 0x04).CopyTo(result, flag); flag += 8; } if (configItems.acqStartPressure != null) { getMultiLeakTimeTag(configItems.acqStartPressure, 0x10).CopyTo(result, flag); flag += 8; } if (configItems.acqStartNoise != null) { getMultiLeakTimeTag(configItems.acqStartNoise, 0x20).CopyTo(result, flag); flag += 8; } if (configItems.intervalFlow != 0) { getMultiLeakNumTag(configItems.intervalFlow, 0x05).CopyTo(result, flag); flag += 8; } if (configItems.intervalPressure != 0) { getMultiLeakNumTag(configItems.intervalPressure, 0x11).CopyTo(result, flag); flag += 8; } if (configItems.intervalNoise != 0) { getMultiLeakNumTag(configItems.intervalNoise, 0x21).CopyTo(result, flag); flag += 8; } if (configItems.acqTimesFlow != 0) { //采集次数 getMultiLeakNumTag(configItems.acqTimesFlow, 0x06).CopyTo(result, flag); flag += 8; } if (configItems.acqTimesPressure != 0) { //采集次数 getMultiLeakNumTag(configItems.acqTimesPressure, 0x12).CopyTo(result, flag); flag += 8; } if (configItems.acqTimesNoise != 0) { //采集次数 getMultiLeakNumTag(configItems.acqTimesNoise, 0x22).CopyTo(result, flag); flag += 8; } if (configItems.upStartFlow != null) { //上报开始时间 getMultiLeakTimeTag(configItems.upStartFlow, 0x07).CopyTo(result, flag); flag += 8; } if (configItems.upStartPressure != null) { //上报开始时间 getMultiLeakTimeTag(configItems.upStartPressure, 0x13).CopyTo(result, flag); flag += 8; } if (configItems.upStartNoise != null) { //上报开始时间 getMultiLeakTimeTag(configItems.upStartNoise, 0x23).CopyTo(result, flag); flag += 8; } if (configItems.upTimesFlow != 0) { //上报次数 getMultiLeakNumTag(configItems.upTimesFlow, 0x08).CopyTo(result, flag); flag += 8; } if (configItems.upTimesPressure != 0) { //上报次数 getMultiLeakNumTag(configItems.upTimesPressure, 0x14).CopyTo(result, flag); flag += 8; } if (configItems.upTimesNoise != 0) { //上报次数 getMultiLeakNumTag(configItems.upTimesNoise, 0x24).CopyTo(result, flag); flag += 8; } //压力下限0x10000900 if (configItems.lowerLimitPressure != 0.0) { byte[] tag = { 0x10, 0x00, 0x09, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x00}; byte[] value = BitConverter.GetBytes(configItems.lowerLimitPressure); value.CopyTo(tag, 6); tag.CopyTo(result, flag); flag += 10; } //压力上限0x10000901 if (configItems.upperLimitPressure != 0.0) { byte[] tag = { 0x10, 0x00, 0x09, 0x01, 0x00, 0x04, 0x00, 0x00, 0x00, 0x00}; byte[] value = BitConverter.GetBytes(configItems.upperLimitPressure); value.CopyTo(tag, 6); tag.CopyTo(result, flag); flag += 10; } #endregion if (configItems.MultiCollect != null) { //多功能实时采集 byte[] tag = { 0x20, 0x00, 0x00, 0x01, 0x00, 0x01, 0x00 }; byte value = Convert.ToByte(configItems.MultiCollect, 16); tag[6] = value; tag.CopyTo(result, flag); flag += 7; } if (configItems.SetBrightness != 0) { //调光 byte[] tag = { 0x10, 0x00, 0x02, 0x05, 0x00, 0x01, 0x00 }; byte value = Convert.ToByte(configItems.SetBrightness); tag[6] = value; tag.CopyTo(result, flag); flag += 7; } if (configItems.RemoteControl != null) { //远程控制 byte[] tag = { 0x50, 0x00, 0x00, 0x01, 0x00, 0x01, 0x00 }; byte value = (Convert.ToByte(configItems.RemoteControl, 16)); tag[6] = value; tag.CopyTo(result, flag); flag += 7; } if (configItems.ip != null && configItems.port != 0) { byte[] tag = getIpPortTag(configItems.ip, configItems.port); tag.CopyTo(result, flag); flag += tag.Length; } if (configItems.sm4Key != null) { //sm4算法密钥 byte[] tag = { 0x10, 0x00, 0x0A, 0x00, 0x00, 0x10, 0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00 }; byte[] value = strToHexByte(configItems.sm4Key); value.CopyTo(tag, 6); tag.CopyTo(result, flag); flag += 22; } if (configItems.ideKey != null) { //设备身份标识 byte[] tag = { 0x10, 0x00, 0x0A, 0x01, 0x00, 0x06, 0x00,0x00,0x00,0x00, 0x00,0x00 }; byte[] value = strToHexByte(configItems.sm4Key); value.CopyTo(tag, 6); tag.CopyTo(result, flag); flag += 12; } return result.Take(flag).ToArray(); } public static byte[] getIpPortTag(String ip, int port) { //修改ip 0x10000022 byte[] ipOid = { 0x10, 0x00, 0x00, 0x22 }; byte[] ipValue = System.Text.Encoding.Default.GetBytes(ip); byte[] ipLen = BitConverter.GetBytes((short)ipValue.Length); Array.Reverse(ipLen); //修改port 0x10000023 byte[] portOid = { 0x10, 0x00, 0x00, 0x23 }; byte[] portValue = System.Text.Encoding.Default.GetBytes(port.ToString()); byte[] portLen = BitConverter.GetBytes((short)portValue.Length); Array.Reverse(portLen); byte[] tag = new byte[4 + 2 + ipValue.Length + 4 + 2 + portValue.Length]; ipOid.CopyTo(tag, 0); ipLen.CopyTo(tag, 4); ipValue.CopyTo(tag, 6); portOid.CopyTo(tag, 6 + ipValue.Length); portLen.CopyTo(tag, 10 + ipValue.Length); portValue.CopyTo(tag, 12 + ipValue.Length); return tag; } private static byte[] getMultiLeakTimeTag(String time, byte code) { byte[] tag = { 0x10, 0x00, 0x01, code, 0x00, 0x02, 0x00, 0x00}; return getTimeTag(time, tag); } private static byte[] getMultiLeakNumTag(short number, byte code) { byte[] tag = { 0x10, 0x00, 0x01, code, 0x00, 0x02, 0x00, 0x00}; byte[] value = BitConverter.GetBytes(number); tag[6] = value[1]; tag[7] = value[0]; return tag; } //值是时间 private static byte[] getTimeTag(String time, byte[] tag) { Int16 stime = (Int16)(Convert.ToInt16(time.Substring(0, 2)) * 60 + Convert.ToInt16(time.Substring(2, 2))); byte[] value = BitConverter.GetBytes(stime); tag[6] = value[1]; tag[7] = value[0]; return tag; } private static byte getDevTypeByDevCode(String devCode) { Byte productType = Byte.Parse(devCode.Substring(0, 2)); String devType = System.Configuration.ConfigurationManager.AppSettings["productType" + productType]; return (Byte)(Int16.Parse(devType) | 0x80); } /// <summary> /// 字符串转16进制字节数组 /// </summary> /// <param name="hexString"></param> /// <returns></returns> private static 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; } } }