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
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
package com.dev.electlock.data;
 
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.util.Date;
 
import com.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 void resetLockMacAddr() {
        for(int n=0;n<mac_addr.length;n++) {
            mac_addr[n] = 0xFF;
        }
    }
    
    public String getLockState() {
        String str = "±ÕËø";
        if(lock_state == 1) {
            str = "¿ªËø";
        }
        return str;
    }
    
    public String getWorkModel() {
        String str = "δ֪ģʽ";
        if(workmodel == 1) {
            str = "ÔÚÏßģʽ";
        }else if(workmodel == 0){
            str = "ÀëÏßģʽ";
        }
        return str;
    }
    
 
    public Object getUnlockType() {
        String str = "δ֪-" + unlock_type;
        switch (unlock_type) {
            case Unlock_Type_485:{
                str = "RS485¿ªËø";
            }break;
            case Unlock_Type_ID:{
                str = "Ë¢¿¨¿ªËø";
            }break;
            case Unlock_Type_Bluetooth:{
                str = "À¶ÑÀ¿ªËø";
            }break;
            case Unlock_Type_DI:{
                str = "DI¿ªËø";
            }break;
        }
        return str;
    }
    
    
    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());
    }
 
 
}