From c1855a39f96c9ed4c0263fdb69f6cdc0e65fcaed Mon Sep 17 00:00:00 2001 From: Anton Tananaev Date: Fri, 11 Dec 2015 13:25:29 +1300 Subject: Support custom attributes for OsmAnd protocol --- .../traccar/protocol/OsmAndProtocolDecoder.java | 107 ++++++++++----------- 1 file changed, 49 insertions(+), 58 deletions(-) (limited to 'src/org') diff --git a/src/org/traccar/protocol/OsmAndProtocolDecoder.java b/src/org/traccar/protocol/OsmAndProtocolDecoder.java index 2476a983f..aa941b7fc 100644 --- a/src/org/traccar/protocol/OsmAndProtocolDecoder.java +++ b/src/org/traccar/protocol/OsmAndProtocolDecoder.java @@ -56,67 +56,58 @@ public class OsmAndProtocolDecoder extends BaseProtocolDecoder { Position position = new Position(); position.setProtocol(getProtocolName()); - String id; - if (params.containsKey("id")) { - id = params.get("id").get(0); - } else { - id = params.get("deviceid").get(0); - } - if (!identify(id, channel, remoteAddress)) { - return null; - } - position.setDeviceId(getDeviceId()); - - position.setValid(true); - if (params.containsKey("timestamp")) { - try { - long timestamp = Long.parseLong(params.get("timestamp").get(0)); - if (timestamp < Integer.MAX_VALUE) { - timestamp *= 1000; - } - position.setTime(new Date(timestamp)); - } catch (NumberFormatException error) { - DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); - position.setTime(dateFormat.parse(params.get("timestamp").get(0))); + for (String key : params.keySet()) { + String value = params.get(key).get(0); + switch (key) { + case "id": + case "deviceid": + if (!identify(value, channel, remoteAddress)) { + return null; + } + position.setDeviceId(getDeviceId()); + break; + case "timestamp": + try { + long timestamp = Long.parseLong(value); + if (timestamp < Integer.MAX_VALUE) { + timestamp *= 1000; + } + position.setTime(new Date(timestamp)); + } catch (NumberFormatException error) { + DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); + position.setTime(dateFormat.parse(value)); + } + break; + case "lat": + position.setLatitude(Double.parseDouble(value)); + break; + case "lon": + position.setLongitude(Double.parseDouble(value)); + break; + case "speed": + position.setSpeed(Double.parseDouble(value)); + break; + case "bearing": + case "heading": + position.setCourse(Double.parseDouble(value)); + break; + case "altitude": + position.setAltitude(Double.parseDouble(value)); + break; + case "hdop": + position.set(Event.KEY_HDOP, Double.parseDouble(value)); + break; + case "batt": + position.set(Event.KEY_BATTERY, value); + break; + default: + position.set(key, value); + break; } - } else { - position.setTime(new Date()); - } - position.setLatitude(Double.parseDouble(params.get("lat").get(0))); - position.setLongitude(Double.parseDouble(params.get("lon").get(0))); - - if (params.containsKey("speed")) { - position.setSpeed(Double.parseDouble(params.get("speed").get(0))); - } - - if (params.containsKey("bearing")) { - position.setCourse(Double.parseDouble(params.get("bearing").get(0))); - } else if (params.containsKey("heading")) { - position.setCourse(Double.parseDouble(params.get("heading").get(0))); - } - - if (params.containsKey("altitude")) { - position.setAltitude(Double.parseDouble(params.get("altitude").get(0))); } - if (params.containsKey("hdop")) { - position.set(Event.KEY_HDOP, params.get("hdop").get(0)); - } - - if (params.containsKey("vacc")) { - position.set("vacc", params.get("vacc").get(0)); - } - - if (params.containsKey("hacc")) { - position.set("hacc", params.get("hacc").get(0)); - } - - if (params.containsKey("batt")) { - position.set(Event.KEY_BATTERY, params.get("batt").get(0)); - } - - if (params.containsKey("desc")) { - position.set("description", params.get("desc").get(0)); + if (position.getFixTime() == null) { + position.setTime(new Date()); } if (channel != null) { -- cgit v1.2.3