aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/org/traccar/protocol/MictrackProtocolDecoder.java
blob: 029e7d99c925e31e80e1a99586bc03db798fc0cc (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
/*
 * Copyright 2019 - 2021 Anton Tananaev (anton@traccar.org)
 * Copyright 2020 Roeland Boeters (roeland@geodelta.com)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.traccar.protocol;

import io.netty.channel.Channel;
import org.traccar.BaseProtocolDecoder;
import org.traccar.DeviceSession;
import org.traccar.Protocol;
import org.traccar.helper.DateBuilder;
import org.traccar.helper.Parser;
import org.traccar.helper.PatternBuilder;
import org.traccar.helper.UnitsConverter;
import org.traccar.model.CellTower;
import org.traccar.model.Network;
import org.traccar.model.Position;
import org.traccar.model.WifiAccessPoint;

import java.net.SocketAddress;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;
import java.util.TimeZone;
import java.util.regex.Pattern;

public class MictrackProtocolDecoder extends BaseProtocolDecoder {

    public MictrackProtocolDecoder(Protocol protocol) {
        super(protocol);
    }

    private static final Pattern PATTERN_LOW_ALTITUDE = new PatternBuilder()
            .number("(dd)(dd)(dd).d+,")          // time (hhmmss.sss)
            .expression("([AV]),")               // validity
            .number("(d+)(dd.d+),")              // latitude
            .expression("([NS]),")
            .number("(d+)(dd.d+),")              // longitude
            .expression("([EW]),")
            .number("(d+.?d*)?,")                // speed
            .number("(d+.?d*)?,")                // course
            .number("(d+.?d*)?,")                // altitude
            .number("(dd)(dd)(dd)")              // date (ddmmyy)
            .compile();

    private Date decodeTime(String data) throws ParseException {
        DateFormat dateFormat = new SimpleDateFormat("yyMMddHHmmss");
        dateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
        return dateFormat.parse(data);
    }

    private String decodeAlarm(int event) {
        switch (event) {
            case 0:
                return Position.ALARM_POWER_ON;
            case 5:
                return Position.ALARM_SOS;
            case 8:
                return Position.ALARM_LOW_BATTERY;
            case 9:
                return Position.ALARM_GEOFENCE_ENTER;
            case 10:
                return Position.ALARM_GEOFENCE_EXIT;
            case 12:
                return Position.ALARM_POWER_OFF;
            default:
                return null;
        }
    }

    private void decodeLocation(Position position, String data) throws ParseException {
        int index = 0;
        String[] values = data.split("\\+");

        position.set(Position.KEY_SATELLITES, Integer.parseInt(values[index++]));

        position.setValid(true);
        position.setTime(decodeTime(values[index++]));
        position.setLatitude(Double.parseDouble(values[index++]));
        position.setLongitude(Double.parseDouble(values[index++]));
        position.setSpeed(UnitsConverter.knotsFromKph(Double.parseDouble(values[index++])));
        position.setCourse(Integer.parseInt(values[index++]));

        int event = Integer.parseInt(values[index++]);
        position.set(Position.KEY_ALARM, decodeAlarm(event));
        position.set(Position.KEY_EVENT, event);
        position.set(Position.KEY_BATTERY, Integer.parseInt(values[index++]) * 0.001);
    }

    private void decodeCell(Network network, String data) {
        String[] values = data.split(",");
        int length = values.length % 5 == 0 ? 5 : 4;
        for (int i = 0; i < values.length / length; i++) {
            int mnc = Integer.parseInt(values[i * length]);
            int cid = Integer.parseInt(values[i * length + 1]);
            int lac = Integer.parseInt(values[i * length + 2]);
            int mcc = Integer.parseInt(values[i * length + 3]);
            network.addCellTower(CellTower.from(mcc, mnc, lac, cid));
        }
    }

    private void decodeWifi(Network network, String data) {
        String[] values = data.split(",");
        for (int i = 0; i < values.length / 2; i++) {
            network.addWifiAccessPoint(WifiAccessPoint.from(values[i * 2], Integer.parseInt(values[i * 2 + 1])));
        }
    }

    private void decodeNetwork(Position position, String data, boolean hasWifi, boolean hasCell) throws ParseException {
        int index = 0;
        String[] values = data.split("\\+");

        getLastLocation(position, decodeTime(values[index++]));

        Network network = new Network();

        if (hasWifi) {
            decodeWifi(network, values[index++]);
        }

        if (hasCell) {
            decodeCell(network, values[index++]);
        }

        position.setNetwork(network);

        int event = Integer.parseInt(values[index++]);
        position.set(Position.KEY_ALARM, decodeAlarm(event));
        position.set(Position.KEY_EVENT, event);
        position.set(Position.KEY_BATTERY, Integer.parseInt(values[index++]) * 0.001);
    }

    private void decodeStatus(Position position, String data) throws ParseException {
        int index = 0;
        String[] values = data.split("\\+");

        position.set(Position.KEY_SATELLITES, Integer.parseInt(values[index++]));

        getLastLocation(position, decodeTime(values[index++]));

        index += 4; // fix values

        int event = Integer.parseInt(values[index++]);
        position.set(Position.KEY_ALARM, decodeAlarm(event));
        position.set(Position.KEY_EVENT, event);
        position.set(Position.KEY_BATTERY, Integer.parseInt(values[index++]) * 0.001);
    }

    @Override
    protected Object decode(
            Channel channel, SocketAddress remoteAddress, Object msg) throws Exception {
        String sentence = ((String) msg).trim();

        if (sentence.startsWith("MT")) {
            return decodeStandard(channel, remoteAddress, sentence);
        } else if (sentence.contains("$")) {
            return decodeLowAltitude(channel, remoteAddress, sentence);
        } else {
            return decodeResult(channel, remoteAddress, sentence);
        }
    }

    private Object decodeResult(
            Channel channel, SocketAddress remoteAddress, String sentence) throws Exception {

        if (sentence.matches("\\d{15} .+")) {

            DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, sentence.substring(0, 15));
            if (deviceSession == null) {
                return null;
            }

            Position position = new Position(getProtocolName());
            position.setDeviceId(deviceSession.getDeviceId());

            getLastLocation(position, null);

            position.set(Position.KEY_RESULT, sentence.substring(16, sentence.length() - 1));

            return position;

        } else {

            DeviceSession deviceSession = getDeviceSession(channel, remoteAddress);
            if (deviceSession == null) {
                return null;
            }

            Position position = new Position(getProtocolName());
            position.setDeviceId(deviceSession.getDeviceId());

            getLastLocation(position, null);

            position.set(Position.KEY_RESULT, sentence.substring(0, sentence.length() - 1));

            return position;

        }
    }

    private Object decodeStandard(
            Channel channel, SocketAddress remoteAddress, String sentence) throws Exception {
        String[] fragments = sentence.split(";");

        DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, fragments[2]);
        if (deviceSession == null) {
            return null;
        }

        Position position = new Position(getProtocolName());
        position.setDeviceId(deviceSession.getDeviceId());

        switch (fragments[3]) {
            case "R0":
                decodeLocation(position, fragments[4]);
                break;
            case "R1":
                decodeNetwork(position, fragments[4], true, false);
                break;
            case "R2":
            case "R3":
                decodeNetwork(position, fragments[4], false, true);
                break;
            case "R12":
            case "R13":
                decodeNetwork(position, fragments[4], true, true);
                break;
            case "RH":
                decodeStatus(position, fragments[4]);
                break;
            default:
                return null;
        }

        return position;
    }

    private Object decodeLowAltitude(
            Channel channel, SocketAddress remoteAddress, String sentence) {
        int separator = sentence.indexOf("$");
        if (separator < 0) {
            return null;
        }
        String deviceId = sentence.substring(0, separator);

        DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, deviceId);
        if (deviceSession == null) {
            return null;
        }

        String[] fragments = sentence.substring(sentence.indexOf("$")).split("\\$");

        List<Position> positions = new LinkedList<>();

        for (String message : fragments) {
            Parser parser = new Parser(PATTERN_LOW_ALTITUDE, message);

            if (parser.matches()) {

                Position position = new Position(getProtocolName());
                position.setDeviceId(deviceSession.getDeviceId());

                DateBuilder dateBuilder = new DateBuilder()
                        .setTime(parser.nextInt(), parser.nextInt(), parser.nextInt());

                position.setValid(parser.next().equals("A"));
                position.setLatitude(parser.nextCoordinate());
                position.setLongitude(parser.nextCoordinate());

                position.setSpeed(parser.nextDouble(0));
                position.setCourse(parser.nextDouble(0));
                position.setAltitude(parser.nextDouble(0));

                dateBuilder.setDateReverse(parser.nextInt(), parser.nextInt(), parser.nextInt());
                position.setTime(dateBuilder.getDate());

                positions.add(position);
            }
        }

        return positions;
    }

}