liurunyu
2023-11-22 429f9d85eddbb07081c149d7156964337ba688aa
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
package com.dy.common.mw.protocol.p206V1_0_0.parse;
 
import com.dy.common.mw.protocol.*;
import com.dy.common.mw.protocol.p206V1_0_0.*;
import com.dy.common.util.ByteUtil;
import com.dy.common.util.ByteUtilUnsigned;
import com.dy.common.util.CRC8_for_2_0;
import com.dy.common.util.DateTime;
 
@AnnotationCodeDown(ifAny={
        CodeV1_0_1.cd_06
})
public class Cd_02_Down  implements CodeParse {
 
    //private static Logger log = LogManager.getLogger(Cd_XY_Down.class);
 
    @Override
    public MidResult[] parse(Boolean isLowPower, CodeParseParams params, CodeParseCallback callback) throws Exception {
        ParseParamsForDownV1_0_1 para = (ParseParamsForDownV1_0_1) params ;
        byte[] bs = this.doParse(para) ;
 
        MidResultToRtu midRs = new MidResultToRtu() ;
        midRs.protocolName = para.protocolName ;//协议名称
        midRs.rtuAddr = para.rtuAddr ;//IMEI号(电信平台设备IMEI)
        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 = true ;//RTU不在线,命令是否缓存,低功耗时为true
 
        //对RTU上行数据的应答
        midRs.hasResponse = false ;//是否有应答
        midRs.maxSendTimes = 1 ;//对RTU上行数据应答,所以只需发一次
        if(isLowPower != null && isLowPower.booleanValue()){
            //低功耗时
            midRs.isQuickSend = true ;
        }
 
        return new MidResult[]{midRs} ;
    }
    /**
     * 构造下行数据
     * @param para
     * @return
     * @throws Exception
     */
    public byte[] doParse(ParseParamsForDownV1_0_1 para) throws Exception {
        CommonV1_0_1 commonV1_0_1 = new CommonV1_0_1() ;
        byte[] bytes ;
        byte[] bsHead = new byte[ProtocolConstantV206V1_0_0.lenHead2Code] ;
        byte index = 0 ;
        bsHead[index] = ProtocolConstantV206V1_0_0.P_Head_Byte ;
 
        index++ ;
        bsHead[index] = 0 ;//帧长度
 
        index++ ;
        bsHead[index] = ProtocolConstantV206V1_0_0.P_Head_Byte ;
 
        index++ ;
        bsHead[index] = commonV1_0_1.createCtrl((byte)0) ;
 
        String rtuAddr = para.rtuAddr ;
        String rtuAddr1 = rtuAddr.substring(0, 6) ;
        String rtuAddr2 = rtuAddr.substring(6) ;
 
        index++ ;
        ByteUtil.string2BCD_BE(bsHead, rtuAddr1, index) ;
        index += 3 ;
 
        Integer rtuAddr2Int = Integer.parseInt(rtuAddr2) ;
        ByteUtilUnsigned.short2Bytes_BE(bsHead, rtuAddr2Int.shortValue(), index);
        index += 2 ;
 
        ByteUtil.hex2Bytes(para.commandCode, bsHead, index) ;
 
        byte[] bs = new byte[7] ;
        bs[0] = (byte)0xF2 ;
        ByteUtil.string2BCD_BE(bs, DateTime.yyMMddhhmmss(), 1) ;
 
        bytes = ByteUtil.bytesMerge(bsHead, bs) ;
 
        int len = bytes.length + ProtocolConstantV206V1_0_0.lenTail;
        bytes[ProtocolConstantV206V1_0_0.dataLenIndex] = (byte)len ;
 
        int crc = new CRC8_for_2_0().CRC8(bytes, ProtocolConstantV206V1_0_0.ctrlIndex, bytes.length -1) ;
        bs = new byte[ProtocolConstantV206V1_0_0.lenTail] ;
        bs[0] = (byte)crc ;
        bs[1] = ProtocolConstantV206V1_0_0.P_Tail_Byte ;
        bytes = ByteUtil.bytesMerge(bytes, bs) ;
 
        return bytes ;
    }
 
}