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.protocol.p206V1.CodeV1; 
 | 
import com.dy.common.mw.protocol.p206V1.ProtocolConstantV206V1; 
 | 
import com.dy.common.mw.protocol.p206V2.CodeV2; 
 | 
import com.dy.common.mw.protocol.p206V2.ProtocolConstantV206V2; 
 | 
import com.dy.common.mw.protocol.p206V202404.CodeV202404; 
 | 
import com.dy.common.mw.protocol.p206V202404.ProtocolConstantV206V202404; 
 | 
import com.dy.common.webUtil.BaseResponse; 
 | 
import com.dy.pipIrrGlobal.daoPr.PrControllerMapper; 
 | 
import com.dy.pipIrrGlobal.daoRm.RmCommandHistoryMapper; 
 | 
import com.dy.pipIrrGlobal.pojoPr.PrController; 
 | 
import com.dy.pipIrrGlobal.pojoRm.RmCommandHistory; 
 | 
import com.dy.pipIrrGlobal.rtuMw.Web2RtuMw; 
 | 
import org.springframework.transaction.annotation.Transactional; 
 | 
  
 | 
import java.util.Date; 
 | 
  
 | 
/** 
 | 
 * @Author: liurunyu 
 | 
 * @Date: 2025/5/6 11:41 
 | 
 * @Description 
 | 
 */ 
 | 
public class CommandSv extends Web2RtuMw { 
 | 
  
 | 
  
 | 
    /** 
 | 
     * 从数据库中查询控制器对象 
 | 
     * @param prControllerDao 
 | 
     * @param intakeId 
 | 
     * @return 
 | 
     */ 
 | 
    public PrController getRtu(PrControllerMapper prControllerDao, Long intakeId){ 
 | 
        return prControllerDao.getRtu(intakeId, null); 
 | 
    } 
 | 
  
 | 
    /** 
 | 
     * 检查协议是否支持 
 | 
     * @param ctrlPo 
 | 
     * @return 
 | 
     */ 
 | 
    public String checkProtocol(PrController ctrlPo){ 
 | 
        if(!ctrlPo.getProtocol().equals(ProtocolConstantV206V1.protocolName) 
 | 
                && !ctrlPo.getProtocol().equals(ProtocolConstantV206V2.protocolName) 
 | 
                && !ctrlPo.getProtocol().equals(ProtocolConstantV206V202404.protocolName)) { 
 | 
            return "对应控制器协议" + ctrlPo.getProtocol() + "未实现命令发送逻辑" ; 
 | 
        } 
 | 
        return null ; 
 | 
    } 
 | 
  
 | 
    /** 
 | 
     * 根据协议获取命令名称 
 | 
     * @param nowComCode 
 | 
     * @param ctrlPo 
 | 
     * @return 
 | 
     */ 
 | 
    public String getCommandName(String nowComCode, PrController ctrlPo){ 
 | 
        if(ctrlPo.getProtocol().equals(ProtocolConstantV206V1.protocolName)) { 
 | 
            return CodeV1.getCodeName(nowComCode) ; 
 | 
        }else if(ctrlPo.getProtocol().equals(ProtocolConstantV206V2.protocolName)) { 
 | 
            return CodeV2.getCodeName(nowComCode) ; 
 | 
        }else if(ctrlPo.getProtocol().equals(ProtocolConstantV206V202404.protocolName)) { 
 | 
            return CodeV202404.getCodeName(nowComCode) ; 
 | 
        } 
 | 
        return null ; 
 | 
    } 
 | 
  
 | 
    /** 
 | 
     * 保存命令历史记录 
 | 
     * @param rmCommandHistoryDao 
 | 
     * @param comId 
 | 
     * @param protocol 
 | 
     * @param commandCode 
 | 
     * @param commandName 
 | 
     * @param intakeId 
 | 
     * @param rtuAddr 
 | 
     * @param param 
 | 
     * @param operator 
 | 
     * @return 
 | 
     */ 
 | 
    @Transactional(rollbackFor = Exception.class) 
 | 
    public RmCommandHistory saveComHistoryPo(RmCommandHistoryMapper rmCommandHistoryDao, 
 | 
                                             Long comId, 
 | 
                                             String protocol, 
 | 
                                             String commandCode, 
 | 
                                             String commandName, 
 | 
                                             Long intakeId, 
 | 
                                             String rtuAddr, 
 | 
                                             Object param, 
 | 
                                             Long operator) { 
 | 
        RmCommandHistory po = new RmCommandHistory(); 
 | 
        po.setComId(comId); 
 | 
        po.setCommandCode(commandCode); 
 | 
        po.setCommandName(commandName); 
 | 
        po.setIntakeId(intakeId); 
 | 
        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"; 
 | 
        } 
 | 
    } 
 | 
  
 | 
} 
 |