diff options
-rw-r--r-- | src/org/traccar/protocol/GpsMarkerProtocolDecoder.java | 4 | ||||
-rw-r--r-- | src/org/traccar/protocol/HaicomProtocolDecoder.java | 138 | ||||
-rw-r--r-- | test/org/traccar/protocol/HaicomProtocolDecoderTest.java | 3 |
3 files changed, 66 insertions, 79 deletions
diff --git a/src/org/traccar/protocol/GpsMarkerProtocolDecoder.java b/src/org/traccar/protocol/GpsMarkerProtocolDecoder.java index e0f8d529e..9313ad099 100644 --- a/src/org/traccar/protocol/GpsMarkerProtocolDecoder.java +++ b/src/org/traccar/protocol/GpsMarkerProtocolDecoder.java @@ -38,9 +38,9 @@ public class GpsMarkerProtocolDecoder extends BaseProtocolDecoder { .number("(d{15})") // imei .number("T(dd)(dd)(dd)") // date .number("(dd)(dd)(dd)?") // time - .number("([NS])") + .expression("([NS])") .number("(dd)(dd)(dddd)") // latitude - .number("([EW])") + .expression("([EW])") .number("(ddd)(dd)(dddd)") // longitude .number("(ddd)") // speed .number("(ddd)") // course diff --git a/src/org/traccar/protocol/HaicomProtocolDecoder.java b/src/org/traccar/protocol/HaicomProtocolDecoder.java index 42d806728..d187a4a63 100644 --- a/src/org/traccar/protocol/HaicomProtocolDecoder.java +++ b/src/org/traccar/protocol/HaicomProtocolDecoder.java @@ -1,5 +1,5 @@ /* - * Copyright 2014 Anton Tananaev (anton.tananaev@gmail.com) + * Copyright 2014 - 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. @@ -16,12 +16,13 @@ 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.BitUtil; +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,93 +32,78 @@ public class HaicomProtocolDecoder extends BaseProtocolDecoder { super(protocol); } - private static final Pattern PATTERN = Pattern.compile( - "\\$GPRS" + - "(\\d+)," + // IMEI - "([^,]+)," + // Version - "(\\d{2})(\\d{2})(\\d{2})," + // Date - "(\\d{2})(\\d{2})(\\d{2})," + // Time - "(\\d)" + // Flags - "(\\d{2})(\\d{5})" + // Latitude (DDMMMMM) - "(\\d{3})(\\d{5})," + // Longitude (DDDMMMMM) - "(\\d+)," + // Speed - "(\\d+)," + // Course - "(\\d+)," + // Status - "(\\d+)?," + // GPRS counting value - "(\\d+)?," + // GPS power saving counting value - "(\\d+)," + // Switch status - "(\\d+)" + // Relay status - "(?:[LH]{2})?" + // Power status - "#V(\\d+).*"); // Battery + private static final Pattern PATTERN = new PatternBuilder() + .text("$GPRS") + .number("(d+),") // IMEI + .expression("([^,]+),") // Version + .number("(dd)(dd)(dd),") // Date + .number("(dd)(dd)(dd),") // Time + .number("(d)") // Flags + .number("(dd)(d{5})") // Latitude (DDMMMMM) + .number("(ddd)(d{5}),") // Longitude (DDDMMMMM) + .number("(d+),") // Speed + .number("(d+),") // Course + .number("(d+),") // Status + .number("(d+)?,") // GPRS counting value + .number("(d+)?,") // GPS power saving counting value + .number("(d+),") // Switch status + .number("(d+)") // Relay status + .expression("(?:[LH]{2})?") // Power status + .number("#V(d+)") // Battery + .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()); - // Firmware version - position.set(Event.KEY_VERSION, 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 - int flags = Integer.parseInt(parser.group(index++)); - position.setValid((flags & 0x1) != 0); - - // Latitude - Double latitude = Double.parseDouble(parser.group(index++)); - latitude += Double.parseDouble(parser.group(index++)) / 60000; - if ((flags & 0x4) == 0) latitude = -latitude; - position.setLatitude(latitude); - - // Longitude - Double longitude = Double.parseDouble(parser.group(index++)); - longitude += Double.parseDouble(parser.group(index++)) / 60000; - if ((flags & 0x2) == 0) longitude = -longitude; - position.setLongitude(longitude); - - // Speed - position.setSpeed(Double.parseDouble(parser.group(index++)) / 10); - - // Course - position.setCourse(Double.parseDouble(parser.group(index++)) / 10); - - // Additional data - position.set(Event.KEY_STATUS, parser.group(index++)); - position.set(Event.KEY_GSM, parser.group(index++)); - position.set(Event.KEY_GPS, parser.group(index++)); - position.set(Event.KEY_INPUT, parser.group(index++)); - position.set(Event.KEY_OUTPUT, parser.group(index++)); - position.set(Event.KEY_BATTERY, Double.parseDouble(parser.group(index++)) / 10); + position.set(Event.KEY_VERSION, parser.next()); + + DateBuilder dateBuilder = new DateBuilder() + .setDate(parser.nextInt(), parser.nextInt(), parser.nextInt()) + .setTime(parser.nextInt(), parser.nextInt(), parser.nextInt()); + position.setTime(dateBuilder.getDate()); + + int flags = parser.nextInt(); + + position.setValid(BitUtil.check(flags, 0)); + + double latitude = parser.nextDouble() + parser.nextDouble() / 60000; + if (BitUtil.check(flags, 2)) { + position.setLatitude(latitude); + } else { + position.setLatitude(-latitude); + } + + double longitude = parser.nextDouble() + parser.nextDouble() / 60000; + if (BitUtil.check(flags, 1)) { + position.setLongitude(longitude); + } else { + position.setLongitude(-longitude); + } + + position.setSpeed(parser.nextDouble() / 10); + position.setCourse(parser.nextDouble() / 10); + + position.set(Event.KEY_STATUS, parser.next()); + position.set(Event.KEY_GSM, parser.next()); + position.set(Event.KEY_GPS, parser.next()); + position.set(Event.KEY_INPUT, parser.next()); + position.set(Event.KEY_OUTPUT, parser.next()); + position.set(Event.KEY_BATTERY, parser.nextDouble() / 10); return position; } diff --git a/test/org/traccar/protocol/HaicomProtocolDecoderTest.java b/test/org/traccar/protocol/HaicomProtocolDecoderTest.java index df1c33a06..f8a35499a 100644 --- a/test/org/traccar/protocol/HaicomProtocolDecoderTest.java +++ b/test/org/traccar/protocol/HaicomProtocolDecoderTest.java @@ -11,7 +11,8 @@ public class HaicomProtocolDecoderTest extends ProtocolDecoderTest { HaicomProtocolDecoder decoder = new HaicomProtocolDecoder(new HaicomProtocol()); verifyPosition(decoder, text( - "$GPRS012497007097169,T100001,150618,230031,5402267400332464,0004,2014,000001,,,1,00#V040*")); + "$GPRS012497007097169,T100001,150618,230031,5402267400332464,0004,2014,000001,,,1,00#V040*"), + position("2015-06-18 23:00:31.000", true, 40.37790, -3.54107)); verifyPosition(decoder, text( "$GPRS123456789012345,602S19A,100915,063515,7240649312041079,0019,3156,111000,10004,0000,11111,00LH#V037")); |