diff options
author | Anton Tananaev <anton.tananaev@gmail.com> | 2017-12-08 00:26:33 +1300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-12-08 00:26:33 +1300 |
commit | f59bb0abaf113079288fb11940210d4f54f881e3 (patch) | |
tree | bb2ea92eadb953309467f5f5db9a21acb563d7d3 /src/org/traccar/protocol/FlespiProtocolDecoder.java | |
parent | 5abaff0e1a37184b67b02864b3527d7334965de8 (diff) | |
parent | 4d188ce441334fe2e5cf075e333c123c77909c75 (diff) | |
download | trackermap-server-f59bb0abaf113079288fb11940210d4f54f881e3.tar.gz trackermap-server-f59bb0abaf113079288fb11940210d4f54f881e3.tar.bz2 trackermap-server-f59bb0abaf113079288fb11940210d4f54f881e3.zip |
Merge pull request #3667 from nadezhdamosunova/master
flespi protocol remastered to receive data from flespi stream
Diffstat (limited to 'src/org/traccar/protocol/FlespiProtocolDecoder.java')
-rw-r--r-- | src/org/traccar/protocol/FlespiProtocolDecoder.java | 223 |
1 files changed, 194 insertions, 29 deletions
diff --git a/src/org/traccar/protocol/FlespiProtocolDecoder.java b/src/org/traccar/protocol/FlespiProtocolDecoder.java index d122df383..89e6c6fd3 100644 --- a/src/org/traccar/protocol/FlespiProtocolDecoder.java +++ b/src/org/traccar/protocol/FlespiProtocolDecoder.java @@ -18,6 +18,10 @@ package org.traccar.protocol; import org.jboss.netty.channel.Channel; import org.jboss.netty.handler.codec.http.HttpRequest; import org.jboss.netty.handler.codec.http.HttpResponseStatus; +import org.jboss.netty.handler.codec.http.multipart.Attribute; +import org.jboss.netty.handler.codec.http.multipart.DefaultHttpDataFactory; +import org.jboss.netty.handler.codec.http.multipart.HttpPostRequestDecoder; +import org.jboss.netty.handler.codec.http.multipart.InterfaceHttpData; import org.traccar.BaseHttpProtocolDecoder; import org.traccar.DeviceSession; import org.traccar.model.Position; @@ -27,12 +31,15 @@ import javax.json.JsonArray; import javax.json.JsonNumber; import javax.json.JsonObject; import javax.json.JsonString; +import javax.json.JsonValue; import java.io.StringReader; import java.net.SocketAddress; -import java.nio.charset.StandardCharsets; import java.util.Date; import java.util.LinkedList; import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.Iterator; public class FlespiProtocolDecoder extends BaseHttpProtocolDecoder { @@ -45,8 +52,16 @@ public class FlespiProtocolDecoder extends BaseHttpProtocolDecoder { Channel channel, SocketAddress remoteAddress, Object msg) throws Exception { HttpRequest request = (HttpRequest) msg; - JsonArray result = Json.createReader(new StringReader(request.getContent().toString(StandardCharsets.UTF_8))) - .readArray(); + HttpPostRequestDecoder decoder = new HttpPostRequestDecoder(new DefaultHttpDataFactory(false), request); + InterfaceHttpData data = decoder.getBodyHttpData("data"); + if (data.getHttpDataType() != InterfaceHttpData.HttpDataType.Attribute) { + sendResponse(channel, HttpResponseStatus.BAD_REQUEST); + return null; + } + + Attribute attribute = (Attribute) data; + String value = attribute.getValue(); + JsonArray result = Json.createReader(new StringReader(value)).readArray(); List<Position> positions = new LinkedList<>(); for (int i = 0; i < result.size(); i++) { JsonObject message = result.getJsonObject(i); @@ -60,6 +75,7 @@ public class FlespiProtocolDecoder extends BaseHttpProtocolDecoder { } Position position = new Position(); position.setDeviceId(deviceSession.getDeviceId()); + position.setProtocol(getProtocolName()); decodePosition(message, position); positions.add(position); } @@ -69,36 +85,185 @@ public class FlespiProtocolDecoder extends BaseHttpProtocolDecoder { } private void decodePosition(JsonObject object, Position position) { - position.setProtocol(getProtocolName()); - - Date deviceTime = new Date((long) object.getJsonNumber("timestamp").doubleValue() * 1000); - position.setTime(deviceTime); - JsonNumber lat = object.getJsonNumber("position.latitude"); - JsonNumber lon = object.getJsonNumber("position.longitude"); - if (lat != null && lon != null) { - position.setLatitude(lat.doubleValue()); - position.setLongitude(lon.doubleValue()); - } else { - getLastLocation(position, deviceTime); - } - JsonNumber speed = object.getJsonNumber("position.speed"); - if (speed != null) { - position.setSpeed(speed.doubleValue()); - } - JsonNumber course = object.getJsonNumber("position.direction"); - if (course != null) { - position.setCourse(course.doubleValue()); + // store all params in position + Set<Map.Entry<String, JsonValue>> params = object.entrySet(); + Iterator<Map.Entry<String, JsonValue>> it = params.iterator(); + while (it.hasNext()) { + Map.Entry<String, JsonValue> param = it.next(); + String paramName = param.getKey(); + JsonValue paramValue = param.getValue(); + int index = -1; + if (paramName.contains("#")) { + String[] parts = paramName.split("#"); + paramName = parts[0]; + index = Integer.parseInt(parts[1]); + } + if (!saveParam(paramName, index, paramValue, position)) { + saveUnknownParam(param.getKey(), param.getValue(), position); + } } - JsonNumber altitude = object.getJsonNumber("position.altitude"); - if (altitude != null) { - position.setAltitude(altitude.doubleValue()); + if (position.getLatitude() == 0 && position.getLongitude() == 0) { + getLastLocation(position, position.getDeviceTime()); } + } - position.setValid(object.getBoolean("position.valid", true)); - position.set(Position.KEY_SATELLITES, object.getInt("position.satellites", 0)); + private boolean saveParam(String name, int index, JsonValue value, Position position) { + switch (name) { + case "timestamp": + Date deviceTime = new Date((long) (((JsonNumber) value).doubleValue() * 1000)); + position.setTime(deviceTime); + return true; + case "position.latitude": + position.setLatitude(((JsonNumber) value).doubleValue()); + return true; + case "position.longitude": + position.setLongitude(((JsonNumber) value).doubleValue()); + return true; + case "position.speed": + position.setSpeed(((JsonNumber) value).doubleValue()); + return true; + case "position.direction": + position.setCourse(((JsonNumber) value).doubleValue()); + return true; + case "position.altitude": + position.setAltitude(((JsonNumber) value).doubleValue()); + return true; + case "position.satellites": + position.set(Position.KEY_SATELLITES, ((JsonNumber) value).intValue()); + return true; + case "position.valid": + position.setValid(value == JsonValue.TRUE); + return true; + case "position.hdop": + position.set(Position.KEY_HDOP, ((JsonNumber) value).doubleValue()); + return true; + case "position.pdop": + position.set(Position.KEY_PDOP, ((JsonNumber) value).doubleValue()); + return true; + case "din": + case "dout": + String key = ("din".equals(name)) ? Position.KEY_INPUT : Position.KEY_OUTPUT; + if (value == JsonValue.TRUE && index <= 32 && index >= 1) { + if (position.getInteger(key) == 0) { + position.set(key, 1 << (index - 1)); + } else { + position.set(key, (position.getInteger(key) | 1 << (index - 1))); + } + } + return true; + case "gps.vehicle.mileage": + position.set(Position.KEY_ODOMETER, ((JsonNumber) value).doubleValue()); + return true; + case "external.powersource.voltage": + position.set(Position.KEY_POWER, ((JsonNumber) value).doubleValue()); + return true; + case "battery.voltage": + position.set(Position.KEY_BATTERY, ((JsonNumber) value).doubleValue()); + return true; + case "fuel.level": + case "can.fuel.level": + position.set(Position.KEY_FUEL_LEVEL, ((JsonNumber) value).doubleValue()); + return true; + case "engine.rpm": + case "can.engine.rpm": + position.set(Position.KEY_RPM, ((JsonNumber) value).doubleValue()); + return true; + case "can.engine.temperature": + position.set(Position.PREFIX_TEMP + (index > 0 ? index : 0), ((JsonNumber) value).doubleValue()); + return true; + case "engine.ignition.status": + position.set(Position.KEY_IGNITION, value == JsonValue.TRUE); + return true; + case "movement.status": + position.set(Position.KEY_MOTION, value == JsonValue.TRUE); + return true; + case "device.temperature": + position.set(Position.KEY_DEVICE_TEMP, ((JsonNumber) value).doubleValue()); + return true; + case "ibutton.code": + position.set(Position.KEY_DRIVER_UNIQUE_ID, ((JsonString) value).getString()); + return true; + case "vehicle.vin": + position.set(Position.KEY_VIN, ((JsonString) value).getString()); + return true; + case "alarm.event.trigger": + if (value == JsonValue.TRUE) { + position.set(Position.KEY_ALARM, Position.ALARM_GENERAL); + } + return true; + case "towing.event.trigger": + case "towing.alarm.status": + if (value == JsonValue.TRUE) { + position.set(Position.KEY_ALARM, Position.ALARM_TOW); + } + return true; + case "geofence.event.enter": + if (value == JsonValue.TRUE) { + position.set(Position.KEY_ALARM, Position.ALARM_GEOFENCE_ENTER); + } + return true; + case "geofence.event.exit": + if (value == JsonValue.TRUE) { + position.set(Position.KEY_ALARM, Position.ALARM_GEOFENCE_EXIT); + } + return true; + case "shock.event.trigger": + if (value == JsonValue.TRUE) { + position.set(Position.KEY_ALARM, Position.ALARM_SHOCK); + } + return true; + case "overspeeding.event.trigger": + if (value == JsonValue.TRUE) { + position.set(Position.KEY_ALARM, Position.ALARM_OVERSPEED); + } + return true; + case "harsh.acceleration.event.trigger": + if (value == JsonValue.TRUE) { + position.set(Position.KEY_ALARM, Position.ALARM_ACCELERATION); + } + return true; + case "harsh.braking.event.trigger": + if (value == JsonValue.TRUE) { + position.set(Position.KEY_ALARM, Position.ALARM_BRAKING); + } + return true; + case "harsh.cornering.event.trigger": + if (value == JsonValue.TRUE) { + position.set(Position.KEY_ALARM, Position.ALARM_CORNERING); + } + return true; + case "gnss.antenna.cut.status": + if (value == JsonValue.TRUE) { + position.set(Position.KEY_ALARM, Position.ALARM_GPS_ANTENNA_CUT); + } + return true; + case "gsm.jamming.event.trigger": + if (value == JsonValue.TRUE) { + position.set(Position.KEY_ALARM, Position.ALARM_JAMMING); + } + return true; + case "hood.open.status": + if (value == JsonValue.TRUE) { + position.set(Position.KEY_ALARM, Position.ALARM_BONNET); + } + return true; + default: + return false; + } + } - if (object.getBoolean("alarm.event.trigger", false)) { - position.set(Position.KEY_ALARM, Position.ALARM_GENERAL); + private void saveUnknownParam(String name, JsonValue value, Position position) { + if (value instanceof JsonNumber) { + if (((JsonNumber) value).isIntegral()) { + position.set(name, ((JsonNumber) value).longValue()); + } else { + position.set(name, ((JsonNumber) value).doubleValue()); + } + position.set(name, ((JsonNumber) value).doubleValue()); + } else if (value instanceof JsonString) { + position.set(name, ((JsonString) value).getString()); + } else if (value == JsonValue.TRUE || value == JsonValue.FALSE) { + position.set(name, value == JsonValue.TRUE); } } } |