diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/org/traccar/protocol/CastelProtocolDecoder.java | 24 | ||||
-rw-r--r-- | src/org/traccar/protocol/GatorProtocolDecoder.java | 6 | ||||
-rw-r--r-- | src/org/traccar/protocol/V680ProtocolDecoder.java | 131 | ||||
-rw-r--r-- | src/org/traccar/web/BaseServlet.java | 7 | ||||
-rw-r--r-- | src/org/traccar/web/JsonConverter.java | 49 |
5 files changed, 96 insertions, 121 deletions
diff --git a/src/org/traccar/protocol/CastelProtocolDecoder.java b/src/org/traccar/protocol/CastelProtocolDecoder.java index dcfa2868b..fe8d0a525 100644 --- a/src/org/traccar/protocol/CastelProtocolDecoder.java +++ b/src/org/traccar/protocol/CastelProtocolDecoder.java @@ -67,8 +67,14 @@ public class CastelProtocolDecoder extends BaseProtocolDecoder { position.setCourse(buf.readUnsignedShort() % 360); int flags = buf.readUnsignedByte(); - position.setLatitude((flags & 0x02) == 0 ? -lat : lat); - position.setLongitude((flags & 0x01) == 0 ? -lon : lon); + if ((flags & 0x02) == 0) { + lat = -lat; + } + if ((flags & 0x01) == 0) { + lon = -lon; + } + position.setLatitude(lat); + position.setLongitude(lon); position.setValid((flags & 0x0C) > 0); position.set(Event.KEY_SATELLITES, flags >> 4); } @@ -95,17 +101,14 @@ public class CastelProtocolDecoder extends BaseProtocolDecoder { response.writeByte(version); response.writeBytes(id); response.writeShort(ChannelBuffers.swapShort(MSG_HEARTBEAT_RESPONSE)); - response.writeShort(Checksum.crc16(Checksum.CRC16_X25, response.toByteBuffer(0, response.writerIndex()))); + response.writeShort( + Checksum.crc16(Checksum.CRC16_X25, response.toByteBuffer(0, response.writerIndex()))); response.writeByte(0x0D); response.writeByte(0x0A); channel.write(response, remoteAddress); } - } else if ( - type == MSG_LOGIN || - type == MSG_LOGOUT || - type == MSG_GPS || - type == MSG_ALARM || - type == MSG_CURRENT_LOCATION) { + } else if (type == MSG_LOGIN || type == MSG_LOGOUT || type == MSG_GPS + || type == MSG_ALARM || type == MSG_CURRENT_LOCATION) { if (!identify(id.toString(Charset.defaultCharset()).trim(), channel, remoteAddress)) { @@ -122,7 +125,8 @@ public class CastelProtocolDecoder extends BaseProtocolDecoder { response.writeInt(0xFFFFFFFF); response.writeShort(0); response.writeInt((int) (System.currentTimeMillis() / 1000)); - response.writeShort(Checksum.crc16(Checksum.CRC16_X25, response.toByteBuffer(0, response.writerIndex()))); + response.writeShort( + Checksum.crc16(Checksum.CRC16_X25, response.toByteBuffer(0, response.writerIndex()))); response.writeByte(0x0D); response.writeByte(0x0A); channel.write(response, remoteAddress); diff --git a/src/org/traccar/protocol/GatorProtocolDecoder.java b/src/org/traccar/protocol/GatorProtocolDecoder.java index 2dfc2e56a..b8431003b 100644 --- a/src/org/traccar/protocol/GatorProtocolDecoder.java +++ b/src/org/traccar/protocol/GatorProtocolDecoder.java @@ -63,10 +63,8 @@ public class GatorProtocolDecoder extends BaseProtocolDecoder { buf.readUnsignedByte(), buf.readUnsignedByte()); id = id.replaceFirst("^0+(?!$)", ""); - if (type == MSG_POSITION_DATA || - type == MSG_ROLLCALL_RESPONSE || - type == MSG_ALARM_DATA || - type == MSG_BLIND_AREA) { + if (type == MSG_POSITION_DATA || type == MSG_ROLLCALL_RESPONSE + || type == MSG_ALARM_DATA || type == MSG_BLIND_AREA) { // Create new position Position position = new Position(); diff --git a/src/org/traccar/protocol/V680ProtocolDecoder.java b/src/org/traccar/protocol/V680ProtocolDecoder.java index 7628bed88..dc0dcd2f6 100644 --- a/src/org/traccar/protocol/V680ProtocolDecoder.java +++ b/src/org/traccar/protocol/V680ProtocolDecoder.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,24 +31,27 @@ public class V680ProtocolDecoder extends BaseProtocolDecoder { super(protocol); } - private static final Pattern PATTERN = Pattern.compile( - "(?:#(\\d+)#" + // IMEI - "([^#]*)#)?" + // User - "(\\d+)#" + // Fix - "([^#]+)#" + // Password - "([^#]+)#" + // Event - "(\\d+)#" + // Packet number - "([^#]+)?#?" + // GSM base station - "(?:[^#]+#)?" + - "(\\d+)?(\\d{2}\\.\\d+)," + // Longitude (DDDMM.MMMM) - "([EW])," + - "(\\d+)?(\\d{2}\\.\\d+)," + // Latitude (DDMM.MMMM) - "([NS])," + - "(\\d+\\.\\d+)," + // Speed - "(\\d+\\.?\\d*)?#" + // Course - "(\\d{2})(\\d{2})(\\d{2})#" + // Date (DDMMYY) - "(\\d{2})(\\d{2})(\\d{2})" + // Time (HHMMSS) - ".*"); + private static final Pattern PATTERN = new PatternBuilder() + .groupBegin() + .num("#(d+)#") // imei + .xpr("([^#]*)#") // user + .groupEnd(true) + .num("(d+)#") // fix + .xpr("([^#]+)#") // password + .xpr("([^#]+)#") // event + .num("(d+)#") // packet number + .xpr("([^#]+)?#?") // gsm base station + .xpr("(?:[^#]+#)?") + .num("(d+)?(dd.d+),") // longitude + .xpr("([EW]),") + .num("(d+)?(dd.d+),") // latitude + .xpr("([NS]),") + .num("(d+.d+),") // speed + .num("(d+.?d*)?#") // course + .num("(dd)(dd)(dd)#") // date + .num("(dd)(dd)(dd)") // time + .any() + .compile(); @Override protected Object decode( @@ -58,89 +61,51 @@ public class V680ProtocolDecoder extends BaseProtocolDecoder { String sentence = (String) msg; sentence = sentence.trim(); - // Detect device ID if (sentence.length() == 16) { - String imei = sentence.substring(1, sentence.length()); - identify(imei, channel); + + identify(sentence.substring(1, sentence.length()), channel); + } else { - // Parse message - Matcher parser = PATTERN.matcher(sentence); + Parser parser = new Parser(PATTERN, sentence); if (!parser.matches()) { return null; } - // Create new position Position position = new Position(); position.setProtocol(getProtocolName()); - Integer index = 1; - // Get device by IMEI - String imei = parser.group(index++); - if (imei != null) { - identify(imei, channel); + if (parser.hasNext()) { + identify(parser.next(), channel); } if (!hasDeviceId()) { return null; } position.setDeviceId(getDeviceId()); - // User - position.set("user", parser.group(index++)); - - // Validity - position.setValid(Integer.parseInt(parser.group(index++)) > 0); - - // Password - position.set("password", parser.group(index++)); - - // Event - position.set(Event.KEY_EVENT, parser.group(index++)); - - // Packet number - position.set("packet", parser.group(index++)); + position.set("user", parser.next()); + position.setValid(parser.nextInt() > 0); + position.set("password", parser.next()); + position.set(Event.KEY_EVENT, parser.next()); + position.set("packet", parser.next()); + position.set(Event.KEY_GSM, parser.next()); - // GSM base station - position.set(Event.KEY_GSM, parser.group(index++)); + position.setLongitude(parser.nextCoordinate()); + position.setLatitude(parser.nextCoordinate()); + position.setSpeed(parser.nextDouble()); + position.setCourse(parser.nextDouble()); - // Longitude - String lon = parser.group(index++); - Double longitude = (lon != null) ? Double.parseDouble(lon) : 0.0; - longitude += Double.parseDouble(parser.group(index++)) / 60; - if (parser.group(index++).compareTo("W") == 0) longitude = -longitude; - position.setLongitude(longitude); - - // Latitude - String lat = parser.group(index++); - Double latitude = (lat != null) ? Double.parseDouble(lat) : 0.0; - latitude += Double.parseDouble(parser.group(index++)) / 60; - if (parser.group(index++).compareTo("S") == 0) latitude = -latitude; - position.setLatitude(latitude); - - // Speed and Course - position.setSpeed(Double.parseDouble(parser.group(index++))); - String course = parser.group(index++); - if (course != null) { - position.setCourse(Double.parseDouble(course)); - } - - // Date - Calendar time = Calendar.getInstance(TimeZone.getTimeZone("UTC")); - time.clear(); - int day = Integer.parseInt(parser.group(index++)); - int month = Integer.parseInt(parser.group(index++)); + int day = parser.nextInt(); + int month = parser.nextInt(); if (day == 0 && month == 0) { return null; // invalid date } - time.set(Calendar.DAY_OF_MONTH, day); - time.set(Calendar.MONTH, month - 1); - time.set(Calendar.YEAR, 2000 + Integer.parseInt(parser.group(index++))); - - // Time - 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()); + + DateBuilder dateBuilder = new DateBuilder() + .setDate(parser.nextInt(), month, day) + .setTime(parser.nextInt(), parser.nextInt(), parser.nextInt()); + position.setTime(dateBuilder.getDate()); + return position; } diff --git a/src/org/traccar/web/BaseServlet.java b/src/org/traccar/web/BaseServlet.java index 6e06e95ba..cfdff40d3 100644 --- a/src/org/traccar/web/BaseServlet.java +++ b/src/org/traccar/web/BaseServlet.java @@ -33,7 +33,9 @@ public abstract class BaseServlet extends HttpServlet { public static final String USER_KEY = "user"; @Override - protected final void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { + protected final void service( + HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { + String command = req.getPathInfo(); if (command == null) { command = ""; @@ -52,7 +54,8 @@ public abstract class BaseServlet extends HttpServlet { } } - protected abstract boolean handle(String command, HttpServletRequest req, HttpServletResponse resp) throws Exception; + protected abstract boolean handle( + String command, HttpServletRequest req, HttpServletResponse resp) throws Exception; public long getUserId(HttpServletRequest req) { Long userId = (Long) req.getSession().getAttribute(USER_KEY); diff --git a/src/org/traccar/web/JsonConverter.java b/src/org/traccar/web/JsonConverter.java index d8681cc3a..b19894dd6 100644 --- a/src/org/traccar/web/JsonConverter.java +++ b/src/org/traccar/web/JsonConverter.java @@ -38,12 +38,15 @@ import org.traccar.helper.Log; import org.traccar.model.Factory; import org.traccar.model.MiscFormatter; -public class JsonConverter { +public final class JsonConverter { - private static final DateTimeFormatter dateFormat = ISODateTimeFormat.dateTime(); + private JsonConverter() { + } + + private static final DateTimeFormatter DATE_FORMAT = ISODateTimeFormat.dateTime(); public static Date parseDate(String value) { - return dateFormat.parseDateTime(value).toDate(); + return DATE_FORMAT.parseDateTime(value).toDate(); } public static <T extends Factory> T objectFromJson(Reader reader, T prototype) throws ParseException { @@ -63,26 +66,28 @@ public class JsonConverter { final String name = Introspector.decapitalize(method.getName().substring(3)); Class<?> parameterType = method.getParameterTypes()[0]; - if (json.containsKey(name)) try { - if (parameterType.equals(boolean.class)) { - method.invoke(object, json.getBoolean(name)); - } else if (parameterType.equals(int.class)) { - method.invoke(object, json.getJsonNumber(name).intValue()); - } else if (parameterType.equals(long.class)) { - if (json.get(name).getValueType() == JsonValue.ValueType.NUMBER) { - method.invoke(object, json.getJsonNumber(name).longValue()); + if (json.containsKey(name)) { + try { + if (parameterType.equals(boolean.class)) { + method.invoke(object, json.getBoolean(name)); + } else if (parameterType.equals(int.class)) { + method.invoke(object, json.getJsonNumber(name).intValue()); + } else if (parameterType.equals(long.class)) { + if (json.get(name).getValueType() == JsonValue.ValueType.NUMBER) { + method.invoke(object, json.getJsonNumber(name).longValue()); + } + } else if (parameterType.equals(double.class)) { + method.invoke(object, json.getJsonNumber(name).doubleValue()); + } else if (parameterType.equals(String.class)) { + method.invoke(object, json.getString(name)); + } else if (parameterType.equals(Date.class)) { + method.invoke(object, DATE_FORMAT.parseDateTime(json.getString(name)).toDate()); + } else if (parameterType.equals(Map.class)) { + method.invoke(object, MiscFormatter.fromJson(json.getJsonObject(name))); } - } else if (parameterType.equals(double.class)) { - method.invoke(object, json.getJsonNumber(name).doubleValue()); - } else if (parameterType.equals(String.class)) { - method.invoke(object, json.getString(name)); - } else if (parameterType.equals(Date.class)) { - method.invoke(object, dateFormat.parseDateTime(json.getString(name)).toDate()); - } else if (parameterType.equals(Map.class)) { - method.invoke(object, MiscFormatter.fromJson(json.getJsonObject(name))); + } catch (IllegalAccessException | InvocationTargetException error) { + Log.warning(error); } - } catch (IllegalAccessException | InvocationTargetException error) { - Log.warning(error); } } } @@ -119,7 +124,7 @@ public class JsonConverter { } else if (method.getReturnType().equals(Date.class)) { Date value = (Date) method.invoke(object); if (value != null) { - json.add(name, dateFormat.print(new DateTime(value))); + json.add(name, DATE_FORMAT.print(new DateTime(value))); } } else if (method.getReturnType().equals(Map.class)) { json.add(name, MiscFormatter.toJson((Map) method.invoke(object))); |