From 8ac74d53d687641b7af79e6ca3a84d8851b46236 Mon Sep 17 00:00:00 2001 From: Anton Tananaev Date: Sun, 15 Nov 2015 14:30:37 +1300 Subject: Fix LBS info for some decoders --- src/org/traccar/protocol/FlextrackProtocolDecoder.java | 4 ++-- src/org/traccar/protocol/Gl200ProtocolDecoder.java | 12 +++++++----- 2 files changed, 9 insertions(+), 7 deletions(-) (limited to 'src') diff --git a/src/org/traccar/protocol/FlextrackProtocolDecoder.java b/src/org/traccar/protocol/FlextrackProtocolDecoder.java index 363d5502c..bc9825b49 100644 --- a/src/org/traccar/protocol/FlextrackProtocolDecoder.java +++ b/src/org/traccar/protocol/FlextrackProtocolDecoder.java @@ -125,8 +125,8 @@ public class FlextrackProtocolDecoder extends BaseProtocolDecoder { position.setAltitude(parser.nextInt()); position.set(Event.KEY_HDOP, parser.nextInt() * 0.1); - position.set(Event.KEY_CID, parser.next()); - position.set(Event.KEY_LAC, parser.next()); + position.set(Event.KEY_CID, parser.nextInt(16)); + position.set(Event.KEY_LAC, parser.nextInt(16)); position.set(Event.KEY_ODOMETER, parser.nextInt()); return position; diff --git a/src/org/traccar/protocol/Gl200ProtocolDecoder.java b/src/org/traccar/protocol/Gl200ProtocolDecoder.java index 29884bb00..06c548669 100644 --- a/src/org/traccar/protocol/Gl200ProtocolDecoder.java +++ b/src/org/traccar/protocol/Gl200ProtocolDecoder.java @@ -116,7 +116,7 @@ public class Gl200ProtocolDecoder extends BaseProtocolDecoder { .text(",") .number("(0ddd)?,") // mcc .number("(0ddd)?,") // mnc - .number("(xxxx|x{8})?,") // loc + .number("(xxxx|x{8})?,") // lac .number("(xxxx)?,") // cell .groupBegin() .number("(d+.d)?,") // odometer @@ -215,10 +215,12 @@ public class Gl200ProtocolDecoder extends BaseProtocolDecoder { getLastLocation(position, null); } - position.set(Event.KEY_MCC, parser.next()); - position.set(Event.KEY_MNC, parser.next()); - position.set(Event.KEY_LAC, parser.next()); - position.set(Event.KEY_CID, parser.next()); + if (parser.hasNext(4)) { + position.set(Event.KEY_MCC, parser.nextInt()); + position.set(Event.KEY_MNC, parser.nextInt()); + position.set(Event.KEY_LAC, parser.nextInt(16)); + position.set(Event.KEY_CID, parser.nextInt(16)); + } position.set(Event.KEY_ODOMETER, parser.next()); position.set(Event.KEY_BATTERY, parser.next()); -- cgit v1.2.3 From de46b77e923d3412d74dbf745933bd62acaeca77 Mon Sep 17 00:00:00 2001 From: Anton Tananaev Date: Sun, 15 Nov 2015 17:22:29 +1300 Subject: Store correct MCC and MNC values --- src/org/traccar/protocol/MegastekProtocolDecoder.java | 12 +++++++----- src/org/traccar/protocol/MeitrackProtocolDecoder.java | 6 +++--- src/org/traccar/protocol/T800xProtocolDecoder.java | 12 ++++++++---- test/org/traccar/ProtocolDecoderTest.java | 16 ++++++++++++++++ 4 files changed, 34 insertions(+), 12 deletions(-) (limited to 'src') diff --git a/src/org/traccar/protocol/MegastekProtocolDecoder.java b/src/org/traccar/protocol/MegastekProtocolDecoder.java index a58e57703..b2db50fd4 100644 --- a/src/org/traccar/protocol/MegastekProtocolDecoder.java +++ b/src/org/traccar/protocol/MegastekProtocolDecoder.java @@ -171,9 +171,11 @@ public class MegastekProtocolDecoder extends BaseProtocolDecoder { position.set(Event.KEY_CHARGE, Integer.parseInt(charger) == 1); } - position.set(Event.KEY_MCC, parser.next()); - position.set(Event.KEY_MNC, parser.next()); - position.set(Event.KEY_LAC, parser.next()); + if (parser.hasNext(3)) { + position.set(Event.KEY_MCC, parser.nextInt()); + position.set(Event.KEY_MNC, parser.nextInt()); + position.set(Event.KEY_LAC, parser.next()); + } } else { @@ -194,8 +196,8 @@ public class MegastekProtocolDecoder extends BaseProtocolDecoder { } position.setDeviceId(getDeviceId()); - position.set(Event.KEY_MCC, parser.next()); - position.set(Event.KEY_MNC, parser.next()); + position.set(Event.KEY_MCC, parser.nextInt()); + position.set(Event.KEY_MNC, parser.nextInt()); position.set(Event.KEY_LAC, parser.next()); position.set(Event.KEY_GSM, parser.next()); diff --git a/src/org/traccar/protocol/MeitrackProtocolDecoder.java b/src/org/traccar/protocol/MeitrackProtocolDecoder.java index 2b8460fc7..475329c5d 100644 --- a/src/org/traccar/protocol/MeitrackProtocolDecoder.java +++ b/src/org/traccar/protocol/MeitrackProtocolDecoder.java @@ -119,8 +119,8 @@ public class MeitrackProtocolDecoder extends BaseProtocolDecoder { position.set(Event.KEY_ODOMETER, parser.next()); position.set("runtime", parser.next()); - position.set(Event.KEY_MCC, parser.next()); - position.set(Event.KEY_MCC, parser.next()); + position.set(Event.KEY_MCC, parser.nextInt()); + position.set(Event.KEY_MNC, parser.nextInt()); position.set(Event.KEY_LAC, parser.next()); position.set(Event.KEY_CID, parser.next()); position.set(Event.KEY_STATUS, parser.next()); @@ -196,7 +196,7 @@ public class MeitrackProtocolDecoder extends BaseProtocolDecoder { position.set(Event.KEY_ODOMETER, buf.readUnsignedInt()); position.set("runtime", buf.readUnsignedInt()); position.set(Event.KEY_MCC, buf.readUnsignedShort()); - position.set(Event.KEY_MCC, buf.readUnsignedShort()); + position.set(Event.KEY_MNC, buf.readUnsignedShort()); position.set(Event.KEY_LAC, buf.readUnsignedShort()); position.set(Event.KEY_CID, buf.readUnsignedShort()); position.set(Event.KEY_STATUS, buf.readUnsignedShort()); diff --git a/src/org/traccar/protocol/T800xProtocolDecoder.java b/src/org/traccar/protocol/T800xProtocolDecoder.java index 2127be331..83d815e0f 100644 --- a/src/org/traccar/protocol/T800xProtocolDecoder.java +++ b/src/org/traccar/protocol/T800xProtocolDecoder.java @@ -143,10 +143,14 @@ public class T800xProtocolDecoder extends BaseProtocolDecoder { getLastLocation(position, dateBuilder.getDate()); - position.set(Event.KEY_MCC, buf.readUnsignedShort()); - position.set(Event.KEY_MNC, buf.readUnsignedShort()); - position.set(Event.KEY_LAC, buf.readUnsignedShort()); - position.set(Event.KEY_CID, buf.readUnsignedShort()); + byte[] array = new byte[16]; + buf.readBytes(array); + ChannelBuffer swapped = ChannelBuffers.wrappedBuffer(ByteOrder.LITTLE_ENDIAN, array); + + position.set(Event.KEY_MCC, swapped.readUnsignedShort()); + position.set(Event.KEY_MNC, swapped.readUnsignedShort()); + position.set(Event.KEY_LAC, swapped.readUnsignedShort()); + position.set(Event.KEY_CID, swapped.readUnsignedShort()); // two more cell towers diff --git a/test/org/traccar/ProtocolDecoderTest.java b/test/org/traccar/ProtocolDecoderTest.java index 5292bd32d..7aea23f1b 100644 --- a/test/org/traccar/ProtocolDecoderTest.java +++ b/test/org/traccar/ProtocolDecoderTest.java @@ -187,6 +187,22 @@ public class ProtocolDecoderTest { Assert.assertFalse("no attributes", attributes.isEmpty()); } + if (attributes.containsKey(Event.KEY_MCC)) { + checkInteger(attributes.get(Event.KEY_MCC), 100, 999); + } + + if (attributes.containsKey(Event.KEY_MNC)) { + checkInteger(attributes.get(Event.KEY_MNC), 0, 999); + } + + /*if (attributes.containsKey(Event.KEY_LAC)) { + checkInteger(attributes.get(Event.KEY_LAC), 1, 65535); + } + + if (attributes.containsKey(Event.KEY_CID)) { + checkInteger(attributes.get(Event.KEY_CID), 1, 268435455); + }*/ + /*if (attributes.containsKey(Event.KEY_MCC) || attributes.containsKey(Event.KEY_MNC)) { checkInteger(attributes.get(Event.KEY_MCC), 100, 999); checkInteger(attributes.get(Event.KEY_MNC), 0, 999); -- cgit v1.2.3 From 3ba58a7d025c33c5f4e5e2d595158ecf9f420008 Mon Sep 17 00:00:00 2001 From: Anton Tananaev Date: Sun, 15 Nov 2015 18:07:03 +1300 Subject: Store LAC and CID in right format --- src/org/traccar/protocol/EelinkProtocolDecoder.java | 5 ++++- src/org/traccar/protocol/Gl200ProtocolDecoder.java | 4 +++- src/org/traccar/protocol/Jt600ProtocolDecoder.java | 9 +++++++-- .../traccar/protocol/MegastekProtocolDecoder.java | 18 ++++++++++++------ .../traccar/protocol/MeitrackProtocolDecoder.java | 4 ++-- .../traccar/protocol/SuntechProtocolDecoder.java | 2 +- src/org/traccar/protocol/Tlt2hProtocolDecoder.java | 2 +- src/org/traccar/protocol/TotemProtocolDecoder.java | 21 ++++++++++++++++----- test/org/traccar/ProtocolDecoderTest.java | 4 ++-- 9 files changed, 48 insertions(+), 21 deletions(-) (limited to 'src') diff --git a/src/org/traccar/protocol/EelinkProtocolDecoder.java b/src/org/traccar/protocol/EelinkProtocolDecoder.java index b3a062db3..b75587743 100644 --- a/src/org/traccar/protocol/EelinkProtocolDecoder.java +++ b/src/org/traccar/protocol/EelinkProtocolDecoder.java @@ -86,7 +86,10 @@ public class EelinkProtocolDecoder extends BaseProtocolDecoder { position.setSpeed(UnitsConverter.knotsFromKph(buf.readUnsignedByte())); position.setCourse(buf.readUnsignedShort()); - position.set(Event.KEY_CID, ChannelBuffers.hexDump(buf.readBytes(9))); + position.set(Event.KEY_MCC, buf.readUnsignedShort()); + position.set(Event.KEY_MNC, buf.readUnsignedShort()); + position.set(Event.KEY_LAC, buf.readUnsignedShort()); + position.set(Event.KEY_CID, buf.readUnsignedShort()); position.setValid((buf.readUnsignedByte() & 0x01) != 0); diff --git a/src/org/traccar/protocol/Gl200ProtocolDecoder.java b/src/org/traccar/protocol/Gl200ProtocolDecoder.java index 06c548669..42786d4fe 100644 --- a/src/org/traccar/protocol/Gl200ProtocolDecoder.java +++ b/src/org/traccar/protocol/Gl200ProtocolDecoder.java @@ -116,7 +116,9 @@ public class Gl200ProtocolDecoder extends BaseProtocolDecoder { .text(",") .number("(0ddd)?,") // mcc .number("(0ddd)?,") // mnc - .number("(xxxx|x{8})?,") // lac + .number("(?:xxxx)?") + .number("(xxxx)").optional(2) // lac + .text(",") .number("(xxxx)?,") // cell .groupBegin() .number("(d+.d)?,") // odometer diff --git a/src/org/traccar/protocol/Jt600ProtocolDecoder.java b/src/org/traccar/protocol/Jt600ProtocolDecoder.java index d9e31fe48..b576b063d 100644 --- a/src/org/traccar/protocol/Jt600ProtocolDecoder.java +++ b/src/org/traccar/protocol/Jt600ProtocolDecoder.java @@ -94,8 +94,13 @@ public class Jt600ProtocolDecoder extends BaseProtocolDecoder { position.setAltitude(buf.readUnsignedShort()); - position.set(Event.KEY_CID, buf.readUnsignedShort()); - position.set(Event.KEY_LAC, buf.readUnsignedShort()); + int cid = buf.readUnsignedShort(); + int lac = buf.readUnsignedShort(); + if (cid != 0 && lac != 0) { + position.set(Event.KEY_CID, cid); + position.set(Event.KEY_LAC, lac); + } + position.set(Event.KEY_GSM, buf.readUnsignedByte()); } else if (version == 2) { diff --git a/src/org/traccar/protocol/MegastekProtocolDecoder.java b/src/org/traccar/protocol/MegastekProtocolDecoder.java index b2db50fd4..dc9e6056e 100644 --- a/src/org/traccar/protocol/MegastekProtocolDecoder.java +++ b/src/org/traccar/protocol/MegastekProtocolDecoder.java @@ -53,14 +53,16 @@ public class MegastekProtocolDecoder extends BaseProtocolDecoder { .number("(d)?,") // charger .number("(d+)?,") // mcc .number("(d+)?,") // mnc - .number("(xxxx,xxxx);") // location code + .number("(xxxx),") // lac + .number("(xxxx);") // cid .any() // checksum .compile(); private static final Pattern PATTERN_ALTERNATIVE = new PatternBuilder() .number("(d+),") // mcc .number("(d+),") // mnc - .number("(xxxx,xxxx),") // location code + .number("(xxxx),") // lac + .number("(xxxx),") // cid .number("(d+),") // gsm signal .number("(d+),") // battery .number("(d+),") // flags @@ -174,7 +176,8 @@ public class MegastekProtocolDecoder extends BaseProtocolDecoder { if (parser.hasNext(3)) { position.set(Event.KEY_MCC, parser.nextInt()); position.set(Event.KEY_MNC, parser.nextInt()); - position.set(Event.KEY_LAC, parser.next()); + position.set(Event.KEY_LAC, parser.nextInt(16)); + position.set(Event.KEY_CID, parser.nextInt(16)); } } else { @@ -198,7 +201,8 @@ public class MegastekProtocolDecoder extends BaseProtocolDecoder { position.set(Event.KEY_MCC, parser.nextInt()); position.set(Event.KEY_MNC, parser.nextInt()); - position.set(Event.KEY_LAC, parser.next()); + position.set(Event.KEY_LAC, parser.nextInt(16)); + position.set(Event.KEY_CID, parser.nextInt(16)); position.set(Event.KEY_GSM, parser.next()); position.set(Event.KEY_BATTERY, Double.parseDouble(parser.next())); @@ -238,7 +242,8 @@ public class MegastekProtocolDecoder extends BaseProtocolDecoder { .number("(d+.d+),") // odometer .number("(d+),") // mcc .number("(d+),") // mnc - .number("(xxxx,xxxx),") // cell + .number("(xxxx),") // lac + .number("(xxxx),") // cid .number("(d+)?,") // gsm .expression("([01]+),") // input .expression("([01]+),") // output @@ -297,7 +302,8 @@ public class MegastekProtocolDecoder extends BaseProtocolDecoder { position.set(Event.KEY_ODOMETER, parser.nextDouble()); position.set(Event.KEY_MCC, parser.nextInt()); position.set(Event.KEY_MNC, parser.nextInt()); - position.set(Event.KEY_CID, parser.next()); + position.set(Event.KEY_LAC, parser.nextInt(16)); + position.set(Event.KEY_CID, parser.nextInt(16)); String gsm = parser.next(); if (gsm != null) { diff --git a/src/org/traccar/protocol/MeitrackProtocolDecoder.java b/src/org/traccar/protocol/MeitrackProtocolDecoder.java index 475329c5d..4bde5cf75 100644 --- a/src/org/traccar/protocol/MeitrackProtocolDecoder.java +++ b/src/org/traccar/protocol/MeitrackProtocolDecoder.java @@ -121,8 +121,8 @@ public class MeitrackProtocolDecoder extends BaseProtocolDecoder { position.set("runtime", parser.next()); position.set(Event.KEY_MCC, parser.nextInt()); position.set(Event.KEY_MNC, parser.nextInt()); - position.set(Event.KEY_LAC, parser.next()); - position.set(Event.KEY_CID, parser.next()); + position.set(Event.KEY_LAC, parser.nextInt(16)); + position.set(Event.KEY_CID, parser.nextInt(16)); position.set(Event.KEY_STATUS, parser.next()); for (int i = 1; i <= 3; i++) { diff --git a/src/org/traccar/protocol/SuntechProtocolDecoder.java b/src/org/traccar/protocol/SuntechProtocolDecoder.java index b0d85b101..38f771eec 100644 --- a/src/org/traccar/protocol/SuntechProtocolDecoder.java +++ b/src/org/traccar/protocol/SuntechProtocolDecoder.java @@ -83,7 +83,7 @@ public class SuntechProtocolDecoder extends BaseProtocolDecoder { .setTime(parser.nextInt(), parser.nextInt(), parser.nextInt()); position.setTime(dateBuilder.getDate()); - position.set(Event.KEY_CID, parser.next()); + parser.next(); // location code + bsic position.setValid(true); position.setLatitude(parser.nextDouble()); diff --git a/src/org/traccar/protocol/Tlt2hProtocolDecoder.java b/src/org/traccar/protocol/Tlt2hProtocolDecoder.java index d9d50947e..24e9893bb 100644 --- a/src/org/traccar/protocol/Tlt2hProtocolDecoder.java +++ b/src/org/traccar/protocol/Tlt2hProtocolDecoder.java @@ -86,7 +86,7 @@ public class Tlt2hProtocolDecoder extends BaseProtocolDecoder { position.setProtocol(getProtocolName()); position.setDeviceId(getDeviceId()); - position.set(Event.KEY_CID, parser.next()); + parser.next(); // base station info DateBuilder dateBuilder = new DateBuilder() .setTime(parser.nextInt(), parser.nextInt(), parser.nextInt()); diff --git a/src/org/traccar/protocol/TotemProtocolDecoder.java b/src/org/traccar/protocol/TotemProtocolDecoder.java index 321b78b7c..88eecab70 100644 --- a/src/org/traccar/protocol/TotemProtocolDecoder.java +++ b/src/org/traccar/protocol/TotemProtocolDecoder.java @@ -55,7 +55,8 @@ public class TotemProtocolDecoder extends BaseProtocolDecoder { .number("(ddd)") // battery .number("(dddd)|") // power .number("(d+)|").optional() // adc - .number("(x+)|") // location code + .number("x*(xxxx)") // lac + .number("(xxxx)|") // cid .number("(d+)|") // temperature .number("(d+.d+)|") // odometer .number("d+|") // serial number @@ -84,7 +85,8 @@ public class TotemProtocolDecoder extends BaseProtocolDecoder { .number("(dd)") // battery .number("(dd)|") // external power .number("(d+)|") // adc - .number("(x{8})|") // location code + .number("(xxxx)") // lac + .number("(xxxx)|") // cid .number("(d+)|") // temperature .number("(d+.d+)|") // odometer .number("d+|") // serial number @@ -107,7 +109,8 @@ public class TotemProtocolDecoder extends BaseProtocolDecoder { .number("(dddd)") // adc 2 .number("(ddd)") // temperature 1 .number("(ddd)") // temperature 2 - .number("(x{8})") // location code + .number("(xxxx)") // lac + .number("(xxxx)") // cid .expression("([AV])") // validity .number("(dd)") // satellites .number("(ddd)") // course @@ -185,7 +188,14 @@ public class TotemProtocolDecoder extends BaseProtocolDecoder { position.set(Event.KEY_BATTERY, parser.next()); position.set(Event.KEY_POWER, parser.nextDouble()); position.set(Event.PREFIX_ADC + 1, parser.next()); - position.set(Event.KEY_LAC, parser.next()); + + int lac = parser.nextInt(16); + int cid = parser.nextInt(16); + if (lac != 0 && cid != 0) { + position.set(Event.KEY_LAC, lac); + position.set(Event.KEY_CID, cid); + } + position.set(Event.PREFIX_TEMP + 1, parser.next()); position.set(Event.KEY_ODOMETER, parser.next()); @@ -203,7 +213,8 @@ public class TotemProtocolDecoder extends BaseProtocolDecoder { position.set(Event.PREFIX_ADC + 2, parser.next()); position.set(Event.PREFIX_TEMP + 1, parser.next()); position.set(Event.PREFIX_TEMP + 2, parser.next()); - position.set(Event.KEY_LAC, parser.next()); + position.set(Event.KEY_LAC, parser.nextInt(16)); + position.set(Event.KEY_CID, parser.nextInt(16)); position.setValid(parser.next().equals("A")); position.set(Event.KEY_SATELLITES, parser.next()); diff --git a/test/org/traccar/ProtocolDecoderTest.java b/test/org/traccar/ProtocolDecoderTest.java index 7aea23f1b..dbb11965b 100644 --- a/test/org/traccar/ProtocolDecoderTest.java +++ b/test/org/traccar/ProtocolDecoderTest.java @@ -195,13 +195,13 @@ public class ProtocolDecoderTest { checkInteger(attributes.get(Event.KEY_MNC), 0, 999); } - /*if (attributes.containsKey(Event.KEY_LAC)) { + if (attributes.containsKey(Event.KEY_LAC)) { checkInteger(attributes.get(Event.KEY_LAC), 1, 65535); } if (attributes.containsKey(Event.KEY_CID)) { checkInteger(attributes.get(Event.KEY_CID), 1, 268435455); - }*/ + } /*if (attributes.containsKey(Event.KEY_MCC) || attributes.containsKey(Event.KEY_MNC)) { checkInteger(attributes.get(Event.KEY_MCC), 100, 999); -- cgit v1.2.3 From 98aeb9261b37dc174c37236292cb2d726937c3d5 Mon Sep 17 00:00:00 2001 From: Anton Tananaev Date: Sun, 15 Nov 2015 18:13:16 +1300 Subject: Make LBS tests even stricter --- src/org/traccar/protocol/TeltonikaProtocolDecoder.java | 2 +- test/org/traccar/ProtocolDecoderTest.java | 9 +++++++-- 2 files changed, 8 insertions(+), 3 deletions(-) (limited to 'src') diff --git a/src/org/traccar/protocol/TeltonikaProtocolDecoder.java b/src/org/traccar/protocol/TeltonikaProtocolDecoder.java index 3592cae79..2217b5ce4 100644 --- a/src/org/traccar/protocol/TeltonikaProtocolDecoder.java +++ b/src/org/traccar/protocol/TeltonikaProtocolDecoder.java @@ -114,7 +114,7 @@ public class TeltonikaProtocolDecoder extends BaseProtocolDecoder { } if (BitUtil.check(locationMask, 5)) { - position.set("area", buf.readUnsignedShort()); + position.set(Event.KEY_LAC, buf.readUnsignedShort()); position.set(Event.KEY_CID, buf.readUnsignedShort()); } diff --git a/test/org/traccar/ProtocolDecoderTest.java b/test/org/traccar/ProtocolDecoderTest.java index ef98b3467..8f7ed628b 100644 --- a/test/org/traccar/ProtocolDecoderTest.java +++ b/test/org/traccar/ProtocolDecoderTest.java @@ -187,11 +187,16 @@ public class ProtocolDecoderTest { Assert.assertFalse("no attributes", attributes.isEmpty()); } + if (attributes.containsKey(Event.KEY_LAC) || attributes.containsKey(Event.KEY_CID)) { + checkInteger(attributes.get(Event.KEY_LAC), 1, 65535); + checkInteger(attributes.get(Event.KEY_CID), 1, 268435455); + } + if (attributes.containsKey(Event.KEY_MCC) || attributes.containsKey(Event.KEY_MNC)) { checkInteger(attributes.get(Event.KEY_MCC), 100, 999); checkInteger(attributes.get(Event.KEY_MNC), 0, 999); - checkInteger(attributes.get(Event.KEY_LAC), 1, 65535); - checkInteger(attributes.get(Event.KEY_CID), 1, 268435455); + Assert.assertTrue("value missing", attributes.containsKey(Event.KEY_LAC)); + Assert.assertTrue("value missing", attributes.containsKey(Event.KEY_CID)); } } -- cgit v1.2.3 From 3563910f1b551c02db5dffc9745bd8aba9cbeeeb Mon Sep 17 00:00:00 2001 From: Anton Tananaev Date: Mon, 16 Nov 2015 11:33:06 +1300 Subject: Disable async debugging and add test --- src/org/traccar/web/AsyncServlet.java | 2 +- test/org/traccar/web/AsyncServletTest.java | 13 +++++++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) create mode 100644 test/org/traccar/web/AsyncServletTest.java (limited to 'src') diff --git a/src/org/traccar/web/AsyncServlet.java b/src/org/traccar/web/AsyncServlet.java index 63b08ff3e..3347f765c 100644 --- a/src/org/traccar/web/AsyncServlet.java +++ b/src/org/traccar/web/AsyncServlet.java @@ -50,7 +50,7 @@ public class AsyncServlet extends BaseServlet { public static class AsyncSession { - private static final boolean DEBUG_ASYNC = true; + public static final boolean DEBUG_ASYNC = false; private static final long SESSION_TIMEOUT = 30; private static final long REQUEST_TIMEOUT = 20; diff --git a/test/org/traccar/web/AsyncServletTest.java b/test/org/traccar/web/AsyncServletTest.java new file mode 100644 index 000000000..e2f7b2f21 --- /dev/null +++ b/test/org/traccar/web/AsyncServletTest.java @@ -0,0 +1,13 @@ +package org.traccar.web; + +import org.junit.Assert; +import org.junit.Test; + +public class AsyncServletTest { + + @Test + public void testDebugDisabled() { + Assert.assertFalse("debugging enabled", AsyncServlet.AsyncSession.DEBUG_ASYNC); + } + +} -- cgit v1.2.3 From c7d721cfc55b614ab4bf3d656381b7b590ae99e9 Mon Sep 17 00:00:00 2001 From: Anton Tananaev Date: Mon, 16 Nov 2015 11:33:26 +1300 Subject: Add outdated position boolean flag --- src/org/traccar/BaseProtocolDecoder.java | 2 ++ src/org/traccar/model/Position.java | 10 ++++++++++ 2 files changed, 12 insertions(+) (limited to 'src') diff --git a/src/org/traccar/BaseProtocolDecoder.java b/src/org/traccar/BaseProtocolDecoder.java index f8abdcc85..bd91f5e09 100644 --- a/src/org/traccar/BaseProtocolDecoder.java +++ b/src/org/traccar/BaseProtocolDecoder.java @@ -75,6 +75,8 @@ public abstract class BaseProtocolDecoder extends ExtendedObjectDecoder { } public void getLastLocation(Position position, Date deviceTime) { + position.setOutdated(true); + Position last = Context.getConnectionManager().getLastPosition(getDeviceId()); if (last != null) { position.setFixTime(last.getFixTime()); diff --git a/src/org/traccar/model/Position.java b/src/org/traccar/model/Position.java index 1ed559ecd..9494da042 100644 --- a/src/org/traccar/model/Position.java +++ b/src/org/traccar/model/Position.java @@ -47,6 +47,16 @@ public class Position extends Event implements Factory { setFixTime(time); } + private boolean outdated; + + public boolean getOutdated() { + return outdated; + } + + public void setOutdated(boolean outdated) { + this.outdated = outdated; + } + private boolean valid; public boolean getValid() { -- cgit v1.2.3 From 306098fbeda545babb2c48c80e7fe98e22abb801 Mon Sep 17 00:00:00 2001 From: Anton Tananaev Date: Mon, 16 Nov 2015 11:46:59 +1300 Subject: Update device status in the database --- debug.xml | 4 ++++ src/org/traccar/database/ConnectionManager.java | 15 ++++++++++++--- src/org/traccar/database/DataManager.java | 6 ++++++ 3 files changed, 22 insertions(+), 3 deletions(-) (limited to 'src') diff --git a/debug.xml b/debug.xml index 47f17bdae..b09a73d0a 100644 --- a/debug.xml +++ b/debug.xml @@ -209,6 +209,10 @@ UPDATE device SET name = :name, uniqueId = :uniqueId WHERE id = :id; + + UPDATE device SET status = :status, lastUpdate = :lastUpdate WHERE id = :id; + + DELETE FROM device WHERE id = :id; diff --git a/src/org/traccar/database/ConnectionManager.java b/src/org/traccar/database/ConnectionManager.java index 450f2f61f..cb7b4b6d8 100644 --- a/src/org/traccar/database/ConnectionManager.java +++ b/src/org/traccar/database/ConnectionManager.java @@ -26,6 +26,7 @@ import java.util.List; import java.util.Map; import java.util.Set; import org.jboss.netty.channel.Channel; +import org.traccar.Context; import org.traccar.Protocol; import org.traccar.helper.Log; import org.traccar.model.Device; @@ -68,9 +69,17 @@ public class ConnectionManager { } public synchronized void updateDevice(long deviceId, String status, Date time) { - // TODO update cache and call listener - /*Log.debug(deviceId + " " + status + " " - + new SimpleDateFormat(Log.DATE_FORMAT).format(time));*/ + Device device = Context.getIdentityManager().getDeviceById(deviceId); + device.setStatus(status); + device.setLastUpdate(time); + + try { + Context.getDataManager().updateDeviceStatus(device); + } catch (SQLException error) { + Log.warning(error); + } + + // TODO call listener } public synchronized void updatePosition(Position position) { diff --git a/src/org/traccar/database/DataManager.java b/src/org/traccar/database/DataManager.java index 24a07a05c..a40b87d24 100644 --- a/src/org/traccar/database/DataManager.java +++ b/src/org/traccar/database/DataManager.java @@ -310,6 +310,12 @@ public class DataManager implements IdentityManager { .executeUpdate(); } + public void updateDeviceStatus(Device device) throws SQLException { + QueryBuilder.create(dataSource, getQuery("database.updateDeviceStatus")) + .setObject(device) + .executeUpdate(); + } + public void removeDevice(Device device) throws SQLException { QueryBuilder.create(dataSource, getQuery("database.deleteDevice")) .setObject(device) -- cgit v1.2.3 From 650d6f02af8a902dccb2d59e999c46de8d72a165 Mon Sep 17 00:00:00 2001 From: Anton Tananaev Date: Mon, 16 Nov 2015 11:57:26 +1300 Subject: Ignore HTTP protocol connection status --- src/org/traccar/MainEventHandler.java | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) (limited to 'src') diff --git a/src/org/traccar/MainEventHandler.java b/src/org/traccar/MainEventHandler.java index 37f0ee387..fa908a70c 100644 --- a/src/org/traccar/MainEventHandler.java +++ b/src/org/traccar/MainEventHandler.java @@ -21,6 +21,7 @@ import org.jboss.netty.channel.ChannelStateEvent; import org.jboss.netty.channel.ExceptionEvent; import org.jboss.netty.channel.MessageEvent; import org.jboss.netty.channel.socket.DatagramChannel; +import org.jboss.netty.handler.codec.http.HttpRequestDecoder; import org.jboss.netty.handler.timeout.IdleStateAwareChannelHandler; import org.jboss.netty.handler.timeout.IdleStateEvent; import org.traccar.helper.Log; @@ -66,7 +67,9 @@ public class MainEventHandler extends IdleStateAwareChannelHandler { Log.info(formatChannel(e.getChannel()) + " disconnected"); closeChannel(e.getChannel()); - Context.getConnectionManager().removeActiveDevice(e.getChannel()); + if (ctx.getPipeline().get("httpDecoder") == null) { + Context.getConnectionManager().removeActiveDevice(e.getChannel()); + } } @Override -- cgit v1.2.3 From 28d60da9a2f80fa8fc3dd28ec5bdd7847a36727c Mon Sep 17 00:00:00 2001 From: Anton Tananaev Date: Mon, 16 Nov 2015 12:24:24 +1300 Subject: Extend async request to include devices --- src/org/traccar/web/AsyncServlet.java | 11 ++++++++++- web/app/controller/Root.js | 13 ++++++++----- 2 files changed, 18 insertions(+), 6 deletions(-) (limited to 'src') diff --git a/src/org/traccar/web/AsyncServlet.java b/src/org/traccar/web/AsyncServlet.java index 3347f765c..ddcbce2ff 100644 --- a/src/org/traccar/web/AsyncServlet.java +++ b/src/org/traccar/web/AsyncServlet.java @@ -158,7 +158,16 @@ public class AsyncServlet extends BaseServlet { JsonObjectBuilder result = Json.createObjectBuilder(); result.add("success", true); - result.add("data", JsonConverter.arrayToJson(positions.values())); + + if (Context.getConfig().getBoolean("web.oldAsyncFormat")) { + result.add("data", JsonConverter.arrayToJson(positions.values())); + } else { + JsonObjectBuilder data = Json.createObjectBuilder(); + data.add("devices", Json.createArrayBuilder().build()); // TODO: send device status + data.add("positions", JsonConverter.arrayToJson(positions.values())); + result.add("data", data.build()); + } + positions.clear(); try { diff --git a/web/app/controller/Root.js b/web/app/controller/Root.js index 79827b8a6..b260cb8a1 100644 --- a/web/app/controller/Root.js +++ b/web/app/controller/Root.js @@ -96,19 +96,22 @@ Ext.define('Traccar.controller.Root', { first: first }, callback: Traccar.app.getErrorHandler(this, function (options, success, response) { - var i, store, data, position; + var i, store, data, devices, positions, position; if (success) { store = Ext.getStore('LatestPositions'); data = Ext.decode(response.responseText).data; + devices = data.devices; + positions = data.positions; - for (i = 0; i < data.length; i++) { - position = store.findRecord('deviceId', data[i].deviceId, 0, false, false, true); + for (i = 0; i < positions.length; i++) { + position = store.findRecord('deviceId', positions[i].deviceId, 0, false, false, true); if (position) { - position.set(data[i]); + position.set(positions[i]); } else { - store.add(Ext.create('Traccar.model.Position', data[i])); + store.add(Ext.create('Traccar.model.Position', positions[i])); } } + this.asyncUpdate(false); } }) -- cgit v1.2.3 From 3471e62b0f3949621265c80dcaf2ba714aade697 Mon Sep 17 00:00:00 2001 From: Anton Tananaev Date: Tue, 17 Nov 2015 11:36:08 +1300 Subject: Finish Huabao protocol implementation --- src/org/traccar/MainEventHandler.java | 1 - .../traccar/protocol/HuabaoProtocolDecoder.java | 113 ++++++++++++++++++++- .../protocol/HuabaoProtocolDecoderTest.java | 9 ++ 3 files changed, 118 insertions(+), 5 deletions(-) (limited to 'src') diff --git a/src/org/traccar/MainEventHandler.java b/src/org/traccar/MainEventHandler.java index fa908a70c..f0ef36e5b 100644 --- a/src/org/traccar/MainEventHandler.java +++ b/src/org/traccar/MainEventHandler.java @@ -21,7 +21,6 @@ import org.jboss.netty.channel.ChannelStateEvent; import org.jboss.netty.channel.ExceptionEvent; import org.jboss.netty.channel.MessageEvent; import org.jboss.netty.channel.socket.DatagramChannel; -import org.jboss.netty.handler.codec.http.HttpRequestDecoder; import org.jboss.netty.handler.timeout.IdleStateAwareChannelHandler; import org.jboss.netty.handler.timeout.IdleStateEvent; import org.traccar.helper.Log; diff --git a/src/org/traccar/protocol/HuabaoProtocolDecoder.java b/src/org/traccar/protocol/HuabaoProtocolDecoder.java index 931f985a5..792c89c8b 100644 --- a/src/org/traccar/protocol/HuabaoProtocolDecoder.java +++ b/src/org/traccar/protocol/HuabaoProtocolDecoder.java @@ -16,10 +16,20 @@ 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.helper.BitUtil; +import org.traccar.helper.ChannelBufferTools; +import org.traccar.helper.Checksum; +import org.traccar.helper.DateBuilder; +import org.traccar.helper.UnitsConverter; +import org.traccar.model.Event; +import org.traccar.model.Position; import java.net.SocketAddress; +import java.nio.charset.Charset; +import java.util.TimeZone; public class HuabaoProtocolDecoder extends BaseProtocolDecoder { @@ -27,7 +37,38 @@ public class HuabaoProtocolDecoder extends BaseProtocolDecoder { super(protocol); } + public static final int MSG_GENERAL_RESPONSE = 0x8001; public static final int MSG_TERMINAL_REGISTER = 0x0100; + public static final int MSG_TERMINAL_REGISTER_RESPONSE = 0x8100; + public static final int MSG_TERMINAL_AUTH = 0x0102; + public static final int MSG_LOCATION_REPORT = 0x0200; + + public static final int RESULT_SUCCESS = 0; + + private void sendResponse( + Channel channel, SocketAddress remoteAddress, int type, ChannelBuffer id, ChannelBuffer data) { + ChannelBuffer response = ChannelBuffers.dynamicBuffer(); + response.writeByte(0x7e); + response.writeShort(type); + response.writeShort(data.readableBytes()); + response.writeBytes(id); + response.writeShort(1); // index + response.writeBytes(data); + response.writeByte(Checksum.xor(response.toByteBuffer(1, response.readableBytes() - 1))); + response.writeByte(0x7e); + if (channel != null) { + channel.write(response, remoteAddress); + } + } + + private void sendGeneralResponse( + Channel channel, SocketAddress remoteAddress, ChannelBuffer id, int type, int index) { + ChannelBuffer response = ChannelBuffers.dynamicBuffer(); + response.writeShort(index); + response.writeShort(type); + response.writeByte(RESULT_SUCCESS); + sendResponse(channel, remoteAddress, MSG_GENERAL_RESPONSE, id, response); + } @Override protected Object decode( @@ -36,10 +77,74 @@ public class HuabaoProtocolDecoder extends BaseProtocolDecoder { ChannelBuffer buf = (ChannelBuffer) msg; buf.readUnsignedByte(); // start marker - //int type = buf.readUnsignedShort(); - //int flags = buf.readUnsignedShort(); - buf.skipBytes(6); // phone number - buf.readUnsignedShort(); // index + int type = buf.readUnsignedShort(); + buf.readUnsignedShort(); // body length + ChannelBuffer id = buf.readBytes(6); // phone number + int index = buf.readUnsignedShort(); + + if (!identify(id.toString(Charset.defaultCharset()), channel, remoteAddress)) { + return null; + } + + if (type == MSG_TERMINAL_REGISTER) { + + ChannelBuffer response = ChannelBuffers.dynamicBuffer(); + response.writeShort(index); + response.writeByte(RESULT_SUCCESS); + response.writeBytes("authentication".getBytes(Charset.defaultCharset())); + sendResponse(channel, remoteAddress, MSG_TERMINAL_REGISTER_RESPONSE, id, response); + + } else if (type == MSG_TERMINAL_AUTH) { + + sendGeneralResponse(channel, remoteAddress, id, type, index); + + } else if (type == MSG_LOCATION_REPORT) { + + Position position = new Position(); + position.setProtocol(getProtocolName()); + position.setDeviceId(getDeviceId()); + + position.set(Event.KEY_ALARM, buf.readUnsignedInt()); + + int flags = buf.readInt(); + + position.set(Event.KEY_IGNITION, BitUtil.check(flags, 0)); + + position.setValid(BitUtil.check(flags, 1)); + + double lat = buf.readUnsignedInt() * 0.000001; + double lon = buf.readUnsignedInt() * 0.000001; + + if (BitUtil.check(flags, 2)) { + position.setLatitude(-lat); + } else { + position.setLatitude(lat); + } + + if (BitUtil.check(flags, 3)) { + position.setLongitude(-lon); + } else { + position.setLongitude(lon); + } + + position.setAltitude(buf.readShort()); + position.setSpeed(UnitsConverter.knotsFromKph(buf.readUnsignedShort() * 0.1)); + position.setCourse(buf.readUnsignedShort()); + + DateBuilder dateBuilder = new DateBuilder(TimeZone.getTimeZone("GMT+8")) + .setYear(ChannelBufferTools.readHexInteger(buf, 2)) + .setMonth(ChannelBufferTools.readHexInteger(buf, 2)) + .setDay(ChannelBufferTools.readHexInteger(buf, 2)) + .setHour(ChannelBufferTools.readHexInteger(buf, 2)) + .setMinute(ChannelBufferTools.readHexInteger(buf, 2)) + .setSecond(ChannelBufferTools.readHexInteger(buf, 2)); + position.setTime(dateBuilder.getDate()); + + // additional information + + return position; + + } return null; } diff --git a/test/org/traccar/protocol/HuabaoProtocolDecoderTest.java b/test/org/traccar/protocol/HuabaoProtocolDecoderTest.java index 0968d6d9c..1e2290851 100644 --- a/test/org/traccar/protocol/HuabaoProtocolDecoderTest.java +++ b/test/org/traccar/protocol/HuabaoProtocolDecoderTest.java @@ -10,9 +10,18 @@ public class HuabaoProtocolDecoderTest extends ProtocolDecoderTest { HuabaoProtocolDecoder decoder = new HuabaoProtocolDecoder(new HuabaoProtocol()); + verifyNothing(decoder, binary( + "7E0100002D013511221122000500000000373031303748422D52303347424400000000000000000000003233363631303402CBD5424136383630387E")); + verifyNothing(decoder, binary( "7e0100002d007089994489002800000000000000000048422d523033474244000000000000000000000031393036373531024142433030303030d17e")); + verifyNothing(decoder, binary( + "7E0102000E013511221122000661757468656E7469636174696F6E3F7E")); + + verifyPosition(decoder, binary( + "7E02000032013511221122000700000000000C000301578CC006CA3A5C00470000000014072317201501040000000030011631010BD07E")); + } } -- cgit v1.2.3 From 5e56a1fbf80033cf5cc30ec7767d3cd92ab41cde Mon Sep 17 00:00:00 2001 From: Anton Tananaev Date: Tue, 17 Nov 2015 13:46:03 +1300 Subject: Update device status via async web service --- src/org/traccar/database/ConnectionManager.java | 28 ++++++++++------- src/org/traccar/web/AsyncServlet.java | 40 ++++++++++++++++++------- 2 files changed, 47 insertions(+), 21 deletions(-) (limited to 'src') diff --git a/src/org/traccar/database/ConnectionManager.java b/src/org/traccar/database/ConnectionManager.java index cb7b4b6d8..345bb4577 100644 --- a/src/org/traccar/database/ConnectionManager.java +++ b/src/org/traccar/database/ConnectionManager.java @@ -36,13 +36,13 @@ public class ConnectionManager { private final Map activeDevices = new HashMap<>(); private final Map positions = new HashMap<>(); - private final Map> listeners = new HashMap<>(); + private final Map> listeners = new HashMap<>(); public ConnectionManager(DataManager dataManager) { if (dataManager != null) { try { for (Position position : dataManager.getLatestPositions()) { - this.positions.put(position.getDeviceId(), position); + positions.put(position.getDeviceId(), position); } } catch (SQLException error) { Log.warning(error); @@ -79,14 +79,19 @@ public class ConnectionManager { Log.warning(error); } - // TODO call listener + if (listeners.containsKey(deviceId)) { + for (UpdateListener listener : listeners.get(deviceId)) { + listener.onUpdateDevice(device); + } + } } public synchronized void updatePosition(Position position) { long deviceId = position.getDeviceId(); positions.put(deviceId, position); + if (listeners.containsKey(deviceId)) { - for (DataCacheListener listener : listeners.get(deviceId)) { + for (UpdateListener listener : listeners.get(deviceId)) { listener.onUpdatePosition(position); } } @@ -109,32 +114,33 @@ public class ConnectionManager { return result; } - public interface DataCacheListener { + public interface UpdateListener { + void onUpdateDevice(Device device); void onUpdatePosition(Position position); } - public void addListener(Collection devices, DataCacheListener listener) { + public void addListener(Collection devices, UpdateListener listener) { for (long deviceId : devices) { addListener(deviceId, listener); } } - public synchronized void addListener(long deviceId, DataCacheListener listener) { + public synchronized void addListener(long deviceId, UpdateListener listener) { if (!listeners.containsKey(deviceId)) { - listeners.put(deviceId, new HashSet()); + listeners.put(deviceId, new HashSet()); } listeners.get(deviceId).add(listener); } - public void removeListener(Collection devices, DataCacheListener listener) { + public void removeListener(Collection devices, UpdateListener listener) { for (long deviceId : devices) { removeListener(deviceId, listener); } } - public synchronized void removeListener(long deviceId, DataCacheListener listener) { + public synchronized void removeListener(long deviceId, UpdateListener listener) { if (!listeners.containsKey(deviceId)) { - listeners.put(deviceId, new HashSet()); + listeners.put(deviceId, new HashSet()); } listeners.get(deviceId).remove(listener); } diff --git a/src/org/traccar/web/AsyncServlet.java b/src/org/traccar/web/AsyncServlet.java index ddcbce2ff..c55fd6e75 100644 --- a/src/org/traccar/web/AsyncServlet.java +++ b/src/org/traccar/web/AsyncServlet.java @@ -36,6 +36,7 @@ import org.traccar.Context; import org.traccar.GlobalTimer; import org.traccar.database.ConnectionManager; import org.traccar.helper.Log; +import org.traccar.model.Device; import org.traccar.model.Position; public class AsyncServlet extends BaseServlet { @@ -60,7 +61,8 @@ public class AsyncServlet extends BaseServlet { private final Set devices = new HashSet<>(); private Timeout sessionTimeout; private Timeout requestTimeout; - private final Map positions = new HashMap<>(); + private final Set deviceUpdates = new HashSet<>(); + private final Set positionUpdates = new HashSet<>(); private AsyncContext activeContext; private void logEvent(String message) { @@ -76,7 +78,7 @@ public class AsyncServlet extends BaseServlet { Collection initialPositions = Context.getConnectionManager().getInitialState(devices); for (Position position : initialPositions) { - positions.put(position.getDeviceId(), position); + positionUpdates.add(position); } Context.getConnectionManager().addListener(devices, dataListener); @@ -86,17 +88,34 @@ public class AsyncServlet extends BaseServlet { return devices.contains(deviceId); } - private final ConnectionManager.DataCacheListener dataListener = new ConnectionManager.DataCacheListener() { + private final ConnectionManager.UpdateListener dataListener = new ConnectionManager.UpdateListener() { + @Override + public void onUpdateDevice(Device device) { + synchronized (AsyncSession.this) { + logEvent("onUpdateDevice deviceId: " + device.getId()); + if (!destroyed) { + if (requestTimeout != null) { + requestTimeout.cancel(); + requestTimeout = null; + } + deviceUpdates.add(device); + if (activeContext != null) { + response(); + } + } + } + } + @Override public void onUpdatePosition(Position position) { synchronized (AsyncSession.this) { - logEvent("onUpdate deviceId: " + position.getDeviceId()); + logEvent("onUpdatePosition deviceId: " + position.getDeviceId()); if (!destroyed) { if (requestTimeout != null) { requestTimeout.cancel(); requestTimeout = null; } - positions.put(position.getDeviceId(), position); + positionUpdates.add(position); if (activeContext != null) { response(); } @@ -142,7 +161,7 @@ public class AsyncServlet extends BaseServlet { sessionTimeout = null; } - if (!positions.isEmpty()) { + if (!deviceUpdates.isEmpty() || !positionUpdates.isEmpty()) { response(); } else { requestTimeout = GlobalTimer.getTimer().newTimeout( @@ -160,15 +179,16 @@ public class AsyncServlet extends BaseServlet { result.add("success", true); if (Context.getConfig().getBoolean("web.oldAsyncFormat")) { - result.add("data", JsonConverter.arrayToJson(positions.values())); + result.add("data", JsonConverter.arrayToJson(positionUpdates)); } else { JsonObjectBuilder data = Json.createObjectBuilder(); - data.add("devices", Json.createArrayBuilder().build()); // TODO: send device status - data.add("positions", JsonConverter.arrayToJson(positions.values())); + data.add("devices", JsonConverter.arrayToJson(deviceUpdates)); + data.add("positions", JsonConverter.arrayToJson(positionUpdates)); result.add("data", data.build()); } - positions.clear(); + deviceUpdates.clear(); + positionUpdates.clear(); try { response.getWriter().println(result.build().toString()); -- cgit v1.2.3 From 65361d8f0359fe2c4e02e76892a565683743b796 Mon Sep 17 00:00:00 2001 From: Ivan Martinez Date: Mon, 16 Nov 2015 23:50:14 -0200 Subject: add support for configurable handlers --- src/org/traccar/BasePipelineFactory.java | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) (limited to 'src') diff --git a/src/org/traccar/BasePipelineFactory.java b/src/org/traccar/BasePipelineFactory.java index c0952db86..7273d51cf 100644 --- a/src/org/traccar/BasePipelineFactory.java +++ b/src/org/traccar/BasePipelineFactory.java @@ -19,6 +19,7 @@ import java.net.InetSocketAddress; import org.jboss.netty.buffer.ChannelBuffer; import org.jboss.netty.buffer.ChannelBuffers; import org.jboss.netty.channel.ChannelEvent; +import org.jboss.netty.channel.ChannelHandler; import org.jboss.netty.channel.ChannelHandlerContext; import org.jboss.netty.channel.ChannelPipeline; import org.jboss.netty.channel.ChannelPipelineFactory; @@ -130,6 +131,9 @@ public abstract class BasePipelineFactory implements ChannelPipelineFactory { pipeline.addLast("geocoder", reverseGeocoderHandler); } pipeline.addLast("remoteAddress", new RemoteAddressHandler()); + + addDynamicHandlers(pipeline, "extraHandler", "extra.handlers"); + if (Context.getDataManager() != null) { pipeline.addLast("dataHandler", new DefaultDataHandler()); } @@ -140,4 +144,19 @@ public abstract class BasePipelineFactory implements ChannelPipelineFactory { return pipeline; } + private void addDynamicHandlers(ChannelPipeline pipeline, String id, String key) { + final String[] extraHandlers = Context.getConfig().getString(key,"").split(","); + int i = 0; + for (String extraHandler : extraHandlers) { + try { + Class c = Class.forName(extraHandler); + i++; + pipeline.addLast(id + "."+i, (ChannelHandler)c.newInstance()); + } catch (Throwable e) { + Log.error("Error loading handler : " + extraHandler + " " + e); + } + } + + } + } -- cgit v1.2.3 From 06fa2ada9247586f6b2eac7906bd9ada50c91577 Mon Sep 17 00:00:00 2001 From: Anton Tananaev Date: Tue, 17 Nov 2015 17:35:32 +1300 Subject: Implement timeout for device status --- src/org/traccar/database/ConnectionManager.java | 35 ++++++++++++++++++++++--- 1 file changed, 32 insertions(+), 3 deletions(-) (limited to 'src') diff --git a/src/org/traccar/database/ConnectionManager.java b/src/org/traccar/database/ConnectionManager.java index 345bb4577..ae931151d 100644 --- a/src/org/traccar/database/ConnectionManager.java +++ b/src/org/traccar/database/ConnectionManager.java @@ -25,8 +25,13 @@ import java.util.LinkedList; import java.util.List; import java.util.Map; import java.util.Set; +import java.util.concurrent.TimeUnit; + import org.jboss.netty.channel.Channel; +import org.jboss.netty.util.Timeout; +import org.jboss.netty.util.TimerTask; import org.traccar.Context; +import org.traccar.GlobalTimer; import org.traccar.Protocol; import org.traccar.helper.Log; import org.traccar.model.Device; @@ -34,11 +39,17 @@ import org.traccar.model.Position; public class ConnectionManager { + private static final long DEFAULT_TIMEOUT = 600; + + private final long deviceTimeout; + private final Map activeDevices = new HashMap<>(); private final Map positions = new HashMap<>(); private final Map> listeners = new HashMap<>(); + private final Map timeouts = new HashMap<>(); public ConnectionManager(DataManager dataManager) { + deviceTimeout = Context.getConfig().getLong("status.timeout", DEFAULT_TIMEOUT) * 1000; if (dataManager != null) { try { for (Position position : dataManager.getLatestPositions()) { @@ -57,7 +68,7 @@ public class ConnectionManager { public void removeActiveDevice(Channel channel) { for (ActiveDevice activeDevice : activeDevices.values()) { if (activeDevice.getChannel() == channel) { - updateDevice(activeDevice.getDeviceId(), Device.STATUS_OFFLINE, new Date()); + updateDevice(activeDevice.getDeviceId(), Device.STATUS_OFFLINE, null); activeDevices.remove(activeDevice.getDeviceId()); break; } @@ -68,10 +79,28 @@ public class ConnectionManager { return activeDevices.get(deviceId); } - public synchronized void updateDevice(long deviceId, String status, Date time) { + public synchronized void updateDevice(final long deviceId, String status, Date time) { Device device = Context.getIdentityManager().getDeviceById(deviceId); device.setStatus(status); - device.setLastUpdate(time); + if (time != null) { + device.setLastUpdate(time); + } + + Timeout timeout = timeouts.remove(deviceId); + if (timeout != null) { + timeout.cancel(); + } + + if (status.equals(Device.STATUS_ONLINE)) { + timeouts.put(deviceId, GlobalTimer.getTimer().newTimeout(new TimerTask() { + @Override + public void run(Timeout timeout) throws Exception { + if (!timeout.isCancelled()) { + updateDevice(deviceId, Device.STATUS_UNKNOWN, null); + } + } + }, deviceTimeout, TimeUnit.MILLISECONDS)); + } try { Context.getDataManager().updateDeviceStatus(device); -- cgit v1.2.3 From 2776f3e5a7d837feff46c003ec32c162ac71b970 Mon Sep 17 00:00:00 2001 From: Anton Tananaev Date: Wed, 18 Nov 2015 10:08:48 +1300 Subject: Clean up adding dynamic handlers --- src/org/traccar/BasePipelineFactory.java | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) (limited to 'src') diff --git a/src/org/traccar/BasePipelineFactory.java b/src/org/traccar/BasePipelineFactory.java index 7273d51cf..6b877883d 100644 --- a/src/org/traccar/BasePipelineFactory.java +++ b/src/org/traccar/BasePipelineFactory.java @@ -120,7 +120,9 @@ public abstract class BasePipelineFactory implements ChannelPipelineFactory { if (Context.isLoggerEnabled()) { pipeline.addLast("logger", new StandardLoggingHandler()); } + addSpecificHandlers(pipeline); + if (filterHandler != null) { pipeline.addLast("filter", filterHandler); } @@ -132,7 +134,7 @@ public abstract class BasePipelineFactory implements ChannelPipelineFactory { } pipeline.addLast("remoteAddress", new RemoteAddressHandler()); - addDynamicHandlers(pipeline, "extraHandler", "extra.handlers"); + addDynamicHandlers(pipeline); if (Context.getDataManager() != null) { pipeline.addLast("dataHandler", new DefaultDataHandler()); @@ -144,19 +146,17 @@ public abstract class BasePipelineFactory implements ChannelPipelineFactory { return pipeline; } - private void addDynamicHandlers(ChannelPipeline pipeline, String id, String key) { - final String[] extraHandlers = Context.getConfig().getString(key,"").split(","); - int i = 0; - for (String extraHandler : extraHandlers) { - try { - Class c = Class.forName(extraHandler); - i++; - pipeline.addLast(id + "."+i, (ChannelHandler)c.newInstance()); - } catch (Throwable e) { - Log.error("Error loading handler : " + extraHandler + " " + e); + private void addDynamicHandlers(ChannelPipeline pipeline) { + if (Context.getConfig().hasKey("extra.handlers")) { + String[] handlers = Context.getConfig().getString("extra.handlers").split(","); + for (int i = 0; i < handlers.length; i++) { + try { + pipeline.addLast("extraHandler." + i, (ChannelHandler) Class.forName(handlers[i]).newInstance()); + } catch (ClassNotFoundException | InstantiationException | IllegalAccessException error) { + Log.warning(error); + } } } - } - + } -- cgit v1.2.3 From 5194dda39cbe7815da9946e0e6822ddc5d27ad3d Mon Sep 17 00:00:00 2001 From: Anton Tananaev Date: Wed, 18 Nov 2015 10:49:40 +1300 Subject: Ignore null values in json object --- src/org/traccar/web/JsonConverter.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src') diff --git a/src/org/traccar/web/JsonConverter.java b/src/org/traccar/web/JsonConverter.java index b19894dd6..c01ce8bd6 100644 --- a/src/org/traccar/web/JsonConverter.java +++ b/src/org/traccar/web/JsonConverter.java @@ -66,7 +66,7 @@ public final class JsonConverter { final String name = Introspector.decapitalize(method.getName().substring(3)); Class parameterType = method.getParameterTypes()[0]; - if (json.containsKey(name)) { + if (json.containsKey(name) && !json.isNull(name)) { try { if (parameterType.equals(boolean.class)) { method.invoke(object, json.getBoolean(name)); -- cgit v1.2.3 From 78e325196729202e416655e87cf7d1593cda2cc7 Mon Sep 17 00:00:00 2001 From: Anton Tananaev Date: Wed, 18 Nov 2015 13:24:28 +1300 Subject: Initial code for location providers --- src/org/traccar/location/BaseLocationProvider.java | 37 ++++++++++++++++++ src/org/traccar/location/LocationProvider.java | 30 +++++++++++++++ .../location/OpenCellIdLocationProvider.java | 24 ++++++++++++ test/org/traccar/geocode/ReverseGeocoderTest.java | 4 -- .../org/traccar/location/LocationProviderTest.java | 44 ++++++++++++++++++++++ 5 files changed, 135 insertions(+), 4 deletions(-) create mode 100644 src/org/traccar/location/BaseLocationProvider.java create mode 100644 src/org/traccar/location/LocationProvider.java create mode 100644 src/org/traccar/location/OpenCellIdLocationProvider.java create mode 100644 test/org/traccar/location/LocationProviderTest.java (limited to 'src') diff --git a/src/org/traccar/location/BaseLocationProvider.java b/src/org/traccar/location/BaseLocationProvider.java new file mode 100644 index 000000000..80b10b33c --- /dev/null +++ b/src/org/traccar/location/BaseLocationProvider.java @@ -0,0 +1,37 @@ +/* + * Copyright 2015 Anton Tananaev (anton.tananaev@gmail.com) + * + * 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.location; + +import org.traccar.model.Event; + +import java.util.Map; + +public abstract class BaseLocationProvider implements LocationProvider { + + @Override + public void getLocation(Map attributes, LocationProviderCallback callback) { + if (attributes.containsKey(Event.KEY_MCC) || attributes.containsKey(Event.KEY_MNC)) { + Number mcc = (Number) attributes.get(Event.KEY_MCC); + Number mnc = (Number) attributes.get(Event.KEY_MNC); + Number lac = (Number) attributes.get(Event.KEY_LAC); + Number cid = (Number) attributes.get(Event.KEY_CID); + getLocation(mcc.intValue(), mnc.intValue(), lac.longValue(), cid.longValue(), callback); + } + } + + protected abstract void getLocation(int mcc, int mnc, long lac, long cid, LocationProviderCallback callback); + +} diff --git a/src/org/traccar/location/LocationProvider.java b/src/org/traccar/location/LocationProvider.java new file mode 100644 index 000000000..cbc9663f8 --- /dev/null +++ b/src/org/traccar/location/LocationProvider.java @@ -0,0 +1,30 @@ +/* + * Copyright 2015 Anton Tananaev (anton.tananaev@gmail.com) + * + * 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.location; + +import java.util.Map; + +public interface LocationProvider { + + interface LocationProviderCallback { + + void onResult(double latitude, double longitude); + + } + + void getLocation(Map attributes, LocationProviderCallback callback); + +} diff --git a/src/org/traccar/location/OpenCellIdLocationProvider.java b/src/org/traccar/location/OpenCellIdLocationProvider.java new file mode 100644 index 000000000..22456c482 --- /dev/null +++ b/src/org/traccar/location/OpenCellIdLocationProvider.java @@ -0,0 +1,24 @@ +/* + * Copyright 2015 Anton Tananaev (anton.tananaev@gmail.com) + * + * 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.location; + +public class OpenCellIdLocationProvider extends BaseLocationProvider { + + protected void getLocation(int mcc, int mnc, long lac, long cid, LocationProviderCallback callback) { + // TODO + } + +} diff --git a/test/org/traccar/geocode/ReverseGeocoderTest.java b/test/org/traccar/geocode/ReverseGeocoderTest.java index 2336418d8..a572b0456 100644 --- a/test/org/traccar/geocode/ReverseGeocoderTest.java +++ b/test/org/traccar/geocode/ReverseGeocoderTest.java @@ -14,11 +14,9 @@ public class ReverseGeocoderTest { testNominatim(); testGisgraphy(); } - } public void testGoogle() { - ReverseGeocoder reverseGeocoder = new GoogleReverseGeocoder(); reverseGeocoder.getAddress(new AddressFormat(), 37.4217550, -122.0846330, new ReverseGeocoder.ReverseGeocoderCallback() { @@ -30,7 +28,6 @@ public class ReverseGeocoderTest { } public void testNominatim() { - ReverseGeocoder reverseGeocoder = new NominatimReverseGeocoder(); reverseGeocoder.getAddress(new AddressFormat(), 40.7337807, -73.9974401, new ReverseGeocoder.ReverseGeocoderCallback() { @@ -42,7 +39,6 @@ public class ReverseGeocoderTest { } public void testGisgraphy() { - ReverseGeocoder reverseGeocoder = new GisgraphyReverseGeocoder(); reverseGeocoder.getAddress(new AddressFormat(), 48.8530000, 2.3400000, new ReverseGeocoder.ReverseGeocoderCallback() { diff --git a/test/org/traccar/location/LocationProviderTest.java b/test/org/traccar/location/LocationProviderTest.java new file mode 100644 index 000000000..c4aedbb09 --- /dev/null +++ b/test/org/traccar/location/LocationProviderTest.java @@ -0,0 +1,44 @@ +package org.traccar.location; + +import org.junit.Assert; +import org.junit.Test; +import org.traccar.geocode.AddressFormat; +import org.traccar.geocode.GisgraphyReverseGeocoder; +import org.traccar.geocode.GoogleReverseGeocoder; +import org.traccar.geocode.NominatimReverseGeocoder; +import org.traccar.geocode.ReverseGeocoder; +import org.traccar.model.Event; + +import java.util.HashMap; +import java.util.Map; + +public class LocationProviderTest { + + private boolean enable = false; + + @Test + public void test() { + if (enable) { + testOpenCellId(); + } + } + + public void testOpenCellId() { + OpenCellIdLocationProvider locationProvider = new OpenCellIdLocationProvider(); + + Map attributes = new HashMap<>(); + attributes.put(Event.KEY_MCC, 250); + attributes.put(Event.KEY_MNC, 2); + attributes.put(Event.KEY_LAC, 4711); + attributes.put(Event.KEY_CID, 7989334); + + locationProvider.getLocation(attributes, new LocationProvider.LocationProviderCallback() { + @Override + public void onResult(double latitude, double longitude) { + Assert.assertEquals(60.07254, latitude, 0.00001); + Assert.assertEquals(30.30996, longitude, 0.00001); + } + }); + } + +} -- cgit v1.2.3 From 6ec22b7a68b5334e215662e4cb83a20f9a1cad18 Mon Sep 17 00:00:00 2001 From: Anton Tananaev Date: Wed, 18 Nov 2015 14:01:00 +1300 Subject: Implement OpenCellID location provider --- src/org/traccar/location/LocationProvider.java | 4 +- .../location/OpenCellIdLocationProvider.java | 44 +++++++++++++++++++++- .../org/traccar/location/LocationProviderTest.java | 15 +++++--- 3 files changed, 55 insertions(+), 8 deletions(-) (limited to 'src') diff --git a/src/org/traccar/location/LocationProvider.java b/src/org/traccar/location/LocationProvider.java index cbc9663f8..2bff1a7ca 100644 --- a/src/org/traccar/location/LocationProvider.java +++ b/src/org/traccar/location/LocationProvider.java @@ -21,7 +21,9 @@ public interface LocationProvider { interface LocationProviderCallback { - void onResult(double latitude, double longitude); + void onSuccess(double latitude, double longitude); + + void onFailure(); } diff --git a/src/org/traccar/location/OpenCellIdLocationProvider.java b/src/org/traccar/location/OpenCellIdLocationProvider.java index 22456c482..2339585ad 100644 --- a/src/org/traccar/location/OpenCellIdLocationProvider.java +++ b/src/org/traccar/location/OpenCellIdLocationProvider.java @@ -15,10 +15,50 @@ */ package org.traccar.location; +import com.ning.http.client.AsyncCompletionHandler; +import com.ning.http.client.Response; +import org.traccar.Context; + +import javax.json.Json; +import javax.json.JsonObject; +import javax.json.JsonReader; + public class OpenCellIdLocationProvider extends BaseLocationProvider { - protected void getLocation(int mcc, int mnc, long lac, long cid, LocationProviderCallback callback) { - // TODO + private String url; + + public OpenCellIdLocationProvider(String key) { + this("http://opencellid.org/cell/get", key); + } + + public OpenCellIdLocationProvider(String url, String key) { + this.url = url + "?format=json&mcc=%d&mnc=%d&lac=%d&cellid=%d&key=" + key; + } + + protected void getLocation(int mcc, int mnc, long lac, long cid, final LocationProviderCallback callback) { + String x = String.format(url, mcc, mnc, lac, cid); + Context.getAsyncHttpClient().prepareGet(String.format(url, mcc, mnc, lac, cid)) + .execute(new AsyncCompletionHandler() { + @Override + public Object onCompleted(Response response) throws Exception { + try (JsonReader reader = Json.createReader(response.getResponseBodyAsStream())) { + JsonObject json = reader.readObject(); + if (json.containsKey("lat") && json.containsKey("lon")) { + callback.onSuccess( + json.getJsonNumber("lat").doubleValue(), + json.getJsonNumber("lon").doubleValue()); + } else { + callback.onFailure(); + } + } + return null; + } + + @Override + public void onThrowable(Throwable t) { + callback.onFailure(); + } + }); } } diff --git a/test/org/traccar/location/LocationProviderTest.java b/test/org/traccar/location/LocationProviderTest.java index c4aedbb09..b7280cfea 100644 --- a/test/org/traccar/location/LocationProviderTest.java +++ b/test/org/traccar/location/LocationProviderTest.java @@ -24,20 +24,25 @@ public class LocationProviderTest { } public void testOpenCellId() { - OpenCellIdLocationProvider locationProvider = new OpenCellIdLocationProvider(); + OpenCellIdLocationProvider locationProvider = new OpenCellIdLocationProvider("fake"); Map attributes = new HashMap<>(); - attributes.put(Event.KEY_MCC, 250); + attributes.put(Event.KEY_MCC, 260); attributes.put(Event.KEY_MNC, 2); - attributes.put(Event.KEY_LAC, 4711); - attributes.put(Event.KEY_CID, 7989334); + attributes.put(Event.KEY_LAC, 10250); + attributes.put(Event.KEY_CID, 26511); locationProvider.getLocation(attributes, new LocationProvider.LocationProviderCallback() { @Override - public void onResult(double latitude, double longitude) { + public void onSuccess(double latitude, double longitude) { Assert.assertEquals(60.07254, latitude, 0.00001); Assert.assertEquals(30.30996, longitude, 0.00001); } + + @Override + public void onFailure() { + Assert.fail(); + } }); } -- cgit v1.2.3 From 3990d779545c7dba72324182058ae20730311f97 Mon Sep 17 00:00:00 2001 From: Anton Tananaev Date: Wed, 18 Nov 2015 15:32:05 +1300 Subject: Implement location provider channel handler --- debug.xml | 3 ++ src/org/traccar/BasePipelineFactory.java | 9 ++++ src/org/traccar/Context.java | 13 ++++++ src/org/traccar/LocationProviderHandler.java | 68 ++++++++++++++++++++++++++++ 4 files changed, 93 insertions(+) create mode 100644 src/org/traccar/LocationProviderHandler.java (limited to 'src') diff --git a/debug.xml b/debug.xml index b09a73d0a..2e6e4e12a 100644 --- a/debug.xml +++ b/debug.xml @@ -18,6 +18,9 @@ nominatim http://nominatim.openstreetmap.org/reverse + false + fake + true