import java.util.HashMap;
import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
import org.apache.commons.lang.StringUtils;
import org.openhab.binding.digitalstrom.internal.discovery.DiscoveryServiceManager;
public class DigitalSTROMHandlerFactory extends BaseThingHandlerFactory {
private final Logger logger = LoggerFactory.getLogger(DigitalSTROMHandlerFactory.class);
- private final Map<String, DiscoveryServiceManager> discoveryServiceManagers = new HashMap<>();
+ private final Map<String, DiscoveryServiceManager> discoveryServiceManagers = new ConcurrentHashMap<>();
private Map<ThingUID, BridgeHandler> bridgeHandlers;
protected synchronized void removeHandler(ThingHandler thingHandler) {
if (thingHandler instanceof BridgeHandler) {
String uid = thingHandler.getThing().getUID().getAsString();
- if (discoveryServiceManagers.get(uid) != null) {
- discoveryServiceManagers.get(uid).unregisterDiscoveryServices(bundleContext);
- discoveryServiceManagers.remove(uid);
+ DiscoveryServiceManager discoveryServiceManager = discoveryServiceManagers.remove(uid);
+ if (discoveryServiceManager != null) {
+ discoveryServiceManager.unregisterDiscoveryServices(bundleContext);
}
}
}
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
+import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;
private SceneReadingJobExecutor sceneJobExecutor;
private EventListener eventListener;
- private final List<TrashDevice> trashDevices = new LinkedList<>();
+ private final List<TrashDevice> trashDevices = new CopyOnWriteArrayList<>();
private long lastBinCheck = 0;
private ManagerStates state = ManagerStates.STOPPED;
}
}
}
-
} else {
logger.debug("Found new device!");
if (trashDevices.isEmpty()) {
currentDevice.getDSID());
} else {
logger.debug("Search device in trashDevices.");
- TrashDevice foundTrashDevice = null;
- for (TrashDevice trashDevice : trashDevices) {
- if (trashDevice != null) {
- if (trashDevice.getDevice().equals(currentDevice)) {
- foundTrashDevice = trashDevice;
- logger.debug(
- "Found device in trashDevices, add TrashDevice with dSID {} to the StructureManager!",
- currentDeviceDSID);
- }
+ boolean found = trashDevices.removeIf(trashDevice -> {
+ if (trashDevice.getDevice().equals(currentDevice)) {
+ logger.debug(
+ "Found device in trashDevices, add TrashDevice with dSID {} to the StructureManager!",
+ currentDeviceDSID);
+ strucMan.addDeviceToStructure(trashDevice.getDevice());
+ return true;
+ } else {
+ return false;
}
- }
- if (foundTrashDevice != null) {
- trashDevices.remove(foundTrashDevice);
- strucMan.addDeviceToStructure(foundTrashDevice.getDevice());
- } else {
+ });
+ if (!found) {
strucMan.addDeviceToStructure(currentDevice);
logger.debug(
"Can't find device in trashDevices, add Device with dSID: {} to the StructureManager!",
DeviceStatusListener.DEVICE_DISCOVERY, device.getDSID().getValue());
} else {
logger.debug(
- "The device-Discovery is not registrated, can't inform device discovery about removed device.");
+ "The device-Discovery is not registered, can't inform device discovery about removed device.");
}
}
if (!trashDevices.isEmpty() && (lastBinCheck + config.getBinCheckTime() < System.currentTimeMillis())) {
- for (TrashDevice trashDevice : trashDevices) {
+ trashDevices.removeIf(trashDevice -> {
if (trashDevice.isTimeToDelete(Calendar.getInstance().get(Calendar.DAY_OF_YEAR))) {
- logger.debug("Found trashDevice that have to delete!");
- trashDevices.remove(trashDevice);
- logger.debug("Delete trashDevice: {}", trashDevice.getDevice().getDSID().getValue());
+ logger.debug("Deleted trashDevice: {}", trashDevice.getDevice().getDSID().getValue());
+ return true;
+ } else {
+ return false;
}
- }
+ });
lastBinCheck = System.currentTimeMillis();
}
}
}
} else {
InternalScene oldScene = this.internalSceneMap.get(intScene.getID());
- String oldSceneName = this.internalSceneMap.get(intScene.getID()).getSceneName();
- String newSceneName = intScene.getSceneName();
- if ((oldSceneName.contains("Zone:") && oldSceneName.contains("Group:") && oldSceneName.contains("Scene:"))
- && !(newSceneName.contains("Zone:") && newSceneName.contains("Group:")
- && newSceneName.contains("Scene:"))) {
- oldScene.setSceneName(newSceneName);
- this.discovery.sceneDiscoverd(oldScene);
+ if (oldScene != null) {
+ String oldSceneName = oldScene.getSceneName();
+ String newSceneName = intScene.getSceneName();
+ if ((oldSceneName.contains("Zone:") && oldSceneName.contains("Group:")
+ && oldSceneName.contains("Scene:"))
+ && !(newSceneName.contains("Zone:") && newSceneName.contains("Group:")
+ && newSceneName.contains("Scene:"))) {
+ oldScene.setSceneName(newSceneName);
+ this.discovery.sceneDiscoverd(oldScene);
+ }
}
}
}