Skip to content

Commit

Permalink
remove listeners from unit discovery
Browse files Browse the repository at this point in the history
Signed-off-by: Roie Geron <roie.geron@gmail.com>
  • Loading branch information
roieg committed May 14, 2022
1 parent 6c2363d commit a423715
Showing 1 changed file with 1 addition and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,13 @@
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.CopyOnWriteArraySet;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;

import org.eclipse.jdt.annotation.NonNull;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.openhab.binding.touchwand.internal.TouchWandBridgeHandler;
import org.openhab.binding.touchwand.internal.TouchWandUnitStatusUpdateListener;
import org.openhab.binding.touchwand.internal.dto.TouchWandUnitData;
import org.openhab.binding.touchwand.internal.dto.TouchWandUnitFromJson;
import org.openhab.core.config.discovery.AbstractDiscoveryService;
Expand Down Expand Up @@ -60,8 +58,7 @@ public class TouchWandUnitDiscoveryService extends AbstractDiscoveryService impl
private @NonNullByDefault({}) TouchWandBridgeHandler touchWandBridgeHandler;
private final Logger logger = LoggerFactory.getLogger(TouchWandUnitDiscoveryService.class);

private @Nullable ScheduledFuture<?> scanningJob;
private CopyOnWriteArraySet<TouchWandUnitStatusUpdateListener> listeners = new CopyOnWriteArraySet<>();
private @Nullable ScheduledFuture<?> scanningJob;

public TouchWandUnitDiscoveryService() {
super(SUPPORTED_THING_TYPES_UIDS, SEARCH_TIME_SEC, true);
Expand Down Expand Up @@ -123,7 +120,6 @@ protected void startScan() {
default:
continue;
}
notifyListeners(touchWandUnit);
}
} catch (JsonSyntaxException e) {
logger.warn("Could not parse unit {}", e.getMessage());
Expand All @@ -134,12 +130,6 @@ protected void startScan() {
}
}

private void notifyListeners(TouchWandUnitData touchWandUnit) {
for (TouchWandUnitStatusUpdateListener listener : listeners) {
listener.onDataReceived(touchWandUnit);
}
}

@Override
protected void stopScan() {
removeOlderResults(getTimestampOfLastScan());
Expand Down Expand Up @@ -176,18 +166,6 @@ protected void stopBackgroundDiscovery() {
}
}

public void registerListener(TouchWandUnitStatusUpdateListener listener) {
if (!listeners.contains(listener)) {
logger.debug("Adding TouchWandWebSocket listener {}", listener);
listeners.add(listener);
}
}

public void unregisterListener(TouchWandUnitStatusUpdateListener listener) {
logger.debug("Removing TouchWandWebSocket listener {}", listener);
listeners.remove(listener);
}

@Override
public int getScanTimeout() {
return SEARCH_TIME_SEC;
Expand Down Expand Up @@ -215,7 +193,6 @@ private void addDeviceDiscoveryResult(TouchWandUnitData unit, ThingTypeUID typeU
public void setThingHandler(@NonNullByDefault({}) ThingHandler handler) {
if (handler instanceof TouchWandBridgeHandler) {
touchWandBridgeHandler = (TouchWandBridgeHandler) handler;
registerListener(touchWandBridgeHandler);
}
}

Expand Down

0 comments on commit a423715

Please sign in to comment.