#
whycq
2025-01-08 00fd3eb5037f07763d4e8d3e5729066d3dc097ea
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
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<T extends IActionBody> 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 = Utils.reverse(actionBody.writeToBytes());   // 命令参数
 
        return Utils.merge(serialNoBytes,cmdCode, valByte, bytes);
    }
 
    void readFromBytes(byte[] messageBodyBytes) {
 
    }
 
 
    public AgvAction(Class<T> 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<T> setAgvNo(String agvNo) {
        this.agvNo = agvNo;
        return this;
    }
 
    public AgvAction<T> setSerialNo(String serialNo) {
        this.serialNo = serialNo;
        return this;
    }
 
    public AgvAction<T> bodySync(DataSupport<T> 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<T> setVal(int val) {
        this.val = val;
        return this;
    }
}