From b91d1b41343a1d3d8625c1dc42c9c7d2244fb903 Mon Sep 17 00:00:00 2001 From: Abyss777 Date: Tue, 19 Jul 2016 16:54:13 +0500 Subject: Split locks in GeofenceManager and try to avoid crosslocks with devices. --- src/org/traccar/database/GeofenceManager.java | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/src/org/traccar/database/GeofenceManager.java b/src/org/traccar/database/GeofenceManager.java index dc31172b9..2994920d9 100644 --- a/src/org/traccar/database/GeofenceManager.java +++ b/src/org/traccar/database/GeofenceManager.java @@ -155,25 +155,34 @@ public class GeofenceManager { public final void refresh() { if (dataManager != null) { try { + + Collection 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 databaseDeviceGeofences = dataManager.getDeviceGeofences(); + Collection allDevices = Context.getDeviceManager().getAllDevices(); + + 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, @@ -204,7 +213,6 @@ public class GeofenceManager { } } finally { - groupGeofencesLock.writeLock().unlock(); deviceGeofencesLock.writeLock().unlock(); } -- cgit v1.2.3 From 6ef76c9e22ada4ed5d89d0a3bd43d759b56bebe0 Mon Sep 17 00:00:00 2001 From: Abyss777 Date: Tue, 19 Jul 2016 17:03:00 +0500 Subject: Missed groupGeofences read lock --- src/org/traccar/database/GeofenceManager.java | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/org/traccar/database/GeofenceManager.java b/src/org/traccar/database/GeofenceManager.java index 2994920d9..930008082 100644 --- a/src/org/traccar/database/GeofenceManager.java +++ b/src/org/traccar/database/GeofenceManager.java @@ -170,6 +170,7 @@ public class GeofenceManager { Collection databaseDeviceGeofences = dataManager.getDeviceGeofences(); Collection allDevices = Context.getDeviceManager().getAllDevices(); + groupGeofencesLock.readLock().lock(); deviceGeofencesLock.writeLock().lock(); try { deviceGeofences.clear(); @@ -214,6 +215,7 @@ public class GeofenceManager { } finally { deviceGeofencesLock.writeLock().unlock(); + groupGeofencesLock.readLock().unlock(); } } catch (SQLException error) { -- cgit v1.2.3 From 8e4d44e045c6e0c800b3b353f6f55c64d731428c Mon Sep 17 00:00:00 2001 From: Anton Tananaev Date: Wed, 20 Jul 2016 10:10:59 +1200 Subject: Adjust TK103 voltage values --- src/org/traccar/protocol/Tk103ProtocolDecoder.java | 4 ++-- test/org/traccar/protocol/Tk103ProtocolDecoderTest.java | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) 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/test/org/traccar/protocol/Tk103ProtocolDecoderTest.java b/test/org/traccar/protocol/Tk103ProtocolDecoderTest.java index bd54a4ade..b15d7894e 100644 --- a/test/org/traccar/protocol/Tk103ProtocolDecoderTest.java +++ b/test/org/traccar/protocol/Tk103ProtocolDecoderTest.java @@ -23,7 +23,7 @@ public class Tk103ProtocolDecoderTest extends ProtocolTest { "(088047194605BZ00,510,010,36e6,932c,43,36e6,766b,36,36e6,7668,32")); verifyAttributes(decoder, text( - "(013632651491,ZC20,040613,040137,6,42,112,0")); + "(013632651491,ZC20,040613,040137,6,421,112,0")); verifyAttributes(decoder, text( "(864768010159785,ZC20,291015,030413,3,362,65535,255")); -- cgit v1.2.3 From 32a7fe7eb22d435906b65f91c37751ece4fea828 Mon Sep 17 00:00:00 2001 From: Abyss777 Date: Wed, 20 Jul 2016 10:45:52 +0500 Subject: - Move groups related actions to DeviceManager - Removed cache updates from getXxxxById --- .checkstyle | 10 ++ src/org/traccar/api/resource/GroupResource.java | 10 +- src/org/traccar/database/DataManager.java | 94 --------------- src/org/traccar/database/DeviceManager.java | 140 ++++++++++++++++++++--- src/org/traccar/database/GeofenceManager.java | 4 +- src/org/traccar/database/PermissionsManager.java | 3 +- 6 files changed, 144 insertions(+), 117 deletions(-) create mode 100644 .checkstyle diff --git a/.checkstyle b/.checkstyle new file mode 100644 index 000000000..0b4b1b908 --- /dev/null +++ b/.checkstyle @@ -0,0 +1,10 @@ + + + + + + + + + + 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 0aecaf2e7..be77a3d36 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 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 getGroups(long userId) throws SQLException { - Collection 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 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..b6cb8c65f 100644 --- a/src/org/traccar/database/DeviceManager.java +++ b/src/org/traccar/database/DeviceManager.java @@ -19,9 +19,11 @@ 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; @@ -30,10 +32,13 @@ 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; @@ -43,14 +48,20 @@ public class DeviceManager implements IdentityManager { private final Map devicesByUniqueId = new HashMap<>(); private long devicesLastUpdate; + private final ReadWriteLock groupsLock = new ReentrantReadWriteLock(); + private final Map groupsById = new HashMap<>(); + private long groupsLastUpdate; + private final Map 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 { + updateDeviceCache(true); + updateGroupCache(true); for (Position position : dataManager.getLatestPositions()) { positions.put(position.getDeviceId(), position); } @@ -96,20 +107,6 @@ public class DeviceManager implements IdentityManager { @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); @@ -273,4 +270,117 @@ public class DeviceManager implements IdentityManager { return result; } + + 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 : dataManager.getAllGroups()) { + groupsById.put(group.getId(), group); + } + groupsLastUpdate = System.currentTimeMillis(); + } + } finally { + groupsLock.writeLock().unlock(); + } + } + } + + public Group getGroupById(long id) { + groupsLock.readLock().lock(); + try { + return groupsById.get(id); + } finally { + groupsLock.readLock().unlock(); + } + } + + public Collection getAllGroups() { + boolean forceUpdate; + groupsLock.readLock().lock(); + try { + forceUpdate = groupsById.isEmpty(); + } finally { + groupsLock.readLock().unlock(); + } + + try { + updateGroupCache(forceUpdate); + } catch (SQLException e) { + Log.warning(e); + } + + groupsLock.readLock().lock(); + try { + return groupsById.values(); + } finally { + groupsLock.readLock().unlock(); + } + } + + public Collection getGroups(long userId) throws SQLException { + Collection 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 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); + dataManager.addGroup(group); + groupsLock.writeLock().lock(); + try { + groupsById.put(group.getId(), group); + } finally { + groupsLock.writeLock().unlock(); + } + } + + public void updateGroup(Group group) throws SQLException { + checkGroupCycles(group); + dataManager.updateGroup(group); + groupsLock.writeLock().lock(); + try { + groupsById.put(group.getId(), group); + } finally { + groupsLock.writeLock().unlock(); + } + } + + public void removeGroup(long groupId) throws SQLException { + dataManager.removeGroup(groupId); + groupsLock.writeLock().lock(); + try { + groupsById.remove(groupId); + } finally { + groupsLock.writeLock().unlock(); + } + } } diff --git a/src/org/traccar/database/GeofenceManager.java b/src/org/traccar/database/GeofenceManager.java index 930008082..74dff70f4 100644 --- a/src/org/traccar/database/GeofenceManager.java +++ b/src/org/traccar/database/GeofenceManager.java @@ -188,8 +188,8 @@ public class GeofenceManager { 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; } 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 userGroupPermissions = getGroupPermissions(permission.getUserId()); Set userDevicePermissions = getDevicePermissions(permission.getUserId()); -- cgit v1.2.3 From 56784bd167ddf0bbc124269519b7a93b346bd5cc Mon Sep 17 00:00:00 2001 From: Abyss777 Date: Wed, 20 Jul 2016 15:57:15 +0500 Subject: Change order cache initialization --- .checkstyle | 10 ---------- .gitignore | 1 + src/org/traccar/database/DeviceManager.java | 2 +- 3 files changed, 2 insertions(+), 11 deletions(-) delete mode 100644 .checkstyle diff --git a/.checkstyle b/.checkstyle deleted file mode 100644 index 0b4b1b908..000000000 --- a/.checkstyle +++ /dev/null @@ -1,10 +0,0 @@ - - - - - - - - - - diff --git a/.gitignore b/.gitignore index 4bb6eefb9..00818a38a 100644 --- a/.gitignore +++ b/.gitignore @@ -6,3 +6,4 @@ nbactions.xml .idea *.iml .DS_Store +.checkstyle diff --git a/src/org/traccar/database/DeviceManager.java b/src/org/traccar/database/DeviceManager.java index b6cb8c65f..43d83d078 100644 --- a/src/org/traccar/database/DeviceManager.java +++ b/src/org/traccar/database/DeviceManager.java @@ -60,8 +60,8 @@ public class DeviceManager implements IdentityManager { dataRefreshDelay = config.getLong("database.refreshDelay", DEFAULT_REFRESH_DELAY) * 1000; if (dataManager != null) { try { - updateDeviceCache(true); updateGroupCache(true); + updateDeviceCache(true); for (Position position : dataManager.getLatestPositions()) { positions.put(position.getDeviceId(), position); } -- cgit v1.2.3 From d6fba0cc479f32f57b3359c7b87344918339a650 Mon Sep 17 00:00:00 2001 From: Anton Tananaev Date: Wed, 20 Jul 2016 23:35:07 +1200 Subject: Revert temporary L100 removal This reverts commit 21a8fb81a0aa88a7bc1cb6804da034ecc9a79bcd. --- src/org/traccar/protocol/L100FrameDecoder.java | 50 +++++++++ src/org/traccar/protocol/L100Protocol.java | 44 ++++++++ src/org/traccar/protocol/L100ProtocolDecoder.java | 116 +++++++++++++++++++++ .../org/traccar/protocol/L100FrameDecoderTest.java | 24 +++++ .../traccar/protocol/L100ProtocolDecoderTest.java | 18 ++++ 5 files changed, 252 insertions(+) create mode 100644 src/org/traccar/protocol/L100FrameDecoder.java create mode 100644 src/org/traccar/protocol/L100Protocol.java create mode 100644 src/org/traccar/protocol/L100ProtocolDecoder.java create mode 100644 test/org/traccar/protocol/L100FrameDecoderTest.java create mode 100644 test/org/traccar/protocol/L100ProtocolDecoderTest.java 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..f866b5d69 --- /dev/null +++ b/src/org/traccar/protocol/L100Protocol.java @@ -0,0 +1,44 @@ +/* + * 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.ConnectionlessBootstrap; +import org.jboss.netty.bootstrap.ServerBootstrap; +import org.jboss.netty.channel.ChannelPipeline; +import org.traccar.BaseProtocol; +import org.traccar.TrackerServer; +import org.traccar.model.Command; + +import java.util.List; + +public class L100Protocol extends BaseProtocol { + + public L100Protocol() { + super("l100"); + } + + @Override + public void initTrackerServers(List 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..b7e2e91cc --- /dev/null +++ b/src/org/traccar/protocol/L100ProtocolDecoder.java @@ -0,0 +1,116 @@ +/* + * 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.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()); + + if (!identify(parser.next(), channel, remoteAddress)) { + return null; + } + position.setDeviceId(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/test/org/traccar/protocol/L100FrameDecoderTest.java b/test/org/traccar/protocol/L100FrameDecoderTest.java new file mode 100644 index 000000000..f474fa678 --- /dev/null +++ b/test/org/traccar/protocol/L100FrameDecoderTest.java @@ -0,0 +1,24 @@ +package org.traccar.protocol; + +import org.junit.Assert; +import org.junit.Test; +import org.traccar.ProtocolTest; + +public class L100FrameDecoderTest extends ProtocolTest { + + @Test + public void testDecode() throws Exception { + + L100FrameDecoder decoder = new L100FrameDecoder(); + + Assert.assertEquals( + binary("200141544c3335363839353033373533333734352c244750524d432c3131313731392e3030302c412c323833382e303034352c4e2c30373731332e333730372c452c302e30302c2c3132303831302c2c2c412a3735242c2330313130303131313030313031302c4e2e432c4e2e432c4e2e432c31323334352e36372c33312e342c342e322c32312c4d43432c4d4e432c4c41432c43656c6c494441544c027a"), + decoder.decode(null, null, binary("200141544c3335363839353033373533333734352c244750524d432c3131313731392e3030302c412c323833382e303034352c4e2c30373731332e333730372c452c302e30302c2c3132303831302c2c2c412a3735242c2330313130303131313030313031302c4e2e432c4e2e432c4e2e432c31323334352e36372c33312e342c342e322c32312c4d43432c4d4e432c4c41432c43656c6c494441544c027a"))); + + Assert.assertEquals( + binary("200341544c3335363839353033373533333734352c244750524d432c3131313731392e3030302c412c323833382e303034352c4e2c30373731332e333730372c452c302e30302c2c3132303831302c2c2c412a3735244c4f432c436f6e6e61756768742043697263757320c2a0436f6e6e617567687420506c61636520c2a04e65772044656c686920c2a044656c6869c2a0496e6469612c2330313130303130313130313031302c322e332c33352e36372c38302c31323334352e36372c33312e342c342e322c32312c4d43432c4d4e432c4c41432c43656c6c494441544c047a"), + decoder.decode(null, null, binary("200341544c3335363839353033373533333734352c244750524d432c3131313731392e3030302c412c323833382e303034352c4e2c30373731332e333730372c452c302e30302c2c3132303831302c2c2c412a3735244c4f432c436f6e6e61756768742043697263757320c2a0436f6e6e617567687420506c61636520c2a04e65772044656c686920c2a044656c6869c2a0496e6469612c2330313130303130313130313031302c322e332c33352e36372c38302c31323334352e36372c33312e342c342e322c32312c4d43432c4d4e432c4c41432c43656c6c494441544c047a"))); + + } + +} diff --git a/test/org/traccar/protocol/L100ProtocolDecoderTest.java b/test/org/traccar/protocol/L100ProtocolDecoderTest.java new file mode 100644 index 000000000..07e72140d --- /dev/null +++ b/test/org/traccar/protocol/L100ProtocolDecoderTest.java @@ -0,0 +1,18 @@ +package org.traccar.protocol; + +import org.junit.Test; +import org.traccar.ProtocolTest; + +public class L100ProtocolDecoderTest extends ProtocolTest { + + @Test + public void testDecode() throws Exception { + + L100ProtocolDecoder decoder = new L100ProtocolDecoder(new L100Protocol()); + + verifyPosition(decoder, binary( + "200141544c3335363839353033373533333734352c244750524d432c3131313731392e3030302c412c323833382e303034352c4e2c30373731332e333730372c452c302e30302c2c3132303831302c2c2c412a37352c2330313130303131313030313031302c4e2e432c4e2e432c4e2e432c31323334352e36372c33312e342c342e322c32312c3130302c3030302c3030303030312c303030303041544c027a")); + + } + +} -- cgit v1.2.3 From dfbadf146bc23231ffb7d3bc03d5933e43acf77f Mon Sep 17 00:00:00 2001 From: Anton Tananaev Date: Wed, 20 Jul 2016 23:37:13 +1200 Subject: Update L100 protocol decoder --- src/org/traccar/protocol/L100ProtocolDecoder.java | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/org/traccar/protocol/L100ProtocolDecoder.java b/src/org/traccar/protocol/L100ProtocolDecoder.java index b7e2e91cc..ff0687751 100644 --- a/src/org/traccar/protocol/L100ProtocolDecoder.java +++ b/src/org/traccar/protocol/L100ProtocolDecoder.java @@ -18,6 +18,7 @@ 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; @@ -82,10 +83,11 @@ public class L100ProtocolDecoder extends BaseProtocolDecoder { Position position = new Position(); position.setProtocol(getProtocolName()); - if (!identify(parser.next(), channel, remoteAddress)) { + DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, parser.next()); + if (deviceSession == null) { return null; } - position.setDeviceId(getDeviceId()); + position.setDeviceId(deviceSession.getDeviceId()); DateBuilder dateBuilder = new DateBuilder() .setTime(parser.nextInt(), parser.nextInt(), parser.nextInt()); -- cgit v1.2.3 From b762d087c09533cc66330d546b1832da2816a400 Mon Sep 17 00:00:00 2001 From: Anton Tananaev Date: Wed, 20 Jul 2016 23:41:34 +1200 Subject: Remove unused imports --- src/org/traccar/protocol/L100Protocol.java | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/org/traccar/protocol/L100Protocol.java b/src/org/traccar/protocol/L100Protocol.java index f866b5d69..95a4dce68 100644 --- a/src/org/traccar/protocol/L100Protocol.java +++ b/src/org/traccar/protocol/L100Protocol.java @@ -15,12 +15,10 @@ */ package org.traccar.protocol; -import org.jboss.netty.bootstrap.ConnectionlessBootstrap; import org.jboss.netty.bootstrap.ServerBootstrap; import org.jboss.netty.channel.ChannelPipeline; import org.traccar.BaseProtocol; import org.traccar.TrackerServer; -import org.traccar.model.Command; import java.util.List; -- cgit v1.2.3 From 89806e14be5ff9637179c497b5ad2ac49a7a35c5 Mon Sep 17 00:00:00 2001 From: Anton Tananaev Date: Thu, 21 Jul 2016 00:13:33 +1200 Subject: Do not close HTTP connection --- src/org/traccar/protocol/OsmAndProtocolDecoder.java | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/org/traccar/protocol/OsmAndProtocolDecoder.java b/src/org/traccar/protocol/OsmAndProtocolDecoder.java index cfa8990dd..c5a37b47a 100644 --- a/src/org/traccar/protocol/OsmAndProtocolDecoder.java +++ b/src/org/traccar/protocol/OsmAndProtocolDecoder.java @@ -123,9 +123,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; -- cgit v1.2.3 From 82259ad4b16f28707ae15f917389774b51e3b0b3 Mon Sep 17 00:00:00 2001 From: Anton Tananaev Date: Thu, 21 Jul 2016 00:15:22 +1200 Subject: Remove unused imports --- src/org/traccar/protocol/OsmAndProtocolDecoder.java | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/org/traccar/protocol/OsmAndProtocolDecoder.java b/src/org/traccar/protocol/OsmAndProtocolDecoder.java index c5a37b47a..da502ca65 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; -- cgit v1.2.3 From b4e0833b1b180f40b132e60ee49d897d409bc000 Mon Sep 17 00:00:00 2001 From: Abyss777 Date: Thu, 21 Jul 2016 09:56:41 +0500 Subject: Migrate from ReentrantReadWriteLocks to ConcurrentHashMaps in DeviceManager UpdateCahces incrementally --- src/org/traccar/database/DeviceManager.java | 286 +++++++++++----------------- 1 file changed, 106 insertions(+), 180 deletions(-) diff --git a/src/org/traccar/database/DeviceManager.java b/src/org/traccar/database/DeviceManager.java index 43d83d078..916e7fe67 100644 --- a/src/org/traccar/database/DeviceManager.java +++ b/src/org/traccar/database/DeviceManager.java @@ -18,15 +18,13 @@ 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; @@ -43,14 +41,12 @@ public class DeviceManager implements IdentityManager { private final DataManager dataManager; private final long dataRefreshDelay; - private final ReadWriteLock devicesLock = new ReentrantReadWriteLock(); - private final Map devicesById = new HashMap<>(); - private final Map devicesByUniqueId = new HashMap<>(); - private long devicesLastUpdate; + private Map devicesById; + private Map devicesByUniqueId; + private AtomicLong devicesLastUpdate = new AtomicLong(0); - private final ReadWriteLock groupsLock = new ReentrantReadWriteLock(); - private final Map groupsById = new HashMap<>(); - private long groupsLastUpdate; + private Map groupsById; + private AtomicLong groupsLastUpdate = new AtomicLong(0); private final Map positions = new ConcurrentHashMap<>(); @@ -72,77 +68,76 @@ 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(); - } - + boolean needWrite = force || System.currentTimeMillis() - devicesLastUpdate.get() > dataRefreshDelay; 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)); - } + GeofenceManager geofenceManager = Context.getGeofenceManager(); + Collection databaseDevices = dataManager.getAllDevices(); + if (devicesById == null) { + devicesById = new ConcurrentHashMap<>(databaseDevices.size()); + } + if (devicesByUniqueId == null) { + devicesByUniqueId = new ConcurrentHashMap<>(databaseDevices.size()); + } + Set databaseDevicesIds = new HashSet<>(); + Set databaseDevicesUniqIds = new HashSet<>(); + for (Device device : databaseDevices) { + databaseDevicesIds.add(device.getId()); + databaseDevicesUniqIds.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); } - } finally { - devicesLock.writeLock().unlock(); } + for (Long cachedDeviceId : devicesById.keySet()) { + if (!databaseDevicesIds.contains(cachedDeviceId)) { + devicesById.remove(cachedDeviceId); + } + } + for (String cachedDeviceUniqId : devicesByUniqueId.keySet()) { + if (!databaseDevicesUniqIds.contains(cachedDeviceUniqId)) { + devicesByUniqueId.remove(cachedDeviceUniqId); + } + } + databaseDevicesIds.clear(); + databaseDevicesUniqIds.clear(); + devicesLastUpdate.set(System.currentTimeMillis()); } } @Override public Device getDeviceById(long id) { - 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 getAllDevices() { - boolean forceUpdate; - devicesLock.readLock().lock(); - try { - forceUpdate = devicesById.isEmpty(); - } finally { - devicesLock.readLock().unlock(); - } + boolean forceUpdate = devicesById.isEmpty(); try { updateDeviceCache(forceUpdate); @@ -150,23 +145,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 getDevices(long userId) throws SQLException { Collection 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; } @@ -174,56 +159,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); } @@ -234,13 +197,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); @@ -272,47 +230,40 @@ public class DeviceManager implements IdentityManager { } private void updateGroupCache(boolean force) throws SQLException { - boolean needWrite; - groupsLock.readLock().lock(); - try { - needWrite = force || System.currentTimeMillis() - groupsLastUpdate > dataRefreshDelay; - } finally { - groupsLock.readLock().unlock(); - } + boolean needWrite = force || System.currentTimeMillis() - groupsLastUpdate.get() > dataRefreshDelay; if (needWrite) { - groupsLock.writeLock().lock(); - try { - if (force || System.currentTimeMillis() - groupsLastUpdate > dataRefreshDelay) { - groupsById.clear(); - for (Group group : dataManager.getAllGroups()) { - groupsById.put(group.getId(), group); - } - groupsLastUpdate = System.currentTimeMillis(); + Collection databaseGroups = dataManager.getAllGroups(); + if (groupsById == null) { + groupsById = new ConcurrentHashMap<>(databaseGroups.size()); + } + Set 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); } - } finally { - groupsLock.writeLock().unlock(); } + databaseGroupsIds.clear(); + groupsLastUpdate.set(System.currentTimeMillis()); } } public Group getGroupById(long id) { - groupsLock.readLock().lock(); - try { - return groupsById.get(id); - } finally { - groupsLock.readLock().unlock(); - } + return groupsById.get(id); } public Collection getAllGroups() { - boolean forceUpdate; - groupsLock.readLock().lock(); - try { - forceUpdate = groupsById.isEmpty(); - } finally { - groupsLock.readLock().unlock(); - } + boolean forceUpdate = groupsById.isEmpty(); try { updateGroupCache(forceUpdate); @@ -320,12 +271,7 @@ public class DeviceManager implements IdentityManager { Log.warning(e); } - groupsLock.readLock().lock(); - try { - return groupsById.values(); - } finally { - groupsLock.readLock().unlock(); - } + return groupsById.values(); } public Collection getGroups(long userId) throws SQLException { @@ -337,50 +283,30 @@ public class DeviceManager implements IdentityManager { } private void checkGroupCycles(Group group) { - groupsLock.readLock().lock(); - try { - Set 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()); + Set groups = new HashSet<>(); + while (group != null) { + if (groups.contains(group.getId())) { + throw new IllegalArgumentException("Cycle in group hierarchy"); } - } finally { - groupsLock.readLock().unlock(); + groups.add(group.getId()); + group = groupsById.get(group.getGroupId()); } } public void addGroup(Group group) throws SQLException { checkGroupCycles(group); dataManager.addGroup(group); - groupsLock.writeLock().lock(); - try { - groupsById.put(group.getId(), group); - } finally { - groupsLock.writeLock().unlock(); - } + groupsById.put(group.getId(), group); } public void updateGroup(Group group) throws SQLException { checkGroupCycles(group); dataManager.updateGroup(group); - groupsLock.writeLock().lock(); - try { - groupsById.put(group.getId(), group); - } finally { - groupsLock.writeLock().unlock(); - } + groupsById.put(group.getId(), group); } public void removeGroup(long groupId) throws SQLException { dataManager.removeGroup(groupId); - groupsLock.writeLock().lock(); - try { - groupsById.remove(groupId); - } finally { - groupsLock.writeLock().unlock(); - } + groupsById.remove(groupId); } } -- cgit v1.2.3 From 35eea73b2b9e381efd5da60662f0f64f59b91406 Mon Sep 17 00:00:00 2001 From: Abyss777 Date: Thu, 21 Jul 2016 17:39:04 +0500 Subject: Act with atomic more carefully --- src/org/traccar/database/DeviceManager.java | 25 +++++++++++++------------ 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/src/org/traccar/database/DeviceManager.java b/src/org/traccar/database/DeviceManager.java index 916e7fe67..f442dfb19 100644 --- a/src/org/traccar/database/DeviceManager.java +++ b/src/org/traccar/database/DeviceManager.java @@ -43,10 +43,10 @@ public class DeviceManager implements IdentityManager { private Map devicesById; private Map devicesByUniqueId; - private AtomicLong devicesLastUpdate = new AtomicLong(0); + private AtomicLong devicesLastUpdate = new AtomicLong(); private Map groupsById; - private AtomicLong groupsLastUpdate = new AtomicLong(0); + private AtomicLong groupsLastUpdate = new AtomicLong(); private final Map positions = new ConcurrentHashMap<>(); @@ -68,8 +68,10 @@ public class DeviceManager implements IdentityManager { } private void updateDeviceCache(boolean force) throws SQLException { - boolean needWrite = force || System.currentTimeMillis() - devicesLastUpdate.get() > dataRefreshDelay; - if (needWrite) { + + long lastUpdate = devicesLastUpdate.get(); + if (force || System.currentTimeMillis() - lastUpdate > dataRefreshDelay + && devicesLastUpdate.compareAndSet(lastUpdate, System.currentTimeMillis())) { GeofenceManager geofenceManager = Context.getGeofenceManager(); Collection databaseDevices = dataManager.getAllDevices(); if (devicesById == null) { @@ -79,10 +81,10 @@ public class DeviceManager implements IdentityManager { devicesByUniqueId = new ConcurrentHashMap<>(databaseDevices.size()); } Set databaseDevicesIds = new HashSet<>(); - Set databaseDevicesUniqIds = new HashSet<>(); + Set databaseDevicesUniqueIds = new HashSet<>(); for (Device device : databaseDevices) { databaseDevicesIds.add(device.getId()); - databaseDevicesUniqIds.add(device.getUniqueId()); + databaseDevicesUniqueIds.add(device.getUniqueId()); if (devicesById.containsKey(device.getId())) { Device cachedDevice = devicesById.get(device.getId()); cachedDevice.setName(device.getName()); @@ -112,13 +114,12 @@ public class DeviceManager implements IdentityManager { } } for (String cachedDeviceUniqId : devicesByUniqueId.keySet()) { - if (!databaseDevicesUniqIds.contains(cachedDeviceUniqId)) { + if (!databaseDevicesUniqueIds.contains(cachedDeviceUniqId)) { devicesByUniqueId.remove(cachedDeviceUniqId); } } databaseDevicesIds.clear(); - databaseDevicesUniqIds.clear(); - devicesLastUpdate.set(System.currentTimeMillis()); + databaseDevicesUniqueIds.clear(); } } @@ -230,9 +231,10 @@ public class DeviceManager implements IdentityManager { } private void updateGroupCache(boolean force) throws SQLException { - boolean needWrite = force || System.currentTimeMillis() - groupsLastUpdate.get() > dataRefreshDelay; - if (needWrite) { + long lastUpdate = groupsLastUpdate.get(); + if (force || System.currentTimeMillis() - lastUpdate > dataRefreshDelay + && groupsLastUpdate.compareAndSet(lastUpdate, System.currentTimeMillis())) { Collection databaseGroups = dataManager.getAllGroups(); if (groupsById == null) { groupsById = new ConcurrentHashMap<>(databaseGroups.size()); @@ -254,7 +256,6 @@ public class DeviceManager implements IdentityManager { } } databaseGroupsIds.clear(); - groupsLastUpdate.set(System.currentTimeMillis()); } } -- cgit v1.2.3 From e269c81b1b8044d051196e97f0884935c153feb0 Mon Sep 17 00:00:00 2001 From: Abyss777 Date: Thu, 21 Jul 2016 17:44:57 +0500 Subject: Added parentheses --- src/org/traccar/database/DeviceManager.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/org/traccar/database/DeviceManager.java b/src/org/traccar/database/DeviceManager.java index f442dfb19..4dd7b41cb 100644 --- a/src/org/traccar/database/DeviceManager.java +++ b/src/org/traccar/database/DeviceManager.java @@ -70,7 +70,7 @@ public class DeviceManager implements IdentityManager { private void updateDeviceCache(boolean force) throws SQLException { long lastUpdate = devicesLastUpdate.get(); - if (force || System.currentTimeMillis() - lastUpdate > dataRefreshDelay + if ((force || System.currentTimeMillis() - lastUpdate > dataRefreshDelay) && devicesLastUpdate.compareAndSet(lastUpdate, System.currentTimeMillis())) { GeofenceManager geofenceManager = Context.getGeofenceManager(); Collection databaseDevices = dataManager.getAllDevices(); @@ -233,7 +233,7 @@ public class DeviceManager implements IdentityManager { private void updateGroupCache(boolean force) throws SQLException { long lastUpdate = groupsLastUpdate.get(); - if (force || System.currentTimeMillis() - lastUpdate > dataRefreshDelay + if ((force || System.currentTimeMillis() - lastUpdate > dataRefreshDelay) && groupsLastUpdate.compareAndSet(lastUpdate, System.currentTimeMillis())) { Collection databaseGroups = dataManager.getAllGroups(); if (groupsById == null) { -- cgit v1.2.3 From c92f37404893cce14a25379bcd84bb081e94f1b8 Mon Sep 17 00:00:00 2001 From: Anton Tananaev Date: Fri, 22 Jul 2016 13:26:45 +1200 Subject: Workaround for incorrect hemisphere (fix #2139) --- src/org/traccar/protocol/WatchProtocolDecoder.java | 6 +++--- test/org/traccar/protocol/WatchProtocolDecoderTest.java | 4 ++++ 2 files changed, 7 insertions(+), 3 deletions(-) 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 diff --git a/test/org/traccar/protocol/WatchProtocolDecoderTest.java b/test/org/traccar/protocol/WatchProtocolDecoderTest.java index 89e9a7e77..a8f7b12bc 100644 --- a/test/org/traccar/protocol/WatchProtocolDecoderTest.java +++ b/test/org/traccar/protocol/WatchProtocolDecoderTest.java @@ -10,6 +10,10 @@ public class WatchProtocolDecoderTest extends ProtocolTest { WatchProtocolDecoder decoder = new WatchProtocolDecoder(new WatchProtocol()); + verifyPosition(decoder, text( + "[3G*6105117105*008D*UD2,210716,231601,V,-33.480366,N,-70.7630692,E,0.00,0.0,0.0,0,100,34,0,0,00000000,3,255,730,2,29731,54315,167,29731,54316,162,29731,54317,145"), + position("2016-07-21 23:16:01.000", false, -33.48037, -70.76307)); + verifyPosition(decoder, text( "[3G*4700222306*0077*UD,120316,140610,V,48.779045,N, 9.1574736,E,0.00,0.0,0.0,0,25,83,0,0,00000000,2,255,262,1,21041,9067,121,21041,5981,116")); -- cgit v1.2.3 From a23508077eacb97d50c7a5eafc319aa1b3fdea94 Mon Sep 17 00:00:00 2001 From: Abyss777 Date: Fri, 22 Jul 2016 13:42:53 +0500 Subject: Actualize swagger.json --- swagger.json | 601 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 583 insertions(+), 18 deletions(-) diff --git a/swagger.json b/swagger.json index 12b5020c7..6b126ef22 100644 --- a/swagger.json +++ b/swagger.json @@ -159,6 +159,59 @@ } } }, + "/devices/geofences": { + "post": { + "consumes": [ + "application/json" + ], + "produces": [ + "application/json" + ], + "parameters": [ + { + "name": "body", + "in": "body", + "required": true, + "schema": { + "$ref": "#/definitions/DeviceGeofence" + } + } + ], + "responses": { + "200": { + "description": "OK", + "headers": {}, + "schema": { + "$ref": "#/definitions/DeviceGeofence" + } + } + } + }, + "delete": { + "consumes": [ + "application/json" + ], + "produces": [ + "application/json" + ], + "parameters": [ + { + "name": "body", + "in": "body", + "required": true, + "schema": { + "$ref": "#/definitions/DeviceGeofence" + } + } + ], + "responses": { + "204": { + "description": "No Content", + "headers": {} + } + } + } + }, "/groups": { "get": { "consumes": [ @@ -279,6 +332,59 @@ } } }, + "/groups/geofences": { + "post": { + "consumes": [ + "application/json" + ], + "produces": [ + "application/json" + ], + "parameters": [ + { + "name": "body", + "in": "body", + "required": true, + "schema": { + "$ref": "#/definitions/GroupGeofence" + } + } + ], + "responses": { + "200": { + "description": "OK", + "headers": {}, + "schema": { + "$ref": "#/definitions/GroupGeofence" + } + } + } + }, + "delete": { + "consumes": [ + "application/json" + ], + "produces": [ + "application/json" + ], + "parameters": [ + { + "name": "body", + "in": "body", + "required": true, + "schema": { + "$ref": "#/definitions/GroupGeofence" + } + } + ], + "responses": { + "204": { + "description": "No Content", + "headers": {} + } + } + } + }, "/permissions/devices": { "post": { "consumes": [ @@ -385,6 +491,59 @@ } } }, + "/permissions/geofences": { + "post": { + "consumes": [ + "application/json" + ], + "produces": [ + "application/json" + ], + "parameters": [ + { + "name": "body", + "in": "body", + "required": true, + "schema": { + "$ref": "#/definitions/GeofencePermission" + } + } + ], + "responses": { + "200": { + "description": "OK", + "headers": {}, + "schema": { + "$ref": "#/definitions/GeofencePermission" + } + } + } + }, + "delete": { + "consumes": [ + "application/json" + ], + "produces": [ + "application/json" + ], + "parameters": [ + { + "name": "body", + "in": "body", + "required": true, + "schema": { + "$ref": "#/definitions/GeofencePermission" + } + } + ], + "responses": { + "204": { + "description": "No Content", + "headers": {} + } + } + } + }, "/positions": { "get": { "consumes": [ @@ -654,11 +813,314 @@ } } } + }, + "/users/notifications": { + "get": { + "consumes": [ + "application/json" + ], + "produces": [ + "application/json" + ], + "parameters": [ + { + "name": "all", + "in": "query", + "required": true, + "type": "boolean" + }, + { + "name": "userId", + "in": "query", + "required": true, + "type": "integer" + } + ], + "responses": { + "200": { + "description": "OK", + "headers": {}, + "schema": { + "type": "array", + "items": { + "$ref": "#/definitions/Notification" + } + } + } + } + }, + "post": { + "consumes": [ + "application/json" + ], + "produces": [ + "application/json" + ], + "parameters": [ + { + "name": "body", + "in": "body", + "required": true, + "schema": { + "$ref": "#/definitions/Notification" + } + } + ], + "responses": { + "200": { + "description": "OK", + "headers": {}, + "schema": { + "$ref": "#/definitions/Notification" + } + } + } + } + }, + "/commandtypes": { + "post": { + "consumes": [ + "application/json" + ], + "produces": [ + "application/json" + ], + "parameters": [ + { + "name": "deviceId", + "in": "query", + "required": true, + "type": "integer" + } + ], + "responses": { + "200": { + "description": "OK", + "headers": {}, + "schema": { + "type": "array", + "items": { + "$ref": "#/definitions/CommandType" + } + } + } + } + } + }, + "/geofences": { + "get": { + "consumes": [ + "application/json" + ], + "produces": [ + "application/json" + ], + "parameters": [ + { + "name": "all", + "in": "query", + "required": true, + "type": "boolean" + }, + { + "name": "userId", + "in": "query", + "required": true, + "type": "integer" + }, + { + "name": "groupId", + "in": "query", + "required": true, + "type": "integer" + }, + { + "name": "deviceId", + "in": "query", + "required": true, + "type": "integer" + } + ], + "responses": { + "200": { + "description": "OK", + "headers": {}, + "schema": { + "type": "array", + "items": { + "$ref": "#/definitions/Geofence" + } + } + } + } + }, + "post": { + "consumes": [ + "application/json" + ], + "produces": [ + "application/json" + ], + "parameters": [ + { + "name": "body", + "in": "body", + "required": true, + "schema": { + "$ref": "#/definitions/Geofence" + } + } + ], + "responses": { + "200": { + "description": "OK", + "headers": {}, + "schema": { + "$ref": "#/definitions/Geofence" + } + } + } + } + }, + "/geofences/{id}": { + "put": { + "consumes": [ + "application/json" + ], + "produces": [ + "application/json" + ], + "parameters": [ + { + "name": "id", + "in": "path", + "required": true, + "type": "integer" + }, + { + "name": "body", + "in": "body", + "required": true, + "schema": { + "$ref": "#/definitions/Geofence" + } + } + ], + "responses": { + "200": { + "description": "OK", + "headers": {}, + "schema": { + "$ref": "#/definitions/Geofence" + } + } + } + }, + "delete": { + "consumes": [ + "application/json" + ], + "produces": [ + "application/json" + ], + "parameters": [ + { + "name": "id", + "in": "path", + "required": true, + "type": "integer" + } + ], + "responses": { + "204": { + "description": "No Content", + "headers": {} + } + } + } + }, + "/events": { + "get": { + "consumes": [ + "application/json" + ], + "produces": [ + "application/json" + ], + "parameters": [ + { + "name": "deviceId", + "in": "query", + "required": true, + "type": "integer" + }, + { + "name": "type", + "in": "query", + "required": true, + "type": "string" + }, + { + "name": "interval", + "in": "query", + "required": true, + "type": "integer" + } + ], + "responses": { + "200": { + "description": "OK", + "headers": {}, + "schema": { + "type": "array", + "items": { + "$ref": "#/definitions/Event" + } + } + } + } + } + }, + "/events/{id}": { + "get": { + "consumes": [ + "application/json" + ], + "produces": [ + "application/json" + ], + "parameters": [ + { + "name": "id", + "in": "path", + "required": true, + "type": "integer" + } + ], + "responses": { + "200": { + "description": "OK", + "headers": {}, + "schema": { + "$ref": "#/definitions/Event" + } + } + } + } } }, "definitions": { "Position": { "properties": { + "id": { + "type": "integer" + }, + "protocol": { + "type": "string" + }, + "deviceTime": { + "type": "string" + }, "fixTime": { "type": "string" }, @@ -685,13 +1147,14 @@ }, "address": { "type": "string" - } + }, + "attributes": {} } }, "User": { "properties": { "id": { - "type": "number" + "type": "integer" }, "name": { "type": "string" @@ -724,20 +1187,21 @@ "type": "number" }, "zoom": { - "type": "number" + "type": "integer" }, "password": { "type": "string" }, "twelveHourFormat": { "type": "boolean" - } + }, + "attributes": {} } }, "Server": { "properties": { "id": { - "type": "number" + "type": "integer" }, "registration": { "type": "boolean" @@ -767,7 +1231,7 @@ "type": "number" }, "zoom": { - "type": "number" + "type": "integer" }, "twelveHourFormat": { "type": "boolean" @@ -775,12 +1239,20 @@ } }, "Command": { - "properties": {} + "properties": { + "deviceId": { + "type": "integer" + }, + "type": { + "type": "string" + }, + "attributes": {} + } }, "Device": { "properties": { "id": { - "type": "number" + "type": "integer" }, "name": { "type": "string" @@ -795,45 +1267,138 @@ "type": "string" }, "positionId": { - "type": "number" + "type": "integer" }, "groupId": { - "type": "number" - } + "type": "integer" + }, + "geofenceIds": {}, + "attributes": {} } }, "Group": { "properties": { "id": { - "type": "number" + "type": "integer" }, "name": { "type": "string" }, "groupId": { - "type": "number" + "type": "integer" } } }, "DevicePermission": { "properties": { "userId": { - "type": "number" + "type": "integer" }, "deviceId": { - "type": "number" + "type": "integer" } } }, "GroupPermission": { "properties": { "userId": { - "type": "number" + "type": "integer" }, "groupId": { - "type": "number" + "type": "integer" + } + } + }, + "GeofencePermission": { + "properties": { + "userId": { + "type": "integer" + }, + "geofenceId": { + "type": "integer" + } + } + }, + "GroupGeofence": { + "properties": { + "groupId": { + "type": "integer" + }, + "geofenceId": { + "type": "integer" + } + } + }, + "DeviceGeofence": { + "properties": { + "deviceId": { + "type": "integer" + }, + "geofenceId": { + "type": "integer" + } + } + }, + "CommandType": { + "properties": { + "type": { + "type": "string" } } + }, + "Geofence": { + "properties": { + "id": { + "type": "integer" + }, + "name": { + "type": "string" + }, + "description": { + "type": "string" + }, + "area": { + "type": "string" + }, + "attributes": {} + } + }, + "Notification": { + "properties": { + "id": { + "type": "integer" + }, + "type": { + "type": "string" + }, + "userId": { + "type": "integer" + }, + "attributes": {} + } + }, + "Event": { + "properties": { + "id": { + "type": "integer" + }, + "type": { + "type": "string" + }, + "serverTime": { + "type": "string" + }, + "deviceId": { + "type": "integer" + }, + "positionId": { + "type": "integer" + }, + "geofenceId": { + "type": "integer" + }, + "attributes": {} + } } } -} +} \ No newline at end of file -- cgit v1.2.3 From 37d28b62627c2eabe135b58672ce20e8e58bf8d6 Mon Sep 17 00:00:00 2001 From: Abyss777 Date: Fri, 22 Jul 2016 13:45:05 +0500 Subject: Update version --- swagger.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/swagger.json b/swagger.json index 6b126ef22..a16794a41 100644 --- a/swagger.json +++ b/swagger.json @@ -1,7 +1,7 @@ { "swagger": "2.0", "info": { - "version": "3.5", + "version": "3.7", "title": "traccar" }, "host": "traccar.org", -- cgit v1.2.3 From e218ed3cd4fa3c1630ba15faacc23774c40543d0 Mon Sep 17 00:00:00 2001 From: Anton Tananaev Date: Sat, 23 Jul 2016 00:58:59 +1200 Subject: Return http error if unknown --- src/org/traccar/protocol/OsmAndProtocolDecoder.java | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/org/traccar/protocol/OsmAndProtocolDecoder.java b/src/org/traccar/protocol/OsmAndProtocolDecoder.java index da502ca65..12192c457 100644 --- a/src/org/traccar/protocol/OsmAndProtocolDecoder.java +++ b/src/org/traccar/protocol/OsmAndProtocolDecoder.java @@ -64,6 +64,9 @@ 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()); -- cgit v1.2.3 From 0275aa2bc793e0c3057ee954df4e4b02e6708898 Mon Sep 17 00:00:00 2001 From: Anton Tananaev Date: Sat, 23 Jul 2016 00:59:27 +1200 Subject: Update test generator script --- tools/test-generator.py | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/tools/test-generator.py b/tools/test-generator.py index 681d1755d..8d7377c2c 100755 --- a/tools/test-generator.py +++ b/tools/test-generator.py @@ -3,11 +3,11 @@ import sys import math import urllib -import urllib2 +import httplib import time id = '123456789012345' -server = 'http://localhost:5055' +server = 'localhost:5055' period = 1 step = 0.001 @@ -32,9 +32,10 @@ for i in range(0, len(waypoints)): lon = lon1 + (lon2 - lon1) * j / count points.append((lat, lon)) -def send(lat, lon, course): +def send(conn, lat, lon, course): params = (('id', id), ('timestamp', int(time.time())), ('lat', lat), ('lon', lon), ('bearing', course)) - urllib2.urlopen(server + '?' + urllib.urlencode(params)).read() + conn.request('GET', '?' + urllib.urlencode(params)) + conn.getresponse() def course(lat1, lon1, lat2, lon2): lat1 = lat1 * math.pi / 180 @@ -47,9 +48,11 @@ def course(lat1, lon1, lat2, lon2): index = 0 +conn = httplib.HTTPConnection(server) + while True: (lat1, lon1) = points[index % len(points)] (lat2, lon2) = points[(index + 1) % len(points)] - send(lat1, lon1, course(lat1, lon1, lat2, lon2)) + send(conn, lat1, lon1, course(lat1, lon1, lat2, lon2)) time.sleep(period) index += 1 -- cgit v1.2.3 From 5d9c7f4dde298d5b02a2aa688bb27949072e2a57 Mon Sep 17 00:00:00 2001 From: Anton Tananaev Date: Sat, 23 Jul 2016 01:01:15 +1200 Subject: Fix checkstyle issue --- src/org/traccar/protocol/OsmAndProtocolDecoder.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/org/traccar/protocol/OsmAndProtocolDecoder.java b/src/org/traccar/protocol/OsmAndProtocolDecoder.java index 12192c457..f46511b27 100644 --- a/src/org/traccar/protocol/OsmAndProtocolDecoder.java +++ b/src/org/traccar/protocol/OsmAndProtocolDecoder.java @@ -48,8 +48,7 @@ public class OsmAndProtocolDecoder extends BaseProtocolDecoder { QueryStringDecoder decoder = new QueryStringDecoder(request.getUri()); Map> 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(); } @@ -65,7 +64,8 @@ public class OsmAndProtocolDecoder extends BaseProtocolDecoder { DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, value); if (deviceSession == null) { if (channel != null) { - channel.write(new DefaultHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.BAD_REQUEST)); + channel.write( + new DefaultHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.BAD_REQUEST)); } return null; } -- cgit v1.2.3 From 83d2fdc694ca75eaef53adef76dad938e1bd6262 Mon Sep 17 00:00:00 2001 From: Anton Tananaev Date: Sat, 23 Jul 2016 01:26:41 +1200 Subject: Support GT02 command responses --- src/org/traccar/protocol/Gt02ProtocolDecoder.java | 13 +++++++++++-- test/org/traccar/protocol/Gt02ProtocolDecoderTest.java | 6 ++++++ 2 files changed, 17 insertions(+), 2 deletions(-) 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/test/org/traccar/protocol/Gt02ProtocolDecoderTest.java b/test/org/traccar/protocol/Gt02ProtocolDecoderTest.java index 3cfa0ea4a..c957acd6c 100644 --- a/test/org/traccar/protocol/Gt02ProtocolDecoderTest.java +++ b/test/org/traccar/protocol/Gt02ProtocolDecoderTest.java @@ -10,6 +10,12 @@ public class Gt02ProtocolDecoderTest extends ProtocolTest { Gt02ProtocolDecoder decoder = new Gt02ProtocolDecoder(new Gt02Protocol()); + verifyAttributes(decoder, binary( + "6868150000035889905895258400831c07415045584f4b210d0a")); + + verifyAttributes(decoder, binary( + "68682d0000035889905895258400951c1f415045584572726f723a20506172616d65746572203120284f4e2f4f4646290d0a")); + verifyAttributes(decoder, binary( "68680f0504035889905831401700df1a00000d0a")); -- cgit v1.2.3 From e176392ddbea44dc94c0904f21338a11b862ad4d Mon Sep 17 00:00:00 2001 From: Anton Tananaev Date: Sat, 23 Jul 2016 11:49:28 +1200 Subject: Improve GpsGate protocol decoder --- src/org/traccar/protocol/GpsGateProtocolDecoder.java | 4 ++-- test/org/traccar/protocol/GpsGateProtocolDecoderTest.java | 3 +++ 2 files changed, 5 insertions(+), 2 deletions(-) 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/test/org/traccar/protocol/GpsGateProtocolDecoderTest.java b/test/org/traccar/protocol/GpsGateProtocolDecoderTest.java index 30861099f..11cfb66f5 100644 --- a/test/org/traccar/protocol/GpsGateProtocolDecoderTest.java +++ b/test/org/traccar/protocol/GpsGateProtocolDecoderTest.java @@ -10,6 +10,9 @@ public class GpsGateProtocolDecoderTest extends ProtocolTest { GpsGateProtocolDecoder decoder = new GpsGateProtocolDecoder(new GpsGateProtocol()); + verifyPosition(decoder, text( + "$FRCMD,356406061385182,_SendMessage,,5223.88542,N,11440.45866,W,951.2,0.027,,220716,153507.00,1*5F")); + verifyPosition(decoder, text( "$FRCMD,353067011068246,_SendMessage,,1918.1942,N,09906.3696,W,2246.5,000.0,295.9,150416,213147.00,1,Odometer=*70")); -- cgit v1.2.3 From c7a0fa08fde55e8d14bfee9ed5f4f14ee42c0c30 Mon Sep 17 00:00:00 2001 From: Fabián Inostroza Date: Sun, 24 Jul 2016 02:44:04 -0400 Subject: Correctly process negative latitude and longitude. --- src/org/traccar/protocol/EasyTrackProtocolDecoder.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) 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); -- cgit v1.2.3