|  |  | 
 |  |  | 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.Callback; | 
 |  |  | import com.dy.common.util.NumUtil; | 
 |  |  | import com.dy.common.webUtil.BaseResponse; | 
 |  |  | import com.dy.common.webUtil.BaseResponseUtils; | 
 |  |  | 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.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; | 
 |  |  |  | 
 |  |  | 
 |  |  |  * @Date: 2025/4/30 16:08 | 
 |  |  |  * @Description | 
 |  |  |  */ | 
 |  |  |  | 
 |  |  | @Slf4j | 
 |  |  | @Tag(name = "远程透传命令", description = "远程透传命令") | 
 |  |  | @RestController | 
 |  |  | @RequestMapping(path = "command") | 
 |  |  | @RequestMapping(path = "comTrans") | 
 |  |  | @RequiredArgsConstructor | 
 |  |  | public class ComTransCtrl { | 
 |  |  | @Scope("prototype") //因为有对象类属性,所以采用原型模式,每次请求新建一个实例对象 | 
 |  |  | public class ComTransCtrl extends ComCtrl { | 
 |  |  |  | 
 |  |  |     private final ComTransSv comSv; | 
 |  |  |     private static final String RtuSuccessMsg = "控制器接收并执行命令成功"; | 
 |  |  |  | 
 |  |  |     @Autowired | 
 |  |  |     private ComTransSv sv ; | 
 |  |  |  | 
 |  |  |     /** | 
 |  |  |      * 向设备(控制器)发送透传命令 | 
 |  |  |      * @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) { | 
 |  |  |         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() ; | 
 |  |  |                     } | 
 |  |  |                 } | 
 |  |  |             } | 
 |  |  |         } | 
 |  |  |         return res ; | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |     /** | 
 |  |  |      * 验证 | 
 |  |  |      * @param dto | 
 |  |  |      * @return | 
 |  |  |      */ | 
 |  |  |     @Override | 
 |  |  |     protected String checkDto(DtoBase dto){ | 
 |  |  |         ComTransDto myDto = (ComTransDto)dto; | 
 |  |  |         if(!NumUtil.isHex(myDto.comCode)){ | 
 |  |  |             return "命令功能码不是十六进制数"; | 
 |  |  |         } | 
 |  |  |         if(!NumUtil.isHex(myDto.comData)){ | 
 |  |  |             return "命令数据不是十六进制数"; | 
 |  |  |         } | 
 |  |  |         if(myDto.comData.length() % 2 != 0){ | 
 |  |  |             return "命令数据不完备(长度不是偶数)"; | 
 |  |  |         } | 
 |  |  |         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 "设置成功,IP地址:" + ip + ",端口号:" + 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 "查询成功,IP地址:" + ip + ",端口号:" + 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是null | 
 |  |  |         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 ? "控制器已执行遥控关阀命令" : "控制器拒绝执行遥控关阀命令"; | 
 |  |  |         } | 
 |  |  |     } | 
 |  |  |  | 
 |  |  | } |