package com.dy.common.mw.protocol.p206V1_0_0; 
 | 
  
 | 
import com.dy.common.util.ByteUtil; 
 | 
import com.dy.common.util.ByteUtilUnsigned; 
 | 
  
 | 
  
 | 
public class CommonV1_0_1 { 
 | 
    /** 
 | 
     * 检查头 
 | 
     * @param bs 上行字节数组 
 | 
     * @return true是,false否 
 | 
     * @throws Exception  异常 
 | 
     */ 
 | 
    public Boolean isThisProtocolHead(byte[] bs) throws Exception{ 
 | 
        if(bs == null){ 
 | 
            return null ; 
 | 
        }else if(bs.length >= (ProtocolConstantV206V1_0_0.ctrlIndex - 1) 
 | 
                && bs[0] == ProtocolConstantV206V1_0_0.P_Head_Byte 
 | 
                && bs[2] == ProtocolConstantV206V1_0_0.P_Head_Byte){ 
 | 
            //String vs = this.parseVersion(bs) ; 
 | 
            //return vs.equals(ProtocolConstantV206V1_0_0.version); 
 | 
            return true ; 
 | 
        }else{ 
 | 
            return false ; 
 | 
        } 
 | 
    } 
 | 
     
 | 
    /** 
 | 
     * 检查头 
 | 
     * @param bs 上行字节数组 
 | 
     * @throws Exception 异常 
 | 
     */ 
 | 
    public void checkHead(byte[] bs) throws Exception{ 
 | 
        if(bs.length < 13 || bs[0] != ProtocolConstantV206V1_0_0.P_Head_Byte || bs[2] != ProtocolConstantV206V1_0_0.P_Head_Byte){ 
 | 
            throw new Exception("上行数据帧头不正确!") ; 
 | 
        } 
 | 
    } 
 | 
     
 | 
    /** 
 | 
     * 检查尾 
 | 
     * @param bs 上行字节数组 
 | 
     * @throws Exception 异常 
 | 
     */ 
 | 
    public void checkTail(byte[] bs) throws Exception{ 
 | 
        if(bs.length < 1 || bs[bs.length - 1] != ProtocolConstantV206V1_0_0.P_Tail_Byte){ 
 | 
            throw new Exception("上行数据尾不正确!") ; 
 | 
        } 
 | 
    } 
 | 
  
 | 
  
 | 
    /** 
 | 
     * 分析数据字节数 
 | 
     * @param bs 上行字节数组 
 | 
     * @return 数据长度 
 | 
     * @throws Exception 异常 
 | 
     */ 
 | 
    public int parseDataLen(byte[] bs)throws Exception{ 
 | 
        return ByteUtilUnsigned.bytes2Short_BE(bs, ProtocolConstantV206V1_0_0.dataLenIndex) ; 
 | 
    } 
 | 
  
 | 
  
 | 
    /** 
 | 
     * 分析Rtu地址 
 | 
     * @param bs 上行字节数组 
 | 
     * @return 控制器地址 
 | 
     * @throws Exception 异常 
 | 
     */ 
 | 
    public String parseRtuAddr(byte[] bs)throws Exception{ 
 | 
        String rtuAddrBCD = "" + ByteUtil.BCD2Long_BE(bs, ProtocolConstantV206V1_0_0.rtuAddr1Index_start, ProtocolConstantV206V1_0_0.rtuAddr1Index_end) ; 
 | 
        String rtuAddrStr = "" + ByteUtilUnsigned.bytes2Short_BE(bs, ProtocolConstantV206V1_0_0.rtuAddr2Index_start) ; 
 | 
        while(rtuAddrStr.length() < 6){ 
 | 
            rtuAddrStr = "0" + rtuAddrStr ; 
 | 
        } 
 | 
        return rtuAddrBCD + rtuAddrStr ; 
 | 
    } 
 | 
  
 | 
    /** 
 | 
     * 分析功能码 
 | 
     * @param bs 上行字节数组 
 | 
     * @return 功能码 
 | 
     */ 
 | 
    public String parseCode(byte[] bs){ 
 | 
        return ByteUtil.bytes2Hex(bs, false, ProtocolConstantV206V1_0_0.codeIndex, 1) ; 
 | 
    } 
 | 
  
 | 
  
 | 
    /** 
 | 
     * 校验和检查 
 | 
     * @param bs 上行字节数组 
 | 
     * @return 通过true,未通过false 
 | 
     */ 
 | 
    public boolean checkCrc(byte[] bs) { 
 | 
        byte he = 0 ; 
 | 
        for(int i = 0 ; i <= bs.length - 3 ; i++){ 
 | 
            he = (byte)(he + bs[i]) ; 
 | 
        } 
 | 
        return bs[bs.length - 2] == he ; 
 | 
    } 
 | 
  
 | 
    /** 
 | 
     * 校验和检查 
 | 
     * @param bs  上行字节数组 
 | 
     * @return 通过null,未通过返回原因 
 | 
     * @throws Exception 异常 
 | 
     */ 
 | 
    public String checkCrc_str(byte[] bs) throws Exception { 
 | 
        byte[] he = new byte[1] ; 
 | 
        for(int i = 0 ; i <= bs.length - 3 ; i++){ 
 | 
            he[0] = (byte)(he[0] + bs[i]) ; 
 | 
        } 
 | 
        short hes = ByteUtilUnsigned.byte2Byte(he, 0); 
 | 
        short heOrg = ByteUtilUnsigned.byte2Byte(bs, bs.length - 2); 
 | 
        if(hes == heOrg){ 
 | 
            return null ; 
 | 
        }else{ 
 | 
            return "计算校验和是:" + hes + ",上传校验和是" + heOrg ; 
 | 
        } 
 | 
    } 
 | 
  
 | 
  
 | 
    /* 
 | 
    构造控制域 
 | 
    D7                  D6                  D5~D4            D3~D0 
 | 
    传输方向位 DIR        拆分标志位 DIV       帧计数位 FCB       功能码 
 | 
    */ 
 | 
    public byte createCtrl(byte funcCode){ 
 | 
        byte b = 0 ; 
 | 
        b = (byte)(b | funcCode) ; 
 | 
        //FCB == 3 
 | 
        b = (byte)(b | (byte)60) ; 
 | 
        //DIV = 0 
 | 
        //DIR = 0 
 | 
        return b ; 
 | 
    } 
 | 
  
 | 
    /** 
 | 
     * 得到关开阀类型名称 
 | 
     * @param type 字节 
 | 
     * @return 名称 
 | 
     */ 
 | 
    public static String openCloseValveType(byte type){ 
 | 
        return switch (type) { 
 | 
            case 1 -> "刷卡开阀"; 
 | 
            case 2 -> "刷卡关阀"; 
 | 
            case 3 -> "中心站开阀"; 
 | 
            case 4 -> "中心站关阀"; 
 | 
            case 5 -> "欠费关阀"; 
 | 
            case 6 -> "流量计故障关阀"; 
 | 
            case 7 -> "紧急关阀"; 
 | 
            case 8 -> "用户远程开阀"; 
 | 
            case 9 -> "用户远程关阀"; 
 | 
            default -> "未知"; 
 | 
        }; 
 | 
    } 
 | 
     
 | 
    /* 
 | 
     * 分析版本号 
 | 
     * @param bs  上行字节数组 
 | 
     * @return 版本号 
 | 
     * @throws Exception 异常 
 | 
    public String parseVersion(byte[] bs)throws Exception{ 
 | 
        short ver = ByteUtilUnsigned.byte2Byte(bs, ProtocolConstantV206V1_0_0.versionIndex) ; 
 | 
        char[] cs = ("" + ver).toCharArray() ; 
 | 
        StringBuilder vs = new StringBuilder() ; 
 | 
        for(byte i = 0 ; i < cs.length; i++){ 
 | 
            if(i == 0){ 
 | 
                vs.append(cs[i]) ; 
 | 
            }else{ 
 | 
                vs.append(".").append(cs[i]) ; 
 | 
            } 
 | 
        } 
 | 
        return vs.toString() ; 
 | 
    } 
 | 
    */ 
 | 
  
 | 
} 
 |