diff options
author | Anton Tananaev <anton.tananaev@gmail.com> | 2021-06-12 13:36:08 -0700 |
---|---|---|
committer | Anton Tananaev <anton.tananaev@gmail.com> | 2021-06-12 13:36:08 -0700 |
commit | 83c66842e675eee90f59080d37c919e0e7fed4df (patch) | |
tree | 7078ef6c163d765a44ae69498c189ade05762404 /src | |
parent | 1580ad6d6e2fd20ba75438163b8c3eb65f62f174 (diff) | |
download | trackermap-server-83c66842e675eee90f59080d37c919e0e7fed4df.tar.gz trackermap-server-83c66842e675eee90f59080d37c919e0e7fed4df.tar.bz2 trackermap-server-83c66842e675eee90f59080d37c919e0e7fed4df.zip |
Support extended Portman format
Diffstat (limited to 'src')
-rw-r--r-- | src/main/java/org/traccar/protocol/PortmanProtocolDecoder.java | 83 | ||||
-rw-r--r-- | src/test/java/org/traccar/protocol/PortmanProtocolDecoderTest.java | 3 |
2 files changed, 79 insertions, 7 deletions
diff --git a/src/main/java/org/traccar/protocol/PortmanProtocolDecoder.java b/src/main/java/org/traccar/protocol/PortmanProtocolDecoder.java index 7d734c9b1..ebb93abd6 100644 --- a/src/main/java/org/traccar/protocol/PortmanProtocolDecoder.java +++ b/src/main/java/org/traccar/protocol/PortmanProtocolDecoder.java @@ -1,5 +1,5 @@ /* - * Copyright 2020 Anton Tananaev (anton@traccar.org) + * Copyright 2020 - 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. @@ -33,9 +33,9 @@ public class PortmanProtocolDecoder extends BaseProtocolDecoder { super(protocol); } - private static final Pattern PATTERN = new PatternBuilder() + private static final Pattern PATTERN_STANDARD = new PatternBuilder() .text("$PTMLA,") // header - .number("(d+),") // imei + .expression("([^,]+),") // id .expression("([ABCL]),") // validity .number("(dd)(dd)(dd)") // date (yymmdd) .number("(dd)(dd)(dd),") // time (hhmmss) @@ -55,11 +55,9 @@ public class PortmanProtocolDecoder extends BaseProtocolDecoder { .number("(?:G(d+)|[^,]*)") // fuel .compile(); - @Override - protected Object decode( - Channel channel, SocketAddress remoteAddress, Object msg) throws Exception { + private Object decodeStandard(Channel channel, SocketAddress remoteAddress, String sentence) { - Parser parser = new Parser(PATTERN, (String) msg); + Parser parser = new Parser(PATTERN_STANDARD, sentence); if (!parser.matches()) { return null; } @@ -91,4 +89,75 @@ public class PortmanProtocolDecoder extends BaseProtocolDecoder { return position; } + private static final Pattern PATTERN_EXTENDED = new PatternBuilder() + .text("$EXT,") // header + .expression("([^,]+),") // id + .expression("([ABCL]),") // validity + .number("(dd)(dd)(dd)") // date (yymmdd) + .number("(dd)(dd)(dd),") // time (hhmmss) + .expression("([NS])") + .number("(dd)(dd.d+)") // latitude + .expression("([EW])") + .number("(d{2,3})(dd.d+),") // longitude + .number("(d+),") // speed + .number("(d+),") // course + .number("(?:NA|C(-?d+)),") // temperature + .number("(?:NA|F(d+)),") // fuel + .number("(d+),") // satellites + .number("(d+),") // rssi + .number("(d+.d+),") // odometer + .number("(?:NA|(d+)),") // card id + .number("(x{8}),") // status + .number("(d+)") // event + .any() + .compile(); + + private Object decodeExtended(Channel channel, SocketAddress remoteAddress, String sentence) { + + Parser parser = new Parser(PATTERN_EXTENDED, sentence); + if (!parser.matches()) { + return null; + } + + DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, parser.next()); + if (deviceSession == null) { + return null; + } + + Position position = new Position(getProtocolName()); + position.setDeviceId(deviceSession.getDeviceId()); + + position.setValid(!parser.next().equals("L")); + position.setTime(parser.nextDateTime()); + position.setLatitude(parser.nextCoordinate(Parser.CoordinateFormat.HEM_DEG_MIN)); + position.setLongitude(parser.nextCoordinate(Parser.CoordinateFormat.HEM_DEG_MIN)); + position.setSpeed(UnitsConverter.knotsFromKph(parser.nextInt())); + position.setCourse(parser.nextInt()); + + position.set(Position.PREFIX_TEMP + 1, parser.next()); + position.set(Position.KEY_FUEL_LEVEL, parser.nextInt()); + position.set(Position.KEY_SATELLITES, parser.nextInt()); + position.set(Position.KEY_RSSI, parser.nextInt()); + position.set(Position.KEY_ODOMETER, parser.nextDouble() * 1000); + position.set(Position.KEY_DRIVER_UNIQUE_ID, parser.next()); + position.set(Position.KEY_STATUS, parser.nextHexLong()); + position.set(Position.KEY_EVENT, parser.nextInt()); + + return position; + } + + @Override + protected Object decode( + Channel channel, SocketAddress remoteAddress, Object msg) throws Exception { + + String sentence = (String) msg; + if (sentence.startsWith("$PTMLA")) { + return decodeStandard(channel, remoteAddress, sentence); + } else if (sentence.startsWith("$EXT")) { + return decodeExtended(channel, remoteAddress, sentence); + } else { + return null; + } + } + } diff --git a/src/test/java/org/traccar/protocol/PortmanProtocolDecoderTest.java b/src/test/java/org/traccar/protocol/PortmanProtocolDecoderTest.java index 5cc98d7e4..37798d960 100644 --- a/src/test/java/org/traccar/protocol/PortmanProtocolDecoderTest.java +++ b/src/test/java/org/traccar/protocol/PortmanProtocolDecoderTest.java @@ -11,6 +11,9 @@ public class PortmanProtocolDecoderTest extends ProtocolTest { var decoder = new PortmanProtocolDecoder(null); verifyPosition(decoder, text( + "$EXT,P0RTMANGRANT,A,210609201710,N0951.6879W08357.0129,0,0,NA,NA,11,25,174700.25,NA,01820000,108")); + + verifyPosition(decoder, text( "$PTMLA,355854050074633,A,200612153351,N2543.0681W10009.2974,0,190,NA,C9830000,NA,108,8,2.66,16,GNA")); } |