From a6b06189ea0f1b5a68c9cab49d50eccd98328bfa Mon Sep 17 00:00:00 2001 From: Anton Tananaev Date: Fri, 16 Oct 2015 09:44:03 +1300 Subject: Use UDP protocol for GPSMTA --- src/org/traccar/helper/Parser.java | 8 +++ src/org/traccar/protocol/GpsmtaProtocol.java | 4 +- .../traccar/protocol/GpsmtaProtocolDecoder.java | 65 +++++++++++----------- 3 files changed, 42 insertions(+), 35 deletions(-) (limited to 'src/org') diff --git a/src/org/traccar/helper/Parser.java b/src/org/traccar/helper/Parser.java index 2fd615ca5..107ae7bff 100644 --- a/src/org/traccar/helper/Parser.java +++ b/src/org/traccar/helper/Parser.java @@ -62,6 +62,14 @@ public class Parser { } } + public long nextLong() { + if (hasNext()) { + return Long.parseLong(next()); + } else { + return 0; + } + } + public double nextDouble() { if (hasNext()) { return Double.parseDouble(next()); diff --git a/src/org/traccar/protocol/GpsmtaProtocol.java b/src/org/traccar/protocol/GpsmtaProtocol.java index 28974855d..2c4c1f218 100644 --- a/src/org/traccar/protocol/GpsmtaProtocol.java +++ b/src/org/traccar/protocol/GpsmtaProtocol.java @@ -15,7 +15,7 @@ */ package org.traccar.protocol; -import org.jboss.netty.bootstrap.ServerBootstrap; +import org.jboss.netty.bootstrap.ConnectionlessBootstrap; import org.jboss.netty.channel.ChannelPipeline; import org.jboss.netty.handler.codec.string.StringDecoder; import org.traccar.BaseProtocol; @@ -31,7 +31,7 @@ public class GpsmtaProtocol extends BaseProtocol { @Override public void initTrackerServers(List serverList) { - serverList.add(new TrackerServer(new ServerBootstrap(), this.getName()) { + serverList.add(new TrackerServer(new ConnectionlessBootstrap(), this.getName()) { @Override protected void addSpecificHandlers(ChannelPipeline pipeline) { pipeline.addLast("stringDecoder", new StringDecoder()); diff --git a/src/org/traccar/protocol/GpsmtaProtocolDecoder.java b/src/org/traccar/protocol/GpsmtaProtocolDecoder.java index 134dc6496..7aec6901c 100644 --- a/src/org/traccar/protocol/GpsmtaProtocolDecoder.java +++ b/src/org/traccar/protocol/GpsmtaProtocolDecoder.java @@ -21,6 +21,8 @@ import java.util.regex.Matcher; import java.util.regex.Pattern; import org.jboss.netty.channel.Channel; import org.traccar.BaseProtocolDecoder; +import org.traccar.helper.Parser; +import org.traccar.helper.PatternBuilder; import org.traccar.model.Event; import org.traccar.model.Position; @@ -30,28 +32,27 @@ public class GpsmtaProtocolDecoder extends BaseProtocolDecoder { super(protocol); } - private static final Pattern PATTERN = Pattern.compile( - "(\\d+) " + // UID - "(\\d+) " + // Time - "(\\d+\\.\\d+) " + // Latitude - "(\\d+\\.\\d+) " + // Longitude - "(\\d+) " + // Speed - "(\\d+) " + // Course - "(\\d+) " + // Accuracy - "(\\d+) " + // Altitude - "(\\d+) " + // Flags - "(\\d+) " + // Battery - "(\\d+) " + // Temperature - "(\\d).*"); // Changing status + private static final Pattern PATTERN = new PatternBuilder() + .num("(d+) ") // uid + .num("(d+) ") // time + .num("(d+.d+) ") // latitude + .num("(d+.d+) ") // longitude + .num("(d+) ") // speed + .num("(d+) ") // course + .num("(d+) ") // accuracy + .num("(d+) ") // altitude + .num("(d+) ") // flags + .num("(d+) ") // battery + .num("(d+) ") // temperature + .num("(d)") // changing status + .any() + .compile(); @Override protected Object decode( - Channel channel, SocketAddress remoteAddress, Object msg) - throws Exception { + Channel channel, SocketAddress remoteAddress, Object msg) throws Exception { - String sentence = (String) msg; - - Matcher parser = PATTERN.matcher(sentence); + Parser parser = new Parser(PATTERN, (String) msg); if (!parser.matches()) { return null; } @@ -59,27 +60,25 @@ public class GpsmtaProtocolDecoder extends BaseProtocolDecoder { Position position = new Position(); position.setProtocol(getProtocolName()); - Integer index = 1; - - if (!identify(parser.group(index++), channel, remoteAddress)) { + if (!identify(parser.next(), channel, remoteAddress)) { return null; } position.setDeviceId(getDeviceId()); - String time = parser.group(index++); + String time = parser.next(); position.setTime(new Date(Long.parseLong(time) * 1000)); - position.setLatitude(Double.parseDouble(parser.group(index++))); - position.setLongitude(Double.parseDouble(parser.group(index++))); - position.setSpeed(Integer.parseInt(parser.group(index++))); - position.setCourse(Integer.parseInt(parser.group(index++))); - index++; // accuracy - position.setAltitude(Integer.parseInt(parser.group(index++))); - - position.set(Event.KEY_STATUS, Integer.parseInt(parser.group(index++))); - position.set(Event.KEY_BATTERY, Integer.parseInt(parser.group(index++))); - position.set(Event.PREFIX_TEMP + 1, Integer.parseInt(parser.group(index++))); - position.set(Event.KEY_CHARGE, Integer.parseInt(parser.group(index++)) == 1); + position.setLatitude(parser.nextDouble()); + position.setLongitude(parser.nextDouble()); + position.setSpeed(parser.nextInt()); + position.setCourse(parser.nextInt()); + parser.next(); + position.setAltitude(parser.nextInt()); + + position.set(Event.KEY_STATUS, parser.nextInt()); + position.set(Event.KEY_BATTERY, parser.nextInt()); + position.set(Event.PREFIX_TEMP + 1, parser.nextInt()); + position.set(Event.KEY_CHARGE, parser.nextInt() == 1); if (channel != null) { channel.write(time, remoteAddress); -- cgit v1.2.3