diff options
Diffstat (limited to 'src/org')
-rw-r--r-- | src/org/traccar/helper/DateBuilder.java | 3 | ||||
-rw-r--r-- | src/org/traccar/helper/Parser.java | 70 | ||||
-rw-r--r-- | src/org/traccar/protocol/MeiligaoProtocolDecoder.java | 135 |
3 files changed, 113 insertions, 95 deletions
diff --git a/src/org/traccar/helper/DateBuilder.java b/src/org/traccar/helper/DateBuilder.java index 17936a329..6e3fbc460 100644 --- a/src/org/traccar/helper/DateBuilder.java +++ b/src/org/traccar/helper/DateBuilder.java @@ -59,10 +59,12 @@ public class DateBuilder { return setDate(year, month, day); } + @Deprecated public DateBuilder setDate(String year, String month, String day) { return setDate(Integer.parseInt(year), Integer.parseInt(month), Integer.parseInt(day)); } + @Deprecated public DateBuilder setDateReverse(String day, String month, String year) { return setDate(year, month, day); } @@ -91,6 +93,7 @@ public class DateBuilder { return setHour(hour).setMinute(minute).setSecond(second); } + @Deprecated public DateBuilder setTime(String hour, String minute, String second) { return setTime(Integer.parseInt(hour), Integer.parseInt(minute), Integer.parseInt(second)); } diff --git a/src/org/traccar/helper/Parser.java b/src/org/traccar/helper/Parser.java new file mode 100644 index 000000000..9b346d810 --- /dev/null +++ b/src/org/traccar/helper/Parser.java @@ -0,0 +1,70 @@ +/* + * 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.helper; + +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +public class Parser { + + private int position = 1; + private Matcher matcher; + + public Parser(Pattern pattern, String input) { + matcher = pattern.matcher(input); + } + + public boolean matches() { + return matcher.matches(); + } + + public boolean hasNext() { + if (matcher.group(position) != null) { + return true; + } else { + position++; + return false; + } + } + + public String next() { + return matcher.group(position++); + } + + public int nextInt() { + return Integer.parseInt(next()); + } + + public int nextInt(int radix) { + return Integer.parseInt(next(), radix); + } + + public double nextDouble() { + return Double.parseDouble(next()); + } + + // Format: (degrees)(minutes)(hemisphere) + public double nextCoordinate() { + double coordinate = nextDouble(); + coordinate += nextDouble() / 60; + String hemisphere = next(); + if (hemisphere.equals("S") || hemisphere.equals("W")) { + coordinate = -coordinate; + } + return coordinate; + } + +} diff --git a/src/org/traccar/protocol/MeiligaoProtocolDecoder.java b/src/org/traccar/protocol/MeiligaoProtocolDecoder.java index 6acdef406..67330cca4 100644 --- a/src/org/traccar/protocol/MeiligaoProtocolDecoder.java +++ b/src/org/traccar/protocol/MeiligaoProtocolDecoder.java @@ -28,6 +28,8 @@ import org.jboss.netty.channel.Channel; import org.traccar.BaseProtocolDecoder; import org.traccar.Context; import org.traccar.helper.Checksum; +import org.traccar.helper.DateBuilder; +import org.traccar.helper.Parser; import org.traccar.model.Event; import org.traccar.model.Position; @@ -208,130 +210,73 @@ public class MeiligaoProtocolDecoder extends BaseProtocolDecoder { } } - // Parse message - String sentence = buf.toString(buf.readerIndex(), buf.readableBytes() - 4, Charset.defaultCharset()); - Matcher parser = (command == MSG_RFID ? PATTERN_RFID : PATTERN).matcher(sentence); + Parser parser = new Parser(command == MSG_RFID ? PATTERN_RFID : PATTERN, + buf.toString(buf.readerIndex(), buf.readableBytes() - 4, Charset.defaultCharset())); if (!parser.matches()) { return null; } - Integer index = 1; if (command == MSG_RFID) { - // Time and date - Calendar time = Calendar.getInstance(TimeZone.getTimeZone("UTC")); - time.clear(); - 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++))); - time.set(Calendar.DAY_OF_MONTH, Integer.parseInt(parser.group(index++))); - time.set(Calendar.MONTH, Integer.parseInt(parser.group(index++)) - 1); - time.set(Calendar.YEAR, 2000 + Integer.parseInt(parser.group(index++))); - position.setTime(time.getTime()); - - // 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); - - // 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() + .setTime(parser.nextInt(), parser.nextInt(), parser.nextInt()) + .setDateReverse(parser.nextInt(), parser.nextInt(), parser.nextInt()); + position.setTime(dateBuilder.getDate()); + + position.setLatitude(parser.nextCoordinate()); + position.setLongitude(parser.nextCoordinate()); } else { - // Time - Calendar time = Calendar.getInstance(TimeZone.getTimeZone("UTC")); - time.clear(); - 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++))); - String mseconds = parser.group(index++); - if (mseconds != null) { - time.set(Calendar.MILLISECOND, Integer.parseInt(mseconds)); + DateBuilder dateBuilder = new DateBuilder() + .setTime(parser.nextInt(), parser.nextInt(), parser.nextInt()); + if (parser.hasNext()) { + dateBuilder.setMillis(parser.nextInt()); } - // 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); - - // 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); - - // Speed - String speed = parser.group(index++); - if (speed != null) { - position.setSpeed(Double.parseDouble(speed)); + position.setValid(parser.next().equals("A")); + position.setLatitude(parser.nextCoordinate()); + position.setLongitude(parser.nextCoordinate()); + + if (parser.hasNext()) { + position.setSpeed(parser.nextDouble()); } - // Course - String course = parser.group(index++); - if (course != null) { - position.setCourse(Double.parseDouble(course)); + if (parser.hasNext()) { + position.setCourse(parser.nextDouble()); } - // Date - time.set(Calendar.DAY_OF_MONTH, Integer.parseInt(parser.group(index++))); - time.set(Calendar.MONTH, Integer.parseInt(parser.group(index++)) - 1); - time.set(Calendar.YEAR, 2000 + Integer.parseInt(parser.group(index++))); - position.setTime(time.getTime()); + dateBuilder.setDateReverse(parser.nextInt(), parser.nextInt(), parser.nextInt()); + position.setTime(dateBuilder.getDate()); - // HDOP - position.set(Event.KEY_HDOP, parser.group(index++)); + position.set(Event.KEY_HDOP, parser.next()); - // Altitude - String altitude = parser.group(index++); - if (altitude != null) { - position.setAltitude(Double.parseDouble(altitude)); + if (parser.hasNext()) { + position.setAltitude(parser.nextDouble()); } - // State - String state = parser.group(index++); - if (state != null) { - position.set(Event.KEY_STATUS, state); - } + position.set(Event.KEY_STATUS, parser.next()); - // ADC for (int i = 1; i <= 8; i++) { - String adc = parser.group(index++); - if (adc != null) { - position.set(Event.PREFIX_ADC + i, Integer.parseInt(adc, 16)); + if (parser.hasNext()) { + position.set(Event.PREFIX_ADC + i, parser.nextInt(16)); } } - // Cell identifier - position.set(Event.KEY_CELL, parser.group(index++)); + position.set(Event.KEY_CELL, parser.next()); - // GSM signal - String gsm = parser.group(index++); - if (gsm != null) { - position.set(Event.KEY_GSM, Integer.parseInt(gsm, 16)); + if (parser.hasNext()) { + position.set(Event.KEY_GSM, parser.nextInt(16)); } - // Odometer - String odometer = parser.group(index++); - if (odometer == null) { - odometer = parser.group(index++); - } - if (odometer != null) { - position.set(Event.KEY_ODOMETER, Integer.parseInt(odometer, 16)); + if (parser.hasNext()) { + position.set(Event.KEY_ODOMETER, parser.nextInt(16)); + } else if (parser.hasNext()) { + position.set(Event.KEY_ODOMETER, parser.nextInt(16)); } - // RFID - String rfid = parser.group(index++); - if (rfid != null) { - position.set(Event.KEY_RFID, Integer.parseInt(rfid, 16)); + if (parser.hasNext()) { + position.set(Event.KEY_RFID, parser.nextInt(16)); } } |