diff options
Diffstat (limited to 'src/org/traccar/database/DeviceManager.java')
-rw-r--r-- | src/org/traccar/database/DeviceManager.java | 52 |
1 files changed, 43 insertions, 9 deletions
diff --git a/src/org/traccar/database/DeviceManager.java b/src/org/traccar/database/DeviceManager.java index f5fde9e1f..ae5784b01 100644 --- a/src/org/traccar/database/DeviceManager.java +++ b/src/org/traccar/database/DeviceManager.java @@ -19,6 +19,7 @@ import java.sql.SQLException; import java.util.ArrayList; import java.util.Collection; import java.util.HashSet; +import java.util.Iterator; import java.util.LinkedList; import java.util.List; import java.util.Map; @@ -49,6 +50,7 @@ public class DeviceManager implements IdentityManager { private Map<Long, Device> devicesById; private Map<String, Device> devicesByUniqueId; + private Map<String, Device> devicesByPhone; private AtomicLong devicesLastUpdate = new AtomicLong(); private Map<Long, Group> groupsById; @@ -90,24 +92,39 @@ public class DeviceManager implements IdentityManager { if (devicesByUniqueId == null) { devicesByUniqueId = new ConcurrentHashMap<>(databaseDevices.size()); } + if (devicesByPhone == null) { + devicesByPhone = new ConcurrentHashMap<>(databaseDevices.size()); + } Set<Long> databaseDevicesIds = new HashSet<>(); Set<String> databaseDevicesUniqueIds = new HashSet<>(); + Set<String> databaseDevicesPhones = new HashSet<>(); for (Device device : databaseDevices) { databaseDevicesIds.add(device.getId()); databaseDevicesUniqueIds.add(device.getUniqueId()); + databaseDevicesPhones.add(device.getPhone()); if (devicesById.containsKey(device.getId())) { Device cachedDevice = devicesById.get(device.getId()); cachedDevice.setName(device.getName()); cachedDevice.setGroupId(device.getGroupId()); + cachedDevice.setCategory(device.getCategory()); + cachedDevice.setContact(device.getContact()); + cachedDevice.setModel(device.getModel()); cachedDevice.setAttributes(device.getAttributes()); if (!device.getUniqueId().equals(cachedDevice.getUniqueId())) { - devicesByUniqueId.remove(cachedDevice.getUniqueId()); devicesByUniqueId.put(device.getUniqueId(), cachedDevice); } cachedDevice.setUniqueId(device.getUniqueId()); + if (device.getPhone() != null && !device.getPhone().isEmpty() + && !device.getPhone().equals(cachedDevice.getPhone())) { + devicesByPhone.put(device.getPhone(), cachedDevice); + } + cachedDevice.setPhone(device.getPhone()); } else { devicesById.put(device.getId(), device); devicesByUniqueId.put(device.getUniqueId(), device); + if (device.getPhone() != null && !device.getPhone().isEmpty()) { + devicesByPhone.put(device.getPhone(), device); + } if (geofenceManager != null) { Position lastPosition = getLastPosition(device.getId()); if (lastPosition != null) { @@ -117,18 +134,21 @@ public class DeviceManager implements IdentityManager { device.setStatus(Device.STATUS_OFFLINE); } } - for (Long cachedDeviceId : devicesById.keySet()) { - if (!databaseDevicesIds.contains(cachedDeviceId)) { - devicesById.remove(cachedDeviceId); + for (Iterator<Long> iterator = devicesById.keySet().iterator(); iterator.hasNext();) { + if (!databaseDevicesIds.contains(iterator.next())) { + iterator.remove(); + } + } + for (Iterator<String> iterator = devicesByUniqueId.keySet().iterator(); iterator.hasNext();) { + if (!databaseDevicesUniqueIds.contains(iterator.next())) { + iterator.remove(); } } - for (String cachedDeviceUniqId : devicesByUniqueId.keySet()) { - if (!databaseDevicesUniqueIds.contains(cachedDeviceUniqId)) { - devicesByUniqueId.remove(cachedDeviceUniqId); + for (Iterator<String> iterator = devicesByPhone.keySet().iterator(); iterator.hasNext();) { + if (!databaseDevicesPhones.contains(iterator.next())) { + iterator.remove(); } } - databaseDevicesIds.clear(); - databaseDevicesUniqueIds.clear(); } } @@ -146,6 +166,10 @@ public class DeviceManager implements IdentityManager { return devicesByUniqueId.get(uniqueId); } + public Device getDeviceByPhone(String phone) { + return devicesByPhone.get(phone); + } + public Collection<Device> getAllDevices() { boolean forceUpdate = devicesById.isEmpty(); @@ -180,6 +204,9 @@ public class DeviceManager implements IdentityManager { devicesById.put(device.getId(), device); devicesByUniqueId.put(device.getUniqueId(), device); + if (device.getPhone() != null && !device.getPhone().isEmpty()) { + devicesByPhone.put(device.getPhone(), device); + } } public void updateDevice(Device device) throws SQLException { @@ -187,6 +214,9 @@ public class DeviceManager implements IdentityManager { devicesById.put(device.getId(), device); devicesByUniqueId.put(device.getUniqueId(), device); + if (device.getPhone() != null && !device.getPhone().isEmpty()) { + devicesByPhone.put(device.getPhone(), device); + } } public void updateDeviceStatus(Device device) throws SQLException { @@ -202,8 +232,12 @@ public class DeviceManager implements IdentityManager { if (devicesById.containsKey(deviceId)) { String deviceUniqueId = devicesById.get(deviceId).getUniqueId(); + String phone = devicesById.get(deviceId).getPhone(); devicesById.remove(deviceId); devicesByUniqueId.remove(deviceUniqueId); + if (phone != null && !phone.isEmpty()) { + devicesByPhone.remove(phone); + } } positions.remove(deviceId); } |