package com.phm.netty.domain; import cn.hutool.core.date.DateUtil; import cn.hutool.core.util.ObjectUtil; import com.alibaba.fastjson2.JSONObject; import com.phm.netty.utils.BitUtils; import com.phm.netty.utils.ByteUtils; import io.netty.util.CharsetUtil; import lombok.Data; import lombok.experimental.Accessors; import javax.xml.bind.annotation.XmlRootElement; import java.io.Serializable; /** * @Description Message * @Author WGK * @Date 2023/9/14 11:41 */ @Data @Accessors(chain = true) public class Message implements Serializable { /** * 数据长度 */ private int len; /** * 接收的通讯数据body */ private String data; /** * 消息类型 */ private short type; /** * 毫秒时间戳,从2020.1.1开始 */ private int timestamp = (int) (DateUtil.current(false) - DateUtil.parse("2020-01-01").getTime()); /** * 数据源 */ private String source = "GPHM"; /** * 目标 */ private String target = "SPHM"; /** * 目标 */ private int reserver = 0; public Message() { } /** * 字节数组解析为 Message 对象 * * @param msg Message 对象 * @param buf 字节数组 * @param len 要解析的长度 */ public static void bytesToMsg(Message msg, byte[] buf, int len) { int index = 0; msg.len = len; msg.type = BitUtils.toShort(buf, index); index += 2; msg.source = BitUtils.toString(buf, index); index += 8; msg.target = BitUtils.toString(buf, index); index += 8; msg.timestamp = BitUtils.toInt(buf, index); index += 4; msg.reserver = BitUtils.toInt(buf, index); index += 4; byte[] bytes = new byte[len - index]; System.arraycopy(buf, index, bytes, 0, len - index); msg.data = new String(bytes, CharsetUtil.UTF_8); } public static void main(String[] args) { } public static byte[] testMsg() { Message message = new Message(); message.setType((short) 1).setTarget("GPHM").setSource("SPHM").setData("故障预测ERTYU"); return Message.msgToBytes(message); } public static byte[] msgToBytes(Message msg) { if (ObjectUtil.isEmpty(msg)) { return new byte[0]; } int dataLen = msg.data.getBytes().length; msg.setLen(dataLen + 26); byte[] bytes = new byte[msg.len + 4]; byte[] msgLenBytes = BitUtils.getBytes(msg.len); System.arraycopy(msgLenBytes, 0, bytes, 0, 4); byte[] msgTypeBytes = BitUtils.getBytes(msg.type); System.arraycopy(msgTypeBytes, 0, bytes, 4, 2); byte[] msgSourceBytes = BitUtils.getBytes(msg.source, 8); System.arraycopy(msgSourceBytes, 0, bytes, 6, 8); byte[] msgTargetBytes = BitUtils.getBytes(msg.target, 8); System.arraycopy(msgTargetBytes, 0, bytes, 14, 8); byte[] msgTimeStampBytes = BitUtils.getBytes(msg.timestamp); System.arraycopy(msgTimeStampBytes, 0, bytes, 22, 4); byte[] msgReserverBytes = BitUtils.getBytes(msg.reserver); System.arraycopy(msgReserverBytes, 0, bytes, 26, 4); byte[] msgDataBytes = msg.getData().getBytes(); System.arraycopy(msgDataBytes, 0, bytes, 30, msgDataBytes.length); return bytes; } public Message(Object object) { String str = object.toString(); JSONObject jsonObject = JSONObject.parseObject(str); type = Short.parseShort(jsonObject.getString("type")); data = jsonObject.getString("data"); len = str.length(); } public String toJsonString() { return "{" + "\n \"len\": " + len + ",\n" + " \"type\": " + type + ",\n" + " \"source\": " + source + ",\n" + " \"target\": " + target + ",\n" + " \"timestamp\": " + timestamp + ",\n" + " \"data\": " + data + "\n" + "}"; } }