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
package com.dy.pipIrrRemote.monitor;
 
import com.alibaba.fastjson2.JSONObject;
import com.dy.common.aop.SsoAop;
import com.dy.common.mw.protocol.Command;
import com.dy.common.util.IDLongGenerator;
import com.dy.common.util.NumUtil;
import com.dy.common.webUtil.BaseResponse;
import com.dy.common.webUtil.BaseResponseUtils;
import com.dy.pipIrrGlobal.command.ComResultWait;
import com.dy.pipIrrGlobal.pojoPr.PrController;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.env.Environment;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
 
import java.util.Objects;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.TimeUnit;
 
/**
 * @Author: liurunyu
 * @Date: 2025/4/30 16:08
 * @Description
 */
@Slf4j
@Tag(name = "远程透传命令", description = "远程透传命令")
@RestController
@RequestMapping(path = "comTrans")
@RequiredArgsConstructor
public class ComTransCtrl {
 
    private static final String RtuSuccessMsg = "控制器接收并执行命令成功";
 
    @Autowired
    private Environment env ;
 
    @Autowired
    private RestTemplate restTemplate ;
 
    @Value("${mw.waitMwRtnResultTimeout}")
    private int waitMwRtnResultTimeout ;
 
    @Value("${mw.rtuCallbackUrl_rm}")
    private String rtuResultSendWebUrl;
 
    @Autowired
    private ComTransSv comSv;
 
    /**
     * 向设备(控制器)发送透传命令
     * @param dto 前端发来的值对象
     * @param bindingResult 对dto验证的结果
     * @return 返回前端
     */
    @PostMapping(path = "send", consumes = MediaType.APPLICATION_JSON_VALUE)
    @SsoAop()
    public BaseResponse<Object> send(@RequestBody @Valid ComTransDto dto, BindingResult bindingResult) {
        if (bindingResult != null && bindingResult.hasErrors()) {
            return BaseResponseUtils.buildError(Objects.requireNonNull(bindingResult.getFieldError()).getDefaultMessage());
        }
        String msg = this.checkDto(dto) ;
        if(msg != null){
            return BaseResponseUtils.buildError(msg) ;
        }
        //得到控制器对象
        PrController ctrlPo = comSv.getRtu(dto.getIntakeId());
        if (ctrlPo == null) {
            return BaseResponseUtils.buildError("从数据库中未得到控制器数据") ;
        }
        //检查协议
        msg = comSv.checkProtocol(ctrlPo) ;
        if(msg != null) {
            return BaseResponseUtils.buildError(msg) ;
        }
        //得到功能码对应的命令名称
        String comName = comSv.getCommandName(dto.comCode, ctrlPo) ;
        if(comName == null) {
            return BaseResponseUtils.buildError("未得到功能码对应命令名称") ;
        }
        Long comId = new IDLongGenerator().generate();
        String comData = dto.comData.toUpperCase() ;
        //生成并保存命令日志
        comSv.saveComHistoryPo(comId, ctrlPo.getProtocol(), dto.comCode, "透传(" + comName + ")",
                dto.getIntakeId(), ctrlPo.getRtuAddr(), new ComTransParam(dto.comCode, comData), dto.getOperator());
        try{
            CompletableFuture<JSONObject> feature = new CompletableFuture<>();
            ComResultWait.put(comId, feature);
            //创建外部透传命令(发给控制器)
            Command com = comSv.createOuterTransparentCommand("" + comId, dto.comCode);
            com.rtuAddr = ctrlPo.getRtuAddr() ;
            com.attachment = comData ;
            com.rtuResultSendWebUrl = rtuResultSendWebUrl;
            //得到通信中间件发送命令的web URL
            String rqUrl = comSv.get2MwRequestUrl(this.env, comSv.ContextComSend) ;
            //向通信中间件发送web请求
            BaseResponse res = comSv.sendPostRequest2Mw(restTemplate, rqUrl, com) ;
            //处理通信中间件对web请求的响应
            msg = comSv.dealMwDealResponse(res) ;
            if(msg != null) {
                return BaseResponseUtils.buildError(msg) ;
            }else{
                try{
                    //等待通信中间件通知控制器执行命令上行数据(命令结果)
                    JSONObject resultData = feature.get(waitMwRtnResultTimeout, TimeUnit.SECONDS);
                    return BaseResponseUtils.buildSuccess(this.createRtnMsg(dto.comCode, resultData));
                }catch (Exception e){
                    return BaseResponseUtils.buildFail("等待通信中间件通知命令结果超时");
                }
            }
        }catch (Exception e){
            return BaseResponseUtils.buildFail("服务端构造并向通信中间件发送请求时异常" + (e.getMessage() == null?"":e.getMessage())) ;
        }finally {
            try {
                //最后清除CompletableFuture缓存
                ComResultWait.remove(comId);
            }catch (Exception ee){}
        }
    }
 
    /**
     * 验证
     * @param dto
     * @return
     */
    private String checkDto(ComTransDto dto){
        if(!NumUtil.isHex(dto.comCode)){
            return "命令功能码不是十六进制数";
        }
        if(!NumUtil.isHex(dto.comData)){
            return "命令数据不是十六进制数";
        }
        if(dto.comData.length() % 2 != 0){
            return "命令数据不完备(长度不是偶数)";
        }
        if(!dto.comData.contains(dto.comCode)){
            return "命令数据中不包含功能码";
        }
        return null ;
    }
 
    private String createRtnMsg(String code, JSONObject resultData){
        String msg;
        if(resultData != null){
            JSONObject codeData = resultData.getJSONObject("data") ;
            switch (code){
                case "3C": msg = createCd3CRtnMsg(codeData); break;
                case "10": msg = createCd10RtnMsg(codeData); break;
                case "21": msg = createCd21RtnMsg(codeData); break;
                case "37": msg = createCd37RtnMsg(codeData); break;
                case "50": msg = createCd50RtnMsg(codeData); break;
                case "65": msg = createCd65RtnMsg(codeData); break;
                case "66": msg = createCd66RtnMsg(codeData); break;
                case "67": msg = createCd67RtnMsg(codeData); break;
                case "91": msg = createCd91RtnMsg(codeData); break;
                case "92": msg = createCd92RtnMsg(codeData); break;
                case "93": msg = createCd93RtnMsg(codeData); break;
                default: msg = RtuSuccessMsg; break;
            }
        }else{
            msg = RtuSuccessMsg ;
        }
        return msg;
    }
    private String createCd3CRtnMsg(JSONObject codeData){
        if(codeData == null){
           return RtuSuccessMsg ;
        }else{
            Integer minute = codeData.getInteger("minute");
            return "设置成功,自报周期:" + minute + "分钟" ;
        }
    }
    private String createCd10RtnMsg(JSONObject codeData){
        if(codeData == null){
            return RtuSuccessMsg ;
        }else {
            String newRtuAddr = codeData.getString("newRtuAddr");
            return "设置成功,控制器地址:" + newRtuAddr;
        }
    }
    private String createCd21RtnMsg(JSONObject codeData){
        if(codeData == null){
            return RtuSuccessMsg ;
        }else {
            String ip = codeData.getString("ip");
            Integer port = codeData.getInteger("port");
            return "设置成功,IP地址:" + ip + ",端口号:" + port;
        }
    }
    private String createCd37RtnMsg(JSONObject codeData){
        if(codeData == null){
            return RtuSuccessMsg ;
        }else {
            Integer second = codeData.getInteger("second");
            return "设置成功,流量采集周期:" + second + "秒";
        }
    }
   private String createCd50RtnMsg(JSONObject codeData){
       if(codeData == null){
           return RtuSuccessMsg ;
       }else {
           String rtuAddr = codeData.getString("rtuAddr");
           return "查询成功,控制器地址:" + rtuAddr;
       }
    }
    private String createCd65RtnMsg(JSONObject codeData){
        if(codeData == null){
            return RtuSuccessMsg ;
        }else {
            Integer minute = codeData.getInteger("minute");
            return "查询成功,自报周期:" + minute + "分钟";
        }
    }
    private String createCd66RtnMsg(JSONObject codeData){
        if(codeData == null){
            return RtuSuccessMsg ;
        }else {
            String ip = codeData.getString("ip");
            Integer port = codeData.getInteger("port");
            return "查询成功,IP地址:" + ip + ",端口号:" + port;
        }
    }
    private String createCd67RtnMsg(JSONObject codeData){
        if(codeData == null){
            return RtuSuccessMsg ;
        }else {
            Integer second = codeData.getInteger("second");
            return "查询成功,流量采集周期:" + second + "秒";
        }
    }
    private String createCd91RtnMsg(JSONObject codeData){
        //此命令的codeData是null
        return "控制器清空历史记录成功" ;
    }
    private String createCd92RtnMsg(JSONObject codeData){
        if(codeData == null){
            return RtuSuccessMsg ;
        }else {
            Boolean success = codeData.getBoolean("success");
            return success ? "控制器已执行遥控开阀命令" : "控制器拒绝执行遥控开阀命令";
        }
    }
    private String createCd93RtnMsg(JSONObject codeData){
        if(codeData == null){
            return RtuSuccessMsg ;
        }else {
            Boolean success = codeData.getBoolean("success");
            return success ? "控制器已执行遥控关阀命令" : "控制器拒绝执行遥控关阀命令";
        }
    }
 
}