From 62afcbeaa5cc328bff01ae1facb18a6b8c03c5bc Mon Sep 17 00:00:00 2001
From: zhubaomin <zhubaomin>
Date: 星期二, 24 六月 2025 16:57:18 +0800
Subject: [PATCH] Merge branch 'master' of http://8.140.179.55:20000/r/pipIrr-SV
---
pipIrr-platform/pipIrr-web/pipIrr-web-remote/src/main/java/com/dy/pipIrrRemote/monitor/common/Com4MqttCtrl.java | 194 ++++++++++++++++++++++++++++++++++++++++++++++++
1 files changed, 194 insertions(+), 0 deletions(-)
diff --git a/pipIrr-platform/pipIrr-web/pipIrr-web-remote/src/main/java/com/dy/pipIrrRemote/monitor/common/Com4MqttCtrl.java b/pipIrr-platform/pipIrr-web/pipIrr-web-remote/src/main/java/com/dy/pipIrrRemote/monitor/common/Com4MqttCtrl.java
new file mode 100644
index 0000000..9bae744
--- /dev/null
+++ b/pipIrr-platform/pipIrr-web/pipIrr-web-remote/src/main/java/com/dy/pipIrrRemote/monitor/common/Com4MqttCtrl.java
@@ -0,0 +1,194 @@
+package com.dy.pipIrrRemote.monitor.common;
+
+import com.alibaba.fastjson2.JSONObject;
+import com.dy.common.mw.protocol.Command;
+import com.dy.common.mw.protocol4Mqtt.MqttSubMsg;
+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.PrStManure;
+import com.dy.pipIrrGlobal.pojoRm.RmCommandHistory;
+import com.dy.pipIrrRemote.common.dto.Dto4MqttBase;
+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 Com4MqttCtrl {
+
+ @Autowired
+ protected Environment env ;
+
+ @Autowired
+ protected RestTemplate restTemplate ;
+
+ @Value("${mw.waitMwRtnResultTimeout}")
+ protected int waitMwRtnResultTimeout ;
+
+ @Value("${mw.mqttCallbackUrl_rm}")
+ protected String mqttResultSendWebUrl;
+
+ //姘磋偉鏈哄璞�
+ protected PrStManure ctrlPo ;
+ //寮傛绛夊緟鍣�
+ protected CompletableFuture<MqttSubMsg> feature;
+ //鍛戒护鍚嶇О
+ protected String comName ;
+ //鍛戒护鏃ュ織id
+ protected Long comId ;
+
+ /**
+ * 鍙戦�佸懡浠ゅ墠-1锛氶獙璇�
+ * @param comSv
+ * @param comCode
+ * @param dto
+ * @param bindingResult
+ * @return
+ */
+ public BaseResponse<Object> pre1(Com4MqttSv comSv, String comCode, Dto4MqttBase 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 protocol
+ * @param protocolVer
+ * @param comCode
+ * @param dto
+ * @param bindingResult
+ * @return
+ */
+ public BaseResponse<Object> pre2(Com4MqttSv comSv, String protocol, Short protocolVer, String comCode, Dto4MqttBase dto, BindingResult bindingResult) {
+ //寰楀埌姘磋偉鏈哄璞�
+ ctrlPo = comSv.getManure(dto.getManureId());
+ if (ctrlPo == null) {
+ return BaseResponseUtils.buildError("鏈嶅姟绔嚭閿欙紝浠庢暟鎹簱涓湭寰楀埌姘磋偉鏈烘暟鎹�") ;
+ }
+ //妫�鏌ュ崗璁�
+ String msg = comSv.checkProtocol(ctrlPo) ;
+ if(msg != null) {
+ return BaseResponseUtils.buildError("鏈嶅姟绔嚭閿欙紝" + msg) ;
+ }
+ //寰楀埌鍔熻兘鐮佸搴旂殑鍛戒护鍚嶇О
+ comName = comSv.getCommandName(comCode, protocol, protocolVer) ;
+ if(comName == null) {
+ return BaseResponseUtils.buildError("鏈嶅姟绔嚭閿欙紝鏈緱鍒板姛鑳界爜瀵瑰簲鍛戒护鍚嶇О") ;
+ }
+ return null ;
+ }
+ /**
+ * 鍙戦�佸懡浠ゅ墠-3锛氫繚瀛樺懡浠ゆ棩蹇�
+ * @param comSv sv瀵硅薄
+ * @param manureId 姘磋偉鏈篒D
+ * @param operator 褰撳墠鐢ㄧ櫥褰曠敤鎴穒d(鎿嶄綔浜�)
+ * @param protocol 鍗忚
+ * @param protocolVerion 鍗忚
+ * @param comCode 鍔熻兘鐮�
+ * @param param 鍛戒护鍙傛暟
+ * @return
+ */
+ public BaseResponse<Object> pre3(Com4MqttSv comSv, Long manureId, Long operator, String protocol, Short protocolVerion, String comCode, Cd4MqttParameter param) {
+ comId = new IDLongGenerator().generate();
+ //鐢熸垚骞朵繚瀛樺懡浠ゆ棩蹇�
+ RmCommandHistory po = comSv.saveComHistoryPo(comId,
+ protocol + protocolVerion ,
+ comCode,
+ comName,
+ manureId,
+ ctrlPo.fboxId ,
+ param,
+ operator);
+ if(po == null){
+ return BaseResponseUtils.buildError("鏈嶅姟绔嚭閿欙紝鏈兘鐢熸垚骞朵繚瀛樺懡浠ゆ棩蹇�") ;
+ }
+ return null ;
+ }
+ /**
+ * 鍙戦�佸懡浠ゅ墠-4锛氬噯澶嘑eature
+ * @return
+ */
+ public void pre4() {
+ feature = new CompletableFuture<>();
+ ComResultWait.put(comId, feature);
+ }
+
+ /**
+ * 鍙戦�佸懡浠�
+ * @param comSv
+ * @param com
+ * @return
+ */
+ public BaseResponse<Object> doSend(Com4MqttSv comSv, Command com){
+ //寰楀埌閫氫俊涓棿浠跺彂閫佸懡浠ょ殑web URL
+ String rqUrl = comSv.get2MwRequestUrl(env, comSv.ContextComSend) ;
+ //鍚戦�氫俊涓棿浠跺彂閫亀eb璇锋眰
+ 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<Object> after(String comCode, Callback callback) {
+ try{
+ //绛夊緟閫氫俊涓棿浠堕�氱煡姘磋偉鏈烘墽琛屽懡浠や笂琛屾暟鎹紙鍛戒护缁撴灉锛�
+ MqttSubMsg subMsg = feature.get(waitMwRtnResultTimeout, TimeUnit.SECONDS);
+ return BaseResponseUtils.buildSuccess(this.dealComResult(comCode, subMsg, callback));
+ }catch (Exception e){
+ return BaseResponseUtils.buildFail("绛夊緟閫氫俊涓棿浠堕�氱煡鍛戒护缁撴灉瓒呮椂");
+ }
+ }
+
+ /**
+ * 鍙戦�佸懡浠ゆ渶鍚�
+ * @return
+ */
+ public void end(){
+ try {
+ //鏈�鍚庢竻闄ompletableFuture缂撳瓨
+ if(ComResultWait.contain(comId)){
+ ComResultWait.remove(comId);
+ }
+ }catch (Exception ee){}
+ }
+
+ /**
+ * 楠岃瘉
+ * @param dto
+ * @return
+ */
+ protected abstract String checkDto(Dto4MqttBase dto) ;
+
+
+ /**
+ * 鐢熸垚鍛戒护杩斿洖淇℃伅
+ */
+ protected abstract String dealComResult(String code, MqttSubMsg subMsg, Callback callback);
+}
--
Gitblit v1.8.0