aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/org/traccar/protocol/FreematicsProtocolDecoder.java
blob: d7d9d62ed9d16cdab2ecc13d5c8566d82db1e66d (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
/*
 * Copyright 2018 - 2021 Anton Tananaev (anton@traccar.org)
 *
 * 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.session.DeviceSession;
import org.traccar.NetworkMessage;
import org.traccar.Protocol;
import org.traccar.helper.Checksum;
import org.traccar.helper.DateBuilder;
import org.traccar.helper.UnitsConverter;
import org.traccar.model.Position;

import java.net.SocketAddress;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;

public class FreematicsProtocolDecoder extends BaseProtocolDecoder {

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

    private Object decodeEvent(
            Channel channel, SocketAddress remoteAddress, String sentence) {

        DeviceSession deviceSession = null;
        String event = null;
        String time = null;

        for (String pair : sentence.split(",")) {
            String[] data = pair.split("=");
            String key = data[0];
            String value = data[1];
            switch (key) {
                case "ID", "VIN" -> {
                    if (deviceSession == null) {
                        deviceSession = getDeviceSession(channel, remoteAddress, value);
                    }
                }
                case "EV" -> event = value;
                case "TS" -> time = value;
            }
        }

        if (channel != null && deviceSession != null && event != null && time != null) {
            String message = String.format("1#EV=%s,RX=1,TS=%s", event, time);
            message += '*' + Checksum.sum(message);
            channel.writeAndFlush(new NetworkMessage(message, remoteAddress));
        }

        return null;
    }

    private Object decodePosition(
            Channel channel, SocketAddress remoteAddress, String sentence, String id) {

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

        List<Position> positions = new LinkedList<>();
        Position position = null;
        DateBuilder dateBuilder = null;

        for (String pair : sentence.split(",")) {
            String[] data = pair.split("[=:]");
            int key;
            try {
                key = Integer.parseInt(data[0], 16);
            } catch (NumberFormatException e) {
                continue;
            }
            String value = data[1];
            if (key == 0x0) {
                if (position != null) {
                    position.setTime(dateBuilder.getDate());
                    positions.add(position);
                }
                position = new Position(getProtocolName());
                position.setDeviceId(deviceSession.getDeviceId());
                dateBuilder = new DateBuilder(new Date());
            } else if (position != null) {
                switch (key) {
                    case 0x11 -> {
                        value = ("000000" + value).substring(value.length());
                        dateBuilder.setDateReverse(
                                Integer.parseInt(value.substring(0, 2)),
                                Integer.parseInt(value.substring(2, 4)),
                                Integer.parseInt(value.substring(4)));
                    }
                    case 0x10 -> {
                        value = ("00000000" + value).substring(value.length());
                        dateBuilder.setTime(
                                Integer.parseInt(value.substring(0, 2)),
                                Integer.parseInt(value.substring(2, 4)),
                                Integer.parseInt(value.substring(4, 6)),
                                Integer.parseInt(value.substring(6)) * 10);
                    }
                    case 0xA -> {
                        position.setValid(true);
                        position.setLatitude(Double.parseDouble(value));
                    }
                    case 0xB -> {
                        position.setValid(true);
                        position.setLongitude(Double.parseDouble(value));
                    }
                    case 0xC -> position.setAltitude(Double.parseDouble(value));
                    case 0xD -> position.setSpeed(UnitsConverter.knotsFromKph(Double.parseDouble(value)));
                    case 0xE -> position.setCourse(Integer.parseInt(value));
                    case 0xF -> position.set(Position.KEY_SATELLITES, Integer.parseInt(value));
                    case 0x12 -> position.set(Position.KEY_HDOP, Integer.parseInt(value));
                    case 0x20 -> position.set(Position.KEY_ACCELERATION, value);
                    case 0x24 -> position.set(Position.KEY_BATTERY, Integer.parseInt(value) * 0.01);
                    case 0x81 -> position.set(Position.KEY_RSSI, Integer.parseInt(value));
                    case 0x82 -> position.set(Position.KEY_DEVICE_TEMP, Double.parseDouble(value) * 0.1);
                    case 0x104 -> position.set(Position.KEY_ENGINE_LOAD, Integer.parseInt(value));
                    case 0x105 -> position.set(Position.KEY_COOLANT_TEMP, Integer.parseInt(value));
                    case 0x10c -> position.set(Position.KEY_RPM, Integer.parseInt(value));
                    case 0x10d -> position.set(Position.KEY_OBD_SPEED, Integer.parseInt(value));
                    case 0x111 -> position.set(Position.KEY_THROTTLE, Integer.parseInt(value));
                    default -> position.set(Position.PREFIX_IO + key, value);
                }
            }
        }

        if (position != null) {
            if (!position.getValid()) {
                getLastLocation(position, null);
            }
            position.setTime(dateBuilder.getDate());
            positions.add(position);
        }

        return positions.isEmpty() ? null : positions;
    }

    @Override
    protected Object decode(
            Channel channel, SocketAddress remoteAddress, Object msg) throws Exception {

        String sentence = (String) msg;
        int startIndex = sentence.indexOf('#');
        int endIndex = sentence.indexOf('*');

        if (startIndex > 0 && endIndex > 0) {
            String id = sentence.substring(0, startIndex);
            sentence = sentence.substring(startIndex + 1, endIndex);

            if (sentence.startsWith("EV")) {
                return decodeEvent(channel, remoteAddress, sentence);
            } else {
                return decodePosition(channel, remoteAddress, sentence, id);
            }
        }

        return null;
    }

}