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.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;
private final Logger logger = LoggerFactory.getLogger(TouchWandUnitDiscoveryService.class);
private @Nullable ScheduledFuture<?> scanningJob;
- private CopyOnWriteArraySet<TouchWandUnitStatusUpdateListener> listeners = new CopyOnWriteArraySet<>();
public TouchWandUnitDiscoveryService() {
super(SUPPORTED_THING_TYPES_UIDS, SEARCH_TIME_SEC, true);
default:
continue;
}
- notifyListeners(touchWandUnit);
}
} catch (JsonSyntaxException e) {
logger.warn("Could not parse unit {}", e.getMessage());
}
}
- private void notifyListeners(TouchWandUnitData touchWandUnit) {
- for (TouchWandUnitStatusUpdateListener listener : listeners) {
- listener.onDataReceived(touchWandUnit);
- }
- }
-
@Override
protected void stopScan() {
removeOlderResults(getTimestampOfLastScan());
}
}
- 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;
public void setThingHandler(@NonNullByDefault({}) ThingHandler handler) {
if (handler instanceof TouchWandBridgeHandler) {
touchWandBridgeHandler = (TouchWandBridgeHandler) handler;
- registerListener(touchWandBridgeHandler);
}
}