左晓为主开发手持机充值管理机
zuoxiao
2024-04-25 8d53327d1b3df1aea5f9eb9fa22c644ccfeeabf1
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
package com.dayu.qihealonelibrary.card;
 
import com.dayu.baselibrary.tools.HexUtil;
import com.dayu.qihealonelibrary.utils.MyCommon;
import com.tencent.bugly.crashreport.CrashReport;
 
import java.io.Serializable;
import java.util.Calendar;
import java.util.List;
 
/**
 * Copyright (C), 2023,
 * Author: zuo
 * Date: 2023-11-07 9:37
 * Description: 齐河项目用户卡结构
 */
public class UserCard implements Serializable {
    public String cardType = MyCommon.USER_CARD_TYPE_1;//写卡标志 A1刷卡开泵前 A8刷卡开泵后  A2叠加充值
 
    public short arerNumber;//区域号(底位在前高位在后)
 
    public short deviceNumberl;//设备编号(底位在前高位在后)
    public byte rechargeTimes;//充值次数
 
    public int totalWater;//用户总用水量  底位在前,高位在后2位小数点  含两个小数点的整数
 
    public int totalElectric;//总用电量位 底位在前,高位在后  1位小数点  含1位小数点的整数
 
    public int balance;//剩余金额 底位在前  2位小数点,单位元
 
    public int surplusWater;//剩余水量 底位在前  2位小数点 单位立方米
 
    public Calendar rechargeDate;// 购水时间 BCD
 
 
    /**
     * 通过byte转bean
     *
     * @param data
     */
    public static UserCard getBean(List<byte[]> data) {
        try {
            UserCard userCard = new UserCard();
            //第0块解析
            byte[] zero = data.get(0);
 
            userCard.cardType = HexUtil.byteToHex(zero[0]);
 
            byte[] arerNumberByte = new byte[2];
            System.arraycopy(zero, 1, arerNumberByte, 0, arerNumberByte.length);
            userCard.arerNumber = (short) HexUtil.get16to10LowHigh(HexUtil.bytesToHex(arerNumberByte));
 
            byte[] deviceNumberlByte = new byte[2];
            System.arraycopy(zero, 3, deviceNumberlByte, 0, deviceNumberlByte.length);
            userCard.deviceNumberl = (short) HexUtil.get16to10LowHigh(HexUtil.bytesToHex(deviceNumberlByte));
 
            userCard.rechargeTimes = HexUtil.hexToByte(HexUtil.byteToHex(zero[5]));
 
            byte[] totalWaterByte = new byte[4];
            System.arraycopy(zero, 3, totalWaterByte, 0, totalWaterByte.length);
            userCard.totalWater = HexUtil.get16to10LowHigh(HexUtil.bytesToHex(totalWaterByte));
 
            byte[] totalElectricByte = new byte[4];
            System.arraycopy(zero, 3, totalElectricByte, 0, totalElectricByte.length);
            userCard.totalElectric = HexUtil.get16to10LowHigh(HexUtil.bytesToHex(totalElectricByte));
 
            //第1块解析
            byte[] one = data.get(1);
 
            byte[] balanceByte = new byte[4];
            System.arraycopy(one, 0, balanceByte, 0, balanceByte.length);
            userCard.balance = HexUtil.get16to10LowHigh(HexUtil.bytesToHex(balanceByte));
 
            byte[] surplusWaterByte = new byte[4];
            System.arraycopy(one, 4, surplusWaterByte, 0, surplusWaterByte.length);
            userCard.surplusWater = HexUtil.get16to10LowHigh(HexUtil.bytesToHex(surplusWaterByte));
 
            byte[] rechargeDateByte = new byte[3];
            System.arraycopy(one, 12, rechargeDateByte, 0, rechargeDateByte.length);
 
            int year = HexUtil.getBcdToInt(one[12]);
            int month = HexUtil.getBcdToInt(one[13]);
            int day = HexUtil.getBcdToInt(one[14]);
 
            Calendar calendar = Calendar.getInstance();
            calendar.set(2000 + year, month, day, 0, 0, 0);
            userCard.rechargeDate = calendar;
 
            return userCard;
 
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
 
    }
 
 
    /**
     * 用户卡0块
     */
    public class Zero extends BaseCard {
        public byte[] toByte() {
            try {
                byte[] data = new byte[16];
                data[0] = HexUtil.hexToByte(cardType);
                byte[] arerNumberBytes = HexUtil.hexToByteArray(HexUtil.get10To16LowHigh(arerNumber));
                if (arerNumberBytes != null) {
                    System.arraycopy(arerNumberBytes, 0, data, 1, arerNumberBytes.length);
                }
                byte[] deviceNumberlBytes = HexUtil.hexToByteArray(HexUtil.get10To16LowHigh(deviceNumberl));
                if (deviceNumberlBytes != null) {
                    System.arraycopy(deviceNumberlBytes, 0, data, 3, deviceNumberlBytes.length);
                }
                byte rechargeTimesByte = HexUtil.hexToByte(HexUtil.get10to16(rechargeTimes));
                data[5] = rechargeTimesByte;
                byte[] totalWaterBytes = HexUtil.hexToByteArray(HexUtil.get10To16LowHigh(totalWater));
                if (totalWaterBytes != null) {
                    System.arraycopy(totalWaterBytes, 0, data, 6, totalWaterBytes.length);
                }
                byte[] totalElectricBytes = HexUtil.hexToByteArray(HexUtil.get10To16LowHigh(totalElectric));
                if (totalElectricBytes != null) {
                    System.arraycopy(totalElectricBytes, 0, data, 10, totalElectricBytes.length);
                }
                data[15] = getByteSum(data);
                return data;
            } catch (Exception e) {
                e.printStackTrace();
            }
 
            return null;
        }
 
    }
 
    /**
     * 用户卡1块
     */
    public class One extends BaseCard {
 
        public byte[] toBytes() {
            byte[] data = new byte[16];
 
 
            try {
                byte[] balanceBytes = HexUtil.hexToByteArray(HexUtil.get10To16LowHigh(balance));
                if (balanceBytes != null) {
                    System.arraycopy(balanceBytes, 0, data, 0, balanceBytes.length);
                }
 
                byte[] surplusWaterBytes = HexUtil.hexToByteArray(HexUtil.get10To16LowHigh(surplusWater));
                if (surplusWaterBytes != null) {
                    System.arraycopy(surplusWaterBytes, 0, data, 0, surplusWaterBytes.length);
                }
 
 
                if (rechargeDate != null) {
                    // 获取年、月、日、时、分、秒
                    int year = (rechargeDate.get(Calendar.YEAR)) % 1000;
                    int month = rechargeDate.get(Calendar.MONTH) + 1; // 月份从0开始,所以需要加1
                    int day = rechargeDate.get(Calendar.DAY_OF_MONTH);
                    byte bcdYear = (byte) ((year / 10) << 4 | ((year % 10) / 100));
                    byte bcdMonth = (byte) ((month / 10) << 4 | (month % 10));
                    byte bcdDay = (byte) ((day / 10) << 4 | (day % 10));
                    data[12] = bcdYear;
                    data[13] = bcdMonth;
                    data[14] = bcdDay;
                }
 
                data[15] = getByteSum(data);
            } catch (Exception e) {
                e.printStackTrace();
                CrashReport.postCatchedException(e);
            }
            return data;
        }
 
 
    }
 
    /**
     * 用户卡2块
     */
    public class Two extends BaseCard {
        public byte[] toBytes() {
            byte[] data = new byte[16];
 
            try {
                byte[] balanceBytes = HexUtil.hexToByteArray(HexUtil.get10To16LowHigh(balance));
                if (balanceBytes != null) {
                    System.arraycopy(balanceBytes, 0, data, 0, balanceBytes.length);
                }
 
                byte[] surplusWaterBytes = HexUtil.hexToByteArray(HexUtil.get10To16LowHigh(surplusWater));
                if (surplusWaterBytes != null) {
                    System.arraycopy(surplusWaterBytes, 0, data, 0, surplusWaterBytes.length);
                }
 
                if (rechargeDate != null) {
                    // 获取年、月、日、时、分、秒
                    int year = (rechargeDate.get(Calendar.YEAR)) % 1000;
                    int month = rechargeDate.get(Calendar.MONTH) + 1; // 月份从0开始,所以需要加1
                    int day = rechargeDate.get(Calendar.DAY_OF_MONTH);
                    byte bcdYear = (byte) ((year / 10) << 4 | ((year % 10) / 100));
                    byte bcdMonth = (byte) ((month / 10) << 4 | (month % 10));
                    byte bcdDay = (byte) ((day / 10) << 4 | (day % 10));
                    data[12] = bcdYear;
                    data[13] = bcdMonth;
                    data[14] = bcdDay;
                }
 
                data[15] = getByteSum(data);
            } catch (Exception e) {
                e.printStackTrace();
                CrashReport.postCatchedException(e);
            }
            return data;
        }
    }
 
    public byte[] getZeroBytes() {
        Zero zero = new Zero();
        return zero.toByte();
    }
 
    public byte[] getOneBytes() {
        One zero = new One();
        return zero.toBytes();
    }
 
    public byte[] getTwoBytes() {
        Two zero = new Two();
        return zero.toBytes();
    }
 
 
    public String getCardType() {
        return cardType;
    }
 
    public void setCardType(String cardType) {
        this.cardType = cardType;
    }
 
    public short getArerNumber() {
        return arerNumber;
    }
 
    public void setArerNumber(short arerNumber) {
        this.arerNumber = arerNumber;
    }
 
    public short getDeviceNumberl() {
        return deviceNumberl;
    }
 
    public void setDeviceNumberl(short deviceNumberl) {
        this.deviceNumberl = deviceNumberl;
    }
 
    public byte getRechargeTimes() {
        return rechargeTimes;
    }
 
    public void setRechargeTimes(byte rechargeTimes) {
        this.rechargeTimes = rechargeTimes;
    }
 
    public int getTotalWater() {
        return totalWater;
    }
 
    public void setTotalWater(int totalWater) {
        this.totalWater = totalWater;
    }
 
    public int getTotalElectric() {
        return totalElectric;
    }
 
    public void setTotalElectric(int totalElectric) {
        this.totalElectric = totalElectric;
    }
 
    public int getBalance() {
        return balance;
    }
 
    public void setBalance(int balance) {
        this.balance = balance;
    }
 
    public int getSurplusWater() {
        return surplusWater;
    }
 
    public void setSurplusWater(int surplusWater) {
        this.surplusWater = surplusWater;
    }
 
    public Calendar getRechargeDate() {
        return rechargeDate;
    }
 
    public void setRechargeDate(Calendar rechargeDate) {
        this.rechargeDate = rechargeDate;
    }
 
 
}