|  |  |  | 
|---|
|  |  |  | 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.Callback; | 
|---|
|  |  |  | 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 com.dy.pipIrrRemote.common.dto.DtoBase; | 
|---|
|  |  |  | import com.dy.pipIrrRemote.monitor.common.ComCtrl; | 
|---|
|  |  |  | 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.context.annotation.Scope; | 
|---|
|  |  |  | 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 | 
|---|
|  |  |  | 
|---|
|  |  |  | @RestController | 
|---|
|  |  |  | @RequestMapping(path = "comTrans") | 
|---|
|  |  |  | @RequiredArgsConstructor | 
|---|
|  |  |  | public class ComTransCtrl { | 
|---|
|  |  |  | @Scope("prototype") //因为有对象类属性,所以采用原型模式,每次请求新建一个实例对象 | 
|---|
|  |  |  | public class ComTransCtrl extends ComCtrl { | 
|---|
|  |  |  |  | 
|---|
|  |  |  | 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; | 
|---|
|  |  |  | private ComTransSv sv ; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | * 向设备(控制器)发送透传命令 | 
|---|
|  |  |  | 
|---|
|  |  |  | @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("等待通信中间件通知命令结果超时"); | 
|---|
|  |  |  | BaseResponse<Object> res ; | 
|---|
|  |  |  | //发送命令前-1:验证 | 
|---|
|  |  |  | res = super.pre1(sv, dto.comCode, dto, bindingResult); | 
|---|
|  |  |  | if(res == null) { | 
|---|
|  |  |  | //发送命令前-2:获得数据 | 
|---|
|  |  |  | res = super.pre2(sv, dto.comCode, dto, bindingResult); | 
|---|
|  |  |  | if(res == null) { | 
|---|
|  |  |  | //发送命令前-3:保存命令日志 | 
|---|
|  |  |  | this.comName = "透传(" + comName + ")" ; | 
|---|
|  |  |  | String comData = dto.comData.toUpperCase() ; | 
|---|
|  |  |  | ComTransParam comParam = ComTransParam.builder().commandCode(dto.comCode).data(comData).build(); | 
|---|
|  |  |  | res = super.pre3(sv, dto.getIntakeId(), dto.getOperator(), dto.comCode, comParam); | 
|---|
|  |  |  | if(res == null) { | 
|---|
|  |  |  | //发送命令前-4:准备Feature | 
|---|
|  |  |  | super.pre4(); | 
|---|
|  |  |  | try{ | 
|---|
|  |  |  | //创建外部透传命令(发给控制器) | 
|---|
|  |  |  | Command com = sv.createOuterTransparentCommand("" + comId, dto.comCode); | 
|---|
|  |  |  | com.rtuAddr = ctrlPo.getRtuAddr() ; | 
|---|
|  |  |  | com.attachment = comData ; | 
|---|
|  |  |  | com.rtuResultSendWebUrl = rtuResultSendWebUrl; | 
|---|
|  |  |  | //发送命令 | 
|---|
|  |  |  | res = super.doSend(sv, com) ; | 
|---|
|  |  |  | if(res == null) { | 
|---|
|  |  |  | //发送命令后 | 
|---|
|  |  |  | res = super.after(dto.comCode, null); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | }catch (Exception e){ | 
|---|
|  |  |  | res = BaseResponseUtils.buildFail("服务端构造并向通信中间件发送请求时异常" + (e.getMessage() == null?"":e.getMessage())) ; | 
|---|
|  |  |  | }finally { | 
|---|
|  |  |  | //最终 | 
|---|
|  |  |  | super.end() ; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | }catch (Exception e){ | 
|---|
|  |  |  | return BaseResponseUtils.buildFail("服务端构造并向通信中间件发送请求时异常" + (e.getMessage() == null?"":e.getMessage())) ; | 
|---|
|  |  |  | }finally { | 
|---|
|  |  |  | try { | 
|---|
|  |  |  | //最后清除CompletableFuture缓存 | 
|---|
|  |  |  | ComResultWait.remove(comId); | 
|---|
|  |  |  | }catch (Exception ee){} | 
|---|
|  |  |  | } | 
|---|
|  |  |  | return res ; | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | 
|---|
|  |  |  | * @param dto | 
|---|
|  |  |  | * @return | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | private String checkDto(ComTransDto dto){ | 
|---|
|  |  |  | if(!NumUtil.isHex(dto.comCode)){ | 
|---|
|  |  |  | @Override | 
|---|
|  |  |  | protected String checkDto(DtoBase dto){ | 
|---|
|  |  |  | ComTransDto myDto = (ComTransDto)dto; | 
|---|
|  |  |  | if(!NumUtil.isHex(myDto.comCode)){ | 
|---|
|  |  |  | return "命令功能码不是十六进制数"; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | if(!NumUtil.isHex(dto.comData)){ | 
|---|
|  |  |  | if(!NumUtil.isHex(myDto.comData)){ | 
|---|
|  |  |  | return "命令数据不是十六进制数"; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | if(dto.comData.length() % 2 != 0){ | 
|---|
|  |  |  | if(myDto.comData.length() % 2 != 0){ | 
|---|
|  |  |  | return "命令数据不完备(长度不是偶数)"; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | if(!dto.comData.contains(dto.comCode)){ | 
|---|
|  |  |  | if(!myDto.comData.contains(myDto.comCode)){ | 
|---|
|  |  |  | return "命令数据中不包含功能码"; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | return null ; | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | private String createRtnMsg(String code, JSONObject resultData){ | 
|---|
|  |  |  | @Override | 
|---|
|  |  |  | protected String dealComResult(String code, JSONObject resultData, Callback callback){ | 
|---|
|  |  |  | String msg; | 
|---|
|  |  |  | if(resultData != null){ | 
|---|
|  |  |  | JSONObject codeData = resultData.getJSONObject("data") ; | 
|---|