From c710550576a3882b7ecccc47f69b5c1528fa969d Mon Sep 17 00:00:00 2001 From: Philipp Prangenberg Date: Mon, 5 Dec 2016 12:30:15 +0100 Subject: Added functionality to get group by name. Implemented configurable default group and default category when registering unknown devices --- src/org/traccar/BaseProtocolDecoder.java | 17 ++++++++++++++++- src/org/traccar/database/DeviceManager.java | 9 +++++++++ 2 files changed, 25 insertions(+), 1 deletion(-) (limited to 'src/org') diff --git a/src/org/traccar/BaseProtocolDecoder.java b/src/org/traccar/BaseProtocolDecoder.java index d8130c79e..5b5cc3146 100644 --- a/src/org/traccar/BaseProtocolDecoder.java +++ b/src/org/traccar/BaseProtocolDecoder.java @@ -19,6 +19,7 @@ import org.jboss.netty.channel.Channel; import org.jboss.netty.channel.socket.DatagramChannel; import org.traccar.helper.Log; import org.traccar.model.Device; +import org.traccar.model.Group; import org.traccar.model.Position; import java.net.InetSocketAddress; @@ -34,8 +35,22 @@ public abstract class BaseProtocolDecoder extends ExtendedObjectDecoder { public long addUnknownDevice(String uniqueId) { Device device = new Device(); + String defaultGroupName = Context.getConfig().getString("database.registerUnknown.defaultGroup", "NOTDEFINED"); device.setName(uniqueId); device.setUniqueId(uniqueId); + device.setCategory(Context.getConfig().getString("database.registerUnknown.defaultCategory", "default")); + + if(!defaultGroupName.equals("NOTDEFINED")){ +// for (Group g:Context.getDeviceManager().getAllGroups()) { +// if(g.getName().equalsIgnoreCase(defaultGroupName)){ +// device.setGroupId(g.getGroupId()); +// break; +// } +// } + Group defaultGroup = Context.getDeviceManager().getGroupByName(defaultGroupName); + if(defaultGroup != null) + device.setGroupId(defaultGroup.getId()); + } try { Context.getDeviceManager().addDevice(device); @@ -183,4 +198,4 @@ public abstract class BaseProtocolDecoder extends ExtendedObjectDecoder { } } -} +} \ No newline at end of file diff --git a/src/org/traccar/database/DeviceManager.java b/src/org/traccar/database/DeviceManager.java index c70e67231..700d22679 100644 --- a/src/org/traccar/database/DeviceManager.java +++ b/src/org/traccar/database/DeviceManager.java @@ -49,6 +49,7 @@ public class DeviceManager implements IdentityManager { private AtomicLong devicesLastUpdate = new AtomicLong(); private Map groupsById; + private Map groupsByName; private AtomicLong groupsLastUpdate = new AtomicLong(); private final Map positions = new ConcurrentHashMap<>(); @@ -245,15 +246,21 @@ public class DeviceManager implements IdentityManager { if (groupsById == null) { groupsById = new ConcurrentHashMap<>(databaseGroups.size()); } + if(groupsByName == null){ + groupsByName = new ConcurrentHashMap<>(databaseGroups.size()); + } Set databaseGroupsIds = new HashSet<>(); + Set databaseGroupsNames = new HashSet<>(); for (Group group : databaseGroups) { databaseGroupsIds.add(group.getId()); + databaseGroupsNames.add(group.getName()); 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); + groupsByName.put(group.getName(), group); } } for (Long cachedGroupId : groupsById.keySet()) { @@ -269,6 +276,8 @@ public class DeviceManager implements IdentityManager { return groupsById.get(id); } + public Group getGroupByName(String name) { return groupsByName.get(name); } + public Collection getAllGroups() { boolean forceUpdate = groupsById.isEmpty(); -- cgit v1.2.3 From ece410b8fb0a0f186ed81c1a9fc8f0d4bbe0f545 Mon Sep 17 00:00:00 2001 From: Philipp Prangenberg Date: Mon, 5 Dec 2016 12:31:47 +0100 Subject: Beautification --- src/org/traccar/BaseProtocolDecoder.java | 6 ------ 1 file changed, 6 deletions(-) (limited to 'src/org') diff --git a/src/org/traccar/BaseProtocolDecoder.java b/src/org/traccar/BaseProtocolDecoder.java index 5b5cc3146..9e04fa5cd 100644 --- a/src/org/traccar/BaseProtocolDecoder.java +++ b/src/org/traccar/BaseProtocolDecoder.java @@ -41,12 +41,6 @@ public abstract class BaseProtocolDecoder extends ExtendedObjectDecoder { device.setCategory(Context.getConfig().getString("database.registerUnknown.defaultCategory", "default")); if(!defaultGroupName.equals("NOTDEFINED")){ -// for (Group g:Context.getDeviceManager().getAllGroups()) { -// if(g.getName().equalsIgnoreCase(defaultGroupName)){ -// device.setGroupId(g.getGroupId()); -// break; -// } -// } Group defaultGroup = Context.getDeviceManager().getGroupByName(defaultGroupName); if(defaultGroup != null) device.setGroupId(defaultGroup.getId()); -- cgit v1.2.3 From b55e3d1044e973cf14f7521bfacb592867f19d44 Mon Sep 17 00:00:00 2001 From: Philipp Prangenberg Date: Mon, 5 Dec 2016 12:36:17 +0100 Subject: Removed unnecessary Set --- src/org/traccar/database/DeviceManager.java | 1 - 1 file changed, 1 deletion(-) (limited to 'src/org') diff --git a/src/org/traccar/database/DeviceManager.java b/src/org/traccar/database/DeviceManager.java index 700d22679..e13fa2dbb 100644 --- a/src/org/traccar/database/DeviceManager.java +++ b/src/org/traccar/database/DeviceManager.java @@ -253,7 +253,6 @@ public class DeviceManager implements IdentityManager { Set databaseGroupsNames = new HashSet<>(); for (Group group : databaseGroups) { databaseGroupsIds.add(group.getId()); - databaseGroupsNames.add(group.getName()); if (groupsById.containsKey(group.getId())) { Group cachedGroup = groupsById.get(group.getId()); cachedGroup.setName(group.getName()); -- cgit v1.2.3 From e59f8af8c74940223e6ac3f0f6b99390bb464876 Mon Sep 17 00:00:00 2001 From: Philipp Prangenberg Date: Mon, 5 Dec 2016 15:01:39 +0100 Subject: Removed unnecessary Set declaration --- src/org/traccar/database/DeviceManager.java | 1 - 1 file changed, 1 deletion(-) (limited to 'src/org') diff --git a/src/org/traccar/database/DeviceManager.java b/src/org/traccar/database/DeviceManager.java index e13fa2dbb..f442317d3 100644 --- a/src/org/traccar/database/DeviceManager.java +++ b/src/org/traccar/database/DeviceManager.java @@ -250,7 +250,6 @@ public class DeviceManager implements IdentityManager { groupsByName = new ConcurrentHashMap<>(databaseGroups.size()); } Set databaseGroupsIds = new HashSet<>(); - Set databaseGroupsNames = new HashSet<>(); for (Group group : databaseGroups) { databaseGroupsIds.add(group.getId()); if (groupsById.containsKey(group.getId())) { -- cgit v1.2.3 From f8b3d5cc7286726e7719ddcd4db8d8456aa6e01f Mon Sep 17 00:00:00 2001 From: Philipp Prangenberg Date: Tue, 6 Dec 2016 08:55:11 +0100 Subject: Style changes --- src/org/traccar/BaseProtocolDecoder.java | 7 ++++--- src/org/traccar/database/DeviceManager.java | 6 ++++-- 2 files changed, 8 insertions(+), 5 deletions(-) (limited to 'src/org') diff --git a/src/org/traccar/BaseProtocolDecoder.java b/src/org/traccar/BaseProtocolDecoder.java index 9e04fa5cd..103a999b0 100644 --- a/src/org/traccar/BaseProtocolDecoder.java +++ b/src/org/traccar/BaseProtocolDecoder.java @@ -40,10 +40,11 @@ public abstract class BaseProtocolDecoder extends ExtendedObjectDecoder { device.setUniqueId(uniqueId); device.setCategory(Context.getConfig().getString("database.registerUnknown.defaultCategory", "default")); - if(!defaultGroupName.equals("NOTDEFINED")){ + if (!defaultGroupName.equals("NOTDEFINED")) { Group defaultGroup = Context.getDeviceManager().getGroupByName(defaultGroupName); - if(defaultGroup != null) + if (defaultGroup != null) { device.setGroupId(defaultGroup.getId()); + } } try { @@ -192,4 +193,4 @@ public abstract class BaseProtocolDecoder extends ExtendedObjectDecoder { } } -} \ No newline at end of file +} diff --git a/src/org/traccar/database/DeviceManager.java b/src/org/traccar/database/DeviceManager.java index f442317d3..403953d56 100644 --- a/src/org/traccar/database/DeviceManager.java +++ b/src/org/traccar/database/DeviceManager.java @@ -246,7 +246,7 @@ public class DeviceManager implements IdentityManager { if (groupsById == null) { groupsById = new ConcurrentHashMap<>(databaseGroups.size()); } - if(groupsByName == null){ + if (groupsByName == null) { groupsByName = new ConcurrentHashMap<>(databaseGroups.size()); } Set databaseGroupsIds = new HashSet<>(); @@ -274,7 +274,9 @@ public class DeviceManager implements IdentityManager { return groupsById.get(id); } - public Group getGroupByName(String name) { return groupsByName.get(name); } + public Group getGroupByName(String name) { + return groupsByName.get(name); + } public Collection getAllGroups() { boolean forceUpdate = groupsById.isEmpty(); -- cgit v1.2.3 From 7546048a58c16ad083d732aacd2af2d4a200b47c Mon Sep 17 00:00:00 2001 From: Philipp Prangenberg Date: Tue, 6 Dec 2016 11:45:05 +0100 Subject: Updating HashMap groupsByName in group-access methods --- src/org/traccar/database/DeviceManager.java | 7 +++++++ 1 file changed, 7 insertions(+) (limited to 'src/org') diff --git a/src/org/traccar/database/DeviceManager.java b/src/org/traccar/database/DeviceManager.java index 403953d56..a75b0616c 100644 --- a/src/org/traccar/database/DeviceManager.java +++ b/src/org/traccar/database/DeviceManager.java @@ -313,17 +313,24 @@ public class DeviceManager implements IdentityManager { checkGroupCycles(group); dataManager.addGroup(group); groupsById.put(group.getId(), group); + groupsByName.put(group.getName(), group); } public void updateGroup(Group group) throws SQLException { + String oldGroupName = groupsById.get(group.getId()).getName(); checkGroupCycles(group); dataManager.updateGroup(group); groupsById.put(group.getId(), group); + if (!oldGroupName.equals(group.getName())) { + groupsByName.remove(oldGroupName); + } + groupsByName.put(group.getName(), group); } public void removeGroup(long groupId) throws SQLException { dataManager.removeGroup(groupId); groupsById.remove(groupId); + groupsByName.remove(getGroupById(groupId).getName()); } public boolean lookupAttributeBoolean( -- cgit v1.2.3 From d196a75e70f80dd1a45202b82052b1227bf1ae8d Mon Sep 17 00:00:00 2001 From: Philipp Prangenberg Date: Thu, 8 Dec 2016 08:10:50 +0100 Subject: Moving from GroupName to GroupID in default settings --- src/org/traccar/BaseProtocolDecoder.java | 12 ++++-------- src/org/traccar/database/DeviceManager.java | 16 ---------------- 2 files changed, 4 insertions(+), 24 deletions(-) (limited to 'src/org') diff --git a/src/org/traccar/BaseProtocolDecoder.java b/src/org/traccar/BaseProtocolDecoder.java index 103a999b0..ff7ab685a 100644 --- a/src/org/traccar/BaseProtocolDecoder.java +++ b/src/org/traccar/BaseProtocolDecoder.java @@ -19,7 +19,6 @@ import org.jboss.netty.channel.Channel; import org.jboss.netty.channel.socket.DatagramChannel; import org.traccar.helper.Log; import org.traccar.model.Device; -import org.traccar.model.Group; import org.traccar.model.Position; import java.net.InetSocketAddress; @@ -35,16 +34,13 @@ public abstract class BaseProtocolDecoder extends ExtendedObjectDecoder { public long addUnknownDevice(String uniqueId) { Device device = new Device(); - String defaultGroupName = Context.getConfig().getString("database.registerUnknown.defaultGroup", "NOTDEFINED"); + long defaultGroupId = Context.getConfig().getLong("database.registerUnknown.defaultGroupId"); device.setName(uniqueId); device.setUniqueId(uniqueId); - device.setCategory(Context.getConfig().getString("database.registerUnknown.defaultCategory", "default")); + device.setCategory(Context.getConfig().getString("database.registerUnknown.defaultCategory")); - if (!defaultGroupName.equals("NOTDEFINED")) { - Group defaultGroup = Context.getDeviceManager().getGroupByName(defaultGroupName); - if (defaultGroup != null) { - device.setGroupId(defaultGroup.getId()); - } + if (defaultGroupId != 0) { + device.setGroupId(defaultGroupId); } try { diff --git a/src/org/traccar/database/DeviceManager.java b/src/org/traccar/database/DeviceManager.java index a75b0616c..c70e67231 100644 --- a/src/org/traccar/database/DeviceManager.java +++ b/src/org/traccar/database/DeviceManager.java @@ -49,7 +49,6 @@ public class DeviceManager implements IdentityManager { private AtomicLong devicesLastUpdate = new AtomicLong(); private Map groupsById; - private Map groupsByName; private AtomicLong groupsLastUpdate = new AtomicLong(); private final Map positions = new ConcurrentHashMap<>(); @@ -246,9 +245,6 @@ public class DeviceManager implements IdentityManager { if (groupsById == null) { groupsById = new ConcurrentHashMap<>(databaseGroups.size()); } - if (groupsByName == null) { - groupsByName = new ConcurrentHashMap<>(databaseGroups.size()); - } Set databaseGroupsIds = new HashSet<>(); for (Group group : databaseGroups) { databaseGroupsIds.add(group.getId()); @@ -258,7 +254,6 @@ public class DeviceManager implements IdentityManager { cachedGroup.setGroupId(group.getGroupId()); } else { groupsById.put(group.getId(), group); - groupsByName.put(group.getName(), group); } } for (Long cachedGroupId : groupsById.keySet()) { @@ -274,10 +269,6 @@ public class DeviceManager implements IdentityManager { return groupsById.get(id); } - public Group getGroupByName(String name) { - return groupsByName.get(name); - } - public Collection getAllGroups() { boolean forceUpdate = groupsById.isEmpty(); @@ -313,24 +304,17 @@ public class DeviceManager implements IdentityManager { checkGroupCycles(group); dataManager.addGroup(group); groupsById.put(group.getId(), group); - groupsByName.put(group.getName(), group); } public void updateGroup(Group group) throws SQLException { - String oldGroupName = groupsById.get(group.getId()).getName(); checkGroupCycles(group); dataManager.updateGroup(group); groupsById.put(group.getId(), group); - if (!oldGroupName.equals(group.getName())) { - groupsByName.remove(oldGroupName); - } - groupsByName.put(group.getName(), group); } public void removeGroup(long groupId) throws SQLException { dataManager.removeGroup(groupId); groupsById.remove(groupId); - groupsByName.remove(getGroupById(groupId).getName()); } public boolean lookupAttributeBoolean( -- cgit v1.2.3 From d139c55f06342fefb80082a0f6ffa6adea77f8dc Mon Sep 17 00:00:00 2001 From: Philipp Prangenberg Date: Fri, 9 Dec 2016 14:34:39 +0100 Subject: Moved declaration to section where it is used --- src/org/traccar/BaseProtocolDecoder.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/org') diff --git a/src/org/traccar/BaseProtocolDecoder.java b/src/org/traccar/BaseProtocolDecoder.java index ff7ab685a..ea0905bf2 100644 --- a/src/org/traccar/BaseProtocolDecoder.java +++ b/src/org/traccar/BaseProtocolDecoder.java @@ -34,11 +34,11 @@ public abstract class BaseProtocolDecoder extends ExtendedObjectDecoder { public long addUnknownDevice(String uniqueId) { Device device = new Device(); - long defaultGroupId = Context.getConfig().getLong("database.registerUnknown.defaultGroupId"); device.setName(uniqueId); device.setUniqueId(uniqueId); device.setCategory(Context.getConfig().getString("database.registerUnknown.defaultCategory")); + long defaultGroupId = Context.getConfig().getLong("database.registerUnknown.defaultGroupId"); if (defaultGroupId != 0) { device.setGroupId(defaultGroupId); } -- cgit v1.2.3