| package com.dy.pipIrrGlobal.command; | 
|   | 
| import com.alibaba.fastjson2.JSON; | 
| import com.alibaba.fastjson2.JSONObject; | 
| import com.dy.common.mw.protocol.Command; | 
| import com.dy.common.mw.protocol.CommandBackParam; | 
| import com.dy.common.mw.protocol4Mqtt.pSdV1.CodeSdV1; | 
| import com.dy.common.mw.protocol4Mqtt.pSdV1.ProtocolConstantSdV1; | 
| import com.dy.common.webUtil.BaseResponse; | 
| import com.dy.pipIrrGlobal.daoPr.PrStManureMapper; | 
| import com.dy.pipIrrGlobal.daoRm.RmCommandHistoryMapper; | 
| import com.dy.pipIrrGlobal.pojoPr.PrStManure; | 
| import com.dy.pipIrrGlobal.pojoRm.RmCommandHistory; | 
| import com.dy.pipIrrGlobal.rtuMw.Web2RtuMw; | 
|   | 
| import java.util.Date; | 
|   | 
| /** | 
|  * @Author: liurunyu | 
|  * @Date: 2025/5/6 11:41 | 
|  * @Description | 
|  */ | 
| public class Command4MqttSv extends Web2RtuMw { | 
|   | 
|   | 
|     /** | 
|      * 从数据库中查询水肥机对象 | 
|      * @param prStManureDao | 
|      * @param manureId | 
|      * @return | 
|      */ | 
|     public PrStManure getManure(PrStManureMapper prStManureDao, Long manureId){ | 
|         return prStManureDao.selectByPrimaryKey(manureId); | 
|     } | 
|   | 
|     /** | 
|      * 检查协议是否支持 | 
|      * @param ctrlPo | 
|      * @return | 
|      */ | 
|     public String checkProtocol(PrStManure ctrlPo){ | 
|         return null ; | 
|     } | 
|   | 
|     /** | 
|      * 根据协议获取命令名称 | 
|      * @param nowComCode | 
|      * @param protocol | 
|      * @return | 
|      */ | 
|     public String getCommandName(String nowComCode, String protocol, Short protocolVer){ | 
|         if(protocol.equals(ProtocolConstantSdV1.protocolName) | 
|             && protocolVer.equals(ProtocolConstantSdV1.protocolVer)) { | 
|             return CodeSdV1.getCodeName(nowComCode) ; | 
|         } | 
|         return null ; | 
|     } | 
|   | 
|     /** | 
|      * 保存命令历史记录 | 
|      * @param rmCommandHistoryDao | 
|      * @param comId | 
|      * @param protocol | 
|      * @param commandCode | 
|      * @param commandName | 
|      * @param manureId | 
|      * @param rtuAddr | 
|      * @param param | 
|      * @param operator | 
|      * @return | 
|      */ | 
|     public RmCommandHistory saveComHistoryPo(RmCommandHistoryMapper rmCommandHistoryDao, | 
|                                              Long comId, | 
|                                              String protocol, | 
|                                              String commandCode, | 
|                                              String commandName, | 
|                                              Long manureId, | 
|                                              String rtuAddr, | 
|                                              Object param, | 
|                                              Long operator) { | 
|         RmCommandHistory po = new RmCommandHistory(); | 
|         po.setComId(comId); | 
|         po.setCommandCode(commandCode); | 
|         po.setCommandName(commandName); | 
|         po.setIntakeId(manureId); | 
|         po.setRtuAddr(rtuAddr); | 
|         po.setProtocol(protocol); | 
|         po.setParam((JSONObject) JSON.toJSON(param)); | 
|         po.setSendTime(new Date()); | 
|         po.setOperator(operator); | 
|         int count = rmCommandHistoryDao.insertSelective(po) ; | 
|         if(count == 0){ | 
|             po = null ; | 
|         } | 
|         return po; | 
|     } | 
|   | 
|     /** | 
|      * 处理通信中间件返回的命令处理结果 | 
|      * @param res | 
|      * @return | 
|      */ | 
|     public String dealMwDealResponse(BaseResponse res){ | 
|         if(res != null){ | 
|             Command reCom = JSON.parseObject(res.getContent() == null ? null : JSON.toJSONString(res.getContent()), Command.class) ; | 
|             CommandBackParam bakParam = JSON.parseObject((reCom== null || reCom.param == null) ? null : JSON.toJSONString(reCom.param), CommandBackParam.class) ; | 
|             if(res.isSuccess()){ | 
|                 if(bakParam != null){ | 
|                     if(bakParam.getSuccess().booleanValue()){ | 
|                         //通信中间件成功处理了命令 | 
|                         //等待控制器接收并执行命令后的应答,然后通信中间件通知本模块 | 
|                         return null ; | 
|                     }else{ | 
|                         return "通信中间件处理命令失败,失败信息:" + bakParam.getMessage(); | 
|                     } | 
|                 }else{ | 
|                     return "通信中间件返回命令结果中不包含CommandBackParam类型参数"; | 
|                 } | 
|             }else{ | 
|                 if(bakParam != null){ | 
|                     if(bakParam.getSuccess().booleanValue()){ | 
|                         //通信中间件成功处理了命令 | 
|                         //等待控制器接收并执行命令后的应答,然后通信中间件通知本模块 | 
|                         return "通信中间件处理命令失败,失败信息:" + (res.getMsg() == null? "" : ("," + res.getMsg())) ; | 
|                     }else{ | 
|                         return "通信中间件处理命令失败,失败信息:" + bakParam.getMessage(); | 
|                     } | 
|                 }else{ | 
|                     return "通信中间件处理命令失败,失败信息:" + (res.getMsg() == null? "" : ("," + res.getMsg())) ; | 
|                 } | 
|             } | 
|         }else{ | 
|             return "通信中间件返回命令结果为null"; | 
|         } | 
|     } | 
|   | 
| } |