package com.example.agvcontroller.protocol; import com.example.agvcontroller.action.ForceSwitchAction; import com.example.agvcontroller.socket.NettyServerHandler; import java.io.Serializable; /** * Created by vincent on 2023/3/14 */ @SuppressWarnings("all") public class AgvAction implements Serializable { private static final long serialVersionUID = -1701520567284510548L; private String agvNo; private String serialNo; private int val; private T actionBody; private HandleCmdType handleCmdType; public byte[] writeToBytes() { // byte[] bytes = new String serialNo = Utils.zeroFill(this.serialNo, 16); byte[] serialNoBytes = Utils.reverse(serialNo.getBytes()); // 流水号 byte cmdCode = (byte) handleCmdType.cmdCode; // 命令码 byte valByte = (byte) val; // 属性值 byte[] bytes = actionBody.writeToBytes(); // 命令参数 return Utils.merge(serialNoBytes,cmdCode, valByte, bytes); } void readFromBytes(byte[] messageBodyBytes) { } public AgvAction(Class clazz) { try { actionBody = clazz.newInstance(); } catch (Exception e) { e.printStackTrace(); } this.handleCmdType = HandleCmdType.find(actionBody.getClass()); if (null == handleCmdType) { throw new RuntimeException(clazz.getName() + "检索 HandleCmdType 枚举失败"); } } public AgvAction setAgvNo(String agvNo) { this.agvNo = agvNo; return this; } public AgvAction setSerialNo(String serialNo) { this.serialNo = serialNo; return this; } public AgvAction bodySync(DataSupport dataSupport) { dataSupport.sync(this.actionBody); return this; } public static void main(String[] args) { AgvAction agvAction = new AgvAction<>(ForceSwitchAction.class) .setAgvNo("1") .setSerialNo("asdsadsadsad") .setVal(1) .bodySync((action) -> action.setPwd((short) 21)); NettyServerHandler.sendMessageToClient("123213", agvAction); } public String getAgvNo() { return agvNo; } public String getSerialNo() { return serialNo; } public T getActionBody() { return actionBody; } public void setActionBody(T actionBody) { this.actionBody = actionBody; } public HandleCmdType getHandleCmdType() { return handleCmdType; } public void setHandleCmdType(HandleCmdType handleCmdType) { this.handleCmdType = handleCmdType; } public int getVal() { return val; } public AgvAction setVal(int val) { this.val = val; return this; } }