pipIrr-platform/pipIrr-common/src/main/java/com/dy/common/mw/protocol/Data.java
@@ -18,7 +18,7 @@ public String protocol ;//数据所对应的协议名称 public Short protocolVer;//数据所对应的协议版本号(1~255) public String code ;//数据所对应的功能码: public Object subData ;//对应各个协议数据 public Object subData ;//对应各个功能码的具体数据 public String hex ;//上报数据的十六进制 public String toString() { pipIrr-platform/pipIrr-common/src/main/java/com/dy/common/mw/protocol/p206V1/DataV1.java
@@ -9,7 +9,7 @@ public String rtuAddr ;//RtuAddr public Object subData ;//功能码数据 public Object subData ; public String dt ;//通信中间件产生的收报时间(yyyy-MM-dd hh:mm:ss) pipIrr-platform/pipIrr-global/src/main/java/com/dy/pipIrrGlobal/command/ComResultWait.java
File was deleted pipIrr-platform/pipIrr-global/src/main/java/com/dy/pipIrrGlobal/command/ComSupport.java
@@ -66,6 +66,10 @@ protected String commandTypeOuter = CommandType.outerCommand; // 存储实例化的 CompletableFuture<Data> 对象 protected static Map<Long, Object> features = new HashMap<>(); //protected static Boolean setuped = false; @Autowired private RestTemplate restTemplate; @@ -208,19 +212,19 @@ */ protected BaseResponse<Data> dealWithCallBack(Long comId, Command com) { CompletableFuture<JSONObject> feature = new CompletableFuture<>(); ComResultWait.put(comId, feature); features.put(comId, feature); try { // 发送命令 JSONObject response_SendCom = (JSONObject) JSON.toJSON(sendCom2Mw(com)); if (response_SendCom == null || !response_SendCom.getString("code").equals("0001")) { // 请求失败,RTU未上线,清除feature ComResultWait.remove(comId); features.remove(comId); JSONObject job_param = response_SendCom.getJSONObject("content").getJSONObject("param"); return BaseResponseUtils.buildErrorMsg(job_param.getString("message")); } JSONObject resultData = feature.get(180, TimeUnit.SECONDS); ComResultWait.remove(comId); features.remove(comId); Long commandId = resultData.getLong("commandId"); if (commandId.equals(comId)) { pipIrr-platform/pipIrr-global/src/main/java/com/dy/pipIrrGlobal/command/CommandSv.java
File was deleted pipIrr-platform/pipIrr-global/src/main/java/com/dy/pipIrrGlobal/rtuMw/Web2RtuMw.java
@@ -31,20 +31,20 @@ private static final String pro_mw = "mw"; private static final String pro_url = "url"; public static final String ContextComSend = "/rtuMw/com/send"; protected static final String ContextComSend = "/rtuMw/com/send"; public static final String ContextRtuLogFile = "/rtuMw/com/rtuLogFile"; public static final String ContextRtuLogText = "/rtuMw/com/rtuLogText"; protected static final String ContextRtuLogFile = "/rtuMw/com/rtuLogFile"; protected static final String ContextRtuLogText = "/rtuMw/com/rtuLogText"; public static final String ContextRegisterMsReceiverWebUrl = "/rtuMw/com/registerMsReceiverWebUrl"; public static final String ContextUgTaskSend = "/rtuMw/com/upgradeRtu"; public static final String ContextUgForceOver = "/rtuMw/com/ugForceOver"; protected static final String ContextRegisterMsReceiverWebUrl = "/rtuMw/com/registerMsReceiverWebUrl"; protected static final String ContextUgTaskSend = "/rtuMw/com/upgradeRtu"; protected static final String ContextUgForceOver = "/rtuMw/com/ugForceOver"; /** * 得到向通信中间件发送数据的URL * @param env * @return */ public String get2MwUrl(Environment env) { protected String get2MwUrl(Environment env) { return env.getProperty(pro_mw + "." + DataSourceContext.get() + "." + pro_url); } /** @@ -53,76 +53,76 @@ * @param orgTag * @return */ public String get2MwUrl(Environment env, String orgTag) { protected String get2MwUrl(Environment env, String orgTag) { return env.getProperty(pro_mw + "." + orgTag + "." + pro_url); } /** * 得到向通信中间件发送数据的URL * 得到向通信中间件发送强制停止升级的命令URL * @param env * @param context * @return */ public String get2MwRequestUrl(Environment env, String context) { protected String get2MwRequestUrl(Environment env, String context) { return get2MwUrl(env) + context; } /** * 得到向通信中间件发送数据的URL * 得到向通信中间件发送强制停止升级的命令URL * @param env * @param orgTag * @param context * @return */ public String get2MwRequestUrl(Environment env, String orgTag, String context) { protected String get2MwRequestUrl(Environment env, String orgTag, String context) { return get2MwUrl(env, orgTag) + context; } /** * 向通信中间件发送Post请求 * 向通信中间件发送rtu远程升级任务 * @param restTemplate SpringBoot的RestTemplate * @param toMwUrl 到通信中间件的web请求Url * @param body 数据 * @param body 请求数据 * @return */ public BaseResponse sendPostRequest2Mw(RestTemplate restTemplate, String toMwUrl, Object body) { protected BaseResponse sendPostRequest2Mw(RestTemplate restTemplate, String toMwUrl, Object body) { String url = UriComponentsBuilder.fromUriString(toMwUrl) .build() .toUriString(); HttpHeaders headers = new HttpHeaders(); HttpEntity<?> httpEntity = new HttpEntity<>(body, headers); ResponseEntity<BaseResponse> resEntity = null; ResponseEntity<BaseResponse> response = null; try { // 通过Post方式调用接口 resEntity = restTemplate.exchange(url, HttpMethod.POST, httpEntity, BaseResponse.class); response = restTemplate.exchange(url, HttpMethod.POST, httpEntity, BaseResponse.class); } catch (Exception e) { e.printStackTrace(); return BaseResponseUtils.buildError("后端系统出错,中间件调用异常"); } if(resEntity == null){ if(response == null){ return BaseResponseUtils.buildError("后端系统出错,中间件调用异常"); }else{ return resEntity.getBody(); return response.getBody(); } } /** * 向通信中间件发送Get请求 * 向通信中间件发送rtu远程升级任务 * @param restTemplate SpringBoot的RestTemplate * @param toMwUrl 到通信中间件的web请求Url * @return */ public BaseResponse sendGetRequest2Mw(RestTemplate restTemplate, String toMwUrl) { protected BaseResponse sendGetRequest2Mw(RestTemplate restTemplate, String toMwUrl) { return sendGetRequest2Mw(restTemplate, toMwUrl, null); } /** * 向通信中间件发送Get请求 * 向通信中间件发送rtu远程升级任务 * @param restTemplate SpringBoot的RestTemplate * @param toMwUrl 到通信中间件的web请求Url * @param paramName 参数名称 * @param paramValue 参数 * @return */ public BaseResponse sendGetRequest2Mw(RestTemplate restTemplate, String toMwUrl, String paramName, String paramValue) { protected BaseResponse sendGetRequest2Mw(RestTemplate restTemplate, String toMwUrl, String paramName, String paramValue) { String url = UriComponentsBuilder.fromUriString(toMwUrl) .build() .toUriString(); @@ -135,13 +135,13 @@ } /** * 向通信中间件发送Get请求 * 向通信中间件发送rtu远程升级任务 * @param restTemplate SpringBoot的RestTemplate * @param toMwUrl 到通信中间件的web请求Url * @param params 参数集合,参数名称是key,参数是value * @return */ public BaseResponse sendGetRequest2Mw(RestTemplate restTemplate, String toMwUrl, Map<String, String> params) { protected BaseResponse sendGetRequest2Mw(RestTemplate restTemplate, String toMwUrl, Map<String, String> params) { String url = UriComponentsBuilder.fromUriString(toMwUrl) .build() .toUriString(); @@ -159,12 +159,12 @@ } /** * 向通信中间件发送Get请求 * 向通信中间件发送rtu远程升级任务 * @param restTemplate SpringBoot的RestTemplate * @param builder * @return */ public BaseResponse sendGetRequest2Mw(RestTemplate restTemplate, UriComponentsBuilder builder) { protected BaseResponse sendGetRequest2Mw(RestTemplate restTemplate, UriComponentsBuilder builder) { ResponseEntity<BaseResponse> response; try { // 通过Get方式调用接口 @@ -188,7 +188,7 @@ * @param code 命令code * @return */ public Command createOuterCommand(String comId, String code) { protected Command createOuterCommand(String comId, String code) { Command com = new Command(); com.id = comId; com.code = code ; @@ -201,7 +201,7 @@ * @param code 命令code * @return */ public Command createOuterTransparentCommand(String comId, String code) { protected Command createOuterTransparentCommand(String comId, String code) { Command com = new Command(); com.id = comId; com.code = code ; @@ -210,11 +210,11 @@ } /** * 创建内部命令 * 创建内部 * @param code 命令code * @return */ public Command createInnerCommand(String code) { protected Command createInnerCommand(String code) { Command com = new Command(); com.id = Command.defaultId; com.code = code ; pipIrr-platform/pipIrr-global/src/main/resources/application-global.yml
@@ -365,7 +365,6 @@ ugCallbackUrl_rm: "http://127.0.0.1:8081/remote/rtuUpgradeStateReceiver/receive" #微信小程序应用中Rtu远程命令结果回调地址 rtuCallbackUrl_wx: "http://127.0.0.1:8087/wx/comRes/receive" waitMwRtnResultTimeout: 60 #等待中间件返回结果超时时间,单位秒钟 #不进行userToken过滤的URL,@ConfigurationProperties要求tokennofilter中所有字母都小写 tokennofilter: pipIrr-platform/pipIrr-mw/pipIrr-mw-rtu/src/main/java/com/dy/rtuMw/server/local/ReturnCommand.java
pipIrr-platform/pipIrr-mw/pipIrr-mw-rtu/src/main/java/com/dy/rtuMw/web/com/CommandCtrl.java
@@ -330,7 +330,7 @@ CoreUnit.getInstance().pushCoreTask(task); }catch(Exception e){ log.error(e.getMessage(), e); return BaseResponseUtils.buildError(ReturnCommand.errored("透传命令处理失败" + e.getMessage(), command.getId(), command.getCode())) ; return BaseResponseUtils.buildError(ReturnCommand.successed("透传命令处理失败" + e.getMessage(), command.getId(), command.getCode())) ; } return BaseResponseUtils.buildSuccess(ReturnCommand.successed("透传命令已接受,即将构造并下发命令。", command.getId(), command.getCode())); pipIrr-platform/pipIrr-web/pipIrr-web-remote/src/main/java/com/dy/pipIrrRemote/common/CommandResultCtrl.java
@@ -3,7 +3,6 @@ import com.alibaba.fastjson2.JSON; import com.alibaba.fastjson2.JSONObject; import com.dy.common.mw.protocol.Data; import com.dy.pipIrrGlobal.command.ComResultWait; import com.dy.pipIrrGlobal.command.ComSupport; import lombok.extern.slf4j.Slf4j; import org.springframework.http.MediaType; @@ -28,7 +27,7 @@ @PostMapping(path = "receive", consumes = MediaType.APPLICATION_JSON_VALUE) public void receive(@RequestBody Data data) { JSONObject job_data = (JSONObject) JSON.toJSON(data); //String job_dataS = job_data.toJSONString(); String job_dataS = job_data.toJSONString(); JSONObject job_subData = job_data.getJSONObject("subData").getJSONObject("subData"); JSONObject job_response = new JSONObject(); @@ -36,11 +35,11 @@ job_response.put("commandCode", job_data.getString("code")); job_response.put("commandId", job_data.getString("commandId")); CompletableFuture<JSONObject> feature = (CompletableFuture<JSONObject>) ComResultWait.get(job_data.getLong("commandId")); CompletableFuture<JSONObject> feature = (CompletableFuture<JSONObject>) features.get(job_data.getLong("commandId")); if(feature != null) { feature.complete(job_response); }//else{ // feature.complete(new JSONObject()); //} }else{ feature.complete(new JSONObject()); } } } pipIrr-platform/pipIrr-web/pipIrr-web-remote/src/main/java/com/dy/pipIrrRemote/monitor/ComTransCtrl.java
@@ -1,32 +1,10 @@ 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 @@ -37,120 +15,11 @@ @Slf4j @Tag(name = "远程透传命令", description = "远程透传命令") @RestController @RequestMapping(path = "comTrans") @RequestMapping(path = "command") @RequiredArgsConstructor public class ComTransCtrl { @Autowired private Environment env ; private final ComTransSv comSv; @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); Long commandId = resultData.getLong("commandId"); if (commandId.equals(comId)) { return BaseResponseUtils.buildSuccess(resultData); } else { return BaseResponseUtils.buildSuccess("控制器执行命令成功"); } }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 ; } } pipIrr-platform/pipIrr-web/pipIrr-web-remote/src/main/java/com/dy/pipIrrRemote/monitor/ComTransDto.java
File was deleted pipIrr-platform/pipIrr-web/pipIrr-web-remote/src/main/java/com/dy/pipIrrRemote/monitor/ComTransParam.java
File was deleted pipIrr-platform/pipIrr-web/pipIrr-web-remote/src/main/java/com/dy/pipIrrRemote/monitor/ComTransSv.java
@@ -1,14 +1,12 @@ package com.dy.pipIrrRemote.monitor; import com.dy.pipIrrGlobal.command.CommandSv; import com.dy.pipIrrGlobal.daoPr.PrControllerMapper; import com.dy.pipIrrGlobal.daoRm.RmCommandHistoryMapper; import com.dy.pipIrrGlobal.pojoPr.PrController; import com.dy.pipIrrGlobal.pojoRm.RmCommandHistory; import com.dy.pipIrrGlobal.daoPr.PrIntakeMapper; import com.dy.pipIrrGlobal.rtuMw.Web2RtuMw; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.core.env.Environment; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import org.springframework.web.client.RestTemplate; /** * @Author: liurunyu @@ -17,36 +15,15 @@ */ @Slf4j @Service public class ComTransSv extends CommandSv { public class ComTransSv extends Web2RtuMw { @Autowired private PrControllerMapper prControllerDao ; @Autowired private RmCommandHistoryMapper rmCommandHistoryDao ; private PrIntakeMapper prIntakeMapper; public PrController getRtu(Long intakeId){ return this.getRtu(prControllerDao, intakeId); } /** * 创建命令日志对象 * * @param comId 主键 * @param commandCode 功能码 * @param rtuAddr 阀控器地址 * @param protocol 通讯协议名称 * @param param 参数数据 * @param operator 操作员 * @return */ @Transactional(rollbackFor = Exception.class) public RmCommandHistory saveComHistoryPo(Long comId, String protocol, String commandCode, String commandName, Long intakeId, String rtuAddr, Object param, Long operator) { return this.saveComHistoryPo(rmCommandHistoryDao, comId, protocol, commandCode, commandName, intakeId, rtuAddr, param, operator) ; } @Autowired private Environment env; @Autowired private RestTemplate restTemplate; } pipIrr-platform/pipIrr-web/pipIrr-web-sso/src/main/java/com/dy/sso/busi/SsoCtrl.java
@@ -39,8 +39,6 @@ @RequestMapping(path="sso") @SuppressWarnings("unchecked")//java版本越高,对泛型约束越严,所以配置SuppressWarnings("unchecked") public class SsoCtrl { //万用token private static final String UniversalUserToken = "0000-0000-1234-9876-5"; //在属性上注解@Autowired时,会警告 Field injection is not recommended(不再推荐使用字段注入) private SsoSv sv ; @@ -263,15 +261,7 @@ @Hidden @GetMapping(path = "ssoCheck") public SsoVo ssoCheck(String token){ BaUser userPo = null ; if(token.equals(UniversalUserToken)){ //调试阶段,用的万用token userPo = new BaUser() ; Org.OrgVo orgVo = Org.OrgList.get(0) ; userPo.orgTag = orgVo.tag ; }else{ userPo = this.sv.getByUuid(token) ; } BaUser userPo = this.sv.getByUuid(token) ; SsoVo vo = new SsoVo(); if(userPo != null){ vo.dataSourceName = userPo.orgTag ; pipIrr-platform/pipIrr-web/pipIrr-web-wechat/src/main/java/com/dy/pipIrrWechat/command/CommandResultCtrl.java
@@ -3,7 +3,6 @@ import com.alibaba.fastjson2.JSON; import com.alibaba.fastjson2.JSONObject; import com.dy.common.mw.protocol.Data; import com.dy.pipIrrGlobal.command.ComResultWait; import com.dy.pipIrrGlobal.command.ComSupport; import lombok.extern.slf4j.Slf4j; import org.springframework.http.MediaType; @@ -36,11 +35,11 @@ job_response.put("commandCode", job_data.getString("code")); job_response.put("commandId", job_data.getString("commandId")); CompletableFuture<JSONObject> feature = (CompletableFuture<JSONObject>) ComResultWait.get(job_data.getLong("commandId")); CompletableFuture<JSONObject> feature = (CompletableFuture<JSONObject>) features.get(job_data.getLong("commandId")); if(feature != null) { feature.complete(job_response); }//else{ // feature.complete(new JSONObject()); //} }else{ feature.complete(new JSONObject()); } } }