liurunyu
2023-11-18 c1ddfd71223c1a7d704b6f21b669fbfcb37adc82
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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
package com.dy.common.mw.protocol;
 
import java.io.Serializable;
 
import com.alibaba.fastjson2.JSON ;
 
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; 
    
    /**
     * 水表协议名称,这个可以为空,因为水表上行数据解析出协议名称,然后系统把协议名称保存在水表数据记录中,从此协议名称就不再空了
     */
    public String protocol;
    
    /**
     * RTU 地址
     */
    public String rtuAddr ;
    
    /**
     * 水表号
     * 应用电信平台时有效
     */
    public String meterNo ;
    
    /**
     * 电信平台上的产品ID,当不在电信平台上运行时,为null
     * 应用电信平台时有效
     */
    public Integer productId ;
    
    /**
     * 电信平台上的设备ID,当不在电信平台上运行时,为null
     * 应用电信平台时有效
     */
    public String deviceId ;
    
    /**
     * 命令类型:Rtu命令、针对监控中间件的命令
     * 由com.dy.common.mw.protocol.CommandType类定义
     */
    public String type ;
    
    /**
     * 功能码
     */
    public String code ; 
    
    /**
     * 不返回中间件对命令处理结果(true:不返回处理结果,false或null:返回处理结果)
     */
    public Boolean noRtMwDealRes ;
 
    /**
     * 具体参数数据
     */
    public Object param ;
    
    /**
     * 其他数据
     */
    public Object attachment ;
    
    public String toString(){
        String s = "命令id=" + id + "\n" ;
        s += (protocol == null ? "" : ("协议=" + protocol + "\n"));
        s += (rtuAddr == null ? "" : ("IMEI号=" + rtuAddr + "\n"));
        s += (meterNo == null ? "" : ("水表号=" + meterNo + "\n"));
        if(productId != null){
            s +=  "电信平台产品ID=" + productId + "\n" ;
        }
        if(deviceId != null && !deviceId.trim().equals("")){
            s +=  "电信平台设备ID=" + deviceId + "\n" ;
        }
        s += "命令类型=" + (type.equals(CommandType.innerCommand)?"内部命令":"终端命令") + "\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{
            return JSON.toJSONString(this) ;
        }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{
            return JSON.parseObject(json, Command.class) ;
        }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 static void main(String[] args){
        String json = "{\"attachment\":null,\"code\":\"03\",\"deviceId\":\"d8c9601f214747d98d47a4736e5\",\"id\":\"999999999\",\"meterNo\":\"23040600377\",\"productId\":16873252,\"param\":\"0002\",\"protocol\":\"HAC_NBhV2_5\",\"rtuAddr\":\"863318060168996\",\"type\":\"outerCommand\"}" ;
        try {
            Command com = jsonToObject(json) ;
            System.out.println(com.param);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
     */
 
    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 getProtocol() {
        return protocol;
    }
    public void setProtocol(String protocol) {
        this.protocol = protocol;
    }
    public String getMeterNo() {
        return meterNo;
    }
    public void setMeterNo(String meterNo) {
        this.meterNo = meterNo;
    }
    public Integer getProductId() {
        return productId;
    }
    public void setProductId(Integer productId) {
        this.productId = productId;
    }
    public String getDeviceId() {
        return deviceId;
    }
    public void setDeviceId(String deviceId) {
        this.deviceId = deviceId;
    }
    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 ;
    }
    
}