From 911c44c63567dff7af58544d998adc22d23df672 Mon Sep 17 00:00:00 2001 From: Kostiantyn Luzan Date: Sun, 25 Feb 2018 02:33:06 +0200 Subject: Add support for OpenGTS compatible clients (GPSLogger for Android) Signed-off-by: Kostiantyn Luzan --- setup/default.xml | 1 + src/org/traccar/protocol/OpenGtsProtocol.java | 45 ++++++++ .../traccar/protocol/OpenGtsProtocolDecoder.java | 115 +++++++++++++++++++++ .../protocol/OpenGtsProtocolDecoderTest.java | 18 ++++ 4 files changed, 179 insertions(+) create mode 100644 src/org/traccar/protocol/OpenGtsProtocol.java create mode 100644 src/org/traccar/protocol/OpenGtsProtocolDecoder.java create mode 100644 test/org/traccar/protocol/OpenGtsProtocolDecoderTest.java diff --git a/setup/default.xml b/setup/default.xml index 2c4f45712..6be2c08e3 100644 --- a/setup/default.xml +++ b/setup/default.xml @@ -227,5 +227,6 @@ 5156 5157 5158 + 5159 diff --git a/src/org/traccar/protocol/OpenGtsProtocol.java b/src/org/traccar/protocol/OpenGtsProtocol.java new file mode 100644 index 000000000..2a20cb159 --- /dev/null +++ b/src/org/traccar/protocol/OpenGtsProtocol.java @@ -0,0 +1,45 @@ +/* + * Copyright 2015 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 org.jboss.netty.bootstrap.ServerBootstrap; +import org.jboss.netty.channel.ChannelPipeline; +import org.jboss.netty.handler.codec.http.HttpRequestDecoder; +import org.jboss.netty.handler.codec.http.HttpResponseEncoder; +import org.traccar.BaseProtocol; +import org.traccar.TrackerServer; + +import java.util.List; + +public class OpenGtsProtocol extends BaseProtocol { + + public OpenGtsProtocol() { + super("opengts"); + } + + @Override + public void initTrackerServers(List serverList) { + serverList.add(new TrackerServer(new ServerBootstrap(), getName()) { + @Override + protected void addSpecificHandlers(ChannelPipeline pipeline) { + pipeline.addLast("httpEncoder", new HttpResponseEncoder()); + pipeline.addLast("httpDecoder", new HttpRequestDecoder()); + pipeline.addLast("objectDecoder", new OpenGtsProtocolDecoder(OpenGtsProtocol.this)); + } + }); + } + +} diff --git a/src/org/traccar/protocol/OpenGtsProtocolDecoder.java b/src/org/traccar/protocol/OpenGtsProtocolDecoder.java new file mode 100644 index 000000000..9be69ed7f --- /dev/null +++ b/src/org/traccar/protocol/OpenGtsProtocolDecoder.java @@ -0,0 +1,115 @@ +/* + * Copyright 2013 - 2017 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 org.jboss.netty.channel.Channel; +import org.jboss.netty.handler.codec.http.HttpRequest; +import org.jboss.netty.handler.codec.http.HttpResponseStatus; +import org.jboss.netty.handler.codec.http.QueryStringDecoder; +import org.traccar.BaseHttpProtocolDecoder; +import org.traccar.DeviceSession; +import org.traccar.helper.DateBuilder; +import org.traccar.helper.Parser; +import org.traccar.helper.PatternBuilder; +import org.traccar.model.Position; + +import java.net.SocketAddress; +import java.util.List; +import java.util.Map; +import java.util.regex.Pattern; + +public class OpenGtsProtocolDecoder extends BaseHttpProtocolDecoder { + + private static final Pattern PATTERN = new PatternBuilder() + .text("$GPRMC,") + .number("(dd)(dd)(dd),") // time (hhmmss) + .expression("([AV]),") // validity + .number("(d+)(dd.d+),") // latitude + .expression("([NS]),") + .number("(d+)(dd.d+),") // longitude + .expression("([EW]),") + .number("(d+.d+),") // speed + .number("(d+.d+),") // course + .number("(dd)(dd)(dd),") // date (ddmmyy) + .any() + .compile(); + + public OpenGtsProtocolDecoder(OpenGtsProtocol protocol) { + super(protocol); + } + + @Override + protected Object decode( + Channel channel, SocketAddress remoteAddress, Object msg) throws Exception { + + HttpRequest request = (HttpRequest) msg; + QueryStringDecoder decoder = new QueryStringDecoder(request.getUri()); + Map> params = decoder.getParameters(); + + Position position = new Position(); + position.setProtocol(getProtocolName()); + + for (Map.Entry> entry : params.entrySet()) { + String value = entry.getValue().get(0); + switch (entry.getKey()) { + case "id": + DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, value); + if (deviceSession == null) { + sendResponse(channel, HttpResponseStatus.BAD_REQUEST); + return null; + } + position.setDeviceId(deviceSession.getDeviceId()); + break; + case "gprmc": + Parser parser = new Parser(PATTERN, value); + if (!parser.matches()) { + sendResponse(channel, HttpResponseStatus.BAD_REQUEST); + return null; + } + + DateBuilder dateBuilder = new DateBuilder() + .setTime(parser.nextInt(0), parser.nextInt(0), parser.nextInt(0)); + + position.setValid(parser.next().equals("A")); + position.setLatitude(parser.nextCoordinate()); + position.setLongitude(parser.nextCoordinate()); + position.setSpeed(parser.nextDouble(0)); + position.setCourse(parser.nextDouble(0)); + + dateBuilder.setDateReverse(parser.nextInt(0), parser.nextInt(0), parser.nextInt(0)); + position.setTime(dateBuilder.getDate()); + break; + case "alt": + position.setAltitude(Double.parseDouble(value)); + break; + case "batt": + position.set(Position.KEY_BATTERY_LEVEL, Double.parseDouble(value)); + break; + default: + break; + } + } + + if (position.getDeviceId() != 0) { + sendResponse(channel, HttpResponseStatus.OK); + return position; + } else { + sendResponse(channel, HttpResponseStatus.BAD_REQUEST); + return null; + } + } + +} diff --git a/test/org/traccar/protocol/OpenGtsProtocolDecoderTest.java b/test/org/traccar/protocol/OpenGtsProtocolDecoderTest.java new file mode 100644 index 000000000..0e6be8d10 --- /dev/null +++ b/test/org/traccar/protocol/OpenGtsProtocolDecoderTest.java @@ -0,0 +1,18 @@ +package org.traccar.protocol; + +import org.junit.Test; +import org.traccar.ProtocolTest; + +public class OpenGtsProtocolDecoderTest extends ProtocolTest { + + @Test + public void testDecode() throws Exception { + + OpenGtsProtocolDecoder decoder = new OpenGtsProtocolDecoder(new OpenGtsProtocol()); + + verifyPosition(decoder, request( + "/?id=123456789012345&dev=dev_name&acct=account&batt=0&code=0xF020&alt=160.5&gprmc=$GPRMC,191555,A,5025.46624,N,3030.39937,E,0.000000,0.000000,200218,,*2F")); + + } + +} -- cgit v1.2.3 From a79386202fcf1feb86b7770cbbbcfe65fa4a95ca Mon Sep 17 00:00:00 2001 From: Anton Tananaev Date: Thu, 1 Mar 2018 15:31:05 +1300 Subject: Handle H02 message new line --- src/org/traccar/protocol/H02ProtocolDecoder.java | 2 +- test/org/traccar/protocol/H02ProtocolDecoderTest.java | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/org/traccar/protocol/H02ProtocolDecoder.java b/src/org/traccar/protocol/H02ProtocolDecoder.java index 5ee663ee1..9764adf04 100644 --- a/src/org/traccar/protocol/H02ProtocolDecoder.java +++ b/src/org/traccar/protocol/H02ProtocolDecoder.java @@ -456,7 +456,7 @@ public class H02ProtocolDecoder extends BaseProtocolDecoder { switch (marker) { case "*": - String sentence = buf.toString(StandardCharsets.US_ASCII); + String sentence = buf.toString(StandardCharsets.US_ASCII).trim(); int typeStart = sentence.indexOf(',', sentence.indexOf(',') + 1) + 1; int typeEnd = sentence.indexOf(',', typeStart); if (typeEnd > 0) { diff --git a/test/org/traccar/protocol/H02ProtocolDecoderTest.java b/test/org/traccar/protocol/H02ProtocolDecoderTest.java index b5dcd9ffe..adbdc0a07 100644 --- a/test/org/traccar/protocol/H02ProtocolDecoderTest.java +++ b/test/org/traccar/protocol/H02ProtocolDecoderTest.java @@ -156,7 +156,7 @@ public class H02ProtocolDecoderTest extends ProtocolTest { "*HQ,2705171109,V1,213324,A,5002.5849,N,01433.7822,E,0.00,000,140613,FFFFFFFF#")); verifyPosition(decoder, buffer( - "*TH,2020916012,V1,050316,A,2212.8745,N,11346.6574,E,14.28,028,220902,FFFFFBFF#")); + "*TH,2020916012,V1,050316,A,2212.8745,N,11346.6574,E,14.28,028,220902,FFFFFBFF#\r\n")); verifyPosition(decoder, buffer( "*TH,2020916012,V4,S17,130305,050316,A,2212.8745,N,11346.6574,E,14.28,028,220902,FFFFFBFF#")); -- cgit v1.2.3 From 10cc148834ac5bb694d9ba482f65a2e71faebbb4 Mon Sep 17 00:00:00 2001 From: Anton Tananaev Date: Fri, 2 Mar 2018 05:32:51 +1300 Subject: Support new TAIP message type --- src/org/traccar/protocol/TaipProtocolDecoder.java | 2 +- test/org/traccar/protocol/TaipProtocolDecoderTest.java | 3 +++ 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/src/org/traccar/protocol/TaipProtocolDecoder.java b/src/org/traccar/protocol/TaipProtocolDecoder.java index 9c98799fb..81bebdef7 100644 --- a/src/org/traccar/protocol/TaipProtocolDecoder.java +++ b/src/org/traccar/protocol/TaipProtocolDecoder.java @@ -47,7 +47,7 @@ public class TaipProtocolDecoder extends BaseProtocolDecoder { .groupEnd("?") .number("(d{5})") // seconds .or() - .expression("(?:RGP|RCQ|RBR)") // type + .expression("(?:RGP|RCQ|RCV|RBR)") // type .number("(dd)?") // event .number("(dd)(dd)(dd)") // date (mmddyy) .number("(dd)(dd)(dd)") // time (hhmmss) diff --git a/test/org/traccar/protocol/TaipProtocolDecoderTest.java b/test/org/traccar/protocol/TaipProtocolDecoderTest.java index cdf5ff4a3..a92e82498 100644 --- a/test/org/traccar/protocol/TaipProtocolDecoderTest.java +++ b/test/org/traccar/protocol/TaipProtocolDecoderTest.java @@ -10,6 +10,9 @@ public class TaipProtocolDecoderTest extends ProtocolTest { TaipProtocolDecoder decoder = new TaipProtocolDecoder(new TaipProtocol()); + verifyPosition(decoder, text( + ">RCV12270218010247-3471349-058400030002057F001200020A1D013010600001509+0000FF+0000FF;#1DE2;ID=7196;*03<")); + verifyPosition(decoder, text( ">RPV03874+3477708-0923453100029212;ID=0017;*71<")); -- cgit v1.2.3 From 9246922750d211b1c6ec36d38a63928f3e21a0a6 Mon Sep 17 00:00:00 2001 From: Anton Tananaev Date: Fri, 2 Mar 2018 14:41:35 +1300 Subject: Revert: Decode additional Totem alarm codes --- src/org/traccar/protocol/TotemProtocolDecoder.java | 20 +------------------- 1 file changed, 1 insertion(+), 19 deletions(-) diff --git a/src/org/traccar/protocol/TotemProtocolDecoder.java b/src/org/traccar/protocol/TotemProtocolDecoder.java index 1c5130a6c..2f205ba58 100644 --- a/src/org/traccar/protocol/TotemProtocolDecoder.java +++ b/src/org/traccar/protocol/TotemProtocolDecoder.java @@ -167,26 +167,12 @@ public class TotemProtocolDecoder extends BaseProtocolDecoder { switch (value) { case 0x01: return Position.ALARM_SOS; - case 0x02: - return Position.ALARM_OVERSPEED; - case 0x04: - return Position.ALARM_GEOFENCE_EXIT; - case 0x05: - return Position.ALARM_GEOFENCE_ENTER; - case 0x06: - return Position.ALARM_TOW; case 0x10: return Position.ALARM_LOW_BATTERY; case 0x11: return Position.ALARM_OVERSPEED; - case 0x12: - return Position.ALARM_LOW_POWER; - case 0x13: - return Position.ALARM_LOW_BATTERY; case 0x30: return Position.ALARM_PARKING; - case 0x40: - return Position.ALARM_SHOCK; case 0x42: return Position.ALARM_GEOFENCE_EXIT; case 0x43: @@ -259,11 +245,7 @@ public class TotemProtocolDecoder extends BaseProtocolDecoder { private boolean decode3(Position position, Parser parser) { if (parser.hasNext()) { - short alarm = Short.parseShort(parser.next(), 16); - position.set(Position.KEY_ALARM, decodeAlarm(alarm)); - if (alarm >= 0x21 && alarm <= 0x28) { - position.set(Position.PREFIX_IN + ((alarm - 0x21) / 2 + 1), alarm % 2 > 0); - } + position.set(Position.KEY_ALARM, decodeAlarm(Short.parseShort(parser.next(), 16))); } position.setTime(parser.nextDateTime(Parser.DateTimeFormat.DMY_HMS)); -- cgit v1.2.3 From 019c3acb9245fac1a072400676193f0696c512d7 Mon Sep 17 00:00:00 2001 From: Anton Tananaev Date: Fri, 2 Mar 2018 14:59:39 +1300 Subject: Improve Totem AT09 status decoding --- src/org/traccar/protocol/TotemProtocolDecoder.java | 23 ++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/src/org/traccar/protocol/TotemProtocolDecoder.java b/src/org/traccar/protocol/TotemProtocolDecoder.java index 2f205ba58..7d8a22f8c 100644 --- a/src/org/traccar/protocol/TotemProtocolDecoder.java +++ b/src/org/traccar/protocol/TotemProtocolDecoder.java @@ -18,6 +18,7 @@ package org.traccar.protocol; import org.jboss.netty.channel.Channel; import org.traccar.BaseProtocolDecoder; import org.traccar.DeviceSession; +import org.traccar.helper.BitUtil; import org.traccar.helper.DateBuilder; import org.traccar.helper.Parser; import org.traccar.helper.PatternBuilder; @@ -276,7 +277,26 @@ public class TotemProtocolDecoder extends BaseProtocolDecoder { private boolean decode4(Position position, Parser parser) { - position.set(Position.KEY_STATUS, parser.next()); + long status = parser.nextHexLong(); + + position.set(Position.KEY_ALARM, BitUtil.check(status, 32 - 1) ? Position.ALARM_SOS : null); + position.set(Position.KEY_IGNITION, BitUtil.check(status, 32 - 2)); + position.set(Position.KEY_ALARM, BitUtil.check(status, 32 - 3) ? Position.ALARM_OVERSPEED : null); + position.set(Position.KEY_CHARGE, BitUtil.check(status, 32 - 4)); + position.set(Position.KEY_ALARM, BitUtil.check(status, 32 - 5) ? Position.ALARM_GEOFENCE_EXIT : null); + position.set(Position.KEY_ALARM, BitUtil.check(status, 32 - 6) ? Position.ALARM_GEOFENCE_ENTER : null); + position.set(Position.PREFIX_OUT + 1, BitUtil.check(status, 32 - 9)); + position.set(Position.PREFIX_OUT + 2, BitUtil.check(status, 32 - 10)); + position.set(Position.PREFIX_OUT + 3, BitUtil.check(status, 32 - 11)); + position.set(Position.PREFIX_OUT + 4, BitUtil.check(status, 32 - 12)); + position.set(Position.PREFIX_IN + 2, BitUtil.check(status, 32 - 13)); + position.set(Position.PREFIX_IN + 3, BitUtil.check(status, 32 - 14)); + position.set(Position.PREFIX_IN + 4, BitUtil.check(status, 32 - 15)); + position.set(Position.KEY_ALARM, BitUtil.check(status, 32 - 16) ? Position.ALARM_SHOCK : null); + position.set(Position.KEY_ALARM, BitUtil.check(status, 32 - 18) ? Position.ALARM_LOW_BATTERY : null); + position.set(Position.KEY_ALARM, BitUtil.check(status, 32 - 22) ? Position.ALARM_JAMMING : null); + + position.setValid(BitUtil.check(status, 32 - 20)); position.setTime(parser.nextDateTime()); @@ -300,7 +320,6 @@ public class TotemProtocolDecoder extends BaseProtocolDecoder { position.set(Position.KEY_HDOP, parser.nextDouble(0)); position.set(Position.KEY_ODOMETER, parser.nextInt(0) * 1000); - position.setValid(true); position.setLatitude(parser.nextCoordinate()); position.setLongitude(parser.nextCoordinate()); -- cgit v1.2.3 From 9eb532a1193ed7a00c60a04e2d839a4e611992fd Mon Sep 17 00:00:00 2001 From: Anton Tananaev Date: Sat, 3 Mar 2018 11:17:01 +1300 Subject: Support additional TAIP attributes --- src/org/traccar/protocol/TaipProtocolDecoder.java | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/src/org/traccar/protocol/TaipProtocolDecoder.java b/src/org/traccar/protocol/TaipProtocolDecoder.java index 81bebdef7..9555d19e9 100644 --- a/src/org/traccar/protocol/TaipProtocolDecoder.java +++ b/src/org/traccar/protocol/TaipProtocolDecoder.java @@ -67,6 +67,19 @@ public class TaipProtocolDecoder extends BaseProtocolDecoder { .number("(ddd)") // battery .number("(x{8})") // odometer .number("[01]") // gps power + .groupBegin() + .number("[23]") // fix mode + .number("(dd)") // pdop + .number("dd") // satellites + .number("xxxx") // seconds from last + .number("[01]") // modem power + .number("[0-5]") // gsm status + .number("(dd)") // rssi + .number("([-+]dddd)") // temperature 1 + .number("xx") // seconds from last + .number("([-+]dddd)") // temperature 2 + .number("xx") // seconds from last + .groupEnd("?") .groupEnd("?") .any() .compile(); @@ -160,6 +173,13 @@ public class TaipProtocolDecoder extends BaseProtocolDecoder { position.set(Position.KEY_ODOMETER, parser.nextLong(16, 0)); } + if (parser.hasNext(4)) { + position.set(Position.KEY_PDOP, parser.nextInt()); + position.set(Position.KEY_RSSI, parser.nextInt()); + position.set(Position.PREFIX_TEMP + 1, parser.nextInt() * 0.01); + position.set(Position.PREFIX_TEMP + 2, parser.nextInt() * 0.01); + } + position.setValid(true); String[] attributes = null; -- cgit v1.2.3 From f8fb4e0476d050aadb66734836c785c1624fea38 Mon Sep 17 00:00:00 2001 From: Anton Tananaev Date: Sun, 4 Mar 2018 14:06:27 +1300 Subject: Add GPS103 OBD test case --- test/org/traccar/protocol/Gps103ProtocolDecoderTest.java | 3 +++ 1 file changed, 3 insertions(+) diff --git a/test/org/traccar/protocol/Gps103ProtocolDecoderTest.java b/test/org/traccar/protocol/Gps103ProtocolDecoderTest.java index acafe3982..668070e1a 100644 --- a/test/org/traccar/protocol/Gps103ProtocolDecoderTest.java +++ b/test/org/traccar/protocol/Gps103ProtocolDecoderTest.java @@ -10,6 +10,9 @@ public class Gps103ProtocolDecoderTest extends ProtocolTest { Gps103ProtocolDecoder decoder = new Gps103ProtocolDecoder(new Gps103Protocol()); + verifyAttributes(decoder, text( + "imei:359710048977327,OBD,180301094003,5000000,0.00,0.00,98,18,68.63%,55,25.10%,1368,14.24,,,,;")); + verifyAttributes(decoder, text( "imei:862106025092216,OBD,170605095949,195874,,370.8,808,066,30.0%,+87,13.0%,02444,14.3,,,,;")); -- cgit v1.2.3 From 88456dc2e2b3cc5a618b2a041c43230ed550f12f Mon Sep 17 00:00:00 2001 From: Anton Tananaev Date: Tue, 6 Mar 2018 07:14:45 +1300 Subject: Suntech ST600 series support --- src/org/traccar/protocol/SuntechProtocolDecoder.java | 15 +++++++++++---- test/org/traccar/protocol/SuntechProtocolDecoderTest.java | 6 ++++++ 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/src/org/traccar/protocol/SuntechProtocolDecoder.java b/src/org/traccar/protocol/SuntechProtocolDecoder.java index 58e670307..034894b52 100644 --- a/src/org/traccar/protocol/SuntechProtocolDecoder.java +++ b/src/org/traccar/protocol/SuntechProtocolDecoder.java @@ -21,6 +21,8 @@ import org.traccar.Context; import org.traccar.DeviceSession; import org.traccar.helper.BitUtil; import org.traccar.helper.UnitsConverter; +import org.traccar.model.CellTower; +import org.traccar.model.Network; import org.traccar.model.Position; import java.net.SocketAddress; @@ -156,7 +158,7 @@ public class SuntechProtocolDecoder extends BaseProtocolDecoder { } } - private Position decode235( + private Position decode2356( Channel channel, SocketAddress remoteAddress, String protocol, String[] values) throws ParseException { int index = 0; @@ -175,7 +177,7 @@ public class SuntechProtocolDecoder extends BaseProtocolDecoder { position.setDeviceId(deviceSession.getDeviceId()); position.set(Position.KEY_TYPE, type); - if (protocol.equals("ST300") || protocol.equals("ST500")) { + if (protocol.equals("ST300") || protocol.equals("ST500") || protocol.equals("ST600")) { index += 1; // model } @@ -186,7 +188,12 @@ public class SuntechProtocolDecoder extends BaseProtocolDecoder { position.setTime(dateFormat.parse(values[index++] + values[index++])); if (!protocol.equals("ST500")) { - index += 1; // cell + int cid = Integer.parseInt(values[index++], 16); + if (protocol.equals("ST600")) { + position.setNetwork(new Network(CellTower.from( + Integer.parseInt(values[index++]), Integer.parseInt(values[index++]), + Integer.parseInt(values[index++]), cid, Integer.parseInt(values[index++])))); + } } position.setLatitude(Double.parseDouble(values[index++])); @@ -369,7 +376,7 @@ public class SuntechProtocolDecoder extends BaseProtocolDecoder { } else if (values[0].equals("ST910")) { return decode9(channel, remoteAddress, values); } else { - return decode235(channel, remoteAddress, values[0].substring(0, 5), values); + return decode2356(channel, remoteAddress, values[0].substring(0, 5), values); } } diff --git a/test/org/traccar/protocol/SuntechProtocolDecoderTest.java b/test/org/traccar/protocol/SuntechProtocolDecoderTest.java index 566ffb012..bcc8a8b8b 100644 --- a/test/org/traccar/protocol/SuntechProtocolDecoderTest.java +++ b/test/org/traccar/protocol/SuntechProtocolDecoderTest.java @@ -27,6 +27,12 @@ public class SuntechProtocolDecoderTest extends ProtocolTest { SuntechProtocolDecoder decoder = new SuntechProtocolDecoder(new SuntechProtocol()); + verifyPosition(decoder, text( + "ST600STT;107850496;20;419;20180227;14:30:45;00462b08;736;3;4524;50;-16.479091;-068.119119;000.346;000.00;4;1;0;13.89;000000;1;0223;000003;0.0;0;0.00")); + + verifyPosition(decoder, text( + "ST600STT;100850000;01;010;20081017;07:41:56;0000004f;450;20;0023;24;+37.478519;+126.886819;000.012;000.00;9;1;0;15.30;00110000;1;0072;0;4.5;1;12.35")); + verifyPosition(decoder, text( "STT;100850000;3FFFFF;26;010;1;20161117;08:37:39;0000004F;450;0;0014;20;+37.479323;+126.887827;62.03;65.43;10;1;00000101;00001000;1;2;0492")); -- cgit v1.2.3 From 5ac0f773023a9c1c70524f1b63d2afc7d837a31f Mon Sep 17 00:00:00 2001 From: Abyss777 Date: Tue, 6 Mar 2018 13:52:44 +0500 Subject: Implement additional way to detect SMPP delivery receipts --- src/org/traccar/smpp/ClientSmppSessionHandler.java | 9 ++++++++- src/org/traccar/smpp/SmppClient.java | 12 +++++++++--- 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/src/org/traccar/smpp/ClientSmppSessionHandler.java b/src/org/traccar/smpp/ClientSmppSessionHandler.java index 69ef9af41..3585f8376 100644 --- a/src/org/traccar/smpp/ClientSmppSessionHandler.java +++ b/src/org/traccar/smpp/ClientSmppSessionHandler.java @@ -50,7 +50,14 @@ public class ClientSmppSessionHandler extends DefaultSmppSessionHandler { smppClient.mapDataCodingToCharset(((DeliverSm) request).getDataCoding())); Log.debug("SMS Message Received: " + message.trim() + ", Source Address: " + sourceAddress); - if (!SmppUtil.isMessageTypeAnyDeliveryReceipt(((DeliverSm) request).getEsmClass())) { + boolean isDeliveryReceipt = false; + if (smppClient.getDetectDlrByOpts()) { + isDeliveryReceipt = request.getOptionalParameters() != null; + } else { + isDeliveryReceipt = SmppUtil.isMessageTypeAnyDeliveryReceipt(((DeliverSm) request).getEsmClass()); + } + + if (!isDeliveryReceipt) { TextMessageEventHandler.handleTextMessage(sourceAddress, message); } } diff --git a/src/org/traccar/smpp/SmppClient.java b/src/org/traccar/smpp/SmppClient.java index 602087a65..0f7d2cf6d 100644 --- a/src/org/traccar/smpp/SmppClient.java +++ b/src/org/traccar/smpp/SmppClient.java @@ -61,7 +61,8 @@ public class SmppClient { private String sourceAddress; private String commandSourceAddress; private int submitTimeout; - private boolean requestDrl; + private boolean requestDlr; + private boolean detectDlrByOpts; private String notificationsCharsetName; private byte notificationsDataCoding; private String commandsCharsetName; @@ -92,7 +93,8 @@ public class SmppClient { commandSourceAddress = Context.getConfig().getString("sms.smpp.commandSourceAddress", sourceAddress); submitTimeout = Context.getConfig().getInteger("sms.smpp.submitTimeout", 10000); - requestDrl = Context.getConfig().getBoolean("sms.smpp.requestDrl"); + requestDlr = Context.getConfig().getBoolean("sms.smpp.requestDlr"); + detectDlrByOpts = Context.getConfig().getBoolean("sms.smpp.detectDlrByOpts"); notificationsCharsetName = Context.getConfig().getString("sms.smpp.notificationsCharset", CharsetUtil.NAME_UCS_2); @@ -153,6 +155,10 @@ public class SmppClient { } } + public boolean getDetectDlrByOpts() { + return detectDlrByOpts; + } + protected synchronized void reconnect() { try { disconnect(); @@ -213,7 +219,7 @@ public class SmppClient { byte[] textBytes; textBytes = CharsetUtil.encode(message, command ? commandsCharsetName : notificationsCharsetName); submit.setDataCoding(command ? commandsDataCoding : notificationsDataCoding); - if (requestDrl) { + if (requestDlr) { submit.setRegisteredDelivery(SmppConstants.REGISTERED_DELIVERY_SMSC_RECEIPT_REQUESTED); } submit.setShortMessage(textBytes); -- cgit v1.2.3 From 34499f29f0f20c4acecc455fde67c5332790e926 Mon Sep 17 00:00:00 2001 From: Anton Tananaev Date: Thu, 8 Mar 2018 05:30:22 +1300 Subject: Decode additional TL3000 data --- .../traccar/protocol/Ivt401ProtocolDecoder.java | 38 ++++++++++++++++++++++ .../protocol/Ivt401ProtocolDecoderTest.java | 3 ++ 2 files changed, 41 insertions(+) diff --git a/src/org/traccar/protocol/Ivt401ProtocolDecoder.java b/src/org/traccar/protocol/Ivt401ProtocolDecoder.java index 6e11a763c..d2f1d3d69 100644 --- a/src/org/traccar/protocol/Ivt401ProtocolDecoder.java +++ b/src/org/traccar/protocol/Ivt401ProtocolDecoder.java @@ -58,6 +58,23 @@ public class Ivt401ProtocolDecoder extends BaseProtocolDecoder { .number("(-?d+),") // tilt .number("(d+),") // trip .number("(d+),") // odometer + .groupBegin() + .number("([01]),") // overspeed + .number("[01],") // input 2 misuse + .number("[01],") // immobilizer + .number("[01],") // temperature alert + .number("[0-2]+,") // geofence + .number("([0-3]),") // harsh driving + .number("[01],") // reconnect + .number("([01]),") // low battery + .number("([01]),") // power disconnected + .number("[01],") // gps failure + .number("([01]),") // towing + .number("[01],") // server unreachable + .number("[128],") // sleep mode + .expression("([^,]+)?,") // driver id + .number("d+,") // sms count + .groupEnd("?") .any() .compile(); @@ -138,6 +155,27 @@ public class Ivt401ProtocolDecoder extends BaseProtocolDecoder { position.set(Position.KEY_ODOMETER, parser.nextLong()); + if (parser.hasNext(6)) { + position.set(Position.KEY_ALARM, parser.nextInt() == 1 ? Position.ALARM_OVERSPEED : null); + switch (parser.nextInt()) { + case 1: + position.set(Position.KEY_ALARM, Position.ALARM_ACCELERATION); + break; + case 2: + position.set(Position.KEY_ALARM, Position.ALARM_BRAKING); + break; + case 3: + position.set(Position.KEY_ALARM, Position.ALARM_CORNERING); + break; + default: + break; + } + position.set(Position.KEY_ALARM, parser.nextInt() == 1 ? Position.ALARM_LOW_BATTERY : null); + position.set(Position.KEY_ALARM, parser.nextInt() == 1 ? Position.ALARM_POWER_CUT : null); + position.set(Position.KEY_ALARM, parser.nextInt() == 1 ? Position.ALARM_TOW : null); + position.set(Position.KEY_DRIVER_UNIQUE_ID, parser.next()); + } + return position; } diff --git a/test/org/traccar/protocol/Ivt401ProtocolDecoderTest.java b/test/org/traccar/protocol/Ivt401ProtocolDecoderTest.java index 3c4f25cb1..d8a67ad7b 100644 --- a/test/org/traccar/protocol/Ivt401ProtocolDecoderTest.java +++ b/test/org/traccar/protocol/Ivt401ProtocolDecoderTest.java @@ -10,6 +10,9 @@ public class Ivt401ProtocolDecoderTest extends ProtocolTest { Ivt401ProtocolDecoder decoder = new Ivt401ProtocolDecoder(new Ivt401Protocol()); + verifyPosition(decoder, text( + "(TLA,356917051007891,190118,090211,+16.986606,+82.242416,0,66,4,13,1,5,000,00,0.0,11.59,8.30,37.77,0.0,1,1.02,0,0,208,0,0,0,0,000000000,0,0,0,0,0,0,0,1,8654604,5,9,114)")); + verifyPosition(decoder, text( "(TLN,862107032006249,230218,180500,+18.479728,+73.896339,30,0,944,13,1,5,111,11,0.00,10.88,6.31,29.55,0.00,0,0.99,66,0,0,88,95)")); -- cgit v1.2.3 From b4c411a95fb4432efe5aef26444f0699e43e90dc Mon Sep 17 00:00:00 2001 From: Anton Tananaev Date: Thu, 8 Mar 2018 16:48:51 +1300 Subject: Support GV300 fuel sensor --- .../traccar/protocol/Gl200TextProtocolDecoder.java | 41 +++++++++++++++++----- .../protocol/Gl200TextProtocolDecoderTest.java | 3 ++ 2 files changed, 35 insertions(+), 9 deletions(-) diff --git a/src/org/traccar/protocol/Gl200TextProtocolDecoder.java b/src/org/traccar/protocol/Gl200TextProtocolDecoder.java index e664c4734..c47764989 100644 --- a/src/org/traccar/protocol/Gl200TextProtocolDecoder.java +++ b/src/org/traccar/protocol/Gl200TextProtocolDecoder.java @@ -210,7 +210,7 @@ public class Gl200TextProtocolDecoder extends BaseProtocolDecoder { .number("(?:[0-9A-Z]{2}xxxx)?,") // protocol version .number("(d{15}|x{14}),") // imei .expression("[^,]*,") // device name - .number("x{8},") // mask + .number("(x{8}),") // mask .number("(d+)?,") // power .number("d{1,2},") // report type .number("d{1,2},") // count @@ -730,6 +730,8 @@ public class Gl200TextProtocolDecoder extends BaseProtocolDecoder { return null; } + long mask = parser.nextHexLong(); + LinkedList positions = new LinkedList<>(); int power = parser.nextInt(0); @@ -759,14 +761,35 @@ public class Gl200TextProtocolDecoder extends BaseProtocolDecoder { int index = 0; String[] data = parser.next().split(","); - if (data.length > 1) { - int deviceType = Integer.parseInt(data[index++]); - if (deviceType == 2) { - int deviceCount = Integer.parseInt(data[index++]); - for (int i = 1; i <= deviceCount; i++) { - index++; // id - index++; // type - position.set(Position.PREFIX_TEMP + i, (short) Integer.parseInt(data[index++], 16) * 0.0625); + + index += 1; // device type + + if (BitUtil.check(mask, 0)) { + index += 1; // digital fuel sensor data + } + + if (BitUtil.check(mask, 1)) { + int deviceCount = Integer.parseInt(data[index++]); + for (int i = 1; i <= deviceCount; i++) { + index += 1; // id + index += 1; // type + position.set(Position.PREFIX_TEMP + i, (short) Integer.parseInt(data[index++], 16) * 0.0625); + } + } + + if (BitUtil.check(mask, 2)) { + index += 1; // can data + } + + if (BitUtil.check(mask, 3) || BitUtil.check(mask, 4)) { + int deviceCount = Integer.parseInt(data[index++]); + for (int i = 1; i <= deviceCount; i++) { + index += 1; // type + if (BitUtil.check(mask, 3)) { + position.set(Position.KEY_FUEL_LEVEL, Double.parseDouble(data[index++])); + } + if (BitUtil.check(mask, 4)) { + index += 1; // volume } } } diff --git a/test/org/traccar/protocol/Gl200TextProtocolDecoderTest.java b/test/org/traccar/protocol/Gl200TextProtocolDecoderTest.java index e92936342..9ec67273c 100644 --- a/test/org/traccar/protocol/Gl200TextProtocolDecoderTest.java +++ b/test/org/traccar/protocol/Gl200TextProtocolDecoderTest.java @@ -10,6 +10,9 @@ public class Gl200TextProtocolDecoderTest extends ProtocolTest { Gl200TextProtocolDecoder decoder = new Gl200TextProtocolDecoder(new Gl200Protocol()); + verifyPositions(decoder, buffer( + "+RESP:GTERI,250C02,868789023691057,,00000019,,10,1,1,0.0,196,2258.0,-99.201807,19.559242,20180214002957,0334,0003,235B,7F8D,00,6786.7,,,,100,110000,1,0394,1,4,100.0,100.0,20180214003006,C72B$")); + verifyAttributes(decoder, buffer( "+RESP:GTCAN,310603,863286023335723,gv65,00,1,C03FFFFF,,0,,719601.00,,,,,,,,274.99,179.02,95.98,84761.00,,,0,,0,,,0,0.0,216,29.8,-2.155296,51.899400,20180209172714,0234,0010,53F3,8D38,00,20180211002128,E94E$")); -- cgit v1.2.3 From c2ed376ea2bac8d2e6ac0ffb75be9e60f98fe92b Mon Sep 17 00:00:00 2001 From: Anton Tananaev Date: Fri, 9 Mar 2018 01:48:53 +1300 Subject: Fix ST600R cell decoding --- src/org/traccar/protocol/SuntechProtocolDecoder.java | 2 +- test/org/traccar/protocol/SuntechProtocolDecoderTest.java | 3 +++ 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/src/org/traccar/protocol/SuntechProtocolDecoder.java b/src/org/traccar/protocol/SuntechProtocolDecoder.java index 034894b52..b739e699b 100644 --- a/src/org/traccar/protocol/SuntechProtocolDecoder.java +++ b/src/org/traccar/protocol/SuntechProtocolDecoder.java @@ -192,7 +192,7 @@ public class SuntechProtocolDecoder extends BaseProtocolDecoder { if (protocol.equals("ST600")) { position.setNetwork(new Network(CellTower.from( Integer.parseInt(values[index++]), Integer.parseInt(values[index++]), - Integer.parseInt(values[index++]), cid, Integer.parseInt(values[index++])))); + Integer.parseInt(values[index++], 16), cid, Integer.parseInt(values[index++])))); } } diff --git a/test/org/traccar/protocol/SuntechProtocolDecoderTest.java b/test/org/traccar/protocol/SuntechProtocolDecoderTest.java index bcc8a8b8b..bbcead2d0 100644 --- a/test/org/traccar/protocol/SuntechProtocolDecoderTest.java +++ b/test/org/traccar/protocol/SuntechProtocolDecoderTest.java @@ -27,6 +27,9 @@ public class SuntechProtocolDecoderTest extends ProtocolTest { SuntechProtocolDecoder decoder = new SuntechProtocolDecoder(new SuntechProtocol()); + verifyPosition(decoder, text( + "ST600STT;008084783;20;419;20180308;18:00:36;0032cc3e;736;3;445c;41;-16.530023;-068.084267;018.640;267.99;10;1;11655;13.33;100000;2;0336;000061;4.5;0;0.00")); + verifyPosition(decoder, text( "ST600STT;107850496;20;419;20180227;14:30:45;00462b08;736;3;4524;50;-16.479091;-068.119119;000.346;000.00;4;1;0;13.89;000000;1;0223;000003;0.0;0;0.00")); -- cgit v1.2.3 From b7898abf1be64a2f9750d1020b20e9ed578d61e9 Mon Sep 17 00:00:00 2001 From: Anton Tananaev Date: Fri, 9 Mar 2018 14:21:19 +1300 Subject: Minor OpenGts protocol cleanup --- src/org/traccar/protocol/OpenGtsProtocol.java | 2 +- src/org/traccar/protocol/OpenGtsProtocolDecoder.java | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/org/traccar/protocol/OpenGtsProtocol.java b/src/org/traccar/protocol/OpenGtsProtocol.java index 2a20cb159..a0246ba1b 100644 --- a/src/org/traccar/protocol/OpenGtsProtocol.java +++ b/src/org/traccar/protocol/OpenGtsProtocol.java @@ -1,5 +1,5 @@ /* - * Copyright 2015 Anton Tananaev (anton@traccar.org) + * Copyright 2018 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. diff --git a/src/org/traccar/protocol/OpenGtsProtocolDecoder.java b/src/org/traccar/protocol/OpenGtsProtocolDecoder.java index 9be69ed7f..ba8f434d8 100644 --- a/src/org/traccar/protocol/OpenGtsProtocolDecoder.java +++ b/src/org/traccar/protocol/OpenGtsProtocolDecoder.java @@ -1,5 +1,5 @@ /* - * Copyright 2013 - 2017 Anton Tananaev (anton@traccar.org) + * Copyright 2018 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. @@ -35,14 +35,14 @@ public class OpenGtsProtocolDecoder extends BaseHttpProtocolDecoder { private static final Pattern PATTERN = new PatternBuilder() .text("$GPRMC,") - .number("(dd)(dd)(dd),") // time (hhmmss) + .number("(dd)(dd)(dd),") // time (hhmmss) .expression("([AV]),") // validity .number("(d+)(dd.d+),") // latitude .expression("([NS]),") .number("(d+)(dd.d+),") // longitude .expression("([EW]),") .number("(d+.d+),") // speed - .number("(d+.d+),") // course + .number("(d+.d+),") // course .number("(dd)(dd)(dd),") // date (ddmmyy) .any() .compile(); -- cgit v1.2.3 From 7bf00894ad47ef47ce4c9ccc84b560956f3b41f8 Mon Sep 17 00:00:00 2001 From: Anton Tananaev Date: Fri, 9 Mar 2018 15:07:57 +1300 Subject: Extend Watch frame decoder --- src/org/traccar/protocol/WatchFrameDecoder.java | 85 +++++++++++++--------- .../traccar/protocol/WatchFrameDecoderTest.java | 4 + 2 files changed, 54 insertions(+), 35 deletions(-) diff --git a/src/org/traccar/protocol/WatchFrameDecoder.java b/src/org/traccar/protocol/WatchFrameDecoder.java index 826a8b4d0..9adea2843 100644 --- a/src/org/traccar/protocol/WatchFrameDecoder.java +++ b/src/org/traccar/protocol/WatchFrameDecoder.java @@ -1,5 +1,5 @@ /* - * Copyright 2017 Anton Tananaev (anton@traccar.org) + * Copyright 2017 - 2018 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. @@ -25,47 +25,62 @@ import java.nio.charset.StandardCharsets; public class WatchFrameDecoder extends FrameDecoder { - public static final int MESSAGE_HEADER = 20; - @Override protected Object decode( ChannelHandlerContext ctx, Channel channel, ChannelBuffer buf) throws Exception { - if (buf.readableBytes() >= MESSAGE_HEADER) { - ChannelBuffer lengthBuffer = ChannelBuffers.dynamicBuffer(); - buf.getBytes(buf.readerIndex() + MESSAGE_HEADER - 4 - 1, lengthBuffer, 4); - int length = Integer.parseInt(lengthBuffer.toString(StandardCharsets.US_ASCII), 16) + MESSAGE_HEADER + 1; - if (buf.readableBytes() >= length) { - ChannelBuffer frame = ChannelBuffers.dynamicBuffer(); - int endIndex = buf.readerIndex() + length; - while (buf.readerIndex() < endIndex) { - byte b = buf.readByte(); - if (b == 0x7D) { - switch (buf.readByte()) { - case 0x01: - frame.writeByte(0x7D); - break; - case 0x02: - frame.writeByte(0x5B); - break; - case 0x03: - frame.writeByte(0x5D); - break; - case 0x04: - frame.writeByte(0x2C); - break; - case 0x05: - frame.writeByte(0x2A); - break; - default: - throw new IllegalArgumentException(); - } - } else { - frame.writeByte(b); + int idIndex = buf.indexOf(buf.readerIndex(), buf.writerIndex(), (byte) '*') + 1; + if (idIndex <= 0) { + return null; + } + + int lengthIndex = buf.indexOf(idIndex, buf.writerIndex(), (byte) '*') + 1; + if (lengthIndex <= 0) { + return null; + } else if (lengthIndex - idIndex > 10 + 1) { + lengthIndex = buf.indexOf(lengthIndex, buf.writerIndex(), (byte) '*') + 1; + if (lengthIndex <= 0) { + return null; + } + } + + int payloadIndex = buf.indexOf(lengthIndex, buf.writerIndex(), (byte) '*'); + if (payloadIndex < 0) { + return null; + } + + int length = Integer.parseInt( + buf.toString(lengthIndex, payloadIndex - lengthIndex, StandardCharsets.US_ASCII), 16); + if (buf.readableBytes() >= payloadIndex + 1 + length + 1) { + ChannelBuffer frame = ChannelBuffers.dynamicBuffer(); + int endIndex = buf.readerIndex() + payloadIndex + 1 + length + 1; + while (buf.readerIndex() < endIndex) { + byte b = buf.readByte(); + if (b == 0x7D) { + switch (buf.readByte()) { + case 0x01: + frame.writeByte(0x7D); + break; + case 0x02: + frame.writeByte(0x5B); + break; + case 0x03: + frame.writeByte(0x5D); + break; + case 0x04: + frame.writeByte(0x2C); + break; + case 0x05: + frame.writeByte(0x2A); + break; + default: + throw new IllegalArgumentException(); } + } else { + frame.writeByte(b); } - return frame; } + return frame; } return null; diff --git a/test/org/traccar/protocol/WatchFrameDecoderTest.java b/test/org/traccar/protocol/WatchFrameDecoderTest.java index 664501a43..a1eca30bc 100644 --- a/test/org/traccar/protocol/WatchFrameDecoderTest.java +++ b/test/org/traccar/protocol/WatchFrameDecoderTest.java @@ -18,6 +18,10 @@ public class WatchFrameDecoderTest extends ProtocolTest { binary("5b33472a383330383430363237392a303030392a4c4b2c302c302c38345d"), decoder.decode(null, null, binary("5b33472a383330383430363237392a303030392a4c4b2c302c302c38345d"))); + verifyFrame( + binary("5b5a4a2a3031343131313030313335303330342a303033342a303030392a4c4b2c302c302c31395d"), + decoder.decode(null, null, binary("5b5a4a2a3031343131313030313335303330342a303033342a303030392a4c4b2c302c302c31395d"))); + } } -- cgit v1.2.3 From 7831801c2d3d4d425659f985a53dbc1eaef6b5fb Mon Sep 17 00:00:00 2001 From: Anton Tananaev Date: Fri, 9 Mar 2018 15:18:22 +1300 Subject: Extend Watch response format --- src/org/traccar/protocol/WatchProtocolDecoder.java | 30 ++++++++++++++++------ .../traccar/protocol/WatchProtocolDecoderTest.java | 9 +++++++ 2 files changed, 31 insertions(+), 8 deletions(-) diff --git a/src/org/traccar/protocol/WatchProtocolDecoder.java b/src/org/traccar/protocol/WatchProtocolDecoder.java index fe62874b5..6adcb34f2 100644 --- a/src/org/traccar/protocol/WatchProtocolDecoder.java +++ b/src/org/traccar/protocol/WatchProtocolDecoder.java @@ -1,5 +1,5 @@ /* - * Copyright 2015 - 2017 Anton Tananaev (anton@traccar.org) + * Copyright 2015 - 2018 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. @@ -60,10 +60,15 @@ public class WatchProtocolDecoder extends BaseProtocolDecoder { .expression("(.*)") // cell and wifi .compile(); - private void sendResponse(Channel channel, String manufacturer, String id, String content) { + private void sendResponse(Channel channel, String manufacturer, String id, String index, String content) { if (channel != null) { - channel.write(String.format( - "[%s*%s*%04x*%s]", manufacturer, id, content.length(), content)); + if (index != null) { + channel.write(String.format( + "[%s*%s*%s*%04x*%s]", manufacturer, id, index, content.length(), content)); + } else { + channel.write(String.format( + "[%s*%s*%04x*%s]", manufacturer, id, content.length(), content)); + } } } @@ -134,13 +139,22 @@ public class WatchProtocolDecoder extends BaseProtocolDecoder { String manufacturer = buf.readBytes(2).toString(StandardCharsets.US_ASCII); buf.skipBytes(1); // delimiter - String id = buf.readBytes(10).toString(StandardCharsets.US_ASCII); + int idLength = buf.indexOf(buf.readerIndex(), buf.writerIndex(), (byte) '*'); + String id = buf.readBytes(idLength).toString(StandardCharsets.US_ASCII); DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, id); if (deviceSession == null) { return null; } buf.skipBytes(1); // delimiter + + String index = null; + if (idLength > 10) { + int indexLength = buf.indexOf(buf.readerIndex(), buf.writerIndex(), (byte) '*'); + index = buf.readBytes(indexLength).toString(StandardCharsets.US_ASCII); + buf.skipBytes(1); // delimiter + } + buf.skipBytes(4); // length buf.skipBytes(1); // delimiter @@ -159,7 +173,7 @@ public class WatchProtocolDecoder extends BaseProtocolDecoder { if (type.equals("LK")) { - sendResponse(channel, manufacturer, id, "LK"); + sendResponse(channel, manufacturer, id, index, "LK"); if (buf.readable()) { String[] values = buf.toString(StandardCharsets.US_ASCII).split(","); @@ -179,7 +193,7 @@ public class WatchProtocolDecoder extends BaseProtocolDecoder { || type.equals("AL") || type.equals("WT")) { if (type.equals("AL")) { - sendResponse(channel, manufacturer, id, "AL"); + sendResponse(channel, manufacturer, id, index, "AL"); } Parser parser = new Parser(PATTERN_POSITION, buf.toString(StandardCharsets.US_ASCII)); @@ -217,7 +231,7 @@ public class WatchProtocolDecoder extends BaseProtocolDecoder { } else if (type.equals("TKQ")) { - sendResponse(channel, manufacturer, id, "TKQ"); + sendResponse(channel, manufacturer, id, index, "TKQ"); } else if (type.equals("PULSE") || type.equals("heart")) { diff --git a/test/org/traccar/protocol/WatchProtocolDecoderTest.java b/test/org/traccar/protocol/WatchProtocolDecoderTest.java index c960ccc25..46b4c1cd0 100644 --- a/test/org/traccar/protocol/WatchProtocolDecoderTest.java +++ b/test/org/traccar/protocol/WatchProtocolDecoderTest.java @@ -10,6 +10,15 @@ public class WatchProtocolDecoderTest extends ProtocolTest { WatchProtocolDecoder decoder = new WatchProtocolDecoder(new WatchProtocol()); + verifyPosition(decoder, buffer( + "[ZJ*014111001350304*0033*0064*UD,070318,020827,V,00.000000,N,000.000000,E,0,0,0,0,100,19,1000,50,00000000,1,255,460,0,9346,5223,42]")); + + verifyPosition(decoder, buffer( + "[ZJ*014111001350304*0035*0097*UD,070318,020857,V,00.000000,N,000.000000,E,0,0,0,0,100,19,1000,50,00000000,5,255,460,0,9346,5223,42,9346,5214,21,9784,4083,13,9346,5222,11,9346,5221,8]")); + + verifyPosition(decoder, buffer( + "[ZJ*014111001350304*0038*008a*UD,070318,021027,V,00.000000,N,000.000000,E,0,0,0,0,100,18,1000,50,00000000,4,255,460,0,9346,5223,42,9346,5214,20,9784,4083,11,9346,5221,5]")); + verifyPosition(decoder, buffer( "[3G*8308373902*0080*AL,230817,095346,A,47.083950,N,15.4821850,E,7.60,273.8,0.0,4,15,44,0,0,00200010,2,255,232,1,7605,42530,118,7605,58036,119,0,65.8]")); -- cgit v1.2.3 From 0c5fa5cc8ea437db3c449b158310c5783aaea3ef Mon Sep 17 00:00:00 2001 From: Anton Tananaev Date: Fri, 9 Mar 2018 15:25:30 +1300 Subject: Fix Watch protocol decoding --- src/org/traccar/protocol/WatchProtocolDecoder.java | 6 +++--- .../org/traccar/protocol/WatchProtocolDecoderTest.java | 18 +++++++++--------- 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/src/org/traccar/protocol/WatchProtocolDecoder.java b/src/org/traccar/protocol/WatchProtocolDecoder.java index 6adcb34f2..1dd07a3f7 100644 --- a/src/org/traccar/protocol/WatchProtocolDecoder.java +++ b/src/org/traccar/protocol/WatchProtocolDecoder.java @@ -48,7 +48,7 @@ public class WatchProtocolDecoder extends BaseProtocolDecoder { .expression("([NS]),") .number(" *(-?d+.d+),") // longitude .expression("([EW])?,") - .number("(d+.d+),") // speed + .number("(d+.?d*),") // speed .number("(d+.?d*),") // course .number("(d+.?d*),") // altitude .number("(d+),") // satellites @@ -139,7 +139,7 @@ public class WatchProtocolDecoder extends BaseProtocolDecoder { String manufacturer = buf.readBytes(2).toString(StandardCharsets.US_ASCII); buf.skipBytes(1); // delimiter - int idLength = buf.indexOf(buf.readerIndex(), buf.writerIndex(), (byte) '*'); + int idLength = buf.indexOf(buf.readerIndex(), buf.writerIndex(), (byte) '*') - buf.readerIndex(); String id = buf.readBytes(idLength).toString(StandardCharsets.US_ASCII); DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, id); if (deviceSession == null) { @@ -150,7 +150,7 @@ public class WatchProtocolDecoder extends BaseProtocolDecoder { String index = null; if (idLength > 10) { - int indexLength = buf.indexOf(buf.readerIndex(), buf.writerIndex(), (byte) '*'); + int indexLength = buf.indexOf(buf.readerIndex(), buf.writerIndex(), (byte) '*') - buf.readerIndex(); index = buf.readBytes(indexLength).toString(StandardCharsets.US_ASCII); buf.skipBytes(1); // delimiter } diff --git a/test/org/traccar/protocol/WatchProtocolDecoderTest.java b/test/org/traccar/protocol/WatchProtocolDecoderTest.java index 46b4c1cd0..657cb0640 100644 --- a/test/org/traccar/protocol/WatchProtocolDecoderTest.java +++ b/test/org/traccar/protocol/WatchProtocolDecoderTest.java @@ -10,15 +10,6 @@ public class WatchProtocolDecoderTest extends ProtocolTest { WatchProtocolDecoder decoder = new WatchProtocolDecoder(new WatchProtocol()); - verifyPosition(decoder, buffer( - "[ZJ*014111001350304*0033*0064*UD,070318,020827,V,00.000000,N,000.000000,E,0,0,0,0,100,19,1000,50,00000000,1,255,460,0,9346,5223,42]")); - - verifyPosition(decoder, buffer( - "[ZJ*014111001350304*0035*0097*UD,070318,020857,V,00.000000,N,000.000000,E,0,0,0,0,100,19,1000,50,00000000,5,255,460,0,9346,5223,42,9346,5214,21,9784,4083,13,9346,5222,11,9346,5221,8]")); - - verifyPosition(decoder, buffer( - "[ZJ*014111001350304*0038*008a*UD,070318,021027,V,00.000000,N,000.000000,E,0,0,0,0,100,18,1000,50,00000000,4,255,460,0,9346,5223,42,9346,5214,20,9784,4083,11,9346,5221,5]")); - verifyPosition(decoder, buffer( "[3G*8308373902*0080*AL,230817,095346,A,47.083950,N,15.4821850,E,7.60,273.8,0.0,4,15,44,0,0,00200010,2,255,232,1,7605,42530,118,7605,58036,119,0,65.8]")); @@ -93,6 +84,15 @@ public class WatchProtocolDecoderTest extends ProtocolTest { verifyAttributes(decoder, buffer( "[3G*6005412902*0008*heart,71]")); + verifyPosition(decoder, buffer( + "[ZJ*014111001350304*0033*0064*UD,070318,020827,V,00.000000,N,000.000000,E,0,0,0,0,100,19,1000,50,00000000,1,255,460,0,9346,5223,42]")); + + verifyPosition(decoder, buffer( + "[ZJ*014111001350304*0035*0097*UD,070318,020857,V,00.000000,N,000.000000,E,0,0,0,0,100,19,1000,50,00000000,5,255,460,0,9346,5223,42,9346,5214,21,9784,4083,13,9346,5222,11,9346,5221,8]")); + + verifyPosition(decoder, buffer( + "[ZJ*014111001350304*0038*008a*UD,070318,021027,V,00.000000,N,000.000000,E,0,0,0,0,100,18,1000,50,00000000,4,255,460,0,9346,5223,42,9346,5214,20,9784,4083,11,9346,5221,5]")); + } } -- cgit v1.2.3 From 6259ca71bc9c1963e049226a375e058af9dc29c0 Mon Sep 17 00:00:00 2001 From: Anton Tananaev Date: Sat, 10 Mar 2018 15:01:12 +1300 Subject: Update Java libraries --- pom.xml | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/pom.xml b/pom.xml index 33b3a8092..81e9d49c2 100644 --- a/pom.xml +++ b/pom.xml @@ -4,14 +4,14 @@ 4.0.0 org.traccar traccar - 3.15-SNAPSHOT + 3.16-SNAPSHOT traccar https://www.traccar.org UTF-8 - 9.2.22.v20170606 + 9.2.24.v20180105 2.25.1 @@ -45,12 +45,12 @@ org.postgresql postgresql - 42.1.4.jre7 + 42.2.1.jre7 com.microsoft.sqlserver mssql-jdbc - 6.2.2.jre7 + 6.4.0.jre7 com.microsoft.azure @@ -61,7 +61,7 @@ com.zaxxer HikariCP-java7 - 2.4.13 + 2.4.13 io.netty @@ -126,7 +126,7 @@ org.liquibase liquibase-core - 3.5.3 + 3.5.5 javax.mail @@ -136,12 +136,12 @@ org.jxls jxls - 2.4.2 + 2.4.4 org.jxls jxls-poi - 1.0.13 + 1.0.14 org.apache.velocity @@ -161,7 +161,7 @@ org.mnode.ical4j ical4j - 2.0.4 + 2.0.5 com.fizzed @@ -187,7 +187,7 @@ maven-checkstyle-plugin - 2.17 + 3.0.0 checkstyle.xml @@ -290,7 +290,7 @@ org.codehaus.mojo extra-enforcer-rules - 1.0-beta-6 + 1.0-beta-7 @@ -320,7 +320,7 @@ maven-pmd-plugin - 3.8 + 3.9.0 -- cgit v1.2.3 From eedfb54d208ba48783cb9d03a775723660e9ff73 Mon Sep 17 00:00:00 2001 From: Anton Tananaev Date: Sat, 10 Mar 2018 15:02:21 +1300 Subject: Fix PMD code issues --- src/org/traccar/api/MediaFilter.java | 2 +- src/org/traccar/protocol/FlespiProtocolDecoder.java | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/org/traccar/api/MediaFilter.java b/src/org/traccar/api/MediaFilter.java index 4685ce05b..25e242b01 100644 --- a/src/org/traccar/api/MediaFilter.java +++ b/src/org/traccar/api/MediaFilter.java @@ -43,7 +43,7 @@ public class MediaFilter implements Filter { @Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { - HttpServletResponse httpResponse = ((HttpServletResponse) response); + HttpServletResponse httpResponse = (HttpServletResponse) response; try { HttpSession session = ((HttpServletRequest) request).getSession(false); Long userId = null; diff --git a/src/org/traccar/protocol/FlespiProtocolDecoder.java b/src/org/traccar/protocol/FlespiProtocolDecoder.java index be0e4a07f..526e10fa2 100644 --- a/src/org/traccar/protocol/FlespiProtocolDecoder.java +++ b/src/org/traccar/protocol/FlespiProtocolDecoder.java @@ -123,7 +123,7 @@ public class FlespiProtocolDecoder extends BaseHttpProtocolDecoder { return true; case "din": case "dout": - position.set((name.equals("din") ? Position.KEY_INPUT : Position.KEY_OUTPUT), + position.set(name.equals("din") ? Position.KEY_INPUT : Position.KEY_OUTPUT, ((JsonNumber) value).intValue()); return true; case "gps.vehicle.mileage": -- cgit v1.2.3 From 37058629a3b39da810baeef92f70a6a5693d0f33 Mon Sep 17 00:00:00 2001 From: Anton Tananaev Date: Sat, 10 Mar 2018 15:05:23 +1300 Subject: Update Traccar version --- setup/traccar.iss | 2 +- swagger.json | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/setup/traccar.iss b/setup/traccar.iss index 96cd5fdc0..1d6501d31 100644 --- a/setup/traccar.iss +++ b/setup/traccar.iss @@ -1,6 +1,6 @@ [Setup] AppName=Traccar -AppVersion=3.15 +AppVersion=3.16 DefaultDirName={pf}\Traccar AlwaysRestart=yes OutputBaseFilename=traccar-setup diff --git a/swagger.json b/swagger.json index 265e7bfd5..e6c242a3e 100644 --- a/swagger.json +++ b/swagger.json @@ -1,7 +1,7 @@ { "swagger": "2.0", "info": { - "version": "3.15", + "version": "3.16", "title": "traccar" }, "host": "demo.traccar.org", -- cgit v1.2.3 From 4d7c36cbd2ec7f6e7d971f4de013603d4cb80d0e Mon Sep 17 00:00:00 2001 From: Anton Tananaev Date: Sat, 10 Mar 2018 15:30:28 +1300 Subject: Fix setup script issues --- setup/setup.sh | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/setup/setup.sh b/setup/setup.sh index 39a00d8e8..d2e5b2dec 100755 --- a/setup/setup.sh +++ b/setup/setup.sh @@ -9,7 +9,8 @@ then rm -r ../out rm /opt/traccar/setup.sh chmod -R go+rX /opt/traccar - /opt/traccar/bin/installDaemon.sh + cd /opt/traccar/bin/ + . /opt/traccar/bin/installDaemon.sh else echo 'Java 7 or higher is required' fi -- cgit v1.2.3 From a58b40a49e96f8fc876b88b6b10489f8532893d9 Mon Sep 17 00:00:00 2001 From: Anton Tananaev Date: Sat, 10 Mar 2018 16:23:06 +1300 Subject: Replace deprecated classes --- pom.xml | 5 +++ src/org/traccar/BaseProtocol.java | 4 +- src/org/traccar/ExtendedObjectDecoder.java | 4 +- src/org/traccar/api/SecurityRequestFilter.java | 4 +- src/org/traccar/api/resource/SessionResource.java | 6 +-- src/org/traccar/helper/DataConverter.java | 47 ++++++++++++++++++++++ src/org/traccar/helper/Hashing.java | 9 ++--- .../traccar/protocol/At2000ProtocolDecoder.java | 4 +- .../traccar/protocol/EelinkProtocolEncoder.java | 4 +- .../traccar/protocol/HuabaoProtocolEncoder.java | 6 +-- .../traccar/protocol/MeiligaoProtocolEncoder.java | 4 +- .../traccar/protocol/RuptelaProtocolDecoder.java | 6 +-- .../traccar/protocol/SigfoxProtocolDecoder.java | 4 +- src/org/traccar/protocol/T800xProtocolEncoder.java | 4 +- src/org/traccar/protocol/WatchProtocolEncoder.java | 4 +- .../traccar/protocol/Xt2400ProtocolDecoder.java | 4 +- test/org/traccar/ProtocolTest.java | 4 +- 17 files changed, 87 insertions(+), 36 deletions(-) create mode 100644 src/org/traccar/helper/DataConverter.java diff --git a/pom.xml b/pom.xml index 81e9d49c2..14de784f3 100644 --- a/pom.xml +++ b/pom.xml @@ -32,6 +32,11 @@ joda-time 2.9.9 + + commons-codec + commons-codec + 1.11 + com.h2database h2 diff --git a/src/org/traccar/BaseProtocol.java b/src/org/traccar/BaseProtocol.java index 90b9f21f2..07adbea5e 100644 --- a/src/org/traccar/BaseProtocol.java +++ b/src/org/traccar/BaseProtocol.java @@ -18,9 +18,9 @@ package org.traccar; import org.jboss.netty.buffer.ChannelBuffers; import org.jboss.netty.handler.codec.string.StringEncoder; import org.traccar.database.ActiveDevice; +import org.traccar.helper.DataConverter; import org.traccar.model.Command; -import javax.xml.bind.DatatypeConverter; import java.util.Arrays; import java.util.Collection; import java.util.HashSet; @@ -79,7 +79,7 @@ public abstract class BaseProtocol implements Protocol { if (activeDevice.getChannel().getPipeline().get(StringEncoder.class) != null) { activeDevice.write(data); } else { - activeDevice.write(ChannelBuffers.wrappedBuffer(DatatypeConverter.parseHexBinary(data))); + activeDevice.write(ChannelBuffers.wrappedBuffer(DataConverter.parseHex(data))); } } else { throw new RuntimeException("Command " + command.getType() + " is not supported in protocol " + getName()); diff --git a/src/org/traccar/ExtendedObjectDecoder.java b/src/org/traccar/ExtendedObjectDecoder.java index 268e6f688..75a24212f 100644 --- a/src/org/traccar/ExtendedObjectDecoder.java +++ b/src/org/traccar/ExtendedObjectDecoder.java @@ -23,9 +23,9 @@ import org.jboss.netty.channel.ChannelHandlerContext; import org.jboss.netty.channel.ChannelUpstreamHandler; import org.jboss.netty.channel.Channels; import org.jboss.netty.channel.MessageEvent; +import org.traccar.helper.DataConverter; import org.traccar.model.Position; -import javax.xml.bind.DatatypeConverter; import java.net.SocketAddress; import java.nio.charset.StandardCharsets; import java.util.Collection; @@ -39,7 +39,7 @@ public abstract class ExtendedObjectDecoder implements ChannelUpstreamHandler { ChannelBuffer buf = (ChannelBuffer) originalMessage; position.set(Position.KEY_ORIGINAL, ChannelBuffers.hexDump(buf, 0, buf.writerIndex())); } else if (originalMessage instanceof String) { - position.set(Position.KEY_ORIGINAL, DatatypeConverter.printHexBinary( + position.set(Position.KEY_ORIGINAL, DataConverter.printHex( ((String) originalMessage).getBytes(StandardCharsets.US_ASCII))); } } diff --git a/src/org/traccar/api/SecurityRequestFilter.java b/src/org/traccar/api/SecurityRequestFilter.java index 7024bdbc9..aace9f705 100644 --- a/src/org/traccar/api/SecurityRequestFilter.java +++ b/src/org/traccar/api/SecurityRequestFilter.java @@ -17,6 +17,7 @@ package org.traccar.api; import org.traccar.Context; import org.traccar.api.resource.SessionResource; +import org.traccar.helper.DataConverter; import org.traccar.helper.Log; import org.traccar.model.User; @@ -28,7 +29,6 @@ import javax.ws.rs.container.ContainerRequestFilter; import javax.ws.rs.container.ResourceInfo; import javax.ws.rs.core.Response; import javax.ws.rs.core.SecurityContext; -import javax.xml.bind.DatatypeConverter; import java.lang.reflect.Method; import java.nio.charset.StandardCharsets; import java.sql.SQLException; @@ -43,7 +43,7 @@ public class SecurityRequestFilter implements ContainerRequestFilter { public static String[] decodeBasicAuth(String auth) { auth = auth.replaceFirst("[B|b]asic ", ""); - byte[] decodedBytes = DatatypeConverter.parseBase64Binary(auth); + byte[] decodedBytes = DataConverter.parseBase64(auth); if (decodedBytes != null && decodedBytes.length > 0) { return new String(decodedBytes, StandardCharsets.US_ASCII).split(":", 2); } diff --git a/src/org/traccar/api/resource/SessionResource.java b/src/org/traccar/api/resource/SessionResource.java index 2a0bd4364..fd331c766 100644 --- a/src/org/traccar/api/resource/SessionResource.java +++ b/src/org/traccar/api/resource/SessionResource.java @@ -17,6 +17,7 @@ package org.traccar.api.resource; import org.traccar.Context; import org.traccar.api.BaseResource; +import org.traccar.helper.DataConverter; import org.traccar.helper.LogAction; import org.traccar.model.User; @@ -34,7 +35,6 @@ import javax.ws.rs.QueryParam; import javax.ws.rs.WebApplicationException; import javax.ws.rs.core.MediaType; import javax.ws.rs.core.Response; -import javax.xml.bind.DatatypeConverter; import java.io.UnsupportedEncodingException; import java.net.URLDecoder; @@ -63,11 +63,11 @@ public class SessionResource extends BaseResource { if (cookies != null) { for (Cookie cookie : cookies) { if (cookie.getName().equals(USER_COOKIE_KEY)) { - byte[] emailBytes = DatatypeConverter.parseBase64Binary( + byte[] emailBytes = DataConverter.parseBase64( URLDecoder.decode(cookie.getValue(), StandardCharsets.US_ASCII.name())); email = new String(emailBytes, StandardCharsets.UTF_8); } else if (cookie.getName().equals(PASS_COOKIE_KEY)) { - byte[] passwordBytes = DatatypeConverter.parseBase64Binary( + byte[] passwordBytes = DataConverter.parseBase64( URLDecoder.decode(cookie.getValue(), StandardCharsets.US_ASCII.name())); password = new String(passwordBytes, StandardCharsets.UTF_8); } diff --git a/src/org/traccar/helper/DataConverter.java b/src/org/traccar/helper/DataConverter.java new file mode 100644 index 000000000..7abd4ae93 --- /dev/null +++ b/src/org/traccar/helper/DataConverter.java @@ -0,0 +1,47 @@ +/* + * Copyright 2018 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.helper; + +import org.apache.commons.codec.DecoderException; +import org.apache.commons.codec.binary.Base64; +import org.apache.commons.codec.binary.Hex; + +public final class DataConverter { + + private DataConverter() { + } + + public static byte[] parseHex(String string) { + try { + return Hex.decodeHex(string); + } catch (DecoderException e) { + throw new RuntimeException(e); + } + } + + public static String printHex(byte[] data) { + return Hex.encodeHexString(data); + } + + public static byte[] parseBase64(String string) { + return Base64.decodeBase64(string); + } + + public static String printBase64(byte[] data) { + return Base64.encodeBase64String(data); + } + +} diff --git a/src/org/traccar/helper/Hashing.java b/src/org/traccar/helper/Hashing.java index 3fcc124fa..e91310eda 100644 --- a/src/org/traccar/helper/Hashing.java +++ b/src/org/traccar/helper/Hashing.java @@ -17,7 +17,6 @@ package org.traccar.helper; import javax.crypto.SecretKeyFactory; import javax.crypto.spec.PBEKeySpec; -import javax.xml.bind.DatatypeConverter; import java.security.NoSuchAlgorithmException; import java.security.SecureRandom; import java.security.spec.InvalidKeySpecException; @@ -75,13 +74,13 @@ public final class Hashing { RANDOM.nextBytes(salt); byte[] hash = function(password.toCharArray(), salt); return new HashingResult( - DatatypeConverter.printHexBinary(hash), - DatatypeConverter.printHexBinary(salt)); + DataConverter.printHex(hash), + DataConverter.printHex(salt)); } public static boolean validatePassword(String password, String hashHex, String saltHex) { - byte[] hash = DatatypeConverter.parseHexBinary(hashHex); - byte[] salt = DatatypeConverter.parseHexBinary(saltHex); + byte[] hash = DataConverter.parseHex(hashHex); + byte[] salt = DataConverter.parseHex(saltHex); return slowEquals(hash, function(password.toCharArray(), salt)); } diff --git a/src/org/traccar/protocol/At2000ProtocolDecoder.java b/src/org/traccar/protocol/At2000ProtocolDecoder.java index 06d80c497..e0f2b4278 100644 --- a/src/org/traccar/protocol/At2000ProtocolDecoder.java +++ b/src/org/traccar/protocol/At2000ProtocolDecoder.java @@ -20,13 +20,13 @@ import org.jboss.netty.buffer.ChannelBuffers; import org.jboss.netty.channel.Channel; import org.traccar.BaseProtocolDecoder; import org.traccar.DeviceSession; +import org.traccar.helper.DataConverter; import org.traccar.helper.UnitsConverter; import org.traccar.model.Position; import javax.crypto.Cipher; import javax.crypto.spec.IvParameterSpec; import javax.crypto.spec.SecretKeySpec; -import javax.xml.bind.DatatypeConverter; import java.net.SocketAddress; import java.nio.ByteOrder; import java.nio.charset.StandardCharsets; @@ -84,7 +84,7 @@ public class At2000ProtocolDecoder extends BaseProtocolDecoder { IvParameterSpec ivSpec = new IvParameterSpec(iv); SecretKeySpec keySpec = new SecretKeySpec( - DatatypeConverter.parseHexBinary("000102030405060708090a0b0c0d0e0f"), "AES"); + DataConverter.parseHex("000102030405060708090a0b0c0d0e0f"), "AES"); cipher = Cipher.getInstance("AES/CBC/NoPadding"); cipher.init(Cipher.DECRYPT_MODE, keySpec, ivSpec); diff --git a/src/org/traccar/protocol/EelinkProtocolEncoder.java b/src/org/traccar/protocol/EelinkProtocolEncoder.java index 76865a039..4d2d86e68 100644 --- a/src/org/traccar/protocol/EelinkProtocolEncoder.java +++ b/src/org/traccar/protocol/EelinkProtocolEncoder.java @@ -18,10 +18,10 @@ package org.traccar.protocol; import org.jboss.netty.buffer.ChannelBuffer; import org.jboss.netty.buffer.ChannelBuffers; import org.traccar.BaseProtocolEncoder; +import org.traccar.helper.DataConverter; import org.traccar.helper.Log; import org.traccar.model.Command; -import javax.xml.bind.DatatypeConverter; import java.nio.ByteBuffer; import java.nio.charset.StandardCharsets; @@ -47,7 +47,7 @@ public class EelinkProtocolEncoder extends BaseProtocolEncoder { ChannelBuffer buf = ChannelBuffers.dynamicBuffer(); if (connectionless) { - buf.writeBytes(ChannelBuffers.wrappedBuffer(DatatypeConverter.parseHexBinary('0' + uniqueId))); + buf.writeBytes(ChannelBuffers.wrappedBuffer(DataConverter.parseHex('0' + uniqueId))); } buf.writeByte(0x67); diff --git a/src/org/traccar/protocol/HuabaoProtocolEncoder.java b/src/org/traccar/protocol/HuabaoProtocolEncoder.java index 7d6f0510d..d1889bf5e 100644 --- a/src/org/traccar/protocol/HuabaoProtocolEncoder.java +++ b/src/org/traccar/protocol/HuabaoProtocolEncoder.java @@ -18,10 +18,10 @@ package org.traccar.protocol; import org.jboss.netty.buffer.ChannelBuffer; import org.jboss.netty.buffer.ChannelBuffers; import org.traccar.BaseProtocolEncoder; +import org.traccar.helper.DataConverter; import org.traccar.helper.Log; import org.traccar.model.Command; -import javax.xml.bind.DatatypeConverter; import java.text.SimpleDateFormat; import java.util.Date; @@ -31,10 +31,10 @@ public class HuabaoProtocolEncoder extends BaseProtocolEncoder { protected Object encodeCommand(Command command) { ChannelBuffer id = ChannelBuffers.wrappedBuffer( - DatatypeConverter.parseHexBinary(getUniqueId(command.getDeviceId()))); + DataConverter.parseHex(getUniqueId(command.getDeviceId()))); ChannelBuffer data = ChannelBuffers.dynamicBuffer(); - byte[] time = DatatypeConverter.parseHexBinary(new SimpleDateFormat("yyMMddHHmmss").format(new Date())); + byte[] time = DataConverter.parseHex(new SimpleDateFormat("yyMMddHHmmss").format(new Date())); switch (command.getType()) { case Command.TYPE_ENGINE_STOP: diff --git a/src/org/traccar/protocol/MeiligaoProtocolEncoder.java b/src/org/traccar/protocol/MeiligaoProtocolEncoder.java index 2e0a1e84c..340d947e3 100644 --- a/src/org/traccar/protocol/MeiligaoProtocolEncoder.java +++ b/src/org/traccar/protocol/MeiligaoProtocolEncoder.java @@ -19,10 +19,10 @@ import org.jboss.netty.buffer.ChannelBuffer; import org.jboss.netty.buffer.ChannelBuffers; import org.traccar.BaseProtocolEncoder; import org.traccar.helper.Checksum; +import org.traccar.helper.DataConverter; import org.traccar.helper.Log; import org.traccar.model.Command; -import javax.xml.bind.DatatypeConverter; import java.nio.charset.StandardCharsets; import java.util.TimeZone; @@ -44,7 +44,7 @@ public class MeiligaoProtocolEncoder extends BaseProtocolEncoder { buf.writeShort(2 + 2 + 7 + 2 + content.readableBytes() + 2 + 2); // message length - buf.writeBytes(DatatypeConverter.parseHexBinary((getUniqueId(deviceId) + "FFFFFFFFFFFFFF").substring(0, 14))); + buf.writeBytes(DataConverter.parseHex((getUniqueId(deviceId) + "FFFFFFFFFFFFFF").substring(0, 14))); buf.writeShort(type); diff --git a/src/org/traccar/protocol/RuptelaProtocolDecoder.java b/src/org/traccar/protocol/RuptelaProtocolDecoder.java index 87ee8246a..7b11cc5c3 100644 --- a/src/org/traccar/protocol/RuptelaProtocolDecoder.java +++ b/src/org/traccar/protocol/RuptelaProtocolDecoder.java @@ -20,10 +20,10 @@ import org.jboss.netty.buffer.ChannelBuffers; import org.jboss.netty.channel.Channel; import org.traccar.BaseProtocolDecoder; import org.traccar.DeviceSession; +import org.traccar.helper.DataConverter; import org.traccar.helper.UnitsConverter; import org.traccar.model.Position; -import javax.xml.bind.DatatypeConverter; import java.net.SocketAddress; import java.nio.charset.StandardCharsets; import java.util.Date; @@ -196,7 +196,7 @@ public class RuptelaProtocolDecoder extends BaseProtocolDecoder { } if (channel != null) { - channel.write(ChannelBuffers.wrappedBuffer(DatatypeConverter.parseHexBinary("0002640113bc"))); + channel.write(ChannelBuffers.wrappedBuffer(DataConverter.parseHex("0002640113bc"))); } return positions; @@ -229,7 +229,7 @@ public class RuptelaProtocolDecoder extends BaseProtocolDecoder { } if (channel != null) { - channel.write(ChannelBuffers.wrappedBuffer(DatatypeConverter.parseHexBinary("00026d01c4a4"))); + channel.write(ChannelBuffers.wrappedBuffer(DataConverter.parseHex("00026d01c4a4"))); } return positions; diff --git a/src/org/traccar/protocol/SigfoxProtocolDecoder.java b/src/org/traccar/protocol/SigfoxProtocolDecoder.java index 65ab2cc55..b454e00fa 100644 --- a/src/org/traccar/protocol/SigfoxProtocolDecoder.java +++ b/src/org/traccar/protocol/SigfoxProtocolDecoder.java @@ -22,12 +22,12 @@ import org.jboss.netty.handler.codec.http.HttpRequest; import org.jboss.netty.handler.codec.http.HttpResponseStatus; import org.traccar.BaseHttpProtocolDecoder; import org.traccar.DeviceSession; +import org.traccar.helper.DataConverter; import org.traccar.helper.UnitsConverter; import org.traccar.model.Position; import javax.json.Json; import javax.json.JsonObject; -import javax.xml.bind.DatatypeConverter; import java.io.StringReader; import java.net.SocketAddress; import java.net.URLDecoder; @@ -61,7 +61,7 @@ public class SigfoxProtocolDecoder extends BaseHttpProtocolDecoder { position.setTime(new Date(json.getInt("time") * 1000L)); ChannelBuffer buf = ChannelBuffers.wrappedBuffer( - ByteOrder.LITTLE_ENDIAN, DatatypeConverter.parseHexBinary(json.getString("data"))); + ByteOrder.LITTLE_ENDIAN, DataConverter.parseHex(json.getString("data"))); int type = buf.readUnsignedByte() >> 4; if (type == 0) { diff --git a/src/org/traccar/protocol/T800xProtocolEncoder.java b/src/org/traccar/protocol/T800xProtocolEncoder.java index 6ed5dbccd..038a5e51a 100644 --- a/src/org/traccar/protocol/T800xProtocolEncoder.java +++ b/src/org/traccar/protocol/T800xProtocolEncoder.java @@ -18,10 +18,10 @@ package org.traccar.protocol; import org.jboss.netty.buffer.ChannelBuffer; import org.jboss.netty.buffer.ChannelBuffers; import org.traccar.BaseProtocolEncoder; +import org.traccar.helper.DataConverter; import org.traccar.helper.Log; import org.traccar.model.Command; -import javax.xml.bind.DatatypeConverter; import java.nio.charset.StandardCharsets; public class T800xProtocolEncoder extends BaseProtocolEncoder { @@ -39,7 +39,7 @@ public class T800xProtocolEncoder extends BaseProtocolEncoder { buf.writeByte(T800xProtocolDecoder.MSG_COMMAND); buf.writeShort(7 + 8 + 1 + content.length()); buf.writeShort(1); // serial number - buf.writeBytes(DatatypeConverter.parseHexBinary("0" + getUniqueId(command.getDeviceId()))); + buf.writeBytes(DataConverter.parseHex("0" + getUniqueId(command.getDeviceId()))); buf.writeByte(MODE_SETTING); buf.writeBytes(content.getBytes(StandardCharsets.US_ASCII)); diff --git a/src/org/traccar/protocol/WatchProtocolEncoder.java b/src/org/traccar/protocol/WatchProtocolEncoder.java index d2d3b52d1..5206fbf10 100644 --- a/src/org/traccar/protocol/WatchProtocolEncoder.java +++ b/src/org/traccar/protocol/WatchProtocolEncoder.java @@ -16,10 +16,10 @@ package org.traccar.protocol; import org.traccar.StringProtocolEncoder; +import org.traccar.helper.DataConverter; import org.traccar.helper.Log; import org.traccar.model.Command; -import javax.xml.bind.DatatypeConverter; import java.nio.charset.StandardCharsets; import java.text.DecimalFormat; import java.text.DecimalFormatSymbols; @@ -68,7 +68,7 @@ public class WatchProtocolEncoder extends StringProtocolEncoder implements Strin } private String getBinaryData(Command command) { - byte[] data = DatatypeConverter.parseHexBinary(command.getString(Command.KEY_DATA)); + byte[] data = DataConverter.parseHex(command.getString(Command.KEY_DATA)); int encodedLength = data.length; for (byte b : data) { diff --git a/src/org/traccar/protocol/Xt2400ProtocolDecoder.java b/src/org/traccar/protocol/Xt2400ProtocolDecoder.java index 72be473c1..1be943e98 100644 --- a/src/org/traccar/protocol/Xt2400ProtocolDecoder.java +++ b/src/org/traccar/protocol/Xt2400ProtocolDecoder.java @@ -20,10 +20,10 @@ import org.jboss.netty.channel.Channel; import org.traccar.BaseProtocolDecoder; import org.traccar.Context; import org.traccar.DeviceSession; +import org.traccar.helper.DataConverter; import org.traccar.helper.UnitsConverter; import org.traccar.model.Position; -import javax.xml.bind.DatatypeConverter; import java.net.SocketAddress; import java.nio.charset.StandardCharsets; import java.util.Date; @@ -95,7 +95,7 @@ public class Xt2400ProtocolDecoder extends BaseProtocolDecoder { Pattern pattern = Pattern.compile(":wycfg pcr\\[\\d+\\] ([0-9a-fA-F]{2})[0-9a-fA-F]{2}([0-9a-fA-F]+)"); Matcher matcher = pattern.matcher(configString); while (matcher.find()) { - formats.put(Short.parseShort(matcher.group(1), 16), DatatypeConverter.parseHexBinary(matcher.group(2))); + formats.put(Short.parseShort(matcher.group(1), 16), DataConverter.parseHex(matcher.group(2))); } } diff --git a/test/org/traccar/ProtocolTest.java b/test/org/traccar/ProtocolTest.java index e67b53aa8..75a98000b 100644 --- a/test/org/traccar/ProtocolTest.java +++ b/test/org/traccar/ProtocolTest.java @@ -5,11 +5,11 @@ import org.jboss.netty.buffer.ChannelBuffers; import org.jboss.netty.handler.codec.http.DefaultHttpRequest; import org.jboss.netty.handler.codec.http.HttpMethod; import org.jboss.netty.handler.codec.http.HttpVersion; +import org.traccar.helper.DataConverter; import org.traccar.model.CellTower; import org.traccar.model.Command; import org.traccar.model.Position; -import javax.xml.bind.DatatypeConverter; import java.nio.ByteOrder; import java.nio.charset.StandardCharsets; import java.text.DateFormat; @@ -56,7 +56,7 @@ public class ProtocolTest extends BaseTest { protected ChannelBuffer binary(ByteOrder endianness, String... data) { return ChannelBuffers.wrappedBuffer( - endianness, DatatypeConverter.parseHexBinary(concatenateStrings(data))); + endianness, DataConverter.parseHex(concatenateStrings(data))); } protected String text(String... data) { -- cgit v1.2.3 From 0f63a6fc8388deebb4c5f4b0bbd506d48c941053 Mon Sep 17 00:00:00 2001 From: Anton Tananaev Date: Sat, 10 Mar 2018 17:32:11 +1300 Subject: Update submodule commit --- traccar-web | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/traccar-web b/traccar-web index f08f6fa4b..464aa58c4 160000 --- a/traccar-web +++ b/traccar-web @@ -1 +1 @@ -Subproject commit f08f6fa4b20f003dd624656746a49237bbff2441 +Subproject commit 464aa58c49ac5e04d419a68b6ee5bcf9d0c36798 -- cgit v1.2.3 From 755874d6e42a68178db75d9037785fc8271382a1 Mon Sep 17 00:00:00 2001 From: Anton Tananaev Date: Sun, 11 Mar 2018 08:56:53 +1300 Subject: Fix setup script issue (fix #3804) --- setup/setup.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/setup/setup.sh b/setup/setup.sh index d2e5b2dec..51a139dd1 100755 --- a/setup/setup.sh +++ b/setup/setup.sh @@ -10,7 +10,7 @@ then rm /opt/traccar/setup.sh chmod -R go+rX /opt/traccar cd /opt/traccar/bin/ - . /opt/traccar/bin/installDaemon.sh + /opt/traccar/bin/installDaemon.sh else echo 'Java 7 or higher is required' fi -- cgit v1.2.3 From 8874b4bf69f38ef94d70bef2a8e4d52e0845fa77 Mon Sep 17 00:00:00 2001 From: Anton Tananaev Date: Mon, 12 Mar 2018 00:51:46 +1300 Subject: Support STEPP 3 FALCOM messages --- src/org/traccar/protocol/T55ProtocolDecoder.java | 8 +++++--- test/org/traccar/protocol/T55ProtocolDecoderTest.java | 6 ++++++ 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/src/org/traccar/protocol/T55ProtocolDecoder.java b/src/org/traccar/protocol/T55ProtocolDecoder.java index dbc467993..be3cb5f67 100644 --- a/src/org/traccar/protocol/T55ProtocolDecoder.java +++ b/src/org/traccar/protocol/T55ProtocolDecoder.java @@ -1,5 +1,5 @@ /* - * Copyright 2012 - 2017 Anton Tananaev (anton@traccar.org) + * Copyright 2012 - 2018 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. @@ -250,9 +250,11 @@ public class T55ProtocolDecoder extends BaseProtocolDecoder { } else if (sentence.startsWith("$PCPTI")) { getDeviceSession(channel, remoteAddress, sentence.substring(7, sentence.indexOf(",", 7))); } else if (sentence.startsWith("IMEI")) { - getDeviceSession(channel, remoteAddress, sentence.substring(5, sentence.length())); + getDeviceSession(channel, remoteAddress, sentence.substring(5)); + } else if (sentence.startsWith("$IMEI")) { + getDeviceSession(channel, remoteAddress, sentence.substring(6)); } else if (sentence.startsWith("$GPFID")) { - deviceSession = getDeviceSession(channel, remoteAddress, sentence.substring(7, sentence.length())); + deviceSession = getDeviceSession(channel, remoteAddress, sentence.substring(7)); if (deviceSession != null && position != null) { Position position = this.position; position.setDeviceId(deviceSession.getDeviceId()); diff --git a/test/org/traccar/protocol/T55ProtocolDecoderTest.java b/test/org/traccar/protocol/T55ProtocolDecoderTest.java index 7f60a1807..ec699f111 100644 --- a/test/org/traccar/protocol/T55ProtocolDecoderTest.java +++ b/test/org/traccar/protocol/T55ProtocolDecoderTest.java @@ -10,12 +10,18 @@ public class T55ProtocolDecoderTest extends ProtocolTest { T55ProtocolDecoder decoder = new T55ProtocolDecoder(new T55Protocol()); + verifyNull(decoder, text( + "$IMEI=355797031609284")); + verifyNull(decoder, text( "086415031C20")); verifyNull(decoder, text( "358244017671308")); + verifyPosition(decoder, text( + "$GPRMC,192350.000,V,0000.0000,N,00000.0000,E,,,110318,,*12")); + verifyPosition(decoder, text( "$GPRMC,073446.000,A,1255.5125,N,07738.2948,E,0.00,0.53,080316,D*71,11,865733027593268,1,090,086,123,456,789,987,12345")); -- cgit v1.2.3 From d5c79363d91f6ae8594fe2cdde447c7efefaa798 Mon Sep 17 00:00:00 2001 From: Anton Tananaev Date: Mon, 12 Mar 2018 02:58:34 +1300 Subject: Support Lantrix T1700 TAIP protocol --- src/org/traccar/protocol/TaipProtocolDecoder.java | 72 ++++++++++++++-------- .../traccar/protocol/TaipProtocolDecoderTest.java | 3 + 2 files changed, 48 insertions(+), 27 deletions(-) diff --git a/src/org/traccar/protocol/TaipProtocolDecoder.java b/src/org/traccar/protocol/TaipProtocolDecoder.java index 9555d19e9..a7aa9dd96 100644 --- a/src/org/traccar/protocol/TaipProtocolDecoder.java +++ b/src/org/traccar/protocol/TaipProtocolDecoder.java @@ -1,5 +1,5 @@ /* - * Copyright 2013 - 2017 Anton Tananaev (anton@traccar.org) + * Copyright 2013 - 2018 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. @@ -62,16 +62,23 @@ public class TaipProtocolDecoder extends BaseProtocolDecoder { .number("(ddd)") // speed .number("(ddd)") // course .groupBegin() + .number("([023])") // fix mode + .number("xx") // data age + .number("(xx)") // input + .number("(dd)") // event + .number("(dd)") // hdop + .or() + .groupBegin() .number("(xx)") // input .number("(xx)") // satellites .number("(ddd)") // battery .number("(x{8})") // odometer .number("[01]") // gps power .groupBegin() - .number("[23]") // fix mode + .number("([023])") // fix mode .number("(dd)") // pdop .number("dd") // satellites - .number("xxxx") // seconds from last + .number("xxxx") // data age .number("[01]") // modem power .number("[0-5]") // gsm status .number("(dd)") // rssi @@ -81,6 +88,7 @@ public class TaipProtocolDecoder extends BaseProtocolDecoder { .number("xx") // seconds from last .groupEnd("?") .groupEnd("?") + .groupEnd() .any() .compile(); @@ -116,6 +124,7 @@ public class TaipProtocolDecoder extends BaseProtocolDecoder { Position position = new Position(getProtocolName()); + Boolean valid = null; Integer event = null; if (parser.hasNext(3)) { @@ -129,27 +138,6 @@ public class TaipProtocolDecoder extends BaseProtocolDecoder { event = parser.nextInt(); } - if (event != null) { - switch (event) { - case 22: - position.set(Position.KEY_ALARM, Position.ALARM_ACCELERATION); - break; - case 23: - position.set(Position.KEY_ALARM, Position.ALARM_BRAKING); - break; - case 24: - position.set(Position.KEY_ALARM, Position.ALARM_ACCIDENT); - break; - case 26: - case 28: - position.set(Position.KEY_ALARM, Position.ALARM_CORNERING); - break; - default: - position.set(Position.KEY_EVENT, event); - break; - } - } - if (parser.hasNext(6)) { position.setTime(parser.nextDateTime(Parser.DateTimeFormat.DMY_HMS)); } @@ -166,6 +154,15 @@ public class TaipProtocolDecoder extends BaseProtocolDecoder { position.setSpeed(UnitsConverter.knotsFromMph(parser.nextDouble(0))); position.setCourse(parser.nextDouble(0)); + if (parser.hasNext(4)) { + valid = parser.nextInt() > 0; + int input = parser.nextHexInt(); + position.set(Position.KEY_IGNITION, BitUtil.check(input, 7)); + position.set(Position.KEY_INPUT, input); + event = parser.nextInt(); + position.set(Position.KEY_HDOP, parser.nextInt()); + } + if (parser.hasNext(4)) { position.set(Position.KEY_INPUT, parser.nextHexInt(0)); position.set(Position.KEY_SATELLITES, parser.nextHexInt(0)); @@ -174,13 +171,35 @@ public class TaipProtocolDecoder extends BaseProtocolDecoder { } if (parser.hasNext(4)) { + valid = parser.nextInt() > 0; position.set(Position.KEY_PDOP, parser.nextInt()); position.set(Position.KEY_RSSI, parser.nextInt()); position.set(Position.PREFIX_TEMP + 1, parser.nextInt() * 0.01); position.set(Position.PREFIX_TEMP + 2, parser.nextInt() * 0.01); } - position.setValid(true); + position.setValid(valid != null ? valid : true); + + if (event != null) { + position.set(Position.KEY_EVENT, event); + switch (event) { + case 22: + position.set(Position.KEY_ALARM, Position.ALARM_ACCELERATION); + break; + case 23: + position.set(Position.KEY_ALARM, Position.ALARM_BRAKING); + break; + case 24: + position.set(Position.KEY_ALARM, Position.ALARM_ACCIDENT); + break; + case 26: + case 28: + position.set(Position.KEY_ALARM, Position.ALARM_CORNERING); + break; + default: + break; + } + } String[] attributes = null; beginIndex = sentence.indexOf(';'); @@ -250,14 +269,13 @@ public class TaipProtocolDecoder extends BaseProtocolDecoder { if (deviceSession != null) { if (channel != null) { if (messageIndex != null) { - String response = ">ACK;" + messageIndex + ";ID=" + uniqueId + ";*"; + String response = ">ACK;ID=" + uniqueId + ";" + messageIndex + ";*"; response += String.format("%02X", Checksum.xor(response)) + "<"; channel.write(response, remoteAddress); } else { channel.write(uniqueId, remoteAddress); } } - return position; } diff --git a/test/org/traccar/protocol/TaipProtocolDecoderTest.java b/test/org/traccar/protocol/TaipProtocolDecoderTest.java index a92e82498..264724ce9 100644 --- a/test/org/traccar/protocol/TaipProtocolDecoderTest.java +++ b/test/org/traccar/protocol/TaipProtocolDecoderTest.java @@ -10,6 +10,9 @@ public class TaipProtocolDecoderTest extends ProtocolTest { TaipProtocolDecoder decoder = new TaipProtocolDecoder(new TaipProtocol()); + verifyPosition(decoder, text( + ">RGP211217112154-2748332-058946350000000FF7F2100;ID=AA01;#0002;*2D<")); + verifyPosition(decoder, text( ">RCV12270218010247-3471349-058400030002057F001200020A1D013010600001509+0000FF+0000FF;#1DE2;ID=7196;*03<")); -- cgit v1.2.3 From 4f3cd9dae285cfbd0ff541911381c08f50b59435 Mon Sep 17 00:00:00 2001 From: Anton Tananaev Date: Mon, 12 Mar 2018 04:56:12 +1300 Subject: Implement Castelcom fuel cut commands --- src/org/traccar/protocol/CastelProtocol.java | 8 ++- .../traccar/protocol/CastelProtocolDecoder.java | 1 + .../traccar/protocol/CastelProtocolEncoder.java | 73 ++++++++++++++++++++++ .../protocol/CastelProtocolEncoderTest.java | 22 +++++++ 4 files changed, 103 insertions(+), 1 deletion(-) create mode 100644 src/org/traccar/protocol/CastelProtocolEncoder.java create mode 100644 test/org/traccar/protocol/CastelProtocolEncoderTest.java diff --git a/src/org/traccar/protocol/CastelProtocol.java b/src/org/traccar/protocol/CastelProtocol.java index db9df0674..d5ba5cd55 100644 --- a/src/org/traccar/protocol/CastelProtocol.java +++ b/src/org/traccar/protocol/CastelProtocol.java @@ -1,5 +1,5 @@ /* - * Copyright 2015 Anton Tananaev (anton@traccar.org) + * Copyright 2015 - 2018 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. @@ -21,6 +21,7 @@ import org.jboss.netty.channel.ChannelPipeline; import org.jboss.netty.handler.codec.frame.LengthFieldBasedFrameDecoder; import org.traccar.BaseProtocol; import org.traccar.TrackerServer; +import org.traccar.model.Command; import java.nio.ByteOrder; import java.util.List; @@ -29,6 +30,9 @@ public class CastelProtocol extends BaseProtocol { public CastelProtocol() { super("castel"); + setSupportedDataCommands( + Command.TYPE_ENGINE_STOP, + Command.TYPE_ENGINE_RESUME); } @Override @@ -37,6 +41,7 @@ public class CastelProtocol extends BaseProtocol { @Override protected void addSpecificHandlers(ChannelPipeline pipeline) { pipeline.addLast("frameDecoder", new LengthFieldBasedFrameDecoder(1024, 2, 2, -4, 0)); + pipeline.addLast("objectEncoder", new CastelProtocolEncoder()); pipeline.addLast("objectDecoder", new CastelProtocolDecoder(CastelProtocol.this)); } }; @@ -46,6 +51,7 @@ public class CastelProtocol extends BaseProtocol { server = new TrackerServer(new ConnectionlessBootstrap(), getName()) { @Override protected void addSpecificHandlers(ChannelPipeline pipeline) { + pipeline.addLast("objectEncoder", new CastelProtocolEncoder()); pipeline.addLast("objectDecoder", new CastelProtocolDecoder(CastelProtocol.this)); } }; diff --git a/src/org/traccar/protocol/CastelProtocolDecoder.java b/src/org/traccar/protocol/CastelProtocolDecoder.java index ebd3d202c..44a5e213c 100644 --- a/src/org/traccar/protocol/CastelProtocolDecoder.java +++ b/src/org/traccar/protocol/CastelProtocolDecoder.java @@ -96,6 +96,7 @@ public class CastelProtocolDecoder extends BaseProtocolDecoder { public static final short MSG_CC_LOGIN = 0x4001; public static final short MSG_CC_LOGIN_RESPONSE = (short) 0x8001; public static final short MSG_CC_HEARTBEAT = 0x4206; + public static final short MSG_CC_PETROL_CONTROL = 0x4583; public static final short MSG_CC_HEARTBEAT_RESPONSE = (short) 0x8206; private Position readPosition(DeviceSession deviceSession, ChannelBuffer buf) { diff --git a/src/org/traccar/protocol/CastelProtocolEncoder.java b/src/org/traccar/protocol/CastelProtocolEncoder.java new file mode 100644 index 000000000..d69fc62cf --- /dev/null +++ b/src/org/traccar/protocol/CastelProtocolEncoder.java @@ -0,0 +1,73 @@ +/* + * Copyright 2018 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 org.jboss.netty.buffer.ChannelBuffer; +import org.jboss.netty.buffer.ChannelBuffers; +import org.traccar.BaseProtocolEncoder; +import org.traccar.Context; +import org.traccar.helper.Checksum; +import org.traccar.helper.Log; +import org.traccar.model.Command; + +import java.nio.ByteOrder; +import java.nio.charset.StandardCharsets; + +public class CastelProtocolEncoder extends BaseProtocolEncoder { + + private ChannelBuffer encodeContent(long deviceId, int type, ChannelBuffer content) { + ChannelBuffer buf = ChannelBuffers.dynamicBuffer(ByteOrder.LITTLE_ENDIAN, 0); + String uniqueId = Context.getIdentityManager().getById(deviceId).getUniqueId(); + + buf.writeByte('@'); + buf.writeByte('@'); + + buf.writeShort(2 + 2 + 1 + 20 + content.readableBytes()); // length + + buf.writeByte(4); // protocol version + + buf.writeBytes(uniqueId.getBytes(StandardCharsets.US_ASCII)); + buf.writeZero(20 - uniqueId.length()); + + buf.writeShort(type); + buf.writeBytes(content); + + buf.writeShort(Checksum.crc16(Checksum.CRC16_X25, buf.toByteBuffer())); + + buf.writeByte('\r'); + buf.writeByte('\n'); + + return buf; + } + + @Override + protected Object encodeCommand(Command command) { + ChannelBuffer content = ChannelBuffers.dynamicBuffer(ByteOrder.LITTLE_ENDIAN, 0); + switch (command.getType()) { + case Command.TYPE_ENGINE_STOP: + content.writeByte(1); + return encodeContent(command.getDeviceId(), CastelProtocolDecoder.MSG_CC_PETROL_CONTROL, content); + case Command.TYPE_ENGINE_RESUME: + content.writeByte(0); + return encodeContent(command.getDeviceId(), CastelProtocolDecoder.MSG_CC_PETROL_CONTROL, content); + default: + Log.warning(new UnsupportedOperationException(command.getType())); + break; + } + return null; + } + +} diff --git a/test/org/traccar/protocol/CastelProtocolEncoderTest.java b/test/org/traccar/protocol/CastelProtocolEncoderTest.java new file mode 100644 index 000000000..6e519363f --- /dev/null +++ b/test/org/traccar/protocol/CastelProtocolEncoderTest.java @@ -0,0 +1,22 @@ +package org.traccar.protocol; + +import org.junit.Test; +import org.traccar.ProtocolTest; +import org.traccar.model.Command; + +public class CastelProtocolEncoderTest extends ProtocolTest { + + @Test + public void testEncode() throws Exception { + + CastelProtocolEncoder encoder = new CastelProtocolEncoder(); + + Command command = new Command(); + command.setDeviceId(1); + command.setType(Command.TYPE_ENGINE_STOP); + + verifyCommand(encoder, command, binary("40401a00043132333435363738393031323334350000000000834501c7280d0a")); + + } + +} -- cgit v1.2.3 From d8bbc2b6e8c1e7447f3e317747982b3c6362c66d Mon Sep 17 00:00:00 2001 From: Anton Tananaev Date: Mon, 12 Mar 2018 05:21:58 +1300 Subject: Add new OBD PID parameters --- src/org/traccar/helper/ObdDecoder.java | 31 +++++++++------------- .../protocol/CastelProtocolDecoderTest.java | 3 +++ 2 files changed, 16 insertions(+), 18 deletions(-) diff --git a/src/org/traccar/helper/ObdDecoder.java b/src/org/traccar/helper/ObdDecoder.java index 4bc3bcdfb..1bdcce352 100644 --- a/src/org/traccar/helper/ObdDecoder.java +++ b/src/org/traccar/helper/ObdDecoder.java @@ -1,5 +1,5 @@ /* - * Copyright 2015 - 2016 Anton Tananaev (anton@traccar.org) + * Copyright 2015 - 2018 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. @@ -29,15 +29,6 @@ public final class ObdDecoder { private static final int MODE_FREEZE_FRAME = 0x02; private static final int MODE_CODES = 0x03; - private static final int PID_ENGINE_LOAD = 0x04; - private static final int PID_COOLANT_TEMPERATURE = 0x05; - private static final int PID_ENGINE_RPM = 0x0C; - private static final int PID_VEHICLE_SPEED = 0x0D; - private static final int PID_THROTTLE_POSITION = 0x11; - private static final int PID_MIL_DISTANCE = 0x21; - private static final int PID_FUEL_LEVEL = 0x2F; - private static final int PID_DISTANCE_CLEARED = 0x31; - public static Map.Entry decode(int mode, String value) { switch (mode) { case MODE_CURRENT: @@ -86,21 +77,25 @@ public final class ObdDecoder { public static Map.Entry decodeData(int pid, int value, boolean convert) { switch (pid) { - case PID_ENGINE_LOAD: + case 0x04: return createEntry(Position.KEY_ENGINE_LOAD, convert ? value * 100 / 255 : value); - case PID_COOLANT_TEMPERATURE: + case 0x05: return createEntry(Position.KEY_COOLANT_TEMP, convert ? value - 40 : value); - case PID_ENGINE_RPM: + case 0x0B: + return createEntry("mapIntake", value); + case 0x0C: return createEntry(Position.KEY_RPM, convert ? value / 4 : value); - case PID_VEHICLE_SPEED: + case 0x0D: return createEntry(Position.KEY_OBD_SPEED, value); - case PID_THROTTLE_POSITION: + case 0x0F: + return createEntry("intakeTemp", convert ? value - 40 : value); + case 0x11: return createEntry(Position.KEY_THROTTLE, convert ? value * 100 / 255 : value); - case PID_MIL_DISTANCE: + case 0x21: return createEntry("milDistance", value); - case PID_FUEL_LEVEL: + case 0x2F: return createEntry(Position.KEY_FUEL_LEVEL, convert ? value * 100 / 255 : value); - case PID_DISTANCE_CLEARED: + case 0x31: return createEntry("clearedDistance", value); default: return null; diff --git a/test/org/traccar/protocol/CastelProtocolDecoderTest.java b/test/org/traccar/protocol/CastelProtocolDecoderTest.java index 2dccf4a0a..cb44087a1 100644 --- a/test/org/traccar/protocol/CastelProtocolDecoderTest.java +++ b/test/org/traccar/protocol/CastelProtocolDecoderTest.java @@ -12,6 +12,9 @@ public class CastelProtocolDecoderTest extends ProtocolTest { CastelProtocolDecoder decoder = new CastelProtocolDecoder(new CastelProtocol()); + verifyAttributes(decoder, binary(ByteOrder.LITTLE_ENDIAN, + "4040560004323133474c3230313630303033363400000000004002a122a05a5423a05abe0f2a000000000007f1f90014000000040001640011170003001e000505210b210c210d210f2101062b58ef02001a25950d0a")); + verifyAttributes(decoder, binary(ByteOrder.LITTLE_ENDIAN, "404057000431303031313132353239393837000000000000004002C1F06952F0F169529C9111000000000069830000470000000400036401014C01030078000505210C210D210F21102101073BE8030064280AEB930D0A")); -- cgit v1.2.3 From b2ab0b471591494523b12e5f1810a481b2cd1a65 Mon Sep 17 00:00:00 2001 From: Anton Tananaev Date: Mon, 12 Mar 2018 06:44:27 +1300 Subject: Cautela NMEA conversion protocol --- setup/default.xml | 1 + src/org/traccar/protocol/CautelaProtocol.java | 47 +++++++++++++ .../traccar/protocol/CautelaProtocolDecoder.java | 77 ++++++++++++++++++++++ .../protocol/CautelaProtocolDecoderTest.java | 18 +++++ 4 files changed, 143 insertions(+) create mode 100644 src/org/traccar/protocol/CautelaProtocol.java create mode 100644 src/org/traccar/protocol/CautelaProtocolDecoder.java create mode 100644 test/org/traccar/protocol/CautelaProtocolDecoderTest.java diff --git a/setup/default.xml b/setup/default.xml index 6be2c08e3..81a3cd128 100644 --- a/setup/default.xml +++ b/setup/default.xml @@ -228,5 +228,6 @@ 5157 5158 5159 + 5160 diff --git a/src/org/traccar/protocol/CautelaProtocol.java b/src/org/traccar/protocol/CautelaProtocol.java new file mode 100644 index 000000000..89ab7a1d0 --- /dev/null +++ b/src/org/traccar/protocol/CautelaProtocol.java @@ -0,0 +1,47 @@ +/* + * Copyright 2018 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 org.jboss.netty.bootstrap.ServerBootstrap; +import org.jboss.netty.channel.ChannelPipeline; +import org.jboss.netty.handler.codec.frame.LineBasedFrameDecoder; +import org.jboss.netty.handler.codec.string.StringDecoder; +import org.jboss.netty.handler.codec.string.StringEncoder; +import org.traccar.BaseProtocol; +import org.traccar.TrackerServer; + +import java.util.List; + +public class CautelaProtocol extends BaseProtocol { + + public CautelaProtocol() { + super("cautela"); + } + + @Override + public void initTrackerServers(List serverList) { + serverList.add(new TrackerServer(new ServerBootstrap(), getName()) { + @Override + protected void addSpecificHandlers(ChannelPipeline pipeline) { + pipeline.addLast("frameDecoder", new LineBasedFrameDecoder(1024)); + pipeline.addLast("stringEncoder", new StringEncoder()); + pipeline.addLast("stringDecoder", new StringDecoder()); + pipeline.addLast("objectDecoder", new CautelaProtocolDecoder(CautelaProtocol.this)); + } + }); + } + +} diff --git a/src/org/traccar/protocol/CautelaProtocolDecoder.java b/src/org/traccar/protocol/CautelaProtocolDecoder.java new file mode 100644 index 000000000..94ba35c91 --- /dev/null +++ b/src/org/traccar/protocol/CautelaProtocolDecoder.java @@ -0,0 +1,77 @@ +/* + * Copyright 2018 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 org.jboss.netty.channel.Channel; +import org.traccar.BaseProtocolDecoder; +import org.traccar.DeviceSession; +import org.traccar.helper.DateBuilder; +import org.traccar.helper.Parser; +import org.traccar.helper.PatternBuilder; +import org.traccar.model.Position; + +import java.net.SocketAddress; +import java.util.regex.Pattern; + +public class CautelaProtocolDecoder extends BaseProtocolDecoder { + + public CautelaProtocolDecoder(CautelaProtocol protocol) { + super(protocol); + } + + private static final Pattern PATTERN = new PatternBuilder() + .number("(d+),") // type + .number("(d+),") // imei + .number("(dd),(dd),(dd),") // date (ddmmyy) + .number("(-?d+.d+),") // longitude + .number("(-?d+.d+),") // latitude + .number("(dd)(dd),") // time (hhmm) + .any() + .compile(); + + @Override + protected Object decode( + Channel channel, SocketAddress remoteAddress, Object msg) throws Exception { + + Parser parser = new Parser(PATTERN, (String) msg); + if (!parser.matches()) { + return null; + } + + String type = parser.next(); + + DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, parser.next()); + if (deviceSession == null) { + return null; + } + + Position position = new Position(getProtocolName()); + position.setDeviceId(deviceSession.getDeviceId()); + + DateBuilder dateBuilder = new DateBuilder(); + dateBuilder.setDateReverse(parser.nextInt(), parser.nextInt(), parser.nextInt()); + + position.setValid(true); + position.setLongitude(parser.nextDouble()); + position.setLatitude(parser.nextDouble()); + + dateBuilder.setHour(parser.nextInt()).setMinute(parser.nextInt()); + position.setTime(dateBuilder.getDate()); + + return position; + } + +} diff --git a/test/org/traccar/protocol/CautelaProtocolDecoderTest.java b/test/org/traccar/protocol/CautelaProtocolDecoderTest.java new file mode 100644 index 000000000..4fd785c9f --- /dev/null +++ b/test/org/traccar/protocol/CautelaProtocolDecoderTest.java @@ -0,0 +1,18 @@ +package org.traccar.protocol; + +import org.junit.Test; +import org.traccar.ProtocolTest; + +public class CautelaProtocolDecoderTest extends ProtocolTest { + + @Test + public void testDecode() throws Exception { + + CautelaProtocolDecoder decoder = new CautelaProtocolDecoder(new CautelaProtocol()); + + verifyPosition(decoder, text( + "20,010907000000,14,02,18,16.816667,96.166667,1325,S,*2E")); + + } + +} -- cgit v1.2.3 From 51785ed622057269459c3abf6ba85d0001d71e71 Mon Sep 17 00:00:00 2001 From: Anton Tananaev Date: Mon, 12 Mar 2018 06:49:06 +1300 Subject: Fix Cautela coordinates decoding --- src/org/traccar/protocol/CautelaProtocolDecoder.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/org/traccar/protocol/CautelaProtocolDecoder.java b/src/org/traccar/protocol/CautelaProtocolDecoder.java index 94ba35c91..d7bf4fb51 100644 --- a/src/org/traccar/protocol/CautelaProtocolDecoder.java +++ b/src/org/traccar/protocol/CautelaProtocolDecoder.java @@ -36,8 +36,8 @@ public class CautelaProtocolDecoder extends BaseProtocolDecoder { .number("(d+),") // type .number("(d+),") // imei .number("(dd),(dd),(dd),") // date (ddmmyy) - .number("(-?d+.d+),") // longitude .number("(-?d+.d+),") // latitude + .number("(-?d+.d+),") // longitude .number("(dd)(dd),") // time (hhmm) .any() .compile(); @@ -65,8 +65,8 @@ public class CautelaProtocolDecoder extends BaseProtocolDecoder { dateBuilder.setDateReverse(parser.nextInt(), parser.nextInt(), parser.nextInt()); position.setValid(true); - position.setLongitude(parser.nextDouble()); position.setLatitude(parser.nextDouble()); + position.setLongitude(parser.nextDouble()); dateBuilder.setHour(parser.nextInt()).setMinute(parser.nextInt()); position.setTime(dateBuilder.getDate()); -- cgit v1.2.3 From 708c38728c8c250ba7205cd7fd77182895d1ea07 Mon Sep 17 00:00:00 2001 From: Anton Tananaev Date: Tue, 13 Mar 2018 00:57:51 +1300 Subject: Remove missing GL200 parameters (fix #3805) --- .../traccar/protocol/Gl200TextProtocolDecoder.java | 96 +++++++++++++--------- 1 file changed, 57 insertions(+), 39 deletions(-) diff --git a/src/org/traccar/protocol/Gl200TextProtocolDecoder.java b/src/org/traccar/protocol/Gl200TextProtocolDecoder.java index c47764989..362fc38c7 100644 --- a/src/org/traccar/protocol/Gl200TextProtocolDecoder.java +++ b/src/org/traccar/protocol/Gl200TextProtocolDecoder.java @@ -429,8 +429,8 @@ public class Gl200TextProtocolDecoder extends BaseProtocolDecoder { parser.next(); // odometer or external power - position.set(Position.KEY_BATTERY, parser.nextDouble(0)); - position.set(Position.KEY_CHARGE, parser.nextInt(0) == 1); + position.set(Position.KEY_BATTERY, parser.nextDouble()); + position.set(Position.KEY_CHARGE, parser.nextInt() == 1); position.set(Position.KEY_BATTERY_LEVEL, parser.nextInt()); @@ -444,7 +444,7 @@ public class Gl200TextProtocolDecoder extends BaseProtocolDecoder { getLastLocation(position, parser.nextDateTime()); - position.set(Position.KEY_INDEX, parser.nextHexInt(0)); + position.set(Position.KEY_INDEX, parser.nextHexInt()); return position; } @@ -457,8 +457,8 @@ public class Gl200TextProtocolDecoder extends BaseProtocolDecoder { } position.set("deviceType", parser.next()); - position.set(Position.KEY_VERSION_FW, parser.nextHexInt(0)); - position.set(Position.KEY_VERSION_HW, parser.nextHexInt(0)); + position.set(Position.KEY_VERSION_FW, parser.nextHexInt()); + position.set(Position.KEY_VERSION_HW, parser.nextHexInt()); getLastLocation(position, parser.nextDateTime()); @@ -466,8 +466,8 @@ public class Gl200TextProtocolDecoder extends BaseProtocolDecoder { } private void decodeLocation(Position position, Parser parser) { - int hdop = parser.nextInt(0); - position.setValid(hdop > 0); + Integer hdop = parser.nextInt(); + position.setValid(hdop == null || hdop > 0); position.set(Position.KEY_HDOP, hdop); position.setSpeed(UnitsConverter.knotsFromKph(parser.nextDouble(0))); @@ -476,25 +476,27 @@ public class Gl200TextProtocolDecoder extends BaseProtocolDecoder { if (parser.hasNext(8)) { position.setValid(true); - position.setLongitude(parser.nextDouble(0)); - position.setLatitude(parser.nextDouble(0)); + position.setLongitude(parser.nextDouble()); + position.setLatitude(parser.nextDouble()); position.setTime(parser.nextDateTime()); } else { getLastLocation(position, null); } if (parser.hasNext(6)) { - int mcc = parser.nextInt(0); - int mnc = parser.nextInt(0); + int mcc = parser.nextInt(); + int mnc = parser.nextInt(); if (parser.hasNext(2)) { - position.setNetwork(new Network(CellTower.from(mcc, mnc, parser.nextInt(0), parser.nextInt(0)))); + position.setNetwork(new Network(CellTower.from(mcc, mnc, parser.nextInt(), parser.nextInt()))); } if (parser.hasNext(2)) { - position.setNetwork(new Network(CellTower.from(mcc, mnc, parser.nextHexInt(0), parser.nextHexInt(0)))); + position.setNetwork(new Network(CellTower.from(mcc, mnc, parser.nextHexInt(), parser.nextHexInt()))); } } - position.set(Position.KEY_ODOMETER, parser.nextDouble(0) * 1000); + if (parser.hasNext()) { + position.set(Position.KEY_ODOMETER, parser.nextDouble() * 1000); + } } private Object decodeObd(Channel channel, SocketAddress remoteAddress, String sentence) { @@ -509,16 +511,22 @@ public class Gl200TextProtocolDecoder extends BaseProtocolDecoder { position.set(Position.PREFIX_TEMP + 1, parser.nextInt()); position.set(Position.KEY_FUEL_CONSUMPTION, parser.next()); position.set("dtcsClearedDistance", parser.nextInt()); - position.set("odbConnect", parser.nextInt(0) == 1); + if (parser.hasNext()) { + position.set("odbConnect", parser.nextInt() == 1); + } position.set("dtcsNumber", parser.nextInt()); position.set("dtcsCodes", parser.next()); position.set(Position.KEY_THROTTLE, parser.nextInt()); position.set(Position.KEY_FUEL_LEVEL, parser.nextInt()); - position.set(Position.KEY_OBD_ODOMETER, parser.nextInt(0) * 1000); + if (parser.hasNext()) { + position.set(Position.KEY_OBD_ODOMETER, parser.nextInt() * 1000); + } decodeLocation(position, parser); - position.set(Position.KEY_ODOMETER, parser.nextDouble(0) * 1000); + if (parser.hasNext()) { + position.set(Position.KEY_OBD_ODOMETER, (int) (parser.nextDouble() * 1000)); + } decodeDeviceTime(position, parser); @@ -649,14 +657,14 @@ public class Gl200TextProtocolDecoder extends BaseProtocolDecoder { private void decodeStatus(Position position, Parser parser) { if (parser.hasNext(3)) { - int ignition = parser.nextHexInt(0); + int ignition = parser.nextHexInt(); if (BitUtil.check(ignition, 4)) { position.set(Position.KEY_IGNITION, false); } else if (BitUtil.check(ignition, 5)) { position.set(Position.KEY_IGNITION, true); } - position.set(Position.KEY_INPUT, parser.nextHexInt(0)); - position.set(Position.KEY_OUTPUT, parser.nextHexInt(0)); + position.set(Position.KEY_INPUT, parser.nextHexInt()); + position.set(Position.KEY_OUTPUT, parser.nextHexInt()); } } @@ -674,7 +682,7 @@ public class Gl200TextProtocolDecoder extends BaseProtocolDecoder { LinkedList positions = new LinkedList<>(); String vin = parser.next(); - int power = parser.nextInt(0); + Integer power = parser.nextInt(); Parser itemParser = new Parser(PATTERN_LOCATION, parser.next()); while (itemParser.find()) { @@ -692,14 +700,18 @@ public class Gl200TextProtocolDecoder extends BaseProtocolDecoder { decodeLocation(position, parser); - if (power > 10) { + if (power != null && power > 10) { position.set(Position.KEY_POWER, power * 0.001); // only on some devices } - position.set(Position.KEY_ODOMETER, parser.nextDouble(0) * 1000); + if (parser.hasNext()) { + position.set(Position.KEY_ODOMETER, parser.nextDouble() * 1000); + } position.set(Position.KEY_BATTERY_LEVEL, parser.nextInt()); - position.set(Position.KEY_ODOMETER, parser.nextDouble(0) * 1000); + if (parser.hasNext()) { + position.set(Position.KEY_ODOMETER, parser.nextDouble() * 1000); + } position.set(Position.KEY_HOURS, parser.next()); position.set(Position.PREFIX_ADC + 1, parser.next()); position.set(Position.PREFIX_ADC + 2, parser.next()); @@ -734,7 +746,7 @@ public class Gl200TextProtocolDecoder extends BaseProtocolDecoder { LinkedList positions = new LinkedList<>(); - int power = parser.nextInt(0); + Integer power = parser.nextInt(); Parser itemParser = new Parser(PATTERN_LOCATION, parser.next()); while (itemParser.find()) { @@ -750,8 +762,10 @@ public class Gl200TextProtocolDecoder extends BaseProtocolDecoder { decodeLocation(position, parser); - position.set(Position.KEY_POWER, power * 0.001); - position.set(Position.KEY_ODOMETER, parser.nextDouble(0) * 1000); + if (power != null) { + position.set(Position.KEY_POWER, power * 0.001); + } + position.set(Position.KEY_ODOMETER, parser.nextDouble() * 1000); position.set(Position.KEY_HOURS, parser.next()); position.set(Position.PREFIX_ADC + 1, parser.next()); position.set(Position.PREFIX_ADC + 2, parser.next()); @@ -813,7 +827,7 @@ public class Gl200TextProtocolDecoder extends BaseProtocolDecoder { decodeLocation(position, parser); position.set(Position.KEY_HOURS, parser.next()); - position.set(Position.KEY_ODOMETER, parser.nextDouble(0) * 1000); + position.set(Position.KEY_ODOMETER, parser.nextDouble() * 1000); decodeDeviceTime(position, parser); @@ -831,7 +845,7 @@ public class Gl200TextProtocolDecoder extends BaseProtocolDecoder { decodeLocation(position, parser); - position.set(Position.KEY_ODOMETER, parser.nextDouble(0) * 1000); + position.set(Position.KEY_ODOMETER, parser.nextDouble() * 1000); decodeDeviceTime(position, parser); @@ -849,7 +863,7 @@ public class Gl200TextProtocolDecoder extends BaseProtocolDecoder { Network network = new Network(); - parser.nextInt(0); // count + parser.nextInt(); // count Matcher matcher = Pattern.compile("([0-9a-fA-F]{12}),(-?\\d+),,,,").matcher(parser.next()); while (matcher.find()) { String mac = matcher.group(1).replaceAll("(..)", "$1:"); @@ -859,7 +873,7 @@ public class Gl200TextProtocolDecoder extends BaseProtocolDecoder { position.setNetwork(network); - position.set(Position.KEY_BATTERY_LEVEL, parser.nextInt(0)); + position.set(Position.KEY_BATTERY_LEVEL, parser.nextInt()); return position; } @@ -897,7 +911,7 @@ public class Gl200TextProtocolDecoder extends BaseProtocolDecoder { return null; } - int reportType = parser.nextInt(0); + int reportType = parser.nextInt(); if (type.equals("NMR")) { position.set(Position.KEY_MOTION, reportType == 1); } else if (type.equals("SOS")) { @@ -906,10 +920,14 @@ public class Gl200TextProtocolDecoder extends BaseProtocolDecoder { decodeLocation(position, parser); - position.set(Position.KEY_ODOMETER, parser.nextDouble(0) * 1000); - position.set(Position.KEY_BATTERY_LEVEL, parser.nextInt(0)); + if (parser.hasNext()) { + position.set(Position.KEY_ODOMETER, parser.nextDouble() * 1000); + } + position.set(Position.KEY_BATTERY_LEVEL, parser.nextInt()); - position.set(Position.KEY_ODOMETER, parser.nextDouble(0) * 1000); + if (parser.hasNext()) { + position.set(Position.KEY_ODOMETER, parser.nextDouble() * 1000); + } decodeDeviceTime(position, parser); @@ -927,7 +945,7 @@ public class Gl200TextProtocolDecoder extends BaseProtocolDecoder { return null; } - int hdop = parser.nextInt(0); + int hdop = parser.nextInt(); position.setValid(hdop > 0); position.set(Position.KEY_HDOP, hdop); @@ -936,8 +954,8 @@ public class Gl200TextProtocolDecoder extends BaseProtocolDecoder { position.setAltitude(parser.nextDouble(0)); if (parser.hasNext(2)) { - position.setLongitude(parser.nextDouble(0)); - position.setLatitude(parser.nextDouble(0)); + position.setLongitude(parser.nextDouble()); + position.setLatitude(parser.nextDouble()); } else { getLastLocation(position, null); } @@ -948,7 +966,7 @@ public class Gl200TextProtocolDecoder extends BaseProtocolDecoder { if (parser.hasNext(4)) { position.setNetwork(new Network(CellTower.from( - parser.nextInt(0), parser.nextInt(0), parser.nextHexInt(0), parser.nextHexInt(0)))); + parser.nextInt(), parser.nextInt(), parser.nextHexInt(), parser.nextHexInt()))); } decodeDeviceTime(position, parser); -- cgit v1.2.3 From 612547f1b0cae5e0ba39359ed349799f2a87ca2f Mon Sep 17 00:00:00 2001 From: Anton Tananaev Date: Tue, 13 Mar 2018 01:00:09 +1300 Subject: Handle missing temperature (fix #3806) --- src/org/traccar/protocol/Gl200TextProtocolDecoder.java | 4 +++- test/org/traccar/protocol/Gl200TextProtocolDecoderTest.java | 3 +++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/src/org/traccar/protocol/Gl200TextProtocolDecoder.java b/src/org/traccar/protocol/Gl200TextProtocolDecoder.java index 362fc38c7..ff300d429 100644 --- a/src/org/traccar/protocol/Gl200TextProtocolDecoder.java +++ b/src/org/traccar/protocol/Gl200TextProtocolDecoder.java @@ -787,7 +787,9 @@ public class Gl200TextProtocolDecoder extends BaseProtocolDecoder { for (int i = 1; i <= deviceCount; i++) { index += 1; // id index += 1; // type - position.set(Position.PREFIX_TEMP + i, (short) Integer.parseInt(data[index++], 16) * 0.0625); + if (!data[index++].isEmpty()) { + position.set(Position.PREFIX_TEMP + i, (short) Integer.parseInt(data[index - 1], 16) * 0.0625); + } } } diff --git a/test/org/traccar/protocol/Gl200TextProtocolDecoderTest.java b/test/org/traccar/protocol/Gl200TextProtocolDecoderTest.java index 9ec67273c..56a159768 100644 --- a/test/org/traccar/protocol/Gl200TextProtocolDecoderTest.java +++ b/test/org/traccar/protocol/Gl200TextProtocolDecoderTest.java @@ -10,6 +10,9 @@ public class Gl200TextProtocolDecoderTest extends ProtocolTest { Gl200TextProtocolDecoder decoder = new Gl200TextProtocolDecoder(new Gl200Protocol()); + verifyPositions(decoder, buffer( + "+RESP:GTERI,310603,863286023345490,,00000002,,10,1,2,0.3,0,155.7,8.000000,52.000000,20171215213040,0262,0002,1450,9F13,00,1130.3,00539:27:19,,,110000,2,1,28FFD5239115034E,1,,20171215213041,27C7$")); + verifyPositions(decoder, buffer( "+RESP:GTERI,250C02,868789023691057,,00000019,,10,1,1,0.0,196,2258.0,-99.201807,19.559242,20180214002957,0334,0003,235B,7F8D,00,6786.7,,,,100,110000,1,0394,1,4,100.0,100.0,20180214003006,C72B$")); -- cgit v1.2.3 From ca72b34c7d7dcf2d6b20b87ce3f8cc26ac7911a1 Mon Sep 17 00:00:00 2001 From: Anton Tananaev Date: Tue, 13 Mar 2018 01:55:23 +1300 Subject: Fix CT-03 init response --- src/org/traccar/protocol/WatchProtocolDecoder.java | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/org/traccar/protocol/WatchProtocolDecoder.java b/src/org/traccar/protocol/WatchProtocolDecoder.java index 1dd07a3f7..495803ab6 100644 --- a/src/org/traccar/protocol/WatchProtocolDecoder.java +++ b/src/org/traccar/protocol/WatchProtocolDecoder.java @@ -171,7 +171,11 @@ public class WatchProtocolDecoder extends BaseProtocolDecoder { buf.readerIndex(contentIndex + 1); } - if (type.equals("LK")) { + if (type.equals("INIT")) { + + sendResponse(channel, manufacturer, id, index, "INIT,1"); + + } else if (type.equals("LK")) { sendResponse(channel, manufacturer, id, index, "LK"); -- cgit v1.2.3 From 7264149967d2bee385afcab6a8435d4b77141a49 Mon Sep 17 00:00:00 2001 From: Anton Tananaev Date: Tue, 13 Mar 2018 02:05:48 +1300 Subject: Fix method length issue --- src/org/traccar/protocol/WatchProtocolDecoder.java | 69 +++++++++++----------- 1 file changed, 35 insertions(+), 34 deletions(-) diff --git a/src/org/traccar/protocol/WatchProtocolDecoder.java b/src/org/traccar/protocol/WatchProtocolDecoder.java index 495803ab6..12ec3bd3f 100644 --- a/src/org/traccar/protocol/WatchProtocolDecoder.java +++ b/src/org/traccar/protocol/WatchProtocolDecoder.java @@ -97,8 +97,38 @@ public class WatchProtocolDecoder extends BaseProtocolDecoder { return null; } - private void decodeTail(Position position, String data) { - String[] values = data.split(","); + private Position decodePosition(DeviceSession deviceSession, String data) { + + Parser parser = new Parser(PATTERN_POSITION, data); + if (!parser.matches()) { + return null; + } + + Position position = new Position(getProtocolName()); + position.setDeviceId(deviceSession.getDeviceId()); + + position.setTime(parser.nextDateTime(Parser.DateTimeFormat.DMY_HMS)); + + position.setValid(parser.next().equals("A")); + position.setLatitude(parser.nextCoordinate(Parser.CoordinateFormat.DEG_HEM)); + position.setLongitude(parser.nextCoordinate(Parser.CoordinateFormat.DEG_HEM)); + position.setSpeed(UnitsConverter.knotsFromKph(parser.nextDouble(0))); + position.setCourse(parser.nextDouble(0)); + position.setAltitude(parser.nextDouble(0)); + + position.set(Position.KEY_SATELLITES, parser.nextInt(0)); + position.set(Position.KEY_RSSI, parser.nextInt(0)); + position.set(Position.KEY_BATTERY_LEVEL, parser.nextInt(0)); + + position.set(Position.KEY_STEPS, parser.nextInt(0)); + + int status = parser.nextHexInt(0); + position.set(Position.KEY_ALARM, decodeAlarm(status)); + if (BitUtil.check(status, 4)) { + position.set(Position.KEY_MOTION, true); + } + + String[] values = parser.next().split(","); int index = 0; Network network = new Network(); @@ -127,6 +157,8 @@ public class WatchProtocolDecoder extends BaseProtocolDecoder { if (network.getCellTowers() != null || network.getWifiAccessPoints() != null) { position.setNetwork(network); } + + return position; } @Override @@ -200,38 +232,7 @@ public class WatchProtocolDecoder extends BaseProtocolDecoder { sendResponse(channel, manufacturer, id, index, "AL"); } - Parser parser = new Parser(PATTERN_POSITION, buf.toString(StandardCharsets.US_ASCII)); - if (!parser.matches()) { - return null; - } - - Position position = new Position(getProtocolName()); - position.setDeviceId(deviceSession.getDeviceId()); - - position.setTime(parser.nextDateTime(Parser.DateTimeFormat.DMY_HMS)); - - position.setValid(parser.next().equals("A")); - position.setLatitude(parser.nextCoordinate(Parser.CoordinateFormat.DEG_HEM)); - position.setLongitude(parser.nextCoordinate(Parser.CoordinateFormat.DEG_HEM)); - position.setSpeed(UnitsConverter.knotsFromKph(parser.nextDouble(0))); - position.setCourse(parser.nextDouble(0)); - position.setAltitude(parser.nextDouble(0)); - - position.set(Position.KEY_SATELLITES, parser.nextInt(0)); - position.set(Position.KEY_RSSI, parser.nextInt(0)); - position.set(Position.KEY_BATTERY_LEVEL, parser.nextInt(0)); - - position.set(Position.KEY_STEPS, parser.nextInt(0)); - - int status = parser.nextHexInt(0); - position.set(Position.KEY_ALARM, decodeAlarm(status)); - if (BitUtil.check(status, 4)) { - position.set(Position.KEY_MOTION, true); - } - - decodeTail(position, parser.next()); - - return position; + return decodePosition(deviceSession, buf.toString(StandardCharsets.US_ASCII)); } else if (type.equals("TKQ")) { -- cgit v1.2.3 From 709b02703cfd974bc17127f3e4683b9d96249eb9 Mon Sep 17 00:00:00 2001 From: Anton Tananaev Date: Tue, 13 Mar 2018 06:40:50 +1300 Subject: Support ATrack fuel sensor --- src/org/traccar/protocol/AtrackProtocolDecoder.java | 17 +++++++++++++++-- .../org/traccar/protocol/AtrackProtocolDecoderTest.java | 3 +++ 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/src/org/traccar/protocol/AtrackProtocolDecoder.java b/src/org/traccar/protocol/AtrackProtocolDecoder.java index 236b608d6..8138f0fcb 100644 --- a/src/org/traccar/protocol/AtrackProtocolDecoder.java +++ b/src/org/traccar/protocol/AtrackProtocolDecoder.java @@ -1,5 +1,5 @@ /* - * Copyright 2013 - 2017 Anton Tananaev (anton@traccar.org) + * Copyright 2013 - 2018 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. @@ -36,6 +36,7 @@ import java.util.HashMap; import java.util.LinkedList; import java.util.List; import java.util.Map; +import java.util.regex.Matcher; import java.util.regex.Pattern; public class AtrackProtocolDecoder extends BaseProtocolDecoder { @@ -43,6 +44,7 @@ public class AtrackProtocolDecoder extends BaseProtocolDecoder { private static final int MIN_DATA_LENGTH = 40; private boolean longDate; + private boolean decimalFuel; private boolean custom; private String form; @@ -52,6 +54,7 @@ public class AtrackProtocolDecoder extends BaseProtocolDecoder { super(protocol); longDate = Context.getConfig().getBoolean(getProtocolName() + ".longDate"); + decimalFuel = Context.getConfig().getBoolean(getProtocolName() + ".decimalFuel"); custom = Context.getConfig().getBoolean(getProtocolName() + ".custom"); form = Context.getConfig().getString(getProtocolName() + ".form"); @@ -330,7 +333,17 @@ public class AtrackProtocolDecoder extends BaseProtocolDecoder { position.set(Position.PREFIX_TEMP + 1, buf.readShort() * 0.1); position.set(Position.PREFIX_TEMP + 2, buf.readShort() * 0.1); - position.set("message", readString(buf)); + String message = readString(buf); + if (message != null && !message.isEmpty()) { + Pattern pattern = Pattern.compile("FULS:F=(\\p{XDigit}+) t=(\\p{XDigit}+) N=(\\p{XDigit}+)"); + Matcher matcher = pattern.matcher(message); + if (matcher.find()) { + int value = Integer.parseInt(matcher.group(3), decimalFuel ? 10 : 16); + position.set(Position.KEY_FUEL_LEVEL, value * 0.1); + } else { + position.set("message", message); + } + } if (custom) { String form = this.form; diff --git a/test/org/traccar/protocol/AtrackProtocolDecoderTest.java b/test/org/traccar/protocol/AtrackProtocolDecoderTest.java index b5271f511..bd606c320 100644 --- a/test/org/traccar/protocol/AtrackProtocolDecoderTest.java +++ b/test/org/traccar/protocol/AtrackProtocolDecoderTest.java @@ -10,6 +10,9 @@ public class AtrackProtocolDecoderTest extends ProtocolTest { AtrackProtocolDecoder decoder = new AtrackProtocolDecoder(new AtrackProtocol()); + verifyPositions(decoder, binary( + "4050b5ed004a2523000310c83713f8c05a88b43e5a88b43f5a88b43f021e0ad5fffdc0a800f3020003059100080000000000000007d007d046554c533a463d3230393120743d3137204e3d3039303100")); + verifyAttributes(decoder, buffer( "$INFO=358683066267395,AX7,Rev.0.61 Build.1624,358683066267395,466924131626767,89886920041316267670,144,0,9,1,12,1,0\r\n")); -- cgit v1.2.3 From bb49f1e2c38e23860e87e1801edc456f30165d5f Mon Sep 17 00:00:00 2001 From: Anton Tananaev Date: Tue, 13 Mar 2018 16:46:42 +1300 Subject: DM G50 hello message response --- src/org/traccar/protocol/DmtProtocolDecoder.java | 15 ++++++++++----- test/org/traccar/protocol/DmtProtocolDecoderTest.java | 3 +++ 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/src/org/traccar/protocol/DmtProtocolDecoder.java b/src/org/traccar/protocol/DmtProtocolDecoder.java index 74db5a6f7..3739253f0 100644 --- a/src/org/traccar/protocol/DmtProtocolDecoder.java +++ b/src/org/traccar/protocol/DmtProtocolDecoder.java @@ -1,5 +1,5 @@ /* - * Copyright 2017 Anton Tananaev (anton@traccar.org) + * Copyright 2017 - 2018 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. @@ -70,8 +70,7 @@ public class DmtProtocolDecoder extends BaseProtocolDecoder { buf.skipBytes(2); // header int type = buf.readUnsignedByte(); - - buf.readUnsignedShort(); // length + int length = buf.readUnsignedShort(); if (type == MSG_HELLO) { @@ -81,8 +80,14 @@ public class DmtProtocolDecoder extends BaseProtocolDecoder { channel, remoteAddress, buf.readBytes(15).toString(StandardCharsets.US_ASCII)); ChannelBuffer response = ChannelBuffers.dynamicBuffer(ByteOrder.LITTLE_ENDIAN, 0); - response.writeInt((int) ((System.currentTimeMillis() - 1356998400000L) / 1000)); - response.writeInt(deviceSession != null ? 0 : 1); // flags + if (length == 51) { + response.writeByte(0); // reserved + response.writeInt(0); // reserved + } else { + response.writeInt((int) ((System.currentTimeMillis() - 1356998400000L) / 1000)); + response.writeInt(deviceSession != null ? 0 : 1); // flags + } + sendResponse(channel, MSG_HELLO_RESPONSE, response); } else if (type == MSG_COMMIT) { diff --git a/test/org/traccar/protocol/DmtProtocolDecoderTest.java b/test/org/traccar/protocol/DmtProtocolDecoderTest.java index 2575d77ed..a83dcafe2 100644 --- a/test/org/traccar/protocol/DmtProtocolDecoderTest.java +++ b/test/org/traccar/protocol/DmtProtocolDecoderTest.java @@ -12,6 +12,9 @@ public class DmtProtocolDecoderTest extends ProtocolTest { DmtProtocolDecoder decoder = new DmtProtocolDecoder(new DmtProtocol()); + verifyNull(decoder, binary(ByteOrder.LITTLE_ENDIAN, + "0255003300001b00003335333232393032373533393235310038393931353030303030303030313330343539340000000403041910780603")); + verifyNull(decoder, binary(ByteOrder.LITTLE_ENDIAN, "025500310038f90100333533333233303831363639373330003839363130313835303031383234383434363330002202010900000000")); -- cgit v1.2.3 From 2a30c9de983223a3af1070543975165ceb0710f9 Mon Sep 17 00:00:00 2001 From: Anton Tananaev Date: Wed, 14 Mar 2018 05:27:23 +1300 Subject: Option to ignore duplicate alarms --- setup/default.xml | 1 + src/org/traccar/events/AlertEventHandler.java | 24 ++++++++++++++++++++---- 2 files changed, 21 insertions(+), 4 deletions(-) diff --git a/setup/default.xml b/setup/default.xml index 81a3cd128..8701ddc4c 100644 --- a/setup/default.xml +++ b/setup/default.xml @@ -19,6 +19,7 @@ ./logs/tracker-server.log true + true true ./media diff --git a/src/org/traccar/events/AlertEventHandler.java b/src/org/traccar/events/AlertEventHandler.java index 003ccb662..7db371c70 100644 --- a/src/org/traccar/events/AlertEventHandler.java +++ b/src/org/traccar/events/AlertEventHandler.java @@ -1,5 +1,5 @@ /* - * Copyright 2016 Anton Tananaev (anton@traccar.org) + * Copyright 2016 - 2018 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. @@ -19,18 +19,34 @@ import java.util.Collections; import java.util.Map; import org.traccar.BaseEventHandler; +import org.traccar.Context; import org.traccar.model.Event; import org.traccar.model.Position; public class AlertEventHandler extends BaseEventHandler { + private final boolean ignoreDuplicateAlerts; + + public AlertEventHandler() { + ignoreDuplicateAlerts = Context.getConfig().getBoolean("event.ignoreDuplicateAlerts"); + } + @Override protected Map analyzePosition(Position position) { Object alarm = position.getAttributes().get(Position.KEY_ALARM); if (alarm != null) { - Event event = new Event(Event.TYPE_ALARM, position.getDeviceId(), position.getId()); - event.set(Position.KEY_ALARM, (String) alarm); - return Collections.singletonMap(event, position); + boolean ignoreAlert = false; + if (ignoreDuplicateAlerts) { + Position lastPosition = Context.getIdentityManager().getLastPosition(position.getDeviceId()); + if (lastPosition != null && alarm.equals(lastPosition.getAttributes().get(Position.KEY_ALARM))) { + ignoreAlert = true; + } + } + if (!ignoreAlert) { + Event event = new Event(Event.TYPE_ALARM, position.getDeviceId(), position.getId()); + event.set(Position.KEY_ALARM, (String) alarm); + return Collections.singletonMap(event, position); + } } return null; } -- cgit v1.2.3 From 4ae3297dde98d292cd0c87141440f94312459b65 Mon Sep 17 00:00:00 2001 From: Anton Tananaev Date: Wed, 14 Mar 2018 06:08:04 +1300 Subject: Include Teltonika RDIF if not zero --- src/org/traccar/protocol/TeltonikaProtocolDecoder.java | 7 +++++-- test/org/traccar/protocol/TeltonikaProtocolDecoderTest.java | 3 +++ 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/src/org/traccar/protocol/TeltonikaProtocolDecoder.java b/src/org/traccar/protocol/TeltonikaProtocolDecoder.java index 9e249247a..d2069e6c9 100644 --- a/src/org/traccar/protocol/TeltonikaProtocolDecoder.java +++ b/src/org/traccar/protocol/TeltonikaProtocolDecoder.java @@ -1,5 +1,5 @@ /* - * Copyright 2013 - 2017 Anton Tananaev (anton@traccar.org) + * Copyright 2013 - 2018 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. @@ -133,7 +133,10 @@ public class TeltonikaProtocolDecoder extends BaseProtocolDecoder { position.set(Position.PREFIX_TEMP + 3, readValue(buf, length, true) * 0.1); break; case 78: - position.set(Position.KEY_DRIVER_UNIQUE_ID, String.format("%016X", readValue(buf, length, false))); + long driverUniqueId = readValue(buf, length, false); + if (driverUniqueId != 0) { + position.set(Position.KEY_DRIVER_UNIQUE_ID, String.format("%016X", driverUniqueId)); + } break; case 80: position.set("workMode", readValue(buf, length, false)); diff --git a/test/org/traccar/protocol/TeltonikaProtocolDecoderTest.java b/test/org/traccar/protocol/TeltonikaProtocolDecoderTest.java index c4fae52ff..d62eef4a0 100644 --- a/test/org/traccar/protocol/TeltonikaProtocolDecoderTest.java +++ b/test/org/traccar/protocol/TeltonikaProtocolDecoderTest.java @@ -14,6 +14,9 @@ public class TeltonikaProtocolDecoderTest extends ProtocolTest { verifyNull(decoder, binary( "000F313233343536373839303132333435")); + verifyPositions(decoder, false, binary( + "0000000000000035080100000161f37c50500020de5ba60ef11450000000000000000006040100b300b400ef000109002000014e0000000000000000010000be52")); + verifyPositions(decoder, false, binary( "000000000000008c08010000013feb55ff74000f0ea850209a690000940000120000001e09010002000300040016014703f0001504c8000c0900730a00460b00501300464306d7440000b5000bb60007422e9f180000cd0386ce000107c700000000f10000601a46000001344800000bb84900000bb84a00000bb84c00000000024e0000000000000000cf00000000000000000100003fca")); -- cgit v1.2.3 From 0aa78a779f764cad80d54d4710118212d52402c1 Mon Sep 17 00:00:00 2001 From: Anton Tananaev Date: Wed, 14 Mar 2018 07:09:40 +1300 Subject: Decode Xirgo events and alarms --- src/org/traccar/model/Position.java | 1 + src/org/traccar/protocol/XirgoProtocolDecoder.java | 65 +++++++++++++++++++++- 2 files changed, 64 insertions(+), 2 deletions(-) diff --git a/src/org/traccar/model/Position.java b/src/org/traccar/model/Position.java index 981c2292f..49e3231c3 100644 --- a/src/org/traccar/model/Position.java +++ b/src/org/traccar/model/Position.java @@ -115,6 +115,7 @@ public class Position extends Message { public static final String ALARM_GPS_ANTENNA_CUT = "gpsAntennaCut"; public static final String ALARM_ACCIDENT = "accident"; public static final String ALARM_TOW = "tow"; + public static final String ALARM_IDLE = "idle"; public static final String ALARM_ACCELERATION = "hardAcceleration"; public static final String ALARM_BRAKING = "hardBraking"; public static final String ALARM_CORNERING = "hardCornering"; diff --git a/src/org/traccar/protocol/XirgoProtocolDecoder.java b/src/org/traccar/protocol/XirgoProtocolDecoder.java index 10dd298fd..1a753cb3e 100644 --- a/src/org/traccar/protocol/XirgoProtocolDecoder.java +++ b/src/org/traccar/protocol/XirgoProtocolDecoder.java @@ -1,5 +1,5 @@ /* - * Copyright 2015 - 2017 Anton Tananaev (anton@traccar.org) + * Copyright 2015 - 2018 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. @@ -95,6 +95,67 @@ public class XirgoProtocolDecoder extends BaseProtocolDecoder { .any() .compile(); + private void decodeEvent(Position position, int event) { + + position.set(Position.KEY_EVENT, event); + + switch (event) { + case 4005: + position.set(Position.KEY_CHARGE, false); + break; + case 6002: + position.set(Position.KEY_ALARM, Position.ALARM_OVERSPEED); + break; + case 6006: + position.set(Position.KEY_ALARM, Position.ALARM_ACCELERATION); + break; + case 6007: + position.set(Position.KEY_ALARM, Position.ALARM_BRAKING); + break; + case 6008: + position.set(Position.KEY_ALARM, Position.ALARM_LOW_POWER); + break; + case 6009: + position.set(Position.KEY_ALARM, Position.ALARM_POWER_CUT); + break; + case 6010: + position.set(Position.KEY_ALARM, Position.ALARM_POWER_RESTORED); + break; + case 6011: + case 6013: + position.set(Position.KEY_IGNITION, true); + break; + case 6012: + case 6014: + position.set(Position.KEY_IGNITION, false); + break; + case 6016: + position.set(Position.KEY_ALARM, Position.ALARM_IDLE); + break; + case 6017: + position.set(Position.KEY_ALARM, Position.ALARM_TOW); + break; + case 6030: + case 6071: + position.set(Position.KEY_MOTION, true); + break; + case 6031: + position.set(Position.KEY_MOTION, false); + break; + case 6032: + position.set(Position.KEY_ALARM, Position.ALARM_PARKING); + break; + case 6090: + position.set(Position.KEY_ALARM, Position.ALARM_REMOVING); + break; + case 6091: + position.set(Position.KEY_ALARM, Position.ALARM_LOW_BATTERY); + break; + default: + break; + } + } + @Override protected Object decode( Channel channel, SocketAddress remoteAddress, Object msg) throws Exception { @@ -133,7 +194,7 @@ public class XirgoProtocolDecoder extends BaseProtocolDecoder { } position.setDeviceId(deviceSession.getDeviceId()); - position.set(Position.KEY_EVENT, parser.next()); + decodeEvent(position, parser.nextInt()); position.setTime(parser.nextDateTime()); -- cgit v1.2.3 From b04758893af970e7954571771c1e6c136d472f20 Mon Sep 17 00:00:00 2001 From: Anton Tananaev Date: Wed, 14 Mar 2018 07:11:35 +1300 Subject: Additional Xirgo events --- src/org/traccar/protocol/XirgoProtocolDecoder.java | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/src/org/traccar/protocol/XirgoProtocolDecoder.java b/src/org/traccar/protocol/XirgoProtocolDecoder.java index 1a753cb3e..461503af1 100644 --- a/src/org/traccar/protocol/XirgoProtocolDecoder.java +++ b/src/org/traccar/protocol/XirgoProtocolDecoder.java @@ -100,6 +100,18 @@ public class XirgoProtocolDecoder extends BaseProtocolDecoder { position.set(Position.KEY_EVENT, event); switch (event) { + case 4001: + case 4003: + case 6011: + case 6013: + position.set(Position.KEY_IGNITION, true); + break; + case 4002: + case 4004: + case 6012: + case 6014: + position.set(Position.KEY_IGNITION, false); + break; case 4005: position.set(Position.KEY_CHARGE, false); break; @@ -121,14 +133,6 @@ public class XirgoProtocolDecoder extends BaseProtocolDecoder { case 6010: position.set(Position.KEY_ALARM, Position.ALARM_POWER_RESTORED); break; - case 6011: - case 6013: - position.set(Position.KEY_IGNITION, true); - break; - case 6012: - case 6014: - position.set(Position.KEY_IGNITION, false); - break; case 6016: position.set(Position.KEY_ALARM, Position.ALARM_IDLE); break; -- cgit v1.2.3 From 7c7002e3b001b9e4f8f9a00f9a14dff87b5c9862 Mon Sep 17 00:00:00 2001 From: Anton Tananaev Date: Wed, 14 Mar 2018 07:58:15 +1300 Subject: Support Watch blood pressure --- src/org/traccar/protocol/WatchProtocolDecoder.java | 14 +++++++++----- test/org/traccar/protocol/WatchProtocolDecoderTest.java | 3 +++ 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/src/org/traccar/protocol/WatchProtocolDecoder.java b/src/org/traccar/protocol/WatchProtocolDecoder.java index 12ec3bd3f..325f2efd9 100644 --- a/src/org/traccar/protocol/WatchProtocolDecoder.java +++ b/src/org/traccar/protocol/WatchProtocolDecoder.java @@ -238,7 +238,7 @@ public class WatchProtocolDecoder extends BaseProtocolDecoder { sendResponse(channel, manufacturer, id, index, "TKQ"); - } else if (type.equals("PULSE") || type.equals("heart")) { + } else if (type.equals("PULSE") || type.equals("heart") || type.equals("bphrt")) { if (buf.readable()) { @@ -247,10 +247,14 @@ public class WatchProtocolDecoder extends BaseProtocolDecoder { getLastLocation(position, new Date()); - position.setValid(false); - String pulse = buf.toString(StandardCharsets.US_ASCII); - position.set("pulse", pulse); - position.set(Position.KEY_RESULT, pulse); + String[] values = buf.toString(StandardCharsets.US_ASCII).split(","); + int valueIndex = 0; + + if (type.equals("bphrt")) { + position.set("pressureLow", values[valueIndex++]); + position.set("pressureHigh", values[valueIndex++]); + } + position.set("pulse", values[valueIndex]); return position; diff --git a/test/org/traccar/protocol/WatchProtocolDecoderTest.java b/test/org/traccar/protocol/WatchProtocolDecoderTest.java index 657cb0640..6ba04bf8e 100644 --- a/test/org/traccar/protocol/WatchProtocolDecoderTest.java +++ b/test/org/traccar/protocol/WatchProtocolDecoderTest.java @@ -10,6 +10,9 @@ public class WatchProtocolDecoderTest extends ProtocolTest { WatchProtocolDecoder decoder = new WatchProtocolDecoder(new WatchProtocol()); + verifyAttributes(decoder, buffer( + "[3G*4700609403*0013*bphrt,120,79,73,,,,]")); + verifyPosition(decoder, buffer( "[3G*8308373902*0080*AL,230817,095346,A,47.083950,N,15.4821850,E,7.60,273.8,0.0,4,15,44,0,0,00200010,2,255,232,1,7605,42530,118,7605,58036,119,0,65.8]")); -- cgit v1.2.3 From 82eace30ab2bd8742301519e6157c8ef238f4885 Mon Sep 17 00:00:00 2001 From: Kevin Goos Date: Wed, 14 Mar 2018 15:04:57 +0100 Subject: Working on laipac2 --- src/org/traccar/protocol/Laipac2Protocol.java | 47 ++++++++ .../traccar/protocol/Laipac2ProtocolDecoder.java | 123 +++++++++++++++++++++ .../traccar/protocol/OsmAndProtocolDecoder.java | 3 +- tools/translate.py | 35 ++++++ traccar-web | 2 +- 5 files changed, 207 insertions(+), 3 deletions(-) create mode 100644 src/org/traccar/protocol/Laipac2Protocol.java create mode 100644 src/org/traccar/protocol/Laipac2ProtocolDecoder.java create mode 100644 tools/translate.py diff --git a/src/org/traccar/protocol/Laipac2Protocol.java b/src/org/traccar/protocol/Laipac2Protocol.java new file mode 100644 index 000000000..5f132f626 --- /dev/null +++ b/src/org/traccar/protocol/Laipac2Protocol.java @@ -0,0 +1,47 @@ +/* + * Copyright 2015 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 org.jboss.netty.bootstrap.ServerBootstrap; +import org.jboss.netty.channel.ChannelPipeline; +import org.jboss.netty.handler.codec.frame.LineBasedFrameDecoder; +import org.jboss.netty.handler.codec.string.StringDecoder; +import org.jboss.netty.handler.codec.string.StringEncoder; +import org.traccar.BaseProtocol; +import org.traccar.TrackerServer; + +import java.util.List; + +public class Laipac2Protocol extends BaseProtocol { + + public Laipac2Protocol() { + super("laipac2"); + } + + @Override + public void initTrackerServers(List serverList) { + serverList.add(new TrackerServer(new ServerBootstrap(), getName()) { + @Override + protected void addSpecificHandlers(ChannelPipeline pipeline) { + pipeline.addLast("frameDecoder", new LineBasedFrameDecoder(1024)); + pipeline.addLast("stringEncoder", new StringEncoder()); + pipeline.addLast("stringDecoder", new StringDecoder()); + pipeline.addLast("objectDecoder", new Laipac2ProtocolDecoder(Laipac2Protocol.this)); + } + }); + } + +} diff --git a/src/org/traccar/protocol/Laipac2ProtocolDecoder.java b/src/org/traccar/protocol/Laipac2ProtocolDecoder.java new file mode 100644 index 000000000..ba40c0dce --- /dev/null +++ b/src/org/traccar/protocol/Laipac2ProtocolDecoder.java @@ -0,0 +1,123 @@ +/* + * Copyright 2013 - 2017 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 org.jboss.netty.channel.Channel; +import org.traccar.BaseProtocolDecoder; +import org.traccar.DeviceSession; +import org.traccar.helper.Checksum; +import org.traccar.helper.DateBuilder; +import org.traccar.helper.Parser; +import org.traccar.helper.PatternBuilder; +import org.traccar.model.Position; + +import java.net.SocketAddress; +import java.util.regex.Pattern; + +public class Laipac2ProtocolDecoder extends BaseProtocolDecoder { + + public Laipac2ProtocolDecoder(Laipac2Protocol protocol) { + super(protocol); + } + + private static final Pattern PATTERN = new PatternBuilder() + .text("$AVRMC,") + .expression("([^,]+),") // identifier + .number("(dd)(dd)(dd),") // time (hhmmss) + .expression("([AVRPavrp]),") // validity + .number("(dd)(dd.d+),") // latitude + .expression("([NS]),") + .number("(ddd)(dd.d+),") // longitude + .number("([EW]),") + .number("(d+.d+),") // speed + .number("(d+.d+),") // course + .number("(dd)(dd)(dd),") // date (ddmmyy) + .expression("([abZXMHE86430]),") // event code + .number("(d+),") // battery voltage + .number("(d+),") // current mileage + .number("(d),") // GPS on/off (1 = on, 0 = off) + .number("(d),") // Analog port 1 + .number("(d+),") // Analog port 2 + .expression("([0-9a-fA-F]{4})") // Cell 1 - Cell Net Code + .expression("([0-9a-fA-F]{4}),") // Cell 1 - Cell ID Code + .number("(d+)") // Cell 2 + .text("*") + .number("(xx)") // checksum + .compile(); + + @Override + protected Object decode(Channel channel, SocketAddress remoteAddress, Object msg) throws Exception { + String sentence = (String) msg; + + if (sentence.startsWith("$ECHK") && channel != null) { + channel.write(sentence + "\r\n"); // heartbeat + return null; + } + + Parser parser = new Parser(PATTERN, 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()); + + DateBuilder dateBuilder = new DateBuilder() + .setTime(parser.nextInt(0), parser.nextInt(0), parser.nextInt(0)); + + String status = parser.next(); + position.setValid(status.toUpperCase().equals("A")); + + position.setLatitude(parser.nextCoordinate()); + position.setLongitude(parser.nextCoordinate()); + position.setSpeed(parser.nextDouble(0)); + position.setCourse(parser.nextDouble(0)); + + dateBuilder.setDateReverse(parser.nextInt(0), parser.nextInt(0), parser.nextInt(0)); + position.setTime(dateBuilder.getDate()); + + String type = parser.next(); + position.set(Position.KEY_BATTERY_LEVEL, parser.nextDouble()); + //position.set(Position.KEY_, parser.nextDouble()); + String checksum = parser.next(); + + if (channel != null) { + + if (Character.isLowerCase(status.charAt(0))) { + String response = "$EAVACK," + type + "," + checksum; + response += Checksum.nmea(response); + channel.write(response); + } + + if (type.equals("S") || type.equals("T")) { + channel.write("$AVCFG,00000000,t*21"); + } else if (type.equals("3")) { + channel.write("$AVCFG,00000000,d*31"); + } else if (type.equals("X") || type.equals("4")) { + channel.write("$AVCFG,00000000,x*2D"); + } + + } + + return position; + } + +} diff --git a/src/org/traccar/protocol/OsmAndProtocolDecoder.java b/src/org/traccar/protocol/OsmAndProtocolDecoder.java index 03abdd588..014ebb233 100644 --- a/src/org/traccar/protocol/OsmAndProtocolDecoder.java +++ b/src/org/traccar/protocol/OsmAndProtocolDecoder.java @@ -42,8 +42,7 @@ public class OsmAndProtocolDecoder extends BaseHttpProtocolDecoder { } @Override - protected Object decode( - Channel channel, SocketAddress remoteAddress, Object msg) throws Exception { + protected Object decode(Channel channel, SocketAddress remoteAddress, Object msg) throws Exception { HttpRequest request = (HttpRequest) msg; QueryStringDecoder decoder = new QueryStringDecoder(request.getUri()); diff --git a/tools/translate.py b/tools/translate.py new file mode 100644 index 000000000..e8324a61a --- /dev/null +++ b/tools/translate.py @@ -0,0 +1,35 @@ +#!/usr/bin/python + +import os +import optparse +import urllib2 +import json +import base64 + +parser = optparse.OptionParser() +parser.add_option("-u", "--user", dest="username", help="transifex user login") +parser.add_option("-p", "--password", dest="password", help="transifex user password") + +(options, args) = parser.parse_args() + +if not options.username or not options.password: + parser.error('User name and password are required') + +os.chdir(os.path.dirname(os.path.abspath(__file__))) + +path = "../web/l10n/" + +def request(url): + req = urllib2.Request(url) + auth = base64.encodestring("%s:%s" % (options.username, options.password)).replace("\n", "") + req.add_header("Authorization", "Basic %s" % auth) + return urllib2.urlopen(req) + +resource = json.load(request("https://www.transifex.com/api/2/project/traccar/resource/web/?details")) + +for language in resource["available_languages"]: + code = language["code"] + data = request("https://www.transifex.com/api/2/project/traccar/resource/web/translation/" + code + "?file") + file = open(path + code + ".json", "wb") + file.write(data.read()) + file.close() diff --git a/traccar-web b/traccar-web index f08f6fa4b..464aa58c4 160000 --- a/traccar-web +++ b/traccar-web @@ -1 +1 @@ -Subproject commit f08f6fa4b20f003dd624656746a49237bbff2441 +Subproject commit 464aa58c49ac5e04d419a68b6ee5bcf9d0c36798 -- cgit v1.2.3 From 2bc0b397c1aeb28bb82b887a78ceb8c8de940e8c Mon Sep 17 00:00:00 2001 From: Kevin Goos Date: Wed, 14 Mar 2018 16:42:09 +0100 Subject: Added extra info for laipac --- setup/default.xml | 3 +- src/org/traccar/model/Position.java | 7 +- src/org/traccar/protocol/Laipac2Protocol.java | 47 -------- .../traccar/protocol/Laipac2ProtocolDecoder.java | 123 -------------------- .../traccar/protocol/LaipacSFKamelProtocol.java | 47 ++++++++ .../protocol/LaipacSFKamelProtocolDecoder.java | 128 +++++++++++++++++++++ .../protocol/LaipacSFKamelProtocolDecoderTest.java | 35 ++++++ 7 files changed, 218 insertions(+), 172 deletions(-) delete mode 100644 src/org/traccar/protocol/Laipac2Protocol.java delete mode 100644 src/org/traccar/protocol/Laipac2ProtocolDecoder.java create mode 100644 src/org/traccar/protocol/LaipacSFKamelProtocol.java create mode 100644 src/org/traccar/protocol/LaipacSFKamelProtocolDecoder.java create mode 100644 test/org/traccar/protocol/LaipacSFKamelProtocolDecoderTest.java diff --git a/setup/default.xml b/setup/default.xml index 6be2c08e3..515abf61d 100644 --- a/setup/default.xml +++ b/setup/default.xml @@ -116,7 +116,8 @@ 5045 5046 5047 - 5048 + + 5048 5049 5050 5051 diff --git a/src/org/traccar/model/Position.java b/src/org/traccar/model/Position.java index 981c2292f..fdecb7e20 100644 --- a/src/org/traccar/model/Position.java +++ b/src/org/traccar/model/Position.java @@ -52,6 +52,8 @@ public class Position extends Message { public static final String KEY_BATTERY_LEVEL = "batteryLevel"; // percentage public static final String KEY_FUEL_LEVEL = "fuel"; // liters public static final String KEY_FUEL_CONSUMPTION = "fuelConsumption"; // liters/hour + public static final String KEY_ANALOG_1 = "analog 1"; // volts + public static final String KEY_ANALOG_2 = "analog 2"; // volts public static final String KEY_VERSION_FW = "versionFw"; public static final String KEY_VERSION_HW = "versionHw"; @@ -88,6 +90,10 @@ public class Position extends Message { public static final String KEY_DRIVER_UNIQUE_ID = "driverUniqueId"; + public static final String KEY_CELL_NET_CODE = "cellNetCode"; + public static final String KEY_CELL_ID_CODE = "cellIdCode"; + public static final String KEY_COUNTRY_CODE = "countryCode"; + // Start with 1 not 0 public static final String PREFIX_TEMP = "temp"; public static final String PREFIX_ADC = "adc"; @@ -290,5 +296,4 @@ public class Position extends Message { public String getType() { return super.getType(); } - } diff --git a/src/org/traccar/protocol/Laipac2Protocol.java b/src/org/traccar/protocol/Laipac2Protocol.java deleted file mode 100644 index 5f132f626..000000000 --- a/src/org/traccar/protocol/Laipac2Protocol.java +++ /dev/null @@ -1,47 +0,0 @@ -/* - * Copyright 2015 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 org.jboss.netty.bootstrap.ServerBootstrap; -import org.jboss.netty.channel.ChannelPipeline; -import org.jboss.netty.handler.codec.frame.LineBasedFrameDecoder; -import org.jboss.netty.handler.codec.string.StringDecoder; -import org.jboss.netty.handler.codec.string.StringEncoder; -import org.traccar.BaseProtocol; -import org.traccar.TrackerServer; - -import java.util.List; - -public class Laipac2Protocol extends BaseProtocol { - - public Laipac2Protocol() { - super("laipac2"); - } - - @Override - public void initTrackerServers(List serverList) { - serverList.add(new TrackerServer(new ServerBootstrap(), getName()) { - @Override - protected void addSpecificHandlers(ChannelPipeline pipeline) { - pipeline.addLast("frameDecoder", new LineBasedFrameDecoder(1024)); - pipeline.addLast("stringEncoder", new StringEncoder()); - pipeline.addLast("stringDecoder", new StringDecoder()); - pipeline.addLast("objectDecoder", new Laipac2ProtocolDecoder(Laipac2Protocol.this)); - } - }); - } - -} diff --git a/src/org/traccar/protocol/Laipac2ProtocolDecoder.java b/src/org/traccar/protocol/Laipac2ProtocolDecoder.java deleted file mode 100644 index ba40c0dce..000000000 --- a/src/org/traccar/protocol/Laipac2ProtocolDecoder.java +++ /dev/null @@ -1,123 +0,0 @@ -/* - * Copyright 2013 - 2017 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 org.jboss.netty.channel.Channel; -import org.traccar.BaseProtocolDecoder; -import org.traccar.DeviceSession; -import org.traccar.helper.Checksum; -import org.traccar.helper.DateBuilder; -import org.traccar.helper.Parser; -import org.traccar.helper.PatternBuilder; -import org.traccar.model.Position; - -import java.net.SocketAddress; -import java.util.regex.Pattern; - -public class Laipac2ProtocolDecoder extends BaseProtocolDecoder { - - public Laipac2ProtocolDecoder(Laipac2Protocol protocol) { - super(protocol); - } - - private static final Pattern PATTERN = new PatternBuilder() - .text("$AVRMC,") - .expression("([^,]+),") // identifier - .number("(dd)(dd)(dd),") // time (hhmmss) - .expression("([AVRPavrp]),") // validity - .number("(dd)(dd.d+),") // latitude - .expression("([NS]),") - .number("(ddd)(dd.d+),") // longitude - .number("([EW]),") - .number("(d+.d+),") // speed - .number("(d+.d+),") // course - .number("(dd)(dd)(dd),") // date (ddmmyy) - .expression("([abZXMHE86430]),") // event code - .number("(d+),") // battery voltage - .number("(d+),") // current mileage - .number("(d),") // GPS on/off (1 = on, 0 = off) - .number("(d),") // Analog port 1 - .number("(d+),") // Analog port 2 - .expression("([0-9a-fA-F]{4})") // Cell 1 - Cell Net Code - .expression("([0-9a-fA-F]{4}),") // Cell 1 - Cell ID Code - .number("(d+)") // Cell 2 - .text("*") - .number("(xx)") // checksum - .compile(); - - @Override - protected Object decode(Channel channel, SocketAddress remoteAddress, Object msg) throws Exception { - String sentence = (String) msg; - - if (sentence.startsWith("$ECHK") && channel != null) { - channel.write(sentence + "\r\n"); // heartbeat - return null; - } - - Parser parser = new Parser(PATTERN, 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()); - - DateBuilder dateBuilder = new DateBuilder() - .setTime(parser.nextInt(0), parser.nextInt(0), parser.nextInt(0)); - - String status = parser.next(); - position.setValid(status.toUpperCase().equals("A")); - - position.setLatitude(parser.nextCoordinate()); - position.setLongitude(parser.nextCoordinate()); - position.setSpeed(parser.nextDouble(0)); - position.setCourse(parser.nextDouble(0)); - - dateBuilder.setDateReverse(parser.nextInt(0), parser.nextInt(0), parser.nextInt(0)); - position.setTime(dateBuilder.getDate()); - - String type = parser.next(); - position.set(Position.KEY_BATTERY_LEVEL, parser.nextDouble()); - //position.set(Position.KEY_, parser.nextDouble()); - String checksum = parser.next(); - - if (channel != null) { - - if (Character.isLowerCase(status.charAt(0))) { - String response = "$EAVACK," + type + "," + checksum; - response += Checksum.nmea(response); - channel.write(response); - } - - if (type.equals("S") || type.equals("T")) { - channel.write("$AVCFG,00000000,t*21"); - } else if (type.equals("3")) { - channel.write("$AVCFG,00000000,d*31"); - } else if (type.equals("X") || type.equals("4")) { - channel.write("$AVCFG,00000000,x*2D"); - } - - } - - return position; - } - -} diff --git a/src/org/traccar/protocol/LaipacSFKamelProtocol.java b/src/org/traccar/protocol/LaipacSFKamelProtocol.java new file mode 100644 index 000000000..5e1beabbd --- /dev/null +++ b/src/org/traccar/protocol/LaipacSFKamelProtocol.java @@ -0,0 +1,47 @@ +/* + * Copyright 2015 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 org.jboss.netty.bootstrap.ServerBootstrap; +import org.jboss.netty.channel.ChannelPipeline; +import org.jboss.netty.handler.codec.frame.LineBasedFrameDecoder; +import org.jboss.netty.handler.codec.string.StringDecoder; +import org.jboss.netty.handler.codec.string.StringEncoder; +import org.traccar.BaseProtocol; +import org.traccar.TrackerServer; + +import java.util.List; + +public class LaipacSFKamelProtocol extends BaseProtocol { + + public LaipacSFKamelProtocol() { + super("laipacsfkamel"); + } + + @Override + public void initTrackerServers(List serverList) { + serverList.add(new TrackerServer(new ServerBootstrap(), getName()) { + @Override + protected void addSpecificHandlers(ChannelPipeline pipeline) { + pipeline.addLast("frameDecoder", new LineBasedFrameDecoder(1024)); + pipeline.addLast("stringEncoder", new StringEncoder()); + pipeline.addLast("stringDecoder", new StringDecoder()); + pipeline.addLast("objectDecoder", new LaipacSFKamelProtocolDecoder(LaipacSFKamelProtocol.this)); + } + }); + } + +} diff --git a/src/org/traccar/protocol/LaipacSFKamelProtocolDecoder.java b/src/org/traccar/protocol/LaipacSFKamelProtocolDecoder.java new file mode 100644 index 000000000..decf279df --- /dev/null +++ b/src/org/traccar/protocol/LaipacSFKamelProtocolDecoder.java @@ -0,0 +1,128 @@ +/* + * Copyright 2013 - 2017 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 org.jboss.netty.channel.Channel; +import org.traccar.BaseProtocolDecoder; +import org.traccar.DeviceSession; +import org.traccar.helper.Checksum; +import org.traccar.helper.DateBuilder; +import org.traccar.helper.Parser; +import org.traccar.helper.PatternBuilder; +import org.traccar.model.Position; + +import java.net.SocketAddress; +import java.util.regex.Pattern; + +public class LaipacSFKamelProtocolDecoder extends BaseProtocolDecoder { + + public LaipacSFKamelProtocolDecoder(LaipacSFKamelProtocol protocol) { + super(protocol); + } + + private static final Pattern PATTERN = new PatternBuilder() + .text("$AVRMC,") + .expression("([^,]+),") // identifier + .number("(dd)(dd)(dd),") // time (hhmmss) + .expression("([AVRPavrp]),") // validity + .number("(dd)(dd.d+),") // latitude + .expression("([NS]),") + .number("(ddd)(dd.d+),") // longitude + .number("([EW]),") + .number("(d+.d+),") // speed + .number("(d+.d+),") // course + .number("(dd)(dd)(dd),") // date (ddmmyy) + .expression("([abZXMHE86430]),") // event code + .number("(d+),") // battery voltage + .number("(d+),") // current mileage + .number("(d),") // GPS on/off (1 = on, 0 = off) + .number("(d+),") // Analog port 1 + .number("(d+),") // Analog port 2 + .expression("([0-9a-fA-F]{4})") // Cell 1 - Cell Net Code + .expression("([0-9a-fA-F]{4}),") // Cell 1 - Cell ID Code + .number("(d{3})") // Cell 2 - Country Code + .number("(d{3})") // Cell 2 - Operator Code + .text("*") + .number("(xx)") // checksum + .compile(); + + @Override + protected Object decode(Channel channel, SocketAddress remoteAddress, Object msg) throws Exception { + String sentence = (String) msg; + + if (sentence.startsWith("$ECHK") && channel != null) { + channel.write(sentence + "\r\n"); // heartbeat + return null; + } + + Parser parser = new Parser(PATTERN, 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()); + + DateBuilder dateBuilder = new DateBuilder() + .setTime(parser.nextInt(0), parser.nextInt(0), parser.nextInt(0)); + + String status = parser.next(); + position.setValid(status.toUpperCase().equals("A")); + + position.setLatitude(parser.nextCoordinate()); + position.setLongitude(parser.nextCoordinate()); + position.setSpeed(parser.nextDouble(0)); + position.setCourse(parser.nextDouble(0)); + + dateBuilder.setDateReverse(parser.nextInt(0), parser.nextInt(0), parser.nextInt(0)); + position.setTime(dateBuilder.getDate()); + + String eventCode = parser.next(); + position.set(Position.KEY_EVENT, eventCode); + position.set(Position.KEY_BATTERY, parser.nextDouble() * 0.001); + position.set(Position.KEY_TOTAL_DISTANCE, parser.nextDouble()); + position.set(Position.KEY_GPS, parser.nextInt()); + position.set(Position.KEY_ANALOG_1, parser.nextDouble() * 0.001); + position.set(Position.KEY_ANALOG_2, parser.nextDouble() * 0.001); + position.set(Position.KEY_CELL_NET_CODE, parser.next()); + position.set(Position.KEY_CELL_ID_CODE, parser.next()); + position.set(Position.KEY_COUNTRY_CODE, parser.next()); + position.set(Position.KEY_OPERATOR, parser.next()); + String checksum = parser.next(); + + if (channel != null) { + if (Character.isLowerCase(status.charAt(0))) { + String response = "$EAVACK," + eventCode + "," + checksum; + response += Checksum.nmea(response); + channel.write(response); + } + + if (eventCode.equals("3")) { + channel.write("$AVCFG,00000000,d*31"); + } else if (eventCode.equals("X") || eventCode.equals("4")) { + channel.write("$AVCFG,00000000,x*2D"); + } + } + + return position; + } + +} diff --git a/test/org/traccar/protocol/LaipacSFKamelProtocolDecoderTest.java b/test/org/traccar/protocol/LaipacSFKamelProtocolDecoderTest.java new file mode 100644 index 000000000..05de5dcbe --- /dev/null +++ b/test/org/traccar/protocol/LaipacSFKamelProtocolDecoderTest.java @@ -0,0 +1,35 @@ +package org.traccar.protocol; + +import org.junit.Test; +import org.traccar.ProtocolTest; + +public class LaipacSFKamelProtocolDecoderTest extends ProtocolTest { + + @Test + public void testDecode() throws Exception { + + LaipacSFKamelProtocolDecoder decoder = new LaipacSFKamelProtocolDecoder(new LaipacSFKamelProtocol()); + + verifyPosition(decoder, text( + "$AVRMC,358174067149865,084514,r,5050.1314,N,00419.9719,E,0.68,306.39,120318,0,3882,84,1,0,0,3EE4A617,020610*4E")); + + verifyNull(decoder, text( + "$AVSYS,99999999,V1.50,SN0000103,32768*15")); + + verifyNull(decoder, text( + "$ECHK,99999999,0*35")); + + verifyNull(decoder, text( + "$AVSYS,MSG00002,14406,7046811160,64*1A")); + + verifyNull(decoder, text( + "$EAVSYS,MSG00002,8931086013104404999,,Owner,0x52014406*76")); + + verifyNull(decoder, text( + "$ECHK,MSG00002,0*5E")); + + verifyPosition(decoder, text( + "$AVRMC,358174067149865,111602,r,5050.1262,N,00419.9660,E,0.00,0.00,120318,0,3843,95,1,0,0,3EE4A617,020610*44")); + } + +} -- cgit v1.2.3 From 9863a0a0c9e3f796679f0fda3ab1048a3bac2475 Mon Sep 17 00:00:00 2001 From: Anton Tananaev Date: Thu, 15 Mar 2018 06:05:03 +1300 Subject: Fix Watch pressure decoding --- src/org/traccar/protocol/WatchProtocolDecoder.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/org/traccar/protocol/WatchProtocolDecoder.java b/src/org/traccar/protocol/WatchProtocolDecoder.java index 325f2efd9..c57279296 100644 --- a/src/org/traccar/protocol/WatchProtocolDecoder.java +++ b/src/org/traccar/protocol/WatchProtocolDecoder.java @@ -251,8 +251,8 @@ public class WatchProtocolDecoder extends BaseProtocolDecoder { int valueIndex = 0; if (type.equals("bphrt")) { - position.set("pressureLow", values[valueIndex++]); position.set("pressureHigh", values[valueIndex++]); + position.set("pressureLow", values[valueIndex++]); } position.set("pulse", values[valueIndex]); -- cgit v1.2.3 From 27a155908f6f460dde1a52c949cea7ee6625976a Mon Sep 17 00:00:00 2001 From: Anton Tananaev Date: Thu, 15 Mar 2018 16:05:10 +1300 Subject: Support DM G50 fixed 64 byte messages --- src/org/traccar/protocol/DmtProtocolDecoder.java | 262 +++++++++++++-------- .../traccar/protocol/DmtProtocolDecoderTest.java | 6 + 2 files changed, 175 insertions(+), 93 deletions(-) diff --git a/src/org/traccar/protocol/DmtProtocolDecoder.java b/src/org/traccar/protocol/DmtProtocolDecoder.java index 3739253f0..204d81820 100644 --- a/src/org/traccar/protocol/DmtProtocolDecoder.java +++ b/src/org/traccar/protocol/DmtProtocolDecoder.java @@ -21,6 +21,7 @@ import org.jboss.netty.channel.Channel; import org.traccar.BaseProtocolDecoder; import org.traccar.DeviceSession; import org.traccar.helper.BitUtil; +import org.traccar.helper.DateBuilder; import org.traccar.helper.UnitsConverter; import org.traccar.model.Position; @@ -42,6 +43,7 @@ public class DmtProtocolDecoder extends BaseProtocolDecoder { public static final int MSG_DATA_RECORD = 0x04; public static final int MSG_COMMIT = 0x05; public static final int MSG_COMMIT_RESPONSE = 0x06; + public static final int MSG_DATA_RECORD_64 = 0x10; public static final int MSG_CANNED_REQUEST_1 = 0x14; public static final int MSG_CANNED_RESPONSE_1 = 0x15; @@ -61,6 +63,169 @@ public class DmtProtocolDecoder extends BaseProtocolDecoder { } } + private List decodeFixed64(Channel channel, SocketAddress remoteAddress, ChannelBuffer buf) { + + DeviceSession deviceSession = getDeviceSession(channel, remoteAddress); + if (deviceSession == null) { + return null; + } + + List positions = new LinkedList<>(); + + while (buf.readableBytes() >= 64) { + Position position = new Position(getProtocolName()); + position.setDeviceId(deviceSession.getDeviceId()); + + buf.readByte(); // type + + position.set(Position.KEY_INDEX, buf.readUnsignedInt()); + + long time = buf.readUnsignedInt(); + position.setTime(new DateBuilder() + .setYear((int) (2000 + (time & 0x3F))) + .setMonth((int) (time >> 6) & 0xF) + .setDay((int) (time >> 10) & 0x1F) + .setHour((int) (time >> 15) & 0x1F) + .setMinute((int) (time >> 20) & 0x3F) + .setSecond((int) (time >> 26) & 0x3F) + .getDate()); + + position.setLatitude(buf.readInt() * 0.0000001); + position.setLongitude(buf.readInt() * 0.0000001); + position.setSpeed(UnitsConverter.knotsFromCps(buf.readUnsignedShort())); + position.setCourse(buf.readUnsignedByte() * 2); + position.setAltitude(buf.readShort()); + + buf.readUnsignedShort(); // position accuracy + buf.readUnsignedByte(); // speed accuracy + + position.set(Position.KEY_EVENT, buf.readUnsignedByte()); + + position.setValid(BitUtil.check(buf.readByte(), 0)); + + position.set(Position.KEY_INPUT, buf.readUnsignedInt()); + position.set(Position.KEY_OUTPUT, buf.readUnsignedShort()); + + for (int i = 1; i <= 5; i++) { + position.set(Position.PREFIX_ADC + i, buf.readShort()); + } + + position.set(Position.KEY_DEVICE_TEMP, buf.readByte()); + + buf.readShort(); // accelerometer x + buf.readShort(); // accelerometer y + buf.readShort(); // accelerometer z + + buf.skipBytes(8); // device id + + position.set(Position.KEY_PDOP, buf.readUnsignedShort() * 0.01); + + buf.skipBytes(2); // reserved + + buf.readUnsignedShort(); // checksum + + positions.add(position); + } + + return positions; + } + + private List decodeStandard(Channel channel, SocketAddress remoteAddress, ChannelBuffer buf) { + + DeviceSession deviceSession = getDeviceSession(channel, remoteAddress); + if (deviceSession == null) { + return null; + } + + List positions = new LinkedList<>(); + + while (buf.readable()) { + int recordEnd = buf.readerIndex() + buf.readUnsignedShort(); + + Position position = new Position(getProtocolName()); + position.setDeviceId(deviceSession.getDeviceId()); + + position.set(Position.KEY_INDEX, buf.readUnsignedInt()); + + position.setDeviceTime(new Date(1356998400000L + buf.readUnsignedInt() * 1000)); // since 1 Jan 2013 + + position.set(Position.KEY_EVENT, buf.readUnsignedByte()); + + while (buf.readerIndex() < recordEnd) { + + int fieldId = buf.readUnsignedByte(); + int fieldLength = buf.readUnsignedByte(); + int fieldEnd = buf.readerIndex() + (fieldLength == 255 ? buf.readUnsignedShort() : fieldLength); + + if (fieldId == 0) { + + position.setFixTime(new Date(1356998400000L + buf.readUnsignedInt() * 1000)); + position.setLatitude(buf.readInt() * 0.0000001); + position.setLongitude(buf.readInt() * 0.0000001); + position.setAltitude(buf.readShort()); + position.setSpeed(UnitsConverter.knotsFromCps(buf.readUnsignedShort())); + + buf.readUnsignedByte(); // speed accuracy + + position.setCourse(buf.readUnsignedByte() * 2); + + position.set(Position.KEY_PDOP, buf.readUnsignedByte() * 0.1); + + position.setAccuracy(buf.readUnsignedByte()); + position.setValid(buf.readUnsignedByte() != 0); + + } else if (fieldId == 2) { + + int input = buf.readInt(); + int output = buf.readUnsignedShort(); + int status = buf.readUnsignedShort(); + + position.set(Position.KEY_IGNITION, BitUtil.check(input, 0)); + + position.set(Position.KEY_INPUT, input); + position.set(Position.KEY_OUTPUT, output); + position.set(Position.KEY_STATUS, status); + + } else if (fieldId == 6) { + + while (buf.readerIndex() < fieldEnd) { + switch (buf.readUnsignedByte()) { + case 1: + position.set(Position.KEY_BATTERY, buf.readUnsignedShort() * 0.001); + break; + case 2: + position.set(Position.KEY_POWER, buf.readUnsignedShort() * 0.01); + break; + case 3: + position.set(Position.KEY_DEVICE_TEMP, buf.readShort() * 0.01); + break; + case 4: + position.set(Position.KEY_RSSI, buf.readUnsignedShort()); + break; + case 5: + position.set("solarPower", buf.readUnsignedShort() * 0.001); + break; + default: + break; + } + } + + } + + buf.readerIndex(fieldEnd); + + } + + if (position.getFixTime() == null) { + getLastLocation(position, position.getDeviceTime()); + } + + positions.add(position); + } + + return positions; + } + @Override protected Object decode( Channel channel, SocketAddress remoteAddress, Object msg) throws Exception { @@ -106,102 +271,13 @@ public class DmtProtocolDecoder extends BaseProtocolDecoder { sendResponse(channel, MSG_CANNED_RESPONSE_2, null); - } else if (type == MSG_DATA_RECORD) { - - DeviceSession deviceSession = getDeviceSession(channel, remoteAddress); - if (deviceSession == null) { - return null; - } - - List positions = new LinkedList<>(); - - while (buf.readable()) { - - int recordEnd = buf.readerIndex() + buf.readUnsignedShort(); - - Position position = new Position(getProtocolName()); - position.setDeviceId(deviceSession.getDeviceId()); - - position.set(Position.KEY_INDEX, buf.readUnsignedInt()); - - position.setDeviceTime(new Date(1356998400000L + buf.readUnsignedInt() * 1000)); // since 1 Jan 2013 - - position.set(Position.KEY_EVENT, buf.readUnsignedByte()); - - while (buf.readerIndex() < recordEnd) { + } else if (type == MSG_DATA_RECORD_64) { - int fieldId = buf.readUnsignedByte(); - int fieldLength = buf.readUnsignedByte(); - int fieldEnd = buf.readerIndex() + (fieldLength == 255 ? buf.readUnsignedShort() : fieldLength); + return decodeFixed64(channel, remoteAddress, buf); - if (fieldId == 0) { - - position.setFixTime(new Date(1356998400000L + buf.readUnsignedInt() * 1000)); - position.setLatitude(buf.readInt() * 0.0000001); - position.setLongitude(buf.readInt() * 0.0000001); - position.setAltitude(buf.readShort()); - position.setSpeed(UnitsConverter.knotsFromCps(buf.readUnsignedShort())); - - buf.readUnsignedByte(); // speed accuracy - - position.setCourse(buf.readUnsignedByte() * 2); - - position.set(Position.KEY_PDOP, buf.readUnsignedByte() * 0.1); - - position.setAccuracy(buf.readUnsignedByte()); - position.setValid(buf.readUnsignedByte() != 0); - - } else if (fieldId == 2) { - - int input = buf.readInt(); - int output = buf.readUnsignedShort(); - int status = buf.readUnsignedShort(); - - position.set(Position.KEY_IGNITION, BitUtil.check(input, 0)); - - position.set(Position.KEY_INPUT, input); - position.set(Position.KEY_OUTPUT, output); - position.set(Position.KEY_STATUS, status); - - } else if (fieldId == 6) { - - while (buf.readerIndex() < fieldEnd) { - switch (buf.readUnsignedByte()) { - case 1: - position.set(Position.KEY_BATTERY, buf.readUnsignedShort() * 0.001); - break; - case 2: - position.set(Position.KEY_POWER, buf.readUnsignedShort() * 0.01); - break; - case 3: - position.set(Position.KEY_DEVICE_TEMP, buf.readShort() * 0.01); - break; - case 4: - position.set(Position.KEY_RSSI, buf.readUnsignedShort()); - break; - case 5: - position.set("solarPower", buf.readUnsignedShort() * 0.001); - break; - default: - break; - } - } - - } - - buf.readerIndex(fieldEnd); - - } - - if (position.getFixTime() == null) { - getLastLocation(position, position.getDeviceTime()); - } - - positions.add(position); - - } + } else if (type == MSG_DATA_RECORD) { - return positions; + return decodeStandard(channel, remoteAddress, buf); } diff --git a/test/org/traccar/protocol/DmtProtocolDecoderTest.java b/test/org/traccar/protocol/DmtProtocolDecoderTest.java index a83dcafe2..2e386c48e 100644 --- a/test/org/traccar/protocol/DmtProtocolDecoderTest.java +++ b/test/org/traccar/protocol/DmtProtocolDecoderTest.java @@ -15,6 +15,12 @@ public class DmtProtocolDecoderTest extends ProtocolTest { verifyNull(decoder, binary(ByteOrder.LITTLE_ENDIAN, "0255003300001b00003335333232393032373533393235310038393931353030303030303030313330343539340000000403041910780603")); + verifyPositions(decoder, false, binary(ByteOrder.LITTLE_ENDIAN, + "02551040000eaca40d00d2b8e562c51f9912f39a6bee00007e420091090903070100000000008b1065360000000000007fd401c4fcf2feffffffffffffffffee0000003f1b")); + + verifyPositions(decoder, false, binary(ByteOrder.LITTLE_ENDIAN, + "02551080000eada40d00d2b8e58ac51f9912f39a6bee00007e42007e090709070000000000009010fc330000000000007fc201a0fc04ffffffffffffffffffe5000000c5d00eaea40d00d2b8e58ac51f9912f39a6bee00007e42007e09070207000000000000851008340000000000007fc201a0fc04ff0000000000000000e5000000c96d")); + verifyNull(decoder, binary(ByteOrder.LITTLE_ENDIAN, "025500310038f90100333533333233303831363639373330003839363130313835303031383234383434363330002202010900000000")); -- cgit v1.2.3 From b9fac418d0de3c472ff5603294d9ce3713c05291 Mon Sep 17 00:00:00 2001 From: Kevin Goos Date: Thu, 15 Mar 2018 10:42:15 +0100 Subject: Fixed ack message and added optional cell params --- .../protocol/LaipacSFKamelProtocolDecoder.java | 33 ++++++++++++++-------- 1 file changed, 21 insertions(+), 12 deletions(-) diff --git a/src/org/traccar/protocol/LaipacSFKamelProtocolDecoder.java b/src/org/traccar/protocol/LaipacSFKamelProtocolDecoder.java index decf279df..0966e83ff 100644 --- a/src/org/traccar/protocol/LaipacSFKamelProtocolDecoder.java +++ b/src/org/traccar/protocol/LaipacSFKamelProtocolDecoder.java @@ -55,6 +55,7 @@ public class LaipacSFKamelProtocolDecoder extends BaseProtocolDecoder { .expression("([0-9a-fA-F]{4}),") // Cell 1 - Cell ID Code .number("(d{3})") // Cell 2 - Country Code .number("(d{3})") // Cell 2 - Operator Code + .optional(4) .text("*") .number("(xx)") // checksum .compile(); @@ -102,27 +103,35 @@ public class LaipacSFKamelProtocolDecoder extends BaseProtocolDecoder { position.set(Position.KEY_GPS, parser.nextInt()); position.set(Position.KEY_ANALOG_1, parser.nextDouble() * 0.001); position.set(Position.KEY_ANALOG_2, parser.nextDouble() * 0.001); - position.set(Position.KEY_CELL_NET_CODE, parser.next()); - position.set(Position.KEY_CELL_ID_CODE, parser.next()); - position.set(Position.KEY_COUNTRY_CODE, parser.next()); - position.set(Position.KEY_OPERATOR, parser.next()); + String checksum = parser.next(); + if (parser.hasNext()) { + position.set(Position.KEY_CELL_NET_CODE, checksum); + position.set(Position.KEY_CELL_ID_CODE, parser.next()); + position.set(Position.KEY_COUNTRY_CODE, parser.next()); + position.set(Position.KEY_OPERATOR, parser.next()); + checksum = parser.next(); + } + + String result = sentence.replaceAll("^\\$(.*)\\*[0-9a-fA-F]{2}$", "$1"); + if (Integer.parseInt(checksum, 16) != Checksum.xor(result)) + { + return null; + } if (channel != null) { - if (Character.isLowerCase(status.charAt(0))) { + if (eventCode.equals("3")) { + channel.write("$AVCFG,00000000,d*31\r\n"); + } else if (eventCode.equals("X") || eventCode.equals("4")) { + channel.write("$AVCFG,00000000,x*2D\r\n"); + } else if (Character.isLowerCase(status.charAt(0))) { String response = "$EAVACK," + eventCode + "," + checksum; response += Checksum.nmea(response); + response += "\r\n"; channel.write(response); } - - if (eventCode.equals("3")) { - channel.write("$AVCFG,00000000,d*31"); - } else if (eventCode.equals("X") || eventCode.equals("4")) { - channel.write("$AVCFG,00000000,x*2D"); - } } return position; } - } -- cgit v1.2.3 From 411f12863460107187f1597b0bfd85627b1b413a Mon Sep 17 00:00:00 2001 From: Anton Tananaev Date: Fri, 16 Mar 2018 01:20:29 +1300 Subject: Update Gisgraphy test case --- test/org/traccar/geocoder/GeocoderTest.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/org/traccar/geocoder/GeocoderTest.java b/test/org/traccar/geocoder/GeocoderTest.java index 1ddc43ad2..8cf6ac524 100644 --- a/test/org/traccar/geocoder/GeocoderTest.java +++ b/test/org/traccar/geocoder/GeocoderTest.java @@ -83,9 +83,9 @@ public class GeocoderTest { public void onFailure(Throwable e) { } }); - assertEquals("Rue du Jardinet, Paris, FR", waitAddress()); + assertEquals("Rue du Jardinet, Paris, ÃŽle-de-France, FR", waitAddress()); - assertEquals("Rue du Jardinet, Paris, FR", geocoder.getAddress(48.8530000, 2.3400000, null)); + assertEquals("Rue du Jardinet, Paris, ÃŽle-de-France, FR", geocoder.getAddress(48.8530000, 2.3400000, null)); } public void testOpenCage() throws InterruptedException { -- cgit v1.2.3 From 0933f39774235641b1f4bdd1e1402b71bf9be9ed Mon Sep 17 00:00:00 2001 From: Anton Tananaev Date: Fri, 16 Mar 2018 01:24:41 +1300 Subject: Fix DM G50 coordinates decoding --- src/org/traccar/protocol/DmtProtocolDecoder.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/org/traccar/protocol/DmtProtocolDecoder.java b/src/org/traccar/protocol/DmtProtocolDecoder.java index 204d81820..30a6ae13a 100644 --- a/src/org/traccar/protocol/DmtProtocolDecoder.java +++ b/src/org/traccar/protocol/DmtProtocolDecoder.java @@ -90,8 +90,8 @@ public class DmtProtocolDecoder extends BaseProtocolDecoder { .setSecond((int) (time >> 26) & 0x3F) .getDate()); - position.setLatitude(buf.readInt() * 0.0000001); position.setLongitude(buf.readInt() * 0.0000001); + position.setLatitude(buf.readInt() * 0.0000001); position.setSpeed(UnitsConverter.knotsFromCps(buf.readUnsignedShort())); position.setCourse(buf.readUnsignedByte() * 2); position.setAltitude(buf.readShort()); -- cgit v1.2.3 From 9c13bf44cd83c3d2489b763a6c5af6d04d26ad48 Mon Sep 17 00:00:00 2001 From: Kevin Goos Date: Thu, 15 Mar 2018 16:24:33 +0100 Subject: Changed event to alarm codes, and added cfg for event code Z --- .../protocol/LaipacSFKamelProtocolDecoder.java | 34 ++++++++++++++++++++-- 1 file changed, 32 insertions(+), 2 deletions(-) diff --git a/src/org/traccar/protocol/LaipacSFKamelProtocolDecoder.java b/src/org/traccar/protocol/LaipacSFKamelProtocolDecoder.java index 0966e83ff..0d3995144 100644 --- a/src/org/traccar/protocol/LaipacSFKamelProtocolDecoder.java +++ b/src/org/traccar/protocol/LaipacSFKamelProtocolDecoder.java @@ -50,8 +50,8 @@ public class LaipacSFKamelProtocolDecoder extends BaseProtocolDecoder { .number("(d+),") // current mileage .number("(d),") // GPS on/off (1 = on, 0 = off) .number("(d+),") // Analog port 1 - .number("(d+),") // Analog port 2 - .expression("([0-9a-fA-F]{4})") // Cell 1 - Cell Net Code + .number("(d+)") // Analog port 2 + .expression(",([0-9a-fA-F]{4})") // Cell 1 - Cell Net Code .expression("([0-9a-fA-F]{4}),") // Cell 1 - Cell ID Code .number("(d{3})") // Cell 2 - Country Code .number("(d{3})") // Cell 2 - Operator Code @@ -87,6 +87,7 @@ public class LaipacSFKamelProtocolDecoder extends BaseProtocolDecoder { String status = parser.next(); position.setValid(status.toUpperCase().equals("A")); + position.set(Position.KEY_STATUS, status); position.setLatitude(parser.nextCoordinate()); position.setLongitude(parser.nextCoordinate()); @@ -97,6 +98,10 @@ public class LaipacSFKamelProtocolDecoder extends BaseProtocolDecoder { position.setTime(dateBuilder.getDate()); String eventCode = parser.next(); + String decodedAlarm = decodeAlarm(eventCode); + if (decodedAlarm != null) { + position.set(Position.KEY_ALARM, decodeAlarm(eventCode)); + } position.set(Position.KEY_EVENT, eventCode); position.set(Position.KEY_BATTERY, parser.nextDouble() * 0.001); position.set(Position.KEY_TOTAL_DISTANCE, parser.nextDouble()); @@ -124,6 +129,8 @@ public class LaipacSFKamelProtocolDecoder extends BaseProtocolDecoder { channel.write("$AVCFG,00000000,d*31\r\n"); } else if (eventCode.equals("X") || eventCode.equals("4")) { channel.write("$AVCFG,00000000,x*2D\r\n"); + } else if (eventCode.equals("Z")) { + channel.write("$AVCFG,00000000,z*2F\r\n"); } else if (Character.isLowerCase(status.charAt(0))) { String response = "$EAVACK," + eventCode + "," + checksum; response += Checksum.nmea(response); @@ -134,4 +141,27 @@ public class LaipacSFKamelProtocolDecoder extends BaseProtocolDecoder { return position; } + + private String decodeAlarm(String event) { + if (event.equals('Z')) { + return Position.ALARM_LOW_BATTERY; + } else if (event.equals('X')) { + return Position.ALARM_GEOFENCE_ENTER; + } else if (event.equals('T')) { + return Position.ALARM_TAMPERING; + } else if(event.equals("H")) { + return Position.ALARM_POWER_OFF; + } else if (event.equals('X')) { + return Position.ALARM_GEOFENCE_ENTER; + } else if (event.equals('8')) { + return Position.ALARM_SHOCK; + } else if (event.equals('7') && event.equals('4')) { + return Position.ALARM_GEOFENCE_EXIT; + } else if (event.equals('6')) { + return Position.ALARM_OVERSPEED; + } else if (event.equals('3')) { + return Position.ALARM_SOS; + } + return null; + } } -- cgit v1.2.3 From 34a5b8ceaff6617a8958ce04fb6c7f65d1d19979 Mon Sep 17 00:00:00 2001 From: Anton Tananaev Date: Fri, 16 Mar 2018 19:58:26 +1300 Subject: Fix Castel command encoding --- src/org/traccar/protocol/CastelProtocolEncoder.java | 4 ++-- test/org/traccar/protocol/CastelProtocolEncoderTest.java | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/org/traccar/protocol/CastelProtocolEncoder.java b/src/org/traccar/protocol/CastelProtocolEncoder.java index d69fc62cf..7ba33ca6d 100644 --- a/src/org/traccar/protocol/CastelProtocolEncoder.java +++ b/src/org/traccar/protocol/CastelProtocolEncoder.java @@ -28,7 +28,7 @@ import java.nio.charset.StandardCharsets; public class CastelProtocolEncoder extends BaseProtocolEncoder { - private ChannelBuffer encodeContent(long deviceId, int type, ChannelBuffer content) { + private ChannelBuffer encodeContent(long deviceId, short type, ChannelBuffer content) { ChannelBuffer buf = ChannelBuffers.dynamicBuffer(ByteOrder.LITTLE_ENDIAN, 0); String uniqueId = Context.getIdentityManager().getById(deviceId).getUniqueId(); @@ -42,7 +42,7 @@ public class CastelProtocolEncoder extends BaseProtocolEncoder { buf.writeBytes(uniqueId.getBytes(StandardCharsets.US_ASCII)); buf.writeZero(20 - uniqueId.length()); - buf.writeShort(type); + buf.writeShort(ChannelBuffers.swapShort(type)); buf.writeBytes(content); buf.writeShort(Checksum.crc16(Checksum.CRC16_X25, buf.toByteBuffer())); diff --git a/test/org/traccar/protocol/CastelProtocolEncoderTest.java b/test/org/traccar/protocol/CastelProtocolEncoderTest.java index 6e519363f..fc0a92c86 100644 --- a/test/org/traccar/protocol/CastelProtocolEncoderTest.java +++ b/test/org/traccar/protocol/CastelProtocolEncoderTest.java @@ -15,7 +15,7 @@ public class CastelProtocolEncoderTest extends ProtocolTest { command.setDeviceId(1); command.setType(Command.TYPE_ENGINE_STOP); - verifyCommand(encoder, command, binary("40401a00043132333435363738393031323334350000000000834501c7280d0a")); + verifyCommand(encoder, command, binary("40401a00043132333435363738393031323334350000000000458301fe6a0d0a")); } -- cgit v1.2.3 From 38c1ff9bccc1c417a0c2b412271aa7240a3b7db7 Mon Sep 17 00:00:00 2001 From: Anton Tananaev Date: Fri, 16 Mar 2018 22:57:59 +1300 Subject: Implement Continental RVS protocol --- setup/default.xml | 1 + src/org/traccar/protocol/ContinentalProtocol.java | 43 ++++++++++ .../protocol/ContinentalProtocolDecoder.java | 93 ++++++++++++++++++++++ .../protocol/ContinentalProtocolDecoderTest.java | 25 ++++++ 4 files changed, 162 insertions(+) create mode 100644 src/org/traccar/protocol/ContinentalProtocol.java create mode 100644 src/org/traccar/protocol/ContinentalProtocolDecoder.java create mode 100644 test/org/traccar/protocol/ContinentalProtocolDecoderTest.java diff --git a/setup/default.xml b/setup/default.xml index 8701ddc4c..68465011e 100644 --- a/setup/default.xml +++ b/setup/default.xml @@ -230,5 +230,6 @@ 5158 5159 5160 + 5161 diff --git a/src/org/traccar/protocol/ContinentalProtocol.java b/src/org/traccar/protocol/ContinentalProtocol.java new file mode 100644 index 000000000..e2b1226cf --- /dev/null +++ b/src/org/traccar/protocol/ContinentalProtocol.java @@ -0,0 +1,43 @@ +/* + * Copyright 2018 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 org.jboss.netty.bootstrap.ServerBootstrap; +import org.jboss.netty.channel.ChannelPipeline; +import org.jboss.netty.handler.codec.frame.LengthFieldBasedFrameDecoder; +import org.traccar.BaseProtocol; +import org.traccar.TrackerServer; + +import java.util.List; + +public class ContinentalProtocol extends BaseProtocol { + + public ContinentalProtocol() { + super("continental"); + } + + @Override + public void initTrackerServers(List serverList) { + serverList.add(new TrackerServer(new ServerBootstrap(), getName()) { + @Override + protected void addSpecificHandlers(ChannelPipeline pipeline) { + pipeline.addLast("frameDecoder", new LengthFieldBasedFrameDecoder(1024, 2, 2)); + pipeline.addLast("objectDecoder", new ContinentalProtocolDecoder(ContinentalProtocol.this)); + } + }); + } + +} diff --git a/src/org/traccar/protocol/ContinentalProtocolDecoder.java b/src/org/traccar/protocol/ContinentalProtocolDecoder.java new file mode 100644 index 000000000..2138eb39e --- /dev/null +++ b/src/org/traccar/protocol/ContinentalProtocolDecoder.java @@ -0,0 +1,93 @@ +/* + * Copyright 2018 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 org.jboss.netty.buffer.ChannelBuffer; +import org.jboss.netty.channel.Channel; +import org.traccar.BaseProtocolDecoder; +import org.traccar.DeviceSession; +import org.traccar.helper.UnitsConverter; +import org.traccar.model.Position; + +import java.net.SocketAddress; +import java.util.Date; + +public class ContinentalProtocolDecoder extends BaseProtocolDecoder { + + public ContinentalProtocolDecoder(ContinentalProtocol protocol) { + super(protocol); + } + + public static final int MSG_KEEPALIVE = 0x00; + public static final int MSG_STATUS = 0x02; + public static final int MSG_ACK = 0x06; + public static final int MSG_NACK = 0x15; + + @Override + protected Object decode( + Channel channel, SocketAddress remoteAddress, Object msg) throws Exception { + + ChannelBuffer buf = (ChannelBuffer) msg; + + buf.skipBytes(2); // header + buf.readUnsignedShort(); // length + buf.readUnsignedByte(); // software version + + DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, String.valueOf(buf.readUnsignedInt())); + if (deviceSession == null) { + return null; + } + + buf.readUnsignedByte(); // product + + int type = buf.readUnsignedByte(); + + if (type == MSG_STATUS) { + + Position position = new Position(getProtocolName()); + position.setDeviceId(deviceSession.getDeviceId()); + + position.setFixTime(new Date(buf.readUnsignedInt() * 1000L)); + + buf.readUnsignedByte(); + position.setLatitude(buf.readMedium() / 3600.0); + + buf.readUnsignedByte(); + position.setLongitude(buf.readMedium() / 3600.0); + + position.setCourse(buf.readUnsignedShort()); + position.setSpeed(UnitsConverter.knotsFromKph(buf.readUnsignedShort())); + + position.setValid(buf.readUnsignedByte() > 0); + + position.setDeviceTime(new Date(buf.readUnsignedInt() * 1000L)); + + position.set(Position.KEY_EVENT, buf.readUnsignedShort()); + position.set(Position.KEY_INPUT, buf.readUnsignedShort()); + position.set(Position.KEY_OUTPUT, buf.readUnsignedShort()); + position.set(Position.KEY_BATTERY, buf.readUnsignedByte()); + position.set(Position.KEY_DEVICE_TEMP, buf.readByte()); + + buf.readUnsignedShort(); // reserved + + return position; + + } + + return null; + } + +} diff --git a/test/org/traccar/protocol/ContinentalProtocolDecoderTest.java b/test/org/traccar/protocol/ContinentalProtocolDecoderTest.java new file mode 100644 index 000000000..fbc7c3219 --- /dev/null +++ b/test/org/traccar/protocol/ContinentalProtocolDecoderTest.java @@ -0,0 +1,25 @@ +package org.traccar.protocol; + +import org.junit.Test; +import org.traccar.ProtocolTest; + +public class ContinentalProtocolDecoderTest extends ProtocolTest { + + @Test + public void testDecode() throws Exception { + + ContinentalProtocolDecoder decoder = new ContinentalProtocolDecoder(new ContinentalProtocol()); + + verifyPosition(decoder, binary( + "5356002A1100003039030243A68B5700FEB5AB00FD715F012700000143A68B57000E000000000C2F00000130"), + position("2005-12-19 10:28:39.000", true, -23.49027, -46.55138)); + + verifyPosition(decoder, binary( + "5356002a0d0010a12403025a9ea47f00feb48400fd6e63000c0000015a9ea480000e000100000c000000")); + + verifyPosition(decoder, binary( + "5356002a0d0010a1240302581b944100febed800fd9fa30139001300581c73fa000e000000000d000001")); + + } + +} -- cgit v1.2.3 From 901c331745c8aed5593ca80f3faff5ee4d06d2a8 Mon Sep 17 00:00:00 2001 From: Kevin Goos Date: Fri, 16 Mar 2018 12:13:02 +0100 Subject: Added null check --- src/org/traccar/protocol/LaipacSFKamelProtocolDecoder.java | 3 ++- test/org/traccar/protocol/LaipacSFKamelProtocolDecoderTest.java | 6 +++--- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/src/org/traccar/protocol/LaipacSFKamelProtocolDecoder.java b/src/org/traccar/protocol/LaipacSFKamelProtocolDecoder.java index 0d3995144..dc169c668 100644 --- a/src/org/traccar/protocol/LaipacSFKamelProtocolDecoder.java +++ b/src/org/traccar/protocol/LaipacSFKamelProtocolDecoder.java @@ -118,8 +118,9 @@ public class LaipacSFKamelProtocolDecoder extends BaseProtocolDecoder { checksum = parser.next(); } + String result = sentence.replaceAll("^\\$(.*)\\*[0-9a-fA-F]{2}$", "$1"); - if (Integer.parseInt(checksum, 16) != Checksum.xor(result)) + if (checksum == null || Integer.parseInt(checksum, 16) != Checksum.xor(result)) { return null; } diff --git a/test/org/traccar/protocol/LaipacSFKamelProtocolDecoderTest.java b/test/org/traccar/protocol/LaipacSFKamelProtocolDecoderTest.java index 05de5dcbe..69c4243ab 100644 --- a/test/org/traccar/protocol/LaipacSFKamelProtocolDecoderTest.java +++ b/test/org/traccar/protocol/LaipacSFKamelProtocolDecoderTest.java @@ -2,6 +2,7 @@ package org.traccar.protocol; import org.junit.Test; import org.traccar.ProtocolTest; +import org.traccar.helper.Checksum; public class LaipacSFKamelProtocolDecoderTest extends ProtocolTest { @@ -11,7 +12,7 @@ public class LaipacSFKamelProtocolDecoderTest extends ProtocolTest { LaipacSFKamelProtocolDecoder decoder = new LaipacSFKamelProtocolDecoder(new LaipacSFKamelProtocol()); verifyPosition(decoder, text( - "$AVRMC,358174067149865,084514,r,5050.1314,N,00419.9719,E,0.68,306.39,120318,0,3882,84,1,0,0,3EE4A617,020610*4E")); + "$AVRMC,999999999999999,084514,r,5050.1314,N,00419.9719,E,0.68,306.39,120318,0,3882,84,1,0,0,3EE4A617,020610*4D")); verifyNull(decoder, text( "$AVSYS,99999999,V1.50,SN0000103,32768*15")); @@ -29,7 +30,6 @@ public class LaipacSFKamelProtocolDecoderTest extends ProtocolTest { "$ECHK,MSG00002,0*5E")); verifyPosition(decoder, text( - "$AVRMC,358174067149865,111602,r,5050.1262,N,00419.9660,E,0.00,0.00,120318,0,3843,95,1,0,0,3EE4A617,020610*44")); + "$AVRMC,999999999999999,111602,r,5050.1262,N,00419.9660,E,0.00,0.00,120318,0,3843,95,1,0,0,3EE4A617,020610*47")); } - } -- cgit v1.2.3 From 1216e3bee2abfecedfe1250a9dd5d45c83a2fc02 Mon Sep 17 00:00:00 2001 From: Kevin Goos Date: Fri, 16 Mar 2018 13:18:18 +0100 Subject: Changed port and cleanup project --- setup/default.xml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/setup/default.xml b/setup/default.xml index 515abf61d..626dd7e7e 100644 --- a/setup/default.xml +++ b/setup/default.xml @@ -116,8 +116,7 @@ 5045 5046 5047 - - 5048 + 5048 5049 5050 5051 @@ -229,5 +228,6 @@ 5157 5158 5159 + 5160 -- cgit v1.2.3 From b34cf25b5ac1f178d783a512d3b95faed4bdb010 Mon Sep 17 00:00:00 2001 From: Kevin Goos Date: Fri, 16 Mar 2018 13:31:18 +0100 Subject: Fixed style issues --- src/org/traccar/protocol/LaipacSFKamelProtocolDecoder.java | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/org/traccar/protocol/LaipacSFKamelProtocolDecoder.java b/src/org/traccar/protocol/LaipacSFKamelProtocolDecoder.java index dc169c668..60a6b2468 100644 --- a/src/org/traccar/protocol/LaipacSFKamelProtocolDecoder.java +++ b/src/org/traccar/protocol/LaipacSFKamelProtocolDecoder.java @@ -120,8 +120,7 @@ public class LaipacSFKamelProtocolDecoder extends BaseProtocolDecoder { String result = sentence.replaceAll("^\\$(.*)\\*[0-9a-fA-F]{2}$", "$1"); - if (checksum == null || Integer.parseInt(checksum, 16) != Checksum.xor(result)) - { + if (checksum == null || Integer.parseInt(checksum, 16) != Checksum.xor(result)) { return null; } @@ -150,7 +149,7 @@ public class LaipacSFKamelProtocolDecoder extends BaseProtocolDecoder { return Position.ALARM_GEOFENCE_ENTER; } else if (event.equals('T')) { return Position.ALARM_TAMPERING; - } else if(event.equals("H")) { + } else if (event.equals("H")) { return Position.ALARM_POWER_OFF; } else if (event.equals('X')) { return Position.ALARM_GEOFENCE_ENTER; -- cgit v1.2.3 From 4f08521052188bc0de0be2f1a62f2bc07f70c8a4 Mon Sep 17 00:00:00 2001 From: Kevin Goos Date: Fri, 16 Mar 2018 15:33:46 +0100 Subject: Fixed build error on char array --- src/org/traccar/helper/DataConverter.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/org/traccar/helper/DataConverter.java b/src/org/traccar/helper/DataConverter.java index 7abd4ae93..0df0219ad 100644 --- a/src/org/traccar/helper/DataConverter.java +++ b/src/org/traccar/helper/DataConverter.java @@ -26,7 +26,7 @@ public final class DataConverter { public static byte[] parseHex(String string) { try { - return Hex.decodeHex(string); + return Hex.decodeHex(string.toCharArray()); } catch (DecoderException e) { throw new RuntimeException(e); } -- cgit v1.2.3 From 5c73dbe4b4af5176c25ddabb7788833f67b22500 Mon Sep 17 00:00:00 2001 From: Kevin Goos Date: Mon, 19 Mar 2018 10:02:43 +0100 Subject: Cleanup of repo for merge --- src/org/traccar/helper/DataConverter.java | 2 +- tools/translate.py | 35 ------------------------------- 2 files changed, 1 insertion(+), 36 deletions(-) delete mode 100644 tools/translate.py diff --git a/src/org/traccar/helper/DataConverter.java b/src/org/traccar/helper/DataConverter.java index 0df0219ad..7abd4ae93 100644 --- a/src/org/traccar/helper/DataConverter.java +++ b/src/org/traccar/helper/DataConverter.java @@ -26,7 +26,7 @@ public final class DataConverter { public static byte[] parseHex(String string) { try { - return Hex.decodeHex(string.toCharArray()); + return Hex.decodeHex(string); } catch (DecoderException e) { throw new RuntimeException(e); } diff --git a/tools/translate.py b/tools/translate.py deleted file mode 100644 index e8324a61a..000000000 --- a/tools/translate.py +++ /dev/null @@ -1,35 +0,0 @@ -#!/usr/bin/python - -import os -import optparse -import urllib2 -import json -import base64 - -parser = optparse.OptionParser() -parser.add_option("-u", "--user", dest="username", help="transifex user login") -parser.add_option("-p", "--password", dest="password", help="transifex user password") - -(options, args) = parser.parse_args() - -if not options.username or not options.password: - parser.error('User name and password are required') - -os.chdir(os.path.dirname(os.path.abspath(__file__))) - -path = "../web/l10n/" - -def request(url): - req = urllib2.Request(url) - auth = base64.encodestring("%s:%s" % (options.username, options.password)).replace("\n", "") - req.add_header("Authorization", "Basic %s" % auth) - return urllib2.urlopen(req) - -resource = json.load(request("https://www.transifex.com/api/2/project/traccar/resource/web/?details")) - -for language in resource["available_languages"]: - code = language["code"] - data = request("https://www.transifex.com/api/2/project/traccar/resource/web/translation/" + code + "?file") - file = open(path + code + ".json", "wb") - file.write(data.read()) - file.close() -- cgit v1.2.3 From 97f42f8ecd5f8c9792354c5253273b56d12c0282 Mon Sep 17 00:00:00 2001 From: Kevin Goos Date: Mon, 19 Mar 2018 10:32:28 +0100 Subject: Fixed not usefull change --- src/org/traccar/protocol/OsmAndProtocolDecoder.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/org/traccar/protocol/OsmAndProtocolDecoder.java b/src/org/traccar/protocol/OsmAndProtocolDecoder.java index 014ebb233..a9e411978 100644 --- a/src/org/traccar/protocol/OsmAndProtocolDecoder.java +++ b/src/org/traccar/protocol/OsmAndProtocolDecoder.java @@ -42,7 +42,8 @@ public class OsmAndProtocolDecoder extends BaseHttpProtocolDecoder { } @Override - protected Object decode(Channel channel, SocketAddress remoteAddress, Object msg) throws Exception { + protected Object decode( + Channel channel, SocketAddress remoteAddress, Object msg) throws Exception { HttpRequest request = (HttpRequest) msg; QueryStringDecoder decoder = new QueryStringDecoder(request.getUri()); -- cgit v1.2.3 From 7297c042cc1df171b4ca8959b46001d898c2373c Mon Sep 17 00:00:00 2001 From: Kevin Goos Date: Mon, 19 Mar 2018 10:41:35 +0100 Subject: Fixed analog keys --- src/org/traccar/model/Position.java | 2 -- src/org/traccar/protocol/LaipacSFKamelProtocolDecoder.java | 7 ++++--- src/org/traccar/protocol/OsmAndProtocolDecoder.java | 2 +- 3 files changed, 5 insertions(+), 6 deletions(-) diff --git a/src/org/traccar/model/Position.java b/src/org/traccar/model/Position.java index 0e8bd9a6e..65bd03de6 100644 --- a/src/org/traccar/model/Position.java +++ b/src/org/traccar/model/Position.java @@ -52,8 +52,6 @@ public class Position extends Message { public static final String KEY_BATTERY_LEVEL = "batteryLevel"; // percentage public static final String KEY_FUEL_LEVEL = "fuel"; // liters public static final String KEY_FUEL_CONSUMPTION = "fuelConsumption"; // liters/hour - public static final String KEY_ANALOG_1 = "analog 1"; // volts - public static final String KEY_ANALOG_2 = "analog 2"; // volts public static final String KEY_VERSION_FW = "versionFw"; public static final String KEY_VERSION_HW = "versionHw"; diff --git a/src/org/traccar/protocol/LaipacSFKamelProtocolDecoder.java b/src/org/traccar/protocol/LaipacSFKamelProtocolDecoder.java index 60a6b2468..925b032a9 100644 --- a/src/org/traccar/protocol/LaipacSFKamelProtocolDecoder.java +++ b/src/org/traccar/protocol/LaipacSFKamelProtocolDecoder.java @@ -61,7 +61,8 @@ public class LaipacSFKamelProtocolDecoder extends BaseProtocolDecoder { .compile(); @Override - protected Object decode(Channel channel, SocketAddress remoteAddress, Object msg) throws Exception { + protected Object decode( + Channel channel, SocketAddress remoteAddress, Object msg) throws Exception { String sentence = (String) msg; if (sentence.startsWith("$ECHK") && channel != null) { @@ -106,8 +107,8 @@ public class LaipacSFKamelProtocolDecoder extends BaseProtocolDecoder { position.set(Position.KEY_BATTERY, parser.nextDouble() * 0.001); position.set(Position.KEY_TOTAL_DISTANCE, parser.nextDouble()); position.set(Position.KEY_GPS, parser.nextInt()); - position.set(Position.KEY_ANALOG_1, parser.nextDouble() * 0.001); - position.set(Position.KEY_ANALOG_2, parser.nextDouble() * 0.001); + position.set(Position.PREFIX_ADC + 1, parser.nextDouble() * 0.001); + position.set(Position.PREFIX_ADC + 2, parser.nextDouble() * 0.001); String checksum = parser.next(); if (parser.hasNext()) { diff --git a/src/org/traccar/protocol/OsmAndProtocolDecoder.java b/src/org/traccar/protocol/OsmAndProtocolDecoder.java index a9e411978..03abdd588 100644 --- a/src/org/traccar/protocol/OsmAndProtocolDecoder.java +++ b/src/org/traccar/protocol/OsmAndProtocolDecoder.java @@ -43,7 +43,7 @@ public class OsmAndProtocolDecoder extends BaseHttpProtocolDecoder { @Override protected Object decode( - Channel channel, SocketAddress remoteAddress, Object msg) throws Exception { + Channel channel, SocketAddress remoteAddress, Object msg) throws Exception { HttpRequest request = (HttpRequest) msg; QueryStringDecoder decoder = new QueryStringDecoder(request.getUri()); -- cgit v1.2.3 From 694b5ce8dd817e3f1c9bb94a43e9f52cdc4f6845 Mon Sep 17 00:00:00 2001 From: Kevin Goos Date: Mon, 19 Mar 2018 16:04:38 +0100 Subject: Merged laipac and laipac kamel protocol to one --- src/org/traccar/helper/DataConverter.java | 2 +- .../traccar/protocol/LaipacProtocolDecoder.java | 116 +++++++++++--- .../traccar/protocol/LaipacSFKamelProtocol.java | 47 ------ .../protocol/LaipacSFKamelProtocolDecoder.java | 168 --------------------- .../protocol/LaipacProtocolDecoderTest.java | 28 +++- .../protocol/LaipacSFKamelProtocolDecoderTest.java | 35 ----- 6 files changed, 118 insertions(+), 278 deletions(-) delete mode 100644 src/org/traccar/protocol/LaipacSFKamelProtocol.java delete mode 100644 src/org/traccar/protocol/LaipacSFKamelProtocolDecoder.java delete mode 100644 test/org/traccar/protocol/LaipacSFKamelProtocolDecoderTest.java diff --git a/src/org/traccar/helper/DataConverter.java b/src/org/traccar/helper/DataConverter.java index 7abd4ae93..0df0219ad 100644 --- a/src/org/traccar/helper/DataConverter.java +++ b/src/org/traccar/helper/DataConverter.java @@ -26,7 +26,7 @@ public final class DataConverter { public static byte[] parseHex(String string) { try { - return Hex.decodeHex(string); + return Hex.decodeHex(string.toCharArray()); } catch (DecoderException e) { throw new RuntimeException(e); } diff --git a/src/org/traccar/protocol/LaipacProtocolDecoder.java b/src/org/traccar/protocol/LaipacProtocolDecoder.java index 99189d012..dc7c394e4 100644 --- a/src/org/traccar/protocol/LaipacProtocolDecoder.java +++ b/src/org/traccar/protocol/LaipacProtocolDecoder.java @@ -35,25 +35,34 @@ public class LaipacProtocolDecoder extends BaseProtocolDecoder { private static final Pattern PATTERN = new PatternBuilder() .text("$AVRMC,") - .expression("([^,]+),") // identifier - .number("(dd)(dd)(dd),") // time (hhmmss) - .expression("([AVRPavrp]),") // validity - .number("(dd)(dd.d+),") // latitude + .expression("([^,]+),") // identifier + .number("(dd)(dd)(dd),") // time (hhmmss) + .expression("([AVRPavrp]),") // validity + .number("(dd)(dd.d+),") // latitude .expression("([NS]),") - .number("(ddd)(dd.d+),") // longitude + .number("(ddd)(dd.d+),") // longitude .number("([EW]),") - .number("(d+.d+),") // speed - .number("(d+.d+),") // course - .number("(dd)(dd)(dd),") // date (ddmmyy) - .expression("(.),") // type - .expression("[^*]+").text("*") - .number("(xx)") // checksum + .number("(d+.d+),") // speed + .number("(d+.d+),") // course + .number("(dd)(dd)(dd),") // date (ddmmyy) + .expression("([abZXTSMHFE86430]),") // event code + .number("(d+)").expression("(\\.?)").number("(d*),") // battery voltage + .number("(d+),") // current mileage + .number("(d),") // GPS on/off (1 = on, 0 = off) + .number("(d+),") // Analog port 1 + .number("(d+)") // Analog port 2 + .expression(",([0-9a-fA-F]{4})") // Cell 1 - Cell Net Code + .expression("([0-9a-fA-F]{4}),") // Cell 1 - Cell ID Code + .number("(d{3})") // Cell 2 - Country Code + .number("(d{3})") // Cell 2 - Operator Code + .optional(4) + .text("*") + .number("(xx)") // checksum .compile(); @Override protected Object decode( Channel channel, SocketAddress remoteAddress, Object msg) throws Exception { - String sentence = (String) msg; if (sentence.startsWith("$ECHK") && channel != null) { @@ -79,6 +88,7 @@ public class LaipacProtocolDecoder extends BaseProtocolDecoder { String status = parser.next(); position.setValid(status.toUpperCase().equals("A")); + position.set(Position.KEY_STATUS, status); position.setLatitude(parser.nextCoordinate()); position.setLongitude(parser.nextCoordinate()); @@ -88,28 +98,84 @@ public class LaipacProtocolDecoder extends BaseProtocolDecoder { dateBuilder.setDateReverse(parser.nextInt(0), parser.nextInt(0), parser.nextInt(0)); position.setTime(dateBuilder.getDate()); - String type = parser.next(); + String eventCode = parser.next(); + String decodedAlarm = decodeAlarm(eventCode); + if (decodedAlarm != null) { + position.set(Position.KEY_ALARM, decodeAlarm(eventCode)); + } + position.set(Position.KEY_EVENT, eventCode); + + double batteryVoltage = parser.nextDouble(); + if (parser.next().isEmpty()) { + parser.next(); + batteryVoltage *= 0.001; + } else { + batteryVoltage += parser.nextDouble() * 0.001; + } + position.set(Position.KEY_BATTERY, batteryVoltage); + + position.set(Position.KEY_TOTAL_DISTANCE, parser.nextDouble()); + position.set(Position.KEY_GPS, parser.nextInt()); + position.set(Position.PREFIX_ADC + 1, parser.nextDouble() * 0.001); + position.set(Position.PREFIX_ADC + 2, parser.nextDouble() * 0.001); + + setNextValue(parser, position, Position.KEY_CELL_NET_CODE); + setNextValue(parser, position, Position.KEY_CELL_ID_CODE); + setNextValue(parser, position, Position.KEY_COUNTRY_CODE); + setNextValue(parser, position, Position.KEY_OPERATOR); + String checksum = parser.next(); + String result = sentence.replaceAll("^\\$(.*)\\*[0-9a-fA-F]{2}$", "$1"); + if (checksum == null || Integer.parseInt(checksum, 16) != Checksum.xor(result)) { + return null; + } if (channel != null) { - - if (Character.isLowerCase(status.charAt(0))) { - String response = "$EAVACK," + type + "," + checksum; + if (eventCode.equals("3")) { + channel.write("$AVCFG,00000000,d*31\r\n"); + } else if (eventCode.equals("X") || eventCode.equals("4")) { + channel.write("$AVCFG,00000000,x*2D\r\n"); + } else if (eventCode.equals("Z")) { + channel.write("$AVCFG,00000000,z*2F\r\n"); + } else if (Character.isLowerCase(status.charAt(0))) { + String response = "$EAVACK," + eventCode + "," + checksum; response += Checksum.nmea(response); + response += "\r\n"; channel.write(response); } - - if (type.equals("S") || type.equals("T")) { - channel.write("$AVCFG,00000000,t*21"); - } else if (type.equals("3")) { - channel.write("$AVCFG,00000000,d*31"); - } else if (type.equals("X") || type.equals("4")) { - channel.write("$AVCFG,00000000,x*2D"); - } - } return position; } + private void setNextValue(Parser parser, Position position, String key) + { + String value = parser.next(); + if (value != null) { + position.set(key, value); + } + } + + private String decodeAlarm(String event) { + if (event.equals('Z')) { + return Position.ALARM_LOW_BATTERY; + } else if (event.equals('X')) { + return Position.ALARM_GEOFENCE_ENTER; + } else if (event.equals('T')) { + return Position.ALARM_TAMPERING; + } else if (event.equals("H")) { + return Position.ALARM_POWER_OFF; + } else if (event.equals('X')) { + return Position.ALARM_GEOFENCE_ENTER; + } else if (event.equals('8')) { + return Position.ALARM_SHOCK; + } else if (event.equals('7') && event.equals('4')) { + return Position.ALARM_GEOFENCE_EXIT; + } else if (event.equals('6')) { + return Position.ALARM_OVERSPEED; + } else if (event.equals('3')) { + return Position.ALARM_SOS; + } + return null; + } } diff --git a/src/org/traccar/protocol/LaipacSFKamelProtocol.java b/src/org/traccar/protocol/LaipacSFKamelProtocol.java deleted file mode 100644 index 5e1beabbd..000000000 --- a/src/org/traccar/protocol/LaipacSFKamelProtocol.java +++ /dev/null @@ -1,47 +0,0 @@ -/* - * Copyright 2015 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 org.jboss.netty.bootstrap.ServerBootstrap; -import org.jboss.netty.channel.ChannelPipeline; -import org.jboss.netty.handler.codec.frame.LineBasedFrameDecoder; -import org.jboss.netty.handler.codec.string.StringDecoder; -import org.jboss.netty.handler.codec.string.StringEncoder; -import org.traccar.BaseProtocol; -import org.traccar.TrackerServer; - -import java.util.List; - -public class LaipacSFKamelProtocol extends BaseProtocol { - - public LaipacSFKamelProtocol() { - super("laipacsfkamel"); - } - - @Override - public void initTrackerServers(List serverList) { - serverList.add(new TrackerServer(new ServerBootstrap(), getName()) { - @Override - protected void addSpecificHandlers(ChannelPipeline pipeline) { - pipeline.addLast("frameDecoder", new LineBasedFrameDecoder(1024)); - pipeline.addLast("stringEncoder", new StringEncoder()); - pipeline.addLast("stringDecoder", new StringDecoder()); - pipeline.addLast("objectDecoder", new LaipacSFKamelProtocolDecoder(LaipacSFKamelProtocol.this)); - } - }); - } - -} diff --git a/src/org/traccar/protocol/LaipacSFKamelProtocolDecoder.java b/src/org/traccar/protocol/LaipacSFKamelProtocolDecoder.java deleted file mode 100644 index 925b032a9..000000000 --- a/src/org/traccar/protocol/LaipacSFKamelProtocolDecoder.java +++ /dev/null @@ -1,168 +0,0 @@ -/* - * Copyright 2013 - 2017 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 org.jboss.netty.channel.Channel; -import org.traccar.BaseProtocolDecoder; -import org.traccar.DeviceSession; -import org.traccar.helper.Checksum; -import org.traccar.helper.DateBuilder; -import org.traccar.helper.Parser; -import org.traccar.helper.PatternBuilder; -import org.traccar.model.Position; - -import java.net.SocketAddress; -import java.util.regex.Pattern; - -public class LaipacSFKamelProtocolDecoder extends BaseProtocolDecoder { - - public LaipacSFKamelProtocolDecoder(LaipacSFKamelProtocol protocol) { - super(protocol); - } - - private static final Pattern PATTERN = new PatternBuilder() - .text("$AVRMC,") - .expression("([^,]+),") // identifier - .number("(dd)(dd)(dd),") // time (hhmmss) - .expression("([AVRPavrp]),") // validity - .number("(dd)(dd.d+),") // latitude - .expression("([NS]),") - .number("(ddd)(dd.d+),") // longitude - .number("([EW]),") - .number("(d+.d+),") // speed - .number("(d+.d+),") // course - .number("(dd)(dd)(dd),") // date (ddmmyy) - .expression("([abZXMHE86430]),") // event code - .number("(d+),") // battery voltage - .number("(d+),") // current mileage - .number("(d),") // GPS on/off (1 = on, 0 = off) - .number("(d+),") // Analog port 1 - .number("(d+)") // Analog port 2 - .expression(",([0-9a-fA-F]{4})") // Cell 1 - Cell Net Code - .expression("([0-9a-fA-F]{4}),") // Cell 1 - Cell ID Code - .number("(d{3})") // Cell 2 - Country Code - .number("(d{3})") // Cell 2 - Operator Code - .optional(4) - .text("*") - .number("(xx)") // checksum - .compile(); - - @Override - protected Object decode( - Channel channel, SocketAddress remoteAddress, Object msg) throws Exception { - String sentence = (String) msg; - - if (sentence.startsWith("$ECHK") && channel != null) { - channel.write(sentence + "\r\n"); // heartbeat - return null; - } - - Parser parser = new Parser(PATTERN, 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()); - - DateBuilder dateBuilder = new DateBuilder() - .setTime(parser.nextInt(0), parser.nextInt(0), parser.nextInt(0)); - - String status = parser.next(); - position.setValid(status.toUpperCase().equals("A")); - position.set(Position.KEY_STATUS, status); - - position.setLatitude(parser.nextCoordinate()); - position.setLongitude(parser.nextCoordinate()); - position.setSpeed(parser.nextDouble(0)); - position.setCourse(parser.nextDouble(0)); - - dateBuilder.setDateReverse(parser.nextInt(0), parser.nextInt(0), parser.nextInt(0)); - position.setTime(dateBuilder.getDate()); - - String eventCode = parser.next(); - String decodedAlarm = decodeAlarm(eventCode); - if (decodedAlarm != null) { - position.set(Position.KEY_ALARM, decodeAlarm(eventCode)); - } - position.set(Position.KEY_EVENT, eventCode); - position.set(Position.KEY_BATTERY, parser.nextDouble() * 0.001); - position.set(Position.KEY_TOTAL_DISTANCE, parser.nextDouble()); - position.set(Position.KEY_GPS, parser.nextInt()); - position.set(Position.PREFIX_ADC + 1, parser.nextDouble() * 0.001); - position.set(Position.PREFIX_ADC + 2, parser.nextDouble() * 0.001); - - String checksum = parser.next(); - if (parser.hasNext()) { - position.set(Position.KEY_CELL_NET_CODE, checksum); - position.set(Position.KEY_CELL_ID_CODE, parser.next()); - position.set(Position.KEY_COUNTRY_CODE, parser.next()); - position.set(Position.KEY_OPERATOR, parser.next()); - checksum = parser.next(); - } - - - String result = sentence.replaceAll("^\\$(.*)\\*[0-9a-fA-F]{2}$", "$1"); - if (checksum == null || Integer.parseInt(checksum, 16) != Checksum.xor(result)) { - return null; - } - - if (channel != null) { - if (eventCode.equals("3")) { - channel.write("$AVCFG,00000000,d*31\r\n"); - } else if (eventCode.equals("X") || eventCode.equals("4")) { - channel.write("$AVCFG,00000000,x*2D\r\n"); - } else if (eventCode.equals("Z")) { - channel.write("$AVCFG,00000000,z*2F\r\n"); - } else if (Character.isLowerCase(status.charAt(0))) { - String response = "$EAVACK," + eventCode + "," + checksum; - response += Checksum.nmea(response); - response += "\r\n"; - channel.write(response); - } - } - - return position; - } - - private String decodeAlarm(String event) { - if (event.equals('Z')) { - return Position.ALARM_LOW_BATTERY; - } else if (event.equals('X')) { - return Position.ALARM_GEOFENCE_ENTER; - } else if (event.equals('T')) { - return Position.ALARM_TAMPERING; - } else if (event.equals("H")) { - return Position.ALARM_POWER_OFF; - } else if (event.equals('X')) { - return Position.ALARM_GEOFENCE_ENTER; - } else if (event.equals('8')) { - return Position.ALARM_SHOCK; - } else if (event.equals('7') && event.equals('4')) { - return Position.ALARM_GEOFENCE_EXIT; - } else if (event.equals('6')) { - return Position.ALARM_OVERSPEED; - } else if (event.equals('3')) { - return Position.ALARM_SOS; - } - return null; - } -} diff --git a/test/org/traccar/protocol/LaipacProtocolDecoderTest.java b/test/org/traccar/protocol/LaipacProtocolDecoderTest.java index 787f33e65..c8a23a5a7 100644 --- a/test/org/traccar/protocol/LaipacProtocolDecoderTest.java +++ b/test/org/traccar/protocol/LaipacProtocolDecoderTest.java @@ -28,8 +28,7 @@ public class LaipacProtocolDecoderTest extends ProtocolTest { verifyNull(decoder, text( "$ECHK,MSG00002,0*5E")); - verifyPosition(decoder, text( - "$AVRMC,99999999,164339,A,4351.0542,N,07923.5445,W,0.29,78.66,180703,0,3.727,17,1,0,0*37"), + verifyPosition(decoder, text("$AVRMC,99999999,164339,A,4351.0542,N,07923.5445,W,0.29,78.66,180703,0,3.727,17,1,0,0*37"), position("2003-07-18 16:43:39.000", true, 43.85090, -79.39241)); verifyPosition(decoder, text( @@ -95,6 +94,31 @@ public class LaipacProtocolDecoderTest extends ProtocolTest { verifyPosition(decoder, text( "$AVRMC,96414215,170046,p,4310.7965,N,07652.0816,E,0.00,0.00,071016,0,4069,98,1,0,0*04")); + verifyPosition(decoder, text( + "$AVRMC,999999999999999,111602,r,5050.1262,N,00419.9660,E,0.00,0.00,120318,0,3843,95,1,0,0,3EE4A617,020610*47")); + + verifyPosition(decoder, text( + "$AVRMC,358174067149865,143456,R,5050.1285,N,00420.0620,E,0.00,309.27,190318,0,3455,119,1,0,0,3EE4A617,020610*54")); + + verifyPosition(decoder, text( + "$AVRMC,999999999999999,084514,r,5050.1314,N,00419.9719,E,0.68,306.39,120318,0,3882,84,1,0,0,3EE4A617,020610*4D")); + + //Alarm button + verifyPosition(decoder, text( + "$AVRMC,358174067149865,142945,R,5050.1254,N,00420.0490,E,0.00,0.00,190318,3,3455,119,1,0,0,3EE4A617,020610*53")); + + //G-Sensor + verifyPosition(decoder, text( + "$AVRMC,358174067149865,143407,R,5050.1254,N,00420.0490,E,0.00,0.00,190318,8,3455,119,1,0,0,3EE4A617,020610*52")); + + //Powered off + verifyPosition(decoder, text( + "$AVRMC,358174067149865,143648,A,5050.1141,N,00420.0525,E,1.24,174.38,190318,H,3455,119,1,0,0,3EE4A617,020610*3E")); + + //No network + verifyPosition(decoder, text( + "$AVRMC,358174067149865,143747,R,5050.1124,N,00420.0542,E,1.34,161.96,190318,a,3416,119,1,0,0*7D")); + } } diff --git a/test/org/traccar/protocol/LaipacSFKamelProtocolDecoderTest.java b/test/org/traccar/protocol/LaipacSFKamelProtocolDecoderTest.java deleted file mode 100644 index 69c4243ab..000000000 --- a/test/org/traccar/protocol/LaipacSFKamelProtocolDecoderTest.java +++ /dev/null @@ -1,35 +0,0 @@ -package org.traccar.protocol; - -import org.junit.Test; -import org.traccar.ProtocolTest; -import org.traccar.helper.Checksum; - -public class LaipacSFKamelProtocolDecoderTest extends ProtocolTest { - - @Test - public void testDecode() throws Exception { - - LaipacSFKamelProtocolDecoder decoder = new LaipacSFKamelProtocolDecoder(new LaipacSFKamelProtocol()); - - verifyPosition(decoder, text( - "$AVRMC,999999999999999,084514,r,5050.1314,N,00419.9719,E,0.68,306.39,120318,0,3882,84,1,0,0,3EE4A617,020610*4D")); - - verifyNull(decoder, text( - "$AVSYS,99999999,V1.50,SN0000103,32768*15")); - - verifyNull(decoder, text( - "$ECHK,99999999,0*35")); - - verifyNull(decoder, text( - "$AVSYS,MSG00002,14406,7046811160,64*1A")); - - verifyNull(decoder, text( - "$EAVSYS,MSG00002,8931086013104404999,,Owner,0x52014406*76")); - - verifyNull(decoder, text( - "$ECHK,MSG00002,0*5E")); - - verifyPosition(decoder, text( - "$AVRMC,999999999999999,111602,r,5050.1262,N,00419.9660,E,0.00,0.00,120318,0,3843,95,1,0,0,3EE4A617,020610*47")); - } -} -- cgit v1.2.3 From 04dc588d2fac50a864542b63a29cd10f19d72345 Mon Sep 17 00:00:00 2001 From: Kevin Goos Date: Mon, 19 Mar 2018 16:08:10 +0100 Subject: Fixed build errors --- src/org/traccar/protocol/LaipacProtocolDecoder.java | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/org/traccar/protocol/LaipacProtocolDecoder.java b/src/org/traccar/protocol/LaipacProtocolDecoder.java index dc7c394e4..7f26f225b 100644 --- a/src/org/traccar/protocol/LaipacProtocolDecoder.java +++ b/src/org/traccar/protocol/LaipacProtocolDecoder.java @@ -148,8 +148,7 @@ public class LaipacProtocolDecoder extends BaseProtocolDecoder { return position; } - private void setNextValue(Parser parser, Position position, String key) - { + private void setNextValue(Parser parser, Position position, String key) { String value = parser.next(); if (value != null) { position.set(key, value); -- cgit v1.2.3 From 37f9c76f4690b73f44c5dfbfe77509c5348b6401 Mon Sep 17 00:00:00 2001 From: Kevin Goos Date: Mon, 19 Mar 2018 16:12:03 +0100 Subject: Removed old port --- setup/default.xml | 1 - 1 file changed, 1 deletion(-) diff --git a/setup/default.xml b/setup/default.xml index 59ff2d460..da12467cc 100644 --- a/setup/default.xml +++ b/setup/default.xml @@ -231,5 +231,4 @@ 5159 5160 5161 - 5162 -- cgit v1.2.3 From 98e9a26fd916bcdace022af1d9f7dd80d1a2d2fe Mon Sep 17 00:00:00 2001 From: Kevin Goos Date: Tue, 20 Mar 2018 09:14:19 +0100 Subject: Fixed formatting --- .../traccar/protocol/LaipacProtocolDecoder.java | 37 +++++++++++----------- 1 file changed, 19 insertions(+), 18 deletions(-) diff --git a/src/org/traccar/protocol/LaipacProtocolDecoder.java b/src/org/traccar/protocol/LaipacProtocolDecoder.java index 7f26f225b..4fc0201ef 100644 --- a/src/org/traccar/protocol/LaipacProtocolDecoder.java +++ b/src/org/traccar/protocol/LaipacProtocolDecoder.java @@ -35,34 +35,35 @@ public class LaipacProtocolDecoder extends BaseProtocolDecoder { private static final Pattern PATTERN = new PatternBuilder() .text("$AVRMC,") - .expression("([^,]+),") // identifier - .number("(dd)(dd)(dd),") // time (hhmmss) - .expression("([AVRPavrp]),") // validity - .number("(dd)(dd.d+),") // latitude + .expression("([^,]+),") // identifier + .number("(dd)(dd)(dd),") // time (hhmmss) + .expression("([AVRPavrp]),") // validity + .number("(dd)(dd.d+),") // latitude .expression("([NS]),") - .number("(ddd)(dd.d+),") // longitude + .number("(ddd)(dd.d+),") // longitude .number("([EW]),") - .number("(d+.d+),") // speed - .number("(d+.d+),") // course - .number("(dd)(dd)(dd),") // date (ddmmyy) - .expression("([abZXTSMHFE86430]),") // event code + .number("(d+.d+),") // speed + .number("(d+.d+),") // course + .number("(dd)(dd)(dd),") // date (ddmmyy) + .expression("([abZXTSMHFE86430]),") // event code .number("(d+)").expression("(\\.?)").number("(d*),") // battery voltage - .number("(d+),") // current mileage - .number("(d),") // GPS on/off (1 = on, 0 = off) - .number("(d+),") // Analog port 1 - .number("(d+)") // Analog port 2 - .expression(",([0-9a-fA-F]{4})") // Cell 1 - Cell Net Code - .expression("([0-9a-fA-F]{4}),") // Cell 1 - Cell ID Code - .number("(d{3})") // Cell 2 - Country Code - .number("(d{3})") // Cell 2 - Operator Code + .number("(d+),") // current mileage + .number("(d),") // GPS on/off (1 = on, 0 = off) + .number("(d+),") // Analog port 1 + .number("(d+)") // Analog port 2 + .expression(",([0-9a-fA-F]{4})") // Cell 1 - Cell Net Code + .expression("([0-9a-fA-F]{4}),") // Cell 1 - Cell ID Code + .number("(d{3})") // Cell 2 - Country Code + .number("(d{3})") // Cell 2 - Operator Code .optional(4) .text("*") - .number("(xx)") // checksum + .number("(xx)") // checksum .compile(); @Override protected Object decode( Channel channel, SocketAddress remoteAddress, Object msg) throws Exception { + String sentence = (String) msg; if (sentence.startsWith("$ECHK") && channel != null) { -- cgit v1.2.3 From 2ce379a7ecf6c7b62673589d5c955727e22b2271 Mon Sep 17 00:00:00 2001 From: Kevin Goos Date: Tue, 20 Mar 2018 09:18:59 +0100 Subject: Fixed test formatting --- test/org/traccar/protocol/LaipacProtocolDecoderTest.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/test/org/traccar/protocol/LaipacProtocolDecoderTest.java b/test/org/traccar/protocol/LaipacProtocolDecoderTest.java index c8a23a5a7..41925040e 100644 --- a/test/org/traccar/protocol/LaipacProtocolDecoderTest.java +++ b/test/org/traccar/protocol/LaipacProtocolDecoderTest.java @@ -28,7 +28,8 @@ public class LaipacProtocolDecoderTest extends ProtocolTest { verifyNull(decoder, text( "$ECHK,MSG00002,0*5E")); - verifyPosition(decoder, text("$AVRMC,99999999,164339,A,4351.0542,N,07923.5445,W,0.29,78.66,180703,0,3.727,17,1,0,0*37"), + verifyPosition(decoder, text( + "$AVRMC,99999999,164339,A,4351.0542,N,07923.5445,W,0.29,78.66,180703,0,3.727,17,1,0,0*37"), position("2003-07-18 16:43:39.000", true, 43.85090, -79.39241)); verifyPosition(decoder, text( -- cgit v1.2.3 From 2ff1d912fad21a8b5014459300221171c3b836d2 Mon Sep 17 00:00:00 2001 From: Kevin Goos Date: Tue, 20 Mar 2018 10:15:00 +0100 Subject: Cleanup for cell info --- setup/default.xml | 5 +++-- src/org/traccar/model/Position.java | 4 ---- src/org/traccar/protocol/LaipacProtocolDecoder.java | 11 ++++++++--- 3 files changed, 11 insertions(+), 9 deletions(-) diff --git a/setup/default.xml b/setup/default.xml index da12467cc..455e84ae1 100644 --- a/setup/default.xml +++ b/setup/default.xml @@ -33,7 +33,7 @@ true ./schema/changelog-master.xml - + SELECT * FROM users WHERE email = :email OR login = :email @@ -231,4 +231,5 @@ 5159 5160 5161 - + + \ No newline at end of file diff --git a/src/org/traccar/model/Position.java b/src/org/traccar/model/Position.java index 65bd03de6..fa303c500 100644 --- a/src/org/traccar/model/Position.java +++ b/src/org/traccar/model/Position.java @@ -88,10 +88,6 @@ public class Position extends Message { public static final String KEY_DRIVER_UNIQUE_ID = "driverUniqueId"; - public static final String KEY_CELL_NET_CODE = "cellNetCode"; - public static final String KEY_CELL_ID_CODE = "cellIdCode"; - public static final String KEY_COUNTRY_CODE = "countryCode"; - // Start with 1 not 0 public static final String PREFIX_TEMP = "temp"; public static final String PREFIX_ADC = "adc"; diff --git a/src/org/traccar/protocol/LaipacProtocolDecoder.java b/src/org/traccar/protocol/LaipacProtocolDecoder.java index 4fc0201ef..5221e46ad 100644 --- a/src/org/traccar/protocol/LaipacProtocolDecoder.java +++ b/src/org/traccar/protocol/LaipacProtocolDecoder.java @@ -22,6 +22,8 @@ import org.traccar.helper.Checksum; import org.traccar.helper.DateBuilder; import org.traccar.helper.Parser; import org.traccar.helper.PatternBuilder; +import org.traccar.model.CellTower; +import org.traccar.model.Network; import org.traccar.model.Position; import java.net.SocketAddress; @@ -120,9 +122,12 @@ public class LaipacProtocolDecoder extends BaseProtocolDecoder { position.set(Position.PREFIX_ADC + 1, parser.nextDouble() * 0.001); position.set(Position.PREFIX_ADC + 2, parser.nextDouble() * 0.001); - setNextValue(parser, position, Position.KEY_CELL_NET_CODE); - setNextValue(parser, position, Position.KEY_CELL_ID_CODE); - setNextValue(parser, position, Position.KEY_COUNTRY_CODE); + int cellNetCode = Integer.parseInt(parser.next(), 16); + Long cellId = Long.parseLong(parser.next(), 16); + int countryCode = parser.nextInt(); + + position.setNetwork(new Network(CellTower.from(countryCode, cellNetCode, 0, cellId))); + setNextValue(parser, position, Position.KEY_OPERATOR); String checksum = parser.next(); -- cgit v1.2.3 From 02e3f0f196c9dc1227b1074ff39f52efd7ab418c Mon Sep 17 00:00:00 2001 From: Abyss777 Date: Tue, 20 Mar 2018 16:30:08 +0500 Subject: Add user objects to Event forward payload --- src/org/traccar/database/NotificationManager.java | 9 ++++++++- src/org/traccar/notification/EventForwarder.java | 13 +++++++++---- src/org/traccar/notification/JsonTypeEventForwarder.java | 6 ++++-- src/org/traccar/notification/MultiPartEventForwarder.java | 5 +++-- 4 files changed, 24 insertions(+), 9 deletions(-) diff --git a/src/org/traccar/database/NotificationManager.java b/src/org/traccar/database/NotificationManager.java index 34c39e8ab..1c59a8666 100644 --- a/src/org/traccar/database/NotificationManager.java +++ b/src/org/traccar/database/NotificationManager.java @@ -73,9 +73,16 @@ public class NotificationManager extends ExtendedObjectManager { long deviceId = event.getDeviceId(); Set users = Context.getPermissionsManager().getDeviceUsers(deviceId); + Set usersToForward = null; + if (Context.getEventForwarder() != null) { + usersToForward = new HashSet<>(); + } for (long userId : users) { if (event.getGeofenceId() == 0 || Context.getGeofenceManager() != null && Context.getGeofenceManager().checkItemPermission(userId, event.getGeofenceId())) { + if (usersToForward != null) { + usersToForward.add(userId); + } boolean sentWeb = false; boolean sentMail = false; boolean sentSms = Context.getSmppManager() == null; @@ -102,7 +109,7 @@ public class NotificationManager extends ExtendedObjectManager { } } if (Context.getEventForwarder() != null) { - Context.getEventForwarder().forwardEvent(event, position); + Context.getEventForwarder().forwardEvent(event, position, usersToForward); } } diff --git a/src/org/traccar/notification/EventForwarder.java b/src/org/traccar/notification/EventForwarder.java index 8be4c23b0..b13f8fe43 100644 --- a/src/org/traccar/notification/EventForwarder.java +++ b/src/org/traccar/notification/EventForwarder.java @@ -30,6 +30,8 @@ import org.traccar.model.Position; import java.nio.charset.StandardCharsets; import java.util.HashMap; import java.util.Map; +import java.util.Set; + import com.ning.http.client.FluentCaseInsensitiveStringsMap; public abstract class EventForwarder { @@ -46,8 +48,9 @@ public abstract class EventForwarder { private static final String KEY_EVENT = "event"; private static final String KEY_GEOFENCE = "geofence"; private static final String KEY_DEVICE = "device"; + private static final String KEY_USERS = "users"; - public final void forwardEvent(Event event, Position position) { + public final void forwardEvent(Event event, Position position, Set users) { BoundRequestBuilder requestBuilder = Context.getAsyncHttpClient().preparePost(url); requestBuilder.setBodyEncoding(StandardCharsets.UTF_8.name()); @@ -60,7 +63,7 @@ public abstract class EventForwarder { requestBuilder.setHeaders(params); } - setContent(event, position, requestBuilder); + setContent(event, position, users, requestBuilder); requestBuilder.execute(); } @@ -79,7 +82,7 @@ public abstract class EventForwarder { return paramsMap; } - protected String prepareJsonPayload(Event event, Position position) { + protected String prepareJsonPayload(Event event, Position position, Set users) { Map data = new HashMap<>(); data.put(KEY_EVENT, event); if (position != null) { @@ -95,6 +98,7 @@ public abstract class EventForwarder { data.put(KEY_GEOFENCE, geofence); } } + data.put(KEY_USERS, Context.getUsersManager().getItems(users)); try { return Context.getObjectMapper().writeValueAsString(data); } catch (JsonProcessingException e) { @@ -104,6 +108,7 @@ public abstract class EventForwarder { } protected abstract String getContentType(); - protected abstract void setContent(Event event, Position position, BoundRequestBuilder requestBuilder); + protected abstract void setContent( + Event event, Position position, Set users, BoundRequestBuilder requestBuilder); } diff --git a/src/org/traccar/notification/JsonTypeEventForwarder.java b/src/org/traccar/notification/JsonTypeEventForwarder.java index c1e4220d0..27ef61af1 100644 --- a/src/org/traccar/notification/JsonTypeEventForwarder.java +++ b/src/org/traccar/notification/JsonTypeEventForwarder.java @@ -1,5 +1,7 @@ package org.traccar.notification; +import java.util.Set; + import org.traccar.model.Event; import org.traccar.model.Position; @@ -13,8 +15,8 @@ public class JsonTypeEventForwarder extends EventForwarder { } @Override - protected void setContent(Event event, Position position, BoundRequestBuilder requestBuilder) { - requestBuilder.setBody(prepareJsonPayload(event, position)); + protected void setContent(Event event, Position position, Set users, BoundRequestBuilder requestBuilder) { + requestBuilder.setBody(prepareJsonPayload(event, position, users)); } } diff --git a/src/org/traccar/notification/MultiPartEventForwarder.java b/src/org/traccar/notification/MultiPartEventForwarder.java index f4c36d3e4..6227c66cc 100644 --- a/src/org/traccar/notification/MultiPartEventForwarder.java +++ b/src/org/traccar/notification/MultiPartEventForwarder.java @@ -3,6 +3,7 @@ package org.traccar.notification; import java.nio.charset.StandardCharsets; import java.util.List; import java.util.Map; +import java.util.Set; import java.util.Map.Entry; import org.traccar.Context; @@ -28,7 +29,7 @@ public class MultiPartEventForwarder extends EventForwarder { } @Override - protected void setContent(Event event, Position position, BoundRequestBuilder requestBuilder) { + protected void setContent(Event event, Position position, Set users, BoundRequestBuilder requestBuilder) { if (additionalParams != null && !additionalParams.isEmpty()) { Map> paramsToAdd = splitIntoKeyValues(additionalParams, "="); @@ -41,6 +42,6 @@ public class MultiPartEventForwarder extends EventForwarder { } } requestBuilder.addBodyPart(new StringPart(payloadParamName, - prepareJsonPayload(event, position), "application/json", StandardCharsets.UTF_8)); + prepareJsonPayload(event, position, users), "application/json", StandardCharsets.UTF_8)); } } -- cgit v1.2.3 From 11a5078af4f323f7132a50f3838c02b6e7f71a42 Mon Sep 17 00:00:00 2001 From: Kevin Goos Date: Tue, 20 Mar 2018 16:25:29 +0100 Subject: Switched network to differnt object --- .../traccar/protocol/LaipacProtocolDecoder.java | 25 ++++++++++------------ 1 file changed, 11 insertions(+), 14 deletions(-) diff --git a/src/org/traccar/protocol/LaipacProtocolDecoder.java b/src/org/traccar/protocol/LaipacProtocolDecoder.java index 5221e46ad..e7698dc95 100644 --- a/src/org/traccar/protocol/LaipacProtocolDecoder.java +++ b/src/org/traccar/protocol/LaipacProtocolDecoder.java @@ -122,13 +122,17 @@ public class LaipacProtocolDecoder extends BaseProtocolDecoder { position.set(Position.PREFIX_ADC + 1, parser.nextDouble() * 0.001); position.set(Position.PREFIX_ADC + 2, parser.nextDouble() * 0.001); - int cellNetCode = Integer.parseInt(parser.next(), 16); - Long cellId = Long.parseLong(parser.next(), 16); - int countryCode = parser.nextInt(); - - position.setNetwork(new Network(CellTower.from(countryCode, cellNetCode, 0, cellId))); - - setNextValue(parser, position, Position.KEY_OPERATOR); + String cellNetCodeString = parser.next(); + String cellIdString = parser.next(); + String countryCodeString = parser.next(); + String operatorCodeString = parser.next(); + if (cellNetCodeString != null && cellIdString != null && countryCodeString != null && operatorCodeString != null) { + int cellNetCode = Integer.parseInt(cellNetCodeString, 16); + Long cellId = Long.parseLong(cellIdString, 16); + int countryCode = Integer.parseInt(countryCodeString); + int operatorCode = Integer.parseInt(operatorCodeString); + position.setNetwork(new Network(CellTower.from(countryCode, operatorCode, cellNetCode, cellId))); + } String checksum = parser.next(); String result = sentence.replaceAll("^\\$(.*)\\*[0-9a-fA-F]{2}$", "$1"); @@ -154,13 +158,6 @@ public class LaipacProtocolDecoder extends BaseProtocolDecoder { return position; } - private void setNextValue(Parser parser, Position position, String key) { - String value = parser.next(); - if (value != null) { - position.set(key, value); - } - } - private String decodeAlarm(String event) { if (event.equals('Z')) { return Position.ALARM_LOW_BATTERY; -- cgit v1.2.3 From 7e7dca780552c7f34a4a7d8a6159d64e3b23a68b Mon Sep 17 00:00:00 2001 From: Kevin Goos Date: Tue, 20 Mar 2018 16:32:52 +0100 Subject: Format line longer then 120 characters --- src/org/traccar/protocol/LaipacProtocolDecoder.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/org/traccar/protocol/LaipacProtocolDecoder.java b/src/org/traccar/protocol/LaipacProtocolDecoder.java index e7698dc95..16ed6b750 100644 --- a/src/org/traccar/protocol/LaipacProtocolDecoder.java +++ b/src/org/traccar/protocol/LaipacProtocolDecoder.java @@ -126,7 +126,8 @@ public class LaipacProtocolDecoder extends BaseProtocolDecoder { String cellIdString = parser.next(); String countryCodeString = parser.next(); String operatorCodeString = parser.next(); - if (cellNetCodeString != null && cellIdString != null && countryCodeString != null && operatorCodeString != null) { + if (cellNetCodeString != null && cellIdString != null && + countryCodeString != null && operatorCodeString != null) { int cellNetCode = Integer.parseInt(cellNetCodeString, 16); Long cellId = Long.parseLong(cellIdString, 16); int countryCode = Integer.parseInt(countryCodeString); -- cgit v1.2.3 From 69f270e4b80803e919f16495b0102dd0f5d6acfe Mon Sep 17 00:00:00 2001 From: Kevin Goos Date: Tue, 20 Mar 2018 16:44:25 +0100 Subject: Fixed formatting --- src/org/traccar/protocol/LaipacProtocolDecoder.java | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/org/traccar/protocol/LaipacProtocolDecoder.java b/src/org/traccar/protocol/LaipacProtocolDecoder.java index 16ed6b750..b6d3dd8b2 100644 --- a/src/org/traccar/protocol/LaipacProtocolDecoder.java +++ b/src/org/traccar/protocol/LaipacProtocolDecoder.java @@ -126,8 +126,10 @@ public class LaipacProtocolDecoder extends BaseProtocolDecoder { String cellIdString = parser.next(); String countryCodeString = parser.next(); String operatorCodeString = parser.next(); - if (cellNetCodeString != null && cellIdString != null && - countryCodeString != null && operatorCodeString != null) { + if (cellNetCodeString != null + && cellIdString != null + && countryCodeString != null + && operatorCodeString != null) { int cellNetCode = Integer.parseInt(cellNetCodeString, 16); Long cellId = Long.parseLong(cellIdString, 16); int countryCode = Integer.parseInt(countryCodeString); -- cgit v1.2.3 From 3449aa51dc8250ba38772b39deb48f1f76be08c1 Mon Sep 17 00:00:00 2001 From: Anton Tananaev Date: Wed, 21 Mar 2018 11:36:45 +1300 Subject: Fix config issue --- setup/default.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/setup/default.xml b/setup/default.xml index 68465011e..1b106d2d0 100644 --- a/setup/default.xml +++ b/setup/default.xml @@ -230,6 +230,6 @@ 5158 5159 5160 - 5161 + 5161 -- cgit v1.2.3 From 917e631284d8e4054f9036eb39c249fe6bdf4248 Mon Sep 17 00:00:00 2001 From: Kevin Goos Date: Wed, 21 Mar 2018 09:04:15 +0100 Subject: Fixed small laipac issues --- src/org/traccar/model/Position.java | 1 + .../traccar/protocol/LaipacProtocolDecoder.java | 74 ++++++++++------------ 2 files changed, 34 insertions(+), 41 deletions(-) diff --git a/src/org/traccar/model/Position.java b/src/org/traccar/model/Position.java index fa303c500..49e3231c3 100644 --- a/src/org/traccar/model/Position.java +++ b/src/org/traccar/model/Position.java @@ -291,4 +291,5 @@ public class Position extends Message { public String getType() { return super.getType(); } + } diff --git a/src/org/traccar/protocol/LaipacProtocolDecoder.java b/src/org/traccar/protocol/LaipacProtocolDecoder.java index b6d3dd8b2..c319c96c0 100644 --- a/src/org/traccar/protocol/LaipacProtocolDecoder.java +++ b/src/org/traccar/protocol/LaipacProtocolDecoder.java @@ -102,10 +102,7 @@ public class LaipacProtocolDecoder extends BaseProtocolDecoder { position.setTime(dateBuilder.getDate()); String eventCode = parser.next(); - String decodedAlarm = decodeAlarm(eventCode); - if (decodedAlarm != null) { - position.set(Position.KEY_ALARM, decodeAlarm(eventCode)); - } + position.set(Position.KEY_ALARM, decodeAlarm(eventCode)); position.set(Position.KEY_EVENT, eventCode); double batteryVoltage = parser.nextDouble(); @@ -117,31 +114,24 @@ public class LaipacProtocolDecoder extends BaseProtocolDecoder { } position.set(Position.KEY_BATTERY, batteryVoltage); - position.set(Position.KEY_TOTAL_DISTANCE, parser.nextDouble()); + position.set(Position.KEY_ODOMETER, parser.nextDouble()); position.set(Position.KEY_GPS, parser.nextInt()); position.set(Position.PREFIX_ADC + 1, parser.nextDouble() * 0.001); position.set(Position.PREFIX_ADC + 2, parser.nextDouble() * 0.001); - String cellNetCodeString = parser.next(); - String cellIdString = parser.next(); - String countryCodeString = parser.next(); - String operatorCodeString = parser.next(); - if (cellNetCodeString != null - && cellIdString != null - && countryCodeString != null - && operatorCodeString != null) { - int cellNetCode = Integer.parseInt(cellNetCodeString, 16); - Long cellId = Long.parseLong(cellIdString, 16); - int countryCode = Integer.parseInt(countryCodeString); - int operatorCode = Integer.parseInt(operatorCodeString); - position.setNetwork(new Network(CellTower.from(countryCode, operatorCode, cellNetCode, cellId))); + String lac = parser.next(); + String cid = parser.next(); + String mcc = parser.next(); + String mnc = parser.next(); + if (lac != null + && cid != null + && mcc != null + && mnc != null) { + position.setNetwork(new Network(CellTower.from(Integer.parseInt(mcc), Integer.parseInt(mnc), + Integer.parseInt(lac, 16), Long.parseLong(cid, 16)))); } String checksum = parser.next(); - String result = sentence.replaceAll("^\\$(.*)\\*[0-9a-fA-F]{2}$", "$1"); - if (checksum == null || Integer.parseInt(checksum, 16) != Checksum.xor(result)) { - return null; - } if (channel != null) { if (eventCode.equals("3")) { @@ -162,25 +152,27 @@ public class LaipacProtocolDecoder extends BaseProtocolDecoder { } private String decodeAlarm(String event) { - if (event.equals('Z')) { - return Position.ALARM_LOW_BATTERY; - } else if (event.equals('X')) { - return Position.ALARM_GEOFENCE_ENTER; - } else if (event.equals('T')) { - return Position.ALARM_TAMPERING; - } else if (event.equals("H")) { - return Position.ALARM_POWER_OFF; - } else if (event.equals('X')) { - return Position.ALARM_GEOFENCE_ENTER; - } else if (event.equals('8')) { - return Position.ALARM_SHOCK; - } else if (event.equals('7') && event.equals('4')) { - return Position.ALARM_GEOFENCE_EXIT; - } else if (event.equals('6')) { - return Position.ALARM_OVERSPEED; - } else if (event.equals('3')) { - return Position.ALARM_SOS; + switch (event) + { + case "Z": + return Position.ALARM_LOW_BATTERY; + case "X": + return Position.ALARM_GEOFENCE_ENTER; + case "T": + return Position.ALARM_TAMPERING; + case "H": + return Position.ALARM_POWER_OFF; + case "8": + return Position.ALARM_SHOCK; + case "7": + case "4": + return Position.ALARM_GEOFENCE_EXIT; + case "6": + return Position.ALARM_OVERSPEED; + case "3": + return Position.ALARM_SOS; + default: + return null; } - return null; } } -- cgit v1.2.3 From 8f8ae1cec61c41526233f6bb13e57a817a39a0a3 Mon Sep 17 00:00:00 2001 From: Kevin Goos Date: Wed, 21 Mar 2018 09:09:56 +0100 Subject: Fixed default.xml changes --- setup/default.xml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/setup/default.xml b/setup/default.xml index 455e84ae1..1b106d2d0 100644 --- a/setup/default.xml +++ b/setup/default.xml @@ -33,7 +33,7 @@ true ./schema/changelog-master.xml - + SELECT * FROM users WHERE email = :email OR login = :email @@ -230,6 +230,6 @@ 5158 5159 5160 - 5161 + 5161 - \ No newline at end of file + -- cgit v1.2.3 From 3a600f2550cea7ddb8e0790c2392f3201cca1f1a Mon Sep 17 00:00:00 2001 From: Kevin Goos Date: Wed, 21 Mar 2018 09:11:21 +0100 Subject: Fixed default --- setup/default.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/setup/default.xml b/setup/default.xml index 1b106d2d0..68465011e 100644 --- a/setup/default.xml +++ b/setup/default.xml @@ -230,6 +230,6 @@ 5158 5159 5160 - 5161 + 5161 -- cgit v1.2.3 From 8cc019cf5c43af6dc741138511623551a772b739 Mon Sep 17 00:00:00 2001 From: Kevin Goos Date: Wed, 21 Mar 2018 09:13:22 +0100 Subject: Fixed formatting issue --- src/org/traccar/protocol/LaipacProtocolDecoder.java | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/org/traccar/protocol/LaipacProtocolDecoder.java b/src/org/traccar/protocol/LaipacProtocolDecoder.java index c319c96c0..f243fc629 100644 --- a/src/org/traccar/protocol/LaipacProtocolDecoder.java +++ b/src/org/traccar/protocol/LaipacProtocolDecoder.java @@ -152,8 +152,7 @@ public class LaipacProtocolDecoder extends BaseProtocolDecoder { } private String decodeAlarm(String event) { - switch (event) - { + switch (event) { case "Z": return Position.ALARM_LOW_BATTERY; case "X": -- cgit v1.2.3 From 64e342442d74116c2247d85467e49722b7ab45ef Mon Sep 17 00:00:00 2001 From: Kevin Goos Date: Wed, 21 Mar 2018 10:37:48 +0100 Subject: Simplify battery voltage --- src/org/traccar/helper/DataConverter.java | 2 +- src/org/traccar/protocol/LaipacProtocolDecoder.java | 13 ++++--------- 2 files changed, 5 insertions(+), 10 deletions(-) diff --git a/src/org/traccar/helper/DataConverter.java b/src/org/traccar/helper/DataConverter.java index 0df0219ad..7abd4ae93 100644 --- a/src/org/traccar/helper/DataConverter.java +++ b/src/org/traccar/helper/DataConverter.java @@ -26,7 +26,7 @@ public final class DataConverter { public static byte[] parseHex(String string) { try { - return Hex.decodeHex(string.toCharArray()); + return Hex.decodeHex(string); } catch (DecoderException e) { throw new RuntimeException(e); } diff --git a/src/org/traccar/protocol/LaipacProtocolDecoder.java b/src/org/traccar/protocol/LaipacProtocolDecoder.java index f243fc629..0a7df26fa 100644 --- a/src/org/traccar/protocol/LaipacProtocolDecoder.java +++ b/src/org/traccar/protocol/LaipacProtocolDecoder.java @@ -48,7 +48,7 @@ public class LaipacProtocolDecoder extends BaseProtocolDecoder { .number("(d+.d+),") // course .number("(dd)(dd)(dd),") // date (ddmmyy) .expression("([abZXTSMHFE86430]),") // event code - .number("(d+)").expression("(\\.?)").number("(d*),") // battery voltage + .number("(d.+),") // battery voltage .number("(d+),") // current mileage .number("(d),") // GPS on/off (1 = on, 0 = off) .number("(d+),") // Analog port 1 @@ -105,14 +105,9 @@ public class LaipacProtocolDecoder extends BaseProtocolDecoder { position.set(Position.KEY_ALARM, decodeAlarm(eventCode)); position.set(Position.KEY_EVENT, eventCode); - double batteryVoltage = parser.nextDouble(); - if (parser.next().isEmpty()) { - parser.next(); - batteryVoltage *= 0.001; - } else { - batteryVoltage += parser.nextDouble() * 0.001; - } - position.set(Position.KEY_BATTERY, batteryVoltage); + String batteryVoltage = parser.next(); + batteryVoltage = batteryVoltage.replaceAll("\\.",""); + position.set(Position.KEY_BATTERY, Double.parseDouble(batteryVoltage) * 0.001); position.set(Position.KEY_ODOMETER, parser.nextDouble()); position.set(Position.KEY_GPS, parser.nextInt()); -- cgit v1.2.3 From 38d7bd680614d02dec49d6f53b5e054380e374d6 Mon Sep 17 00:00:00 2001 From: Kevin Goos Date: Wed, 21 Mar 2018 10:38:43 +0100 Subject: Fixed battery regex --- src/org/traccar/protocol/LaipacProtocolDecoder.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/org/traccar/protocol/LaipacProtocolDecoder.java b/src/org/traccar/protocol/LaipacProtocolDecoder.java index 0a7df26fa..3e6037422 100644 --- a/src/org/traccar/protocol/LaipacProtocolDecoder.java +++ b/src/org/traccar/protocol/LaipacProtocolDecoder.java @@ -48,7 +48,7 @@ public class LaipacProtocolDecoder extends BaseProtocolDecoder { .number("(d+.d+),") // course .number("(dd)(dd)(dd),") // date (ddmmyy) .expression("([abZXTSMHFE86430]),") // event code - .number("(d.+),") // battery voltage + .expression("([\\d.]+),") // battery voltage .number("(d+),") // current mileage .number("(d),") // GPS on/off (1 = on, 0 = off) .number("(d+),") // Analog port 1 -- cgit v1.2.3 From cd943017616c491509d354a0d95bea5ec161da94 Mon Sep 17 00:00:00 2001 From: Kevin Goos Date: Wed, 21 Mar 2018 10:40:37 +0100 Subject: Fixed formatting of the battery voltage comment --- src/org/traccar/protocol/LaipacProtocolDecoder.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/org/traccar/protocol/LaipacProtocolDecoder.java b/src/org/traccar/protocol/LaipacProtocolDecoder.java index 3e6037422..a204f19bc 100644 --- a/src/org/traccar/protocol/LaipacProtocolDecoder.java +++ b/src/org/traccar/protocol/LaipacProtocolDecoder.java @@ -48,7 +48,7 @@ public class LaipacProtocolDecoder extends BaseProtocolDecoder { .number("(d+.d+),") // course .number("(dd)(dd)(dd),") // date (ddmmyy) .expression("([abZXTSMHFE86430]),") // event code - .expression("([\\d.]+),") // battery voltage + .expression("([\\d.]+),") // battery voltage .number("(d+),") // current mileage .number("(d),") // GPS on/off (1 = on, 0 = off) .number("(d+),") // Analog port 1 -- cgit v1.2.3 From 277b14899879e882869552acc7847d63a03effcf Mon Sep 17 00:00:00 2001 From: Kevin Goos Date: Wed, 21 Mar 2018 10:48:20 +0100 Subject: Fixed formatting --- src/org/traccar/protocol/LaipacProtocolDecoder.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/org/traccar/protocol/LaipacProtocolDecoder.java b/src/org/traccar/protocol/LaipacProtocolDecoder.java index a204f19bc..9357123c8 100644 --- a/src/org/traccar/protocol/LaipacProtocolDecoder.java +++ b/src/org/traccar/protocol/LaipacProtocolDecoder.java @@ -106,7 +106,7 @@ public class LaipacProtocolDecoder extends BaseProtocolDecoder { position.set(Position.KEY_EVENT, eventCode); String batteryVoltage = parser.next(); - batteryVoltage = batteryVoltage.replaceAll("\\.",""); + batteryVoltage = batteryVoltage.replaceAll("\\.", ""); position.set(Position.KEY_BATTERY, Double.parseDouble(batteryVoltage) * 0.001); position.set(Position.KEY_ODOMETER, parser.nextDouble()); -- cgit v1.2.3 From d2a130b71e9d8064515b16dff865f476a1aadf15 Mon Sep 17 00:00:00 2001 From: Anton Tananaev Date: Thu, 22 Mar 2018 01:04:42 +1300 Subject: Clean up and refactor Laipac --- .../traccar/protocol/LaipacProtocolDecoder.java | 102 ++++++++++----------- 1 file changed, 47 insertions(+), 55 deletions(-) diff --git a/src/org/traccar/protocol/LaipacProtocolDecoder.java b/src/org/traccar/protocol/LaipacProtocolDecoder.java index 9357123c8..bfaff9ec4 100644 --- a/src/org/traccar/protocol/LaipacProtocolDecoder.java +++ b/src/org/traccar/protocol/LaipacProtocolDecoder.java @@ -1,5 +1,5 @@ /* - * Copyright 2013 - 2017 Anton Tananaev (anton@traccar.org) + * Copyright 2013 - 2018 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. @@ -50,18 +50,42 @@ public class LaipacProtocolDecoder extends BaseProtocolDecoder { .expression("([abZXTSMHFE86430]),") // event code .expression("([\\d.]+),") // battery voltage .number("(d+),") // current mileage - .number("(d),") // GPS on/off (1 = on, 0 = off) - .number("(d+),") // Analog port 1 - .number("(d+)") // Analog port 2 - .expression(",([0-9a-fA-F]{4})") // Cell 1 - Cell Net Code - .expression("([0-9a-fA-F]{4}),") // Cell 1 - Cell ID Code - .number("(d{3})") // Cell 2 - Country Code - .number("(d{3})") // Cell 2 - Operator Code + .number("(d),") // gps status + .number("(d+),") // adc1 + .number("(d+)") // adc2 + .number(",(xxxx)") // lac + .number("(xxxx),") // cid + .number("(ddd)") // mcc + .number("(ddd)") // mnc .optional(4) .text("*") .number("(xx)") // checksum .compile(); + private String decodeAlarm(String event) { + switch (event) { + case "Z": + return Position.ALARM_LOW_BATTERY; + case "X": + return Position.ALARM_GEOFENCE_ENTER; + case "T": + return Position.ALARM_TAMPERING; + case "H": + return Position.ALARM_POWER_OFF; + case "8": + return Position.ALARM_SHOCK; + case "7": + case "4": + return Position.ALARM_GEOFENCE_EXIT; + case "6": + return Position.ALARM_OVERSPEED; + case "3": + return Position.ALARM_SOS; + default: + return null; + } + } + @Override protected Object decode( Channel channel, SocketAddress remoteAddress, Object msg) throws Exception { @@ -101,44 +125,35 @@ public class LaipacProtocolDecoder extends BaseProtocolDecoder { dateBuilder.setDateReverse(parser.nextInt(0), parser.nextInt(0), parser.nextInt(0)); position.setTime(dateBuilder.getDate()); - String eventCode = parser.next(); - position.set(Position.KEY_ALARM, decodeAlarm(eventCode)); - position.set(Position.KEY_EVENT, eventCode); - - String batteryVoltage = parser.next(); - batteryVoltage = batteryVoltage.replaceAll("\\.", ""); - position.set(Position.KEY_BATTERY, Double.parseDouble(batteryVoltage) * 0.001); - + String event = parser.next(); + position.set(Position.KEY_ALARM, decodeAlarm(event)); + position.set(Position.KEY_EVENT, event); + position.set(Position.KEY_BATTERY, Double.parseDouble(parser.next().replaceAll("\\.", "")) * 0.001); position.set(Position.KEY_ODOMETER, parser.nextDouble()); position.set(Position.KEY_GPS, parser.nextInt()); position.set(Position.PREFIX_ADC + 1, parser.nextDouble() * 0.001); position.set(Position.PREFIX_ADC + 2, parser.nextDouble() * 0.001); - String lac = parser.next(); - String cid = parser.next(); - String mcc = parser.next(); - String mnc = parser.next(); - if (lac != null - && cid != null - && mcc != null - && mnc != null) { - position.setNetwork(new Network(CellTower.from(Integer.parseInt(mcc), Integer.parseInt(mnc), - Integer.parseInt(lac, 16), Long.parseLong(cid, 16)))); + Integer lac = parser.nextHexInt(); + Integer cid = parser.nextHexInt(); + Integer mcc = parser.nextInt(); + Integer mnc = parser.nextInt(); + if (lac != null && cid != null && mcc != null && mnc != null) { + position.setNetwork(new Network(CellTower.from(mcc, mnc, lac, cid))); } String checksum = parser.next(); if (channel != null) { - if (eventCode.equals("3")) { + if (event.equals("3")) { channel.write("$AVCFG,00000000,d*31\r\n"); - } else if (eventCode.equals("X") || eventCode.equals("4")) { + } else if (event.equals("X") || event.equals("4")) { channel.write("$AVCFG,00000000,x*2D\r\n"); - } else if (eventCode.equals("Z")) { + } else if (event.equals("Z")) { channel.write("$AVCFG,00000000,z*2F\r\n"); } else if (Character.isLowerCase(status.charAt(0))) { - String response = "$EAVACK," + eventCode + "," + checksum; - response += Checksum.nmea(response); - response += "\r\n"; + String response = "$EAVACK," + event + "," + checksum; + response += Checksum.nmea(response) + "\r\n"; channel.write(response); } } @@ -146,27 +161,4 @@ public class LaipacProtocolDecoder extends BaseProtocolDecoder { return position; } - private String decodeAlarm(String event) { - switch (event) { - case "Z": - return Position.ALARM_LOW_BATTERY; - case "X": - return Position.ALARM_GEOFENCE_ENTER; - case "T": - return Position.ALARM_TAMPERING; - case "H": - return Position.ALARM_POWER_OFF; - case "8": - return Position.ALARM_SHOCK; - case "7": - case "4": - return Position.ALARM_GEOFENCE_EXIT; - case "6": - return Position.ALARM_OVERSPEED; - case "3": - return Position.ALARM_SOS; - default: - return null; - } - } } -- cgit v1.2.3 From 265b1c14a995905da2e216d6002c62bd0d586752 Mon Sep 17 00:00:00 2001 From: Abyss777 Date: Thu, 22 Mar 2018 09:24:25 +0500 Subject: Add support for long SMS sending --- src/org/traccar/smpp/SmppClient.java | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/src/org/traccar/smpp/SmppClient.java b/src/org/traccar/smpp/SmppClient.java index 0f7d2cf6d..bf395f90e 100644 --- a/src/org/traccar/smpp/SmppClient.java +++ b/src/org/traccar/smpp/SmppClient.java @@ -1,6 +1,6 @@ /* - * Copyright 2017 Anton Tananaev (anton@traccar.org) - * Copyright 2017 Andrey Kunitsyn (andrey@traccar.org) + * Copyright 2017 - 2018 Anton Tananaev (anton@traccar.org) + * Copyright 2017 - 2018 Andrey Kunitsyn (andrey@traccar.org) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -35,6 +35,7 @@ import com.cloudhopper.smpp.impl.DefaultSmppClient; import com.cloudhopper.smpp.impl.DefaultSmppSessionHandler; import com.cloudhopper.smpp.pdu.SubmitSm; import com.cloudhopper.smpp.pdu.SubmitSmResp; +import com.cloudhopper.smpp.tlv.Tlv; import com.cloudhopper.smpp.type.Address; import com.cloudhopper.smpp.type.RecoverablePduException; import com.cloudhopper.smpp.type.SmppChannelException; @@ -222,7 +223,13 @@ public class SmppClient { if (requestDlr) { submit.setRegisteredDelivery(SmppConstants.REGISTERED_DELIVERY_SMSC_RECEIPT_REQUESTED); } - submit.setShortMessage(textBytes); + + if (textBytes != null && textBytes.length > 255) { + submit.addOptionalParameter(new Tlv(SmppConstants.TAG_MESSAGE_PAYLOAD, textBytes, "message_payload")); + } else { + submit.setShortMessage(textBytes); + } + submit.setSourceAddress(command ? new Address(commandSourceTon, commandSourceNpi, commandSourceAddress) : new Address(sourceTon, sourceNpi, sourceAddress)); submit.setDestAddress(new Address(destTon, destNpi, destAddress)); -- cgit v1.2.3 From acc6a57605c81e9bb4c897b3da95aa8c875237f8 Mon Sep 17 00:00:00 2001 From: Anton Tananaev Date: Fri, 23 Mar 2018 00:55:47 +1300 Subject: Fix Meitrack taximeter decoding --- src/org/traccar/protocol/MeitrackProtocolDecoder.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/org/traccar/protocol/MeitrackProtocolDecoder.java b/src/org/traccar/protocol/MeitrackProtocolDecoder.java index 3a1885d6f..6fee0fd9f 100644 --- a/src/org/traccar/protocol/MeitrackProtocolDecoder.java +++ b/src/org/traccar/protocol/MeitrackProtocolDecoder.java @@ -256,7 +256,7 @@ public class MeitrackProtocolDecoder extends BaseProtocolDecoder { if (values.length > 5 && !values[5].isEmpty()) { String[] data = values[5].split("\\|"); - boolean started = data[0].charAt(0) == '0'; + boolean started = data[0].charAt(1) == '0'; position.set("taximeterOn", started); position.set("taximeterStart", data[1]); if (data.length > 2) { -- cgit v1.2.3 From 7e1da0e6223f1b07269fb3e43369e35c1f3baf3e Mon Sep 17 00:00:00 2001 From: Anton Tananaev Date: Fri, 23 Mar 2018 04:47:06 +1300 Subject: Additional Totem alarms --- src/org/traccar/protocol/TotemProtocolDecoder.java | 33 +++++++++++++++++++--- 1 file changed, 29 insertions(+), 4 deletions(-) diff --git a/src/org/traccar/protocol/TotemProtocolDecoder.java b/src/org/traccar/protocol/TotemProtocolDecoder.java index 7d8a22f8c..8da188f60 100644 --- a/src/org/traccar/protocol/TotemProtocolDecoder.java +++ b/src/org/traccar/protocol/TotemProtocolDecoder.java @@ -132,7 +132,7 @@ public class TotemProtocolDecoder extends BaseProtocolDecoder { private static final Pattern PATTERN4 = new PatternBuilder() .text("$$") // header .number("dddd") // length - .expression("A[ABC]") // type + .number("(xx)") // type .number("(d+)|") // imei .number("(x{8})") // status .number("(dd)(dd)(dd)") // date (yymmdd) @@ -164,7 +164,7 @@ public class TotemProtocolDecoder extends BaseProtocolDecoder { .any() .compile(); - private String decodeAlarm(Short value) { + private String decodeAlarm123(int value) { switch (value) { case 0x01: return Position.ALARM_SOS; @@ -183,10 +183,31 @@ public class TotemProtocolDecoder extends BaseProtocolDecoder { } } + private String decodeAlarm4(int value) { + switch (value) { + case 0x01: + return Position.ALARM_SOS; + case 0x02: + return Position.ALARM_OVERSPEED; + case 0x04: + return Position.ALARM_GEOFENCE_EXIT; + case 0x05: + return Position.ALARM_GEOFENCE_ENTER; + case 0x40: + return Position.ALARM_SHOCK; + case 0x42: + return Position.ALARM_ACCELERATION; + case 0x43: + return Position.ALARM_BRAKING; + default: + return null; + } + } + private boolean decode12(Position position, Parser parser, Pattern pattern) { if (parser.hasNext()) { - position.set(Position.KEY_ALARM, decodeAlarm(Short.parseShort(parser.next(), 16))); + position.set(Position.KEY_ALARM, decodeAlarm123(Short.parseShort(parser.next(), 16))); } DateBuilder dateBuilder = new DateBuilder(); int year = 0, month = 0, day = 0; @@ -246,7 +267,7 @@ public class TotemProtocolDecoder extends BaseProtocolDecoder { private boolean decode3(Position position, Parser parser) { if (parser.hasNext()) { - position.set(Position.KEY_ALARM, decodeAlarm(Short.parseShort(parser.next(), 16))); + position.set(Position.KEY_ALARM, decodeAlarm123(Short.parseShort(parser.next(), 16))); } position.setTime(parser.nextDateTime(Parser.DateTimeFormat.DMY_HMS)); @@ -350,6 +371,10 @@ public class TotemProtocolDecoder extends BaseProtocolDecoder { Position position = new Position(getProtocolName()); + if (pattern == PATTERN4) { + position.set(Position.KEY_ALARM, decodeAlarm4(parser.nextHexInt())); + } + DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, parser.next()); if (deviceSession == null) { return null; -- cgit v1.2.3 From feaa6e2f2816efad85bdb3a29e40bd2313e65475 Mon Sep 17 00:00:00 2001 From: Anton Tananaev Date: Fri, 23 Mar 2018 05:38:03 +1300 Subject: Initial EGTS protocol implementation --- setup/default.xml | 1 + src/org/traccar/protocol/EgtsFrameDecoder.java | 45 +++++++ src/org/traccar/protocol/EgtsProtocol.java | 42 ++++++ src/org/traccar/protocol/EgtsProtocolDecoder.java | 149 +++++++++++++++++++++ .../org/traccar/protocol/EgtsFrameDecoderTest.java | 21 +++ .../traccar/protocol/EgtsProtocolDecoderTest.java | 27 ++++ 6 files changed, 285 insertions(+) create mode 100644 src/org/traccar/protocol/EgtsFrameDecoder.java create mode 100644 src/org/traccar/protocol/EgtsProtocol.java create mode 100644 src/org/traccar/protocol/EgtsProtocolDecoder.java create mode 100644 test/org/traccar/protocol/EgtsFrameDecoderTest.java create mode 100644 test/org/traccar/protocol/EgtsProtocolDecoderTest.java diff --git a/setup/default.xml b/setup/default.xml index 1b106d2d0..912006bcf 100644 --- a/setup/default.xml +++ b/setup/default.xml @@ -231,5 +231,6 @@ 5159 5160 5161 + 5162 diff --git a/src/org/traccar/protocol/EgtsFrameDecoder.java b/src/org/traccar/protocol/EgtsFrameDecoder.java new file mode 100644 index 000000000..71ffc1811 --- /dev/null +++ b/src/org/traccar/protocol/EgtsFrameDecoder.java @@ -0,0 +1,45 @@ +/* + * Copyright 2018 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 org.jboss.netty.buffer.ChannelBuffer; +import org.jboss.netty.channel.Channel; +import org.jboss.netty.channel.ChannelHandlerContext; +import org.jboss.netty.handler.codec.frame.FrameDecoder; + +public class EgtsFrameDecoder extends FrameDecoder { + + @Override + protected Object decode( + ChannelHandlerContext ctx, Channel channel, ChannelBuffer buf) throws Exception { + + if (buf.readableBytes() < 10) { + return null; + } + + int headerLength = buf.getUnsignedByte(buf.readerIndex() + 3); + int frameLength = buf.getUnsignedShort(buf.readerIndex() + 5); + + int length = headerLength + frameLength + (frameLength > 0 ? 2 : 0); + + if (buf.readableBytes() >= length) { + return buf.readBytes(length); + } + + return null; + } + +} diff --git a/src/org/traccar/protocol/EgtsProtocol.java b/src/org/traccar/protocol/EgtsProtocol.java new file mode 100644 index 000000000..0a57f0061 --- /dev/null +++ b/src/org/traccar/protocol/EgtsProtocol.java @@ -0,0 +1,42 @@ +/* + * Copyright 2018 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 org.jboss.netty.bootstrap.ServerBootstrap; +import org.jboss.netty.channel.ChannelPipeline; +import org.traccar.BaseProtocol; +import org.traccar.TrackerServer; + +import java.util.List; + +public class EgtsProtocol extends BaseProtocol { + + public EgtsProtocol() { + super("egts"); + } + + @Override + public void initTrackerServers(List serverList) { + serverList.add(new TrackerServer(new ServerBootstrap(), getName()) { + @Override + protected void addSpecificHandlers(ChannelPipeline pipeline) { + pipeline.addLast("frameDecoder", new EgtsFrameDecoder()); + pipeline.addLast("objectDecoder", new EgtsProtocolDecoder(EgtsProtocol.this)); + } + }); + } + +} diff --git a/src/org/traccar/protocol/EgtsProtocolDecoder.java b/src/org/traccar/protocol/EgtsProtocolDecoder.java new file mode 100644 index 000000000..f32f564ed --- /dev/null +++ b/src/org/traccar/protocol/EgtsProtocolDecoder.java @@ -0,0 +1,149 @@ +/* + * Copyright 2018 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 org.jboss.netty.buffer.ChannelBuffer; +import org.jboss.netty.channel.Channel; +import org.traccar.BaseProtocolDecoder; +import org.traccar.DeviceSession; +import org.traccar.helper.BitUtil; +import org.traccar.model.Position; + +import java.net.SocketAddress; +import java.util.Date; +import java.util.LinkedList; +import java.util.List; + +public class EgtsProtocolDecoder extends BaseProtocolDecoder { + + public EgtsProtocolDecoder(EgtsProtocol protocol) { + super(protocol); + } + + public static final int SERVICE_AUTH = 1; + public static final int SERVICE_TELEDATA = 2; + public static final int SERVICE_COMMANDS = 4; + public static final int SERVICE_FIRMWARE = 9; + public static final int SERVICE_ECALL = 10; + + public static final int MSG_RECORD_RESPONSE = 0; + public static final int MSG_TERM_IDENTITY = 1; + public static final int MSG_MODULE_DATA = 2; + public static final int MSG_VEHICLE_DATA = 3; + public static final int MSG_AUTH_PARAMS = 4; + public static final int MSG_AUTH_INFO = 5; + public static final int MSG_SERVICE_INFO = 6; + public static final int MSG_RESULT_CODE = 7; + public static final int MSG_POS_DATA = 16; + public static final int MSG_EXT_POS_DATA = 17; + public static final int MSG_AD_SENSORS_DATA = 18; + public static final int MSG_COUNTERS_DATA = 19; + public static final int MSG_STATE_DATA = 20; + public static final int MSG_LOOPIN_DATA = 22; + public static final int MSG_ABS_DIG_SENS_DATA = 23; + public static final int MSG_ABS_AN_SENS_DATA = 24; + public static final int MSG_ABS_CNTR_DATA = 25; + public static final int MSG_ABS_LOOPIN_DATA = 26; + public static final int MSG_LIQUID_LEVEL_SENSOR = 27; + public static final int MSG_PASSENGERS_COUNTERS = 28; + + @Override + protected Object decode( + Channel channel, SocketAddress remoteAddress, Object msg) throws Exception { + + ChannelBuffer buf = (ChannelBuffer) msg; + + buf.skipBytes(buf.getUnsignedByte(buf.readerIndex() + 3)); + + DeviceSession deviceSession = null; + List positions = new LinkedList<>(); + + while (buf.readableBytes() > 2) { + + int length = buf.readUnsignedShort(); + + buf.readUnsignedShort(); // index + + int flags = buf.readUnsignedByte(); + + if (BitUtil.check(flags, 0)) { + String deviceId = String.valueOf(buf.readUnsignedInt()); + if (deviceSession == null) { + deviceSession = getDeviceSession(channel, remoteAddress, deviceId); + } + } + + if (deviceSession == null) { + deviceSession = getDeviceSession(channel, remoteAddress); + } + + if (BitUtil.check(flags, 1)) { + buf.readUnsignedInt(); // event id + } + if (BitUtil.check(flags, 2)) { + buf.readUnsignedInt(); // time + } + + buf.readUnsignedByte(); // source service type + buf.readUnsignedByte(); // recipient service type + + int recordEnd = buf.readerIndex() + length; + + while (buf.readerIndex() < recordEnd) { + int type = buf.readUnsignedByte(); + int end = buf.readUnsignedShort() + buf.readerIndex(); + + if (type == MSG_POS_DATA) { + Position position = new Position(getProtocolName()); + position.setDeviceId(deviceSession.getDeviceId()); + + position.setTime(new Date((buf.readUnsignedInt() + 1262304000) * 1000)); // since 2010-01-01 + position.setLatitude(buf.readUnsignedInt() * 90.0 / 0xFFFFFFFFL); + position.setLongitude(buf.readUnsignedInt() * 180.0 / 0xFFFFFFFFL); + + int positionFlags = buf.readUnsignedByte(); + position.setValid(BitUtil.check(positionFlags, 0)); + if (BitUtil.check(positionFlags, 5)) { + position.setLatitude(-position.getLatitude()); + } + if (BitUtil.check(positionFlags, 6)) { + position.setLongitude(-position.getLongitude()); + } + + int speed = buf.readUnsignedShort(); + position.setSpeed(BitUtil.to(speed, 14)); + position.setCourse(buf.readUnsignedByte() + (BitUtil.check(speed, 15) ? 0x100 : 0)); + + position.set(Position.KEY_ODOMETER, buf.readUnsignedMedium() * 100); + position.set(Position.KEY_INPUT, buf.readUnsignedByte()); + position.set(Position.KEY_EVENT, buf.readUnsignedByte()); + + if (BitUtil.check(positionFlags, 7)) { + position.setAltitude(buf.readMedium()); + } + + positions.add(position); + } + + buf.readerIndex(end); + } + + } + + return positions.isEmpty() ? null : positions; + } + +} diff --git a/test/org/traccar/protocol/EgtsFrameDecoderTest.java b/test/org/traccar/protocol/EgtsFrameDecoderTest.java new file mode 100644 index 000000000..91a53525e --- /dev/null +++ b/test/org/traccar/protocol/EgtsFrameDecoderTest.java @@ -0,0 +1,21 @@ +package org.traccar.protocol; + +import org.junit.Test; +import org.traccar.ProtocolTest; + +import java.nio.ByteOrder; + +public class EgtsFrameDecoderTest extends ProtocolTest { + + @Test + public void testDecode() throws Exception { + + EgtsFrameDecoder decoder = new EgtsFrameDecoder(); + + verifyFrame( + binary("0100020B0025003A5701C91A003A5701CD6E68490202101700CBB4740F7617FD924364104F116A0000000000010300001EC2"), + decoder.decode(null, null, binary(ByteOrder.LITTLE_ENDIAN, "0100020B0025003A5701C91A003A5701CD6E68490202101700CBB4740F7617FD924364104F116A0000000000010300001EC2"))); + + } + +} diff --git a/test/org/traccar/protocol/EgtsProtocolDecoderTest.java b/test/org/traccar/protocol/EgtsProtocolDecoderTest.java new file mode 100644 index 000000000..9a3434bec --- /dev/null +++ b/test/org/traccar/protocol/EgtsProtocolDecoderTest.java @@ -0,0 +1,27 @@ +package org.traccar.protocol; + +import org.junit.Test; +import org.traccar.ProtocolTest; + +import java.nio.ByteOrder; + +public class EgtsProtocolDecoderTest extends ProtocolTest { + + @Test + public void testDecode() throws Exception { + + EgtsProtocolDecoder decoder = new EgtsProtocolDecoder(new EgtsProtocol()); + + verifyPositions(decoder, binary(ByteOrder.LITTLE_ENDIAN, + "0100020B0025003A5701C91A003A5701CD6E68490202101700CBB4740F7617FD924364104F116A0000000000010300001EC2"), + position("2018-03-21 05:38:19.000", true, 51.67569, 55.59189)); + + verifyPositions(decoder, binary(ByteOrder.LITTLE_ENDIAN, + "0100020B0079000000011F6A001424951CA5CB0F23B5740F020210180023B5740F0A301994DA9C524C9128000A000000100082000011040018110300120900000003150100E803001B0700010000340900001B0700420000000000001B0700430000000000001B0700440000000000001B0700450000000000001B0700460000000000008020")); + + verifyPositions(decoder, binary(ByteOrder.LITTLE_ENDIAN, + "0100020B00F200000001D66A001224951CA5CB0FFCB4740F0202101800FCB4740F502119943D9F524C9119805C000000100084000011040018110300120900000003150100E803001B0700410000000000001B0700420000000000001B0700430000000000001B0700440000000000001B0700450000000000001B0700460000000000006A001324951CA5CB0F05B5740F020210180005B5740F222519942D9E524C9100008B000000100083000011040018110300120900000003160100E803001B0700010000310900001B0700420000000000001B0700430000000000001B0700440000000000001B0700450000000000001B070046000000000000134E")); + + } + +} -- cgit v1.2.3 From 2117e6ea67a9016b4a5e4978bfc3ec9fb29a9092 Mon Sep 17 00:00:00 2001 From: Anton Tananaev Date: Fri, 23 Mar 2018 05:48:13 +1300 Subject: Additional EGTS record types --- src/org/traccar/protocol/EgtsProtocolDecoder.java | 50 +++++++++++++++++------ 1 file changed, 38 insertions(+), 12 deletions(-) diff --git a/src/org/traccar/protocol/EgtsProtocolDecoder.java b/src/org/traccar/protocol/EgtsProtocolDecoder.java index f32f564ed..d05504d81 100644 --- a/src/org/traccar/protocol/EgtsProtocolDecoder.java +++ b/src/org/traccar/protocol/EgtsProtocolDecoder.java @@ -77,9 +77,9 @@ public class EgtsProtocolDecoder extends BaseProtocolDecoder { buf.readUnsignedShort(); // index - int flags = buf.readUnsignedByte(); + int recordFlags = buf.readUnsignedByte(); - if (BitUtil.check(flags, 0)) { + if (BitUtil.check(recordFlags, 0)) { String deviceId = String.valueOf(buf.readUnsignedInt()); if (deviceSession == null) { deviceSession = getDeviceSession(channel, remoteAddress, deviceId); @@ -90,10 +90,10 @@ public class EgtsProtocolDecoder extends BaseProtocolDecoder { deviceSession = getDeviceSession(channel, remoteAddress); } - if (BitUtil.check(flags, 1)) { + if (BitUtil.check(recordFlags, 1)) { buf.readUnsignedInt(); // event id } - if (BitUtil.check(flags, 2)) { + if (BitUtil.check(recordFlags, 2)) { buf.readUnsignedInt(); // time } @@ -102,24 +102,25 @@ public class EgtsProtocolDecoder extends BaseProtocolDecoder { int recordEnd = buf.readerIndex() + length; + Position position = new Position(getProtocolName()); + position.setDeviceId(deviceSession.getDeviceId()); + while (buf.readerIndex() < recordEnd) { int type = buf.readUnsignedByte(); int end = buf.readUnsignedShort() + buf.readerIndex(); if (type == MSG_POS_DATA) { - Position position = new Position(getProtocolName()); - position.setDeviceId(deviceSession.getDeviceId()); position.setTime(new Date((buf.readUnsignedInt() + 1262304000) * 1000)); // since 2010-01-01 position.setLatitude(buf.readUnsignedInt() * 90.0 / 0xFFFFFFFFL); position.setLongitude(buf.readUnsignedInt() * 180.0 / 0xFFFFFFFFL); - int positionFlags = buf.readUnsignedByte(); - position.setValid(BitUtil.check(positionFlags, 0)); - if (BitUtil.check(positionFlags, 5)) { + int flags = buf.readUnsignedByte(); + position.setValid(BitUtil.check(flags, 0)); + if (BitUtil.check(flags, 5)) { position.setLatitude(-position.getLatitude()); } - if (BitUtil.check(positionFlags, 6)) { + if (BitUtil.check(flags, 6)) { position.setLongitude(-position.getLongitude()); } @@ -131,16 +132,41 @@ public class EgtsProtocolDecoder extends BaseProtocolDecoder { position.set(Position.KEY_INPUT, buf.readUnsignedByte()); position.set(Position.KEY_EVENT, buf.readUnsignedByte()); - if (BitUtil.check(positionFlags, 7)) { + if (BitUtil.check(flags, 7)) { position.setAltitude(buf.readMedium()); } - positions.add(position); + } else if (type == MSG_EXT_POS_DATA) { + + int flags = buf.readUnsignedByte(); + + if (BitUtil.check(flags, 0)) { + position.set(Position.KEY_VDOP, buf.readUnsignedShort()); + } + if (BitUtil.check(flags, 1)) { + position.set(Position.KEY_HDOP, buf.readUnsignedShort()); + } + if (BitUtil.check(flags, 2)) { + position.set(Position.KEY_PDOP, buf.readUnsignedShort()); + } + if (BitUtil.check(flags, 3)) { + position.set(Position.KEY_SATELLITES, buf.readUnsignedByte()); + } + + } else if (type == MSG_AD_SENSORS_DATA) { + + buf.readUnsignedByte(); // inputs flags + + position.set(Position.KEY_OUTPUT, buf.readUnsignedByte()); + + buf.readUnsignedByte(); // adc flags + } buf.readerIndex(end); } + positions.add(position); } return positions.isEmpty() ? null : positions; -- cgit v1.2.3 From 2fa162dd6043fb8b329d3ec7b306ade76d5007b8 Mon Sep 17 00:00:00 2001 From: Anton Tananaev Date: Sat, 24 Mar 2018 10:57:03 +1300 Subject: Enable future filtering by default --- setup/default.xml | 3 +++ 1 file changed, 3 insertions(+) diff --git a/setup/default.xml b/setup/default.xml index 912006bcf..1788cb901 100644 --- a/setup/default.xml +++ b/setup/default.xml @@ -18,6 +18,9 @@ all ./logs/tracker-server.log + true + 3600 + true true true -- cgit v1.2.3 From 799fb0de7dd7fae0d307985f6b2975c077c262d3 Mon Sep 17 00:00:00 2001 From: Anton Tananaev Date: Sat, 24 Mar 2018 11:45:19 +1300 Subject: Fix Castel commands encoding --- src/org/traccar/protocol/CastelProtocolEncoder.java | 4 ++-- test/org/traccar/protocol/CastelProtocolEncoderTest.java | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/org/traccar/protocol/CastelProtocolEncoder.java b/src/org/traccar/protocol/CastelProtocolEncoder.java index 7ba33ca6d..806dac19e 100644 --- a/src/org/traccar/protocol/CastelProtocolEncoder.java +++ b/src/org/traccar/protocol/CastelProtocolEncoder.java @@ -35,9 +35,9 @@ public class CastelProtocolEncoder extends BaseProtocolEncoder { buf.writeByte('@'); buf.writeByte('@'); - buf.writeShort(2 + 2 + 1 + 20 + content.readableBytes()); // length + buf.writeShort(2 + 2 + 1 + 20 + 2 + content.readableBytes() + 2 + 2); // length - buf.writeByte(4); // protocol version + buf.writeByte(1); // protocol version buf.writeBytes(uniqueId.getBytes(StandardCharsets.US_ASCII)); buf.writeZero(20 - uniqueId.length()); diff --git a/test/org/traccar/protocol/CastelProtocolEncoderTest.java b/test/org/traccar/protocol/CastelProtocolEncoderTest.java index fc0a92c86..bcb93a010 100644 --- a/test/org/traccar/protocol/CastelProtocolEncoderTest.java +++ b/test/org/traccar/protocol/CastelProtocolEncoderTest.java @@ -15,7 +15,7 @@ public class CastelProtocolEncoderTest extends ProtocolTest { command.setDeviceId(1); command.setType(Command.TYPE_ENGINE_STOP); - verifyCommand(encoder, command, binary("40401a00043132333435363738393031323334350000000000458301fe6a0d0a")); + verifyCommand(encoder, command, binary("40402000013132333435363738393031323334350000000000458301a94a0d0a")); } -- cgit v1.2.3 From 0117dfeef0a7ffbc7a47d9681811177136336730 Mon Sep 17 00:00:00 2001 From: Ivan Martinez Date: Sun, 25 Mar 2018 20:19:46 -0300 Subject: method to calculate distance from center of circle --- src/org/traccar/geofence/GeofenceCircle.java | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/org/traccar/geofence/GeofenceCircle.java b/src/org/traccar/geofence/GeofenceCircle.java index d78734714..f6fca63ca 100644 --- a/src/org/traccar/geofence/GeofenceCircle.java +++ b/src/org/traccar/geofence/GeofenceCircle.java @@ -39,9 +39,13 @@ public class GeofenceCircle extends GeofenceGeometry { this.radius = radius; } + public double distanceFromCenter(double latitude, double longitude) { + return DistanceCalculator.distance(centerLatitude, centerLongitude, latitude, longitude); + } + @Override public boolean containsPoint(double latitude, double longitude) { - return DistanceCalculator.distance(centerLatitude, centerLongitude, latitude, longitude) <= radius; + return distanceFromCenter(latitude, longitude) <= radius; } @Override -- cgit v1.2.3 From d847f80334498bde6993b8f3a38c7df05d83b47b Mon Sep 17 00:00:00 2001 From: Anton Tananaev Date: Mon, 26 Mar 2018 06:45:10 +1300 Subject: Implement EGTS responses --- src/org/traccar/helper/Checksum.java | 35 +++++++ src/org/traccar/protocol/EgtsProtocol.java | 7 +- src/org/traccar/protocol/EgtsProtocolDecoder.java | 109 ++++++++++++++++++--- .../traccar/protocol/EgtsProtocolDecoderTest.java | 3 + 4 files changed, 136 insertions(+), 18 deletions(-) diff --git a/src/org/traccar/helper/Checksum.java b/src/org/traccar/helper/Checksum.java index 43ba6a689..c4141e7d2 100644 --- a/src/org/traccar/helper/Checksum.java +++ b/src/org/traccar/helper/Checksum.java @@ -129,6 +129,33 @@ public final class Checksum { 0x8201, 0x42c0, 0x4380, 0x8341, 0x4100, 0x81c1, 0x8081, 0x4040, }; + private static final int[] CRC8_TABLE = { + 0x00, 0x31, 0x62, 0x53, 0xC4, 0xF5, 0xA6, 0x97, + 0xB9, 0x88, 0xDB, 0xEA, 0x7D, 0x4C, 0x1F, 0x2E, + 0x43, 0x72, 0x21, 0x10, 0x87, 0xB6, 0xE5, 0xD4, + 0xFA, 0xCB, 0x98, 0xA9, 0x3E, 0x0F, 0x5C, 0x6D, + 0x86, 0xB7, 0xE4, 0xD5, 0x42, 0x73, 0x20, 0x11, 0x3F, + 0x0E, 0x5D, 0x6C, 0xFB, 0xCA, 0x99, 0xA8, 0xC5, 0xF4, 0xA7, 0x96, + 0x01, 0x30, 0x63, 0x52, 0x7C, 0x4D, 0x1E, 0x2F, 0xB8, 0x89, 0xDA, + 0xEB, 0x3D, 0x0C, 0x5F, 0x6E, 0xF9, 0xC8, 0x9B, 0xAA, 0x84, 0xB5, + 0xE6, 0xD7, 0x40, 0x71, 0x22, 0x13, 0x7E, 0x4F, 0x1C, 0x2D, 0xBA, + 0x8B, 0xD8, 0xE9, 0xC7, 0xF6, 0xA5, 0x94, 0x03, 0x32, 0x61, 0x50, + 0xBB, 0x8A, 0xD9, 0xE8, 0x7F, 0x4E, 0x1D, 0x2C, 0x02, 0x33, 0x60, + 0x51, 0xC6, 0xF7, 0xA4, 0x95, 0xF8, 0xC9, 0x9A, 0xAB, 0x3C, 0x0D, + 0x5E, 0x6F, 0x41, 0x70, 0x23, 0x12, 0x85, 0xB4, 0xE7, 0xD6, 0x7A, + 0x4B, 0x18, 0x29, 0xBE, 0x8F, 0xDC, 0xED, 0xC3, 0xF2, 0xA1, 0x90, + 0x07, 0x36, 0x65, 0x54, 0x39, 0x08, 0x5B, 0x6A, 0xFD, 0xCC, 0x9F, + 0xAE, 0x80, 0xB1, 0xE2, 0xD3, 0x44, 0x75, 0x26, 0x17, 0xFC, 0xCD, + 0x9E, 0xAF, 0x38, 0x09, 0x5A, 0x6B, 0x45, 0x74, 0x27, 0x16, 0x81, + 0xB0, 0xE3, 0xD2, 0xBF, 0x8E, 0xDD, 0xEC, 0x7B, 0x4A, 0x19, 0x28, + 0x06, 0x37, 0x64, 0x55, 0xC2, 0xF3, 0xA0, 0x91, 0x47, 0x76, 0x25, + 0x14, 0x83, 0xB2, 0xE1, 0xD0, 0xFE, 0xCF, 0x9C, 0xAD, 0x3A, 0x0B, + 0x58, 0x69, 0x04, 0x35, 0x66, 0x57, 0xC0, 0xF1, 0xA2, 0x93, 0xBD, + 0x8C, 0xDF, 0xEE, 0x79, 0x48, 0x1B, 0x2A, 0xC1, 0xF0, 0xA3, 0x92, + 0x05, 0x34, 0x67, 0x56, 0x78, 0x49, 0x1A, 0x2B, 0xBC, 0x8D, 0xDE, + 0xEF, 0x82, 0xB3, 0xE0, 0xD1, 0x46, 0x77, 0x24, 0x15, 0x3B, 0x0A, + 0x59, 0x68, 0xFF, 0xCE, 0x9D, 0xAC }; + // More info: http://reveng.sourceforge.net/crc-catalogue/16.htm public static final String CRC16_IBM = "IBM"; public static final String CRC16_MODBUS = "MODBUS"; @@ -181,6 +208,14 @@ public final class Checksum { } } + public static int crc8(ByteBuffer buf) { + int crc = 0xFF; + while (buf.hasRemaining()) { + crc = CRC8_TABLE[(crc ^ buf.get()) & 0xFF]; + } + return crc & 0xFF; + } + public static int crc32(ByteBuffer buf) { CRC32 checksum = new CRC32(); while (buf.hasRemaining()) { diff --git a/src/org/traccar/protocol/EgtsProtocol.java b/src/org/traccar/protocol/EgtsProtocol.java index 0a57f0061..13ec6c9a7 100644 --- a/src/org/traccar/protocol/EgtsProtocol.java +++ b/src/org/traccar/protocol/EgtsProtocol.java @@ -20,6 +20,7 @@ import org.jboss.netty.channel.ChannelPipeline; import org.traccar.BaseProtocol; import org.traccar.TrackerServer; +import java.nio.ByteOrder; import java.util.List; public class EgtsProtocol extends BaseProtocol { @@ -30,13 +31,15 @@ public class EgtsProtocol extends BaseProtocol { @Override public void initTrackerServers(List serverList) { - serverList.add(new TrackerServer(new ServerBootstrap(), getName()) { + TrackerServer server = new TrackerServer(new ServerBootstrap(), getName()) { @Override protected void addSpecificHandlers(ChannelPipeline pipeline) { pipeline.addLast("frameDecoder", new EgtsFrameDecoder()); pipeline.addLast("objectDecoder", new EgtsProtocolDecoder(EgtsProtocol.this)); } - }); + }; + server.setEndianness(ByteOrder.LITTLE_ENDIAN); + serverList.add(server); } } diff --git a/src/org/traccar/protocol/EgtsProtocolDecoder.java b/src/org/traccar/protocol/EgtsProtocolDecoder.java index d05504d81..85376b1a5 100644 --- a/src/org/traccar/protocol/EgtsProtocolDecoder.java +++ b/src/org/traccar/protocol/EgtsProtocolDecoder.java @@ -16,13 +16,17 @@ package org.traccar.protocol; import org.jboss.netty.buffer.ChannelBuffer; +import org.jboss.netty.buffer.ChannelBuffers; import org.jboss.netty.channel.Channel; import org.traccar.BaseProtocolDecoder; import org.traccar.DeviceSession; import org.traccar.helper.BitUtil; +import org.traccar.helper.Checksum; import org.traccar.model.Position; import java.net.SocketAddress; +import java.nio.ByteOrder; +import java.nio.charset.StandardCharsets; import java.util.Date; import java.util.LinkedList; import java.util.List; @@ -33,6 +37,10 @@ public class EgtsProtocolDecoder extends BaseProtocolDecoder { super(protocol); } + public static final int PT_RESPONSE = 0; + public static final int PT_APPDATA = 1; + public static final int PT_SIGNED_APPDATA = 2; + public static final int SERVICE_AUTH = 1; public static final int SERVICE_TELEDATA = 2; public static final int SERVICE_COMMANDS = 4; @@ -60,6 +68,44 @@ public class EgtsProtocolDecoder extends BaseProtocolDecoder { public static final int MSG_LIQUID_LEVEL_SENSOR = 27; public static final int MSG_PASSENGERS_COUNTERS = 28; + private int packetId; + + private void sendResponse( + Channel channel, int packetType, int index, int serviceType, int type, ChannelBuffer content) { + if (channel != null) { + + ChannelBuffer data = ChannelBuffers.dynamicBuffer(ByteOrder.LITTLE_ENDIAN, 0); + data.writeByte(type); + data.writeShort(content.readableBytes()); + data.writeBytes(content); + + ChannelBuffer record = ChannelBuffers.dynamicBuffer(ByteOrder.LITTLE_ENDIAN, 0); + record.writeShort(data.readableBytes()); + record.writeShort(index); + record.writeByte(1 << 6); // flags + record.writeByte(serviceType); + record.writeByte(serviceType); + record.writeBytes(data); + int recordChecksum = Checksum.crc16(Checksum.CRC16_CCITT_FALSE, record.toByteBuffer()); + + ChannelBuffer response = ChannelBuffers.dynamicBuffer(ByteOrder.LITTLE_ENDIAN, 0); + response.writeByte(1); // protocol version + response.writeByte(0); // security key id + response.writeByte(0); // flags + response.writeByte(5 + 2 + 2 + 2); // header length + response.writeByte(0); // encoding + response.writeShort(record.readableBytes()); + response.writeShort(packetId++); + response.writeByte(packetType); + response.writeByte(Checksum.crc8(response.toByteBuffer())); + response.writeBytes(record); + response.writeShort(recordChecksum); + + channel.write(response); + + } + } + @Override protected Object decode( Channel channel, SocketAddress remoteAddress, Object msg) throws Exception { @@ -68,26 +114,16 @@ public class EgtsProtocolDecoder extends BaseProtocolDecoder { buf.skipBytes(buf.getUnsignedByte(buf.readerIndex() + 3)); - DeviceSession deviceSession = null; List positions = new LinkedList<>(); while (buf.readableBytes() > 2) { int length = buf.readUnsignedShort(); - - buf.readUnsignedShort(); // index - + int index = buf.readUnsignedShort(); int recordFlags = buf.readUnsignedByte(); if (BitUtil.check(recordFlags, 0)) { - String deviceId = String.valueOf(buf.readUnsignedInt()); - if (deviceSession == null) { - deviceSession = getDeviceSession(channel, remoteAddress, deviceId); - } - } - - if (deviceSession == null) { - deviceSession = getDeviceSession(channel, remoteAddress); + buf.readUnsignedInt(); // object id } if (BitUtil.check(recordFlags, 1)) { @@ -97,19 +133,58 @@ public class EgtsProtocolDecoder extends BaseProtocolDecoder { buf.readUnsignedInt(); // time } - buf.readUnsignedByte(); // source service type + int serviceType = buf.readUnsignedByte(); buf.readUnsignedByte(); // recipient service type int recordEnd = buf.readerIndex() + length; Position position = new Position(getProtocolName()); - position.setDeviceId(deviceSession.getDeviceId()); + DeviceSession deviceSession = getDeviceSession(channel, remoteAddress); + if (deviceSession != null) { + position.setDeviceId(deviceSession.getDeviceId()); + } + + ChannelBuffer response = ChannelBuffers.dynamicBuffer(ByteOrder.LITTLE_ENDIAN, 0); + response.writeShort(index); + response.writeByte(0); // success + sendResponse(channel, PT_RESPONSE, index, serviceType, MSG_RECORD_RESPONSE, response); while (buf.readerIndex() < recordEnd) { int type = buf.readUnsignedByte(); int end = buf.readUnsignedShort() + buf.readerIndex(); - if (type == MSG_POS_DATA) { + if (type == MSG_TERM_IDENTITY) { + + buf.readUnsignedInt(); // object id + int flags = buf.readUnsignedByte(); + + if (BitUtil.check(flags, 0)) { + buf.readUnsignedShort(); // home dispatcher identifier + } + if (BitUtil.check(flags, 1)) { + getDeviceSession(channel, remoteAddress, buf.readBytes(15).toString(StandardCharsets.US_ASCII)); + } + if (BitUtil.check(flags, 2)) { + getDeviceSession(channel, remoteAddress, buf.readBytes(16).toString(StandardCharsets.US_ASCII)); + } + if (BitUtil.check(flags, 3)) { + buf.skipBytes(3); // language identifier + } + if (BitUtil.check(flags, 5)) { + buf.skipBytes(3); // network identifier + } + if (BitUtil.check(flags, 6)) { + buf.readUnsignedShort(); // buffer size + } + if (BitUtil.check(flags, 7)) { + getDeviceSession(channel, remoteAddress, buf.readBytes(15).toString(StandardCharsets.US_ASCII)); + } + + response = ChannelBuffers.dynamicBuffer(ByteOrder.LITTLE_ENDIAN, 0); + response.writeByte(0); // success + sendResponse(channel, PT_APPDATA, index, serviceType, MSG_RESULT_CODE, response); + + } else if (type == MSG_POS_DATA) { position.setTime(new Date((buf.readUnsignedInt() + 1262304000) * 1000)); // since 2010-01-01 position.setLatitude(buf.readUnsignedInt() * 90.0 / 0xFFFFFFFFL); @@ -166,7 +241,9 @@ public class EgtsProtocolDecoder extends BaseProtocolDecoder { buf.readerIndex(end); } - positions.add(position); + if (deviceSession != null) { + positions.add(position); + } } return positions.isEmpty() ? null : positions; diff --git a/test/org/traccar/protocol/EgtsProtocolDecoderTest.java b/test/org/traccar/protocol/EgtsProtocolDecoderTest.java index 9a3434bec..7e5720b39 100644 --- a/test/org/traccar/protocol/EgtsProtocolDecoderTest.java +++ b/test/org/traccar/protocol/EgtsProtocolDecoderTest.java @@ -12,6 +12,9 @@ public class EgtsProtocolDecoderTest extends ProtocolTest { EgtsProtocolDecoder decoder = new EgtsProtocolDecoder(new EgtsProtocol()); + verifyNull(decoder, binary(ByteOrder.LITTLE_ENDIAN, + "0100000b002400a0d601f01900030081030000000101011600030000004238363434393530333436343333373600014cdc")); + verifyPositions(decoder, binary(ByteOrder.LITTLE_ENDIAN, "0100020B0025003A5701C91A003A5701CD6E68490202101700CBB4740F7617FD924364104F116A0000000000010300001EC2"), position("2018-03-21 05:38:19.000", true, 51.67569, 55.59189)); -- cgit v1.2.3 From 094a9acabf8f0b94d1c62f77a409dbf7e3b137f6 Mon Sep 17 00:00:00 2001 From: Anton Tananaev Date: Tue, 27 Mar 2018 00:49:29 +1300 Subject: Improve Watch frame decoding --- src/org/traccar/protocol/WatchFrameDecoder.java | 14 +++++++++----- test/org/traccar/protocol/WatchFrameDecoderTest.java | 4 ++++ 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/src/org/traccar/protocol/WatchFrameDecoder.java b/src/org/traccar/protocol/WatchFrameDecoder.java index 9adea2843..f6b5ef12c 100644 --- a/src/org/traccar/protocol/WatchFrameDecoder.java +++ b/src/org/traccar/protocol/WatchFrameDecoder.java @@ -37,11 +37,6 @@ public class WatchFrameDecoder extends FrameDecoder { int lengthIndex = buf.indexOf(idIndex, buf.writerIndex(), (byte) '*') + 1; if (lengthIndex <= 0) { return null; - } else if (lengthIndex - idIndex > 10 + 1) { - lengthIndex = buf.indexOf(lengthIndex, buf.writerIndex(), (byte) '*') + 1; - if (lengthIndex <= 0) { - return null; - } } int payloadIndex = buf.indexOf(lengthIndex, buf.writerIndex(), (byte) '*'); @@ -49,6 +44,15 @@ public class WatchFrameDecoder extends FrameDecoder { return null; } + if (payloadIndex + 5 < buf.writerIndex() && buf.getByte(payloadIndex + 5) == '*' + && buf.toString(payloadIndex + 1, 4, StandardCharsets.US_ASCII).matches("[0-9A-F]+")) { + lengthIndex = payloadIndex + 1; + payloadIndex = buf.indexOf(lengthIndex, buf.writerIndex(), (byte) '*'); + if (payloadIndex < 0) { + return null; + } + } + int length = Integer.parseInt( buf.toString(lengthIndex, payloadIndex - lengthIndex, StandardCharsets.US_ASCII), 16); if (buf.readableBytes() >= payloadIndex + 1 + length + 1) { diff --git a/test/org/traccar/protocol/WatchFrameDecoderTest.java b/test/org/traccar/protocol/WatchFrameDecoderTest.java index a1eca30bc..741807de2 100644 --- a/test/org/traccar/protocol/WatchFrameDecoderTest.java +++ b/test/org/traccar/protocol/WatchFrameDecoderTest.java @@ -10,6 +10,10 @@ public class WatchFrameDecoderTest extends ProtocolTest { WatchFrameDecoder decoder = new WatchFrameDecoder(); + verifyFrame( + binary("5b33472a3335323636313039303134333135302a303030412a4c4b2c302c302c3130305d"), + decoder.decode(null, null, binary("5b33472a3335323636313039303134333135302a303030412a4c4b2c302c302c3130305d"))); + verifyFrame( binary("5b33472a383330383430363237392a303030382a72636170747572655d"), decoder.decode(null, null, binary("5b33472a383330383430363237392a303030382a72636170747572655d"))); -- cgit v1.2.3 From cf619556709df6d5481d2e4f712ddd6eeebab89d Mon Sep 17 00:00:00 2001 From: Anton Tananaev Date: Tue, 27 Mar 2018 00:55:44 +1300 Subject: Improve Watch data decoding --- src/org/traccar/protocol/WatchFrameDecoder.java | 2 +- src/org/traccar/protocol/WatchProtocolDecoder.java | 10 ++++++---- test/org/traccar/protocol/WatchProtocolDecoderTest.java | 3 +++ 3 files changed, 10 insertions(+), 5 deletions(-) diff --git a/src/org/traccar/protocol/WatchFrameDecoder.java b/src/org/traccar/protocol/WatchFrameDecoder.java index f6b5ef12c..0009ef30f 100644 --- a/src/org/traccar/protocol/WatchFrameDecoder.java +++ b/src/org/traccar/protocol/WatchFrameDecoder.java @@ -45,7 +45,7 @@ public class WatchFrameDecoder extends FrameDecoder { } if (payloadIndex + 5 < buf.writerIndex() && buf.getByte(payloadIndex + 5) == '*' - && buf.toString(payloadIndex + 1, 4, StandardCharsets.US_ASCII).matches("[0-9A-F]+")) { + && buf.toString(payloadIndex + 1, 4, StandardCharsets.US_ASCII).matches("\\p{XDigit}+")) { lengthIndex = payloadIndex + 1; payloadIndex = buf.indexOf(lengthIndex, buf.writerIndex(), (byte) '*'); if (payloadIndex < 0) { diff --git a/src/org/traccar/protocol/WatchProtocolDecoder.java b/src/org/traccar/protocol/WatchProtocolDecoder.java index c57279296..41cd957ae 100644 --- a/src/org/traccar/protocol/WatchProtocolDecoder.java +++ b/src/org/traccar/protocol/WatchProtocolDecoder.java @@ -171,8 +171,8 @@ public class WatchProtocolDecoder extends BaseProtocolDecoder { String manufacturer = buf.readBytes(2).toString(StandardCharsets.US_ASCII); buf.skipBytes(1); // delimiter - int idLength = buf.indexOf(buf.readerIndex(), buf.writerIndex(), (byte) '*') - buf.readerIndex(); - String id = buf.readBytes(idLength).toString(StandardCharsets.US_ASCII); + int idIndex = buf.indexOf(buf.readerIndex(), buf.writerIndex(), (byte) '*'); + String id = buf.readBytes(idIndex - buf.readerIndex()).toString(StandardCharsets.US_ASCII); DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, id); if (deviceSession == null) { return null; @@ -181,7 +181,9 @@ public class WatchProtocolDecoder extends BaseProtocolDecoder { buf.skipBytes(1); // delimiter String index = null; - if (idLength > 10) { + int contentIndex = buf.indexOf(buf.readerIndex(), buf.writerIndex(), (byte) '*'); + if (contentIndex + 5 < buf.writerIndex() && buf.getByte(contentIndex + 5) == '*' + && buf.toString(contentIndex + 1, 4, StandardCharsets.US_ASCII).matches("\\p{XDigit}+")) { int indexLength = buf.indexOf(buf.readerIndex(), buf.writerIndex(), (byte) '*') - buf.readerIndex(); index = buf.readBytes(indexLength).toString(StandardCharsets.US_ASCII); buf.skipBytes(1); // delimiter @@ -192,7 +194,7 @@ public class WatchProtocolDecoder extends BaseProtocolDecoder { buf.writerIndex(buf.writerIndex() - 1); // ignore ending - int contentIndex = buf.indexOf(buf.readerIndex(), buf.writerIndex(), (byte) ','); + contentIndex = buf.indexOf(buf.readerIndex(), buf.writerIndex(), (byte) ','); if (contentIndex < 0) { contentIndex = buf.writerIndex(); } diff --git a/test/org/traccar/protocol/WatchProtocolDecoderTest.java b/test/org/traccar/protocol/WatchProtocolDecoderTest.java index 6ba04bf8e..50f4c2ec0 100644 --- a/test/org/traccar/protocol/WatchProtocolDecoderTest.java +++ b/test/org/traccar/protocol/WatchProtocolDecoderTest.java @@ -10,6 +10,9 @@ public class WatchProtocolDecoderTest extends ProtocolTest { WatchProtocolDecoder decoder = new WatchProtocolDecoder(new WatchProtocol()); + verifyPosition(decoder, buffer( + "[SG*352661090143150*006C*UD,150817,132115,V,28.435142,N,81.354333,W,2.2038,000,99,00,70,100,0,50,00000000,1,1,310,260,1091,30082,139,,00]")); + verifyAttributes(decoder, buffer( "[3G*4700609403*0013*bphrt,120,79,73,,,,]")); -- cgit v1.2.3 From f2d31534703eb7b56a4c1ce82d354fcbb4254266 Mon Sep 17 00:00:00 2001 From: Anton Tananaev Date: Tue, 27 Mar 2018 05:53:40 +1300 Subject: Reimplement CRC8 and CRC16 calculation --- src/org/traccar/helper/Checksum.java | 265 +++++++--------------- src/org/traccar/protocol/EgtsProtocolDecoder.java | 2 +- test/org/traccar/helper/ChecksumTest.java | 15 +- 3 files changed, 100 insertions(+), 182 deletions(-) diff --git a/src/org/traccar/helper/Checksum.java b/src/org/traccar/helper/Checksum.java index c4141e7d2..5aed84bf8 100644 --- a/src/org/traccar/helper/Checksum.java +++ b/src/org/traccar/helper/Checksum.java @@ -1,5 +1,5 @@ /* - * Copyright 2012 - 2015 Anton Tananaev (anton@traccar.org) + * Copyright 2012 - 2018 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. @@ -24,198 +24,109 @@ public final class Checksum { private Checksum() { } - private static final int[] CRC16_CCITT_TABLE_REVERSE = { - 0x0000, 0x1189, 0x2312, 0x329B, 0x4624, 0x57AD, 0x6536, 0x74BF, - 0x8C48, 0x9DC1, 0xAF5A, 0xBED3, 0xCA6C, 0xDBE5, 0xE97E, 0xF8F7, - 0x1081, 0x0108, 0x3393, 0x221A, 0x56A5, 0x472C, 0x75B7, 0x643E, - 0x9CC9, 0x8D40, 0xBFDB, 0xAE52, 0xDAED, 0xCB64, 0xF9FF, 0xE876, - 0x2102, 0x308B, 0x0210, 0x1399, 0x6726, 0x76AF, 0x4434, 0x55BD, - 0xAD4A, 0xBCC3, 0x8E58, 0x9FD1, 0xEB6E, 0xFAE7, 0xC87C, 0xD9F5, - 0x3183, 0x200A, 0x1291, 0x0318, 0x77A7, 0x662E, 0x54B5, 0x453C, - 0xBDCB, 0xAC42, 0x9ED9, 0x8F50, 0xFBEF, 0xEA66, 0xD8FD, 0xC974, - 0x4204, 0x538D, 0x6116, 0x709F, 0x0420, 0x15A9, 0x2732, 0x36BB, - 0xCE4C, 0xDFC5, 0xED5E, 0xFCD7, 0x8868, 0x99E1, 0xAB7A, 0xBAF3, - 0x5285, 0x430C, 0x7197, 0x601E, 0x14A1, 0x0528, 0x37B3, 0x263A, - 0xDECD, 0xCF44, 0xFDDF, 0xEC56, 0x98E9, 0x8960, 0xBBFB, 0xAA72, - 0x6306, 0x728F, 0x4014, 0x519D, 0x2522, 0x34AB, 0x0630, 0x17B9, - 0xEF4E, 0xFEC7, 0xCC5C, 0xDDD5, 0xA96A, 0xB8E3, 0x8A78, 0x9BF1, - 0x7387, 0x620E, 0x5095, 0x411C, 0x35A3, 0x242A, 0x16B1, 0x0738, - 0xFFCF, 0xEE46, 0xDCDD, 0xCD54, 0xB9EB, 0xA862, 0x9AF9, 0x8B70, - 0x8408, 0x9581, 0xA71A, 0xB693, 0xC22C, 0xD3A5, 0xE13E, 0xF0B7, - 0x0840, 0x19C9, 0x2B52, 0x3ADB, 0x4E64, 0x5FED, 0x6D76, 0x7CFF, - 0x9489, 0x8500, 0xB79B, 0xA612, 0xD2AD, 0xC324, 0xF1BF, 0xE036, - 0x18C1, 0x0948, 0x3BD3, 0x2A5A, 0x5EE5, 0x4F6C, 0x7DF7, 0x6C7E, - 0xA50A, 0xB483, 0x8618, 0x9791, 0xE32E, 0xF2A7, 0xC03C, 0xD1B5, - 0x2942, 0x38CB, 0x0A50, 0x1BD9, 0x6F66, 0x7EEF, 0x4C74, 0x5DFD, - 0xB58B, 0xA402, 0x9699, 0x8710, 0xF3AF, 0xE226, 0xD0BD, 0xC134, - 0x39C3, 0x284A, 0x1AD1, 0x0B58, 0x7FE7, 0x6E6E, 0x5CF5, 0x4D7C, - 0xC60C, 0xD785, 0xE51E, 0xF497, 0x8028, 0x91A1, 0xA33A, 0xB2B3, - 0x4A44, 0x5BCD, 0x6956, 0x78DF, 0x0C60, 0x1DE9, 0x2F72, 0x3EFB, - 0xD68D, 0xC704, 0xF59F, 0xE416, 0x90A9, 0x8120, 0xB3BB, 0xA232, - 0x5AC5, 0x4B4C, 0x79D7, 0x685E, 0x1CE1, 0x0D68, 0x3FF3, 0x2E7A, - 0xE70E, 0xF687, 0xC41C, 0xD595, 0xA12A, 0xB0A3, 0x8238, 0x93B1, - 0x6B46, 0x7ACF, 0x4854, 0x59DD, 0x2D62, 0x3CEB, 0x0E70, 0x1FF9, - 0xF78F, 0xE606, 0xD49D, 0xC514, 0xB1AB, 0xA022, 0x92B9, 0x8330, - 0x7BC7, 0x6A4E, 0x58D5, 0x495C, 0x3DE3, 0x2C6A, 0x1EF1, 0x0F78 - }; - - private static final int[] CRC16_CCITT_TABLE = { - 0x0000, 0x1021, 0x2042, 0x3063, 0x4084, 0x50A5, 0x60C6, 0x70E7, - 0x8108, 0x9129, 0xA14A, 0xB16B, 0xC18C, 0xD1AD, 0xE1CE, 0xF1EF, - 0x1231, 0x0210, 0x3273, 0x2252, 0x52B5, 0x4294, 0x72F7, 0x62D6, - 0x9339, 0x8318, 0xB37B, 0xA35A, 0xD3BD, 0xC39C, 0xF3FF, 0xE3DE, - 0x2462, 0x3443, 0x0420, 0x1401, 0x64E6, 0x74C7, 0x44A4, 0x5485, - 0xA56A, 0xB54B, 0x8528, 0x9509, 0xE5EE, 0xF5CF, 0xC5AC, 0xD58D, - 0x3653, 0x2672, 0x1611, 0x0630, 0x76D7, 0x66F6, 0x5695, 0x46B4, - 0xB75B, 0xA77A, 0x9719, 0x8738, 0xF7DF, 0xE7FE, 0xD79D, 0xC7BC, - 0x48C4, 0x58E5, 0x6886, 0x78A7, 0x0840, 0x1861, 0x2802, 0x3823, - 0xC9CC, 0xD9ED, 0xE98E, 0xF9AF, 0x8948, 0x9969, 0xA90A, 0xB92B, - 0x5AF5, 0x4AD4, 0x7AB7, 0x6A96, 0x1A71, 0x0A50, 0x3A33, 0x2A12, - 0xDBFD, 0xCBDC, 0xFBBF, 0xEB9E, 0x9B79, 0x8B58, 0xBB3B, 0xAB1A, - 0x6CA6, 0x7C87, 0x4CE4, 0x5CC5, 0x2C22, 0x3C03, 0x0C60, 0x1C41, - 0xEDAE, 0xFD8F, 0xCDEC, 0xDDCD, 0xAD2A, 0xBD0B, 0x8D68, 0x9D49, - 0x7E97, 0x6EB6, 0x5ED5, 0x4EF4, 0x3E13, 0x2E32, 0x1E51, 0x0E70, - 0xFF9F, 0xEFBE, 0xDFDD, 0xCFFC, 0xBF1B, 0xAF3A, 0x9F59, 0x8F78, - 0x9188, 0x81A9, 0xB1CA, 0xA1EB, 0xD10C, 0xC12D, 0xF14E, 0xE16F, - 0x1080, 0x00A1, 0x30C2, 0x20E3, 0x5004, 0x4025, 0x7046, 0x6067, - 0x83B9, 0x9398, 0xA3FB, 0xB3DA, 0xC33D, 0xD31C, 0xE37F, 0xF35E, - 0x02B1, 0x1290, 0x22F3, 0x32D2, 0x4235, 0x5214, 0x6277, 0x7256, - 0xB5EA, 0xA5CB, 0x95A8, 0x8589, 0xF56E, 0xE54F, 0xD52C, 0xC50D, - 0x34E2, 0x24C3, 0x14A0, 0x0481, 0x7466, 0x6447, 0x5424, 0x4405, - 0xA7DB, 0xB7FA, 0x8799, 0x97B8, 0xE75F, 0xF77E, 0xC71D, 0xD73C, - 0x26D3, 0x36F2, 0x0691, 0x16B0, 0x6657, 0x7676, 0x4615, 0x5634, - 0xD94C, 0xC96D, 0xF90E, 0xE92F, 0x99C8, 0x89E9, 0xB98A, 0xA9AB, - 0x5844, 0x4865, 0x7806, 0x6827, 0x18C0, 0x08E1, 0x3882, 0x28A3, - 0xCB7D, 0xDB5C, 0xEB3F, 0xFB1E, 0x8BF9, 0x9BD8, 0xABBB, 0xBB9A, - 0x4A75, 0x5A54, 0x6A37, 0x7A16, 0x0AF1, 0x1AD0, 0x2AB3, 0x3A92, - 0xFD2E, 0xED0F, 0xDD6C, 0xCD4D, 0xBDAA, 0xAD8B, 0x9DE8, 0x8DC9, - 0x7C26, 0x6C07, 0x5C64, 0x4C45, 0x3CA2, 0x2C83, 0x1CE0, 0x0CC1, - 0xEF1F, 0xFF3E, 0xCF5D, 0xDF7C, 0xAF9B, 0xBFBA, 0x8FD9, 0x9FF8, - 0x6E17, 0x7E36, 0x4E55, 0x5E74, 0x2E93, 0x3EB2, 0x0ED1, 0x1EF0 - }; + public static class Algorithm { + + private int poly; + private int init; + private boolean refIn; + private boolean refOut; + private int xorOut; + private int[] table; + + public Algorithm(int bits, int poly, int init, boolean refIn, boolean refOut, int xorOut) { + this.poly = poly; + this.init = init; + this.refIn = refIn; + this.refOut = refOut; + this.xorOut = xorOut; + this.table = bits == 8 ? initTable8() : initTable16(); + } - private static final int[] CRC16_IBM_TABLE = { - 0x0000, 0xc0c1, 0xc181, 0x0140, 0xc301, 0x03c0, 0x0280, 0xc241, - 0xc601, 0x06c0, 0x0780, 0xc741, 0x0500, 0xc5c1, 0xc481, 0x0440, - 0xcc01, 0x0cc0, 0x0d80, 0xcd41, 0x0f00, 0xcfc1, 0xce81, 0x0e40, - 0x0a00, 0xcac1, 0xcb81, 0x0b40, 0xc901, 0x09c0, 0x0880, 0xc841, - 0xd801, 0x18c0, 0x1980, 0xd941, 0x1b00, 0xdbc1, 0xda81, 0x1a40, - 0x1e00, 0xdec1, 0xdf81, 0x1f40, 0xdd01, 0x1dc0, 0x1c80, 0xdc41, - 0x1400, 0xd4c1, 0xd581, 0x1540, 0xd701, 0x17c0, 0x1680, 0xd641, - 0xd201, 0x12c0, 0x1380, 0xd341, 0x1100, 0xd1c1, 0xd081, 0x1040, - 0xf001, 0x30c0, 0x3180, 0xf141, 0x3300, 0xf3c1, 0xf281, 0x3240, - 0x3600, 0xf6c1, 0xf781, 0x3740, 0xf501, 0x35c0, 0x3480, 0xf441, - 0x3c00, 0xfcc1, 0xfd81, 0x3d40, 0xff01, 0x3fc0, 0x3e80, 0xfe41, - 0xfa01, 0x3ac0, 0x3b80, 0xfb41, 0x3900, 0xf9c1, 0xf881, 0x3840, - 0x2800, 0xe8c1, 0xe981, 0x2940, 0xeb01, 0x2bc0, 0x2a80, 0xea41, - 0xee01, 0x2ec0, 0x2f80, 0xef41, 0x2d00, 0xedc1, 0xec81, 0x2c40, - 0xe401, 0x24c0, 0x2580, 0xe541, 0x2700, 0xe7c1, 0xe681, 0x2640, - 0x2200, 0xe2c1, 0xe381, 0x2340, 0xe101, 0x21c0, 0x2080, 0xe041, - 0xa001, 0x60c0, 0x6180, 0xa141, 0x6300, 0xa3c1, 0xa281, 0x6240, - 0x6600, 0xa6c1, 0xa781, 0x6740, 0xa501, 0x65c0, 0x6480, 0xa441, - 0x6c00, 0xacc1, 0xad81, 0x6d40, 0xaf01, 0x6fc0, 0x6e80, 0xae41, - 0xaa01, 0x6ac0, 0x6b80, 0xab41, 0x6900, 0xa9c1, 0xa881, 0x6840, - 0x7800, 0xb8c1, 0xb981, 0x7940, 0xbb01, 0x7bc0, 0x7a80, 0xba41, - 0xbe01, 0x7ec0, 0x7f80, 0xbf41, 0x7d00, 0xbdc1, 0xbc81, 0x7c40, - 0xb401, 0x74c0, 0x7580, 0xb541, 0x7700, 0xb7c1, 0xb681, 0x7640, - 0x7200, 0xb2c1, 0xb381, 0x7340, 0xb101, 0x71c0, 0x7080, 0xb041, - 0x5000, 0x90c1, 0x9181, 0x5140, 0x9301, 0x53c0, 0x5280, 0x9241, - 0x9601, 0x56c0, 0x5780, 0x9741, 0x5500, 0x95c1, 0x9481, 0x5440, - 0x9c01, 0x5cc0, 0x5d80, 0x9d41, 0x5f00, 0x9fc1, 0x9e81, 0x5e40, - 0x5a00, 0x9ac1, 0x9b81, 0x5b40, 0x9901, 0x59c0, 0x5880, 0x9841, - 0x8801, 0x48c0, 0x4980, 0x8941, 0x4b00, 0x8bc1, 0x8a81, 0x4a40, - 0x4e00, 0x8ec1, 0x8f81, 0x4f40, 0x8d01, 0x4dc0, 0x4c80, 0x8c41, - 0x4400, 0x84c1, 0x8581, 0x4540, 0x8701, 0x47c0, 0x4680, 0x8641, - 0x8201, 0x42c0, 0x4380, 0x8341, 0x4100, 0x81c1, 0x8081, 0x4040, - }; + private int[] initTable8() { + int[] table = new int[256]; + int crc; + for (int i = 0; i < 256; i++) { + crc = i; + for (int j = 0; j < 8; j++) { + boolean bit = (crc & 0x80) != 0; + crc <<= 1; + if (bit) { + crc ^= poly; + } + } + table[i] = crc & 0xFF; + } + return table; + } - private static final int[] CRC8_TABLE = { - 0x00, 0x31, 0x62, 0x53, 0xC4, 0xF5, 0xA6, 0x97, - 0xB9, 0x88, 0xDB, 0xEA, 0x7D, 0x4C, 0x1F, 0x2E, - 0x43, 0x72, 0x21, 0x10, 0x87, 0xB6, 0xE5, 0xD4, - 0xFA, 0xCB, 0x98, 0xA9, 0x3E, 0x0F, 0x5C, 0x6D, - 0x86, 0xB7, 0xE4, 0xD5, 0x42, 0x73, 0x20, 0x11, 0x3F, - 0x0E, 0x5D, 0x6C, 0xFB, 0xCA, 0x99, 0xA8, 0xC5, 0xF4, 0xA7, 0x96, - 0x01, 0x30, 0x63, 0x52, 0x7C, 0x4D, 0x1E, 0x2F, 0xB8, 0x89, 0xDA, - 0xEB, 0x3D, 0x0C, 0x5F, 0x6E, 0xF9, 0xC8, 0x9B, 0xAA, 0x84, 0xB5, - 0xE6, 0xD7, 0x40, 0x71, 0x22, 0x13, 0x7E, 0x4F, 0x1C, 0x2D, 0xBA, - 0x8B, 0xD8, 0xE9, 0xC7, 0xF6, 0xA5, 0x94, 0x03, 0x32, 0x61, 0x50, - 0xBB, 0x8A, 0xD9, 0xE8, 0x7F, 0x4E, 0x1D, 0x2C, 0x02, 0x33, 0x60, - 0x51, 0xC6, 0xF7, 0xA4, 0x95, 0xF8, 0xC9, 0x9A, 0xAB, 0x3C, 0x0D, - 0x5E, 0x6F, 0x41, 0x70, 0x23, 0x12, 0x85, 0xB4, 0xE7, 0xD6, 0x7A, - 0x4B, 0x18, 0x29, 0xBE, 0x8F, 0xDC, 0xED, 0xC3, 0xF2, 0xA1, 0x90, - 0x07, 0x36, 0x65, 0x54, 0x39, 0x08, 0x5B, 0x6A, 0xFD, 0xCC, 0x9F, - 0xAE, 0x80, 0xB1, 0xE2, 0xD3, 0x44, 0x75, 0x26, 0x17, 0xFC, 0xCD, - 0x9E, 0xAF, 0x38, 0x09, 0x5A, 0x6B, 0x45, 0x74, 0x27, 0x16, 0x81, - 0xB0, 0xE3, 0xD2, 0xBF, 0x8E, 0xDD, 0xEC, 0x7B, 0x4A, 0x19, 0x28, - 0x06, 0x37, 0x64, 0x55, 0xC2, 0xF3, 0xA0, 0x91, 0x47, 0x76, 0x25, - 0x14, 0x83, 0xB2, 0xE1, 0xD0, 0xFE, 0xCF, 0x9C, 0xAD, 0x3A, 0x0B, - 0x58, 0x69, 0x04, 0x35, 0x66, 0x57, 0xC0, 0xF1, 0xA2, 0x93, 0xBD, - 0x8C, 0xDF, 0xEE, 0x79, 0x48, 0x1B, 0x2A, 0xC1, 0xF0, 0xA3, 0x92, - 0x05, 0x34, 0x67, 0x56, 0x78, 0x49, 0x1A, 0x2B, 0xBC, 0x8D, 0xDE, - 0xEF, 0x82, 0xB3, 0xE0, 0xD1, 0x46, 0x77, 0x24, 0x15, 0x3B, 0x0A, - 0x59, 0x68, 0xFF, 0xCE, 0x9D, 0xAC }; + private int[] initTable16() { + int[] table = new int[256]; + int crc; + for (int i = 0; i < 256; i++) { + crc = i << 8; + for (int j = 0; j < 8; j++) { + boolean bit = (crc & 0x8000) != 0; + crc <<= 1; + if (bit) { + crc ^= poly; + } + } + table[i] = crc & 0xFFFF; + } + return table; + } - // More info: http://reveng.sourceforge.net/crc-catalogue/16.htm - public static final String CRC16_IBM = "IBM"; - public static final String CRC16_MODBUS = "MODBUS"; - public static final String CRC16_X25 = "X-25"; - public static final String CRC16_CCITT_FALSE = "CCITT-FALSE"; - public static final String CRC16_KERMIT = "KERMIT"; - public static final String CRC16_XMODEM = "XMODEM"; - public static final String CRC16_AUG_CCITT = "AUG-CCITT"; - public static final String CRC16_GENIBUS = "GENIBUS"; - public static final String CRC16_MCRF4XX = "MCRF4XX"; + } - private static int crc16Unreflected(ByteBuffer buf, int crcIn, int[] table) { - int crc16 = crcIn; - while (buf.hasRemaining()) { - crc16 = table[((crc16 >> 8) ^ buf.get()) & 0xff] ^ (crc16 << 8); + private static int reverse(int value, int bits) { + int result = 0; + for (int i = 0; i < bits; i++) { + result = (result << 1) | (value & 1); + value >>= 1; } - return crc16 & 0xFFFF; + return result; } - private static int crc16Reflected(ByteBuffer buf, int crcIn, int[] table) { - int crc16 = crcIn; + public static int crc8(Algorithm algorithm, ByteBuffer buf) { + int crc = algorithm.init; while (buf.hasRemaining()) { - crc16 = table[(crc16 ^ buf.get()) & 0xff] ^ (crc16 >> 8); + int b = buf.get() & 0xFF; + if (algorithm.refIn) { + b = reverse(b, 8); + } + crc = algorithm.table[(crc & 0xFF) ^ b]; } - return crc16 & 0xFFFF; - } - - public static int crc16(String type, ByteBuffer buf) { - switch (type) { - case CRC16_IBM: - return crc16Reflected(buf, 0, CRC16_IBM_TABLE); - case CRC16_MODBUS: - return crc16Reflected(buf, 0xFFFF, CRC16_IBM_TABLE); - case CRC16_X25: - return crc16Reflected(buf, 0xFFFF, CRC16_CCITT_TABLE_REVERSE) ^ 0xFFFF; - case CRC16_CCITT_FALSE: - return crc16Unreflected(buf, 0xFFFF, CRC16_CCITT_TABLE); - case CRC16_KERMIT: - return crc16Reflected(buf, 0, CRC16_CCITT_TABLE_REVERSE); - case CRC16_XMODEM: - return crc16Unreflected(buf, 0, CRC16_CCITT_TABLE); - case CRC16_AUG_CCITT: - return crc16Unreflected(buf, 0x1d0f, CRC16_CCITT_TABLE); - case CRC16_GENIBUS: - return crc16Unreflected(buf, 0xFFFF, CRC16_CCITT_TABLE) ^ 0xFFFF; - case CRC16_MCRF4XX: - return crc16Reflected(buf, 0xFFFF, CRC16_CCITT_TABLE_REVERSE); - default: - throw new UnsupportedOperationException(type); + if (algorithm.refOut) { + crc = reverse(crc, 8); } + return (crc ^ algorithm.xorOut) & 0xFF; } - public static int crc8(ByteBuffer buf) { - int crc = 0xFF; + public static int crc16(Algorithm algorithm, ByteBuffer buf) { + int crc = algorithm.init; while (buf.hasRemaining()) { - crc = CRC8_TABLE[(crc ^ buf.get()) & 0xFF]; + int b = buf.get() & 0xFF; + if (algorithm.refIn) { + b = reverse(b, 8); + } + crc = (crc << 8) ^ algorithm.table[((crc >> 8) & 0xFF) ^ b]; + } + if (algorithm.refOut) { + crc = reverse(crc, 16); } - return crc & 0xFF; + return (crc ^ algorithm.xorOut) & 0xFFFF; } + public static final Algorithm CRC8_EGTS = new Algorithm(8, 0x31, 0xFF, false, false, 0x00); + public static final Algorithm CRC8_ROHC = new Algorithm(8, 0x07, 0xFF, true, true, 0x00); + + public static final Algorithm CRC16_IBM = new Algorithm(16, 0x8005, 0x0000, true, true, 0x0000); + public static final Algorithm CRC16_X25 = new Algorithm(16, 0x1021, 0xFFFF, true, true, 0xFFFF); + public static final Algorithm CRC16_MODBUS = new Algorithm(16, 0x8005, 0xFFFF, true, true, 0x0000); + public static final Algorithm CRC16_CCITT_FALSE = new Algorithm(16, 0x1021, 0xFFFF, false, false, 0x0000); + public static final Algorithm CRC16_KERMIT = new Algorithm(16, 0x1021, 0x0000, true, true, 0x0000); + public static final Algorithm CRC16_XMODEM = new Algorithm(16, 0x1021, 0x0000, false, false, 0x0000); + public static int crc32(ByteBuffer buf) { CRC32 checksum = new CRC32(); while (buf.hasRemaining()) { diff --git a/src/org/traccar/protocol/EgtsProtocolDecoder.java b/src/org/traccar/protocol/EgtsProtocolDecoder.java index 85376b1a5..fe82a51ed 100644 --- a/src/org/traccar/protocol/EgtsProtocolDecoder.java +++ b/src/org/traccar/protocol/EgtsProtocolDecoder.java @@ -97,7 +97,7 @@ public class EgtsProtocolDecoder extends BaseProtocolDecoder { response.writeShort(record.readableBytes()); response.writeShort(packetId++); response.writeByte(packetType); - response.writeByte(Checksum.crc8(response.toByteBuffer())); + response.writeByte(Checksum.crc8(Checksum.CRC8_EGTS, response.toByteBuffer())); response.writeBytes(record); response.writeShort(recordChecksum); diff --git a/test/org/traccar/helper/ChecksumTest.java b/test/org/traccar/helper/ChecksumTest.java index a7c66a2a2..3bd51448d 100644 --- a/test/org/traccar/helper/ChecksumTest.java +++ b/test/org/traccar/helper/ChecksumTest.java @@ -10,19 +10,26 @@ import static org.junit.Assert.assertEquals; public class ChecksumTest { + @Test + public void testCrc8() { + ChannelBuffer buf = ChannelBuffers.copiedBuffer("123456789", StandardCharsets.US_ASCII); + + assertEquals(0xF7, Checksum.crc8(Checksum.CRC8_EGTS, buf.toByteBuffer())); + assertEquals(0xD0, Checksum.crc8(Checksum.CRC8_ROHC, buf.toByteBuffer())); + } + @Test public void testCrc16() { ChannelBuffer buf = ChannelBuffers.copiedBuffer("123456789", StandardCharsets.US_ASCII); + assertEquals(0xBB3D, Checksum.crc16(Checksum.CRC16_IBM, buf.toByteBuffer())); + assertEquals(0x4B37, Checksum.crc16(Checksum.CRC16_MODBUS, buf.toByteBuffer())); assertEquals(0x906e, Checksum.crc16(Checksum.CRC16_X25, buf.toByteBuffer())); assertEquals(0x29b1, Checksum.crc16(Checksum.CRC16_CCITT_FALSE, buf.toByteBuffer())); assertEquals(0x2189, Checksum.crc16(Checksum.CRC16_KERMIT, buf.toByteBuffer())); assertEquals(0x31c3, Checksum.crc16(Checksum.CRC16_XMODEM, buf.toByteBuffer())); - assertEquals(0xe5cc, Checksum.crc16(Checksum.CRC16_AUG_CCITT, buf.toByteBuffer())); - assertEquals(0xd64e, Checksum.crc16(Checksum.CRC16_GENIBUS, buf.toByteBuffer())); - assertEquals(0x6f91, Checksum.crc16(Checksum.CRC16_MCRF4XX, buf.toByteBuffer())); } - + @Test public void testLuhn() { assertEquals(7, Checksum.luhn(12345678901234L)); -- cgit v1.2.3 From a959b3d754a2bd9e0f8a3b73b01061c88fe46053 Mon Sep 17 00:00:00 2001 From: Anton Tananaev Date: Tue, 27 Mar 2018 05:55:40 +1300 Subject: Implement RoboTrack protocol --- setup/default.xml | 1 + .../traccar/protocol/RoboTrackFrameDecoder.java | 57 +++++++++ src/org/traccar/protocol/RoboTrackProtocol.java | 45 +++++++ .../traccar/protocol/RoboTrackProtocolDecoder.java | 130 +++++++++++++++++++++ .../protocol/RoboTrackFrameDecoderTest.java | 19 +++ .../protocol/RoboTrackProtocolDecoderTest.java | 18 +++ 6 files changed, 270 insertions(+) create mode 100644 src/org/traccar/protocol/RoboTrackFrameDecoder.java create mode 100644 src/org/traccar/protocol/RoboTrackProtocol.java create mode 100644 src/org/traccar/protocol/RoboTrackProtocolDecoder.java create mode 100644 test/org/traccar/protocol/RoboTrackFrameDecoderTest.java create mode 100644 test/org/traccar/protocol/RoboTrackProtocolDecoderTest.java diff --git a/setup/default.xml b/setup/default.xml index 1788cb901..7996ce716 100644 --- a/setup/default.xml +++ b/setup/default.xml @@ -235,5 +235,6 @@ 5160 5161 5162 + 5163 diff --git a/src/org/traccar/protocol/RoboTrackFrameDecoder.java b/src/org/traccar/protocol/RoboTrackFrameDecoder.java new file mode 100644 index 000000000..af215103c --- /dev/null +++ b/src/org/traccar/protocol/RoboTrackFrameDecoder.java @@ -0,0 +1,57 @@ +/* + * Copyright 2018 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 org.jboss.netty.buffer.ChannelBuffer; +import org.jboss.netty.channel.Channel; +import org.jboss.netty.channel.ChannelHandlerContext; +import org.jboss.netty.handler.codec.frame.FrameDecoder; + +public class RoboTrackFrameDecoder extends FrameDecoder { + + private int messageLength(ChannelBuffer buf) { + switch ((int) buf.getByte(buf.readerIndex())) { + case RoboTrackProtocolDecoder.MSG_ID: + return 69; + case RoboTrackProtocolDecoder.MSG_ACK: + return 3; + case RoboTrackProtocolDecoder.MSG_GPS: + case RoboTrackProtocolDecoder.MSG_GSM: + case RoboTrackProtocolDecoder.MSG_IMAGE_START: + return 24; + case RoboTrackProtocolDecoder.MSG_IMAGE_DATA: + return 8 + buf.getUnsignedShort(buf.readerIndex() + 1); + case RoboTrackProtocolDecoder.MSG_IMAGE_END: + return 6; + default: + return Integer.MAX_VALUE; + } + } + + @Override + protected Object decode( + ChannelHandlerContext ctx, Channel channel, ChannelBuffer buf) throws Exception { + + int length = messageLength(buf); + + if (buf.readableBytes() >= length) { + return buf.readBytes(length); + } + + return null; + } + +} diff --git a/src/org/traccar/protocol/RoboTrackProtocol.java b/src/org/traccar/protocol/RoboTrackProtocol.java new file mode 100644 index 000000000..382cb1c2f --- /dev/null +++ b/src/org/traccar/protocol/RoboTrackProtocol.java @@ -0,0 +1,45 @@ +/* + * Copyright 2018 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 org.jboss.netty.bootstrap.ServerBootstrap; +import org.jboss.netty.channel.ChannelPipeline; +import org.traccar.BaseProtocol; +import org.traccar.TrackerServer; + +import java.nio.ByteOrder; +import java.util.List; + +public class RoboTrackProtocol extends BaseProtocol { + + public RoboTrackProtocol() { + super("robotrack"); + } + + @Override + public void initTrackerServers(List serverList) { + TrackerServer server = new TrackerServer(new ServerBootstrap(), getName()) { + @Override + protected void addSpecificHandlers(ChannelPipeline pipeline) { + pipeline.addLast("frameDecoder", new RoboTrackFrameDecoder()); + pipeline.addLast("objectDecoder", new RoboTrackProtocolDecoder(RoboTrackProtocol.this)); + } + }; + server.setEndianness(ByteOrder.LITTLE_ENDIAN); + serverList.add(server); + } + +} diff --git a/src/org/traccar/protocol/RoboTrackProtocolDecoder.java b/src/org/traccar/protocol/RoboTrackProtocolDecoder.java new file mode 100644 index 000000000..2244ea716 --- /dev/null +++ b/src/org/traccar/protocol/RoboTrackProtocolDecoder.java @@ -0,0 +1,130 @@ +/* + * Copyright 2018 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 org.jboss.netty.buffer.ChannelBuffer; +import org.jboss.netty.buffer.ChannelBuffers; +import org.jboss.netty.channel.Channel; +import org.traccar.BaseProtocolDecoder; +import org.traccar.DeviceSession; +import org.traccar.helper.BitUtil; +import org.traccar.helper.Checksum; +import org.traccar.helper.UnitsConverter; +import org.traccar.model.CellTower; +import org.traccar.model.Network; +import org.traccar.model.Position; + +import java.net.SocketAddress; +import java.nio.charset.StandardCharsets; +import java.util.Date; + +public class RoboTrackProtocolDecoder extends BaseProtocolDecoder { + + public RoboTrackProtocolDecoder(RoboTrackProtocol protocol) { + super(protocol); + } + + public static final int MSG_ID = 0x00; + public static final int MSG_ACK = 0x80; + public static final int MSG_GPS = 0x03; + public static final int MSG_GSM = 0x04; + public static final int MSG_IMAGE_START = 0x06; + public static final int MSG_IMAGE_DATA = 0x07; + public static final int MSG_IMAGE_END = 0x08; + + @Override + protected Object decode( + Channel channel, SocketAddress remoteAddress, Object msg) throws Exception { + + ChannelBuffer buf = (ChannelBuffer) msg; + + int type = buf.readUnsignedByte(); + + if (type == MSG_ID) { + + buf.skipBytes(16); // name + + String imei = buf.readBytes(15).toString(StandardCharsets.US_ASCII); + + if (getDeviceSession(channel, remoteAddress, imei) != null && channel != null) { + ChannelBuffer response = ChannelBuffers.dynamicBuffer(); + buf.writeByte(MSG_ACK); + buf.writeByte(0x01); // success + response.writeByte(Checksum.crc8(Checksum.CRC8_ROHC, response.toByteBuffer())); + channel.write(response); + } + + } else if (type == MSG_GPS || type == MSG_GSM) { + + DeviceSession deviceSession = getDeviceSession(channel, remoteAddress); + if (deviceSession == null) { + return null; + } + + Position position = new Position(getProtocolName()); + position.setDeviceId(deviceSession.getDeviceId()); + + position.setDeviceTime(new Date(buf.readUnsignedInt() * 1000)); + + if (type == MSG_GPS) { + + position.setValid(true); + position.setFixTime(position.getDeviceTime()); + position.setLatitude(buf.readInt() * 0.000001); + position.setLongitude(buf.readInt() * 0.000001); + position.setSpeed(UnitsConverter.knotsFromKph(buf.readByte())); + + } else { + + getLastLocation(position, position.getDeviceTime()); + + position.setNetwork(new Network(CellTower.from( + buf.readUnsignedShort(), buf.readUnsignedShort(), + buf.readUnsignedShort(), buf.readUnsignedShort()))); + + buf.readUnsignedByte(); // reserved + + } + + int value = buf.readUnsignedByte(); + + position.set(Position.KEY_SATELLITES, BitUtil.to(value, 4)); + position.set(Position.KEY_RSSI, BitUtil.between(value, 4, 7)); + position.set(Position.KEY_MOTION, BitUtil.check(value, 7)); + + value = buf.readUnsignedByte(); + + position.set(Position.KEY_CHARGE, BitUtil.check(value, 0)); + + for (int i = 1; i <= 4; i++) { + position.set(Position.PREFIX_IN + i, BitUtil.check(value, i)); + } + + position.set(Position.KEY_BATTERY_LEVEL, BitUtil.from(value, 5) * 100 / 7); + position.set(Position.KEY_DEVICE_TEMP, buf.readByte()); + + for (int i = 1; i <= 3; i++) { + position.set(Position.PREFIX_ADC + i, buf.readUnsignedShort()); + } + + return position; + + } + + return null; + } + +} diff --git a/test/org/traccar/protocol/RoboTrackFrameDecoderTest.java b/test/org/traccar/protocol/RoboTrackFrameDecoderTest.java new file mode 100644 index 000000000..2e3853f86 --- /dev/null +++ b/test/org/traccar/protocol/RoboTrackFrameDecoderTest.java @@ -0,0 +1,19 @@ +package org.traccar.protocol; + +import org.junit.Test; +import org.traccar.ProtocolTest; + +public class RoboTrackFrameDecoderTest extends ProtocolTest { + + @Test + public void testDecode() throws Exception { + + RoboTrackFrameDecoder decoder = new RoboTrackFrameDecoder(); + + verifyFrame( + binary("00524f424f545241434b00000000000000383638323034303032323533343136313233343536373839303132312e313261000000312e353761000000312e3030000000003e"), + decoder.decode(null, null, binary("00524f424f545241434b00000000000000383638323034303032323533343136313233343536373839303132312e313261000000312e353761000000312e3030000000003e"))); + + } + +} diff --git a/test/org/traccar/protocol/RoboTrackProtocolDecoderTest.java b/test/org/traccar/protocol/RoboTrackProtocolDecoderTest.java new file mode 100644 index 000000000..b65d9974b --- /dev/null +++ b/test/org/traccar/protocol/RoboTrackProtocolDecoderTest.java @@ -0,0 +1,18 @@ +package org.traccar.protocol; + +import org.junit.Test; +import org.traccar.ProtocolTest; + +public class RoboTrackProtocolDecoderTest extends ProtocolTest { + + @Test + public void testDecode() throws Exception { + + RoboTrackProtocolDecoder decoder = new RoboTrackProtocolDecoder(new RoboTrackProtocol()); + + verifyNull(decoder, binary( + "00524f424f545241434b00000000000000383638323034303032323533343136313233343536373839303132312e313261000000312e353761000000312e3030000000003e")); + + } + +} -- cgit v1.2.3 From b9b2217b2acd3d3e0a202ea0be59bb88e45a4da8 Mon Sep 17 00:00:00 2001 From: Anton Tananaev Date: Tue, 27 Mar 2018 07:15:36 +1300 Subject: Fix EGTS decoding issues --- src/org/traccar/protocol/EgtsProtocolDecoder.java | 11 +++++++---- test/org/traccar/protocol/EgtsProtocolDecoderTest.java | 6 ++++++ 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/src/org/traccar/protocol/EgtsProtocolDecoder.java b/src/org/traccar/protocol/EgtsProtocolDecoder.java index fe82a51ed..e13f18fed 100644 --- a/src/org/traccar/protocol/EgtsProtocolDecoder.java +++ b/src/org/traccar/protocol/EgtsProtocolDecoder.java @@ -162,10 +162,12 @@ public class EgtsProtocolDecoder extends BaseProtocolDecoder { buf.readUnsignedShort(); // home dispatcher identifier } if (BitUtil.check(flags, 1)) { - getDeviceSession(channel, remoteAddress, buf.readBytes(15).toString(StandardCharsets.US_ASCII)); + getDeviceSession( + channel, remoteAddress, buf.readBytes(15).toString(StandardCharsets.US_ASCII).trim()); } if (BitUtil.check(flags, 2)) { - getDeviceSession(channel, remoteAddress, buf.readBytes(16).toString(StandardCharsets.US_ASCII)); + getDeviceSession( + channel, remoteAddress, buf.readBytes(16).toString(StandardCharsets.US_ASCII).trim()); } if (BitUtil.check(flags, 3)) { buf.skipBytes(3); // language identifier @@ -177,7 +179,8 @@ public class EgtsProtocolDecoder extends BaseProtocolDecoder { buf.readUnsignedShort(); // buffer size } if (BitUtil.check(flags, 7)) { - getDeviceSession(channel, remoteAddress, buf.readBytes(15).toString(StandardCharsets.US_ASCII)); + getDeviceSession( + channel, remoteAddress, buf.readBytes(15).toString(StandardCharsets.US_ASCII).trim()); } response = ChannelBuffers.dynamicBuffer(ByteOrder.LITTLE_ENDIAN, 0); @@ -241,7 +244,7 @@ public class EgtsProtocolDecoder extends BaseProtocolDecoder { buf.readerIndex(end); } - if (deviceSession != null) { + if (serviceType == SERVICE_TELEDATA && deviceSession != null) { positions.add(position); } } diff --git a/test/org/traccar/protocol/EgtsProtocolDecoderTest.java b/test/org/traccar/protocol/EgtsProtocolDecoderTest.java index 7e5720b39..af69f321e 100644 --- a/test/org/traccar/protocol/EgtsProtocolDecoderTest.java +++ b/test/org/traccar/protocol/EgtsProtocolDecoderTest.java @@ -12,9 +12,15 @@ public class EgtsProtocolDecoderTest extends ProtocolTest { EgtsProtocolDecoder decoder = new EgtsProtocolDecoder(new EgtsProtocol()); + verifyNull(decoder, binary(ByteOrder.LITTLE_ENDIAN, + "0100010b002200c06401f21700c1640171360d00010101140071360d000238363539303500000000000000000047fc")); + verifyNull(decoder, binary(ByteOrder.LITTLE_ENDIAN, "0100000b002400a0d601f01900030081030000000101011600030000004238363434393530333436343333373600014cdc")); + verifyPositions(decoder, binary(ByteOrder.LITTLE_ENDIAN, + "0100010b00a308c26401029808c3640171360d000202101800e19a7b0fcfb4c49a0bfdb87a911801b70000000010d90000180400021c0000120300000000101800f39a7b0f2fc9c39a9bf2b87a914001b50000000010da0000180400021c0000120300000000101800fa9a7b0fc663c39a21eeb87a914001b60000000010da0000180400021c0000120300000000101800069b7b0f56d8c29a26ebb87a919600ab0000000010da0000180400021c00001203000000001018000a9b7b0fb2c5c29a19f4b87a915a007d0000000010da0000180400021c0000120300000000101800089b7b0f68ccc29a21eeb87a9164008f0000000010da0000180400021c0000120300000000101800079b7b0fa0d1c29aa4ecb87a918200980000000010da0000180400021c00001203000000001018000b9b7b0f34c4c29ad3f7b87a915a00670000000010da0000180400021c00001203000000001018000f9b7b0f3dbec29aaf0fb97a91c8005e0000000010dc0000180400021c0000120300000000101800199b7b0f42bbc29a0855b97a9178006b0000000010db0000180400021c00001203000000001018001b9b7b0fc8b6c29a3c5db97a916e007e0000000010db0000180400021c00001203000000001018001a9b7b0fc4b9c29a8159b97a916e00750000000010db0000180400021c00001203000000001018001d9b7b0f94aec29a3363b97a916400930000000010db0000180400021c00001203000000001018001c9b7b0fcdb3c29a3760b97a916e008a0000000010db0000180400021c0000120300000000101800209b7b0f28a1c29af263b97a918200ba0000000010db0000180400021c00001203000000001018001f9b7b0f61a6c29af263b97a917800b30000000010db0000180400021c00001203000000001018001e9b7b0f58acc29af263b97a916400a50000000010db0000180400021c0000120300000000101800299b7b0ff26fc29ab561b97a916e00b20000000010d90000180400021c00001203000000001018002d9b7b0fd05bc29a3760b97a916e00bd0000000010d80000180400021c0000120300000000101800359b7b0f4f31c29abe5bb97a916400b50000000010d70000180400021c0000120300000000101800379b7b0f5d28c29abe5bb97a916e00b40000000010d60000180400021c0000120300000000101800369b7b0fd62cc29abe5bb97a916400bd0000000010d60000180400021c00001203000000001018003c9b7b0fca09c29a0358b97a918c00bc0000000010d50000180400021c0000120300000000101800419b7b0f38ebc19a0855b97a916e00b40000000010d60000180400021c0000120300000000101800449b7b0f4edcc19a8a53b97a916400c30000000010d60000180400021c0000120300000000101800469b7b0fded1c19acb52b97a916e00b70000000010d60000180400021c0000120300000000101800649b7b0f7a69c19a154cb97a810000bc0000000010d60000180400021c0000120300000000101800709b7b0fcc5cc19a114fb97a915000970000000010d70000180400021c0000120300000000101800729b7b0fda53c19a8a53b97a91a0007d0000000010d70000180400021c0000120300000000101800749b7b0fa24ec19a3c5db97a91a000650000000010d70000180400021c0000120300000000101800789b7b0fe74ac19aca7eb97a910e015c0000000010d80000180400021c00001203000000001018007f9b7b0f6e46c19a78e0b97a9190015c0000000010d80000180400021c0000120300000000101800869b7b0f3641c19a144eba7a9190015c0000000010d60000180400021c00001203000000001018008d9b7b0ffd3bc19a74b9ba7a9190015c0000000010d40000180400021c0000120300000000101800949b7b0fc536c19a1524bb7a9186015b0000000010d20000180400021c00001203000000001018009b9b7b0fce30c19af78dbb7a919a015c0000000010d00000180400021c0000120300000000101800a29b7b0f552cc19a93fbbb7a9172015d0000000010cd0000180400021c0000120300000000101800b09b7b0f2521c19a6ec0bc7a9186015b0000000010c90000180400021c0000120300000000101800a99b7b0f5e26c19a8759bc7a915e015b0000000010cb0000180400021c0000120300000000101800b79b7b0fa81fc19a1328bd7a9172015b0000000010c70000180400021c0000120300000000101800be9b7b0f6b1dc19ac686bd7a914a015c0000000010c60000180400021c0000120300000000101800c19b7b0fed1bc19ad2a9bd7a912201530000000010c60000180400021c0000120300000000101800c39b7b0f6223c19af4bdbd7a910401420000000010c60000180400021c0000120300000000101800c29b7b0fe91ec19a42b4bd7a910e014c0000000010c60000180400021c0000120300000000101800c59b7b0f502fc19a1acfbd7a91fa00300000000010c60000180400021c0000120300000000101800c49b7b0fdb27c19ae6c6bd7a91fa00390000000010c60000180400021c0000120300000000101800c79b7b0fb83fc19a8ad9bd7a91f000180000000010c60000180400021b0000120300000000101800c69b7b0fc536c19a52d4bd7a91f000250000000010c60000180400021b0000120300000000101800ca9b7b0fc85fc19a81dfbd7a910401030000000010c50000180400021b0000120300000000101800c89b7b0fe74ac19a86dcbd7a91fa000e0000000010c50000180400021b0000120300000000101800d29b7b0f06b7c19afbe3bd7a91a000100000000010c50000180400021c0000120300000000101800d59b7b0ff0c5c19a6beebd7a91b400410000000010c40000180400021c0000120300000000101800d49b7b0ff4c2c19af2e9bd7a91a000310000000010c40000180400021c0000120300000000101800d39b7b0f3ebcc19a79e5bd7a9196001e0000000010c40000180400021c0000120300000000101800d69b7b0f6dc7c19ae0f5bd7a91c800570000000010c40000180400021c000012030000000016b7")); + verifyPositions(decoder, binary(ByteOrder.LITTLE_ENDIAN, "0100020B0025003A5701C91A003A5701CD6E68490202101700CBB4740F7617FD924364104F116A0000000000010300001EC2"), position("2018-03-21 05:38:19.000", true, 51.67569, 55.59189)); -- cgit v1.2.3 From bf35aec0e38a9c5b30a41a0d9dcf904320f244ca Mon Sep 17 00:00:00 2001 From: Anton Tananaev Date: Tue, 27 Mar 2018 21:12:01 +1300 Subject: Fix RoboTrack acknowledgement response --- src/org/traccar/protocol/RoboTrackProtocolDecoder.java | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/org/traccar/protocol/RoboTrackProtocolDecoder.java b/src/org/traccar/protocol/RoboTrackProtocolDecoder.java index 2244ea716..323928116 100644 --- a/src/org/traccar/protocol/RoboTrackProtocolDecoder.java +++ b/src/org/traccar/protocol/RoboTrackProtocolDecoder.java @@ -28,6 +28,7 @@ import org.traccar.model.Network; import org.traccar.model.Position; import java.net.SocketAddress; +import java.nio.ByteOrder; import java.nio.charset.StandardCharsets; import java.util.Date; @@ -60,9 +61,9 @@ public class RoboTrackProtocolDecoder extends BaseProtocolDecoder { String imei = buf.readBytes(15).toString(StandardCharsets.US_ASCII); if (getDeviceSession(channel, remoteAddress, imei) != null && channel != null) { - ChannelBuffer response = ChannelBuffers.dynamicBuffer(); - buf.writeByte(MSG_ACK); - buf.writeByte(0x01); // success + ChannelBuffer response = ChannelBuffers.dynamicBuffer(ByteOrder.LITTLE_ENDIAN, 0); + response.writeByte(MSG_ACK); + response.writeByte(0x01); // success response.writeByte(Checksum.crc8(Checksum.CRC8_ROHC, response.toByteBuffer())); channel.write(response); } -- cgit v1.2.3 From 2af34372442ae1ab15bb280c235b857977b6c1a3 Mon Sep 17 00:00:00 2001 From: Anton Tananaev Date: Tue, 27 Mar 2018 21:19:54 +1300 Subject: Fix EGTS speed decoding --- src/org/traccar/protocol/EgtsProtocolDecoder.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/org/traccar/protocol/EgtsProtocolDecoder.java b/src/org/traccar/protocol/EgtsProtocolDecoder.java index e13f18fed..420701e6c 100644 --- a/src/org/traccar/protocol/EgtsProtocolDecoder.java +++ b/src/org/traccar/protocol/EgtsProtocolDecoder.java @@ -22,6 +22,7 @@ import org.traccar.BaseProtocolDecoder; import org.traccar.DeviceSession; import org.traccar.helper.BitUtil; import org.traccar.helper.Checksum; +import org.traccar.helper.UnitsConverter; import org.traccar.model.Position; import java.net.SocketAddress; @@ -203,7 +204,7 @@ public class EgtsProtocolDecoder extends BaseProtocolDecoder { } int speed = buf.readUnsignedShort(); - position.setSpeed(BitUtil.to(speed, 14)); + position.setSpeed(UnitsConverter.knotsFromKph(BitUtil.to(speed, 14) * 0.1)); position.setCourse(buf.readUnsignedByte() + (BitUtil.check(speed, 15) ? 0x100 : 0)); position.set(Position.KEY_ODOMETER, buf.readUnsignedMedium() * 100); -- cgit v1.2.3 From 848dd7a6126b4574dc916f688445c423e37fa282 Mon Sep 17 00:00:00 2001 From: Anton Tananaev Date: Wed, 28 Mar 2018 00:47:47 +1300 Subject: Hardcode RoboTrack response --- src/org/traccar/protocol/RoboTrackProtocolDecoder.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/org/traccar/protocol/RoboTrackProtocolDecoder.java b/src/org/traccar/protocol/RoboTrackProtocolDecoder.java index 323928116..4a3d40a3c 100644 --- a/src/org/traccar/protocol/RoboTrackProtocolDecoder.java +++ b/src/org/traccar/protocol/RoboTrackProtocolDecoder.java @@ -64,7 +64,7 @@ public class RoboTrackProtocolDecoder extends BaseProtocolDecoder { ChannelBuffer response = ChannelBuffers.dynamicBuffer(ByteOrder.LITTLE_ENDIAN, 0); response.writeByte(MSG_ACK); response.writeByte(0x01); // success - response.writeByte(Checksum.crc8(Checksum.CRC8_ROHC, response.toByteBuffer())); + response.writeByte(0x66); // checksum channel.write(response); } -- cgit v1.2.3 From 25d8e41ec8402c915e582874dd246b7c6bb0e5e8 Mon Sep 17 00:00:00 2001 From: Anton Tananaev Date: Wed, 28 Mar 2018 00:48:37 +1300 Subject: Remove unused import --- src/org/traccar/protocol/RoboTrackProtocolDecoder.java | 1 - 1 file changed, 1 deletion(-) diff --git a/src/org/traccar/protocol/RoboTrackProtocolDecoder.java b/src/org/traccar/protocol/RoboTrackProtocolDecoder.java index 4a3d40a3c..4f27fb08e 100644 --- a/src/org/traccar/protocol/RoboTrackProtocolDecoder.java +++ b/src/org/traccar/protocol/RoboTrackProtocolDecoder.java @@ -21,7 +21,6 @@ import org.jboss.netty.channel.Channel; import org.traccar.BaseProtocolDecoder; import org.traccar.DeviceSession; import org.traccar.helper.BitUtil; -import org.traccar.helper.Checksum; import org.traccar.helper.UnitsConverter; import org.traccar.model.CellTower; import org.traccar.model.Network; -- cgit v1.2.3 From 96f2d637d35fafa6226855eea6e39919848e1e80 Mon Sep 17 00:00:00 2001 From: Anton Tananaev Date: Wed, 28 Mar 2018 02:24:49 +1300 Subject: Implement Continental ACK response --- .../protocol/ContinentalProtocolDecoder.java | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/src/org/traccar/protocol/ContinentalProtocolDecoder.java b/src/org/traccar/protocol/ContinentalProtocolDecoder.java index 2138eb39e..726d9e16b 100644 --- a/src/org/traccar/protocol/ContinentalProtocolDecoder.java +++ b/src/org/traccar/protocol/ContinentalProtocolDecoder.java @@ -16,6 +16,7 @@ package org.traccar.protocol; import org.jboss.netty.buffer.ChannelBuffer; +import org.jboss.netty.buffer.ChannelBuffers; import org.jboss.netty.channel.Channel; import org.traccar.BaseProtocolDecoder; import org.traccar.DeviceSession; @@ -36,6 +37,22 @@ public class ContinentalProtocolDecoder extends BaseProtocolDecoder { public static final int MSG_ACK = 0x06; public static final int MSG_NACK = 0x15; + private void sendResponse(Channel channel, long serialNumber) { + if (channel != null) { + ChannelBuffer response = ChannelBuffers.dynamicBuffer(); + + response.writeByte('S'); + response.writeByte('V'); + response.writeShort(2 + 2 + 1 + 4 + 2); // length + response.writeByte(1); // version + response.writeInt((int) serialNumber); + response.writeByte(0); // product + response.writeByte(MSG_ACK); + + channel.write(response); + } + } + @Override protected Object decode( Channel channel, SocketAddress remoteAddress, Object msg) throws Exception { @@ -46,11 +63,14 @@ public class ContinentalProtocolDecoder extends BaseProtocolDecoder { buf.readUnsignedShort(); // length buf.readUnsignedByte(); // software version - DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, String.valueOf(buf.readUnsignedInt())); + long serialNumber = buf.readUnsignedInt(); + DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, String.valueOf(serialNumber)); if (deviceSession == null) { return null; } + sendResponse(channel, serialNumber); + buf.readUnsignedByte(); // product int type = buf.readUnsignedByte(); -- cgit v1.2.3 From 85c43a28405deab09a15ffbc8fcaa38570b69cf8 Mon Sep 17 00:00:00 2001 From: Anton Tananaev Date: Wed, 28 Mar 2018 02:49:55 +1300 Subject: Add terminal id to KHD commands --- src/org/traccar/protocol/KhdProtocolDecoder.java | 13 +++---------- src/org/traccar/protocol/KhdProtocolEncoder.java | 17 ++++++++++++----- test/org/traccar/protocol/KhdProtocolEncoderTest.java | 2 +- 3 files changed, 16 insertions(+), 16 deletions(-) diff --git a/src/org/traccar/protocol/KhdProtocolDecoder.java b/src/org/traccar/protocol/KhdProtocolDecoder.java index d1b5413e5..2f29a16f8 100644 --- a/src/org/traccar/protocol/KhdProtocolDecoder.java +++ b/src/org/traccar/protocol/KhdProtocolDecoder.java @@ -36,17 +36,10 @@ public class KhdProtocolDecoder extends BaseProtocolDecoder { private String readSerialNumber(ChannelBuffer buf) { int b1 = buf.readUnsignedByte(); - int b2 = buf.readUnsignedByte(); - if (b2 > 0x80) { - b2 -= 0x80; - } - int b3 = buf.readUnsignedByte(); - if (b3 > 0x80) { - b3 -= 0x80; - } + int b2 = buf.readUnsignedByte() - 0x80; + int b3 = buf.readUnsignedByte() - 0x80; int b4 = buf.readUnsignedByte(); - String serialNumber = String.format("%02d%02d%02d%02d", b1, b2, b3, b4); - return String.valueOf(Long.parseLong(serialNumber)); + return String.format("%02d%02d%02d%02d", b1, b2, b3, b4); } public static final int MSG_LOGIN = 0xB1; diff --git a/src/org/traccar/protocol/KhdProtocolEncoder.java b/src/org/traccar/protocol/KhdProtocolEncoder.java index 618e43dad..cb26c757a 100644 --- a/src/org/traccar/protocol/KhdProtocolEncoder.java +++ b/src/org/traccar/protocol/KhdProtocolEncoder.java @@ -1,5 +1,5 @@ /* - * Copyright 2015 Anton Tananaev (anton@traccar.org) + * Copyright 2015 - 2018 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. @@ -27,7 +27,7 @@ public class KhdProtocolEncoder extends BaseProtocolEncoder { public static final int MSG_CUT_OIL = 0x39; public static final int MSG_RESUME_OIL = 0x38; - private ChannelBuffer encodeCommand(int command) { + private ChannelBuffer encodeCommand(int command, String uniqueId) { ChannelBuffer buf = ChannelBuffers.dynamicBuffer(); @@ -37,7 +37,12 @@ public class KhdProtocolEncoder extends BaseProtocolEncoder { buf.writeByte(command); buf.writeShort(6); // size - buf.writeInt(0); // terminal id + uniqueId = "00000000".concat(uniqueId); + uniqueId = uniqueId.substring(uniqueId.length() - 8); + buf.writeByte(Integer.parseInt(uniqueId.substring(0, 2))); + buf.writeByte(Integer.parseInt(uniqueId.substring(2, 4)) + 0x80); + buf.writeByte(Integer.parseInt(uniqueId.substring(4, 6)) + 0x80); + buf.writeByte(Integer.parseInt(uniqueId.substring(6, 8))); buf.writeByte(Checksum.xor(buf.toByteBuffer())); buf.writeByte(0x0D); // ending @@ -48,11 +53,13 @@ public class KhdProtocolEncoder extends BaseProtocolEncoder { @Override protected Object encodeCommand(Command command) { + String uniqueId = getUniqueId(command.getDeviceId()); + switch (command.getType()) { case Command.TYPE_ENGINE_STOP: - return encodeCommand(MSG_CUT_OIL); + return encodeCommand(MSG_CUT_OIL, uniqueId); case Command.TYPE_ENGINE_RESUME: - return encodeCommand(MSG_RESUME_OIL); + return encodeCommand(MSG_RESUME_OIL, uniqueId); default: Log.warning(new UnsupportedOperationException(command.getType())); break; diff --git a/test/org/traccar/protocol/KhdProtocolEncoderTest.java b/test/org/traccar/protocol/KhdProtocolEncoderTest.java index 078b7c22e..ab858041a 100644 --- a/test/org/traccar/protocol/KhdProtocolEncoderTest.java +++ b/test/org/traccar/protocol/KhdProtocolEncoderTest.java @@ -15,7 +15,7 @@ public class KhdProtocolEncoderTest extends ProtocolTest { command.setDeviceId(1); command.setType(Command.TYPE_ENGINE_STOP); - verifyCommand(encoder, command, binary("2929390006000000003F0D")); + verifyCommand(encoder, command, binary("29293900065981972d5d0d")); } -- cgit v1.2.3 From b98b3370bf975501857f6300a4f140e05cd82d5f Mon Sep 17 00:00:00 2001 From: Anton Tananaev Date: Wed, 28 Mar 2018 07:31:15 +1300 Subject: Fix GoSafe G6S and G3S decoding --- src/org/traccar/protocol/GoSafeProtocolDecoder.java | 4 ++-- test/org/traccar/protocol/GoSafeProtocolDecoderTest.java | 6 ++++++ 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/src/org/traccar/protocol/GoSafeProtocolDecoder.java b/src/org/traccar/protocol/GoSafeProtocolDecoder.java index 13ce839ea..44dfb08a2 100644 --- a/src/org/traccar/protocol/GoSafeProtocolDecoder.java +++ b/src/org/traccar/protocol/GoSafeProtocolDecoder.java @@ -1,5 +1,5 @@ /* - * Copyright 2015 - 2017 Anton Tananaev (anton@traccar.org) + * Copyright 2015 - 2018 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. @@ -81,7 +81,7 @@ public class GoSafeProtocolDecoder extends BaseProtocolDecoder { .groupBegin() .text("COT:") .number("(d+)") // odometer - .number("(?:;d+:d+:d+)?") // engine hours + .number("(?:;d+-d+-d+)?") // engine hours .expression(",?") .groupEnd("?") .groupBegin() diff --git a/test/org/traccar/protocol/GoSafeProtocolDecoderTest.java b/test/org/traccar/protocol/GoSafeProtocolDecoderTest.java index 42293f7ec..3e4e8f413 100644 --- a/test/org/traccar/protocol/GoSafeProtocolDecoderTest.java +++ b/test/org/traccar/protocol/GoSafeProtocolDecoderTest.java @@ -10,6 +10,12 @@ public class GoSafeProtocolDecoderTest extends ProtocolTest { GoSafeProtocolDecoder decoder = new GoSafeProtocolDecoder(new GoSafeProtocol()); + verifyPositions(decoder, text( + "*GS06,860078024213915,032544190318,,SYS:G3SC;V3.32;V1.1.8,GPS:A;7;N3.052417;E101.787112;0;0;94;1.38,COT:686;0-0-0,ADC:16.25;4.09,DTT:4000;E0;0;0;0;1#")); + + verifyPositions(decoder, text( + "*GS06,351535058659335,062728190318,,SYS:G6S;V3.32;V1.0.5,GPS:A;10;N23.169806;E113.450760;0;0;81;0.77,COT:0,ADC:0.00;0.16,DTT:80;E0;0;0;0;1#")); + verifyPositions(decoder, text( "*GS26,356449061046586,082522030117,,SYS:G737IC;V1.13;V1.0.5,GPS:V;5;N42.594136;W70.723832;0;0;8;2.06,GSM:;;310;260;C76D;9F1D;-85,ADC:3.86,DTT:3918C;;0;0;0;1,#")); -- cgit v1.2.3 From 75375cdc48055ba3a6f4dce1b3a6536e0a32419f Mon Sep 17 00:00:00 2001 From: Anton Tananaev Date: Wed, 28 Mar 2018 07:41:47 +1300 Subject: Fix MT-200X decoding issue --- src/org/traccar/protocol/MegastekProtocolDecoder.java | 6 +++--- test/org/traccar/protocol/MegastekProtocolDecoderTest.java | 3 +++ 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/src/org/traccar/protocol/MegastekProtocolDecoder.java b/src/org/traccar/protocol/MegastekProtocolDecoder.java index 91618b534..14d39e0cc 100644 --- a/src/org/traccar/protocol/MegastekProtocolDecoder.java +++ b/src/org/traccar/protocol/MegastekProtocolDecoder.java @@ -1,5 +1,5 @@ /* - * Copyright 2013 - 2016 Anton Tananaev (anton@traccar.org) + * Copyright 2013 - 2018 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. @@ -250,7 +250,7 @@ public class MegastekProtocolDecoder extends BaseProtocolDecoder { .number("(d+),") // mcc .number("(d+),") // mnc .number("(xxxx),") // lac - .number("(xxxx),") // cid + .number("(x+),") // cid .number("(d+)?,") // gsm .expression("([01]+)?,") // input .expression("([01]+)?,") // output @@ -268,7 +268,7 @@ public class MegastekProtocolDecoder extends BaseProtocolDecoder { .number("(d+)?,") // rfid .expression("[^,]*,") .number("(d+)?,") // battery - .expression("([^,]*);") // alert + .expression("([^,]*)") // alert .any() .compile(); diff --git a/test/org/traccar/protocol/MegastekProtocolDecoderTest.java b/test/org/traccar/protocol/MegastekProtocolDecoderTest.java index 9bb705f17..d10432b38 100644 --- a/test/org/traccar/protocol/MegastekProtocolDecoderTest.java +++ b/test/org/traccar/protocol/MegastekProtocolDecoderTest.java @@ -10,6 +10,9 @@ public class MegastekProtocolDecoderTest extends ProtocolTest { MegastekProtocolDecoder decoder = new MegastekProtocolDecoder(new MegastekProtocol()); + verifyPosition(decoder, text( + "0174$MGV002,014682001957744,014682001957744,R,260318,042537,A,3853.77301,N,07728.66673,W,00,09,00,1.06,0.147,329.51,123.3,,310,26,B46C,5E69375,5,0000,0000,0,,,,,,10,019,Timer,,;!")); + verifyNull(decoder, text( "0112$MGV002,,GVT900-3,S,010114,000003,,,,,,00,00,00,,0.000,0.00,,0.0,,,,,,0000,0000,14,10,0, , ,,1-0,0,Low Ext Vol;!")); -- cgit v1.2.3 From eac3b3804d8bbabde8b5ac8c4661bf0fec469868 Mon Sep 17 00:00:00 2001 From: Anton Tananaev Date: Wed, 28 Mar 2018 19:51:31 +1300 Subject: Add RoboTrack location test cases --- test/org/traccar/protocol/RoboTrackProtocolDecoderTest.java | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/test/org/traccar/protocol/RoboTrackProtocolDecoderTest.java b/test/org/traccar/protocol/RoboTrackProtocolDecoderTest.java index b65d9974b..bd260597f 100644 --- a/test/org/traccar/protocol/RoboTrackProtocolDecoderTest.java +++ b/test/org/traccar/protocol/RoboTrackProtocolDecoderTest.java @@ -3,6 +3,8 @@ package org.traccar.protocol; import org.junit.Test; import org.traccar.ProtocolTest; +import java.nio.ByteOrder; + public class RoboTrackProtocolDecoderTest extends ProtocolTest { @Test @@ -10,9 +12,15 @@ public class RoboTrackProtocolDecoderTest extends ProtocolTest { RoboTrackProtocolDecoder decoder = new RoboTrackProtocolDecoder(new RoboTrackProtocol()); - verifyNull(decoder, binary( + verifyNull(decoder, binary(ByteOrder.LITTLE_ENDIAN, "00524f424f545241434b00000000000000383638323034303032323533343136313233343536373839303132312e313261000000312e353761000000312e3030000000003e")); + verifyPosition(decoder, binary(ByteOrder.LITTLE_ENDIAN, + "03e020bb5a034409034862120210a9e105000000000000b9")); + + verifyPosition(decoder, binary(ByteOrder.LITTLE_ENDIAN, + "03f120bb5a30460903426312021798e105000000000000cd")); + } } -- cgit v1.2.3 From 1e3e7c767ba88e6439dd512dd64b6a8b746b7e96 Mon Sep 17 00:00:00 2001 From: Anton Tananaev Date: Thu, 29 Mar 2018 16:33:28 +1300 Subject: Improve Watch command encoding --- src/org/traccar/BaseProtocolEncoder.java | 12 +++-- src/org/traccar/protocol/WatchProtocolDecoder.java | 24 +++++++--- src/org/traccar/protocol/WatchProtocolEncoder.java | 54 ++++++++++++++-------- .../traccar/protocol/WatchProtocolEncoderTest.java | 16 +++---- 4 files changed, 70 insertions(+), 36 deletions(-) diff --git a/src/org/traccar/BaseProtocolEncoder.java b/src/org/traccar/BaseProtocolEncoder.java index 2c8a81868..18544fd28 100644 --- a/src/org/traccar/BaseProtocolEncoder.java +++ b/src/org/traccar/BaseProtocolEncoder.java @@ -1,5 +1,5 @@ /* - * Copyright 2015 - 2016 Anton Tananaev (anton@traccar.org) + * Copyright 2015 - 2018 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. @@ -45,7 +45,7 @@ public abstract class BaseProtocolEncoder extends OneToOneEncoder { if (msg instanceof Command) { Command command = (Command) msg; - Object encodedCommand = encodeCommand(command); + Object encodedCommand = encodeCommand(channel, command); // Log command StringBuilder s = new StringBuilder(); @@ -65,6 +65,12 @@ public abstract class BaseProtocolEncoder extends OneToOneEncoder { return msg; } - protected abstract Object encodeCommand(Command command); + protected Object encodeCommand(Channel channel, Command command) { + return encodeCommand(command); + } + + protected Object encodeCommand(Command command) { + return null; + } } diff --git a/src/org/traccar/protocol/WatchProtocolDecoder.java b/src/org/traccar/protocol/WatchProtocolDecoder.java index 41cd957ae..68832cd3e 100644 --- a/src/org/traccar/protocol/WatchProtocolDecoder.java +++ b/src/org/traccar/protocol/WatchProtocolDecoder.java @@ -60,7 +60,7 @@ public class WatchProtocolDecoder extends BaseProtocolDecoder { .expression("(.*)") // cell and wifi .compile(); - private void sendResponse(Channel channel, String manufacturer, String id, String index, String content) { + private void sendResponse(Channel channel, String id, String index, String content) { if (channel != null) { if (index != null) { channel.write(String.format( @@ -161,6 +161,17 @@ public class WatchProtocolDecoder extends BaseProtocolDecoder { return position; } + private boolean hasIndex; + private String manufacturer; + + public boolean getHasIndex() { + return hasIndex; + } + + public String getManufacturer() { + return manufacturer; + } + @Override protected Object decode( Channel channel, SocketAddress remoteAddress, Object msg) throws Exception { @@ -168,7 +179,7 @@ public class WatchProtocolDecoder extends BaseProtocolDecoder { ChannelBuffer buf = (ChannelBuffer) msg; buf.skipBytes(1); // header - String manufacturer = buf.readBytes(2).toString(StandardCharsets.US_ASCII); + manufacturer = buf.readBytes(2).toString(StandardCharsets.US_ASCII); buf.skipBytes(1); // delimiter int idIndex = buf.indexOf(buf.readerIndex(), buf.writerIndex(), (byte) '*'); @@ -185,6 +196,7 @@ public class WatchProtocolDecoder extends BaseProtocolDecoder { if (contentIndex + 5 < buf.writerIndex() && buf.getByte(contentIndex + 5) == '*' && buf.toString(contentIndex + 1, 4, StandardCharsets.US_ASCII).matches("\\p{XDigit}+")) { int indexLength = buf.indexOf(buf.readerIndex(), buf.writerIndex(), (byte) '*') - buf.readerIndex(); + hasIndex = true; index = buf.readBytes(indexLength).toString(StandardCharsets.US_ASCII); buf.skipBytes(1); // delimiter } @@ -207,11 +219,11 @@ public class WatchProtocolDecoder extends BaseProtocolDecoder { if (type.equals("INIT")) { - sendResponse(channel, manufacturer, id, index, "INIT,1"); + sendResponse(channel, id, index, "INIT,1"); } else if (type.equals("LK")) { - sendResponse(channel, manufacturer, id, index, "LK"); + sendResponse(channel, id, index, "LK"); if (buf.readable()) { String[] values = buf.toString(StandardCharsets.US_ASCII).split(","); @@ -231,14 +243,14 @@ public class WatchProtocolDecoder extends BaseProtocolDecoder { || type.equals("AL") || type.equals("WT")) { if (type.equals("AL")) { - sendResponse(channel, manufacturer, id, index, "AL"); + sendResponse(channel, id, index, "AL"); } return decodePosition(deviceSession, buf.toString(StandardCharsets.US_ASCII)); } else if (type.equals("TKQ")) { - sendResponse(channel, manufacturer, id, index, "TKQ"); + sendResponse(channel, id, index, "TKQ"); } else if (type.equals("PULSE") || type.equals("heart") || type.equals("bphrt")) { diff --git a/src/org/traccar/protocol/WatchProtocolEncoder.java b/src/org/traccar/protocol/WatchProtocolEncoder.java index 5206fbf10..4c87f3abd 100644 --- a/src/org/traccar/protocol/WatchProtocolEncoder.java +++ b/src/org/traccar/protocol/WatchProtocolEncoder.java @@ -15,6 +15,7 @@ */ package org.traccar.protocol; +import org.jboss.netty.channel.Channel; import org.traccar.StringProtocolEncoder; import org.traccar.helper.DataConverter; import org.traccar.helper.Log; @@ -41,12 +42,27 @@ public class WatchProtocolEncoder extends StringProtocolEncoder implements Strin return null; } + protected String formatCommand(Channel channel, Command command, String format, String... keys) { + + boolean hasIndex = false; + String manufacturer = "CS"; + if (channel != null) { + WatchProtocolDecoder decoder = channel.getPipeline().get(WatchProtocolDecoder.class); + if (decoder != null) { + hasIndex = decoder.getHasIndex(); + manufacturer = decoder.getManufacturer(); + } + } - @Override - protected String formatCommand(Command command, String format, String... keys) { String content = formatCommand(command, format, this, keys); - return String.format("[CS*%s*%04x*%s]", - getUniqueId(command.getDeviceId()), content.length(), content); + + if (hasIndex) { + return String.format("[%s*%s*0001*%04x*%s]", + manufacturer, getUniqueId(command.getDeviceId()), content.length(), content); + } else { + return String.format("[%s*%s*%04x*%s]", + manufacturer, getUniqueId(command.getDeviceId()), content.length(), content); + } } private int getEnableFlag(Command command) { @@ -96,37 +112,37 @@ public class WatchProtocolEncoder extends StringProtocolEncoder implements Strin } @Override - protected Object encodeCommand(Command command) { + protected Object encodeCommand(Channel channel, Command command) { switch (command.getType()) { case Command.TYPE_CUSTOM: - return formatCommand(command, command.getString(Command.KEY_DATA)); + return formatCommand(channel, command, command.getString(Command.KEY_DATA)); case Command.TYPE_POSITION_SINGLE: - return formatCommand(command, "RG"); + return formatCommand(channel, command, "RG"); case Command.TYPE_SOS_NUMBER: - return formatCommand(command, "SOS{%s},{%s}", Command.KEY_INDEX, Command.KEY_PHONE); + return formatCommand(channel, command, "SOS{%s},{%s}", Command.KEY_INDEX, Command.KEY_PHONE); case Command.TYPE_ALARM_SOS: - return formatCommand(command, "SOSSMS," + getEnableFlag(command)); + return formatCommand(channel, command, "SOSSMS," + getEnableFlag(command)); case Command.TYPE_ALARM_BATTERY: - return formatCommand(command, "LOWBAT," + getEnableFlag(command)); + return formatCommand(channel, command, "LOWBAT," + getEnableFlag(command)); case Command.TYPE_REBOOT_DEVICE: - return formatCommand(command, "RESET"); + return formatCommand(channel, command, "RESET"); case Command.TYPE_ALARM_REMOVE: - return formatCommand(command, "REMOVE," + getEnableFlag(command)); + return formatCommand(channel, command, "REMOVE," + getEnableFlag(command)); case Command.TYPE_SILENCE_TIME: - return formatCommand(command, "SILENCETIME,{%s}", Command.KEY_DATA); + return formatCommand(channel, command, "SILENCETIME,{%s}", Command.KEY_DATA); case Command.TYPE_ALARM_CLOCK: - return formatCommand(command, "REMIND,{%s}", Command.KEY_DATA); + return formatCommand(channel, command, "REMIND,{%s}", Command.KEY_DATA); case Command.TYPE_SET_PHONEBOOK: - return formatCommand(command, "PHB,{%s}", Command.KEY_DATA); + return formatCommand(channel, command, "PHB,{%s}", Command.KEY_DATA); case Command.TYPE_VOICE_MESSAGE: - return formatCommand(command, "TK," + getBinaryData(command)); + return formatCommand(channel, command, "TK," + getBinaryData(command)); case Command.TYPE_POSITION_PERIODIC: - return formatCommand(command, "UPLOAD,{%s}", Command.KEY_FREQUENCY); + return formatCommand(channel, command, "UPLOAD,{%s}", Command.KEY_FREQUENCY); case Command.TYPE_SET_TIMEZONE: - return formatCommand(command, "LZ,,{%s}", Command.KEY_TIMEZONE); + return formatCommand(channel, command, "LZ,,{%s}", Command.KEY_TIMEZONE); case Command.TYPE_SET_INDICATOR: - return formatCommand(command, "FLOWER,{%s}", Command.KEY_DATA); + return formatCommand(channel, command, "FLOWER,{%s}", Command.KEY_DATA); default: Log.warning(new UnsupportedOperationException(command.getType())); break; diff --git a/test/org/traccar/protocol/WatchProtocolEncoderTest.java b/test/org/traccar/protocol/WatchProtocolEncoderTest.java index a201b7860..d7784f50b 100644 --- a/test/org/traccar/protocol/WatchProtocolEncoderTest.java +++ b/test/org/traccar/protocol/WatchProtocolEncoderTest.java @@ -18,41 +18,41 @@ public class WatchProtocolEncoderTest extends ProtocolTest { command = new Command(); command.setDeviceId(1); command.setType(Command.TYPE_REBOOT_DEVICE); - assertEquals("[CS*123456789012345*0005*RESET]", encoder.encodeCommand(command)); + assertEquals("[CS*123456789012345*0005*RESET]", encoder.encodeCommand(null, command)); command = new Command(); command.setDeviceId(1); command.setType(Command.TYPE_SOS_NUMBER); command.set(Command.KEY_INDEX, 1); command.set(Command.KEY_PHONE, "123456789"); - assertEquals("[CS*123456789012345*000e*SOS1,123456789]", encoder.encodeCommand(command)); + assertEquals("[CS*123456789012345*000e*SOS1,123456789]", encoder.encodeCommand(null, command)); command = new Command(); command.setDeviceId(1); command.setType(Command.TYPE_VOICE_MESSAGE); command.set(Command.KEY_DATA, "3333"); - assertEquals("[CS*123456789012345*0005*TK,33]", encoder.encodeCommand(command)); + assertEquals("[CS*123456789012345*0005*TK,33]", encoder.encodeCommand(null, command)); command = new Command(); command.setDeviceId(1); command.setType(Command.TYPE_CUSTOM); command.set(Command.KEY_DATA, "WORK,6-9,11-13,13-15,17-19"); - assertEquals("[CS*123456789012345*001a*WORK,6-9,11-13,13-15,17-19]", encoder.encodeCommand(command)); + assertEquals("[CS*123456789012345*001a*WORK,6-9,11-13,13-15,17-19]", encoder.encodeCommand(null, command)); command = new Command(); command.setDeviceId(1); command.setType(Command.TYPE_SET_TIMEZONE); command.set(Command.KEY_TIMEZONE, "Europe/Amsterdam"); - assertEquals("[CS*123456789012345*0006*LZ,,+1]", encoder.encodeCommand(command)); + assertEquals("[CS*123456789012345*0006*LZ,,+1]", encoder.encodeCommand(null, command)); command.set(Command.KEY_TIMEZONE, "GMT+01:30"); - assertEquals("[CS*123456789012345*0008*LZ,,+1.5]", encoder.encodeCommand(command)); + assertEquals("[CS*123456789012345*0008*LZ,,+1.5]", encoder.encodeCommand(null, command)); command.set(Command.KEY_TIMEZONE, "Atlantic/Azores"); - assertEquals("[CS*123456789012345*0006*LZ,,-1]", encoder.encodeCommand(command)); + assertEquals("[CS*123456789012345*0006*LZ,,-1]", encoder.encodeCommand(null, command)); command.set(Command.KEY_TIMEZONE, "GMT-11:30"); - assertEquals("[CS*123456789012345*0009*LZ,,-11.5]", encoder.encodeCommand(command)); + assertEquals("[CS*123456789012345*0009*LZ,,-11.5]", encoder.encodeCommand(null, command)); } -- cgit v1.2.3 From 70b4007226fc0ebcfd5133abb2dcaab61ae929d7 Mon Sep 17 00:00:00 2001 From: Anton Tananaev Date: Thu, 29 Mar 2018 18:53:28 +1300 Subject: Correctly decode BCE speed value --- src/org/traccar/protocol/BceProtocolDecoder.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/org/traccar/protocol/BceProtocolDecoder.java b/src/org/traccar/protocol/BceProtocolDecoder.java index 22d6a5aa8..a023e60c5 100644 --- a/src/org/traccar/protocol/BceProtocolDecoder.java +++ b/src/org/traccar/protocol/BceProtocolDecoder.java @@ -21,6 +21,7 @@ import org.jboss.netty.channel.Channel; import org.traccar.BaseProtocolDecoder; import org.traccar.DeviceSession; import org.traccar.helper.BitUtil; +import org.traccar.helper.UnitsConverter; import org.traccar.model.CellTower; import org.traccar.model.Network; import org.traccar.model.Position; @@ -93,7 +94,7 @@ public class BceProtocolDecoder extends BaseProtocolDecoder { position.setValid(true); position.setLongitude(buf.readFloat()); position.setLatitude(buf.readFloat()); - position.setSpeed(buf.readUnsignedByte()); + position.setSpeed(UnitsConverter.knotsFromKph(buf.readUnsignedByte())); int gps = buf.readUnsignedByte(); position.set(Position.KEY_SATELLITES, gps & 0xf); -- cgit v1.2.3 From b0a63981a99bbef156c8801ad40d29f3bb2aae8e Mon Sep 17 00:00:00 2001 From: Anton Tananaev Date: Wed, 4 Apr 2018 02:21:48 +1200 Subject: Implement PT60 protocol --- setup/default.xml | 1 + src/org/traccar/protocol/Pt60Protocol.java | 47 ++++++++++++ src/org/traccar/protocol/Pt60ProtocolDecoder.java | 83 ++++++++++++++++++++++ .../traccar/protocol/Pt60ProtocolDecoderTest.java | 21 ++++++ 4 files changed, 152 insertions(+) create mode 100644 src/org/traccar/protocol/Pt60Protocol.java create mode 100644 src/org/traccar/protocol/Pt60ProtocolDecoder.java create mode 100644 test/org/traccar/protocol/Pt60ProtocolDecoderTest.java diff --git a/setup/default.xml b/setup/default.xml index 7996ce716..6bab81401 100644 --- a/setup/default.xml +++ b/setup/default.xml @@ -236,5 +236,6 @@ 5161 5162 5163 + 5164 diff --git a/src/org/traccar/protocol/Pt60Protocol.java b/src/org/traccar/protocol/Pt60Protocol.java new file mode 100644 index 000000000..857790efd --- /dev/null +++ b/src/org/traccar/protocol/Pt60Protocol.java @@ -0,0 +1,47 @@ +/* + * Copyright 2018 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 org.jboss.netty.bootstrap.ServerBootstrap; +import org.jboss.netty.channel.ChannelPipeline; +import org.jboss.netty.handler.codec.string.StringDecoder; +import org.jboss.netty.handler.codec.string.StringEncoder; +import org.traccar.BaseProtocol; +import org.traccar.CharacterDelimiterFrameDecoder; +import org.traccar.TrackerServer; + +import java.util.List; + +public class Pt60Protocol extends BaseProtocol { + + public Pt60Protocol() { + super("pt60"); + } + + @Override + public void initTrackerServers(List serverList) { + serverList.add(new TrackerServer(new ServerBootstrap(), getName()) { + @Override + protected void addSpecificHandlers(ChannelPipeline pipeline) { + pipeline.addLast("frameDecoder", new CharacterDelimiterFrameDecoder(1024, "@R#@")); + pipeline.addLast("stringEncoder", new StringEncoder()); + pipeline.addLast("stringDecoder", new StringDecoder()); + pipeline.addLast("objectDecoder", new Pt60ProtocolDecoder(Pt60Protocol.this)); + } + }); + } + +} diff --git a/src/org/traccar/protocol/Pt60ProtocolDecoder.java b/src/org/traccar/protocol/Pt60ProtocolDecoder.java new file mode 100644 index 000000000..c87c22c5f --- /dev/null +++ b/src/org/traccar/protocol/Pt60ProtocolDecoder.java @@ -0,0 +1,83 @@ +/* + * Copyright 2018 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 org.jboss.netty.channel.Channel; +import org.traccar.BaseProtocolDecoder; +import org.traccar.DeviceSession; +import org.traccar.helper.Parser; +import org.traccar.helper.PatternBuilder; +import org.traccar.model.Position; + +import java.net.SocketAddress; +import java.text.DateFormat; +import java.text.SimpleDateFormat; +import java.util.Date; +import java.util.regex.Pattern; + +public class Pt60ProtocolDecoder extends BaseProtocolDecoder { + + public Pt60ProtocolDecoder(Pt60Protocol protocol) { + super(protocol); + } + + private static final Pattern PATTERN = new PatternBuilder() + .text("@G#@,") // header + .number("Vdd,") // protocol version + .number("d,") // type + .number("(d+),") // imei + .number("(d+),") // imsi + .number("(dddd)(dd)(dd)") // date (yyyymmdd) + .number("(dd)(dd)(dd),") // time (hhmmss) + .number("(-?d+.d+);") // latitude + .number("(-?d+.d+),") // longitude + .compile(); + + private void sendResponse(Channel channel) { + if (channel != null) { + DateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmss"); + channel.write("@G#@,V01,38," + dateFormat.format(new Date()) + ",@R#@"); + } + } + + @Override + protected Object decode( + Channel channel, SocketAddress remoteAddress, Object msg) throws Exception { + + sendResponse(channel); + + Parser parser = new Parser(PATTERN, (String) msg); + if (!parser.matches()) { + return null; + } + + Position position = new Position(getProtocolName()); + + DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, parser.next(), parser.next()); + if (deviceSession == null) { + return null; + } + position.setDeviceId(deviceSession.getDeviceId()); + + position.setValid(true); + position.setTime(parser.nextDateTime()); + position.setLatitude(parser.nextDouble()); + position.setLongitude(parser.nextDouble()); + + return position; + } + +} diff --git a/test/org/traccar/protocol/Pt60ProtocolDecoderTest.java b/test/org/traccar/protocol/Pt60ProtocolDecoderTest.java new file mode 100644 index 000000000..5bea875e8 --- /dev/null +++ b/test/org/traccar/protocol/Pt60ProtocolDecoderTest.java @@ -0,0 +1,21 @@ +package org.traccar.protocol; + +import org.junit.Test; +import org.traccar.ProtocolTest; + +public class Pt60ProtocolDecoderTest extends ProtocolTest { + + @Test + public void testDecode() throws Exception { + + Pt60ProtocolDecoder decoder = new Pt60ProtocolDecoder(new Pt60Protocol()); + + verifyPosition(decoder, text( + "@G#@,V01,6,111112222233333,8888888888888888,20150312010203,23.2014050;104.235212,")); + + verifyNull(decoder, text( + "@G#@,V01,1,353882080015633,9460025014649193,")); + + } + +} -- cgit v1.2.3 From dfcfcf741e8a8365f026916fc8c99596557cd29a Mon Sep 17 00:00:00 2001 From: Anton Tananaev Date: Thu, 5 Apr 2018 16:31:40 +1200 Subject: Implement Aquila B protocol --- .../traccar/protocol/AquilaProtocolDecoder.java | 127 ++++++++++++++++++++- .../protocol/AquilaProtocolDecoderTest.java | 18 ++- 2 files changed, 138 insertions(+), 7 deletions(-) diff --git a/src/org/traccar/protocol/AquilaProtocolDecoder.java b/src/org/traccar/protocol/AquilaProtocolDecoder.java index d8081612d..960139b3f 100644 --- a/src/org/traccar/protocol/AquilaProtocolDecoder.java +++ b/src/org/traccar/protocol/AquilaProtocolDecoder.java @@ -1,5 +1,5 @@ /* - * Copyright 2015 - 2017 Anton Tananaev (anton@traccar.org) + * Copyright 2015 - 2018 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. @@ -21,6 +21,8 @@ import org.traccar.DeviceSession; import org.traccar.helper.Parser; import org.traccar.helper.PatternBuilder; import org.traccar.helper.UnitsConverter; +import org.traccar.model.CellTower; +import org.traccar.model.Network; import org.traccar.model.Position; import java.net.SocketAddress; @@ -32,7 +34,7 @@ public class AquilaProtocolDecoder extends BaseProtocolDecoder { super(protocol); } - private static final Pattern PATTERN = new PatternBuilder() + private static final Pattern PATTERN_A = new PatternBuilder() .text("$$") .expression("[^,]*,") // client .number("(d+),") // device serial number @@ -129,11 +131,9 @@ public class AquilaProtocolDecoder extends BaseProtocolDecoder { .number("xx") // checksum .compile(); - @Override - protected Object decode( - Channel channel, SocketAddress remoteAddress, Object msg) throws Exception { + private Position decodeA(Channel channel, SocketAddress remoteAddress, String sentence) { - Parser parser = new Parser(PATTERN, (String) msg); + Parser parser = new Parser(PATTERN_A, sentence); if (!parser.matches()) { return null; } @@ -215,4 +215,119 @@ public class AquilaProtocolDecoder extends BaseProtocolDecoder { return position; } + private static final Pattern PATTERN_B = new PatternBuilder() + .text("$Header,") + .expression("[^,]+,") // client + .expression("[^,]+,") // firmware version + .expression(".{2},") // type + .number("d+,") // message id + .expression("[LH],") // status + .number("(d+),") // imei + .expression("[^,]+,") // registration number + .number("([01]),") // validity + .number("(dd)(dd)(dddd),") // date (ddmmyyyy) + .number("(dd)(dd)(dd),") // time (hhmmss) + .number("(-?d+.d+),") // latitude + .expression("([NS]),") + .number("(-?d+.d+),") // longitude + .expression("([EW]),") + .number("(d+.d+),") // speed + .number("(d+),") // course + .number("(d+),") // satellites + .number("(-?d+.d+),") // altitude + .number("(d+.d+),") // pdop + .number("(d+.d+),") // hdop + .expression("[^,]+,") // operator + .number("([01]),") // ignition + .number("([01]),") // charge + .number("(d+.d+),") // power + .number("(d+.d+),") // battery + .number("[01],") // emergency + .expression("[CO],") // tamper + .number("(d+),") // rssi + .number("(d+),") // mcc + .number("(d+),") // mnc + .number("(x+),") // lac + .number("(x+),") // cid + .number("(d+),(x+),(x+),") // cell 1 + .number("(d+),(x+),(x+),") // cell 2 + .number("(d+),(x+),(x+),") // cell 3 + .number("(d+),(x+),(x+),") // cell 4 + .number("([01])+,") // inputs + .number("([01])+,") // outputs + .number("d+,") // frame number + .number("(d+.d+),") // adc1 + .number("(d+.d+),") // adc2 + .number("d+,") // delta distance + .any() + .compile(); + + private Position decodeB(Channel channel, SocketAddress remoteAddress, String sentence) { + + Parser parser = new Parser(PATTERN_B, 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.nextInt() == 1); + position.setTime(parser.nextDateTime(Parser.DateTimeFormat.DMY_HMS)); + position.setLatitude(parser.nextCoordinate(Parser.CoordinateFormat.DEG_HEM)); + position.setLongitude(parser.nextCoordinate(Parser.CoordinateFormat.DEG_HEM)); + position.setSpeed(UnitsConverter.knotsFromKph(parser.nextDouble())); + position.setCourse(parser.nextInt()); + + position.set(Position.KEY_SATELLITES, parser.nextInt()); + + position.setAltitude(parser.nextDouble()); + + position.set(Position.KEY_PDOP, parser.nextDouble()); + position.set(Position.KEY_HDOP, parser.nextDouble()); + position.set(Position.KEY_IGNITION, parser.nextInt() == 1); + position.set(Position.KEY_CHARGE, parser.nextInt() == 1); + position.set(Position.KEY_POWER, parser.nextDouble()); + position.set(Position.KEY_BATTERY, parser.nextDouble()); + + Network network = new Network(); + + int rssi = parser.nextInt(); + int mcc = parser.nextInt(); + int mnc = parser.nextInt(); + + network.addCellTower(CellTower.from(mcc, mnc, parser.nextHexInt(), parser.nextHexInt(), rssi)); + for (int i = 0; i < 4; i++) { + rssi = parser.nextInt(); + network.addCellTower(CellTower.from(mcc, mnc, parser.nextHexInt(), parser.nextHexInt(), rssi)); + } + + position.setNetwork(network); + + position.set(Position.KEY_INPUT, parser.nextBinInt()); + position.set(Position.KEY_OUTPUT, parser.nextBinInt()); + position.set(Position.PREFIX_ADC + 1, parser.nextDouble()); + position.set(Position.PREFIX_ADC + 2, parser.nextDouble()); + + return position; + } + + @Override + protected Object decode( + Channel channel, SocketAddress remoteAddress, Object msg) throws Exception { + + String sentence = (String) msg; + + if (sentence.startsWith("$$")) { + return decodeA(channel, remoteAddress, sentence); + } else { + return decodeB(channel, remoteAddress, sentence); + } + } + } diff --git a/test/org/traccar/protocol/AquilaProtocolDecoderTest.java b/test/org/traccar/protocol/AquilaProtocolDecoderTest.java index 1148896c1..8984cfcb3 100644 --- a/test/org/traccar/protocol/AquilaProtocolDecoderTest.java +++ b/test/org/traccar/protocol/AquilaProtocolDecoderTest.java @@ -6,7 +6,7 @@ import org.traccar.ProtocolTest; public class AquilaProtocolDecoderTest extends ProtocolTest { @Test - public void testDecode() throws Exception { + public void testDecodeA() throws Exception { AquilaProtocolDecoder decoder = new AquilaProtocolDecoder(new AquilaProtocol()); @@ -51,4 +51,20 @@ public class AquilaProtocolDecoderTest extends ProtocolTest { } + @Test + public void testDecodeB() throws Exception { + + AquilaProtocolDecoder decoder = new AquilaProtocolDecoder(new AquilaProtocol()); + + verifyPosition(decoder, text( + "$Header,nliven,1_37T02B0164MAIS,BR,6,L,861693034634154,KA01I2000,1,09112017,160702,12.976593,N,77.549782,E,25.1,344,15,911.0,1.04,0.68,Airtel,1,1,11.8,3.8,1,C,24,404,45,61b4,9ad9,31,9adb,61b4,35,ffff,0000,33,ffff,0000,31,ffff,0000,0001,00,000014,0.0,0.1,4,()*1E")); + + verifyPosition(decoder, text( + "$Header,iTriangle,1_37T02B0164MAIS_2,NR,1,L,864495034490141,KA01I2000,1,31032018,122247,22.845999,N,75.949005,E,0.0,44,16,545.0,1.19,0.65,AirTel,1,1,12.0,4.3,0,C,13,404,93,0456,16db,27,16dd,0456,22,3843,18ab,19,ebd8,0458,14,072c,18ab,0101,00,003735,0.0,0.0,0,()*48")); + + verifyNull(decoder, text( + "$Header,nliven,KA01I2000,861693034634154,1_37T02B0164MAIS,AIS140,12.976545,N,77.549759,E*50")); + + } + } -- cgit v1.2.3 From a0f83250c58b7d10b345370438d7b1923499294c Mon Sep 17 00:00:00 2001 From: Anton Tananaev Date: Fri, 6 Apr 2018 02:48:02 +1200 Subject: Add Watch alarm test case --- test/org/traccar/protocol/WatchProtocolDecoderTest.java | 3 +++ 1 file changed, 3 insertions(+) diff --git a/test/org/traccar/protocol/WatchProtocolDecoderTest.java b/test/org/traccar/protocol/WatchProtocolDecoderTest.java index 50f4c2ec0..bc567bc4d 100644 --- a/test/org/traccar/protocol/WatchProtocolDecoderTest.java +++ b/test/org/traccar/protocol/WatchProtocolDecoderTest.java @@ -10,6 +10,9 @@ public class WatchProtocolDecoderTest extends ProtocolTest { WatchProtocolDecoder decoder = new WatchProtocolDecoder(new WatchProtocol()); + verifyPosition(decoder, buffer( + "[ZJ*014111001332708*0075*0064*AL,040418,052156,A,22.536207,N,113.938673,E,0,0,0,5,100,82,1000,50,00100000,1,255,460,0,9340,3663,35]")); + verifyPosition(decoder, buffer( "[SG*352661090143150*006C*UD,150817,132115,V,28.435142,N,81.354333,W,2.2038,000,99,00,70,100,0,50,00000000,1,1,310,260,1091,30082,139,,00]")); -- cgit v1.2.3 From 2a768a85e6de2a0b7049764b1f936d8cbcbe23bc Mon Sep 17 00:00:00 2001 From: Anton Tananaev Date: Fri, 6 Apr 2018 04:46:51 +1200 Subject: Decode Totem IO values --- src/org/traccar/model/Position.java | 1 + src/org/traccar/protocol/TotemProtocolDecoder.java | 13 ++++++++++++- 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/src/org/traccar/model/Position.java b/src/org/traccar/model/Position.java index 49e3231c3..fca0f16e3 100644 --- a/src/org/traccar/model/Position.java +++ b/src/org/traccar/model/Position.java @@ -58,6 +58,7 @@ public class Position extends Message { public static final String KEY_TYPE = "type"; public static final String KEY_IGNITION = "ignition"; public static final String KEY_FLAGS = "flags"; + public static final String KEY_ANTENNA = "antenna"; public static final String KEY_CHARGE = "charge"; public static final String KEY_IP = "ip"; public static final String KEY_ARCHIVE = "archive"; diff --git a/src/org/traccar/protocol/TotemProtocolDecoder.java b/src/org/traccar/protocol/TotemProtocolDecoder.java index 8da188f60..d6dd933b5 100644 --- a/src/org/traccar/protocol/TotemProtocolDecoder.java +++ b/src/org/traccar/protocol/TotemProtocolDecoder.java @@ -243,12 +243,23 @@ public class TotemProtocolDecoder extends BaseProtocolDecoder { position.set(Position.KEY_HDOP, parser.nextDouble()); } - position.set(Position.PREFIX_IO + 1, parser.next()); + String io = parser.next(); if (pattern == PATTERN1) { + for (int i = 1; i <= 4; i++) { + position.set(Position.PREFIX_IN + i, io.charAt(3 + i) == '1'); + } position.set(Position.KEY_BATTERY, parser.nextDouble(0) * 0.01); } else { + position.set(Position.KEY_ANTENNA, io.charAt(0) == '1'); + position.set(Position.KEY_CHARGE, io.charAt(1) == '1'); + for (int i = 1; i <= 6; i++) { + position.set(Position.PREFIX_IN + i, io.charAt(1 + i) == '1'); + } position.set(Position.KEY_BATTERY, parser.nextDouble(0) * 0.1); } + for (int i = 1; i <= 4; i++) { + position.set(Position.PREFIX_OUT + i, io.charAt(7 + i) == '1'); + } position.set(Position.KEY_POWER, parser.nextDouble(0)); position.set(Position.PREFIX_ADC + 1, parser.next()); -- cgit v1.2.3 From 6b0689b67d46e5503087e8bc3a53abb02769a461 Mon Sep 17 00:00:00 2001 From: Anton Tananaev Date: Fri, 6 Apr 2018 05:36:59 +1200 Subject: Reimplement GoSafe protocol decoder --- .../traccar/protocol/GoSafeProtocolDecoder.java | 250 ++++++++++----------- .../protocol/GoSafeProtocolDecoderTest.java | 3 + 2 files changed, 125 insertions(+), 128 deletions(-) diff --git a/src/org/traccar/protocol/GoSafeProtocolDecoder.java b/src/org/traccar/protocol/GoSafeProtocolDecoder.java index 44dfb08a2..0fa1934da 100644 --- a/src/org/traccar/protocol/GoSafeProtocolDecoder.java +++ b/src/org/traccar/protocol/GoSafeProtocolDecoder.java @@ -45,75 +45,7 @@ public class GoSafeProtocolDecoder extends BaseProtocolDecoder { .number("(d+),") // imei .number("(dd)(dd)(dd)") // time (hhmmss) .number("(dd)(dd)(dd),") // date (ddmmyy) - .expression("(.*)#?") // data - .compile(); - - private static final Pattern PATTERN_ITEM = new PatternBuilder() - .number("(x+)?,").optional() // event - .groupBegin() - .text("SYS:") - .expression("[^,]*,") - .groupEnd("?") - .groupBegin() - .text("GPS:") - .expression("([AV]);") // validity - .number("(d+);") // satellites - .number("([NS])(d+.d+);") // latitude - .number("([EW])(d+.d+);") // longitude - .number("(d+)?;") // speed - .number("(d+);") // course - .number("(d+);") // altitude - .number("(d+.d+)") // hdop - .number(";(d+.d+)").optional() // vdop - .expression(",?") - .groupEnd() - .groupBegin() - .text("GSM:") - .number("d*;") // registration - .number("d*;") // gsm signal - .number("(d+);") // mcc - .number("(d+);") // mnc - .number("(x+);") // lac - .number("(x+);") // cid - .number("(-d+)") // rssi - .expression("[^,]*,?") - .groupEnd("?") - .groupBegin() - .text("COT:") - .number("(d+)") // odometer - .number("(?:;d+-d+-d+)?") // engine hours - .expression(",?") - .groupEnd("?") - .groupBegin() - .text("ADC:") - .number("(d+.d+)") // power - .number("(?:;(d+.d+))?,?") // battery - .groupEnd("?") - .groupBegin() - .text("DTT:") - .number("(x+);") // status - .number("(x+)?;") // io - .number("(x+);") // geo-fence 0-119 - .number("(x+);") // geo-fence 120-155 - .number("(x+)") // event status - .number("(?:;(x+))?,?") // packet type - .groupEnd("?") - .groupBegin() - .text("ETD:").expression("([^,]+),?") - .groupEnd("?") - .groupBegin() - .text("OBD:") - .number("(x+),?") - .groupEnd("?") - .groupBegin() - .text("FUL:").expression("[^,]*,?") - .groupEnd("?") - .groupBegin() - .text("TRU:").expression("[^,]*,?") - .groupEnd("?") - .groupBegin() - .text("TAG:").expression("([^,]+),?") - .groupEnd("?") + .expression("([^#]*)#?") // data .compile(); private static final Pattern PATTERN_OLD = new PatternBuilder() @@ -133,70 +65,138 @@ public class GoSafeProtocolDecoder extends BaseProtocolDecoder { .any() .compile(); - private Position decodePosition(DeviceSession deviceSession, Parser parser, Date time) { + private void decodeFragment(Position position, String fragment) { + int dataIndex = fragment.indexOf(':'); + int index = 0; + String[] values = fragment.substring(dataIndex + 1).split(";"); + switch (fragment.substring(0, dataIndex)) { + case "GPS": + position.setValid(values[index++].equals("A")); + position.set(Position.KEY_SATELLITES, Integer.parseInt(values[index++])); + position.setLatitude(Double.parseDouble(values[index].substring(1))); + if (values[index++].charAt(0) == 'S') { + position.setLatitude(-position.getLatitude()); + } + position.setLongitude(Double.parseDouble(values[index].substring(1))); + if (values[index++].charAt(0) == 'W') { + position.setLongitude(-position.getLongitude()); + } + if (!values[index++].isEmpty()) { + position.setSpeed(UnitsConverter.knotsFromKph(Integer.parseInt(values[index - 1]))); + } + position.setCourse(Integer.parseInt(values[index++])); + position.setAltitude(Integer.parseInt(values[index++])); + if (index < values.length) { + position.set(Position.KEY_HDOP, Double.parseDouble(values[index++])); + } + if (index < values.length) { + position.set(Position.KEY_VDOP, Double.parseDouble(values[index++])); + } + break; + case "GSM": + index += 1; // registration status + index += 1; // signal strength + position.setNetwork(new Network(CellTower.from( + Integer.parseInt(values[index++]), Integer.parseInt(values[index++]), + Integer.parseInt(values[index++], 16), Integer.parseInt(values[index++], 16), + Integer.parseInt(values[index++])))); + break; + case "COT": + if (index < values.length) { + position.set(Position.KEY_ODOMETER, Integer.parseInt(values[index++])); + } + if (index < values.length) { + String[] hours = values[index].split("-"); + position.set(Position.KEY_HOURS, Integer.parseInt(hours[0]) + + Integer.parseInt(hours[0]) / 60.0 + Integer.parseInt(hours[0]) / 3600.0); + } + break; + case "ADC": + position.set(Position.KEY_POWER, Double.parseDouble(values[index++])); + if (index < values.length) { + position.set(Position.KEY_BATTERY, Double.parseDouble(values[index++])); + } + if (index < values.length) { + position.set(Position.PREFIX_ADC + 1, Double.parseDouble(values[index++])); + } + if (index < values.length) { + position.set(Position.PREFIX_ADC + 2, Double.parseDouble(values[index++])); + } + break; + case "DTT": + position.set(Position.KEY_STATUS, Integer.parseInt(values[index++], 16)); + if (!values[index++].isEmpty()) { + int io = Integer.parseInt(values[index - 1], 16); + position.set(Position.KEY_IGNITION, BitUtil.check(io, 0)); + position.set(Position.PREFIX_IN + 1, BitUtil.check(io, 1)); + position.set(Position.PREFIX_IN + 2, BitUtil.check(io, 2)); + position.set(Position.PREFIX_IN + 3, BitUtil.check(io, 3)); + position.set(Position.PREFIX_IN + 4, BitUtil.check(io, 4)); + position.set(Position.PREFIX_OUT + 1, BitUtil.check(io, 5)); + position.set(Position.PREFIX_OUT + 2, BitUtil.check(io, 6)); + position.set(Position.PREFIX_OUT + 3, BitUtil.check(io, 7)); + } + position.set(Position.KEY_GEOFENCE, values[index++] + values[index++]); + position.set("eventStatus", values[index++]); + if (index < values.length) { + position.set("packetType", values[index++]); + } + break; + case "ETD": + position.set("eventData", values[index++]); + break; + case "OBD": + position.set("obd", values[index++]); + break; + case "TAG": + position.set("tagData", values[index++]); + break; + case "IWD": + if (index < values.length && values[index + 1].equals("0")) { + position.set(Position.KEY_DRIVER_UNIQUE_ID, values[index + 2]); + } + break; + default: + break; + } + } - Position position = new Position(getProtocolName()); - position.setDeviceId(deviceSession.getDeviceId()); + private Object decodeData(DeviceSession deviceSession, Date time, String data) { - if (time != null) { - position.setTime(time); - } + List positions = new LinkedList<>(); + Position position = null; + int index = 0; + String[] fragments = data.split(","); - position.set(Position.KEY_EVENT, parser.next()); + while (index < fragments.length) { - position.setValid(parser.next().equals("A")); - position.set(Position.KEY_SATELLITES, parser.nextInt()); + if (fragments[index].isEmpty() || Character.isDigit(fragments[index].charAt(0))) { - position.setLatitude(parser.nextCoordinate(Parser.CoordinateFormat.HEM_DEG)); - position.setLongitude(parser.nextCoordinate(Parser.CoordinateFormat.HEM_DEG)); - position.setSpeed(UnitsConverter.knotsFromKph(parser.nextDouble(0))); - position.setCourse(parser.nextDouble(0)); - position.setAltitude(parser.nextDouble(0)); + if (position != null) { + positions.add(position); + } - position.set(Position.KEY_HDOP, parser.nextDouble(0)); - position.set(Position.KEY_VDOP, parser.nextDouble(0)); + position = new Position(getProtocolName()); + position.setDeviceId(deviceSession.getDeviceId()); + position.setTime(time); - if (parser.hasNext(5)) { - position.setNetwork(new Network(CellTower.from(parser.nextInt(0), parser.nextInt(0), - parser.nextHexInt(0), parser.nextHexInt(0), parser.nextInt(0)))); - } - if (parser.hasNext()) { - position.set(Position.KEY_ODOMETER, parser.nextInt(0)); - } - position.set(Position.KEY_POWER, parser.nextDouble()); - position.set(Position.KEY_BATTERY, parser.nextDouble()); - - if (parser.hasNext(6)) { - position.set(Position.KEY_STATUS, parser.nextHexLong()); - Integer io = parser.nextHexInt(); - if (io != null) { - position.set(Position.KEY_IGNITION, BitUtil.check(io, 0)); - position.set(Position.PREFIX_IN + 1, BitUtil.check(io, 1)); - position.set(Position.PREFIX_IN + 2, BitUtil.check(io, 2)); - position.set(Position.PREFIX_IN + 3, BitUtil.check(io, 3)); - position.set(Position.PREFIX_IN + 4, BitUtil.check(io, 4)); - position.set(Position.PREFIX_OUT + 1, BitUtil.check(io, 5)); - position.set(Position.PREFIX_OUT + 2, BitUtil.check(io, 6)); - position.set(Position.PREFIX_OUT + 3, BitUtil.check(io, 7)); - } - position.set(Position.KEY_GEOFENCE, parser.next() + parser.next()); - position.set("eventStatus", parser.next()); - position.set("packetType", parser.next()); - } + if (!fragments[index++].isEmpty()) { + position.set(Position.KEY_EVENT, Integer.parseInt(fragments[index - 1])); + } - if (parser.hasNext()) { - position.set("eventData", parser.next()); - } + } else { + + decodeFragment(position, fragments[index++]); + + } - if (parser.hasNext()) { - position.set("obd", parser.next()); } - if (parser.hasNext()) { - position.set("tagData", parser.next()); + if (position != null) { + positions.add(position); } - return position; + return positions; } @Override @@ -246,18 +246,12 @@ public class GoSafeProtocolDecoder extends BaseProtocolDecoder { } else { - Date time = null; + Date time = new Date(); if (parser.hasNext(6)) { time = parser.nextDateTime(Parser.DateTimeFormat.HMS_DMY); } - List positions = new LinkedList<>(); - Parser itemParser = new Parser(PATTERN_ITEM, parser.next()); - while (itemParser.find()) { - positions.add(decodePosition(deviceSession, itemParser, time)); - } - - return positions; + return decodeData(deviceSession, time, parser.next()); } } diff --git a/test/org/traccar/protocol/GoSafeProtocolDecoderTest.java b/test/org/traccar/protocol/GoSafeProtocolDecoderTest.java index 3e4e8f413..5485368b9 100644 --- a/test/org/traccar/protocol/GoSafeProtocolDecoderTest.java +++ b/test/org/traccar/protocol/GoSafeProtocolDecoderTest.java @@ -10,6 +10,9 @@ public class GoSafeProtocolDecoderTest extends ProtocolTest { GoSafeProtocolDecoder decoder = new GoSafeProtocolDecoder(new GoSafeProtocol()); + verifyPositions(decoder, text( + "*GS06,860078024287174,070120310318,,SYS:G3SC;V3.32;V1.1.8,GPS:A;9;N23.169946;E113.450568;0;0;23;0.86,COT:65;20,ADC:4.27;3.73;0.01;0.02,DTT:4004;E0;0;0;0;1,IWD:0;0;000000000000#")); + verifyPositions(decoder, text( "*GS06,860078024213915,032544190318,,SYS:G3SC;V3.32;V1.1.8,GPS:A;7;N3.052417;E101.787112;0;0;94;1.38,COT:686;0-0-0,ADC:16.25;4.09,DTT:4000;E0;0;0;0;1#")); -- cgit v1.2.3 From 590dbd630b8ceddab01d45119f63d55f21140721 Mon Sep 17 00:00:00 2001 From: Anton Tananaev Date: Fri, 6 Apr 2018 05:43:55 +1200 Subject: Decode EasyTrack alarms --- .../traccar/protocol/EasyTrackProtocolDecoder.java | 32 ++++++++++++++++++++-- 1 file changed, 30 insertions(+), 2 deletions(-) diff --git a/src/org/traccar/protocol/EasyTrackProtocolDecoder.java b/src/org/traccar/protocol/EasyTrackProtocolDecoder.java index 50b21841b..16ced37ae 100644 --- a/src/org/traccar/protocol/EasyTrackProtocolDecoder.java +++ b/src/org/traccar/protocol/EasyTrackProtocolDecoder.java @@ -1,5 +1,5 @@ /* - * Copyright 2013 - 2017 Anton Tananaev (anton@traccar.org) + * Copyright 2013 - 2018 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. @@ -54,6 +54,31 @@ public class EasyTrackProtocolDecoder extends BaseProtocolDecoder { .any() .compile(); + private String decodeAlarm(long status) { + if ((status & 0x02000000) > 0) { + return Position.ALARM_GEOFENCE_ENTER; + } + if ((status & 0x04000000) > 0) { + return Position.ALARM_GEOFENCE_EXIT; + } + if ((status & 0x08000000) > 0) { + return Position.ALARM_LOW_BATTERY; + } + if ((status & 0x20000000) > 0) { + return Position.ALARM_VIBRATION; + } + if ((status & 0x80000000) > 0) { + return Position.ALARM_OVERSPEED; + } + if ((status & 0x00010000) > 0) { + return Position.ALARM_SOS; + } + if ((status & 0x00040000) > 0) { + return Position.ALARM_POWER_CUT; + } + return null; + } + @Override protected Object decode( Channel channel, SocketAddress remoteAddress, Object msg) throws Exception { @@ -95,7 +120,10 @@ public class EasyTrackProtocolDecoder extends BaseProtocolDecoder { position.setSpeed(UnitsConverter.knotsFromKph(parser.nextHexInt(0) / 100.0)); position.setCourse(parser.nextHexInt(0) / 100.0); - position.set(Position.KEY_STATUS, parser.next()); + long status = parser.nextHexLong(); + position.set(Position.KEY_STATUS, status); + position.set(Position.KEY_ALARM, decodeAlarm(status)); + position.set("signal", parser.next()); position.set(Position.KEY_POWER, parser.nextDouble(0)); position.set("oil", parser.nextHexInt(0)); -- cgit v1.2.3 From 4d80cb140474f5d47d1dca67580bb2db0f829a9f Mon Sep 17 00:00:00 2001 From: Ivan Martinez Date: Fri, 6 Apr 2018 19:02:40 -0300 Subject: add position accuracy to log --- src/org/traccar/MainEventHandler.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/org/traccar/MainEventHandler.java b/src/org/traccar/MainEventHandler.java index 8e88e15b9..3c8876846 100644 --- a/src/org/traccar/MainEventHandler.java +++ b/src/org/traccar/MainEventHandler.java @@ -66,7 +66,8 @@ public class MainEventHandler extends IdleStateAwareChannelHandler { s.append("lat: ").append(String.format("%.5f", position.getLatitude())).append(", "); s.append("lon: ").append(String.format("%.5f", position.getLongitude())).append(", "); s.append("speed: ").append(String.format("%.1f", position.getSpeed())).append(", "); - s.append("course: ").append(String.format("%.1f", position.getCourse())); + s.append("course: ").append(String.format("%.1f", position.getCourse())).append(", "); + s.append("accuracy: ").append(String.format("%.1f", position.getAccuracy())); Object cmdResult = position.getAttributes().get(Position.KEY_RESULT); if (cmdResult != null) { s.append(", result: ").append(cmdResult); -- cgit v1.2.3 From 8d0dadde10bad915c4571d227ccb17a2810a44bb Mon Sep 17 00:00:00 2001 From: Ivan Martinez Date: Fri, 6 Apr 2018 19:19:02 -0300 Subject: check for valid speed and accuracy before printing, remove uncessary calls to append with comma --- src/org/traccar/MainEventHandler.java | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/src/org/traccar/MainEventHandler.java b/src/org/traccar/MainEventHandler.java index 3c8876846..6228f3709 100644 --- a/src/org/traccar/MainEventHandler.java +++ b/src/org/traccar/MainEventHandler.java @@ -60,14 +60,18 @@ public class MainEventHandler extends IdleStateAwareChannelHandler { // Log position StringBuilder s = new StringBuilder(); s.append(formatChannel(e.getChannel())).append(" "); - s.append("id: ").append(uniqueId).append(", "); - s.append("time: ").append( - new SimpleDateFormat(Log.DATE_FORMAT).format(position.getFixTime())).append(", "); - s.append("lat: ").append(String.format("%.5f", position.getLatitude())).append(", "); - s.append("lon: ").append(String.format("%.5f", position.getLongitude())).append(", "); - s.append("speed: ").append(String.format("%.1f", position.getSpeed())).append(", "); - s.append("course: ").append(String.format("%.1f", position.getCourse())).append(", "); - s.append("accuracy: ").append(String.format("%.1f", position.getAccuracy())); + s.append("id: ").append(uniqueId); + s.append(", time: ").append( + new SimpleDateFormat(Log.DATE_FORMAT).format(position.getFixTime())); + s.append(", lat: ").append(String.format("%.5f", position.getLatitude())); + s.append(", lon: ").append(String.format("%.5f", position.getLongitude())); + if (position.getSpeed() > 0) { + s.append(", speed: ").append(String.format("%.1f", position.getSpeed())); + } + s.append(", course: ").append(String.format("%.1f", position.getCourse())); + if (position.getAccuracy() > 0) { + s.append(", accuracy: ").append(String.format("%.1f", position.getAccuracy())); + } Object cmdResult = position.getAttributes().get(Position.KEY_RESULT); if (cmdResult != null) { s.append(", result: ").append(cmdResult); -- cgit v1.2.3 From 63dc8265e5e1bc55a2c32a46b37470395c65bcb4 Mon Sep 17 00:00:00 2001 From: Ivan Martinez Date: Sun, 8 Apr 2018 10:21:13 -0300 Subject: use geofence attribute to permit per polyline distance configuration --- src/org/traccar/model/Geofence.java | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/org/traccar/model/Geofence.java b/src/org/traccar/model/Geofence.java index 7042325dc..acc81c203 100644 --- a/src/org/traccar/model/Geofence.java +++ b/src/org/traccar/model/Geofence.java @@ -65,7 +65,9 @@ public class Geofence extends ScheduledModel { } else if (area.startsWith("POLYGON")) { geometry = new GeofencePolygon(area); } else if (area.startsWith("LINESTRING")) { - geometry = new GeofencePolyline(area, Context.getConfig().getDouble("geofence.polylineDistance", 25)); + final double distance = getDouble("polylineDistance"); + geometry = new GeofencePolyline(area, distance >= 1 ? distance + : Context.getConfig().getDouble("geofence.polylineDistance", 25)); } else { throw new ParseException("Unknown geometry type", 0); } -- cgit v1.2.3 From 9ff9232e779f7e137fa8b0cba2e0b424ff4879f9 Mon Sep 17 00:00:00 2001 From: Ivan Martinez Date: Sun, 8 Apr 2018 18:53:02 -0300 Subject: just consider zero to check if defined --- src/org/traccar/model/Geofence.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/org/traccar/model/Geofence.java b/src/org/traccar/model/Geofence.java index acc81c203..8560d22e9 100644 --- a/src/org/traccar/model/Geofence.java +++ b/src/org/traccar/model/Geofence.java @@ -66,7 +66,7 @@ public class Geofence extends ScheduledModel { geometry = new GeofencePolygon(area); } else if (area.startsWith("LINESTRING")) { final double distance = getDouble("polylineDistance"); - geometry = new GeofencePolyline(area, distance >= 1 ? distance + geometry = new GeofencePolyline(area, distance > 0 ? distance : Context.getConfig().getDouble("geofence.polylineDistance", 25)); } else { throw new ParseException("Unknown geometry type", 0); -- cgit v1.2.3 From 23c3c2da240e5c44c3d5a04e0714f6f02fa9777e Mon Sep 17 00:00:00 2001 From: Anton Tananaev Date: Mon, 9 Apr 2018 15:35:39 +1200 Subject: Decode watch SOS alarms --- src/org/traccar/protocol/WatchProtocolDecoder.java | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/org/traccar/protocol/WatchProtocolDecoder.java b/src/org/traccar/protocol/WatchProtocolDecoder.java index 68832cd3e..b58c3b6a1 100644 --- a/src/org/traccar/protocol/WatchProtocolDecoder.java +++ b/src/org/traccar/protocol/WatchProtocolDecoder.java @@ -242,11 +242,16 @@ public class WatchProtocolDecoder extends BaseProtocolDecoder { } else if (type.equals("UD") || type.equals("UD2") || type.equals("UD3") || type.equals("AL") || type.equals("WT")) { + Position position = decodePosition(deviceSession, buf.toString(StandardCharsets.US_ASCII)); + if (type.equals("AL")) { + if (position != null) { + position.set(Position.KEY_ALARM, Position.ALARM_SOS); + } sendResponse(channel, id, index, "AL"); } - return decodePosition(deviceSession, buf.toString(StandardCharsets.US_ASCII)); + return position; } else if (type.equals("TKQ")) { -- cgit v1.2.3 From 50009502e2a47bafafede983656f40f99745665a Mon Sep 17 00:00:00 2001 From: Anton Tananaev Date: Mon, 9 Apr 2018 16:21:40 +1200 Subject: Improve GPS103 low message decoding --- .../traccar/protocol/Gps103ProtocolDecoder.java | 233 +++++++++++---------- .../protocol/Gps103ProtocolDecoderTest.java | 3 + 2 files changed, 120 insertions(+), 116 deletions(-) diff --git a/src/org/traccar/protocol/Gps103ProtocolDecoder.java b/src/org/traccar/protocol/Gps103ProtocolDecoder.java index 85b4dcada..f32d22c00 100644 --- a/src/org/traccar/protocol/Gps103ProtocolDecoder.java +++ b/src/org/traccar/protocol/Gps103ProtocolDecoder.java @@ -1,5 +1,5 @@ /* - * Copyright 2012 - 2017 Anton Tananaev (anton@traccar.org) + * Copyright 2012 - 2018 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. @@ -26,6 +26,7 @@ import org.traccar.model.Network; import org.traccar.model.Position; import java.net.SocketAddress; +import java.util.regex.Matcher; import java.util.regex.Pattern; public class Gps103ProtocolDecoder extends BaseProtocolDecoder { @@ -38,10 +39,19 @@ public class Gps103ProtocolDecoder extends BaseProtocolDecoder { .text("imei:") .number("(d+),") // imei .expression("([^,]+),") // alarm + .groupBegin() .number("(dd)/?(dd)/?(dd) ?") // local date (yymmdd) .number("(dd):?(dd)(?:dd)?,") // local time (hhmmss) + .or() + .number("d*,") + .groupEnd() .expression("([^,]+)?,") // rfid - .expression("[FL],") // full / low + .groupBegin() + .text("L,,,") + .number("(x+),,") // lac + .number("(x+),,,") // cid + .or() + .text("F,") .groupBegin() .number("(dd)(dd)(dd).d+") // time utc (hhmmss) .or() @@ -63,24 +73,10 @@ public class Gps103ProtocolDecoder extends BaseProtocolDecoder { .expression("([^,;]+)?,?") .expression("([^,;]+)?,?") .expression("([^,;]+)?,?") + .groupEnd() .any() .compile(); - private static final Pattern PATTERN_NETWORK = new PatternBuilder() - .text("imei:") - .number("(d+),") // imei - .expression("[^,]+,") // alarm - .number("d*,,") - .text("L,,,") - .number("(x+),,") // lac - .number("(x+),,,") // cid - .any() - .compile(); - - private static final Pattern PATTERN_HANDSHAKE = new PatternBuilder() - .number("##,imei:(d+),A") - .compile(); - private static final Pattern PATTERN_OBD = new PatternBuilder() .text("imei:") .number("(d+),") // imei @@ -143,85 +139,9 @@ public class Gps103ProtocolDecoder extends BaseProtocolDecoder { } } - @Override - protected Object decode( - Channel channel, SocketAddress remoteAddress, Object msg) throws Exception { - - String sentence = (String) msg; - - // Send response #1 - if (sentence.contains("##")) { - if (channel != null) { - channel.write("LOAD", remoteAddress); - Parser handshakeParser = new Parser(PATTERN_HANDSHAKE, sentence); - if (handshakeParser.matches()) { - getDeviceSession(channel, remoteAddress, handshakeParser.next()); - } - } - return null; - } - - // Send response #2 - if (!sentence.isEmpty() && Character.isDigit(sentence.charAt(0))) { - if (channel != null) { - channel.write("ON", remoteAddress); - } - int start = sentence.indexOf("imei:"); - if (start >= 0) { - sentence = sentence.substring(start); - } else { - return null; - } - } - - Position position = new Position(getProtocolName()); - - Parser parser = new Parser(PATTERN_NETWORK, sentence); - if (parser.matches()) { - - DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, parser.next()); - if (deviceSession == null) { - return null; - } - position.setDeviceId(deviceSession.getDeviceId()); - - getLastLocation(position, null); - - position.setNetwork(new Network( - CellTower.fromLacCid(parser.nextHexInt(0), parser.nextHexInt(0)))); - - return position; - - } - - parser = new Parser(PATTERN_OBD, sentence); - if (parser.matches()) { - - DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, parser.next()); - if (deviceSession == null) { - return null; - } - position.setDeviceId(deviceSession.getDeviceId()); - - getLastLocation(position, parser.nextDateTime()); - - position.set(Position.KEY_ODOMETER, parser.nextInt(0)); - parser.nextDouble(0); // instant fuel consumption - position.set(Position.KEY_FUEL_CONSUMPTION, parser.nextDouble(0)); - position.set(Position.KEY_HOURS, parser.nextInt()); - position.set(Position.KEY_OBD_SPEED, parser.nextInt(0)); - position.set(Position.KEY_ENGINE_LOAD, parser.next()); - position.set(Position.KEY_COOLANT_TEMP, parser.nextInt()); - position.set(Position.KEY_THROTTLE, parser.next()); - position.set(Position.KEY_RPM, parser.nextInt(0)); - position.set(Position.KEY_BATTERY, parser.nextDouble(0)); - position.set(Position.KEY_DTCS, parser.next().replace(',', ' ').trim()); - - return position; + private Position decodeRegular(Channel channel, SocketAddress remoteAddress, String sentence) { - } - - parser = new Parser(PATTERN, sentence); + Parser parser = new Parser(PATTERN, sentence); if (!parser.matches()) { return null; } @@ -231,6 +151,8 @@ public class Gps103ProtocolDecoder extends BaseProtocolDecoder { if (deviceSession == null) { return null; } + + Position position = new Position(getProtocolName()); position.setDeviceId(deviceSession.getDeviceId()); String alarm = parser.next(); @@ -262,36 +184,115 @@ public class Gps103ProtocolDecoder extends BaseProtocolDecoder { position.set(Position.KEY_DRIVER_UNIQUE_ID, rfid); } - String utcHours = parser.next(); - String utcMinutes = parser.next(); + if (parser.hasNext(2)) { - dateBuilder.setTime(localHours, localMinutes, parser.nextInt(0)); + getLastLocation(position, null); + + position.setNetwork(new Network(CellTower.fromLacCid(parser.nextHexInt(0), parser.nextHexInt(0)))); + + } else { + + String utcHours = parser.next(); + String utcMinutes = parser.next(); - // Timezone calculation - if (utcHours != null && utcMinutes != null) { - int deltaMinutes = (localHours - Integer.parseInt(utcHours)) * 60; - deltaMinutes += localMinutes - Integer.parseInt(utcMinutes); - if (deltaMinutes <= -12 * 60) { - deltaMinutes += 24 * 60; - } else if (deltaMinutes > 12 * 60) { - deltaMinutes -= 24 * 60; + dateBuilder.setTime(localHours, localMinutes, parser.nextInt(0)); + + // Timezone calculation + if (utcHours != null && utcMinutes != null) { + int deltaMinutes = (localHours - Integer.parseInt(utcHours)) * 60; + deltaMinutes += localMinutes - Integer.parseInt(utcMinutes); + if (deltaMinutes <= -12 * 60) { + deltaMinutes += 24 * 60; + } else if (deltaMinutes > 12 * 60) { + deltaMinutes -= 24 * 60; + } + dateBuilder.addMinute(-deltaMinutes); + } + position.setTime(dateBuilder.getDate()); + + position.setValid(parser.next().equals("A")); + position.setFixTime(position.getDeviceTime()); + position.setLatitude(parser.nextCoordinate(Parser.CoordinateFormat.HEM_DEG_MIN_HEM)); + position.setLongitude(parser.nextCoordinate(Parser.CoordinateFormat.HEM_DEG_MIN_HEM)); + position.setSpeed(parser.nextDouble(0)); + position.setCourse(parser.nextDouble(0)); + position.setAltitude(parser.nextDouble(0)); + + for (int i = 1; i <= 5; i++) { + position.set(Position.PREFIX_IO + i, parser.next()); } - dateBuilder.addMinute(-deltaMinutes); + } - position.setTime(dateBuilder.getDate()); - position.setValid(parser.next().equals("A")); - position.setLatitude(parser.nextCoordinate(Parser.CoordinateFormat.HEM_DEG_MIN_HEM)); - position.setLongitude(parser.nextCoordinate(Parser.CoordinateFormat.HEM_DEG_MIN_HEM)); - position.setSpeed(parser.nextDouble(0)); - position.setCourse(parser.nextDouble(0)); - position.setAltitude(parser.nextDouble(0)); + return position; + } + + private Position decodeObd(Channel channel, SocketAddress remoteAddress, String sentence) { - for (int i = 1; i <= 5; i++) { - position.set(Position.PREFIX_IO + i, parser.next()); + Parser parser = new Parser(PATTERN_OBD, 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()); + + getLastLocation(position, parser.nextDateTime()); + + position.set(Position.KEY_ODOMETER, parser.nextInt(0)); + parser.nextDouble(0); // instant fuel consumption + position.set(Position.KEY_FUEL_CONSUMPTION, parser.nextDouble(0)); + position.set(Position.KEY_HOURS, parser.nextInt()); + position.set(Position.KEY_OBD_SPEED, parser.nextInt(0)); + position.set(Position.KEY_ENGINE_LOAD, parser.next()); + position.set(Position.KEY_COOLANT_TEMP, parser.nextInt()); + position.set(Position.KEY_THROTTLE, parser.next()); + position.set(Position.KEY_RPM, parser.nextInt(0)); + position.set(Position.KEY_BATTERY, parser.nextDouble(0)); + position.set(Position.KEY_DTCS, parser.next().replace(',', ' ').trim()); + return position; } + @Override + protected Object decode( + Channel channel, SocketAddress remoteAddress, Object msg) throws Exception { + + String sentence = (String) msg; + + if (sentence.contains("##")) { + if (channel != null) { + channel.write("LOAD", remoteAddress); + Matcher matcher = Pattern.compile("##,imei:(\\d+),A").matcher(sentence); + if (matcher.matches()) { + getDeviceSession(channel, remoteAddress, matcher.group(1)); + } + } + return null; + } + + if (!sentence.isEmpty() && Character.isDigit(sentence.charAt(0))) { + if (channel != null) { + channel.write("ON", remoteAddress); + } + int start = sentence.indexOf("imei:"); + if (start >= 0) { + sentence = sentence.substring(start); + } else { + return null; + } + } + + if (sentence.contains("OBD")) { + return decodeObd(channel, remoteAddress, sentence); + } else { + return decodeRegular(channel, remoteAddress, sentence); + } + } + } diff --git a/test/org/traccar/protocol/Gps103ProtocolDecoderTest.java b/test/org/traccar/protocol/Gps103ProtocolDecoderTest.java index 668070e1a..17ed8da56 100644 --- a/test/org/traccar/protocol/Gps103ProtocolDecoderTest.java +++ b/test/org/traccar/protocol/Gps103ProtocolDecoderTest.java @@ -10,6 +10,9 @@ public class Gps103ProtocolDecoderTest extends ProtocolTest { Gps103ProtocolDecoder decoder = new Gps103ProtocolDecoder(new Gps103Protocol()); + verifyNotNull(decoder, text( + "imei:864895030279986,ac alarm,180404174252,,L,,,296a,,51f7,,,")); + verifyAttributes(decoder, text( "imei:359710048977327,OBD,180301094003,5000000,0.00,0.00,98,18,68.63%,55,25.10%,1368,14.24,,,,;")); -- cgit v1.2.3 From 7efecf8089ca11940f9ff749717e86c05c94c24c Mon Sep 17 00:00:00 2001 From: Anton Tananaev Date: Wed, 11 Apr 2018 00:56:07 +1200 Subject: Fix totem IO decoding --- src/org/traccar/protocol/TotemProtocolDecoder.java | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/org/traccar/protocol/TotemProtocolDecoder.java b/src/org/traccar/protocol/TotemProtocolDecoder.java index d6dd933b5..5a2c203d2 100644 --- a/src/org/traccar/protocol/TotemProtocolDecoder.java +++ b/src/org/traccar/protocol/TotemProtocolDecoder.java @@ -1,5 +1,5 @@ /* - * Copyright 2013 - 2016 Anton Tananaev (anton@traccar.org) + * Copyright 2013 - 2018 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. @@ -243,22 +243,22 @@ public class TotemProtocolDecoder extends BaseProtocolDecoder { position.set(Position.KEY_HDOP, parser.nextDouble()); } - String io = parser.next(); + int io = parser.nextBinInt(); if (pattern == PATTERN1) { for (int i = 1; i <= 4; i++) { - position.set(Position.PREFIX_IN + i, io.charAt(3 + i) == '1'); + position.set(Position.PREFIX_IN + i, BitUtil.check(io, 3 + i)); } position.set(Position.KEY_BATTERY, parser.nextDouble(0) * 0.01); } else { - position.set(Position.KEY_ANTENNA, io.charAt(0) == '1'); - position.set(Position.KEY_CHARGE, io.charAt(1) == '1'); + position.set(Position.KEY_ANTENNA, BitUtil.check(io, 0)); + position.set(Position.KEY_CHARGE, BitUtil.check(io, 1)); for (int i = 1; i <= 6; i++) { - position.set(Position.PREFIX_IN + i, io.charAt(1 + i) == '1'); + position.set(Position.PREFIX_IN + i, BitUtil.check(io, 1 + i)); } position.set(Position.KEY_BATTERY, parser.nextDouble(0) * 0.1); } for (int i = 1; i <= 4; i++) { - position.set(Position.PREFIX_OUT + i, io.charAt(7 + i) == '1'); + position.set(Position.PREFIX_OUT + i, BitUtil.check(io, 7 + i)); } position.set(Position.KEY_POWER, parser.nextDouble(0)); position.set(Position.PREFIX_ADC + 1, parser.next()); -- cgit v1.2.3 From bb071de84dd000ec3067991bb523fe5ef24b76e9 Mon Sep 17 00:00:00 2001 From: Anton Tananaev Date: Wed, 11 Apr 2018 20:31:38 +1200 Subject: Add PT60 test case --- test/org/traccar/protocol/Pt60ProtocolDecoderTest.java | 3 +++ 1 file changed, 3 insertions(+) diff --git a/test/org/traccar/protocol/Pt60ProtocolDecoderTest.java b/test/org/traccar/protocol/Pt60ProtocolDecoderTest.java index 5bea875e8..08fe7658e 100644 --- a/test/org/traccar/protocol/Pt60ProtocolDecoderTest.java +++ b/test/org/traccar/protocol/Pt60ProtocolDecoderTest.java @@ -16,6 +16,9 @@ public class Pt60ProtocolDecoderTest extends ProtocolTest { verifyNull(decoder, text( "@G#@,V01,1,353882080015633,9460025014649193,")); + verifyNull(decoder, text( + "@G#@,V01,43,105681639001701,9460000412618231,20180410092923,5,460;0;9763;3852;-63|460;0;9763;3851;-26|460;0;9763;4080;-22|460;0;9763;3593;-18|460;0;9763;3591;-10,5,14:b8:37:26:64:88;004300680069006e0061004e00650074002d006e0047006e0058;-76|08:9b:4b:93:b5:b1;005400480049004e004b0052004100430045;-77|ec:3d:fd:c9:38:4a;004b00460052006f0075007400650072;-78|b0:d5:9d:c6:f8:82;003300360030514d8d390057006900460069002d00380032;-81|02:fa:84:3b:fa:6a;00470075006500730074005f0032002e003400470048007a;-82,")); + } } -- cgit v1.2.3 From 5847136ad3ab150038439cf0ab5b13f0ee74a2fe Mon Sep 17 00:00:00 2001 From: Anton Tananaev Date: Thu, 12 Apr 2018 02:48:26 +1200 Subject: Handle PT502 binary frames --- src/org/traccar/protocol/Pt502FrameDecoder.java | 47 +++++++++++++++------- .../traccar/protocol/Pt502FrameDecoderTest.java | 4 ++ 2 files changed, 36 insertions(+), 15 deletions(-) diff --git a/src/org/traccar/protocol/Pt502FrameDecoder.java b/src/org/traccar/protocol/Pt502FrameDecoder.java index 252c8dd02..4d3e9b4d5 100644 --- a/src/org/traccar/protocol/Pt502FrameDecoder.java +++ b/src/org/traccar/protocol/Pt502FrameDecoder.java @@ -1,5 +1,5 @@ /* - * Copyright 2014 - 2017 Anton Tananaev (anton@traccar.org) + * Copyright 2014 - 2018 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. @@ -20,6 +20,8 @@ import org.jboss.netty.channel.Channel; import org.jboss.netty.channel.ChannelHandlerContext; import org.jboss.netty.handler.codec.frame.FrameDecoder; +import java.nio.charset.StandardCharsets; + public class Pt502FrameDecoder extends FrameDecoder { private static final int BINARY_HEADER = 5; @@ -28,26 +30,41 @@ public class Pt502FrameDecoder extends FrameDecoder { protected Object decode( ChannelHandlerContext ctx, Channel channel, ChannelBuffer buf) throws Exception { - if (buf.readableBytes() < BINARY_HEADER) { + if (buf.readableBytes() < 10) { return null; } - if (buf.getUnsignedByte(buf.readerIndex()) == 0xbf) { - buf.skipBytes(BINARY_HEADER); - } + if (buf.getUnsignedByte(buf.readerIndex()) == 0xbf + && buf.toString(buf.readerIndex() + BINARY_HEADER, 4, StandardCharsets.US_ASCII).equals("$PHD")) { - int index = buf.indexOf(buf.readerIndex(), buf.writerIndex(), (byte) '\r'); - if (index < 0) { - index = buf.indexOf(buf.readerIndex(), buf.writerIndex(), (byte) '\n'); - } + int length = buf.getUnsignedShort(buf.readerIndex() + 3); + if (buf.readableBytes() >= length) { + buf.skipBytes(BINARY_HEADER); + ChannelBuffer result = buf.readBytes(length - BINARY_HEADER - 2); + buf.skipBytes(2); // line break + return result; + } + + } else { - if (index > 0) { - ChannelBuffer result = buf.readBytes(index - buf.readerIndex()); - while (buf.readable() - && (buf.getByte(buf.readerIndex()) == '\r' || buf.getByte(buf.readerIndex()) == '\n')) { - buf.skipBytes(1); + if (buf.getUnsignedByte(buf.readerIndex()) == 0xbf) { + buf.skipBytes(BINARY_HEADER); } - return result; + + int index = buf.indexOf(buf.readerIndex(), buf.writerIndex(), (byte) '\r'); + if (index < 0) { + index = buf.indexOf(buf.readerIndex(), buf.writerIndex(), (byte) '\n'); + } + + if (index > 0) { + ChannelBuffer result = buf.readBytes(index - buf.readerIndex()); + while (buf.readable() + && (buf.getByte(buf.readerIndex()) == '\r' || buf.getByte(buf.readerIndex()) == '\n')) { + buf.skipBytes(1); + } + return result; + } + } return null; diff --git a/test/org/traccar/protocol/Pt502FrameDecoderTest.java b/test/org/traccar/protocol/Pt502FrameDecoderTest.java index 04180abb0..6c4dd03bd 100644 --- a/test/org/traccar/protocol/Pt502FrameDecoderTest.java +++ b/test/org/traccar/protocol/Pt502FrameDecoderTest.java @@ -12,6 +12,10 @@ public class Pt502FrameDecoderTest extends ProtocolTest { Pt502FrameDecoder decoder = new Pt502FrameDecoder(); + verifyFrame( + binary("24504844302c3936302cffd8ffdb008400140e0f120f0d14121012171514181e32211e1c1c1e3d2c2e243249404c4b47404645505a736250556d5645466488656d777b8182814e608d978c7d96737e817c011517171e1a1e3b21213b7c5346537c7c7c7c7c7c7c7c7c7c7c7c7c7c7c7c7c7c7c7c7c7c7c7c7c7c7c7c7c7c7c7c7c7c7c7c7c7c7c7c7c7c7c7c7c7c7c7c7c7cffc000110800f0014003012100021101031101ffdd0004000affc401a20000010501010101010100000000000000000102030405060708090a0b100002010303020403050504040000017d01020300041105122131410613516107227114328191a1082342b1c11552d1f02433627282090a161718191a25262728292a3435363738393a434445464748494a535455565758595a636465666768696a737475767778797a838485868788898a92939495969798999aa2a3a4a5a6a7a8a9aab2b3b4b5b6b7b8b9bac2c3c4c5c6c7c8c9cad2d3d4d5d6d7d8d9dae1e2e3e4e5e6e7e8e9eaf1f2f3f4f5f6f7f8f9fa0100030101010101010101010000000000000102030405060708090a0b1100020102040403040705040400010277000102031104052131061241510761711322328108144291a1b1c109233352f0156272d10a162434e125f11718191a262728292a35363738393a434445464748494a535455565758595a636465666768696a737475767778797a82838485868788898a92939495969798999aa2a3a4a5a6a7a8a9aab2b3b4b5b6b7b8b9bac2c3c4c5c6c7c8c9cad2d3d4d5d6d7d8d9dae2e3e4e5e6e7e8e9eaf2f3f4f5f6f7f8f9faffda000c03010002110311003f00e5292800ef450020a2800a2801d49400b450014b40052e2800a69340094a05007fffd0e5d14b10055b51b00c76a00527273494005250014500251400525001450015347c25003a928010d25007ffd1e52909a00290d0014b40052d0014500145002e297b50018a280109a6d002d2e2803fffd2e7a04da3777a94fbd0025140052500145002514005250014940054e381400b494008690d007fffd3e4f345001486800a5a005a2800a2801680280168a002909e280100cd028016a48937bfb5007fffd4c5038a42280128a004a280128a003ad2500251400945002a8cb0a9a80133450026692803ffd5e4e8a004a2801694500145002d18a005c5140052e280109a69a0029680140abb147b139eb401ffd6c62290d00251400949400114940052500252d002525003e31c93525002521a004a4a00ffd7e4a8a00281400a29d40094b40053ba500252d0018a31400d3cd250018cd2d005ab58777ccdd074ab645007ffd0c72290d00348a2801280"), + decoder.decode(null, null, binary(ByteOrder.LITTLE_ENDIAN, "bffbf6d10324504844302c3936302cffd8ffdb008400140e0f120f0d14121012171514181e32211e1c1c1e3d2c2e243249404c4b47404645505a736250556d5645466488656d777b8182814e608d978c7d96737e817c011517171e1a1e3b21213b7c5346537c7c7c7c7c7c7c7c7c7c7c7c7c7c7c7c7c7c7c7c7c7c7c7c7c7c7c7c7c7c7c7c7c7c7c7c7c7c7c7c7c7c7c7c7c7c7c7c7c7cffc000110800f0014003012100021101031101ffdd0004000affc401a20000010501010101010100000000000000000102030405060708090a0b100002010303020403050504040000017d01020300041105122131410613516107227114328191a1082342b1c11552d1f02433627282090a161718191a25262728292a3435363738393a434445464748494a535455565758595a636465666768696a737475767778797a838485868788898a92939495969798999aa2a3a4a5a6a7a8a9aab2b3b4b5b6b7b8b9bac2c3c4c5c6c7c8c9cad2d3d4d5d6d7d8d9dae1e2e3e4e5e6e7e8e9eaf1f2f3f4f5f6f7f8f9fa0100030101010101010101010000000000000102030405060708090a0b1100020102040403040705040400010277000102031104052131061241510761711322328108144291a1b1c109233352f0156272d10a162434e125f11718191a262728292a35363738393a434445464748494a535455565758595a636465666768696a737475767778797a82838485868788898a92939495969798999aa2a3a4a5a6a7a8a9aab2b3b4b5b6b7b8b9bac2c3c4c5c6c7c8c9cad2d3d4d5d6d7d8d9dae2e3e4e5e6e7e8e9eaf2f3f4f5f6f7f8f9faffda000c03010002110311003f00e5292800ef450020a2800a2801d49400b450014b40052e2800a69340094a05007fffd0e5d14b10055b51b00c76a00527273494005250014500251400525001450015347c25003a928010d25007ffd1e52909a00290d0014b40052d0014500145002e297b50018a280109a6d002d2e2803fffd2e7a04da3777a94fbd0025140052500145002514005250014940054e381400b494008690d007fffd3e4f345001486800a5a005a2800a2801680280168a002909e280100cd028016a48937bfb5007fffd4c5038a42280128a004a280128a003ad2500251400945002a8cb0a9a80133450026692803ffd5e4e8a004a2801694500145002d18a005c5140052e280109a69a0029680140abb147b139eb401ffd6c62290d00251400949400114940052500252d002525003e31c93525002521a004a4a00ffd7e4a8a00281400a29d40094b40053ba500252d0018a31400d3cd250018cd2d005ab58777ccdd074ab645007ffd0c72290d00348a28012800d0a"))); + verifyFrame( binary("244655533836353332383032363234333836342c3531302d56312e31322c4131312d56332e30"), decoder.decode(null, null, binary(ByteOrder.LITTLE_ENDIAN, "bffb192d00244655533836353332383032363234333836342c3531302d56312e31322c4131312d56332e300d0d"))); -- cgit v1.2.3 From f91863b34ab32a70faf5bd5948a7fa0bad16ceeb Mon Sep 17 00:00:00 2001 From: Anton Tananaev Date: Thu, 12 Apr 2018 14:04:13 +1200 Subject: Convert PT502 to binary protocol --- src/org/traccar/protocol/Pt502Protocol.java | 3 +- src/org/traccar/protocol/Pt502ProtocolDecoder.java | 50 +++++++++++++++------- .../traccar/protocol/Pt502ProtocolDecoderTest.java | 45 ++++++++++--------- 3 files changed, 60 insertions(+), 38 deletions(-) diff --git a/src/org/traccar/protocol/Pt502Protocol.java b/src/org/traccar/protocol/Pt502Protocol.java index 0116422c2..ba820a6a1 100644 --- a/src/org/traccar/protocol/Pt502Protocol.java +++ b/src/org/traccar/protocol/Pt502Protocol.java @@ -1,5 +1,5 @@ /* - * Copyright 2015 - 2017 Anton Tananaev (anton@traccar.org) + * Copyright 2015 - 2018 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. @@ -45,7 +45,6 @@ public class Pt502Protocol extends BaseProtocol { protected void addSpecificHandlers(ChannelPipeline pipeline) { pipeline.addLast("frameDecoder", new Pt502FrameDecoder()); pipeline.addLast("stringEncoder", new StringEncoder()); - pipeline.addLast("stringDecoder", new StringDecoder()); pipeline.addLast("objectEncoder", new Pt502ProtocolEncoder()); pipeline.addLast("objectDecoder", new Pt502ProtocolDecoder(Pt502Protocol.this)); } diff --git a/src/org/traccar/protocol/Pt502ProtocolDecoder.java b/src/org/traccar/protocol/Pt502ProtocolDecoder.java index bc7647744..b60500c25 100644 --- a/src/org/traccar/protocol/Pt502ProtocolDecoder.java +++ b/src/org/traccar/protocol/Pt502ProtocolDecoder.java @@ -1,5 +1,5 @@ /* - * Copyright 2012 - 2017 Anton Tananaev (anton@traccar.org) + * Copyright 2012 - 2018 Anton Tananaev (anton@traccar.org) * Copyright 2012 Luis Parada (luis.parada@gmail.com) * * Licensed under the Apache License, Version 2.0 (the "License"); @@ -16,6 +16,8 @@ */ package org.traccar.protocol; +import org.jboss.netty.buffer.ChannelBuffer; +import org.jboss.netty.buffer.ChannelBuffers; import org.jboss.netty.channel.Channel; import org.traccar.BaseProtocolDecoder; import org.traccar.DeviceSession; @@ -25,13 +27,14 @@ import org.traccar.helper.PatternBuilder; import org.traccar.model.Position; import java.net.SocketAddress; +import java.nio.charset.StandardCharsets; import java.util.regex.Pattern; public class Pt502ProtocolDecoder extends BaseProtocolDecoder { private static final int MAX_CHUNK_SIZE = 960; - private byte[] photo; + private ChannelBuffer photo; public Pt502ProtocolDecoder(Pt502Protocol protocol) { super(protocol); @@ -85,25 +88,15 @@ public class Pt502ProtocolDecoder extends BaseProtocolDecoder { } } - @Override - protected Object decode( - Channel channel, SocketAddress remoteAddress, Object msg) throws Exception { + private Position decodePosition(Channel channel, SocketAddress remoteAddress, String sentence) { - Parser parser = new Parser(PATTERN, (String) msg); + Parser parser = new Parser(PATTERN, sentence); if (!parser.matches()) { return null; } Position position = new Position(getProtocolName()); - - String type = parser.next(); - - if (type.startsWith("PHO") && channel != null) { - photo = new byte[Integer.parseInt(type.substring(3))]; - channel.write("#PHD0," + Math.min(photo.length, MAX_CHUNK_SIZE) + "\r\n"); - } - - position.set(Position.KEY_ALARM, decodeAlarm(type)); + position.set(Position.KEY_ALARM, decodeAlarm(parser.next())); DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, parser.next()); if (deviceSession == null) { @@ -146,4 +139,31 @@ public class Pt502ProtocolDecoder extends BaseProtocolDecoder { return position; } + @Override + protected Object decode( + Channel channel, SocketAddress remoteAddress, Object msg) throws Exception { + + ChannelBuffer buf = (ChannelBuffer) msg; + + int typeEndIndex = buf.indexOf(buf.readerIndex(), buf.writerIndex(), (byte) ','); + String type = buf.toString(buf.readerIndex(), typeEndIndex - buf.readerIndex(), StandardCharsets.US_ASCII); + + if (type.startsWith("$PHD")) { + + // TODO decode photo + + } else { + + if (type.startsWith("$PHO") && channel != null) { + photo = ChannelBuffers.buffer(Integer.parseInt(type.substring(4, type.indexOf('-')))); + channel.write("#PHD0," + Math.min(photo.capacity(), MAX_CHUNK_SIZE) + "\r\n"); + } + + return decodePosition(channel, remoteAddress, buf.toString(StandardCharsets.US_ASCII)); + + } + + return null; + } + } diff --git a/test/org/traccar/protocol/Pt502ProtocolDecoderTest.java b/test/org/traccar/protocol/Pt502ProtocolDecoderTest.java index 8fd1b4ff7..789d2253b 100644 --- a/test/org/traccar/protocol/Pt502ProtocolDecoderTest.java +++ b/test/org/traccar/protocol/Pt502ProtocolDecoderTest.java @@ -11,68 +11,71 @@ public class Pt502ProtocolDecoderTest extends ProtocolTest { Pt502ProtocolDecoder decoder = new Pt502ProtocolDecoder(new Pt502Protocol()); - verifyPosition(decoder, text( + verifyPosition(decoder, buffer( + "$PHO3821-1,1156802639,022125.000,A,0707.0014,N,07307.3725,W,0.0,0.1,110418,,,A/00000,00000/0,0,0,0/500//fd4//")); + + verifyPosition(decoder, buffer( "$POS,1360000277,182241.000,A,0846.0896,N,07552.1738,W,13.58,26.88,291017,,,A/00000,00000/142,0,0,0/62792900//f65//#")); - verifyPosition(decoder, text( + verifyPosition(decoder, buffer( "$PHO0-1,1360000260,123012.000,A,0913.9644,N,07548.8345,W,0.0,309.8,111017,,,A/00000,10000/0,0,0,0/64551600//f98//")); - verifyPosition(decoder, text( + verifyPosition(decoder, buffer( "$POS,865328026243864,151105.000,A,1332.7096,N,204.6787,E,0.0,10.00,050517,,,A/00000,10/1,0/234//FD9/")); - verifyNull(decoder, text( + verifyNull(decoder, buffer( "$FUS865328026243864,510-V1.12,A11-V3.0")); - verifyPosition(decoder, text( + verifyPosition(decoder, buffer( "$HDA,20007,134657.000,A,0626.1607,N,00330.2245,E,33.38,81.79,041016,,,A/00010,00000/270,0,0,0/19948900//fa4//")); - verifyPosition(decoder, text( + verifyPosition(decoder, buffer( "$HDB,20007,134708.000,A,0626.1759,N,00330.3192,E,26.55,80.37,041016,,,A/00010,00000/23b,0,0,0/19949100//fa4//")); - verifyPosition(decoder, text( + verifyPosition(decoder, buffer( "$POS,20007,134704.000,A,0626.1698,N,00330.2870,E,31.23,79.58,041016,,,A/00010,00000/26c,0,0,0/19949100//fa4//#")); - verifyPosition(decoder, text( + verifyPosition(decoder, buffer( "$PHO6608,115099,133140.000,A,1307.1238,N,05936.4194,W,0.00,21.50,290816,,,A/00010,00000/0,0,0,0/185100//f59/")); - verifyPosition(decoder, text( + verifyPosition(decoder, buffer( "$DFR,40456789,083125.000,A,2232.0971,N,11400.9504,E,0.0,5.00,090714,,,A/00000,00/0,0/200076//FE7/")); - verifyPosition(decoder, text( + verifyPosition(decoder, buffer( "$FDA,40456789,083125.000,A,2232.0971,N,11400.9504,E,0.0,5.00,090714,,,A/00000,00/0,0/200076//FE7/")); - verifyAttribute(decoder, text( + verifyAttribute(decoder, buffer( "$CPA,40456789,083125.000,A,2232.0971,N,11400.9504,E,7.62,265.24,291117,,,A/00000,00000/0/1200//#"), Position.KEY_ALARM, Position.ALARM_POWER_CUT); - verifyPosition(decoder, text( + verifyPosition(decoder, buffer( "$POS,216769295715,163237.000,A,3258.1738,S,02755.4350,E,0.00,215.88,100915,,,A/0000,0//232300//5b3/"), position("2015-09-10 16:32:37.000", true, -32.96956, 27.92392)); - verifyPosition(decoder, text( + verifyPosition(decoder, buffer( "$POS,11023456,033731.000,A,0335.2617,N,09841.1587,E,0.00,88.12,210615,,,A/0000,0/1f8/388900//f33//")); - verifyPosition(decoder, text( + verifyPosition(decoder, buffer( "$POS,6094,205523.000,A,1013.6223,N,06728.4248,W,0.0,99.3,011112,,,A/00000,00000/0/23895000//")); - verifyPosition(decoder, text( + verifyPosition(decoder, buffer( "$POS,6120,233326.000,V,0935.1201,N,06914.6933,W,0.00,,151112,,,A/00000,00000/0/0/")); - verifyPosition(decoder, text( + verifyPosition(decoder, buffer( "$POS,6002,233257.000,A,0931.0430,N,06912.8707,W,0.05,146.98,141112,,,A/00010,00000/0/5360872")); - verifyPosition(decoder, text( + verifyPosition(decoder, buffer( "$POS,6095,233344.000,V,0933.0451,N,06912.3360,W,,,151112,,,N/00000,00000/0/1677600/")); - verifyPosition(decoder, text( + verifyPosition(decoder, buffer( "$PHO0,6091,233606.000,A,0902.9855,N,06944.3654,W,0.0,43.8,141112,,,A/00010,00000/0/224000//")); - verifyPosition(decoder, text( + verifyPosition(decoder, buffer( "$POS,353451000164,082405.000,A,1254.8501,N,10051.6752,E,0.00,237.99,160513,,,A/0000,0/0/55000//a71/")); - verifyPosition(decoder, text( + verifyPosition(decoder, buffer( "$POS,012896008586486,154215.000,A,0118.0143,S,03646.9144,E,0.00,83.29,180714,,,A/0000,0/0/29200//644/")); - verifyPosition(decoder, text( + verifyPosition(decoder, buffer( "$POS,1151000,205326.000,A,0901.3037,N,07928.2751,W,48.79,30.55,170814,,,A/00010,10000/0,0,0,0/15986500//fb8/")); } -- cgit v1.2.3 From 617393cf9f052298f7fb35f0c58138c87a6dd5c3 Mon Sep 17 00:00:00 2001 From: Anton Tananaev Date: Thu, 12 Apr 2018 14:20:26 +1200 Subject: Complete PT502 photo decoding --- src/org/traccar/protocol/Pt502ProtocolDecoder.java | 49 ++++++++++++++++++++-- .../traccar/protocol/Pt502ProtocolDecoderTest.java | 3 ++ 2 files changed, 48 insertions(+), 4 deletions(-) diff --git a/src/org/traccar/protocol/Pt502ProtocolDecoder.java b/src/org/traccar/protocol/Pt502ProtocolDecoder.java index b60500c25..76e7ee1bf 100644 --- a/src/org/traccar/protocol/Pt502ProtocolDecoder.java +++ b/src/org/traccar/protocol/Pt502ProtocolDecoder.java @@ -20,6 +20,7 @@ import org.jboss.netty.buffer.ChannelBuffer; import org.jboss.netty.buffer.ChannelBuffers; import org.jboss.netty.channel.Channel; import org.traccar.BaseProtocolDecoder; +import org.traccar.Context; import org.traccar.DeviceSession; import org.traccar.helper.DateBuilder; import org.traccar.helper.Parser; @@ -139,6 +140,14 @@ public class Pt502ProtocolDecoder extends BaseProtocolDecoder { return position; } + private void requestPhotoFragment(Channel channel) { + if (channel != null) { + int offset = photo.writerIndex(); + int size = Math.min(photo.writableBytes(), MAX_CHUNK_SIZE); + channel.write("#PHD" + offset + "," + size + "\r\n"); + } + } + @Override protected Object decode( Channel channel, SocketAddress remoteAddress, Object msg) throws Exception { @@ -150,13 +159,45 @@ public class Pt502ProtocolDecoder extends BaseProtocolDecoder { if (type.startsWith("$PHD")) { - // TODO decode photo + int dataIndex = buf.indexOf(typeEndIndex + 1, buf.writerIndex(), (byte) ',') + 1; + buf.readerIndex(dataIndex); + + if (photo != null) { + + photo.writeBytes(buf.readBytes(buf.readableBytes())); + + if (photo.writableBytes() > 0) { + + requestPhotoFragment(channel); + + } else { + + DeviceSession deviceSession = getDeviceSession(channel, remoteAddress); + String uniqueId = Context.getIdentityManager().getById(deviceSession.getDeviceId()).getUniqueId(); + + Position position = new Position(getProtocolName()); + position.setDeviceId(deviceSession.getDeviceId()); + + getLastLocation(position, null); + + position.set(Position.KEY_IMAGE, Context.getMediaManager().writeFile(uniqueId, photo, "jpg")); + + photo = null; + + return position; + + } + + } } else { - if (type.startsWith("$PHO") && channel != null) { - photo = ChannelBuffers.buffer(Integer.parseInt(type.substring(4, type.indexOf('-')))); - channel.write("#PHD0," + Math.min(photo.capacity(), MAX_CHUNK_SIZE) + "\r\n"); + if (type.startsWith("$PHO")) { + int size = Integer.parseInt(type.split("-")[0].substring(4)); + if (size > 0) { + photo = ChannelBuffers.buffer(size); + requestPhotoFragment(channel); + } } return decodePosition(channel, remoteAddress, buf.toString(StandardCharsets.US_ASCII)); diff --git a/test/org/traccar/protocol/Pt502ProtocolDecoderTest.java b/test/org/traccar/protocol/Pt502ProtocolDecoderTest.java index 789d2253b..cca264faa 100644 --- a/test/org/traccar/protocol/Pt502ProtocolDecoderTest.java +++ b/test/org/traccar/protocol/Pt502ProtocolDecoderTest.java @@ -11,6 +11,9 @@ public class Pt502ProtocolDecoderTest extends ProtocolTest { Pt502ProtocolDecoder decoder = new Pt502ProtocolDecoder(new Pt502Protocol()); + verifyNull(decoder, binary( + "24504844302c3936302cffd8ffdb008400140e0f120f0d14121012171514181e32211e1c1c1e3d2c2e243249404c4b47404645505a736250556d5645466488656d777b8182814e608d978c7d96737e817c011517171e1a1e3b21213b7c5346537c7c7c7c7c7c7c7c7c7c7c7c7c7c7c7c7c7c7c7c7c7c7c7c7c7c7c7c7c7c7c7c7c7c7c7c7c7c7c7c7c7c7c7c7c7c7c7c7c7cffc000110800f0014003012100021101031101ffdd0004000affc401a20000010501010101010100000000000000000102030405060708090a0b100002010303020403050504040000017d01020300041105122131410613516107227114328191a1082342b1c11552d1f02433627282090a161718191a25262728292a3435363738393a434445464748494a535455565758595a636465666768696a737475767778797a838485868788898a92939495969798999aa2a3a4a5a6a7a8a9aab2b3b4b5b6b7b8b9bac2c3c4c5c6c7c8c9cad2d3d4d5d6d7d8d9dae1e2e3e4e5e6e7e8e9eaf1f2f3f4f5f6f7f8f9fa0100030101010101010101010000000000000102030405060708090a0b1100020102040403040705040400010277000102031104052131061241510761711322328108144291a1b1c109233352f0156272d10a162434e125f11718191a262728292a35363738393a434445464748494a535455565758595a636465666768696a737475767778797a82838485868788898a92939495969798999aa2a3a4a5a6a7a8a9aab2b3b4b5b6b7b8b9bac2c3c4c5c6c7c8c9cad2d3d4d5d6d7d8d9dae2e3e4e5e6e7e8e9eaf2f3f4f5f6f7f8f9faffda000c03010002110311003f00e5292800ef450020a2800a2801d49400b450014b40052e2800a69340094a05007fffd0e5d14b10055b51b00c76a00527273494005250014500251400525001450015347c25003a928010d25007ffd1e52909a00290d0014b40052d0014500145002e297b50018a280109a6d002d2e2803fffd2e7a04da3777a94fbd0025140052500145002514005250014940054e381400b494008690d007fffd3e4f345001486800a5a005a2800a2801680280168a002909e280100cd028016a48937bfb5007fffd4c5038a42280128a004a280128a003ad2500251400945002a8cb0a9a80133450026692803ffd5e4e8a004a2801694500145002d18a005c5140052e280109a69a0029680140abb147b139eb401ffd6c62290d00251400949400114940052500252d002525003e31c93525002521a004a4a00ffd7e4a8a00281400a29d40094b40053ba500252d0018a31400d3cd250018cd2d005ab58777ccdd074ab645007ffd0c72290d00348a2801280")); + verifyPosition(decoder, buffer( "$PHO3821-1,1156802639,022125.000,A,0707.0014,N,07307.3725,W,0.0,0.1,110418,,,A/00000,00000/0,0,0,0/500//fd4//")); -- cgit v1.2.3