public static Header HeaderParse(IByteBuffer buffer) { // start sign var startSign = buffer.ReadByte(); // task sn var taskId = buffer.ReadByte(); // msg sn var msgSn = buffer.ReadByte(); // ttl var ttl = buffer.ReadByte(); // 通信方式 // 0x81 - 以太网, 0x82 - GPRS , 0x83 - NBIOT var transportCode = buffer.ReadByte(); var transportType = (TransportType)transportCode; // 时间戳 var timestamp = buffer.ReadInt(); var date = TimestampUtil.ConvertSeconds2DateTime(timestamp).AddHours(8); // 控制命令 心跳 - 0x4064 var funCode = buffer.ReadShort(); var funType = (FunType)funCode; // src mac - 6B var srcMacBytes = new byte[6]; buffer.ReadBytes(srcMacBytes); // dst mac var dstMacBytes = new byte[6]; buffer.ReadBytes(dstMacBytes); var srcMac = BytesUtil.BytesToHex(srcMacBytes); var dstMac = BytesUtil.BytesToHex(dstMacBytes); // content length var length = buffer.ReadShort(); byte[] content = null; if (length > 0) { content = new byte[length]; buffer.ReadBytes(content); } var crc = new byte[2]; buffer.ReadBytes(crc); var header = new Header() { TaskId = taskId, MsgSn = msgSn, Ttl = ttl, TransportType = transportType, Time = date, FunType = funType, SrcMac = srcMac, DstMac = dstMac, Length = length, Content = content, crc = crc }; return(header); }
protected override void Decode(IChannelHandlerContext context, IByteBuffer input, List <object> output) { var msg = new byte[input.ReadableBytes]; input.ReadBytes(msg); var hex = BytesUtil.HexInsertSpace(BytesUtil.BytesToHex(msg)); var srcMac = hex.Substring(50, 18).Replace(" ", ""); Console.WriteLine($"Time: {DateTime.Now} Mac: {srcMac} => 收到服务器消息:{hex}"); }
private byte[] GetCrackDeviceMsg() { var core = MessageBuilder.BuildCrackData(1005, 30f); var sensorMsg = MessageBuilder.BuildSensorMsg(mac, 1, SensorType.THLSD, core); var msg = MessageBuilder.BuildMessage(0, TransportType.GRPS, FunType.GatewayCacheDataBPublish, mac, sensorMsg); var hex = BytesUtil.BytesToHex(msg); Console.WriteLine($"Time: {DateTime.Now} Mac: {mac} => 发送消息:{hex}"); return(msg); }
public override void ChannelRead(IChannelHandlerContext context, object message) { var packet = message as DatagramPacket; var buffer = packet.Content; var resp = Unpooled.Buffer(buffer.ReadableBytes); buffer.ReadBytes(resp); if (buffer != null) { Console.WriteLine("Received from client: " + BytesUtil.BytesToHex(resp.Array)); } var respPacket = new DatagramPacket(resp, packet.Sender); context.WriteAndFlushAsync(respPacket); }
private byte[] GetSettleDeviceMsg() { var srcUnmarkHeight = 3000; var srcHeight = 10050.32f; var unmarkHeight = srcUnmarkHeight + index * 12; var height = srcHeight + index * 1.2f * (new Random().Next(10)); var core = MessageBuilder.BuildSettleData(unmarkHeight, -3, height); var sensorMsg = MessageBuilder.BuildSensorMsg(mac, 1, SensorType.THSTC, core); var msg = MessageBuilder.BuildMessage(0, TransportType.GRPS, FunType.GatewayCacheDataBPublish, mac, sensorMsg); var hex = BytesUtil.HexInsertSpace(BytesUtil.BytesToHex(msg)); Console.WriteLine($"Time: {DateTime.Now} Index : {index} Mac: {mac} => 发送消息:{hex}"); index++; return(msg); }
protected override void ChannelRead0(IChannelHandlerContext ctx, DatagramPacket msg) { Logger.Info("GkDevice收到server端消息"); var buffer = msg.Content; if (buffer == null) { return; } var serverMsg = Unpooled.Buffer(buffer.ReadableBytes); buffer.ReadBytes(serverMsg); var hexMsg = BytesUtil.BytesToHex(serverMsg.Array); Logger.Info($"收到server端消息, data:{hexMsg}"); if (hexMsg.Contains("7ffe0048")) { var deviceInfo = DeviceInfoMsg.Replace("-", ""); var bytes = BytesUtil.Hex2Bytes(deviceInfo); var deviceBuffer = Unpooled.WrappedBuffer(bytes); ctx.WriteAndFlushAsync(new DatagramPacket(deviceBuffer, msg.Sender)); } }