diff options
author | Christoph Krey <c@ckrey.de> | 2018-11-12 09:17:52 +0100 |
---|---|---|
committer | Christoph Krey <c@ckrey.de> | 2018-11-12 09:17:52 +0100 |
commit | e9ae3ba883cee96f410c7a9b3495e86d076410aa (patch) | |
tree | f5cd1c5c83beeb69766a18271c42e68fdd80f5a3 | |
parent | 25b6d4fe524f41ae9a37d7b57a2c4f0e8e1dfa25 (diff) | |
download | traccar-server-e9ae3ba883cee96f410c7a9b3495e86d076410aa.tar.gz traccar-server-e9ae3ba883cee96f410c7a9b3495e86d076410aa.tar.bz2 traccar-server-e9ae3ba883cee96f410c7a9b3495e86d076410aa.zip |
[NEW] backport to traccar 4.2
-rw-r--r-- | src/org/traccar/protocol/OwnTracksProtocolDecoder.java | 162 |
1 files changed, 139 insertions, 23 deletions
diff --git a/src/org/traccar/protocol/OwnTracksProtocolDecoder.java b/src/org/traccar/protocol/OwnTracksProtocolDecoder.java index f38c8ef25..5786cf440 100644 --- a/src/org/traccar/protocol/OwnTracksProtocolDecoder.java +++ b/src/org/traccar/protocol/OwnTracksProtocolDecoder.java @@ -14,6 +14,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ + package org.traccar.protocol; import io.netty.channel.Channel; @@ -21,9 +22,11 @@ import io.netty.handler.codec.http.FullHttpRequest; import io.netty.handler.codec.http.HttpResponseStatus; import org.traccar.BaseHttpProtocolDecoder; import org.traccar.DeviceSession; -import org.traccar.Protocol; +import org.traccar.Context; import org.traccar.helper.UnitsConverter; import org.traccar.model.Position; +import org.traccar.model.Event; +import org.traccar.model.Device; import javax.json.Json; import javax.json.JsonObject; @@ -34,7 +37,7 @@ import java.util.Date; public class OwnTracksProtocolDecoder extends BaseHttpProtocolDecoder { - public OwnTracksProtocolDecoder(Protocol protocol) { + public OwnTracksProtocolDecoder(OwnTracksProtocol protocol) { super(protocol); } @@ -46,12 +49,54 @@ public class OwnTracksProtocolDecoder extends BaseHttpProtocolDecoder { JsonObject root = Json.createReader( new StringReader(request.content().toString(StandardCharsets.US_ASCII))).readObject(); - if (!root.containsKey("_type") || !root.getString("_type").equals("location")) { + if (!root.containsKey("_type")) { + sendResponse(channel, HttpResponseStatus.OK); + return null; + } + if (!root.getString("_type").equals("location") + && !root.getString("_type").equals("lwt")) { + sendResponse(channel, HttpResponseStatus.BAD_REQUEST); + return null; + } + + Position position = new Position(); + String uniqueId; + + if (root.containsKey("topic")) { + uniqueId = root.getString("topic"); + if (root.containsKey("tid")) { + position.set("tid", root.getString("tid")); + } + } else { + uniqueId = root.getString("tid"); + } + DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, uniqueId); + if (deviceSession == null) { + sendResponse(channel, HttpResponseStatus.BAD_REQUEST); + return null; + } + + if (root.getString("_type").equals("lwt")) { + Context.getConnectionManager().updateDevice(deviceSession.getDeviceId(), Device.STATUS_OFFLINE, new Date()); + + sendResponse(channel, HttpResponseStatus.OK); + return null; + } + + if (root.containsKey("t") && root.getString("t").equals("p")) { sendResponse(channel, HttpResponseStatus.OK); return null; } - Position position = new Position(getProtocolName()); + position.setDeviceId(deviceSession.getDeviceId()); + position.setProtocol(getProtocolName()); + //position.set(Position.KEY_ORIGINAL, request.getContent().toString(StandardCharsets.US_ASCII)); + + position.setTime(new Date(root.getJsonNumber("tst").longValue() * 1000)); + if (root.containsKey("sent")) { + position.setDeviceTime(new Date(root.getJsonNumber("sent").longValue() * 1000)); + } + position.setValid(true); position.setLatitude(root.getJsonNumber("lat").doubleValue()); @@ -70,34 +115,105 @@ public class OwnTracksProtocolDecoder extends BaseHttpProtocolDecoder { position.setAccuracy(root.getInt("acc")); } if (root.containsKey("t")) { - position.set("t", root.getString("t")); + String t = root.getString("t"); + position.set("t", t); + Integer rty = -1; + if (root.containsKey("rty")) { + rty = root.getInt("rty"); + } + setEventOrAlarm(position, t, rty); } if (root.containsKey("batt")) { - position.set(Position.KEY_BATTERY, root.getInt("batt")); + position.set(Position.KEY_BATTERY_LEVEL, root.getInt("batt")); } - - position.setTime(new Date(root.getJsonNumber("tst").longValue() * 1000)); - - String uniqueId; - - if (root.containsKey("topic")) { - uniqueId = root.getString("topic"); - if (root.containsKey("tid")) { - position.set("tid", root.getString("tid")); - } - } else { - uniqueId = root.getString("tid"); + if (root.containsKey("uext")) { + position.set(Position.KEY_POWER, root.getJsonNumber("uext").doubleValue()); + } + if (root.containsKey("ubatt")) { + position.set(Position.KEY_BATTERY, root.getJsonNumber("ubatt").doubleValue()); + } + if (root.containsKey("vin")) { + position.set(Position.KEY_VIN, root.getString("vin")); + } + if (root.containsKey("name")) { + position.set(Position.KEY_VIN, root.getString("name")); + } + if (root.containsKey("rpm")) { + position.set(Position.KEY_RPM, root.getInt("rpm")); + } + if (root.containsKey("ign")) { + position.set(Position.KEY_IGNITION, root.getBoolean("ign")); + } + if (root.containsKey("motion")) { + position.set(Position.KEY_MOTION, root.getBoolean("motion")); + } + if (root.containsKey("odometer")) { + position.set(Position.KEY_ODOMETER, root.getJsonNumber("odometer").doubleValue() * 1000.0); + } + if (root.containsKey("hmc")) { + position.set(Position.KEY_HOURS, root.getJsonNumber("hmc").doubleValue() / 3600.0); } - DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, uniqueId); - if (deviceSession == null) { - sendResponse(channel, HttpResponseStatus.BAD_REQUEST); - return null; + if (root.containsKey("anum")) { + Integer anum = root.getInt("anum"); + for (Integer i = 0; i < anum; i++) { + String indexString = String.format("%02d", i); + if (root.containsKey("adda-" + indexString)) { + position.set(Position.PREFIX_ADC + (i + 1), root.getString("adda-" + indexString)); + } + if (root.containsKey("temp_c-" + indexString)) { + position.set(Position.PREFIX_TEMP + (i + 1), + root.getJsonNumber("temp_c-" + indexString).doubleValue()); + } + } } - position.setDeviceId(deviceSession.getDeviceId()); sendResponse(channel, HttpResponseStatus.OK); return position; } + private void setEventOrAlarm(Position position, String t, Integer rty) { + if (t.equals("9")) { + position.set(Position.KEY_EVENT, Event.TYPE_ALARM); + position.set(Position.KEY_ALARM, Position.ALARM_LOW_BATTERY); + } else if (t.equals("1")) { + position.set(Position.KEY_EVENT, Event.TYPE_ALARM); + position.set(Position.KEY_ALARM, Position.ALARM_POWER_ON); + } else if (t.equals("i")) { + //position.set(Position.KEY_EVENT, Event.TYPE_IGNITION_ON); + position.set(Position.KEY_IGNITION, true); + } else if (t.equals("I")) { + //position.set(Position.KEY_EVENT, Event.TYPE_IGNITION_OFF); + position.set(Position.KEY_IGNITION, false); + } else if (t.equals("E")) { + position.set(Position.KEY_EVENT, Event.TYPE_ALARM); + position.set(Position.KEY_ALARM, Position.ALARM_POWER_RESTORED); + } else if (t.equals("e")) { + position.set(Position.KEY_EVENT, Event.TYPE_ALARM); + position.set(Position.KEY_ALARM, Position.ALARM_POWER_CUT); + } else if (t.equals("!")) { + position.set(Position.KEY_EVENT, Event.TYPE_ALARM); + position.set(Position.KEY_ALARM, Position.ALARM_TOW); + } else if (t.equals("s")) { + position.set(Position.KEY_EVENT, Event.TYPE_ALARM); + position.set(Position.KEY_ALARM, Position.ALARM_OVERSPEED); + } else if (t.equals("h")) { + position.set(Position.KEY_EVENT, Event.TYPE_ALARM); + switch (rty) { + case 0: + case 3: + position.set(Position.KEY_ALARM, Position.ALARM_BRAKING); + break; + case 1: + case 4: + position.set(Position.KEY_ALARM, Position.ALARM_ACCELERATION); + break; + case 2: + case 5: + default: + position.set(Position.KEY_ALARM, Position.ALARM_CORNERING); + break; + } + } + } } |