liurunyu
2023-11-18 c1ddfd71223c1a7d704b6f21b669fbfcb37adc82
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
package com.dy.common.mw.protocol.pMeterV1_0_1.parse;
 
import com.dy.common.mw.protocol.AnnotationCodeDown;
import com.dy.common.mw.protocol.CodeParse;
import com.dy.common.mw.protocol.CodeParseCallback;
import com.dy.common.mw.protocol.CodeParseParams;
import com.dy.common.mw.protocol.MidResult;
import com.dy.common.mw.protocol.MidResultToRtu;
import com.dy.common.mw.protocol.pMeterV1_0_1.*;
import com.dy.common.util.ByteUtil;
import com.dy.common.util.ByteUtilUnsigned;
import com.dy.common.util.DateTime;
 
@AnnotationCodeDown(ifAny={
        CodeV1_0_1.cd_01,
        CodeV1_0_1.cd_02,
        CodeV1_0_1.cd_03,
        CodeV1_0_1.cd_04,
        CodeV1_0_1.cd_05
})
public class Cd_XY_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.meterNo = para.meterNo ;//水表号
        midRs.productId = para.productId ;//电信平台产品ID,当不在电信平台运行时,为null
        midRs.deviceId = para.deviceId ;//电信平台设备ID,当不在电信平台运行时,为null
        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
        
        if(para.commandCode != null && (
                para.commandCode.equals(CodeV1_0_1.cd_01) ||
                para.commandCode.equals(CodeV1_0_1.cd_02) ||
                para.commandCode.equals(CodeV1_0_1.cd_03)
                )){
            //对RTU上行数据的应答
            midRs.hasResponse = false ;//是否有应答
            midRs.maxSendTimes = 1 ;//对RTU上行数据应答,所以只需发一次
            if(isLowPower != null && isLowPower.booleanValue()){
                //低功耗时
                midRs.isQuickSend = true ;
            }
        }
 
        return new MidResult[]{midRs} ;
    }
    /**
     * 构造下行数据
     * @param params  
     * @return
     * @throws Exception
     */
    public byte[] doParse(ParseParamsForDownV1_0_1 para) throws Exception {
        byte[] bytes = null ;
        byte[] bsHead = new byte[ProtocolConstantV1_0_1.downBsHeadLen] ;
        byte index = 0 ;
        bsHead[index] = ProtocolConstantV1_0_1.P_Head_Byte ;
        index++ ;
        bsHead[index] = 0 ;//帧长度
        index++ ;
        bsHead[index] = 0 ;//帧长度
        
        index++ ;
        bsHead[index] = ProtocolConstantV1_0_1.versionByte ;//协议版本号
        index++ ;
        
        String rtuAddr = para.rtuAddr ;
        while(true){
            if(rtuAddr.length() >= 16){
                break ;
            }else{
                rtuAddr = "0" + rtuAddr ;
            }
        }
 
        ByteUtil.string2BCD_BE(bsHead, rtuAddr, index) ;//IMEI号
        index += 8 ;
        String meterNo = para.meterNo ;
        while(true){
            if(meterNo.length() >= 16){
                break ;
            }else{
                meterNo = "0" + meterNo ;
            }
        }
        ByteUtil.string2BCD_BE(bsHead, meterNo, index) ;//水表号
        index += 8 ;
        
        ByteUtil.hex2Bytes(para.commandCode, bsHead, index) ;
        
        byte[] bs = new byte[6] ;
        ByteUtil.string2BCD_BE(bs, DateTime.yyMMddhhmmss(), 0) ;
        
        bytes = ByteUtil.bytesMerge(bsHead, bs) ;
        
        int len = bytes.length + ProtocolConstantV1_0_1.downBsTailLen ;
        ByteUtilUnsigned.short2Bytes_BE(bytes, len, ProtocolConstantV1_0_1.dataLenIndex_start);
        
        
        byte he = 0 ;
        for(int i = 0 ; i < bytes.length; i++){
            he += bytes[i] ;
        }
        
        bs = new byte[ProtocolConstantV1_0_1.downBsTailLen] ;
        ByteUtilUnsigned.byte2Byte(bs, he, 0) ;
        bs[1] = ProtocolConstantV1_0_1.P_Tail_Byte ;
        bytes = ByteUtil.bytesMerge(bytes, bs) ;
        
        return bytes ;
    }
 
}