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
|
/*
* Copyright 2020 - 2022 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.channel.Channel;
import io.netty.handler.codec.mqtt.MqttConnectMessage;
import io.netty.handler.codec.mqtt.MqttConnectReturnCode;
import io.netty.handler.codec.mqtt.MqttMessage;
import io.netty.handler.codec.mqtt.MqttMessageBuilders;
import io.netty.handler.codec.mqtt.MqttPublishMessage;
import io.netty.handler.codec.mqtt.MqttSubscribeMessage;
import org.traccar.BaseProtocolDecoder;
import org.traccar.session.DeviceSession;
import org.traccar.NetworkMessage;
import org.traccar.Protocol;
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 IotmProtocolDecoder extends BaseProtocolDecoder {
public IotmProtocolDecoder(Protocol protocol) {
super(protocol);
}
private Object readValue(ByteBuf buf, int sensorType) {
switch (sensorType) {
case 0:
return false;
case 1:
return true;
case 3:
return 0;
case 4:
return buf.readUnsignedByte();
case 5:
return buf.readUnsignedShortLE();
case 6:
return buf.readUnsignedIntLE();
case 7:
case 11:
return buf.readLongLE();
case 8:
return buf.readByte();
case 9:
return buf.readShortLE();
case 10:
return buf.readIntLE();
case 12:
return buf.readFloatLE();
case 13:
return buf.readDoubleLE();
case 32:
return buf.readCharSequence(buf.readUnsignedByte(), StandardCharsets.US_ASCII).toString();
case 33:
return ByteBufUtil.hexDump(buf.readSlice(buf.readUnsignedByte()));
case 64:
return buf.readCharSequence(buf.readUnsignedShortLE(), StandardCharsets.US_ASCII).toString();
case 65:
return ByteBufUtil.hexDump(buf.readSlice(buf.readUnsignedShortLE()));
case 2:
default:
return null;
}
}
private void decodeSensor(Position position, ByteBuf record, int sensorType, int sensorId) {
String key;
switch (sensorId) {
case 0x0002:
position.set(Position.KEY_MOTION, sensorType > 0);
break;
case 0x0008:
case 0x009B:
if (sensorType > 0) {
position.set(Position.KEY_ALARM, Position.ALARM_JAMMING);
}
break;
case 0x0010:
case 0x0011:
case 0x0012:
case 0x0013:
case 0x0014:
case 0x0015:
key = Position.PREFIX_IN + (sensorId - 0x0010 + 2);
position.set(key, sensorType > 0);
break;
case 0x0062:
position.set("doorFL", sensorType > 0);
break;
case 0x0063:
position.set("doorFR", sensorType > 0);
break;
case 0x0064:
position.set("doorRL", sensorType > 0);
break;
case 0x0065:
position.set("doorRR", sensorType > 0);
break;
case 0x001E:
position.set("buttonPresent", sensorType > 0);
break;
case 0x006D:
position.set(Position.KEY_IGNITION, sensorType > 0);
break;
case 0x008B:
position.set("handBrake", sensorType > 0);
break;
case 0x008C:
position.set("footBrake", sensorType > 0);
break;
case 0x0094:
case 0x0095:
case 0x0096:
key = Position.PREFIX_OUT + (sensorId - 0x0094 + 1);
position.set(key, sensorType > 0);
break;
case 0x009A:
position.set(Position.PREFIX_OUT + 4, sensorType > 0);
break;
case 0x2000:
position.set(Position.KEY_OBD_SPEED, record.readUnsignedByte());
break;
case 0x2001:
position.set(Position.KEY_SATELLITES, record.readUnsignedByte());
break;
case 0x2006:
position.set(Position.KEY_THROTTLE, record.readUnsignedByte());
break;
case 0x2007:
position.set(Position.KEY_FUEL_LEVEL, record.readUnsignedByte());
break;
case 0x2008:
position.set(Position.KEY_COOLANT_TEMP, record.readUnsignedByte());
break;
case 0x2009:
position.set("fuel2", record.readUnsignedByte());
break;
case 0x200A:
position.set(Position.KEY_ENGINE_LOAD, record.readUnsignedByte());
break;
case 0x2041:
position.set(Position.KEY_BATTERY_LEVEL, record.readUnsignedByte());
break;
case 0x3000:
position.set(Position.KEY_POWER, record.readUnsignedShortLE() * 0.001);
break;
case 0x3001:
case 0x3002:
case 0x3003:
key = Position.PREFIX_ADC + (0x3003 - sensorId + 3);
position.set(key, record.readUnsignedShortLE() * 0.001);
break;
case 0x3004:
position.set(Position.KEY_BATTERY, record.readUnsignedShortLE() * 0.001);
break;
case 0x300C:
position.set(Position.KEY_RPM, record.readUnsignedShortLE());
break;
case 0x3021:
position.set(Position.KEY_FUEL_CONSUMPTION, record.readUnsignedShortLE() * 0.05);
break;
case 0x3037:
position.set("cargoWeight", record.readUnsignedShortLE() * 2);
break;
case 0x4001:
position.set(Position.KEY_FUEL_USED, record.readUnsignedIntLE());
break;
case 0x4002:
position.set(Position.KEY_HOURS, record.readUnsignedIntLE());
break;
case 0x4003:
position.set(Position.KEY_ODOMETER, record.readUnsignedIntLE() * 5);
break;
case 0x4063:
position.set(Position.KEY_AXLE_WEIGHT, record.readUnsignedIntLE());
break;
case 0x5000:
position.set(Position.KEY_DRIVER_UNIQUE_ID, String.valueOf(record.readLongLE()));
break;
case 0x5004:
case 0x5005:
case 0x5006:
case 0x5007:
key = Position.PREFIX_TEMP + (sensorId - 0x5004 + 1);
position.set(key, record.readLongLE());
break;
case 0x500D:
position.set("trailerId", String.valueOf(record.readLongLE()));
break;
case 0xA000:
position.set(Position.KEY_DEVICE_TEMP, record.readFloatLE());
break;
case 0xA001:
position.set(Position.KEY_ACCELERATION, record.readFloatLE());
break;
case 0xA002:
position.set("cornering", record.readFloatLE());
break;
case 0xA017:
case 0xA018:
case 0xA019:
case 0xA01A:
key = Position.PREFIX_TEMP + (sensorId - 0xA017 + 1);
position.set(key, record.readFloatLE());
break;
case 0xB002:
position.set(Position.KEY_OBD_ODOMETER, record.readDoubleLE());
break;
default:
key = Position.PREFIX_IO + sensorId;
position.getAttributes().put(key, readValue(record, sensorType));
break;
}
}
@Override
protected Object decode(
Channel channel, SocketAddress remoteAddress, Object msg) throws Exception {
if (msg instanceof MqttConnectMessage) {
MqttConnectMessage message = (MqttConnectMessage) msg;
DeviceSession deviceSession = getDeviceSession(
channel, remoteAddress, message.payload().clientIdentifier());
MqttConnectReturnCode returnCode = deviceSession != null
? MqttConnectReturnCode.CONNECTION_ACCEPTED
: MqttConnectReturnCode.CONNECTION_REFUSED_IDENTIFIER_REJECTED;
MqttMessage response = MqttMessageBuilders.connAck().returnCode(returnCode).build();
if (channel != null) {
channel.writeAndFlush(new NetworkMessage(response, remoteAddress));
}
} else if (msg instanceof MqttSubscribeMessage) {
MqttSubscribeMessage message = (MqttSubscribeMessage) msg;
MqttMessage response = MqttMessageBuilders.subAck()
.packetId((short) message.variableHeader().messageId())
.build();
if (channel != null) {
channel.writeAndFlush(new NetworkMessage(response, remoteAddress));
}
} else if (msg instanceof MqttPublishMessage) {
DeviceSession deviceSession = getDeviceSession(channel, remoteAddress);
if (deviceSession == null) {
return null;
}
List<Position> positions = new LinkedList<>();
MqttPublishMessage message = (MqttPublishMessage) msg;
ByteBuf buf = message.payload();
buf.readUnsignedByte(); // structure version
while (buf.readableBytes() > 1) {
int type = buf.readUnsignedByte();
int length = buf.readUnsignedShortLE();
ByteBuf record = buf.readSlice(length);
if (type == 1) {
Position position = new Position(getProtocolName());
position.setDeviceId(deviceSession.getDeviceId());
position.setTime(new Date(record.readUnsignedIntLE() * 1000));
while (record.readableBytes() > 0) {
int sensorType = record.readUnsignedByte();
int sensorId = record.readUnsignedShortLE();
if (sensorType == 14) {
position.setValid(true);
position.setLatitude(record.readFloatLE());
position.setLongitude(record.readFloatLE());
position.setSpeed(UnitsConverter.knotsFromKph(record.readUnsignedShortLE()));
position.set(Position.KEY_HDOP, record.readUnsignedByte());
position.set(Position.KEY_SATELLITES, record.readUnsignedByte());
position.setCourse(record.readUnsignedShortLE());
position.setAltitude(record.readShortLE());
} else {
if (sensorType == 3) {
continue;
}
decodeSensor(position, record, sensorType, sensorId);
}
}
positions.add(position);
} else if (type == 3) {
Position position = new Position(getProtocolName());
position.setDeviceId(deviceSession.getDeviceId());
getLastLocation(position, new Date(record.readUnsignedIntLE() * 1000));
record.readUnsignedByte(); // function identifier
position.set(Position.KEY_EVENT, record.readUnsignedByte());
positions.add(position);
}
}
buf.readUnsignedByte(); // checksum
MqttMessage response = MqttMessageBuilders.pubAck()
.packetId(message.variableHeader().packetId())
.build();
if (channel != null) {
channel.writeAndFlush(new NetworkMessage(response, remoteAddress));
}
return positions.isEmpty() ? null : positions;
}
return null;
}
}
|