From c4d56f4a2c7687739f3bf0f43bcc1f35c9bbeb8b Mon Sep 17 00:00:00 2001
From: liurunyu <lry9898@163.com>
Date: 星期三, 14 五月 2025 15:18:44 +0800
Subject: [PATCH] 1、表阀一体协议解析同步新版本;2、默认设备是表阀一体(0x02)
---
pipIrr-platform/pipIrr-web/pipIrr-web-remote/src/main/java/com/dy/pipIrrRemote/monitor/ComTransCtrl.java | 251 ++++++++++++++++++++++++++++++++------------------
1 files changed, 160 insertions(+), 91 deletions(-)
diff --git a/pipIrr-platform/pipIrr-web/pipIrr-web-remote/src/main/java/com/dy/pipIrrRemote/monitor/ComTransCtrl.java b/pipIrr-platform/pipIrr-web/pipIrr-web-remote/src/main/java/com/dy/pipIrrRemote/monitor/ComTransCtrl.java
index 2c87d06..1ee1493 100644
--- a/pipIrr-platform/pipIrr-web/pipIrr-web-remote/src/main/java/com/dy/pipIrrRemote/monitor/ComTransCtrl.java
+++ b/pipIrr-platform/pipIrr-web/pipIrr-web-remote/src/main/java/com/dy/pipIrrRemote/monitor/ComTransCtrl.java
@@ -3,58 +3,42 @@
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
* @Date: 2025/4/30 16:08
* @Description
*/
-
@Slf4j
@Tag(name = "杩滅▼閫忎紶鍛戒护", description = "杩滅▼閫忎紶鍛戒护")
@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 ;
/**
* 鍚戣澶囷紙鎺у埗鍣級鍙戦�侀�忎紶鍛戒护
@@ -65,71 +49,43 @@
@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) ;
- //鍚戦�氫俊涓棿浠跺彂閫亀eb璇锋眰
- 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("鎺у埗鍣ㄦ墽琛屽懡浠ゆ垚鍔�");
+ 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锛氬噯澶嘑eature
+ 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("绛夊緟閫氫俊涓棿浠堕�氱煡鍛戒护缁撴灉瓒呮椂鎴栧紓甯�");
}
}
- }catch (Exception e){
- return BaseResponseUtils.buildFail("鏈嶅姟绔瀯閫犲苟鍚戦�氫俊涓棿浠跺彂閫佽姹傛椂寮傚父" + (e.getMessage() == null?"":e.getMessage())) ;
- }finally {
- try {
- //鏈�鍚庢竻闄ompletableFuture缂撳瓨
- ComResultWait.remove(comId);
- }catch (Exception ee){}
}
+ return res ;
}
/**
@@ -137,20 +93,133 @@
* @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 ;
}
+ @Override
+ protected String dealComResult(String code, JSONObject resultData, Callback callback){
+ 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 "璁剧疆鎴愬姛锛孖P鍦板潃锛�" + ip + "锛岀鍙e彿锛�" + 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 "鏌ヨ鎴愬姛锛孖P鍦板潃锛�" + ip + "锛岀鍙e彿锛�" + 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鏄痭ull
+ 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 ? "鎺у埗鍣ㄥ凡鎵ц閬ユ帶鍏抽榾鍛戒护" : "鎺у埗鍣ㄦ嫆缁濇墽琛岄仴鎺у叧闃�鍛戒护";
+ }
+ }
+
}
--
Gitblit v1.8.0