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
package com.dev.lock.data;
 
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.util.Date;
 
import com.base.ComBase;
 
public class ElectLock_State {
    public static final int Unlock_Type_485         = 0;    //485
    public static final int Unlock_Type_ID             = 1;    //ID卡
    public static final int Unlock_Type_Bluetooth     = 2;    //蓝牙
    public static final int Unlock_Type_DI             = 3;    //DI
    
    
//    public static int Reg_Count_Real = 8;        //寄存器数量
//    public static int Reg_Count_Real = 11;        //寄存器数量
    public static int Reg_Count_Real = 12;        //寄存器数量
    
    private Date record_time = new Date(0);            //'更新时间',
    private String gprs_sn;                //'gprs模块SN码',
    private String client_ip;            //'设备IP地址',
    private int already_id_count;        //'当前锁已授权卡数量',
    private int max_id_count;            //'授权卡数量存储上限',
    private int lock_state;                //'电子锁状态[0-关 1-开]',
    private String lock_version;        //'软件版本号',
    private int unlock_type;            //'上一次开锁方式[0-485开锁 1-刷卡开锁 2-蓝牙开锁 3-DI开锁]',
    private int unlock_id;                //'上一次刷开开锁卡号[仅刷开开锁时有效]',
    private int lock_addr;                //'设备地址',
    private int comm_count;                //'通信计数',
    private int err_tol_count;            //'总错误计数',
    private int err_count;                //'连续错误计数',
    private int op_cmd;                    //'控制命令',
    private int id_card_set;            //'授权ID卡号',
    private int lock_addr_set;            //'设置设备地址[慎用]',
    private int workmodel;                //工作模式【0-离线模式   1-在线模式】
    
    private int lock_online;            //锁具在线状态[0-离线  1-在线]
    
    private boolean isIDOpen = false;    //是否是ID开锁
    private int last_unlock_type;        //'上一次开锁方式[0-485开锁 1-刷卡开锁 2-蓝牙开锁 3-DI开锁]',
    private int last_unlock_id;            //'上一次刷开开锁卡号[仅刷开开锁时有效]',
    
    private int[] mac_addr = new int[6]; //锁具蓝牙MAC地址
 
    private String ctl_uname = "";        //操作用户名
    
    
    private int lock_open_count;    //'锁具总开启次数'
    private Date last_update_time;    //上一次锁具开启或关闭时间
    
    public ElectLock_State() {
        
    }
 
    
    public boolean putByteBuffer(ByteBuffer buffer) {
        if(buffer.limit() < (Reg_Count_Real*2)) {
            System.out.println("实时信息返回长度错误" + buffer.limit());
            return false;
        }
        
        buffer.order(ByteOrder.BIG_ENDIAN);
        buffer.position(0);
        
        this.already_id_count = ComBase.changeShortToInt(buffer.getShort());        //'当前锁已授权卡数量',
        this.max_id_count = ComBase.changeShortToInt(buffer.getShort());            //'授权卡数量存储上限',
        int tmp_lock_state = lock_state;
        this.lock_state = ComBase.changeShortToInt(buffer.getShort());                //'电子锁状态[0-关 1-开]',
        int ver = ComBase.changeShortToInt(buffer.getShort());
        this.lock_version = " V"+ ver;                                                //'软件版本号',
        
        
        this.unlock_type = ComBase.changeShortToInt(buffer.getShort());                //'上一次开锁方式[0-485开锁 1-刷卡开锁 2-蓝牙开锁 3-DI开锁]',
        this.unlock_id = (buffer.getInt()&0xFFFFFFFF);                                //'上一次刷开开锁卡号[仅刷开开锁时有效]',
        this.lock_addr = ComBase.changeShortToInt(buffer.getShort());                //'设备地址',
        
        for(int k =0;k<mac_addr.length;k++) {
            mac_addr[k] = ComBase.changeByteToInt(buffer.get());
        }
        workmodel  = ComBase.changeShortToInt(buffer.getShort()); 
        
        System.out.println("工作模式:" + workmodel);
//        System.out.println("BlutoothMAC:" + getLockMacAddr());
        
        //System.out.println("this.lock_state:"+tmp_lock_state+"********lock_state:"+lock_state);
        if(this.lock_state != tmp_lock_state) {
            if(this.lock_state == 1) {                
                //锁具状态变化;锁从关闭变为打开
                if(this.unlock_type == Unlock_Type_ID) {
                    //ID最近ID卡开锁
                    isIDOpen = true;
                    last_unlock_type = unlock_type;        //'上一次开锁方式[0-485开锁 1-刷卡开锁 2-蓝牙开锁 3-DI开锁]',
                    last_unlock_id = unlock_id;            //'上一次刷开开锁卡号[仅刷开开锁时有效]',
                }
                this.lock_open_count ++;
            }
            //锁具状态变化
            this.last_update_time = new Date();
        }
        record_time = new Date();
        buffer.compact();
        return true;
    }
    
    public String getLockMacAddr() {
        return String.format("%02x:%02x:%02x:%02x:%02x:%02x", mac_addr[0],mac_addr[1],mac_addr[2],mac_addr[3],mac_addr[4],mac_addr[5]).toUpperCase();
    }
    
    public int getLockDevId() {
        return 10000000 + this.lock_addr;
    }
    
    public Date getRecord_time() {
        return record_time;
    }
 
    public String getGprs_sn() {
        return gprs_sn;
    }
 
    public String getClient_ip() {
        return client_ip;
    }
 
    public int getAlready_id_count() {
        return already_id_count;
    }
 
    public String getCtl_uname() {
        return ctl_uname;
    }
    public int getLock_open_count() {
        return lock_open_count;
    }
 
    public Date getLast_update_time() {
        return last_update_time;
    }
 
    public void setLock_open_count(int lock_open_count) {
        this.lock_open_count = lock_open_count;
    }
 
    public void setLast_update_time(Date last_update_time) {
        this.last_update_time = last_update_time;
    }
 
    public void setCtl_uname(String ctl_uname) {
        this.ctl_uname = ctl_uname;
    }
 
 
    public int getWorkmodel() {
        return workmodel;
    }
 
 
    public void setWorkmodel(int workmodel) {
        this.workmodel = workmodel;
    }
 
 
    public int getMax_id_count() {
        return max_id_count;
    }
 
    public int getLock_state() {
        return lock_state;
    }
 
    public String getLock_version() {
        return lock_version;
    }
 
    public int getUnlock_type() {
        return unlock_type;
    }
 
    public int getUnlock_id() {
        return unlock_id;
    }
 
    public int getLock_addr() {
        return lock_addr;
    }
 
    public int getComm_count() {
        return comm_count;
    }
 
    public int getErr_tol_count() {
        return err_tol_count;
    }
 
    public int getErr_count() {
        return err_count;
    }
 
    public int getLock_online() {
        return lock_online;
    }
 
 
    public void setLock_online(int lock_online) {
        this.lock_online = lock_online;
    }
 
 
    public int getOp_cmd() {
        return op_cmd;
    }
 
    public int getId_card_set() {
        return id_card_set;
    }
 
    public int getLock_addr_set() {
        return lock_addr_set;
    }
 
    public void setRecord_time(Date record_time) {
        this.record_time = record_time;
    }
 
    public void setGprs_sn(String gprs_sn) {
        this.gprs_sn = gprs_sn;
    }
 
    public void setClient_ip(String client_ip) {
        this.client_ip = client_ip;
    }
 
    public void setAlready_id_count(int already_id_count) {
        this.already_id_count = already_id_count;
    }
 
    public void setMax_id_count(int max_id_count) {
        this.max_id_count = max_id_count;
    }
 
    public void setLock_state(int lock_state) {
        this.lock_state = lock_state;
    }
 
    public void setLock_version(String lock_version) {
        this.lock_version = lock_version;
    }
 
    public void setUnlock_type(int unlock_type) {
        this.unlock_type = unlock_type;
    }
 
    public void setUnlock_id(int unlock_id) {
        this.unlock_id = unlock_id;
    }
 
    public void setLock_addr(int lock_addr) {
        this.lock_addr = lock_addr;
    }
 
    public void setComm_count(int comm_count) {
        this.comm_count = comm_count;
    }
 
    public void setErr_tol_count(int err_tol_count) {
        this.err_tol_count = err_tol_count;
    }
 
    public void setErr_count(int err_count) {
        this.err_count = err_count;
    }
 
    public void setOp_cmd(int op_cmd) {
        this.op_cmd = op_cmd;
    }
 
    public void setId_card_set(int id_card_set) {
        this.id_card_set = id_card_set;
    }
 
    public void setLock_addr_set(int lock_addr_set) {
        this.lock_addr_set = lock_addr_set;
    }
 
    public boolean isIDOpen() {
        return isIDOpen;
    }
 
 
    public int getLast_unlock_type() {
        return last_unlock_type;
    }
 
 
    public int getLast_unlock_id() {
        return last_unlock_id;
    }
 
 
    public void setIDOpen(boolean isIDOpen) {
        this.isIDOpen = isIDOpen;
    }
 
 
    public void setLast_unlock_type(int last_unlock_type) {
        this.last_unlock_type = last_unlock_type;
    }
 
 
    public void setLast_unlock_id(int last_unlock_id) {
        this.last_unlock_id = last_unlock_id;
    }
 
 
    public void addCommCount() {
        this.comm_count ++ ;
        if(this.comm_count > 999990000) {
            this.comm_count = 0;
        }
    }
 
    public void clearErrorCount() {
        this.err_count = 0;
    }
 
    public void addErrorCount() {
        this.err_tol_count ++ ;
        if(this.err_tol_count > 999990000) {
            this.err_tol_count = 0;
        }        
        err_count++;
    }
 
    @Override
    public String toString() {
        return "ElectLock_State [record_time=" + record_time + ", gprs_sn=" + gprs_sn + ", client_ip=" + client_ip
                + ", already_id_count=" + already_id_count + ", max_id_count=" + max_id_count + ", lock_state="
                + lock_state + ", lock_version=" + lock_version + ", unlock_type=" + unlock_type + ", unlock_id="
                + unlock_id + ", lock_addr=" + lock_addr + ", comm_count=" + comm_count + ", err_tol_count="
                + err_tol_count + ", err_count=" + err_count + ", op_cmd=" + op_cmd + ", id_card_set=" + id_card_set
                + ", lock_addr_set=" + lock_addr_set + "]";
    }
    
    
    public static void main(String[] args) {
        int[] ss = new int[] {1,58,65,21,54,23};
        System.out.println(String.format("%02x:%02x:%02x:%02x:%02x:%02x", ss[0],ss[1],ss[2],ss[3],ss[4],ss[5]).toUpperCase());
    }
}