diff options
author | Anton Tananaev <anton.tananaev@gmail.com> | 2018-08-10 17:45:43 +1200 |
---|---|---|
committer | Anton Tananaev <anton.tananaev@gmail.com> | 2018-08-10 17:45:59 +1200 |
commit | a567564ea3d91e0597b17bf6c75bf2595c7c84d6 (patch) | |
tree | e3d10d5eecf3b0869d03c1563fe9cd95231e62c3 /src/org/traccar | |
parent | 69412701cb57346240e723e2760a6211a434120f (diff) | |
download | traccar-server-a567564ea3d91e0597b17bf6c75bf2595c7c84d6.tar.gz traccar-server-a567564ea3d91e0597b17bf6c75bf2595c7c84d6.tar.bz2 traccar-server-a567564ea3d91e0597b17bf6c75bf2595c7c84d6.zip |
Extend Atrack text format support
Diffstat (limited to 'src/org/traccar')
-rw-r--r-- | src/org/traccar/protocol/AtrackProtocolDecoder.java | 63 |
1 files changed, 53 insertions, 10 deletions
diff --git a/src/org/traccar/protocol/AtrackProtocolDecoder.java b/src/org/traccar/protocol/AtrackProtocolDecoder.java index f55040e79..b2753f93b 100644 --- a/src/org/traccar/protocol/AtrackProtocolDecoder.java +++ b/src/org/traccar/protocol/AtrackProtocolDecoder.java @@ -32,11 +32,15 @@ import org.traccar.model.Position; import java.net.SocketAddress; import java.nio.charset.StandardCharsets; +import java.text.DateFormat; +import java.text.ParseException; +import java.text.SimpleDateFormat; import java.util.Date; import java.util.HashMap; import java.util.LinkedList; import java.util.List; import java.util.Map; +import java.util.TimeZone; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -99,7 +103,24 @@ public class AtrackProtocolDecoder extends BaseProtocolDecoder { return result; } - private void readCustomData(Position position, ByteBuf buf, String form) { + private void readTextCustomData(Position position, String data, String form) { + String[] keys = form.substring(1).split("%"); + String[] values = data.split(","); + for (int i = 0; i < Math.min(keys.length, values.length); i++) { + switch (keys[i]) { + case "MV": + position.set(Position.KEY_POWER, Integer.parseInt(values[i]) * 0.1); + break; + case "BV": + position.set(Position.KEY_BATTERY, Integer.parseInt(values[i]) * 0.1); + break; + default: + break; + } + } + } + + private void readBinaryCustomData(Position position, ByteBuf buf, String form) { CellTower cellTower = new CellTower(); String[] keys = form.substring(1).split("%"); for (String key : keys) { @@ -108,10 +129,10 @@ public class AtrackProtocolDecoder extends BaseProtocolDecoder { position.set(Position.KEY_SATELLITES, buf.readUnsignedByte()); break; case "MV": - position.set(Position.KEY_POWER, buf.readUnsignedShort()); + position.set(Position.KEY_POWER, buf.readUnsignedShort() * 0.1); break; case "BV": - position.set(Position.KEY_BATTERY, buf.readUnsignedShort()); + position.set(Position.KEY_BATTERY, buf.readUnsignedShort() * 0.1); break; case "GQ": cellTower.setSignalStrength((int) buf.readUnsignedByte()); @@ -266,8 +287,7 @@ public class AtrackProtocolDecoder extends BaseProtocolDecoder { .number("d+,") // length .number("d+,") // index .number("(d+),") // imei - .number("(dddd)(dd)(dd)") // date (yymmdd) - .number("(dd)(dd)(dd),") // time (hhmmss) + .number("(d+),") // date and time .number("d+,") // rtc date and time .number("d+,") // device date and time .number("(-?d+),") // longitude @@ -283,7 +303,9 @@ public class AtrackProtocolDecoder extends BaseProtocolDecoder { .number("[^,]*,") // driver .number("(d+),") // temp1 .number("(d+),") // temp2 - .any() + .expression("[^,]*,") // text message + .expression("(.*)") // custom data + .optional(2) .compile(); private Position decodeText(Channel channel, SocketAddress remoteAddress, String sentence) { @@ -302,7 +324,20 @@ public class AtrackProtocolDecoder extends BaseProtocolDecoder { position.setDeviceId(deviceSession.getDeviceId()); position.setValid(true); - position.setTime(parser.nextDateTime()); + + String time = parser.next(); + if (time.length() >= 14) { + try { + DateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmss"); + dateFormat.setTimeZone(TimeZone.getTimeZone("UTC")); + position.setTime(dateFormat.parse(time)); + } catch (ParseException e) { + throw new RuntimeException(e); + } + } else { + position.setTime(new Date(Long.parseLong(time) * 1000)); + } + position.setLongitude(parser.nextInt() * 0.000001); position.setLatitude(parser.nextInt() * 0.000001); position.setCourse(parser.nextInt()); @@ -319,6 +354,16 @@ public class AtrackProtocolDecoder extends BaseProtocolDecoder { position.set(Position.PREFIX_TEMP + 1, parser.nextInt()); position.set(Position.PREFIX_TEMP + 2, parser.nextInt()); + if (custom) { + String data = parser.next(); + String form = this.form; + if (form == null) { + form = data.substring(0, data.indexOf(',')).substring("%CI".length()); + data = data.substring(data.indexOf(',') + 1); + } + readTextCustomData(position, data, form); + } + return position; } @@ -353,9 +398,7 @@ public class AtrackProtocolDecoder extends BaseProtocolDecoder { buf.skipBytes(7 + 7); - } else { - position.setFixTime(new Date(buf.readUnsignedInt() * 1000)); position.setDeviceTime(new Date(buf.readUnsignedInt() * 1000)); buf.readUnsignedInt(); // send time @@ -401,7 +444,7 @@ public class AtrackProtocolDecoder extends BaseProtocolDecoder { if (form == null) { form = readString(buf).substring("%CI".length()); } - readCustomData(position, buf, form); + readBinaryCustomData(position, buf, form); } positions.add(position); |