| package com.dy.pipIrrRemote.monitor.mqttSd1.irr; | 
|   | 
| import com.alibaba.fastjson2.JSON; | 
| import com.dy.common.aop.SsoAop; | 
| import com.dy.common.mw.protocol.Command; | 
| import com.dy.common.mw.protocol4Mqtt.MqttSubMsg; | 
| import com.dy.common.mw.protocol4Mqtt.pSdV1.CodeSdV1; | 
| import com.dy.common.mw.protocol4Mqtt.pSdV1.ProtocolConstantSdV1; | 
| import com.dy.common.mw.protocol4Mqtt.pSdV1.upVos.ManureVo; | 
| import com.dy.common.util.Callback; | 
| import com.dy.common.webUtil.BaseResponse; | 
| import com.dy.common.webUtil.BaseResponseUtils; | 
| import com.dy.pipIrrRemote.common.dto.Dto4MqttBase; | 
| import com.dy.pipIrrRemote.monitor.common.Com4MqttCtrl; | 
| import com.dy.pipIrrRemote.monitor.mqttSd1.stir.CdDto; | 
| 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; | 
|   | 
| import java.lang.reflect.InvocationHandler; | 
| import java.lang.reflect.Proxy; | 
|   | 
| /** | 
|  * @Author: liurunyu | 
|  * @Date: 2025/6/16 17:35 | 
|  * @Description | 
|  * 大禹将水肥机应用于管灌系统,所以水肥机上未安装灌溉阀门,相应此功能也不应用了 | 
|  */ | 
|   | 
| @Slf4j | 
| @Tag(name = "远程命令", description = "灌溉启停") | 
| @RestController("mqttSd1IrrCtrl") | 
| @RequestMapping(path = "mqttSd1/irr") | 
| @RequiredArgsConstructor | 
| @Scope("prototype") //因为有对象类属性,所以采用原型模式,每次请求新建一个实例对象 | 
| public class CdCtrl extends Com4MqttCtrl { | 
|   | 
|     private static final String RtuSuccessMsg = "控制器接收并执行命令成功,无返回数据"; | 
|   | 
|     private static final String Protocol = ProtocolConstantSdV1.protocolName ; | 
|     private static final Short ProtocolVersion = ProtocolConstantSdV1.protocolVer ; | 
|     private static final String ComCode = CodeSdV1.cd_Irr ; | 
|   | 
|   | 
|     @Autowired | 
|     private CdSv sv ; | 
|   | 
|     /** | 
|      * 向设备(Mqtt中间件->FBox)发送命令 | 
|      * @param dto 前端发来的值对象 | 
|      * @param bindingResult 对dto验证的结果 | 
|      * @return 返回前端 | 
|      */ | 
|     @PostMapping(path = "start", consumes = MediaType.APPLICATION_JSON_VALUE) | 
|     @SsoAop() | 
|     public BaseResponse<Object> start(@RequestBody @Valid com.dy.pipIrrRemote.monitor.mqttSd1.stir.CdDto dto, BindingResult bindingResult) { | 
|         return this.send(dto, bindingResult, true) ; | 
|     } | 
|   | 
|     /** | 
|      * 向设备(Mqtt中间件->FBox)发送命令 | 
|      * @param dto 前端发来的值对象 | 
|      * @param bindingResult 对dto验证的结果 | 
|      * @return 返回前端 | 
|      */ | 
|     @PostMapping(path = "stop", consumes = MediaType.APPLICATION_JSON_VALUE) | 
|     @SsoAop() | 
|     public BaseResponse<Object> stop(@RequestBody @Valid com.dy.pipIrrRemote.monitor.mqttSd1.stir.CdDto dto, BindingResult bindingResult) { | 
|         return this.send(dto, bindingResult, false) ; | 
|     } | 
|   | 
|     /** | 
|      * 向设备(Mqtt中间件->FBox)发送命令 | 
|      * @param dto 前端发来的值对象 | 
|      * @param bindingResult 对dto验证的结果 | 
|      * @return 返回前端 | 
|      */ | 
|     private BaseResponse<Object> send(CdDto dto, BindingResult bindingResult, boolean startTrueStopFalse) { | 
|         BaseResponse<Object> res ; | 
|         //发送命令前-1:验证 | 
|         res = super.pre1(sv, ComCode, dto, bindingResult); | 
|         if(res == null) { | 
|             //发送命令前-2:获得数据 | 
|             res = super.pre2(sv, Protocol, ProtocolVersion, ComCode, dto, bindingResult); | 
|             if (res == null) { | 
|                 //发送命令前-3:保存命令日志 | 
|                 CdParam comParam = CdParam.builder().no(ctrlPo.no).startTrueStopFalse(true).build(); | 
|                 res = super.pre3(sv, dto.manureId, dto.getOperator(), Protocol, ProtocolVersion, ComCode, comParam); | 
|                 if (res == null) { | 
|                     //发送命令前-4:准备Feature | 
|                     super.pre4(); | 
|                     try { | 
|                         //创建外部命令(发给MQTT->FBox) | 
|                         Command com = sv.createMQTTCommand(ctrlPo.fboxId, "" + comId, Protocol, ProtocolVersion, ComCode); | 
|                         com.rtuResultSendWebUrl = mqttResultSendWebUrl; | 
|                         com.param = comParam ; | 
|                         //发送命令 | 
|                         res = super.doSend(sv, com); | 
|                         if (res == null) { | 
|                             //发送命令后 | 
|                             res = super.after(ComCode, null); | 
|                         } | 
|                     } catch (Exception e) { | 
|                         res = BaseResponseUtils.buildFail("服务端构造并向通信中间件发送请求时异常" + (e.getMessage() == null ? "" : e.getMessage())); | 
|                     } finally { | 
|                         //最终 | 
|                         super.end(); | 
|                     } | 
|                 } | 
|             } | 
|         } | 
|         return res ; | 
|     } | 
|   | 
|     @Override | 
|     protected String checkDto(Dto4MqttBase dto) { | 
|         return null; | 
|     } | 
|   | 
|     @Override | 
|     protected String dealComResult(String code, MqttSubMsg subMsg, Callback callback){ | 
|         String msg; | 
|         if(subMsg != null){ | 
|             if(subMsg.vo4Up != null && Proxy.isProxyClass(subMsg.vo4Up.getClass())){ | 
|                 // 获取代理的 InvocationHandler | 
|                 InvocationHandler handler = Proxy.getInvocationHandler(subMsg.vo4Up); | 
|                 String json = JSON.toJSONString(handler) ; | 
|                 ManureVo vo = JSON.parseObject(json, ManureVo.class); | 
|                 msg = vo.toString() ; | 
|             }else{ | 
|                 msg = RtuSuccessMsg ; | 
|             } | 
|         }else{ | 
|             msg = RtuSuccessMsg ; | 
|         } | 
|         return msg; | 
|     } | 
| } |