aboutsummaryrefslogtreecommitdiff
path: root/src/org/traccar/protocol/St210ProtocolDecoder.java
blob: 5e1ac1bf6f6d65cdeed79c17c761bbccd889efd7 (plain)
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
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
package org.traccar.protocol;

import java.util.Calendar;
import java.util.LinkedList;
import java.util.List;
import java.util.TimeZone;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.jboss.netty.channel.Channel;
import org.jboss.netty.channel.ChannelHandlerContext;
import org.traccar.BaseProtocolDecoder;
import org.traccar.ServerManager;
import org.traccar.helper.Log;
import org.traccar.model.DataManager;
import org.traccar.model.ExtendedInfoFormatter;
import org.traccar.model.Position;

public class St210ProtocolDecoder extends BaseProtocolDecoder {


    public St210ProtocolDecoder(ServerManager serverManager) {
        super(serverManager);
    }

    private enum ST210FIELDS {
                HDR_STATUS("SA200STT;","Status Report"),
                HDR_EMERGENCY("SA200EMG;","Emergency Report"),
                HDR_EVENT("SA200EVT;", "Event Report"),
                HDR_ALERT("SA200ALT;","Alert Report"),
                HDR_ALIVE("SA200ALV;","Alive Report"),
                DEV_ID("(\\d+);", "Device ID"),
                SW_VER("(\\d{3});", "Software Release Version"),
                DATE("(\\d+);","GPS date (yyyymmdd) Year + Month + Day"),
                TIME("(\\d{2}:\\d{2}:\\d{2});","GPS time (hh:mm:ss) Hour : Minute : Second"),
                CELL("(\\w+);","Location Code ID (3 digits hex) + Serving Cell BSIC(2 digits decimal)"),
                LAT("(-\\d{2}.\\d+);", "Latitude (+/-xx.xxxxxx)"),
                LON("(-\\d{3}.\\d+);", "Longitude (+/-xxx.xxxxxx)"),
                SPD("(\\d{3}.\\d{3});","Speed in km/h - This value returns to 0 when it is over than 200,000Km"),
                CRS("(\\d{3}.\\d{2});", "Course over ground in degree"),
                SATT("(\\d+);", "Number of satellites"),
                FIX("(\\d);","GPS is fixed(1)\n" + "GPS is not fixed(0)"),
                DIST("(\\d+);","Traveled ddistance in meter"),
                PWR_VOLT("(\\d+.\\d{2});","Voltage value of main power"),
                IO("(\\d+);","Current I/O status of inputs and outputs."),
                MODE("(\\d);","1 = Idle mode (Parking)\n" + "2 = Active Mode (Driving)"),
                MSG_NUM("(\\d{4});","Message number - After 9999 is reported, message number returns to 0000"),
                EMG_ID("(\\d);", "Emergency type"),
                EVT_ID("(\\d);", "Event type"),
                ALERT_ID("(\\d+);", "Alert type");

        private String pattern;

        private String desc;

        private ST210FIELDS(String pattern, String desc) {
            this.pattern = pattern;
            this.desc = desc;
        }

        public String getPattern() {
            return pattern;
        }

        public String getDesc() {
            return desc;
        }

        public void populatePosition(Position position, String groupValue,
                DataManager dataManager) throws Exception {

            switch (this) {

            case DEV_ID:
                position.setDeviceId(dataManager.getDeviceByImei(groupValue)
                        .getId());
                break;

            case LAT:
                position.setLatitude(Double.valueOf(groupValue));
                break;

            case LON:
                position.setLongitude(Double.valueOf(groupValue));
                break;

            case CRS:
                position.setCourse(Double.valueOf(groupValue));
                break;

            case PWR_VOLT:
                position.setPower(Double.valueOf(groupValue));
                break;

            case SPD:
                position.setSpeed(Double.valueOf(groupValue));
                break;

            case MODE:
                //position.setMode(Integer.parseInt(groupValue));
                break;

            case DATE: {
                // Date
                Calendar time = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
                time.clear();
                time.set(Calendar.YEAR, Integer.valueOf(Integer.valueOf(groupValue.substring(0, 4))));
                time.set(Calendar.MONTH, Integer.valueOf(Integer.valueOf(groupValue.substring(4, 6))-1));
                time.set(Calendar.DAY_OF_MONTH, Integer.valueOf(Integer.valueOf(groupValue.substring(6, 8))));

                /*Calendar ret = new GregorianCalendar(TimeZone.getTimeZone("UTC"));

                ret.setTimeInMillis(time.getTimeInMillis() +
                                    TimeZone.getTimeZone("UTC").getOffset(time.getTimeInMillis()) -
                					TimeZone.getDefault().getOffset(time.getTimeInMillis()));*/

                position.setTime(time.getTime());

                break;
            }

            case TIME: {

                Calendar time = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
                time.clear();
                time.setTime(position.getTime());

                time.set(Calendar.HOUR_OF_DAY, Integer.valueOf(Integer.valueOf(groupValue.substring(0, 2))));
                time.set(Calendar.MINUTE, Integer.valueOf(Integer.valueOf(groupValue.substring(3, 5))));
                time.set(Calendar.SECOND, Integer.valueOf(Integer.valueOf(groupValue.substring(6, 8))));

                /*Calendar ret = new GregorianCalendar(TimeZone.getTimeZone("UTC"));

                ret.setTimeInMillis(time.getTimeInMillis() +
                                    TimeZone.getTimeZone("UTC").getOffset(time.getTimeInMillis()) -
                					TimeZone.getDefault().getOffset(time.getTimeInMillis()));*/

                position.setTime(time.getTime());

                break;
            }

            default:
                break;
            }

        }
    }

    private enum FIELD_FIX_VALUE {
        FIXED(1, "GPS is fixed"), NOT_FIXED(0, "GPS is not fixed");

        private int value;

        private String desc;

        private FIELD_FIX_VALUE(int value, String desc) {
            this.value = value;
            this.desc = desc;
        }

        public int getValue() {
            return value;
        }

        public String getDesc() {
            return desc;
        }

        public FIELD_FIX_VALUE getValueOf(String indiceStr) {
            int indice = Integer.valueOf(indiceStr);
            return getValueOf(indice);
        }

        public FIELD_FIX_VALUE getValueOf(int indice) {
            switch (indice) {
            case 1:
                return FIXED;
            case 0:
                return NOT_FIXED;
            default:
                throw new IllegalArgumentException("Index not defined");
            }
        }
    }

    private enum FIELD_MODE_VALUE {
        PARKING(1, "Idle mode (Parking)"), DRIVING(2, "Active Mode (Driving)");

        private int value;

        private String desc;

        private FIELD_MODE_VALUE(int value, String desc) {
            this.value = value;
            this.desc = desc;
        }

        public int getValue() {
            return value;
        }

        public String getDesc() {
            return desc;
        }

        public FIELD_MODE_VALUE getValueOf(String indiceStr) {
            int indice = Integer.valueOf(indiceStr);
            return getValueOf(indice);
        }

        public FIELD_MODE_VALUE getValueOf(int indice) {
            switch (indice) {
            case 1:
                return PARKING;
            case 2:
                return DRIVING;
            default:
                throw new IllegalArgumentException("Index not defined");
            }
        }
    }

    private enum FIELD_EMG_ID_VALUE {
        PANIC(1, "Emergency by panic button"),
        PARKING(2,"Emergency by parking lock"),
        MAIN_POWER(3,"Emergency by removing main power"),
        ANTI_THEFT(5,"Emergency by anti-theft");

        private int value;

        private String desc;

        private FIELD_EMG_ID_VALUE(int value, String desc) {
            this.value = value;
            this.desc = desc;
        }

        public int getValue() {
            return value;
        }

        public String getDesc() {
            return desc;
        }

        public FIELD_EMG_ID_VALUE getValueOf(String indiceStr) {
            int indice = Integer.valueOf(indiceStr);
            return getValueOf(indice);
        }

        public FIELD_EMG_ID_VALUE getValueOf(int indice) {
            switch (indice) {
            case 1:
                return PANIC;
            case 2:
                return PARKING;
            case 3:
                return MAIN_POWER;
            case 5:
                return ANTI_THEFT;
            default:
                throw new IllegalArgumentException("Index not defined");
            }
        }
    }

    private enum FIELD_EVT_ID_VALUE {
        INPUT1_GROUND(1, "Input1 goes to ground state"),
        INPUT1_OPEN(2,"Input1 goes to open state"),
        INPUT2_GROUND(3,"Input2 goes to ground state"),
        INPUT2_OPEN(4,"Input2 goes to open state"),
        INPUT3_GROUND(5,"Input3 goes to ground state"),
        INPUT3_OPEN(6,"Input3 goes to open state");

        private int value;

        private String desc;

        private FIELD_EVT_ID_VALUE(int value, String desc) {
            this.value = value;
            this.desc = desc;
        }

        public int getValue() {
            return value;
        }

        public String getDesc() {
            return desc;
        }

        public FIELD_EVT_ID_VALUE getValueOf(String indiceStr) {
            int indice = Integer.valueOf(indiceStr);
            return getValueOf(indice);
        }

        public FIELD_EVT_ID_VALUE getValueOf(int indice) {
            switch (indice) {
            case 1:
                return INPUT1_GROUND;
            case 2:
                return INPUT1_OPEN;
            case 3:
                return INPUT2_GROUND;
            case 4:
                return INPUT2_OPEN;
            case 5:
                return INPUT3_GROUND;
            case 6:
                return INPUT3_OPEN;
            default:
                throw new IllegalArgumentException("Index not defined");
            }
        }
    }

    private enum FIELD_ALERT_ID_VALUE {
        DRIVING_FASTER(1, "Start driving faster than SPEED_LIMIT"),
        OVER_SPPED(2, "Ended over speed condition"),
        DISCON_GPS(3,"Disconnected GPS antenna"),
        RECON_GPS(4,"Reconnected GPS antenna after disconnected"),
        OUT_GEO_FENCE(5,"The vehicle went out from the geo-fence that has following ID"),
        INTO_GEO_FENCE(6,"The vehicle entered into the geo-fence that has following ID"),
        SHORTED_GPS(8, "Shorted GPS antenna"),
        DEEP_SLEEP_ON(9,"Enter to deep sleep mode"),
        DEEP_SLEEP_OFF(10,"Exite from deep sleep mode"),
        BKP_BATTERY(13,"Backup battery error"),
        BATTERY_DOWN(14,"Vehicle battery goes down to so low level"),
        SHOCKED(15,"Shocked"),
        COLLISION(16, "Occurred some collision"),
        DEVIATE_ROUT(18, "Deviate from predefined rout"),
        ENTER_ROUT(19,"Enter into predefined rout");

        private int value;

        private String desc;

        private FIELD_ALERT_ID_VALUE(int value, String desc) {
            this.value = value;
            this.desc = desc;
        }

        public int getValue() {
            return value;
        }

        public String getDesc() {
            return desc;
        }

        public FIELD_ALERT_ID_VALUE getValueOf(String indiceStr) {
            int indice = Integer.valueOf(indiceStr);
            return getValueOf(indice);
        }

        public FIELD_ALERT_ID_VALUE getValueOf(int indice) {
            switch (indice) {
            case 1:
                return DRIVING_FASTER;
            case 2:
                return OVER_SPPED;
            case 3:
                return DISCON_GPS;
            case 4:
                return RECON_GPS;
            case 5:
                return OUT_GEO_FENCE;
            case 6:
                return INTO_GEO_FENCE;
            case 8:
                return SHORTED_GPS;
            case 9:
                return DEEP_SLEEP_ON;
            case 10:
                return DEEP_SLEEP_OFF;
            case 13:
                return BKP_BATTERY;
            case 14:
                return BATTERY_DOWN;
            case 15:
                return SHOCKED;
            case 16:
                return COLLISION;
            case 18:
                return DEVIATE_ROUT;
            case 19:
                return ENTER_ROUT;
            default:
                throw new IllegalArgumentException("Index not defined");
            }
        }
    }

    private enum ST210REPORTS {

        STATUS("SA200STT"), EMERGENCY("SA200EMG"), EVENT("SA200EVT"), ALERT(
                "SA200ALT"), ALIVE("SA200ALV");

        private String header;

        private ST210REPORTS(String header) {
            this.header = header;
        }

        public String getHeader() {
            return this.header;
        }

        public List<ST210FIELDS> getProtocol() {

            if (this.equals(STATUS)) {
                return StatusProtocol;
            }

            if (this.equals(EMERGENCY)) {
                return EmergencyProtocol;
            }

            if (this.equals(EVENT)) {
                return EventProtocol;
            }

            if (this.equals(ALERT)) {
                return AlertProtocol;
            }

            if (this.equals(ALIVE)) {
                return AliveProtocol;
            }

            return null;
        }

        public Pattern getProtocolPattern() {

            if (this.equals(STATUS)) {
                return getPattern(StatusProtocol);
            }

            if (this.equals(EMERGENCY)) {
                return getPattern(EmergencyProtocol);
            }

            if (this.equals(EVENT)) {
                return getPattern(EventProtocol);
            }

            if (this.equals(ALERT)) {
                return getPattern(AlertProtocol);
            }

            if (this.equals(ALIVE)) {
                return getPattern(AliveProtocol);
            }

            return null;
        }

    }

    private static ST210REPORTS getReportType(String msg) {

        if (msg.startsWith(ST210REPORTS.STATUS.getHeader())) {
            return ST210REPORTS.STATUS;
        }

        if (msg.startsWith(ST210REPORTS.EMERGENCY.getHeader())) {
            return ST210REPORTS.EMERGENCY;
        }

        if (msg.startsWith(ST210REPORTS.EVENT.getHeader())) {
            return ST210REPORTS.EVENT;
        }

        if (msg.startsWith(ST210REPORTS.ALERT.getHeader())) {
            return ST210REPORTS.ALERT;
        }

        if (msg.startsWith(ST210REPORTS.ALIVE.getHeader())) {
            return ST210REPORTS.ALIVE;
        }

        return null;
    }

    private static Pattern getPattern(List<ST210FIELDS> protocol) {

        String patternStr = "";

        for (ST210FIELDS field : protocol) {
            patternStr += field.getPattern();
        }

        if(patternStr.endsWith(";")){
            patternStr = patternStr.substring(0, patternStr.length()-1);
        }

        return Pattern.compile(patternStr);

    }

    @SuppressWarnings("serial")
    static private List<ST210FIELDS> StatusProtocol = new LinkedList<ST210FIELDS>() {

        {
            add(ST210FIELDS.HDR_STATUS);
            add(ST210FIELDS.DEV_ID);
            add(ST210FIELDS.SW_VER);
            add(ST210FIELDS.DATE);
            add(ST210FIELDS.TIME);
            add(ST210FIELDS.CELL);
            add(ST210FIELDS.LAT);
            add(ST210FIELDS.LON);
            add(ST210FIELDS.SPD);
            add(ST210FIELDS.CRS);
            add(ST210FIELDS.SATT);
            add(ST210FIELDS.FIX);
            add(ST210FIELDS.DIST);
            add(ST210FIELDS.PWR_VOLT);
            add(ST210FIELDS.IO);
            add(ST210FIELDS.MODE);
            add(ST210FIELDS.MSG_NUM);
        }

    };

    @SuppressWarnings("serial")
    static private List<ST210FIELDS> EmergencyProtocol = new LinkedList<ST210FIELDS>() {

        {
            add(ST210FIELDS.HDR_EMERGENCY);
            add(ST210FIELDS.DEV_ID);
            add(ST210FIELDS.SW_VER);
            add(ST210FIELDS.DATE);
            add(ST210FIELDS.TIME);
            add(ST210FIELDS.CELL);
            add(ST210FIELDS.LAT);
            add(ST210FIELDS.LON);
            add(ST210FIELDS.SPD);
            add(ST210FIELDS.CRS);
            add(ST210FIELDS.SATT);
            add(ST210FIELDS.FIX);
            add(ST210FIELDS.DIST);
            add(ST210FIELDS.PWR_VOLT);
            add(ST210FIELDS.IO);
            add(ST210FIELDS.EMG_ID);
        }

    };

    @SuppressWarnings("serial")
    static private List<ST210FIELDS> EventProtocol = new LinkedList<ST210FIELDS>() {

        {
            add(ST210FIELDS.HDR_EVENT);
            add(ST210FIELDS.DEV_ID);
            add(ST210FIELDS.SW_VER);
            add(ST210FIELDS.DATE);
            add(ST210FIELDS.TIME);
            add(ST210FIELDS.CELL);
            add(ST210FIELDS.LAT);
            add(ST210FIELDS.LON);
            add(ST210FIELDS.SPD);
            add(ST210FIELDS.CRS);
            add(ST210FIELDS.SATT);
            add(ST210FIELDS.FIX);
            add(ST210FIELDS.DIST);
            add(ST210FIELDS.PWR_VOLT);
            add(ST210FIELDS.IO);
            add(ST210FIELDS.EVT_ID);
        }

    };

    @SuppressWarnings("serial")
    static private List<ST210FIELDS> AlertProtocol = new LinkedList<ST210FIELDS>() {

        {
            add(ST210FIELDS.HDR_ALERT);
            add(ST210FIELDS.DEV_ID);
            add(ST210FIELDS.SW_VER);
            add(ST210FIELDS.DATE);
            add(ST210FIELDS.TIME);
            add(ST210FIELDS.CELL);
            add(ST210FIELDS.LAT);
            add(ST210FIELDS.LON);
            add(ST210FIELDS.SPD);
            add(ST210FIELDS.CRS);
            add(ST210FIELDS.SATT);
            add(ST210FIELDS.FIX);
            add(ST210FIELDS.DIST);
            add(ST210FIELDS.PWR_VOLT);
            add(ST210FIELDS.IO);
            add(ST210FIELDS.ALERT_ID);
        }

    };

    @SuppressWarnings("serial")
    static private List<ST210FIELDS> AliveProtocol = new LinkedList<ST210FIELDS>() {

        {
            add(ST210FIELDS.HDR_ALIVE);
            add(ST210FIELDS.DEV_ID);
        }

    };

    @Override
    public Object decode(ChannelHandlerContext ctx, Channel channel, Object msg) {
        String sentence = (String) msg;
        Log.info("Msg: " + msg);

        Position position = null;

        try{
            position = decodeMsg(sentence);
            Log.info("MESSAGE DECODED WITH SUCCESS!");
        }
        catch(Exception e){
            Log.severe("ERROR WHILE DECODING MESSAGE: " + e.getMessage());
        }

        return position;
    }

    public Position decodeMsg(String msg) throws Exception {

        ST210REPORTS report = getReportType(msg);

        List<ST210FIELDS> protocol = report.getProtocol();

        Pattern protocolPattern = report.getProtocolPattern();

        // Parse message
        Matcher parser = protocolPattern.matcher(msg);
        if (!parser.matches()) {
            throw new Exception("Pattern no match: " + protocolPattern.toString());
        }

        if(report.equals(ST210REPORTS.ALIVE)){
            return null;
        }

        // Create new position
        Position position = new Position();

        position.setAltitude(0D);
        position.setExtendedInfo(new ExtendedInfoFormatter("st210").toString());
        position.setValid(true);

        Integer index = 0;
        for (ST210FIELDS field : protocol) {

            String groupValue = parser.group(index++);

            field.populatePosition(position, groupValue, getDataManager());
        }

        return position;
    }

}