aboutsummaryrefslogtreecommitdiff
path: root/src/org
diff options
context:
space:
mode:
authorninioe <ninioe@gmail.com>2016-08-01 16:12:47 +0300
committerninioe <ninioe@gmail.com>2016-08-01 16:12:47 +0300
commit94781fa3ba35e494e309aee19a5e15c14d3193c1 (patch)
treea1e4a90e9a2fce3db8bc51981aeb8a23cced5e6d /src/org
parentb4841fa0a6293c0b895cf6bb65c6c2d871c78397 (diff)
parente177e7d083a4dd3f64172802661bf658d26d1163 (diff)
downloadtrackermap-server-94781fa3ba35e494e309aee19a5e15c14d3193c1.tar.gz
trackermap-server-94781fa3ba35e494e309aee19a5e15c14d3193c1.tar.bz2
trackermap-server-94781fa3ba35e494e309aee19a5e15c14d3193c1.zip
Merge remote-tracking branch 'refs/remotes/tananaev/master'
Diffstat (limited to 'src/org')
-rw-r--r--src/org/traccar/DefaultDataHandler.java1
-rw-r--r--src/org/traccar/ExtendedObjectDecoder.java3
-rw-r--r--src/org/traccar/Main.java9
-rw-r--r--src/org/traccar/MainEventHandler.java7
-rw-r--r--src/org/traccar/api/resource/ServerResource.java5
-rw-r--r--src/org/traccar/database/DataManager.java27
-rw-r--r--src/org/traccar/database/DeviceManager.java42
-rw-r--r--src/org/traccar/database/PermissionsManager.java11
-rw-r--r--src/org/traccar/events/GeofenceEventHandler.java39
-rw-r--r--src/org/traccar/events/MotionEventHandler.java48
-rw-r--r--src/org/traccar/events/OverspeedEventHandler.java40
-rw-r--r--src/org/traccar/model/Device.java13
-rw-r--r--src/org/traccar/model/Group.java12
-rw-r--r--src/org/traccar/model/Server.java12
-rw-r--r--src/org/traccar/protocol/H02ProtocolDecoder.java2
-rw-r--r--src/org/traccar/protocol/HuaShengProtocolDecoder.java10
-rw-r--r--src/org/traccar/protocol/OsmAndProtocolDecoder.java26
-rw-r--r--src/org/traccar/protocol/T55ProtocolDecoder.java11
18 files changed, 145 insertions, 173 deletions
diff --git a/src/org/traccar/DefaultDataHandler.java b/src/org/traccar/DefaultDataHandler.java
index 8923c3a0e..7194c6a77 100644
--- a/src/org/traccar/DefaultDataHandler.java
+++ b/src/org/traccar/DefaultDataHandler.java
@@ -25,7 +25,6 @@ public class DefaultDataHandler extends BaseDataHandler {
try {
Context.getDataManager().addPosition(position);
- Context.getDeviceManager().updateLatestPosition(position);
} catch (Exception error) {
Log.warning(error);
}
diff --git a/src/org/traccar/ExtendedObjectDecoder.java b/src/org/traccar/ExtendedObjectDecoder.java
index d7ca30de4..1cfc93541 100644
--- a/src/org/traccar/ExtendedObjectDecoder.java
+++ b/src/org/traccar/ExtendedObjectDecoder.java
@@ -36,7 +36,8 @@ public abstract class ExtendedObjectDecoder implements ChannelUpstreamHandler {
if (Context.getConfig().getBoolean("database.saveOriginal") && decodedMessage instanceof Position) {
Position position = (Position) decodedMessage;
if (originalMessage instanceof ChannelBuffer) {
- position.set(Position.KEY_ORIGINAL, ChannelBuffers.hexDump((ChannelBuffer) originalMessage));
+ ChannelBuffer buf = (ChannelBuffer) originalMessage;
+ position.set(Position.KEY_ORIGINAL, ChannelBuffers.hexDump(buf, 0, buf.writerIndex()));
} else if (originalMessage instanceof String) {
position.set(Position.KEY_ORIGINAL, DatatypeConverter.printHexBinary(
((String) originalMessage).getBytes(StandardCharsets.US_ASCII)));
diff --git a/src/org/traccar/Main.java b/src/org/traccar/Main.java
index e7e8d8ab9..e992691ad 100644
--- a/src/org/traccar/Main.java
+++ b/src/org/traccar/Main.java
@@ -23,8 +23,8 @@ import java.util.TimerTask;
import java.util.Locale;
public final class Main {
- static final long CLEAN_DELAY = 0;
- static final long CLEAN_PERIOD = 24 * 60 * 60 * 1000;
+
+ private static final long CLEAN_PERIOD = 24 * 60 * 60 * 1000;
private Main() {
}
@@ -40,8 +40,7 @@ public final class Main {
Context.getWebServer().start();
}
- Timer timer = new Timer();
- timer.scheduleAtFixedRate(new TimerTask() {
+ new Timer().scheduleAtFixedRate(new TimerTask() {
@Override
public void run() {
try {
@@ -50,7 +49,7 @@ public final class Main {
Log.warning(error);
}
}
- }, CLEAN_DELAY, CLEAN_PERIOD);
+ }, 0, CLEAN_PERIOD);
Runtime.getRuntime().addShutdownHook(new Thread() {
@Override
diff --git a/src/org/traccar/MainEventHandler.java b/src/org/traccar/MainEventHandler.java
index 771009aca..c01760283 100644
--- a/src/org/traccar/MainEventHandler.java
+++ b/src/org/traccar/MainEventHandler.java
@@ -26,6 +26,7 @@ import org.jboss.netty.handler.timeout.IdleStateEvent;
import org.traccar.helper.Log;
import org.traccar.model.Position;
+import java.sql.SQLException;
import java.text.SimpleDateFormat;
public class MainEventHandler extends IdleStateAwareChannelHandler {
@@ -36,6 +37,11 @@ public class MainEventHandler extends IdleStateAwareChannelHandler {
if (e.getMessage() != null && e.getMessage() instanceof Position) {
Position position = (Position) e.getMessage();
+ try {
+ Context.getDeviceManager().updateLatestPosition(position);
+ } catch (SQLException error) {
+ Log.warning(error);
+ }
String uniqueId = Context.getIdentityManager().getDeviceById(position.getDeviceId()).getUniqueId();
@@ -54,6 +60,7 @@ public class MainEventHandler extends IdleStateAwareChannelHandler {
s.append(", result: ").append(cmdResult);
}
Log.info(s.toString());
+
}
}
diff --git a/src/org/traccar/api/resource/ServerResource.java b/src/org/traccar/api/resource/ServerResource.java
index 9e42687ab..0ca0d62aa 100644
--- a/src/org/traccar/api/resource/ServerResource.java
+++ b/src/org/traccar/api/resource/ServerResource.java
@@ -37,14 +37,13 @@ public class ServerResource extends BaseResource {
@PermitAll
@GET
public Server get() throws SQLException {
- return Context.getDataManager().getServer();
+ return Context.getPermissionsManager().getServer();
}
@PUT
public Response update(Server entity) throws SQLException {
Context.getPermissionsManager().checkAdmin(getUserId());
- Context.getDataManager().updateServer(entity);
- Context.getPermissionsManager().refresh();
+ Context.getPermissionsManager().updateServer(entity);
return Response.ok(entity).build();
}
diff --git a/src/org/traccar/database/DataManager.java b/src/org/traccar/database/DataManager.java
index 04d0d44ea..78f1b4109 100644
--- a/src/org/traccar/database/DataManager.java
+++ b/src/org/traccar/database/DataManager.java
@@ -305,28 +305,11 @@ public class DataManager {
}
public void clearPositionsHistory() throws SQLException {
- int histDays = config.getInteger("database.positionsHistoryDays");
- if (histDays == 0) {
- return;
- }
-
- String sql = getQuery("database.clearPositionsHistory");
- if (sql == null) {
- return;
- }
-
- for (Device device : getAllDevices()) {
- Date lastUpdate = device.getLastUpdate();
- if (lastUpdate != null) {
-
- Date dateBefore = new Date(lastUpdate.getTime() - histDays * 24 * 3600 * 1000);
-
- QueryBuilder.create(dataSource, sql)
- .setLong("positionId", device.getPositionId())
- .setLong("deviceId", device.getId())
- .setDate("serverTime", dateBefore)
- .executeUpdate();
- }
+ int historyDays = config.getInteger("database.positionsHistoryDays");
+ if (historyDays != 0) {
+ QueryBuilder.create(dataSource, getQuery("database.deletePositions"))
+ .setDate("serverTime", new Date(System.currentTimeMillis() - historyDays * 24 * 3600 * 1000))
+ .executeUpdate();
}
}
diff --git a/src/org/traccar/database/DeviceManager.java b/src/org/traccar/database/DeviceManager.java
index 4dd7b41cb..3476139f2 100644
--- a/src/org/traccar/database/DeviceManager.java
+++ b/src/org/traccar/database/DeviceManager.java
@@ -32,6 +32,7 @@ import org.traccar.helper.Log;
import org.traccar.model.Device;
import org.traccar.model.Group;
import org.traccar.model.Position;
+import org.traccar.model.Server;
public class DeviceManager implements IdentityManager {
@@ -40,6 +41,7 @@ public class DeviceManager implements IdentityManager {
private final Config config;
private final DataManager dataManager;
private final long dataRefreshDelay;
+ private boolean lookupGroupsAttribute;
private Map<Long, Device> devicesById;
private Map<String, Device> devicesByUniqueId;
@@ -54,6 +56,7 @@ public class DeviceManager implements IdentityManager {
this.dataManager = dataManager;
this.config = Context.getConfig();
dataRefreshDelay = config.getLong("database.refreshDelay", DEFAULT_REFRESH_DELAY) * 1000;
+ lookupGroupsAttribute = config.getBoolean("deviceManager.lookupGroupsAttribute");
if (dataManager != null) {
try {
updateGroupCache(true);
@@ -105,7 +108,6 @@ public class DeviceManager implements IdentityManager {
}
}
device.setStatus(Device.STATUS_OFFLINE);
- device.setMotion(Device.STATUS_STOPPED);
}
}
for (Long cachedDeviceId : devicesById.keySet()) {
@@ -176,7 +178,6 @@ public class DeviceManager implements IdentityManager {
if (devicesById.containsKey(device.getId())) {
Device cachedDevice = devicesById.get(device.getId());
cachedDevice.setStatus(device.getStatus());
- cachedDevice.setMotion(device.getMotion());
}
}
@@ -191,10 +192,14 @@ public class DeviceManager implements IdentityManager {
positions.remove(deviceId);
}
+ public boolean isLatestPosition(Position position) {
+ Position lastPosition = getLastPosition(position.getDeviceId());
+ return lastPosition == null || position.getFixTime().compareTo(lastPosition.getFixTime()) > 0;
+ }
+
public void updateLatestPosition(Position position) throws SQLException {
- Position lastPosition = getLastPosition(position.getDeviceId());
- if (lastPosition == null || position.getFixTime().compareTo(lastPosition.getFixTime()) > 0) {
+ if (isLatestPosition(position)) {
dataManager.updateLatestPosition(position);
@@ -310,4 +315,33 @@ public class DeviceManager implements IdentityManager {
dataManager.removeGroup(groupId);
groupsById.remove(groupId);
}
+
+ public String lookupAttribute(long deviceId, String attributeName) {
+ String result = null;
+ Device device = getDeviceById(deviceId);
+ if (device != null) {
+ if (device.getAttributes().containsKey(attributeName)) {
+ result = (String) device.getAttributes().get(attributeName);
+ }
+ if (result == null && lookupGroupsAttribute) {
+ long groupId = device.getGroupId();
+ while (groupId != 0) {
+ if (getGroupById(groupId) != null) {
+ result = (String) getGroupById(groupId).getAttributes().get(attributeName);
+ if (result != null) {
+ break;
+ }
+ groupId = getGroupById(groupId).getGroupId();
+ } else {
+ groupId = 0;
+ }
+ }
+ }
+ if (result == null) {
+ Server server = Context.getPermissionsManager().getServer();
+ result = (String) server.getAttributes().get(attributeName);
+ }
+ }
+ return result;
+ }
}
diff --git a/src/org/traccar/database/PermissionsManager.java b/src/org/traccar/database/PermissionsManager.java
index d786dcc4e..92fcc3ebd 100644
--- a/src/org/traccar/database/PermissionsManager.java
+++ b/src/org/traccar/database/PermissionsManager.java
@@ -34,7 +34,7 @@ public class PermissionsManager {
private final DataManager dataManager;
- private Server server;
+ private volatile Server server;
private final Map<Long, User> users = new HashMap<>();
@@ -153,4 +153,13 @@ public class PermissionsManager {
}
}
+ public Server getServer() {
+ return server;
+ }
+
+ public void updateServer(Server server) throws SQLException {
+ dataManager.updateServer(server);
+ this.server = server;
+ }
+
}
diff --git a/src/org/traccar/events/GeofenceEventHandler.java b/src/org/traccar/events/GeofenceEventHandler.java
index 1ea7aee41..a0291dcfa 100644
--- a/src/org/traccar/events/GeofenceEventHandler.java
+++ b/src/org/traccar/events/GeofenceEventHandler.java
@@ -15,30 +15,23 @@
*/
package org.traccar.events;
-import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import org.traccar.BaseEventHandler;
import org.traccar.Context;
-import org.traccar.database.DataManager;
import org.traccar.database.GeofenceManager;
-import org.traccar.helper.Log;
import org.traccar.model.Device;
import org.traccar.model.Event;
import org.traccar.model.Position;
public class GeofenceEventHandler extends BaseEventHandler {
- private int suppressRepeated;
private GeofenceManager geofenceManager;
- private DataManager dataManager;
public GeofenceEventHandler() {
- suppressRepeated = Context.getConfig().getInteger("event.suppressRepeated", 60);
geofenceManager = Context.getGeofenceManager();
- dataManager = Context.getDataManager();
}
@Override
@@ -47,7 +40,7 @@ public class GeofenceEventHandler extends BaseEventHandler {
if (device == null) {
return null;
}
- if (position.getId() != device.getPositionId() || !position.getValid()) {
+ if (!Context.getDeviceManager().isLatestPosition(position) || !position.getValid()) {
return null;
}
@@ -63,29 +56,15 @@ public class GeofenceEventHandler extends BaseEventHandler {
device.setGeofenceIds(currentGeofences);
Collection<Event> events = new ArrayList<>();
- try {
- if (dataManager.getLastEvents(position.getDeviceId(),
- Event.TYPE_GEOFENCE_ENTER, suppressRepeated).isEmpty()) {
- for (long geofenceId : newGeofences) {
- Event event = new Event(Event.TYPE_GEOFENCE_ENTER, position.getDeviceId(), position.getId());
- event.setGeofenceId(geofenceId);
- events.add(event);
- }
- }
- } catch (SQLException error) {
- Log.warning(error);
+ for (long geofenceId : newGeofences) {
+ Event event = new Event(Event.TYPE_GEOFENCE_ENTER, position.getDeviceId(), position.getId());
+ event.setGeofenceId(geofenceId);
+ events.add(event);
}
- try {
- if (dataManager.getLastEvents(position.getDeviceId(),
- Event.TYPE_GEOFENCE_EXIT, suppressRepeated).isEmpty()) {
- for (long geofenceId : oldGeofences) {
- Event event = new Event(Event.TYPE_GEOFENCE_EXIT, position.getDeviceId(), position.getId());
- event.setGeofenceId(geofenceId);
- events.add(event);
- }
- }
- } catch (SQLException error) {
- Log.warning(error);
+ for (long geofenceId : oldGeofences) {
+ Event event = new Event(Event.TYPE_GEOFENCE_EXIT, position.getDeviceId(), position.getId());
+ event.setGeofenceId(geofenceId);
+ events.add(event);
}
return events;
}
diff --git a/src/org/traccar/events/MotionEventHandler.java b/src/org/traccar/events/MotionEventHandler.java
index 4a3d2f0f0..ddb99185f 100644
--- a/src/org/traccar/events/MotionEventHandler.java
+++ b/src/org/traccar/events/MotionEventHandler.java
@@ -15,25 +15,18 @@
*/
package org.traccar.events;
-import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;
import org.traccar.BaseEventHandler;
import org.traccar.Context;
-import org.traccar.helper.Log;
import org.traccar.model.Device;
import org.traccar.model.Event;
import org.traccar.model.Position;
public class MotionEventHandler extends BaseEventHandler {
- private static final double SPEED_THRESHOLD = 0.01;
- private int suppressRepeated;
-
- public MotionEventHandler() {
- suppressRepeated = Context.getConfig().getInteger("event.suppressRepeated", 60);
- }
+ private static final double SPEED_THRESHOLD = 0.01;
@Override
protected Collection<Event> analyzePosition(Position position) {
@@ -42,40 +35,23 @@ public class MotionEventHandler extends BaseEventHandler {
if (device == null) {
return null;
}
- if (position.getId() != device.getPositionId() || !position.getValid()) {
+ if (!Context.getDeviceManager().isLatestPosition(position) || !position.getValid()) {
return null;
}
Collection<Event> result = null;
double speed = position.getSpeed();
- boolean valid = position.getValid();
- String motion = device.getMotion();
- if (motion == null) {
- motion = Device.STATUS_STOPPED;
+ double oldSpeed = 0;
+ Position lastPosition = Context.getDeviceManager().getLastPosition(position.getDeviceId());
+ if (lastPosition != null) {
+ oldSpeed = lastPosition.getSpeed();
}
- try {
- if (valid && speed > SPEED_THRESHOLD && !motion.equals(Device.STATUS_MOVING)) {
- device.setMotion(Device.STATUS_MOVING);
- Context.getDeviceManager().updateDeviceStatus(device);
- result = new ArrayList<>();
- result.add(new Event(Event.TYPE_DEVICE_MOVING, position.getDeviceId(), position.getId()));
- } else if (valid && speed < SPEED_THRESHOLD && motion.equals(Device.STATUS_MOVING)) {
- device.setMotion(Device.STATUS_STOPPED);
- Context.getDeviceManager().updateDeviceStatus(device);
- result = new ArrayList<>();
- result.add(new Event(Event.TYPE_DEVICE_STOPPED, position.getDeviceId(), position.getId()));
- }
-
- if (result != null && !result.isEmpty()) {
- for (Event event : result) {
- if (!Context.getDataManager().getLastEvents(position.getDeviceId(),
- event.getType(), suppressRepeated).isEmpty()) {
- event = null;
- }
- }
- }
- } catch (SQLException error) {
- Log.warning(error);
+ if (speed > SPEED_THRESHOLD && oldSpeed <= SPEED_THRESHOLD) {
+ result = new ArrayList<>();
+ result.add(new Event(Event.TYPE_DEVICE_MOVING, position.getDeviceId(), position.getId()));
+ } else if (speed <= SPEED_THRESHOLD && oldSpeed > SPEED_THRESHOLD) {
+ result = new ArrayList<>();
+ result.add(new Event(Event.TYPE_DEVICE_STOPPED, position.getDeviceId(), position.getId()));
}
return result;
}
diff --git a/src/org/traccar/events/OverspeedEventHandler.java b/src/org/traccar/events/OverspeedEventHandler.java
index fd005e170..dbdb01ffb 100644
--- a/src/org/traccar/events/OverspeedEventHandler.java
+++ b/src/org/traccar/events/OverspeedEventHandler.java
@@ -15,7 +15,6 @@
*/
package org.traccar.events;
-import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;
@@ -24,17 +23,16 @@ import org.traccar.Context;
import org.traccar.model.Device;
import org.traccar.model.Event;
import org.traccar.model.Position;
-import org.traccar.helper.Log;
import org.traccar.helper.UnitsConverter;
public class OverspeedEventHandler extends BaseEventHandler {
- private double globalSpeedLimit;
- private int suppressRepeated;
+ public static final String ATTRIBUTE_SPEED_LIMIT = "speedLimit";
+
+ private boolean notRepeat;
public OverspeedEventHandler() {
- globalSpeedLimit = UnitsConverter.knotsFromKph(Context.getConfig().getInteger("event.globalSpeedLimit", 0));
- suppressRepeated = Context.getConfig().getInteger("event.suppressRepeated", 60);
+ notRepeat = Context.getConfig().getBoolean("event.overspeed.notRepeat");
}
@Override
@@ -44,24 +42,30 @@ public class OverspeedEventHandler extends BaseEventHandler {
if (device == null) {
return null;
}
- if (position.getId() != device.getPositionId() || !position.getValid()) {
+ if (!Context.getDeviceManager().isLatestPosition(position) || !position.getValid()) {
return null;
}
Collection<Event> events = new ArrayList<>();
double speed = position.getSpeed();
- boolean valid = position.getValid();
-
- if (valid && globalSpeedLimit != 0 && speed > globalSpeedLimit) {
- try {
- if (Context.getDataManager().getLastEvents(
- position.getDeviceId(), Event.TYPE_DEVICE_OVERSPEED, suppressRepeated).isEmpty()) {
- events.add(new Event(Event.TYPE_DEVICE_OVERSPEED, position.getDeviceId(), position.getId()));
- }
- } catch (SQLException error) {
- Log.warning(error);
+ double speedLimit = 0;
+ String speedLimitAttribute = Context.getDeviceManager().lookupAttribute(device.getId(), ATTRIBUTE_SPEED_LIMIT);
+ if (speedLimitAttribute != null) {
+ speedLimit = Double.parseDouble(speedLimitAttribute);
+ }
+ if (speedLimit == 0) {
+ return null;
+ }
+ double oldSpeed = 0;
+ if (notRepeat) {
+ Position lastPosition = Context.getDeviceManager().getLastPosition(position.getDeviceId());
+ if (lastPosition != null) {
+ oldSpeed = lastPosition.getSpeed();
}
-
+ }
+ speedLimit = UnitsConverter.knotsFromKph(speedLimit);
+ if (speed > speedLimit && oldSpeed <= speedLimit) {
+ events.add(new Event(Event.TYPE_DEVICE_OVERSPEED, position.getDeviceId(), position.getId()));
}
return events;
}
diff --git a/src/org/traccar/model/Device.java b/src/org/traccar/model/Device.java
index 1669aee31..e90742836 100644
--- a/src/org/traccar/model/Device.java
+++ b/src/org/traccar/model/Device.java
@@ -92,19 +92,6 @@ public class Device extends Extensible {
this.groupId = groupId;
}
- public static final String STATUS_MOVING = "moving";
- public static final String STATUS_STOPPED = "stopped";
-
- private String motion;
-
- public String getMotion() {
- return motion;
- }
-
- public void setMotion(String motion) {
- this.motion = motion;
- }
-
private List<Long> geofenceIds;
public List<Long> getGeofenceIds() {
diff --git a/src/org/traccar/model/Group.java b/src/org/traccar/model/Group.java
index 00f2b2cfc..e70b3f3d5 100644
--- a/src/org/traccar/model/Group.java
+++ b/src/org/traccar/model/Group.java
@@ -15,17 +15,7 @@
*/
package org.traccar.model;
-public class Group {
-
- private long id;
-
- public long getId() {
- return id;
- }
-
- public void setId(long id) {
- this.id = id;
- }
+public class Group extends Extensible {
private String name;
diff --git a/src/org/traccar/model/Server.java b/src/org/traccar/model/Server.java
index 270fd61fa..b1557bf8f 100644
--- a/src/org/traccar/model/Server.java
+++ b/src/org/traccar/model/Server.java
@@ -15,17 +15,7 @@
*/
package org.traccar.model;
-public class Server {
-
- private long id;
-
- public long getId() {
- return id;
- }
-
- public void setId(long id) {
- this.id = id;
- }
+public class Server extends Extensible {
private boolean registration;
diff --git a/src/org/traccar/protocol/H02ProtocolDecoder.java b/src/org/traccar/protocol/H02ProtocolDecoder.java
index 83ed4b099..bb18a68e2 100644
--- a/src/org/traccar/protocol/H02ProtocolDecoder.java
+++ b/src/org/traccar/protocol/H02ProtocolDecoder.java
@@ -80,7 +80,7 @@ public class H02ProtocolDecoder extends BaseProtocolDecoder {
}
}
- position.set(Position.KEY_IGNITION, !BitUtil.check(status, 10));
+ position.set(Position.KEY_IGNITION, BitUtil.check(status, 10));
position.set(Position.KEY_STATUS, status);
}
diff --git a/src/org/traccar/protocol/HuaShengProtocolDecoder.java b/src/org/traccar/protocol/HuaShengProtocolDecoder.java
index 6901f37fb..e7c83d3b3 100644
--- a/src/org/traccar/protocol/HuaShengProtocolDecoder.java
+++ b/src/org/traccar/protocol/HuaShengProtocolDecoder.java
@@ -38,7 +38,7 @@ public class HuaShengProtocolDecoder extends BaseProtocolDecoder {
public static final int MSG_LOGIN = 0xAA02;
public static final int MSG_LOGIN_RSP = 0xFF03;
- private static void sendResponse(Channel channel, int type, ChannelBuffer content) {
+ private static void sendResponse(Channel channel, int type, int index, ChannelBuffer content) {
if (channel != null) {
ChannelBuffer response = ChannelBuffers.dynamicBuffer();
response.writeByte(0xC0);
@@ -46,7 +46,7 @@ public class HuaShengProtocolDecoder extends BaseProtocolDecoder {
response.writeShort(12 + content.readableBytes());
response.writeShort(type);
response.writeShort(0);
- response.writeInt(1);
+ response.writeInt(index);
response.writeBytes(content);
response.writeByte(0xC0);
channel.write(response);
@@ -80,7 +80,7 @@ public class HuaShengProtocolDecoder extends BaseProtocolDecoder {
if (deviceSession != null && channel != null) {
ChannelBuffer content = ChannelBuffers.dynamicBuffer();
content.writeByte(0); // success
- sendResponse(channel, MSG_LOGIN_RSP, content);
+ sendResponse(channel, MSG_LOGIN_RSP, index, content);
}
} else {
buf.skipBytes(length);
@@ -127,9 +127,7 @@ public class HuaShengProtocolDecoder extends BaseProtocolDecoder {
buf.skipBytes(length);
}
- ChannelBuffer content = ChannelBuffers.dynamicBuffer();
- content.writeInt(index);
- sendResponse(channel, MSG_POSITION_RSP, content);
+ sendResponse(channel, MSG_POSITION_RSP, index, ChannelBuffers.dynamicBuffer());
return position;
diff --git a/src/org/traccar/protocol/OsmAndProtocolDecoder.java b/src/org/traccar/protocol/OsmAndProtocolDecoder.java
index f46511b27..c5884a4d0 100644
--- a/src/org/traccar/protocol/OsmAndProtocolDecoder.java
+++ b/src/org/traccar/protocol/OsmAndProtocolDecoder.java
@@ -17,7 +17,9 @@ package org.traccar.protocol;
import org.jboss.netty.channel.Channel;
import org.jboss.netty.handler.codec.http.DefaultHttpResponse;
+import org.jboss.netty.handler.codec.http.HttpHeaders;
import org.jboss.netty.handler.codec.http.HttpRequest;
+import org.jboss.netty.handler.codec.http.HttpResponse;
import org.jboss.netty.handler.codec.http.HttpResponseStatus;
import org.jboss.netty.handler.codec.http.HttpVersion;
import org.jboss.netty.handler.codec.http.QueryStringDecoder;
@@ -40,6 +42,14 @@ public class OsmAndProtocolDecoder extends BaseProtocolDecoder {
super(protocol);
}
+ private void sendResponse(Channel channel, HttpResponseStatus status) {
+ if (channel != null) {
+ HttpResponse response = new DefaultHttpResponse(HttpVersion.HTTP_1_1, status);
+ response.headers().add(HttpHeaders.Names.CONTENT_LENGTH, 0);
+ channel.write(response);
+ }
+ }
+
@Override
protected Object decode(
Channel channel, SocketAddress remoteAddress, Object msg) throws Exception {
@@ -63,10 +73,7 @@ public class OsmAndProtocolDecoder extends BaseProtocolDecoder {
case "deviceid":
DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, value);
if (deviceSession == null) {
- if (channel != null) {
- channel.write(
- new DefaultHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.BAD_REQUEST));
- }
+ sendResponse(channel, HttpResponseStatus.BAD_REQUEST);
return null;
}
position.setDeviceId(deviceSession.getDeviceId());
@@ -123,11 +130,14 @@ public class OsmAndProtocolDecoder extends BaseProtocolDecoder {
position.setTime(new Date());
}
- if (channel != null) {
- channel.write(new DefaultHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK));
+ DeviceSession deviceSession = getDeviceSession(channel, remoteAddress);
+ if (deviceSession != null) {
+ sendResponse(channel, HttpResponseStatus.OK);
+ return position;
+ } else {
+ sendResponse(channel, HttpResponseStatus.BAD_REQUEST);
+ return null;
}
-
- return position;
}
}
diff --git a/src/org/traccar/protocol/T55ProtocolDecoder.java b/src/org/traccar/protocol/T55ProtocolDecoder.java
index 34efc376f..1909d9bea 100644
--- a/src/org/traccar/protocol/T55ProtocolDecoder.java
+++ b/src/org/traccar/protocol/T55ProtocolDecoder.java
@@ -50,7 +50,9 @@ public class T55ProtocolDecoder extends BaseProtocolDecoder {
.expression("[^,]+")
.number(",(d+)") // satellites
.number(",(d+)") // imei
- .number(",(d+)").optional(3)
+ .number(",([01])") // ignition
+ .number(",(d+)") // fuel
+ .number(",(d+)").optional(5) // battery
.any()
.compile();
@@ -123,13 +125,18 @@ public class T55ProtocolDecoder extends BaseProtocolDecoder {
dateBuilder.setDateReverse(parser.nextInt(), parser.nextInt(), parser.nextInt());
position.setTime(dateBuilder.getDate());
- if (parser.hasNext(3)) {
+ if (parser.hasNext(5)) {
position.set(Position.KEY_SATELLITES, parser.next());
+
deviceSession = getDeviceSession(channel, remoteAddress, parser.next());
if (deviceSession == null) {
return null;
}
position.setDeviceId(deviceSession.getDeviceId());
+
+ position.set(Position.KEY_IGNITION, parser.hasNext() && parser.next().equals("1"));
+ position.set(Position.KEY_FUEL, parser.nextInt());
+ position.set(Position.KEY_BATTERY, parser.nextInt());
}
if (deviceSession != null) {