liurunyu
3 天以前 7aab004601e77890b158f19d1735594a8326feaa
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
package com.dy.common.mw.protocol.p206V202404.parse.global;
 
import com.dy.common.mw.protocol.p206V202404.CommonV202404;
import com.dy.common.mw.protocol.p206V202404.ProtocolConstantV206V202404;
import com.dy.common.mw.protocol.p206V202404.upVos.*;
import com.dy.common.util.ByteUtil;
import com.dy.common.util.ByteUtilUnsigned;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
 
public class GlParse {
 
    private static final Logger log = LogManager.getLogger(GlParse.class);
 
    /**
     * 分析上行数据中的时钟
     * @param bs 上行字节数组
     * @param index 启始位
     * @return 控制器地址
     * @throws Exception 异常
     */
    public static String parseRtuDt(byte[] bs, int index)throws Exception{
        String ss = ByteUtil.BCD2String_BE(bs, index, index) ;
        index++ ;
        String MM = ByteUtil.BCD2String_BE(bs, index, index) ;
        index++ ;
        String HH = ByteUtil.BCD2String_BE(bs, index, index) ;
        index++ ;
        String dd = ByteUtil.BCD2String_BE(bs, index, index) ;
        index++ ;
        String mm = ByteUtil.BCD2String_BE(bs, index, index) ;
        index++ ;
        String yy = ByteUtil.BCD2String_BE(bs, index, index) ;
        return "20" + yy + "-" + mm + "-" + dd + " " + HH + ":" + MM + ":" + ss ;
    }
   /**
     * 分析IC卡编号
     * @param bs 上行字节数组
     * @param index 启始位
     * @param len1 第一部分字节数
     * @param len2 第二部分字节数
     * @return 控制器地址
     * @throws Exception 异常
     */
    public static String parseIcCardNo(byte[] bs, int index, int len1, int len2)throws Exception{
        String rtuAddrBCD = "" + ByteUtil.BCD2Long_BE(bs, index, index + len1 - 1) ;
        String rtuAddrStr = "" + ByteUtilUnsigned.bytes2Short_LE(bs, index + len1) ;
        while(rtuAddrStr.length() < 5){
            rtuAddrStr = "0" + rtuAddrStr ;
        }
        return rtuAddrBCD + rtuAddrStr ;
    }
 
 
    /**
     * 分析状态与报警
     * @param bs 上报数据的字节数组
     * @param index 当前解析数组下标位置
     * @return
     */
    public static void parseAlarmAndState(byte[] bs, int index, AlarmVo vo){
        byte b = bs[index] ;
        vo.statePump = (byte)(b & 0x1) ;
        vo.alarmWaterRemain = (byte)((b >> 1) & 1) ;
        vo.alarm0WaterRemain = (byte)((b >> 2) & 1) ;
        vo.alarmExceedYear = (byte)((b >> 3) & 1) ;
        vo.alarmWaterMeterFault = (byte)((b >> 4) & 1) ;
        vo.alarmSteal = (byte)((b >> 5) & 1) ;
        vo.alarmWaterMeterBreak = (byte)((b >> 6) & 1) ;
        vo.alarmEleMeterFault = (byte)((b >> 7) & 1) ;
 
        b = bs[index + 1] ;
        vo.alarm0MoneyRemain = (byte)(b & 0x1) ;
        vo.alarmDoor = (byte)((b >> 1) & 1) ;
        vo.alarmBatteryVolt = (byte)((b >> 2) & 1) ;
        vo.alarmEleMiss = (byte)((b >> 3) & 1) ;
        vo.alarmEleExceed = (byte)((b >> 4) & 1) ;
        vo.alarmEleLowVolt = (byte)((b >> 5) & 1) ;
        vo.stateIcEnable = (byte)((b >> 6) & 1) ;
        vo.stateOpClValve = (byte)((b >> 7) & 1) ;
 
        b = bs[index + 2] ;
        vo.alarmBlockValve = (byte)(b & 0x1) ;
    }
 
    public static String parseTp(byte[] bs, short index){
        String dt = "" ;
        try{
            dt = dt + ByteUtil.BCD2String_BE(bs, index, index) ;//秒
            index++ ;
            dt = ByteUtil.BCD2String_BE(bs, index, index) + ":" + dt ;//分
            index++ ;
            dt = ByteUtil.BCD2String_BE(bs, index, index) + ":" + dt ;//时
            index++ ;
            dt = ByteUtil.BCD2String_BE(bs, index, index) + " " + dt ;//日
            index++ ;
            bs[index] = (byte)(bs[index] & 0x1F);
            dt = ByteUtil.BCD2String_BE(bs, index, index) + "-" + dt ;//月
            index++ ;
            dt = "20" + ByteUtil.BCD2String_BE(bs, index, index) + "-" + dt ;//年
        }catch (Exception e){
            log.error("解析控制器时钟时异常:" + e.getMessage(), e);
        }
        return dt ;
    }
 
    /**
     * 分析功能80和5B数据
     * @param bs
     * @param cdData
     * @throws Exception
     */
    public static void parseCd80And5B(byte[] bs, DataCd80_5BVo cdData) throws Exception{
        int index = ProtocolConstantV206V202404.dataIndex ;
        cdData.controllerType = ByteUtil.bytes2Hex(bs, false, index, 1) ;
 
        index++ ;
        cdData.projectNo = (int)bs[index];
 
        index++ ;
        cdData.useWaterType = bs[index];
 
        index++ ;
        cdData.orderNo = ByteUtil.BCD2String_BE(bs, index, index + 7) ;
 
        index += 8 ;
        cdData.icCardAddr = ByteUtil.bytes2Hex(bs, false, index, 4) ;
 
        index += 4 ;
        cdData.icCardNo  = GlParse.parseIcCardNo(bs, index, 6, 2) ;
 
        index += 8 ;
        cdData.rtuDt  = GlParse.parseRtuDt(bs, index) ;
 
        index += 6 ;
        Long waterLevel = ByteUtil.BCD2Long_LE(bs, index, index + 3) ;
        cdData.waterLevel = waterLevel/1000.0D;
 
        index += 4 ;
        int waterPress = ByteUtil.BCD2Int_LE(bs, index, index + 2) ;
        cdData.waterPress = waterPress/100.0D;
 
        index += 3 ;
        Long waterInstant = ByteUtil.BCD2Long_LE(bs, index, index + 3) ;
        cdData.waterInstant = waterInstant/100.0D;
 
        index += 4 ;
        Long waterTotal = ByteUtil.BCD2Long_LE(bs, index, index + 4) ;
        cdData.waterTotal = waterTotal/100.0D;
 
        index += 5 ;
        Long eleTotal = ByteUtil.BCD2Long_LE(bs, index, index + 4) ;
        cdData.eleTotal = eleTotal/100.0D;
 
        index += 5 ;
        Long moneyRemain = ByteUtil.BCD2Long_LE(bs, index, index + 4) ;
        cdData.moneyRemain = moneyRemain/10000.0D;
 
        index += 5 ;
        Long waterRemain = ByteUtil.BCD2Long_LE(bs, index, index + 4) ;
        cdData.waterRemain = waterRemain/100.0D;
 
        index += 5 ;
        int thisEle = ByteUtil.BCD2Int_LE(bs, index, index + 3) ;
        cdData.thisEle = thisEle/100.0D;
 
        index += 4 ;
        int thisWater = ByteUtil.BCD2Int_LE(bs, index, index + 3) ;
        cdData.thisWater = thisWater/100.0D;
 
        index += 4 ;
        int thisMoney = ByteUtil.BCD2Int_LE(bs, index, index + 3) ;
        cdData.thisMoney = thisMoney/10000.0D;
 
        index += 4 ;
        cdData.thisDuration = ByteUtil.BCD2Int_LE(bs, index, index + 1) ;
 
        index += 2 ;
        int waterPrice = ByteUtil.BCD2Int_LE(bs, index, index + 1) ;
        cdData.waterPrice = waterPrice/100.0D;
 
        index += 2 ;
        int elePrice = ByteUtil.BCD2Int_LE(bs, index, index + 1) ;
        cdData.elePrice = elePrice/100.0D;
 
        index += 2 ;
        GlParse.parseAlarmAndState(bs, index, cdData) ;//四个字节
 
        index += 4 ;
        int aVolt = ByteUtil.BCD2Int_LE(bs, index, index + 1) ;
        cdData.aVolt = aVolt/10.0D;
 
        index += 2 ;
        int bVolt = ByteUtil.BCD2Int_LE(bs, index, index + 1) ;
        cdData.bVolt = bVolt/10.0D;
 
        index += 2 ;
        int cVolt = ByteUtil.BCD2Int_LE(bs, index, index + 1) ;
        cdData.cVolt = cVolt/10.0D;
 
        index += 2 ;
        int aCurrent = ByteUtil.BCD2Int_LE(bs, index, index + 1) ;
        cdData.aCurrent = aCurrent/10.0D;
 
        index += 2 ;
        int bCurrent = ByteUtil.BCD2Int_LE(bs, index, index + 1) ;
        cdData.bCurrent = bCurrent/10.0D;
 
        index += 2 ;
        int cCurrent = ByteUtil.BCD2Int_LE(bs, index, index + 1) ;
        cdData.cCurrent = cCurrent/10.0D;
 
        index += 2 ;
        int batteryVolt = ByteUtil.BCD2Int_LE(bs, index, index + 1) ;
        cdData.batteryVolt = batteryVolt/100.0D;
 
        index += 2 ;
        int waterMeterTransVolt = ByteUtil.BCD2Int_LE(bs, index, index + 1) ;
        cdData.waterMeterTransVolt = waterMeterTransVolt/100.0D;
 
        index += 2 ;
        int waterMeterWorkVolt = ByteUtil.BCD2Int_LE(bs, index, index + 1) ;
        cdData.waterMeterWorkVolt = waterMeterWorkVolt/100.0D;
 
        index += 2 ;
        cdData.csq = ByteUtil.BCD2Int_LE(bs, index, index) ;
 
    }
 
 
    /**
     * 分析功能85数据
     * @param bs
     * @param cdData
     * @throws Exception
     */
    public static void parseCd85(byte[] bs, DataCd85Vo cdData) throws Exception {
        int index = ProtocolConstantV206V202404.dataIndex ;
        cdData.controllerType = ByteUtil.bytes2Hex(bs, false, index, 1) ;
 
        index++ ;
        cdData.projectNo = (int)bs[index];
 
        index ++ ;
        cdData.clType = bs[index];
        //进行兼容转换
        cdData.clType = CommonV202404.closeType2P206V1(cdData.clType) ;
 
        index++ ;
        cdData.icCardAddr = ByteUtil.bytes2Hex(bs, false, index, 4) ;
 
        index += 4 ;
        cdData.icCardNo = GlParse.parseIcCardNo(bs, index, 6, 2) ;
 
        index += 8 ;
        cdData.orderNo = ByteUtil.BCD2String_BE(bs, index, index + 7) ;
 
        index += 8 ;
        cdData.startDt  = GlParse.parseRtuDt(bs, index) ;
 
        index += 6 ;
        cdData.endDt  = GlParse.parseRtuDt(bs, index) ;
 
        index += 6 ;
        cdData.waterTotalAmount = ByteUtil.BCD2Long_LE(bs, index, index + 4)/100.0D ;
 
        index += 5 ;
        cdData.eleTotalAmount = ByteUtil.BCD2Long_LE(bs, index, index + 4)/100.0D ;
 
        index += 5 ;
        cdData.moneyRemain = ByteUtil.BCD2Long_LE(bs, index, index + 4)/10000.0D ;
 
        index += 5 ;
        cdData.waterRemain = ByteUtil.BCD2Long_LE(bs, index, index + 4)/100.0D ;
 
        index += 5 ;
        cdData.thisEle = ByteUtil.BCD2Long_LE(bs, index, index + 3)/100.0D ;
 
        index += 4 ;
        cdData.thisWater = ByteUtil.BCD2Long_LE(bs, index, index + 3)/100.0D ;
 
        index += 4 ;
        cdData.thisMoney = ByteUtil.BCD2Long_LE(bs, index, index + 3)/10000.0D ;
 
        index += 4 ;
        cdData.thisDuration = ByteUtil.BCD2Int_LE(bs, index, index + 1) ;
 
        index += 2 ;
        GlParse.parseAlarmAndState(bs, index, cdData) ;//四个字节
 
        index += 4 ;
        cdData.price = ByteUtil.BCD2Int_LE(bs, index, index + 1)/100.0 ;
 
    }
    /**
     * 分析功能93、A3数据
     * @param bs
     * @param cdData
     * @throws Exception
     */
    public static void parseCd93A3(byte[] bs, DataCd93_A3Vo cdData) throws Exception {
        int index = ProtocolConstantV206V202404.dataIndex ;
        cdData.controllerType = ByteUtil.bytes2Hex(bs, false, index, 1) ;
 
        index++ ;
        cdData.projectNo = (int)bs[index];
 
        index ++ ;
        cdData.clResult = bs[index];
 
        index ++ ;
        cdData.clType = bs[index];
 
        index++ ;
        cdData.icCardAddr = ByteUtil.bytes2Hex(bs, false, index, 4) ;
 
        index += 4 ;
        cdData.icCardNo = GlParse.parseIcCardNo(bs, index, 6, 2) ;
 
        index += 8 ;
        cdData.orderNo = ByteUtil.BCD2String_BE(bs, index, index + 7) ;
 
        index += 8 ;
        cdData.startDt  = GlParse.parseRtuDt(bs, index) ;
 
        if(cdData.clResult == (byte)0x81){
            //失败了,下面就没有数据了
            return ;
        }
 
        index += 6 ;
        cdData.endDt  = GlParse.parseRtuDt(bs, index) ;
 
        index += 6 ;
        cdData.waterTotalAmount = ByteUtil.BCD2Long_LE(bs, index, index + 4)/100.0D ;
 
        index += 5 ;
        cdData.eleTotalAmount = ByteUtil.BCD2Long_LE(bs, index, index + 4)/100.0D ;
 
        index += 5 ;
        cdData.moneyRemain = ByteUtil.BCD2Long_LE(bs, index, index + 4)/100.0D ;
 
        index += 5 ;
        cdData.waterRemain = ByteUtil.BCD2Long_LE(bs, index, index + 4)/100.0D ;
 
        index += 5 ;
        cdData.thisEle = ByteUtil.BCD2Long_LE(bs, index, index + 3)/100.0D ;
 
        index += 4 ;
        cdData.thisWater = ByteUtil.BCD2Long_LE(bs, index, index + 3)/100.0D ;
 
        index += 4 ;
        cdData.thisMoney = ByteUtil.BCD2Long_LE(bs, index, index + 3)/100.0D ;
 
        index += 4 ;
        cdData.thisDuration = ByteUtil.BCD2Int_LE(bs, index, index + 1) ;
 
        index += 2 ;
        GlParse.parseAlarmAndState(bs, index, cdData) ;//四个字节
    }
}