diff options
Diffstat (limited to 'src/org/traccar/protocol/MegastekProtocolDecoder.java')
-rw-r--r-- | src/org/traccar/protocol/MegastekProtocolDecoder.java | 119 |
1 files changed, 61 insertions, 58 deletions
diff --git a/src/org/traccar/protocol/MegastekProtocolDecoder.java b/src/org/traccar/protocol/MegastekProtocolDecoder.java index 019bb69de..c07e73533 100644 --- a/src/org/traccar/protocol/MegastekProtocolDecoder.java +++ b/src/org/traccar/protocol/MegastekProtocolDecoder.java @@ -20,9 +20,7 @@ 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.model.Event; import org.traccar.model.Position; @@ -44,7 +42,7 @@ public class MegastekProtocolDecoder extends BaseProtocolDecoder { "(\\d+\\.\\d+)?," + // Speed "(\\d+\\.\\d+)?," + // Course "(\\d{2})(\\d{2})(\\d{2})" + // Date (DDMMYY) - "[^\\*]+\\*[0-9a-fA-F]{2}"); // Checksum + ".*"); // Checksum private static final Pattern patternSimple = Pattern.compile( "[FL]," + // Flag @@ -143,10 +141,18 @@ public class MegastekProtocolDecoder extends BaseProtocolDecoder { id = sentence.substring(beginIndex, endIndex); beginIndex = endIndex + 1; - endIndex = sentence.indexOf('*', beginIndex) + 3; + endIndex = sentence.indexOf('*', beginIndex); + if (endIndex != -1) { + endIndex += 3; + } else { + endIndex = sentence.length(); + } gprmc = sentence.substring(beginIndex, endIndex); beginIndex = endIndex + 1; + if (beginIndex > sentence.length()) { + beginIndex = endIndex; + } status = sentence.substring(beginIndex); } else { @@ -175,78 +181,75 @@ public class MegastekProtocolDecoder extends BaseProtocolDecoder { if (simple) { - // Parse status Matcher parser = patternSimple.matcher(status); - if (!parser.matches()) { - return null; - } + if (parser.matches()) { - int index = 1; + int index = 1; - // Alarm - position.set(Event.KEY_ALARM, parser.group(index++)); + position.set(Event.KEY_ALARM, parser.group(index++)); - // IMEI - if (!identify(parser.group(index++), channel, null, false)) { - if (!identify(id, channel)) { - return null; + // IMEI + if (!identify(parser.group(index++), channel, null, false)) { + if (!identify(id, channel)) { + return null; + } } - } - position.setDeviceId(getDeviceId()); + position.setDeviceId(getDeviceId()); - // Satellites - position.set(Event.KEY_SATELLITES, parser.group(index++)); + position.set(Event.KEY_SATELLITES, parser.group(index++)); - // Altitude - String altitude = parser.group(index++); - if (altitude != null) { - position.setAltitude(Double.valueOf(altitude)); - } + String altitude = parser.group(index++); + if (altitude != null) { + position.setAltitude(Double.valueOf(altitude)); + } - // Battery - position.set(Event.KEY_POWER, Double.valueOf(parser.group(index++))); + position.set(Event.KEY_POWER, Double.valueOf(parser.group(index++))); - // Charger - String charger = parser.group(index++); - if (charger != null) { - position.set(Event.KEY_CHARGE, Integer.valueOf(charger) == 1); - } + String charger = parser.group(index++); + if (charger != null) { + position.set(Event.KEY_CHARGE, Integer.valueOf(charger) == 1); + } - position.set(Event.KEY_MCC, parser.group(index++)); - position.set(Event.KEY_MNC, parser.group(index++)); - position.set(Event.KEY_LAC, parser.group(index++)); + position.set(Event.KEY_MCC, parser.group(index++)); + position.set(Event.KEY_MNC, parser.group(index++)); + position.set(Event.KEY_LAC, parser.group(index++)); + + } else { + + if (!identify(id, channel)) { + return null; + } + position.setDeviceId(getDeviceId()); + + } } else { - // Parse status Matcher parser = patternAlternative.matcher(status); if (!parser.matches()) { - return null; - } - int index = 1; + int index = 1; - if (!identify(id, channel)) { - return null; + if (!identify(id, channel)) { + return null; + } + position.setDeviceId(getDeviceId()); + + position.set(Event.KEY_MCC, parser.group(index++)); + position.set(Event.KEY_MNC, parser.group(index++)); + position.set(Event.KEY_LAC, parser.group(index++)); + position.set(Event.KEY_GSM, parser.group(index++)); + + position.set(Event.KEY_BATTERY, Double.valueOf(parser.group(index++))); + + position.set(Event.KEY_FLAGS, parser.group(index++)); + position.set(Event.KEY_INPUT, parser.group(index++)); + position.set(Event.KEY_OUTPUT, parser.group(index++)); + position.set(Event.PREFIX_ADC + 1, parser.group(index++)); + position.set(Event.PREFIX_ADC + 2, parser.group(index++)); + position.set(Event.PREFIX_ADC + 3, parser.group(index++)); + position.set(Event.KEY_ALARM, parser.group(index++)); } - position.setDeviceId(getDeviceId()); - - position.set(Event.KEY_MCC, parser.group(index++)); - position.set(Event.KEY_MNC, parser.group(index++)); - position.set(Event.KEY_LAC, parser.group(index++)); - position.set(Event.KEY_GSM, parser.group(index++)); - - // Battery - position.set(Event.KEY_BATTERY, Double.valueOf(parser.group(index++))); - - position.set(Event.KEY_FLAGS, parser.group(index++)); - position.set(Event.KEY_INPUT, parser.group(index++)); - position.set(Event.KEY_OUTPUT, parser.group(index++)); - position.set(Event.PREFIX_ADC + 1, parser.group(index++)); - position.set(Event.PREFIX_ADC + 2, parser.group(index++)); - position.set(Event.PREFIX_ADC + 3, parser.group(index++)); - position.set(Event.KEY_ALARM, parser.group(index++)); - } return position; |