package com.dy.pipIrrRemote.monitor.common; import com.alibaba.fastjson2.JSONObject; import com.dy.common.mw.protocol.Command; import com.dy.common.util.Callback; import com.dy.common.util.IDLongGenerator; 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.pipIrrGlobal.pojoRm.RmCommandHistory; import com.dy.pipIrrRemote.common.dto.DtoBase; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.core.env.Environment; import org.springframework.validation.BindingResult; import org.springframework.web.client.RestTemplate; import java.util.Objects; import java.util.concurrent.CompletableFuture; import java.util.concurrent.TimeUnit; /** * @Author: liurunyu * @Date: 2025/5/9 14:53 * @Description */ public abstract class ComCtrl { @Autowired protected Environment env ; @Autowired protected RestTemplate restTemplate ; @Value("${mw.waitMwRtnResultTimeout}") protected int waitMwRtnResultTimeout ; @Value("${mw.rtuCallbackUrl_rm}") protected String rtuResultSendWebUrl; @Value("${project.projectNo}") protected Integer projectNo; @Value("${project.controllerType}") protected String controllerType; //控制器对象 protected PrController ctrlPo ; //异步等待器 protected CompletableFuture feature; //命令名称 protected String comName ; //命令日志id protected Long comId ; /** * 发送命令前-1:验证 * @param comSv * @param comCode * @param dto * @param bindingResult * @return */ public BaseResponse pre1(ComSv comSv, String comCode, DtoBase 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) ; } return null ; } /** * 发送命令前-2:获得数据 * @param comSv * @param comCode * @param dto * @param bindingResult * @return */ public BaseResponse pre2(ComSv comSv, String comCode, DtoBase dto, BindingResult bindingResult) { //得到控制器对象 ctrlPo = comSv.getRtu(dto.getIntakeId()); if (ctrlPo == null) { return BaseResponseUtils.buildError("服务端出错,从数据库中未得到控制器数据") ; } //检查协议 String msg = comSv.checkProtocol(ctrlPo) ; if(msg != null) { return BaseResponseUtils.buildError("服务端出错," + msg) ; } //得到功能码对应的命令名称 comName = comSv.getCommandName(comCode, ctrlPo) ; if(comName == null) { return BaseResponseUtils.buildError("服务端出错,未得到功能码对应命令名称") ; } return null ; } /** * 发送命令前-3:保存命令日志 * @param comSv sv对象 * @param intakeId 取水口ID * @param operator 当前用登录用户id(操作人) * @param comCode 功能码 * @param param 命令参数 * @return */ public BaseResponse pre3(ComSv comSv, Long intakeId, Long operator, String comCode, CdParameter param) { comId = new IDLongGenerator().generate(); //生成并保存命令日志 RmCommandHistory po = comSv.saveComHistoryPo(comId, ctrlPo.getProtocol(), comCode, comName, intakeId, ctrlPo.getRtuAddr(), param, operator); if(po == null){ return BaseResponseUtils.buildError("服务端出错,未能生成并保存命令日志") ; } return null ; } /** * 发送命令前-4:准备Feature * @return */ public void pre4() { feature = new CompletableFuture<>(); ComResultWait.put(comId, feature); } /** * 发送命令 * @param comSv * @param com * @return */ public BaseResponse doSend(ComSv comSv, Command com){ //得到通信中间件发送命令的web URL String rqUrl = comSv.get2MwRequestUrl(env, comSv.ContextComSend) ; //向通信中间件发送web请求 BaseResponse res = comSv.sendPostRequest2Mw(restTemplate, rqUrl, com) ; //处理通信中间件对web请求的响应 String msg = comSv.dealMwDealResponse(res) ; if(msg != null) { return BaseResponseUtils.buildError(msg) ; }else{ return null ; } } /** * 发送命令后 * @return */ public BaseResponse after(String comCode, Callback callback) { try{ //等待通信中间件通知控制器执行命令上行数据(命令结果) JSONObject resultData = feature.get(waitMwRtnResultTimeout, TimeUnit.SECONDS); return BaseResponseUtils.buildSuccess(this.dealComResult(comCode, resultData, callback)); }catch (Exception e){ return BaseResponseUtils.buildFail("等待通信中间件通知命令结果超时"); } } /** * 发送命令最后 * @return */ public void end(){ try { //最后清除CompletableFuture缓存 if(ComResultWait.contain(comId)){ ComResultWait.remove(comId); } }catch (Exception ee){} } /** * 验证 * @param dto * @return */ protected abstract String checkDto(DtoBase dto) ; /** * 生成命令返回信息 */ protected abstract String dealComResult(String code, JSONObject resultData, Callback callback); }