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
|
/*
* Copyright 2016 - 2024 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.buffer.ByteBuf;
import io.netty.buffer.ByteBufUtil;
import io.netty.buffer.Unpooled;
import io.netty.channel.Channel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.traccar.BaseProtocolDecoder;
import org.traccar.session.DeviceSession;
import org.traccar.NetworkMessage;
import org.traccar.Protocol;
import org.traccar.helper.BitUtil;
import org.traccar.helper.DateBuilder;
import org.traccar.helper.UnitsConverter;
import org.traccar.model.Position;
import java.net.SocketAddress;
import java.nio.charset.StandardCharsets;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;
public class AstraProtocolDecoder extends BaseProtocolDecoder {
private static final Logger LOGGER = LoggerFactory.getLogger(AstraProtocolDecoder.class);
public AstraProtocolDecoder(Protocol protocol) {
super(protocol);
}
public static final int MSG_HEARTBEAT = 0x1A;
public static final int MSG_DATA = 0x10;
@Override
protected Object decode(
Channel channel, SocketAddress remoteAddress, Object msg) throws Exception {
ByteBuf buf = (ByteBuf) msg;
byte protocol = buf.readByte();
buf.readUnsignedShort(); // length
if (channel != null) {
channel.writeAndFlush(new NetworkMessage(Unpooled.wrappedBuffer(new byte[] {0x06}), remoteAddress));
}
return switch (protocol) {
case 'K' -> decodeK(channel, remoteAddress, buf);
case 'X' -> decodeX(channel, remoteAddress, buf);
default -> null;
};
}
private String readImei(ByteBuf buf) {
return String.format("%08d", buf.readUnsignedInt()) + String.format("%07d", buf.readUnsignedMedium());
}
private Date readTime(ByteBuf buf) {
DateBuilder dateBuilder = new DateBuilder()
.setDate(1980, 1, 6).addMillis(buf.readUnsignedInt() * 1000L);
return dateBuilder.getDate();
}
private Object decodeK(Channel channel, SocketAddress remoteAddress, ByteBuf buf) {
DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, readImei(buf));
if (deviceSession == null) {
return null;
}
List<Position> positions = new LinkedList<>();
while (buf.readableBytes() > 2) {
Position position = new Position(getProtocolName());
position.setDeviceId(deviceSession.getDeviceId());
buf.readUnsignedByte(); // index
position.setValid(true);
position.setLatitude(buf.readInt() * 0.000001);
position.setLongitude(buf.readInt() * 0.000001);
position.setTime(readTime(buf));
position.setSpeed(UnitsConverter.knotsFromKph(buf.readUnsignedByte() * 2));
position.setCourse(buf.readUnsignedByte() * 2);
int reason = buf.readUnsignedMedium();
position.set(Position.KEY_EVENT, reason);
int status = buf.readUnsignedShort();
position.set(Position.KEY_STATUS, status);
position.set(Position.PREFIX_IO + 1, buf.readUnsignedByte());
position.set(Position.PREFIX_ADC + 1, buf.readUnsignedByte());
position.set(Position.KEY_BATTERY, buf.readUnsignedByte());
position.set(Position.KEY_POWER, buf.readUnsignedByte());
buf.readUnsignedByte(); // max journey speed
buf.skipBytes(6); // accelerometer
position.set(Position.KEY_ODOMETER_TRIP, buf.readUnsignedShort());
buf.readUnsignedShort(); // journey idle time
position.setAltitude(buf.readUnsignedByte() * 20);
int quality = buf.readUnsignedByte();
position.set(Position.KEY_SATELLITES, quality & 0xf);
position.set(Position.KEY_RSSI, quality >> 4);
buf.readUnsignedByte(); // geofence events
if (BitUtil.check(status, 8)) {
position.set(Position.KEY_DRIVER_UNIQUE_ID, buf.readSlice(7).toString(StandardCharsets.US_ASCII));
position.set(Position.KEY_ODOMETER, buf.readUnsignedMedium() * 1000);
position.set(Position.KEY_HOURS, UnitsConverter.msFromHours(buf.readUnsignedShort()));
}
if (BitUtil.check(status, 6)) {
LOGGER.warn("Extension data is not supported");
return position;
}
positions.add(position);
}
return positions;
}
private Object decodeX(Channel channel, SocketAddress remoteAddress, ByteBuf buf) {
int count = buf.readUnsignedByte();
DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, readImei(buf));
if (deviceSession == null) {
return null;
}
List<Position> positions = new LinkedList<>();
for (int i = 0; i < count; i++) {
Position position = new Position(getProtocolName());
position.setDeviceId(deviceSession.getDeviceId());
position.set(Position.KEY_INDEX, buf.readUnsignedByte());
long mask = ((long) buf.readUnsignedShort() << 32) + buf.readUnsignedInt();
position.setDeviceTime(readTime(buf));
position.set(Position.KEY_EVENT, buf.readUnsignedInt());
position.set(Position.KEY_STATUS, buf.readUnsignedShort());
if ((mask & 1L) > 0) {
position.set(Position.KEY_POWER, buf.readUnsignedByte() * 0.2);
position.set(Position.KEY_BATTERY_LEVEL, buf.readUnsignedByte());
}
if ((mask & 2L) > 0) {
position.setValid(true);
position.setFixTime(readTime(buf));
position.setLatitude(buf.readInt() * 0.000001);
position.setLongitude(buf.readInt() * 0.000001);
position.setSpeed(UnitsConverter.knotsFromKph(buf.readUnsignedByte() * 2));
buf.readUnsignedByte(); // max speed since last report
position.setCourse(buf.readUnsignedByte() * 2);
position.setAltitude(buf.readUnsignedByte() * 20);
position.set(Position.KEY_ODOMETER_TRIP, buf.readUnsignedShort() * 100);
} else {
getLastLocation(position, position.getDeviceTime());
}
if ((mask & 4L) > 0) {
buf.readUnsignedShort(); // states
buf.readUnsignedShort(); // changes mask
}
if ((mask & 8L) > 0) {
buf.readUnsignedShort(); // adc1
buf.readUnsignedShort(); // adc2
}
if ((mask & 16L) > 0) {
position.set("xMax", buf.readByte());
position.set("xMin", buf.readByte());
position.set("yMax", buf.readByte());
position.set("yMin", buf.readByte());
position.set("zMax", buf.readByte());
position.set("zMin", buf.readByte());
position.set("idleHours", buf.readUnsignedShort());
}
if ((mask & 32L) > 0) {
int value = buf.readUnsignedByte();
position.set(Position.KEY_SATELLITES, BitUtil.to(value, 4));
position.set(Position.KEY_RSSI, BitUtil.from(value, 4));
}
if ((mask & 64L) > 0) {
buf.readUnsignedShort(); // mcc
buf.readUnsignedShort(); // mnc
}
if ((mask & 128L) > 0) {
buf.readUnsignedByte(); // geofences
}
if ((mask & 256L) > 0) {
buf.readUnsignedByte(); // source
buf.readLong(); // driver id
}
if ((mask & 512L) > 0) {
buf.readUnsignedByte(); // source
buf.skipBytes(10); // trailer id
buf.readUnsignedByte(); // status
}
if ((mask & 1024L) > 0) {
position.set("axleWeight", buf.readUnsignedShort());
}
if ((mask & 2048L) > 0) {
position.set(Position.KEY_ODOMETER, buf.readUnsignedMedium() * 1000);
position.set(Position.KEY_HOURS, buf.readUnsignedShort() * 3_600_000);
}
if ((mask & 4096L) > 0) {
position.set("wheelSpeedMax", buf.readUnsignedByte());
position.set("wheelSpeedAvg", buf.readUnsignedByte());
position.set("rpmMax", buf.readUnsignedByte() * 32);
position.set("rpmAvg", buf.readUnsignedByte() * 32);
position.set("acceleratorMax", buf.readUnsignedByte());
position.set("acceleratorAvg", buf.readUnsignedByte());
position.set("engineLoadMax", buf.readUnsignedByte());
position.set("engineLoadAvg", buf.readUnsignedByte());
position.set(Position.KEY_ODOMETER_TRIP, buf.readUnsignedShort() * 100);
position.set(Position.KEY_COOLANT_TEMP, buf.readByte() + 40);
position.set("fmsStatus", buf.readUnsignedShort());
position.set("fmsEvents", buf.readUnsignedShort());
position.set(Position.KEY_FUEL_LEVEL, buf.readUnsignedByte());
position.set(Position.KEY_FUEL_USED, buf.readUnsignedInt() * 0.5);
}
if ((mask & 8192L) > 0) {
position.set("wheelSpeedMax", buf.readUnsignedByte());
position.set("wheelSpeedAvg", buf.readUnsignedByte());
position.set("rpmMax", buf.readUnsignedByte() * 32);
position.set("rpmAvg", buf.readUnsignedByte() * 32);
position.set("acceleratorMax", buf.readUnsignedByte());
position.set("acceleratorAvg", buf.readUnsignedByte());
position.set("engineLoadMax", buf.readUnsignedByte());
position.set("engineLoadAvg", buf.readUnsignedByte());
position.set(Position.KEY_ODOMETER_TRIP, buf.readUnsignedShort() * 100);
position.set(Position.KEY_COOLANT_TEMP, buf.readByte() + 40);
position.set("obdStatus", buf.readUnsignedShort());
position.set("obdEvents", buf.readUnsignedShort());
position.set(Position.KEY_FUEL_LEVEL, buf.readUnsignedByte());
position.set(Position.KEY_FUEL_USED, buf.readUnsignedShort() * 0.1);
}
if ((mask & 16384L) > 0) {
for (int j = 1; j <= 5; j++) {
position.set("dtc" + j, buf.readCharSequence(5, StandardCharsets.US_ASCII).toString());
}
}
if ((mask & 32768L) > 0) {
position.set(Position.KEY_ODOMETER, buf.readUnsignedMedium() * 1000);
position.set(Position.KEY_HOURS, buf.readUnsignedShort() * 3_600_000);
position.set("axleWeight", buf.readUnsignedShort());
position.set("tripFuelUsed", buf.readUnsignedShort() * 0.1);
position.set("tripCruise", buf.readUnsignedShort());
position.set(Position.KEY_ODOMETER_SERVICE, buf.readUnsignedShort() * 5);
}
if ((mask & 65536L) > 0) {
position.set(Position.KEY_ODOMETER, buf.readUnsignedMedium() * 1000);
position.set(Position.KEY_HOURS, buf.readUnsignedShort() * 3_600_000);
buf.readUnsignedShort(); // time with mil on
buf.readUnsignedShort(); // distance with mil on
}
if ((mask & 131072L) > 0) {
for (int j = 1; j <= 6; j++) {
position.set(Position.PREFIX_TEMP + j, buf.readShort() * 0.1);
}
for (int j = 1; j <= 3; j++) {
position.set("setpoint" + j, buf.readByte() * 0.5);
}
buf.readUnsignedByte(); // refrigerator fuel level
buf.readUnsignedShort(); // refrigerator total engine hours
buf.readUnsignedShort(); // refrigerator total standby hours
buf.readUnsignedShort(); // refrigerator status
buf.readUnsignedMedium(); // alarm flags
}
if ((mask & 262144L) > 0) {
for (int j = 1; j <= 4; j++) {
position.set(Position.PREFIX_TEMP + j, (buf.readUnsignedShort() - 550) * 0.1);
}
}
if ((mask & 524288L) > 0) {
position.set("alarmCount", buf.readUnsignedByte());
position.set("alarmQueue", ByteBufUtil.hexDump(buf.readSlice(16)));
}
if ((mask & 4294967296L) > 0) {
for (int j = 1; j <= 6; j++) {
position.set("sensor" + j, buf.readUnsignedMedium());
}
}
positions.add(position);
}
return positions;
}
}
|