From e9ae3ba883cee96f410c7a9b3495e86d076410aa Mon Sep 17 00:00:00 2001 From: Christoph Krey Date: Mon, 12 Nov 2018 09:17:52 +0100 Subject: [NEW] backport to traccar 4.2 --- .../traccar/protocol/OwnTracksProtocolDecoder.java | 162 ++++++++++++++++++--- 1 file 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; + } + } + } } -- cgit v1.2.3 From 22fd6a6a2a38de73b0ce053c862bd7221e9c2184 Mon Sep 17 00:00:00 2001 From: Christoph Krey Date: Mon, 12 Nov 2018 10:37:57 +0100 Subject: [FIX] remove debug code --- src/org/traccar/protocol/OwnTracksProtocolDecoder.java | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/org/traccar/protocol/OwnTracksProtocolDecoder.java b/src/org/traccar/protocol/OwnTracksProtocolDecoder.java index 5786cf440..807112d10 100644 --- a/src/org/traccar/protocol/OwnTracksProtocolDecoder.java +++ b/src/org/traccar/protocol/OwnTracksProtocolDecoder.java @@ -90,7 +90,6 @@ public class OwnTracksProtocolDecoder extends BaseHttpProtocolDecoder { 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")) { @@ -180,10 +179,8 @@ public class OwnTracksProtocolDecoder extends BaseHttpProtocolDecoder { 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); -- cgit v1.2.3 From 2dc53f13f965b17152b4174e51c60d143b12fbc7 Mon Sep 17 00:00:00 2001 From: Christoph Krey Date: Mon, 12 Nov 2018 11:19:48 +0100 Subject: [FIX] removed superfluos blank line, used meaningfull variable name --- src/org/traccar/protocol/OwnTracksProtocolDecoder.java | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/org/traccar/protocol/OwnTracksProtocolDecoder.java b/src/org/traccar/protocol/OwnTracksProtocolDecoder.java index 807112d10..c81ebe522 100644 --- a/src/org/traccar/protocol/OwnTracksProtocolDecoder.java +++ b/src/org/traccar/protocol/OwnTracksProtocolDecoder.java @@ -14,7 +14,6 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - package org.traccar.protocol; import io.netty.channel.Channel; @@ -114,13 +113,13 @@ public class OwnTracksProtocolDecoder extends BaseHttpProtocolDecoder { position.setAccuracy(root.getInt("acc")); } if (root.containsKey("t")) { - String t = root.getString("t"); - position.set("t", t); + String trigger = root.getString("t"); + position.set("t", trigger); Integer rty = -1; if (root.containsKey("rty")) { rty = root.getInt("rty"); } - setEventOrAlarm(position, t, rty); + setEventOrAlarm(position, trigger, rty); } if (root.containsKey("batt")) { position.set(Position.KEY_BATTERY_LEVEL, root.getInt("batt")); -- cgit v1.2.3 From e8f0c569e2aec2ad44a532a8bc366556e1f374c5 Mon Sep 17 00:00:00 2001 From: Christoph Krey Date: Mon, 12 Nov 2018 11:22:51 +0100 Subject: [FIX] use meaningfull variable names --- .../traccar/protocol/OwnTracksProtocolDecoder.java | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/src/org/traccar/protocol/OwnTracksProtocolDecoder.java b/src/org/traccar/protocol/OwnTracksProtocolDecoder.java index c81ebe522..b0a40e4fc 100644 --- a/src/org/traccar/protocol/OwnTracksProtocolDecoder.java +++ b/src/org/traccar/protocol/OwnTracksProtocolDecoder.java @@ -170,30 +170,30 @@ public class OwnTracksProtocolDecoder extends BaseHttpProtocolDecoder { return position; } - private void setEventOrAlarm(Position position, String t, Integer rty) { - if (t.equals("9")) { + private void setEventOrAlarm(Position position, String trigger, Integer rty) { + if (trigger.equals("9")) { position.set(Position.KEY_EVENT, Event.TYPE_ALARM); position.set(Position.KEY_ALARM, Position.ALARM_LOW_BATTERY); - } else if (t.equals("1")) { + } else if (trigger.equals("1")) { position.set(Position.KEY_EVENT, Event.TYPE_ALARM); position.set(Position.KEY_ALARM, Position.ALARM_POWER_ON); - } else if (t.equals("i")) { + } else if (trigger.equals("i")) { position.set(Position.KEY_IGNITION, true); - } else if (t.equals("I")) { + } else if (trigger.equals("I")) { position.set(Position.KEY_IGNITION, false); - } else if (t.equals("E")) { + } else if (trigger.equals("E")) { position.set(Position.KEY_EVENT, Event.TYPE_ALARM); position.set(Position.KEY_ALARM, Position.ALARM_POWER_RESTORED); - } else if (t.equals("e")) { + } else if (trigger.equals("e")) { position.set(Position.KEY_EVENT, Event.TYPE_ALARM); position.set(Position.KEY_ALARM, Position.ALARM_POWER_CUT); - } else if (t.equals("!")) { + } else if (trigger.equals("!")) { position.set(Position.KEY_EVENT, Event.TYPE_ALARM); position.set(Position.KEY_ALARM, Position.ALARM_TOW); - } else if (t.equals("s")) { + } else if (trigger.equals("s")) { position.set(Position.KEY_EVENT, Event.TYPE_ALARM); position.set(Position.KEY_ALARM, Position.ALARM_OVERSPEED); - } else if (t.equals("h")) { + } else if (trigger.equals("h")) { position.set(Position.KEY_EVENT, Event.TYPE_ALARM); switch (rty) { case 0: -- cgit v1.2.3 From b499a5c5632bdb5f7f730f4763f77605d0073fb8 Mon Sep 17 00:00:00 2001 From: Christoph Krey Date: Mon, 12 Nov 2018 11:35:35 +0100 Subject: [FIX] don't set KEY_EVENT in decoder --- .../traccar/protocol/OwnTracksProtocolDecoder.java | 21 +++++++-------------- 1 file changed, 7 insertions(+), 14 deletions(-) diff --git a/src/org/traccar/protocol/OwnTracksProtocolDecoder.java b/src/org/traccar/protocol/OwnTracksProtocolDecoder.java index b0a40e4fc..9dead6344 100644 --- a/src/org/traccar/protocol/OwnTracksProtocolDecoder.java +++ b/src/org/traccar/protocol/OwnTracksProtocolDecoder.java @@ -115,11 +115,11 @@ public class OwnTracksProtocolDecoder extends BaseHttpProtocolDecoder { if (root.containsKey("t")) { String trigger = root.getString("t"); position.set("t", trigger); - Integer rty = -1; + Integer reportType = -1; if (root.containsKey("rty")) { - rty = root.getInt("rty"); + reportType = root.getInt("rty"); } - setEventOrAlarm(position, trigger, rty); + setEventOrAlarm(position, trigger, reportType); } if (root.containsKey("batt")) { position.set(Position.KEY_BATTERY_LEVEL, root.getInt("batt")); @@ -153,8 +153,8 @@ public class OwnTracksProtocolDecoder extends BaseHttpProtocolDecoder { } if (root.containsKey("anum")) { - Integer anum = root.getInt("anum"); - for (Integer i = 0; i < anum; i++) { + Integer numberOfAnalogueInputs = root.getInt("anum"); + for (Integer i = 0; i < numberOfAnalogueInputs; i++) { String indexString = String.format("%02d", i); if (root.containsKey("adda-" + indexString)) { position.set(Position.PREFIX_ADC + (i + 1), root.getString("adda-" + indexString)); @@ -170,32 +170,25 @@ public class OwnTracksProtocolDecoder extends BaseHttpProtocolDecoder { return position; } - private void setEventOrAlarm(Position position, String trigger, Integer rty) { + private void setEventOrAlarm(Position position, String trigger, Integer reportType) { if (trigger.equals("9")) { - position.set(Position.KEY_EVENT, Event.TYPE_ALARM); position.set(Position.KEY_ALARM, Position.ALARM_LOW_BATTERY); } else if (trigger.equals("1")) { - position.set(Position.KEY_EVENT, Event.TYPE_ALARM); position.set(Position.KEY_ALARM, Position.ALARM_POWER_ON); } else if (trigger.equals("i")) { position.set(Position.KEY_IGNITION, true); } else if (trigger.equals("I")) { position.set(Position.KEY_IGNITION, false); } else if (trigger.equals("E")) { - position.set(Position.KEY_EVENT, Event.TYPE_ALARM); position.set(Position.KEY_ALARM, Position.ALARM_POWER_RESTORED); } else if (trigger.equals("e")) { - position.set(Position.KEY_EVENT, Event.TYPE_ALARM); position.set(Position.KEY_ALARM, Position.ALARM_POWER_CUT); } else if (trigger.equals("!")) { - position.set(Position.KEY_EVENT, Event.TYPE_ALARM); position.set(Position.KEY_ALARM, Position.ALARM_TOW); } else if (trigger.equals("s")) { - position.set(Position.KEY_EVENT, Event.TYPE_ALARM); position.set(Position.KEY_ALARM, Position.ALARM_OVERSPEED); } else if (trigger.equals("h")) { - position.set(Position.KEY_EVENT, Event.TYPE_ALARM); - switch (rty) { + switch (reportType) { case 0: case 3: position.set(Position.KEY_ALARM, Position.ALARM_BRAKING); -- cgit v1.2.3 From 179a23cc2abd7210f813554017123a178ef96a8c Mon Sep 17 00:00:00 2001 From: Christoph Krey Date: Mon, 12 Nov 2018 11:38:05 +0100 Subject: [FIX] remove unused include for Event --- src/org/traccar/protocol/OwnTracksProtocolDecoder.java | 1 - 1 file changed, 1 deletion(-) diff --git a/src/org/traccar/protocol/OwnTracksProtocolDecoder.java b/src/org/traccar/protocol/OwnTracksProtocolDecoder.java index 9dead6344..82857ed01 100644 --- a/src/org/traccar/protocol/OwnTracksProtocolDecoder.java +++ b/src/org/traccar/protocol/OwnTracksProtocolDecoder.java @@ -24,7 +24,6 @@ import org.traccar.DeviceSession; 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; -- cgit v1.2.3 From 3d49857da495b6302543133a31051922dbcee5d7 Mon Sep 17 00:00:00 2001 From: Christoph Krey Date: Tue, 13 Nov 2018 18:35:15 +0100 Subject: [FIX] coding style (switch instead of if else if) [FIX] use Protocol as base class [FIX] don't manage device status from decoder --- .../traccar/protocol/OwnTracksProtocolDecoder.java | 83 ++++++++++++---------- 1 file changed, 46 insertions(+), 37 deletions(-) diff --git a/src/org/traccar/protocol/OwnTracksProtocolDecoder.java b/src/org/traccar/protocol/OwnTracksProtocolDecoder.java index 82857ed01..4ce880316 100644 --- a/src/org/traccar/protocol/OwnTracksProtocolDecoder.java +++ b/src/org/traccar/protocol/OwnTracksProtocolDecoder.java @@ -21,10 +21,9 @@ 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.Context; +import org.traccar.Protocol; import org.traccar.helper.UnitsConverter; import org.traccar.model.Position; -import org.traccar.model.Device; import javax.json.Json; import javax.json.JsonObject; @@ -35,7 +34,7 @@ import java.util.Date; public class OwnTracksProtocolDecoder extends BaseHttpProtocolDecoder { - public OwnTracksProtocolDecoder(OwnTracksProtocol protocol) { + public OwnTracksProtocolDecoder(Protocol protocol) { super(protocol); } @@ -75,8 +74,6 @@ public class OwnTracksProtocolDecoder extends BaseHttpProtocolDecoder { } if (root.getString("_type").equals("lwt")) { - Context.getConnectionManager().updateDevice(deviceSession.getDeviceId(), Device.STATUS_OFFLINE, new Date()); - sendResponse(channel, HttpResponseStatus.OK); return null; } @@ -170,38 +167,50 @@ public class OwnTracksProtocolDecoder extends BaseHttpProtocolDecoder { } private void setEventOrAlarm(Position position, String trigger, Integer reportType) { - if (trigger.equals("9")) { - position.set(Position.KEY_ALARM, Position.ALARM_LOW_BATTERY); - } else if (trigger.equals("1")) { - position.set(Position.KEY_ALARM, Position.ALARM_POWER_ON); - } else if (trigger.equals("i")) { - position.set(Position.KEY_IGNITION, true); - } else if (trigger.equals("I")) { - position.set(Position.KEY_IGNITION, false); - } else if (trigger.equals("E")) { - position.set(Position.KEY_ALARM, Position.ALARM_POWER_RESTORED); - } else if (trigger.equals("e")) { - position.set(Position.KEY_ALARM, Position.ALARM_POWER_CUT); - } else if (trigger.equals("!")) { - position.set(Position.KEY_ALARM, Position.ALARM_TOW); - } else if (trigger.equals("s")) { - position.set(Position.KEY_ALARM, Position.ALARM_OVERSPEED); - } else if (trigger.equals("h")) { - switch (reportType) { - 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; - } + switch (trigger) { + case "9": + position.set(Position.KEY_ALARM, Position.ALARM_LOW_BATTERY); + break; + case "1": + position.set(Position.KEY_ALARM, Position.ALARM_POWER_ON); + break; + case "i": + position.set(Position.KEY_IGNITION, true); + break; + case "I": + position.set(Position.KEY_IGNITION, false); + break; + case "E": + position.set(Position.KEY_ALARM, Position.ALARM_POWER_RESTORED); + break; + case "e": + position.set(Position.KEY_ALARM, Position.ALARM_POWER_CUT); + break; + case "!": + position.set(Position.KEY_ALARM, Position.ALARM_TOW); + break; + case "s": + position.set(Position.KEY_ALARM, Position.ALARM_OVERSPEED); + break; + case "h": + switch (reportType) { + 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; + } + break; + default: + break; } } } -- cgit v1.2.3