liurunyu
2024-05-13 2b2a90e952c2c59642c41d8af5759ceb51b9e099
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
package com.dy.common.mw.protocol;
 
import java.io.Serializable;
 
import com.alibaba.fastjson2.JSON ;
import com.alibaba.fastjson2.JSONObject;
 
public class Command implements Serializable{
 
    public static final long serialVersionUID;
    static {
        serialVersionUID = 201211292156L;
    }
 
    /**
     * 默认命令ID
     * 如果命令不在数据库中存储,或其他可以用defaultId
     */
    public static final String defaultId = "999999999" ;
    
    /**
     * 本条命令的ID
     * 一般是命令在数据库中存储记录ID的字符串形式
     */
    public String id; 
    
    /**
     * RTU协议名称,这个可以为空,因为RTU上行数据解析出协议名称,然后系统把协议名称保存在RTU数据记录中,从此协议名称就不再空了
     */
    public String protocol;
    
    /**
     * RTU 地址
     */
    public String rtuAddr ;
 
    /**
     * 命令类型:Rtu命令、针对监控中间件的命令
     * 由com.dy.common.mw.protocol.CommandType类定义
     */
    public String type ;
    
    /**
     * 功能码
     */
    public String code ; 
    
    /**
     * 不返回中间件对命令处理结果(true:不返回处理结果,false或null:返回处理结果)
     */
    public Boolean noRtMwDealRes ;
 
    /**
     * rtu返回命令结果 发向目的地web URL
     */
    public String rtuResultSendWebUrl ;
 
    /**
     * 具体参数数据
     */
    public Object param ;
    
    /**
     * 其他数据
     */
    public Object attachment ;
    
    public String toString(){
        String s = "命令id=" + id + "\n" ;
        s += (protocol == null ? "" : ("协议=" + protocol + "\n"));
        s += (rtuAddr == null ? "" : ("Rtu地址=" + rtuAddr + "\n"));
        s += "命令类型=" + (type.equals(CommandType.innerCommand)?"内部命令":"RTU命令") + "\n" ;
        s += (code == null ? "" : ("功能码=" + code + "\n")) ;
        s += "是否返回中间件对命令处理结果=" + (noRtMwDealRes == null?"是":(noRtMwDealRes?"是":"否") + "\n") ;
        if(param != null){
            s += "参数:" + param  ;
        }
        if(attachment != null){
            s += "其他数据:" + attachment  ;
        }
        return s ;
    }
    /**
     * 对象转成json
     * @return json
     * @throws Exception 异常
     */
    @SuppressWarnings("unused")
    public String toJson()throws Exception{
        try{
            String json = JSON.toJSONString(this) ;
            return json ;
        }catch(Exception e){
            throw new Exception(e.getMessage() , e ) ;
        }
    }
    /**
     * json转成对象
     * @param json json
     * @return Command
     * @throws Exception 异常
     */
    @SuppressWarnings("unused")
    public static Command jsonToObject(String json)throws Exception{
        try{
            JSONObject jsonObject = JSON.parseObject(json);
            Command command = jsonObject.to(Command.class) ;
            //command.param = jsonObject.getObject("param", class);
            return command ;
        }catch(Exception e){
            throw new Exception(e.getMessage() , e ) ;
        }
    }
    
    /**
     * 创建返回的出错命令
     * @param error 错误消息
     * @param commandId 命令ID
     * @return Command
     */
    @SuppressWarnings("unused")
    public Command createReturnErrorCommand(String error , String commandId, String code){
        if(commandId == null){
            this.setId(defaultId) ;
        }else{
            this.setId(commandId) ;
        }
        this.code = code ;
        this.type = CommandType.resultCommand ;
        
        CommandBackParam p = new CommandBackParam() ;
        p.setSuccess(false) ;
        p.setMessage(error) ;
        
        this.param = p ;
        
        return this ;
    }
 
    /**
     * 创建返回的简单成功命令,针对监控中间件的命令
     * @param message 消息
     * @param commandId 命令ID
     * @param code 功能码
     * @return Command
     */
    @SuppressWarnings("unused")
    public Command createReturnSuccessCommand(String message , String commandId, String code){
        if(commandId == null){
            this.setId(defaultId) ;
        }else{
            this.setId(commandId) ;
        }
        this.code = code ;
        this.type = CommandType.resultCommand ;
 
        CommandBackParam p = new CommandBackParam() ;
        p.setSuccess(true) ;
        p.setMessage(message) ;
        
        this.param = p ;
        
        return this ;
    }
 
    public String getId() {
        return id;
    }
    public Command setId(String id) {
        this.id = id;
        return this ;
    }
    public String getRtuAddr() {
        return rtuAddr;
    }
    public Command setRtuAddr(String rtuAddr) {
        this.rtuAddr = rtuAddr;
        return this ;
    }
 
    public String getRtuResultSendWebUrl() {
        return rtuResultSendWebUrl;
    }
 
    public void setRtuResultSendWebUrl(String rtuResultSendWebUrl) {
        this.rtuResultSendWebUrl = rtuResultSendWebUrl;
    }
 
    public String getProtocol() {
        return protocol;
    }
    public void setProtocol(String protocol) {
        this.protocol = protocol;
    }
    public String getType() {
        return type;
    }
    public Command setType(String type) {
        this.type = type;
        return this ;
    }
    public String getCode() {
        return code;
    }
    public Command setCode(String code) {
        this.code = code;
        return this ;
    }
    @SuppressWarnings("unused")
    public Boolean getNoRtMwDealRes() {
        return noRtMwDealRes;
    }
    @SuppressWarnings("unused")
    public void setNoRtMwDealRes(Boolean noRtMwDealRes) {
        this.noRtMwDealRes = noRtMwDealRes;
    }
    public Object getParam() {
        return param;
    }
    public Command setParam(Object param) {
        this.param = param;
        return this ;
    }
    public Object getAttachment() {
        return attachment;
    }
    @SuppressWarnings("unused")
    public Command setAttachment(Object attachment) {
        this.attachment = attachment;
        return this ;
    }
    
}