aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/org/traccar/protocol/FreematicsProtocolDecoder.java
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/java/org/traccar/protocol/FreematicsProtocolDecoder.java')
-rw-r--r--src/main/java/org/traccar/protocol/FreematicsProtocolDecoder.java177
1 files changed, 94 insertions, 83 deletions
diff --git a/src/main/java/org/traccar/protocol/FreematicsProtocolDecoder.java b/src/main/java/org/traccar/protocol/FreematicsProtocolDecoder.java
index b6d7f4e45..aded35823 100644
--- a/src/main/java/org/traccar/protocol/FreematicsProtocolDecoder.java
+++ b/src/main/java/org/traccar/protocol/FreematicsProtocolDecoder.java
@@ -1,5 +1,5 @@
/*
- * Copyright 2018 Anton Tananaev (anton@traccar.org)
+ * 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.
@@ -75,9 +75,9 @@ public class FreematicsProtocolDecoder extends BaseProtocolDecoder {
}
private Object decodePosition(
- Channel channel, SocketAddress remoteAddress, String sentence) throws Exception {
+ Channel channel, SocketAddress remoteAddress, String sentence, String id) {
- DeviceSession deviceSession = getDeviceSession(channel, remoteAddress);
+ DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, id);
if (deviceSession == null) {
return null;
}
@@ -88,94 +88,104 @@ public class FreematicsProtocolDecoder extends BaseProtocolDecoder {
for (String pair : sentence.split(",")) {
String[] data = pair.split("[=:]");
- int key = Integer.parseInt(data[0], 16);
+ int key;
+ try {
+ key = Integer.parseInt(data[0], 16);
+ } catch (NumberFormatException e) {
+ continue;
+ }
String value = data[1];
- switch (key) {
- case 0x0:
- if (position != null) {
- position.setTime(dateBuilder.getDate());
- positions.add(position);
- }
- position = new Position(getProtocolName());
- position.setDeviceId(deviceSession.getDeviceId());
- position.setValid(true);
- dateBuilder = new DateBuilder(new Date());
- break;
- 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)));
- break;
- 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);
- break;
- case 0xA:
- position.setLatitude(Double.parseDouble(value));
- break;
- case 0xB:
- position.setLongitude(Double.parseDouble(value));
- break;
- case 0xC:
- position.setAltitude(Double.parseDouble(value));
- break;
- case 0xD:
- position.setSpeed(UnitsConverter.knotsFromKph(Double.parseDouble(value)));
- break;
- case 0xE:
- position.setCourse(Integer.parseInt(value));
- break;
- case 0xF:
- position.set(Position.KEY_SATELLITES, Integer.parseInt(value));
- break;
- case 0x12:
- position.set(Position.KEY_HDOP, Integer.parseInt(value));
- break;
- case 0x20:
- position.set(Position.KEY_ACCELERATION, value);
- break;
- case 0x24:
- position.set(Position.KEY_BATTERY, Integer.parseInt(value) * 0.01);
- break;
- case 0x81:
- position.set(Position.KEY_RSSI, Integer.parseInt(value));
- break;
- case 0x82:
- position.set(Position.KEY_DEVICE_TEMP, Integer.parseInt(value) * 0.1);
- break;
- case 0x104:
- position.set(Position.KEY_ENGINE_LOAD, Integer.parseInt(value));
- break;
- case 0x105:
- position.set(Position.KEY_COOLANT_TEMP, Integer.parseInt(value));
- break;
- case 0x10c:
- position.set(Position.KEY_RPM, Integer.parseInt(value));
- break;
- case 0x10d:
- position.set(Position.KEY_OBD_SPEED, UnitsConverter.knotsFromKph(Integer.parseInt(value)));
- break;
- case 0x111:
- position.set(Position.KEY_THROTTLE, Integer.parseInt(value));
- break;
- default:
- position.set(Position.PREFIX_IO + key, value);
- break;
+ 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)));
+ break;
+ 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);
+ break;
+ case 0xA:
+ position.setValid(true);
+ position.setLatitude(Double.parseDouble(value));
+ break;
+ case 0xB:
+ position.setValid(true);
+ position.setLongitude(Double.parseDouble(value));
+ break;
+ case 0xC:
+ position.setAltitude(Double.parseDouble(value));
+ break;
+ case 0xD:
+ position.setSpeed(UnitsConverter.knotsFromKph(Double.parseDouble(value)));
+ break;
+ case 0xE:
+ position.setCourse(Integer.parseInt(value));
+ break;
+ case 0xF:
+ position.set(Position.KEY_SATELLITES, Integer.parseInt(value));
+ break;
+ case 0x12:
+ position.set(Position.KEY_HDOP, Integer.parseInt(value));
+ break;
+ case 0x20:
+ position.set(Position.KEY_ACCELERATION, value);
+ break;
+ case 0x24:
+ position.set(Position.KEY_BATTERY, Integer.parseInt(value) * 0.01);
+ break;
+ case 0x81:
+ position.set(Position.KEY_RSSI, Integer.parseInt(value));
+ break;
+ case 0x82:
+ position.set(Position.KEY_DEVICE_TEMP, Integer.parseInt(value) * 0.1);
+ break;
+ case 0x104:
+ position.set(Position.KEY_ENGINE_LOAD, Integer.parseInt(value));
+ break;
+ case 0x105:
+ position.set(Position.KEY_COOLANT_TEMP, Integer.parseInt(value));
+ break;
+ case 0x10c:
+ position.set(Position.KEY_RPM, Integer.parseInt(value));
+ break;
+ case 0x10d:
+ position.set(Position.KEY_OBD_SPEED, UnitsConverter.knotsFromKph(Integer.parseInt(value)));
+ break;
+ case 0x111:
+ position.set(Position.KEY_THROTTLE, Integer.parseInt(value));
+ break;
+ default:
+ position.set(Position.PREFIX_IO + key, value);
+ break;
+ }
}
}
if (position != null) {
+ if (!position.getValid()) {
+ getLastLocation(position, null);
+ }
position.setTime(dateBuilder.getDate());
positions.add(position);
}
- return positions;
+ return positions.isEmpty() ? null : positions;
}
@Override
@@ -187,12 +197,13 @@ public class FreematicsProtocolDecoder extends BaseProtocolDecoder {
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);
+ return decodePosition(channel, remoteAddress, sentence, id);
}
}