diff options
Diffstat (limited to 'src/org/traccar/protocol/ManPowerProtocolDecoder.java')
-rw-r--r-- | src/org/traccar/protocol/ManPowerProtocolDecoder.java | 89 |
1 files changed, 31 insertions, 58 deletions
diff --git a/src/org/traccar/protocol/ManPowerProtocolDecoder.java b/src/org/traccar/protocol/ManPowerProtocolDecoder.java index 113215c1b..48c36bb6a 100644 --- a/src/org/traccar/protocol/ManPowerProtocolDecoder.java +++ b/src/org/traccar/protocol/ManPowerProtocolDecoder.java @@ -16,12 +16,12 @@ package org.traccar.protocol; import java.net.SocketAddress; -import java.util.Calendar; -import java.util.TimeZone; -import java.util.regex.Matcher; import java.util.regex.Pattern; import org.jboss.netty.channel.Channel; import org.traccar.BaseProtocolDecoder; +import org.traccar.helper.DateBuilder; +import org.traccar.helper.Parser; +import org.traccar.helper.PatternBuilder; import org.traccar.model.Event; import org.traccar.model.Position; @@ -31,78 +31,51 @@ public class ManPowerProtocolDecoder extends BaseProtocolDecoder { super(protocol); } - private static final Pattern PATTERN = Pattern.compile( - "simei:" + - "(\\d+)," + // IMEI - "[^,]*,[^,]*," + - "([^,]*)," + // Status - "\\d+,\\d+,\\d+\\.?\\d*," + - "(\\d{2})(\\d{2})(\\d{2})" + // Date - "(\\d{2})(\\d{2})(\\d{2})," + // Time - "([AV])," + // Validity - "(\\d{2})(\\d{2}\\.\\d{4})," + // Latitude (DDMM.MMMM) - "([NS])," + - "(\\d{3})(\\d{2}\\.\\d{4})," + // Longitude (DDDMM.MMMM) - "([EW])?," + - "(\\d+\\.?\\d*)," + // Speed - ".*"); + private static final Pattern PATTERN = new PatternBuilder() + .text("simei:") + .number("(d+),") // imei + .expression("[^,]*,[^,]*,") + .expression("([^,]*),") // status + .number("d+,d+,d+.?d*,") + .number("(dd)(dd)(dd)") // date + .number("(dd)(dd)(dd),") // time + .expression("([AV]),") // validity + .number("(dd)(dd.dddd),") // latitude + .expression("([NS]),") + .number("(ddd)(dd.dddd),") // longitude + .expression("([EW])?,") + .number("(d+.?d*),") // speed + .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; - - // Parse message - Matcher parser = PATTERN.matcher(sentence); + Parser parser = new Parser(PATTERN, (String) msg); if (!parser.matches()) { return null; } - // Create new position Position position = new Position(); position.setProtocol(getProtocolName()); - Integer index = 1; - - // Get device by IMEI - if (!identify(parser.group(index++), channel)) { + if (!identify(parser.next(), channel)) { return null; } position.setDeviceId(getDeviceId()); - // Alarm message - position.set(Event.KEY_STATUS, parser.group(index++)); - - // Date - Calendar time = Calendar.getInstance(TimeZone.getTimeZone("UTC")); - time.clear(); - time.set(Calendar.YEAR, 2000 + Integer.parseInt(parser.group(index++))); - time.set(Calendar.MONTH, Integer.parseInt(parser.group(index++)) - 1); - time.set(Calendar.DAY_OF_MONTH, Integer.parseInt(parser.group(index++))); - time.set(Calendar.HOUR_OF_DAY, Integer.parseInt(parser.group(index++))); - time.set(Calendar.MINUTE, Integer.parseInt(parser.group(index++))); - time.set(Calendar.SECOND, Integer.parseInt(parser.group(index++))); - position.setTime(time.getTime()); - - // Validity - position.setValid(parser.group(index++).compareTo("A") == 0); - - // Latitude - Double latitude = Double.parseDouble(parser.group(index++)); - latitude += Double.parseDouble(parser.group(index++)) / 60; - if (parser.group(index++).compareTo("S") == 0) latitude = -latitude; - position.setLatitude(latitude); + position.set(Event.KEY_STATUS, parser.next()); - // Longitude - Double longitude = Double.parseDouble(parser.group(index++)); - longitude += Double.parseDouble(parser.group(index++)) / 60; - if (parser.group(index++).compareTo("W") == 0) longitude = -longitude; - position.setLongitude(longitude); + DateBuilder dateBuilder = new DateBuilder() + .setDate(parser.nextInt(), parser.nextInt(), parser.nextInt()) + .setTime(parser.nextInt(), parser.nextInt(), parser.nextInt()); + position.setTime(dateBuilder.getDate()); - // Speed - position.setSpeed(Double.parseDouble(parser.group(index++))); + position.setValid(parser.next().equals("A")); + position.setLatitude(parser.nextCoordinate()); + position.setLongitude(parser.nextCoordinate()); + position.setSpeed(parser.nextDouble()); return position; } |