diff options
Diffstat (limited to 'src/org')
-rw-r--r-- | src/org/traccar/api/resource/GroupResource.java | 10 | ||||
-rw-r--r-- | src/org/traccar/database/DataManager.java | 94 | ||||
-rw-r--r-- | src/org/traccar/database/DeviceManager.java | 291 | ||||
-rw-r--r-- | src/org/traccar/database/GeofenceManager.java | 24 | ||||
-rw-r--r-- | src/org/traccar/database/PermissionsManager.java | 3 | ||||
-rw-r--r-- | src/org/traccar/protocol/EasyTrackProtocolDecoder.java | 4 | ||||
-rw-r--r-- | src/org/traccar/protocol/GpsGateProtocolDecoder.java | 4 | ||||
-rw-r--r-- | src/org/traccar/protocol/Gt02ProtocolDecoder.java | 13 | ||||
-rw-r--r-- | src/org/traccar/protocol/L100FrameDecoder.java | 50 | ||||
-rw-r--r-- | src/org/traccar/protocol/L100Protocol.java | 42 | ||||
-rw-r--r-- | src/org/traccar/protocol/L100ProtocolDecoder.java | 118 | ||||
-rw-r--r-- | src/org/traccar/protocol/OsmAndProtocolDecoder.java | 13 | ||||
-rw-r--r-- | src/org/traccar/protocol/Tk103ProtocolDecoder.java | 4 | ||||
-rw-r--r-- | src/org/traccar/protocol/WatchProtocolDecoder.java | 6 |
14 files changed, 424 insertions, 252 deletions
diff --git a/src/org/traccar/api/resource/GroupResource.java b/src/org/traccar/api/resource/GroupResource.java index 957f39ebe..eec28e325 100644 --- a/src/org/traccar/api/resource/GroupResource.java +++ b/src/org/traccar/api/resource/GroupResource.java @@ -43,20 +43,20 @@ public class GroupResource extends BaseResource { @QueryParam("all") boolean all, @QueryParam("userId") long userId) throws SQLException { if (all) { Context.getPermissionsManager().checkAdmin(getUserId()); - return Context.getDataManager().getAllGroups(); + return Context.getDeviceManager().getAllGroups(); } else { if (userId == 0) { userId = getUserId(); } Context.getPermissionsManager().checkUser(getUserId(), userId); - return Context.getDataManager().getGroups(userId); + return Context.getDeviceManager().getGroups(userId); } } @POST public Response add(Group entity) throws SQLException { Context.getPermissionsManager().checkReadonly(getUserId()); - Context.getDataManager().addGroup(entity); + Context.getDeviceManager().addGroup(entity); Context.getDataManager().linkGroup(getUserId(), entity.getId()); Context.getPermissionsManager().refresh(); if (Context.getGeofenceManager() != null) { @@ -70,7 +70,7 @@ public class GroupResource extends BaseResource { public Response update(@PathParam("id") long id, Group entity) throws SQLException { Context.getPermissionsManager().checkReadonly(getUserId()); Context.getPermissionsManager().checkGroup(getUserId(), id); - Context.getDataManager().updateGroup(entity); + Context.getDeviceManager().updateGroup(entity); if (Context.getGeofenceManager() != null) { Context.getGeofenceManager().refresh(); } @@ -82,7 +82,7 @@ public class GroupResource extends BaseResource { public Response remove(@PathParam("id") long id) throws SQLException { Context.getPermissionsManager().checkReadonly(getUserId()); Context.getPermissionsManager().checkGroup(getUserId(), id); - Context.getDataManager().removeGroup(id); + Context.getDeviceManager().removeGroup(id); Context.getPermissionsManager().refresh(); if (Context.getGeofenceManager() != null) { Context.getGeofenceManager().refresh(); diff --git a/src/org/traccar/database/DataManager.java b/src/org/traccar/database/DataManager.java index 710eceebb..04d0d44ea 100644 --- a/src/org/traccar/database/DataManager.java +++ b/src/org/traccar/database/DataManager.java @@ -20,16 +20,9 @@ import java.lang.reflect.Method; import java.net.URL; import java.net.URLClassLoader; import java.sql.SQLException; -import java.util.ArrayList; import java.util.Calendar; import java.util.Collection; import java.util.Date; -import java.util.HashMap; -import java.util.HashSet; -import java.util.Map; -import java.util.Set; -import java.util.concurrent.locks.ReadWriteLock; -import java.util.concurrent.locks.ReentrantReadWriteLock; import javax.naming.InitialContext; import javax.sql.DataSource; @@ -43,7 +36,6 @@ import liquibase.resource.FileSystemResourceAccessor; import liquibase.resource.ResourceAccessor; import org.traccar.Config; -import org.traccar.Context; import org.traccar.helper.Log; import org.traccar.model.Device; import org.traccar.model.DevicePermission; @@ -64,25 +56,15 @@ import com.zaxxer.hikari.HikariDataSource; public class DataManager { - public static final long DEFAULT_REFRESH_DELAY = 300; - private final Config config; private DataSource dataSource; - private final long dataRefreshDelay; - - private final ReadWriteLock groupsLock = new ReentrantReadWriteLock(); - private final Map<Long, Group> groupsById = new HashMap<>(); - private long groupsLastUpdate; - public DataManager(Config config) throws Exception { this.config = config; initDatabase(); initDatabaseSchema(); - - dataRefreshDelay = config.getLong("database.refreshDelay", DEFAULT_REFRESH_DELAY) * 1000; } public DataSource getDataSource() { @@ -131,54 +113,6 @@ public class DataManager { } } - private void updateGroupCache(boolean force) throws SQLException { - boolean needWrite; - groupsLock.readLock().lock(); - try { - needWrite = force || System.currentTimeMillis() - groupsLastUpdate > dataRefreshDelay; - } finally { - groupsLock.readLock().unlock(); - } - - if (needWrite) { - groupsLock.writeLock().lock(); - try { - if (force || System.currentTimeMillis() - groupsLastUpdate > dataRefreshDelay) { - groupsById.clear(); - for (Group group : getAllGroups()) { - groupsById.put(group.getId(), group); - } - groupsLastUpdate = System.currentTimeMillis(); - } - } finally { - groupsLock.writeLock().unlock(); - } - } - } - - public Group getGroupById(long id) { - boolean forceUpdate; - groupsLock.readLock().lock(); - try { - forceUpdate = !groupsById.containsKey(id); - } finally { - groupsLock.readLock().unlock(); - } - - try { - updateGroupCache(forceUpdate); - } catch (SQLException e) { - Log.warning(e); - } - - groupsLock.readLock().lock(); - try { - return groupsById.get(id); - } finally { - groupsLock.readLock().unlock(); - } - } - private String getQuery(String key) { String query = config.getString(key); if (query == null) { @@ -311,44 +245,16 @@ public class DataManager { .executeQuery(Group.class); } - public Collection<Group> getGroups(long userId) throws SQLException { - Collection<Group> groups = new ArrayList<>(); - for (long id : Context.getPermissionsManager().getGroupPermissions(userId)) { - groups.add(getGroupById(id)); - } - return groups; - } - - private void checkGroupCycles(Group group) { - groupsLock.readLock().lock(); - try { - Set<Long> groups = new HashSet<>(); - while (group != null) { - if (groups.contains(group.getId())) { - throw new IllegalArgumentException("Cycle in group hierarchy"); - } - groups.add(group.getId()); - group = groupsById.get(group.getGroupId()); - } - } finally { - groupsLock.readLock().unlock(); - } - } - public void addGroup(Group group) throws SQLException { - checkGroupCycles(group); group.setId(QueryBuilder.create(dataSource, getQuery("database.insertGroup"), true) .setObject(group) .executeUpdate()); - updateGroupCache(true); } public void updateGroup(Group group) throws SQLException { - checkGroupCycles(group); QueryBuilder.create(dataSource, getQuery("database.updateGroup")) .setObject(group) .executeUpdate(); - updateGroupCache(true); } public void removeGroup(long groupId) throws SQLException { diff --git a/src/org/traccar/database/DeviceManager.java b/src/org/traccar/database/DeviceManager.java index 173e68062..4dd7b41cb 100644 --- a/src/org/traccar/database/DeviceManager.java +++ b/src/org/traccar/database/DeviceManager.java @@ -18,39 +18,46 @@ package org.traccar.database; import java.sql.SQLException; import java.util.ArrayList; import java.util.Collection; -import java.util.HashMap; +import java.util.HashSet; import java.util.LinkedList; import java.util.List; import java.util.Map; +import java.util.Set; import java.util.concurrent.ConcurrentHashMap; -import java.util.concurrent.locks.ReadWriteLock; -import java.util.concurrent.locks.ReentrantReadWriteLock; +import java.util.concurrent.atomic.AtomicLong; import org.traccar.Config; import org.traccar.Context; import org.traccar.helper.Log; import org.traccar.model.Device; +import org.traccar.model.Group; import org.traccar.model.Position; public class DeviceManager implements IdentityManager { + public static final long DEFAULT_REFRESH_DELAY = 300; + private final Config config; private final DataManager dataManager; private final long dataRefreshDelay; - private final ReadWriteLock devicesLock = new ReentrantReadWriteLock(); - private final Map<Long, Device> devicesById = new HashMap<>(); - private final Map<String, Device> devicesByUniqueId = new HashMap<>(); - private long devicesLastUpdate; + private Map<Long, Device> devicesById; + private Map<String, Device> devicesByUniqueId; + private AtomicLong devicesLastUpdate = new AtomicLong(); + + private Map<Long, Group> groupsById; + private AtomicLong groupsLastUpdate = new AtomicLong(); private final Map<Long, Position> positions = new ConcurrentHashMap<>(); public DeviceManager(DataManager dataManager) { this.dataManager = dataManager; this.config = Context.getConfig(); - dataRefreshDelay = config.getLong("database.refreshDelay", DataManager.DEFAULT_REFRESH_DELAY) * 1000; + dataRefreshDelay = config.getLong("database.refreshDelay", DEFAULT_REFRESH_DELAY) * 1000; if (dataManager != null) { try { + updateGroupCache(true); + updateDeviceCache(true); for (Position position : dataManager.getLatestPositions()) { positions.put(position.getDeviceId(), position); } @@ -61,91 +68,77 @@ public class DeviceManager implements IdentityManager { } private void updateDeviceCache(boolean force) throws SQLException { - boolean needWrite; - devicesLock.readLock().lock(); - try { - needWrite = force || System.currentTimeMillis() - devicesLastUpdate > dataRefreshDelay; - } finally { - devicesLock.readLock().unlock(); - } - if (needWrite) { - devicesLock.writeLock().lock(); - try { - if (force || System.currentTimeMillis() - devicesLastUpdate > dataRefreshDelay) { - devicesById.clear(); - devicesByUniqueId.clear(); - GeofenceManager geofenceManager = Context.getGeofenceManager(); - for (Device device : dataManager.getAllDevices()) { - devicesById.put(device.getId(), device); - devicesByUniqueId.put(device.getUniqueId(), device); - if (geofenceManager != null) { - Position lastPosition = getLastPosition(device.getId()); - if (lastPosition != null) { - device.setGeofenceIds(geofenceManager.getCurrentDeviceGeofences(lastPosition)); - } + long lastUpdate = devicesLastUpdate.get(); + if ((force || System.currentTimeMillis() - lastUpdate > dataRefreshDelay) + && devicesLastUpdate.compareAndSet(lastUpdate, System.currentTimeMillis())) { + GeofenceManager geofenceManager = Context.getGeofenceManager(); + Collection<Device> databaseDevices = dataManager.getAllDevices(); + if (devicesById == null) { + devicesById = new ConcurrentHashMap<>(databaseDevices.size()); + } + if (devicesByUniqueId == null) { + devicesByUniqueId = new ConcurrentHashMap<>(databaseDevices.size()); + } + Set<Long> databaseDevicesIds = new HashSet<>(); + Set<String> databaseDevicesUniqueIds = new HashSet<>(); + for (Device device : databaseDevices) { + databaseDevicesIds.add(device.getId()); + databaseDevicesUniqueIds.add(device.getUniqueId()); + if (devicesById.containsKey(device.getId())) { + Device cachedDevice = devicesById.get(device.getId()); + cachedDevice.setName(device.getName()); + cachedDevice.setGroupId(device.getGroupId()); + cachedDevice.setAttributes(device.getAttributes()); + if (!device.getUniqueId().equals(cachedDevice.getUniqueId())) { + devicesByUniqueId.remove(cachedDevice.getUniqueId()); + devicesByUniqueId.put(device.getUniqueId(), cachedDevice); + } + cachedDevice.setUniqueId(device.getUniqueId()); + } else { + devicesById.put(device.getId(), device); + devicesByUniqueId.put(device.getUniqueId(), device); + if (geofenceManager != null) { + Position lastPosition = getLastPosition(device.getId()); + if (lastPosition != null) { + device.setGeofenceIds(geofenceManager.getCurrentDeviceGeofences(lastPosition)); } } - devicesLastUpdate = System.currentTimeMillis(); + device.setStatus(Device.STATUS_OFFLINE); + device.setMotion(Device.STATUS_STOPPED); + } + } + for (Long cachedDeviceId : devicesById.keySet()) { + if (!databaseDevicesIds.contains(cachedDeviceId)) { + devicesById.remove(cachedDeviceId); } - } finally { - devicesLock.writeLock().unlock(); } + for (String cachedDeviceUniqId : devicesByUniqueId.keySet()) { + if (!databaseDevicesUniqueIds.contains(cachedDeviceUniqId)) { + devicesByUniqueId.remove(cachedDeviceUniqId); + } + } + databaseDevicesIds.clear(); + databaseDevicesUniqueIds.clear(); } } @Override public Device getDeviceById(long id) { - boolean forceUpdate; - devicesLock.readLock().lock(); - try { - forceUpdate = !devicesById.containsKey(id); - } finally { - devicesLock.readLock().unlock(); - } - - try { - updateDeviceCache(forceUpdate); - } catch (SQLException e) { - Log.warning(e); - } - - devicesLock.readLock().lock(); - try { - return devicesById.get(id); - } finally { - devicesLock.readLock().unlock(); - } + return devicesById.get(id); } @Override public Device getDeviceByUniqueId(String uniqueId) throws SQLException { - boolean forceUpdate; - devicesLock.readLock().lock(); - try { - forceUpdate = !devicesByUniqueId.containsKey(uniqueId) && !config.getBoolean("database.ignoreUnknown"); - } finally { - devicesLock.readLock().unlock(); - } + boolean forceUpdate = !devicesByUniqueId.containsKey(uniqueId) && !config.getBoolean("database.ignoreUnknown"); updateDeviceCache(forceUpdate); - devicesLock.readLock().lock(); - try { - return devicesByUniqueId.get(uniqueId); - } finally { - devicesLock.readLock().unlock(); - } + return devicesByUniqueId.get(uniqueId); } public Collection<Device> getAllDevices() { - boolean forceUpdate; - devicesLock.readLock().lock(); - try { - forceUpdate = devicesById.isEmpty(); - } finally { - devicesLock.readLock().unlock(); - } + boolean forceUpdate = devicesById.isEmpty(); try { updateDeviceCache(forceUpdate); @@ -153,23 +146,13 @@ public class DeviceManager implements IdentityManager { Log.warning(e); } - devicesLock.readLock().lock(); - try { - return devicesById.values(); - } finally { - devicesLock.readLock().unlock(); - } + return devicesById.values(); } public Collection<Device> getDevices(long userId) throws SQLException { Collection<Device> devices = new ArrayList<>(); - devicesLock.readLock().lock(); - try { - for (long id : Context.getPermissionsManager().getDevicePermissions(userId)) { - devices.add(devicesById.get(id)); - } - } finally { - devicesLock.readLock().unlock(); + for (long id : Context.getPermissionsManager().getDevicePermissions(userId)) { + devices.add(devicesById.get(id)); } return devices; } @@ -177,56 +160,34 @@ public class DeviceManager implements IdentityManager { public void addDevice(Device device) throws SQLException { dataManager.addDevice(device); - devicesLock.writeLock().lock(); - try { - devicesById.put(device.getId(), device); - devicesByUniqueId.put(device.getUniqueId(), device); - } finally { - devicesLock.writeLock().unlock(); - } + devicesById.put(device.getId(), device); + devicesByUniqueId.put(device.getUniqueId(), device); } public void updateDevice(Device device) throws SQLException { dataManager.updateDevice(device); - devicesLock.writeLock().lock(); - try { - devicesById.put(device.getId(), device); - devicesByUniqueId.put(device.getUniqueId(), device); - } finally { - devicesLock.writeLock().unlock(); - } + devicesById.put(device.getId(), device); + devicesByUniqueId.put(device.getUniqueId(), device); } public void updateDeviceStatus(Device device) throws SQLException { dataManager.updateDeviceStatus(device); - - devicesLock.writeLock().lock(); - try { - if (devicesById.containsKey(device.getId())) { - Device cachedDevice = devicesById.get(device.getId()); - cachedDevice.setStatus(device.getStatus()); - cachedDevice.setMotion(device.getMotion()); - } - } finally { - devicesLock.writeLock().unlock(); + if (devicesById.containsKey(device.getId())) { + Device cachedDevice = devicesById.get(device.getId()); + cachedDevice.setStatus(device.getStatus()); + cachedDevice.setMotion(device.getMotion()); } } public void removeDevice(long deviceId) throws SQLException { dataManager.removeDevice(deviceId); - devicesLock.writeLock().lock(); - try { - if (devicesById.containsKey(deviceId)) { - String deviceUniqueId = devicesById.get(deviceId).getUniqueId(); - devicesById.remove(deviceId); - devicesByUniqueId.remove(deviceUniqueId); - } - } finally { - devicesLock.writeLock().unlock(); + if (devicesById.containsKey(deviceId)) { + String deviceUniqueId = devicesById.get(deviceId).getUniqueId(); + devicesById.remove(deviceId); + devicesByUniqueId.remove(deviceUniqueId); } - positions.remove(deviceId); } @@ -237,13 +198,8 @@ public class DeviceManager implements IdentityManager { dataManager.updateLatestPosition(position); - devicesLock.writeLock().lock(); - try { - if (devicesById.containsKey(position.getDeviceId())) { - devicesById.get(position.getDeviceId()).setPositionId(position.getId()); - } - } finally { - devicesLock.writeLock().unlock(); + if (devicesById.containsKey(position.getDeviceId())) { + devicesById.get(position.getDeviceId()).setPositionId(position.getId()); } positions.put(position.getDeviceId(), position); @@ -273,4 +229,85 @@ public class DeviceManager implements IdentityManager { return result; } + + private void updateGroupCache(boolean force) throws SQLException { + + long lastUpdate = groupsLastUpdate.get(); + if ((force || System.currentTimeMillis() - lastUpdate > dataRefreshDelay) + && groupsLastUpdate.compareAndSet(lastUpdate, System.currentTimeMillis())) { + Collection<Group> databaseGroups = dataManager.getAllGroups(); + if (groupsById == null) { + groupsById = new ConcurrentHashMap<>(databaseGroups.size()); + } + Set<Long> databaseGroupsIds = new HashSet<>(); + for (Group group : databaseGroups) { + databaseGroupsIds.add(group.getId()); + if (groupsById.containsKey(group.getId())) { + Group cachedGroup = groupsById.get(group.getId()); + cachedGroup.setName(group.getName()); + cachedGroup.setGroupId(group.getGroupId()); + } else { + groupsById.put(group.getId(), group); + } + } + for (Long cachedGroupId : groupsById.keySet()) { + if (!databaseGroupsIds.contains(cachedGroupId)) { + devicesById.remove(cachedGroupId); + } + } + databaseGroupsIds.clear(); + } + } + + public Group getGroupById(long id) { + return groupsById.get(id); + } + + public Collection<Group> getAllGroups() { + boolean forceUpdate = groupsById.isEmpty(); + + try { + updateGroupCache(forceUpdate); + } catch (SQLException e) { + Log.warning(e); + } + + return groupsById.values(); + } + + public Collection<Group> getGroups(long userId) throws SQLException { + Collection<Group> groups = new ArrayList<>(); + for (long id : Context.getPermissionsManager().getGroupPermissions(userId)) { + groups.add(getGroupById(id)); + } + return groups; + } + + private void checkGroupCycles(Group group) { + Set<Long> groups = new HashSet<>(); + while (group != null) { + if (groups.contains(group.getId())) { + throw new IllegalArgumentException("Cycle in group hierarchy"); + } + groups.add(group.getId()); + group = groupsById.get(group.getGroupId()); + } + } + + public void addGroup(Group group) throws SQLException { + checkGroupCycles(group); + dataManager.addGroup(group); + groupsById.put(group.getId(), group); + } + + public void updateGroup(Group group) throws SQLException { + checkGroupCycles(group); + dataManager.updateGroup(group); + groupsById.put(group.getId(), group); + } + + public void removeGroup(long groupId) throws SQLException { + dataManager.removeGroup(groupId); + groupsById.remove(groupId); + } } diff --git a/src/org/traccar/database/GeofenceManager.java b/src/org/traccar/database/GeofenceManager.java index dc31172b9..74dff70f4 100644 --- a/src/org/traccar/database/GeofenceManager.java +++ b/src/org/traccar/database/GeofenceManager.java @@ -155,31 +155,41 @@ public class GeofenceManager { public final void refresh() { if (dataManager != null) { try { + + Collection<GroupGeofence> databaseGroupGeofences = dataManager.getGroupGeofences(); groupGeofencesLock.writeLock().lock(); - deviceGeofencesLock.writeLock().lock(); try { groupGeofences.clear(); - for (GroupGeofence groupGeofence : dataManager.getGroupGeofences()) { + for (GroupGeofence groupGeofence : databaseGroupGeofences) { getGroupGeofences(groupGeofence.getGroupId()).add(groupGeofence.getGeofenceId()); } + } finally { + groupGeofencesLock.writeLock().unlock(); + } + Collection<DeviceGeofence> databaseDeviceGeofences = dataManager.getDeviceGeofences(); + Collection<Device> allDevices = Context.getDeviceManager().getAllDevices(); + + groupGeofencesLock.readLock().lock(); + deviceGeofencesLock.writeLock().lock(); + try { deviceGeofences.clear(); deviceGeofencesWithGroups.clear(); - for (DeviceGeofence deviceGeofence : dataManager.getDeviceGeofences()) { + for (DeviceGeofence deviceGeofence : databaseDeviceGeofences) { getDeviceGeofences(deviceGeofences, deviceGeofence.getDeviceId()) .add(deviceGeofence.getGeofenceId()); getDeviceGeofences(deviceGeofencesWithGroups, deviceGeofence.getDeviceId()) .add(deviceGeofence.getGeofenceId()); } - for (Device device : Context.getDeviceManager().getAllDevices()) { + for (Device device : allDevices) { long groupId = device.getGroupId(); while (groupId != 0) { getDeviceGeofences(deviceGeofencesWithGroups, device.getId()).addAll(getGroupGeofences(groupId)); - if (dataManager.getGroupById(groupId) != null) { - groupId = dataManager.getGroupById(groupId).getGroupId(); + if (Context.getDeviceManager().getGroupById(groupId) != null) { + groupId = Context.getDeviceManager().getGroupById(groupId).getGroupId(); } else { groupId = 0; } @@ -204,8 +214,8 @@ public class GeofenceManager { } } finally { - groupGeofencesLock.writeLock().unlock(); deviceGeofencesLock.writeLock().unlock(); + groupGeofencesLock.readLock().unlock(); } } catch (SQLException error) { diff --git a/src/org/traccar/database/PermissionsManager.java b/src/org/traccar/database/PermissionsManager.java index 5a15375b4..d786dcc4e 100644 --- a/src/org/traccar/database/PermissionsManager.java +++ b/src/org/traccar/database/PermissionsManager.java @@ -78,7 +78,8 @@ public class PermissionsManager { users.put(user.getId(), user); } - GroupTree groupTree = new GroupTree(dataManager.getAllGroups(), Context.getDeviceManager().getAllDevices()); + GroupTree groupTree = new GroupTree(Context.getDeviceManager().getAllGroups(), + Context.getDeviceManager().getAllDevices()); for (GroupPermission permission : dataManager.getGroupPermissions()) { Set<Long> userGroupPermissions = getGroupPermissions(permission.getUserId()); Set<Long> userDevicePermissions = getDevicePermissions(permission.getUserId()); diff --git a/src/org/traccar/protocol/EasyTrackProtocolDecoder.java b/src/org/traccar/protocol/EasyTrackProtocolDecoder.java index fa792d7b5..41f395fd9 100644 --- a/src/org/traccar/protocol/EasyTrackProtocolDecoder.java +++ b/src/org/traccar/protocol/EasyTrackProtocolDecoder.java @@ -86,13 +86,13 @@ public class EasyTrackProtocolDecoder extends BaseProtocolDecoder { .setTime(parser.nextInt(16), parser.nextInt(16), parser.nextInt(16)); position.setTime(dateBuilder.getDate()); - if (BitUtil.check(parser.nextInt(16), 7)) { + if (BitUtil.check(parser.nextInt(16), 3)) { position.setLatitude(-parser.nextInt(16) / 600000.0); } else { position.setLatitude(parser.nextInt(16) / 600000.0); } - if (BitUtil.check(parser.nextInt(16), 7)) { + if (BitUtil.check(parser.nextInt(16), 3)) { position.setLongitude(-parser.nextInt(16) / 600000.0); } else { position.setLongitude(parser.nextInt(16) / 600000.0); diff --git a/src/org/traccar/protocol/GpsGateProtocolDecoder.java b/src/org/traccar/protocol/GpsGateProtocolDecoder.java index 1cb0ddfde..afc17a9f7 100644 --- a/src/org/traccar/protocol/GpsGateProtocolDecoder.java +++ b/src/org/traccar/protocol/GpsGateProtocolDecoder.java @@ -58,10 +58,10 @@ public class GpsGateProtocolDecoder extends BaseProtocolDecoder { .expression("([EW]),") .number("(d+.?d*),") // altitude .number("(d+.?d*),") // speed - .number("(d+.?d*),") // course + .number("(d+.?d*)?,") // course .number("(dd)(dd)(dd),") // date (ddmmyy) .number("(dd)(dd)(dd).?(d+)?,") // time - .expression("([01]),") // validity + .expression("([01])") // validity .any() .compile(); diff --git a/src/org/traccar/protocol/Gt02ProtocolDecoder.java b/src/org/traccar/protocol/Gt02ProtocolDecoder.java index d93b93b83..dea1416ac 100644 --- a/src/org/traccar/protocol/Gt02ProtocolDecoder.java +++ b/src/org/traccar/protocol/Gt02ProtocolDecoder.java @@ -1,5 +1,5 @@ /* - * Copyright 2012 - 2015 Anton Tananaev (anton.tananaev@gmail.com) + * Copyright 2012 - 2016 Anton Tananaev (anton.tananaev@gmail.com) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -26,6 +26,7 @@ import org.traccar.helper.UnitsConverter; import org.traccar.model.Position; import java.net.SocketAddress; +import java.nio.charset.StandardCharsets; public class Gt02ProtocolDecoder extends BaseProtocolDecoder { @@ -33,8 +34,9 @@ public class Gt02ProtocolDecoder extends BaseProtocolDecoder { super(protocol); } - public static final int MSG_HEARTBEAT = 0x1A; public static final int MSG_DATA = 0x10; + public static final int MSG_HEARTBEAT = 0x1A; + public static final int MSG_RESPONSE = 0x1C; @Override protected Object decode( @@ -102,6 +104,13 @@ public class Gt02ProtocolDecoder extends BaseProtocolDecoder { position.setLatitude(latitude); position.setLongitude(longitude); + } else if (type == MSG_RESPONSE) { + + getLastLocation(position, null); + + position.set(Position.KEY_RESULT, + buf.readBytes(buf.readUnsignedByte()).toString(StandardCharsets.US_ASCII)); + } else { return null; diff --git a/src/org/traccar/protocol/L100FrameDecoder.java b/src/org/traccar/protocol/L100FrameDecoder.java new file mode 100644 index 000000000..92af255dd --- /dev/null +++ b/src/org/traccar/protocol/L100FrameDecoder.java @@ -0,0 +1,50 @@ +/* + * Copyright 2016 Anton Tananaev (anton.tananaev@gmail.com) + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.traccar.protocol; + +import org.jboss.netty.buffer.ChannelBuffer; +import org.jboss.netty.channel.Channel; +import org.jboss.netty.channel.ChannelHandlerContext; +import org.jboss.netty.handler.codec.frame.FrameDecoder; + +public class L100FrameDecoder extends FrameDecoder { + + @Override + protected Object decode( + ChannelHandlerContext ctx, Channel channel, ChannelBuffer buf) throws Exception { + + if (buf.readableBytes() < 80) { + return null; + } + + int index = buf.indexOf(buf.readerIndex(), buf.writerIndex(), (byte) 0x02); + if (index == -1) { + index = buf.indexOf(buf.readerIndex(), buf.writerIndex(), (byte) 0x04); + if (index == -1) { + return null; + } + } + + index += 2; // checksum + + if (buf.readableBytes() >= index - buf.readerIndex()) { + return buf.readBytes(index - buf.readerIndex()); + } + + return null; + } + +} diff --git a/src/org/traccar/protocol/L100Protocol.java b/src/org/traccar/protocol/L100Protocol.java new file mode 100644 index 000000000..95a4dce68 --- /dev/null +++ b/src/org/traccar/protocol/L100Protocol.java @@ -0,0 +1,42 @@ +/* + * Copyright 2016 Anton Tananaev (anton.tananaev@gmail.com) + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.traccar.protocol; + +import org.jboss.netty.bootstrap.ServerBootstrap; +import org.jboss.netty.channel.ChannelPipeline; +import org.traccar.BaseProtocol; +import org.traccar.TrackerServer; + +import java.util.List; + +public class L100Protocol extends BaseProtocol { + + public L100Protocol() { + super("l100"); + } + + @Override + public void initTrackerServers(List<TrackerServer> serverList) { + serverList.add(new TrackerServer(new ServerBootstrap(), this.getName()) { + @Override + protected void addSpecificHandlers(ChannelPipeline pipeline) { + pipeline.addLast("frameDecoder", new L100FrameDecoder()); + pipeline.addLast("objectDecoder", new L100ProtocolDecoder(L100Protocol.this)); + } + }); + } + +} diff --git a/src/org/traccar/protocol/L100ProtocolDecoder.java b/src/org/traccar/protocol/L100ProtocolDecoder.java new file mode 100644 index 000000000..ff0687751 --- /dev/null +++ b/src/org/traccar/protocol/L100ProtocolDecoder.java @@ -0,0 +1,118 @@ +/* + * Copyright 2016 Anton Tananaev (anton.tananaev@gmail.com) + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.traccar.protocol; + +import org.jboss.netty.buffer.ChannelBuffer; +import org.jboss.netty.channel.Channel; +import org.traccar.BaseProtocolDecoder; +import org.traccar.DeviceSession; +import org.traccar.helper.DateBuilder; +import org.traccar.helper.Parser; +import org.traccar.helper.PatternBuilder; +import org.traccar.model.Position; + +import java.net.SocketAddress; +import java.nio.charset.StandardCharsets; +import java.util.regex.Pattern; + +public class L100ProtocolDecoder extends BaseProtocolDecoder { + + public L100ProtocolDecoder(L100Protocol protocol) { + super(protocol); + } + + private static final Pattern PATTERN = new PatternBuilder() + .text("ATL") + .number("(d{15}),") // imei + .text("$GPRMC,") + .number("(dd)(dd)(dd).ddd,") // time + .expression("([AV]),") // validity + .number("(dd)(dd.d+),") // latitude + .expression("([NS]),") + .number("(ddd)(dd.d+),") // longitude + .expression("([EW]),") + .number("(d+.?d*)?,") // speed + .number("(d+.?d*)?,") // course + .number("(dd)(dd)(dd),") // date + .any() + .text("#") + .number("([01]+),") // io status + .number("(d+.d+|N.C),") // adc + .expression("[^,]*,") // reserved + .expression("[^,]*,") // reserved + .number("(d+.d+),") // odometer + .number("(d+.d+),") // temperature + .number("(d+.d+),") // battery + .number("(d+),") // gsm + .number("(d+),") // mcc + .number("(d+),") // mnc + .number("(d+),") // lac + .number("(d+)") // cid + .text("ATL") + .compile(); + + @Override + protected Object decode( + Channel channel, SocketAddress remoteAddress, Object msg) throws Exception { + + ChannelBuffer buf = (ChannelBuffer) msg; + + buf.readUnsignedByte(); // start marker + buf.readUnsignedByte(); // type + + String sentence = buf.readBytes(buf.readableBytes() - 2).toString(StandardCharsets.US_ASCII); + + Parser parser = new Parser(PATTERN, sentence); + if (!parser.matches()) { + return null; + } + + Position position = new Position(); + position.setProtocol(getProtocolName()); + + DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, parser.next()); + if (deviceSession == null) { + return null; + } + position.setDeviceId(deviceSession.getDeviceId()); + + DateBuilder dateBuilder = new DateBuilder() + .setTime(parser.nextInt(), parser.nextInt(), parser.nextInt()); + + position.setValid(parser.next().equals("A")); + position.setLatitude(parser.nextCoordinate()); + position.setLongitude(parser.nextCoordinate()); + position.setSpeed(parser.nextDouble()); + position.setCourse(parser.nextDouble()); + + dateBuilder.setDateReverse(parser.nextInt(), parser.nextInt(), parser.nextInt()); + position.setTime(dateBuilder.getDate()); + + position.set(Position.KEY_STATUS, parser.next()); + position.set(Position.PREFIX_ADC + 1, parser.next()); + position.set(Position.KEY_ODOMETER, parser.nextDouble()); + position.set(Position.PREFIX_TEMP + 1, parser.nextDouble()); + position.set(Position.KEY_BATTERY, parser.nextDouble()); + position.set(Position.KEY_GSM, parser.nextInt()); + position.set(Position.KEY_MCC, parser.nextInt()); + position.set(Position.KEY_MNC, parser.nextInt()); + position.set(Position.KEY_LAC, parser.nextInt()); + position.set(Position.KEY_CID, parser.nextInt()); + + return position; + } + +} diff --git a/src/org/traccar/protocol/OsmAndProtocolDecoder.java b/src/org/traccar/protocol/OsmAndProtocolDecoder.java index cfa8990dd..f46511b27 100644 --- a/src/org/traccar/protocol/OsmAndProtocolDecoder.java +++ b/src/org/traccar/protocol/OsmAndProtocolDecoder.java @@ -16,10 +16,8 @@ package org.traccar.protocol; import org.jboss.netty.channel.Channel; -import org.jboss.netty.channel.ChannelFutureListener; import org.jboss.netty.handler.codec.http.DefaultHttpResponse; 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; @@ -50,8 +48,7 @@ public class OsmAndProtocolDecoder extends BaseProtocolDecoder { QueryStringDecoder decoder = new QueryStringDecoder(request.getUri()); Map<String, List<String>> params = decoder.getParameters(); if (params.isEmpty()) { - decoder = new QueryStringDecoder( - request.getContent().toString(StandardCharsets.US_ASCII), false); + decoder = new QueryStringDecoder(request.getContent().toString(StandardCharsets.US_ASCII), false); params = decoder.getParameters(); } @@ -66,6 +63,10 @@ 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)); + } return null; } position.setDeviceId(deviceSession.getDeviceId()); @@ -123,9 +124,7 @@ public class OsmAndProtocolDecoder extends BaseProtocolDecoder { } if (channel != null) { - HttpResponse response = new DefaultHttpResponse( - HttpVersion.HTTP_1_1, HttpResponseStatus.OK); - channel.write(response).addListener(ChannelFutureListener.CLOSE); + channel.write(new DefaultHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK)); } return position; diff --git a/src/org/traccar/protocol/Tk103ProtocolDecoder.java b/src/org/traccar/protocol/Tk103ProtocolDecoder.java index a0998d50d..a76c208b5 100644 --- a/src/org/traccar/protocol/Tk103ProtocolDecoder.java +++ b/src/org/traccar/protocol/Tk103ProtocolDecoder.java @@ -118,12 +118,12 @@ public class Tk103ProtocolDecoder extends BaseProtocolDecoder { int battery = parser.nextInt(); if (battery != 65535) { - position.set(Position.KEY_BATTERY, battery); + position.set(Position.KEY_BATTERY, battery * 0.01); } int power = parser.nextInt(); if (power != 65535) { - position.set(Position.KEY_POWER, power); + position.set(Position.KEY_POWER, power * 0.1); } return position; diff --git a/src/org/traccar/protocol/WatchProtocolDecoder.java b/src/org/traccar/protocol/WatchProtocolDecoder.java index ca63e701e..326552e7f 100644 --- a/src/org/traccar/protocol/WatchProtocolDecoder.java +++ b/src/org/traccar/protocol/WatchProtocolDecoder.java @@ -1,5 +1,5 @@ /* - * Copyright 2015 Anton Tananaev (anton.tananaev@gmail.com) + * Copyright 2015 - 2016 Anton Tananaev (anton.tananaev@gmail.com) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -47,9 +47,9 @@ public class WatchProtocolDecoder extends BaseProtocolDecoder { .number("(dd)(dd)(dd),") // date (ddmmyy) .number("(dd)(dd)(dd),") // time .expression("([AV]),") // validity - .number(" *-?(d+.d+),") // latitude + .number(" *(-?d+.d+),") // latitude .expression("([NS]),") - .number(" *-?(d+.d+),") // longitude + .number(" *(-?d+.d+),") // longitude .expression("([EW])?,") .number("(d+.d+),") // speed .number("(d+.?d*),") // course |