package com.dy.common.mw.protocol.p206V202404.parse; 
 | 
  
 | 
import com.alibaba.fastjson2.JSON; 
 | 
import com.alibaba.fastjson2.JSONObject; 
 | 
import com.dy.common.mw.protocol.*; 
 | 
import com.dy.common.mw.protocol.p206V202404.CodeV202404; 
 | 
import com.dy.common.mw.protocol.p206V202404.ParseParamsForDownV202404; 
 | 
import com.dy.common.mw.protocol.p206V202404.downVos.ComCd21Vo; 
 | 
import com.dy.common.mw.protocol.p206V202404.parse.global.GlCreate; 
 | 
import com.dy.common.util.ByteUtil; 
 | 
  
 | 
import java.nio.charset.StandardCharsets; 
 | 
  
 | 
@AnnotationCodeDown(ifAny={ 
 | 
        CodeV202404.cd_21 
 | 
}) 
 | 
public class Cd_21_Down implements CodeParse { 
 | 
  
 | 
    @Override 
 | 
    public MidResult[] parse(Boolean isLowPower, CodeParseParams params, CodeParseCallback callback) throws Exception { 
 | 
        ParseParamsForDownV202404 para = (ParseParamsForDownV202404) params ; 
 | 
        byte[] bs = this.doParse(para) ; 
 | 
  
 | 
        MidResultToRtu midRs = new MidResultToRtu() ; 
 | 
        midRs.protocolName = para.protocolName ;//协议名称 
 | 
        midRs.rtuResultSendWebUrl = para.rtuResultSendWebUrl ; 
 | 
        midRs.rtuAddr = para.rtuAddr ;//Rtu地址 
 | 
        midRs.commandId = para.commandId ;//命令ID,发起命令的客户端(web端)生成,以匹配命令结果 
 | 
        midRs.downCode = para.commandCode ;//下行命令功能码; 
 | 
        midRs.downBuffer = bs ;//下行命令数据 
 | 
        midRs.downBufHex = ByteUtil.bytes2Hex(bs, true) ;//下行命令数据十六进制形式 
 | 
        midRs.hasResponse = true ;//是否有应答 
 | 
        midRs.maxSendTimes = null ;//命令最大发送次数(当收不到应答时,将重发),如果不设置,命令缓存器进行补充设置 
 | 
        midRs.isCachForOffLine = false ;//RTU不在线,命令是否缓存,低功耗时为true 
 | 
  
 | 
        midRs.isSendFirst = false ;//命令应答,首先发送,防止RTU重新上报 
 | 
        if(isLowPower != null && isLowPower.booleanValue()){ 
 | 
            //低功耗时,尽快发送 
 | 
            midRs.isQuickSend = true ; 
 | 
        } 
 | 
  
 | 
        return new MidResult[]{midRs} ; 
 | 
    } 
 | 
    /** 
 | 
     * 构造下行数据 
 | 
     * @param para 参数 
 | 
     * @return 字节数组 
 | 
     * @throws Exception 异常 
 | 
     */ 
 | 
    public byte[] doParse(ParseParamsForDownV202404 para) throws Exception { 
 | 
        byte[] bytes = GlCreate.createStart2Cd(para.rtuAddr, para.commandCode); 
 | 
        if(para.param != null){ 
 | 
            JSONObject obj = (JSONObject)para.param; 
 | 
            String json = obj.toJSONString(); 
 | 
            ComCd21Vo cvo = JSON.parseObject(json, ComCd21Vo.class) ; 
 | 
            if(cvo == null){ 
 | 
                throw new Exception("json转ComCd21Vo为null") ; 
 | 
            } 
 | 
            if(cvo.channel == null){ 
 | 
                throw new Exception("IP通道号不能为空") ; 
 | 
            } 
 | 
            if(cvo.channel != 1 && cvo.channel != 2){ 
 | 
                throw new Exception("IP通道号只能是1或2") ; 
 | 
            } 
 | 
            if(cvo.ip == null || cvo.ip.equals("")){ 
 | 
                throw new Exception("IP不能为空") ; 
 | 
            } 
 | 
            if(cvo.ip.length() > 15){ 
 | 
                throw new Exception("IP最大长度是15个字符") ; 
 | 
            } 
 | 
            if(cvo.port < 0 || cvo.port > 65535){ 
 | 
                throw new Exception("端口号必须是0~65535范围内的整数") ; 
 | 
            } 
 | 
  
 | 
            byte[] ipBs = cvo.ip.getBytes(StandardCharsets.UTF_8) ; 
 | 
            byte[] portBs = ("" + cvo.port).getBytes(StandardCharsets.UTF_8) ; 
 | 
  
 | 
            byte[] bs = new byte[4] ; 
 | 
            int index = 0 ; 
 | 
            bs[index] = (byte)(Integer.parseInt(cvo.controllerType, 16)); 
 | 
  
 | 
            index ++ ; 
 | 
            bs[index] = cvo.projectNo.byteValue() ; 
 | 
  
 | 
            index ++ ; 
 | 
            bs[index] = cvo.channel.byteValue() ; 
 | 
  
 | 
            index ++ ; 
 | 
            bs[index] = (byte)((ipBs.length << 4) + portBs.length) ; 
 | 
  
 | 
            bytes = ByteUtil.bytesMerge(bytes, bs) ; 
 | 
  
 | 
            bytes = ByteUtil.bytesMerge(bytes, ipBs) ; 
 | 
            bytes = ByteUtil.bytesMerge(bytes, portBs) ; 
 | 
  
 | 
  
 | 
            byte[] bs1 = new byte[7] ; 
 | 
            index ++ ; 
 | 
            GlCreate.createPw(bs1, index); 
 | 
  
 | 
            index += 2 ; 
 | 
            GlCreate.createS2D(bs1, index) ; 
 | 
  
 | 
            index += 4 ; 
 | 
            bs1[index] = 0 ; //时延 
 | 
            bytes = ByteUtil.bytesMerge(bytes, bs1) ; 
 | 
  
 | 
        }else{ 
 | 
            throw new Exception("下行命令参数据为null") ; 
 | 
        } 
 | 
        GlCreate.createLen(bytes);//长度放字节数组中 
 | 
  
 | 
        byte[] bsTail = GlCreate.createCrcTail(bytes) ;//CRC和尾 
 | 
  
 | 
        bytes = ByteUtil.bytesMerge(bytes, bsTail) ; 
 | 
  
 | 
        return bytes ; 
 | 
    } 
 | 
  
 | 
  
 | 
} 
 |