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