try (BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()))) {
String line = in.readLine();
while (!Thread.currentThread().isInterrupted() && line != null) {
- if (!line.isEmpty()) {
- logger.trace("Received from pilight: {}", line);
+ logger.trace("Received from pilight: {}", line);
+ // ignore empty lines and lines starting with "status"
+ if (!line.isEmpty() && !line.startsWith("{\"status\":")) {
final ObjectMapper inputMapper = this.inputMapper;
if (line.startsWith("{\"message\":\"config\"")) {
final @Nullable Message message = inputMapper.readValue(line, Message.class);
} else if (line.startsWith("{\"version\":")) {
final @Nullable Version version = inputMapper.readValue(line, Version.class);
callback.versionReceived(version);
- } else if (line.startsWith("{\"status\":")) {
- // currently unused
} else if ("1".equals(line)) {
throw new IOException("Connection to pilight lost");
} else {
import org.openhab.core.config.discovery.DiscoveryResult;
import org.openhab.core.config.discovery.DiscoveryResultBuilder;
import org.openhab.core.config.discovery.DiscoveryService;
-import org.openhab.core.thing.ThingTypeUID;
import org.openhab.core.thing.ThingUID;
import org.osgi.service.component.annotations.Component;
import org.slf4j.Logger;
private @Nullable ScheduledFuture<?> backgroundDiscoveryJob;
public PilightBridgeDiscoveryService() throws IllegalArgumentException {
- super(getSupportedThingTypeUIDs(), AUTODISCOVERY_SEARCH_TIME_SEC, true);
- }
-
- public static Set<ThingTypeUID> getSupportedThingTypeUIDs() {
- return Set.of(PilightBindingConstants.THING_TYPE_BRIDGE);
+ super(Set.of(PilightBindingConstants.THING_TYPE_BRIDGE), AUTODISCOVERY_SEARCH_TIME_SEC, true);
}
@Override
@Override
protected void startBackgroundDiscovery() {
- logger.debug("Start Pilight device background discovery");
+ logger.debug("Start Pilight bridge background discovery");
final @Nullable ScheduledFuture<?> backgroundDiscoveryJob = this.backgroundDiscoveryJob;
if (backgroundDiscoveryJob == null || backgroundDiscoveryJob.isCancelled()) {
this.backgroundDiscoveryJob = scheduler.scheduleWithFixedDelay(this::startScan, 5,
@Override
protected void stopBackgroundDiscovery() {
- logger.debug("Stop Pilight device background discovery");
+ logger.debug("Stop Pilight bridge background discovery");
final @Nullable ScheduledFuture<?> backgroundDiscoveryJob = this.backgroundDiscoveryJob;
if (backgroundDiscoveryJob != null) {
backgroundDiscoveryJob.cancel(true);
import static org.openhab.binding.pilight.internal.PilightBindingConstants.*;
-import java.util.Date;
-import java.util.HashMap;
+import java.time.Instant;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
-import org.openhab.binding.pilight.internal.PilightHandlerFactory;
import org.openhab.binding.pilight.internal.dto.Config;
import org.openhab.binding.pilight.internal.dto.DeviceType;
import org.openhab.binding.pilight.internal.dto.Status;
@Component(scope = ServiceScope.PROTOTYPE, service = PilightDeviceDiscoveryService.class)
@NonNullByDefault
public class PilightDeviceDiscoveryService extends AbstractThingHandlerDiscoveryService<PilightBridgeHandler> {
- private static final Set<ThingTypeUID> SUPPORTED_THING_TYPES_UIDS = PilightHandlerFactory.SUPPORTED_THING_TYPES_UIDS;
+ private static final Set<ThingTypeUID> SUPPORTED_THING_TYPES_UIDS = Set.of(THING_TYPE_CONTACT, THING_TYPE_DIMMER,
+ THING_TYPE_GENERIC, THING_TYPE_SWITCH);
private static final int AUTODISCOVERY_SEARCH_TIME_SEC = 10;
- private static final int AUTODISCOVERY_BACKGROUND_SEARCH_INTERVAL_SEC = 60 * 10;
+ private static final int AUTODISCOVERY_BACKGROUND_SEARCH_INITIAL_DELAY_SEC = 20;
+ private static final int AUTODISCOVERY_BACKGROUND_SEARCH_INTERVAL_SEC = 10 * 60; // 10 minutes
private final Logger logger = LoggerFactory.getLogger(PilightDeviceDiscoveryService.class);
- private @NonNullByDefault({}) ThingUID bridgeUID;
-
private @Nullable ScheduledFuture<?> backgroundDiscoveryJob;
private CompletableFuture<Config> configFuture;
private CompletableFuture<List<Status>> statusFuture;
statusFuture = new CompletableFuture<>();
configFuture.thenAcceptBoth(statusFuture, (config, allStatus) -> {
- removeOlderResults(getTimestampOfLastScan(), bridgeUID);
+ removeOlderResults(getTimestampOfLastScan(), thingHandler.getThing().getUID());
config.getDevices().forEach((deviceId, device) -> {
final Optional<Status> status = allStatus.stream().filter(s -> s.getDevices().contains(deviceId))
.findFirst();
final ThingUID thingUID = new ThingUID(thingTypeUID, thingHandler.getThing().getUID(), deviceId);
- final Map<String, Object> properties = new HashMap<>();
- properties.put(PROPERTY_NAME, deviceId);
-
DiscoveryResult discoveryResult = DiscoveryResultBuilder.create(thingUID).withThingType(thingTypeUID)
- .withProperties(properties).withBridge(bridgeUID).withRepresentationProperty(PROPERTY_NAME)
+ .withProperties(Map.of(PROPERTY_NAME, deviceId)).withBridge(thingHandler.getThing().getUID())
+ .withRepresentationProperty(PROPERTY_NAME)
.withLabel("Pilight " + typeString + " Device '" + deviceId + "'").build();
thingDiscovered(discoveryResult);
super.stopScan();
configFuture.cancel(true);
statusFuture.cancel(true);
- removeOlderResults(getTimestampOfLastScan(), bridgeUID);
+ removeOlderResults(getTimestampOfLastScan(), thingHandler.getThing().getUID());
}
@Override
logger.debug("Start Pilight device background discovery");
final @Nullable ScheduledFuture<?> backgroundDiscoveryJob = this.backgroundDiscoveryJob;
if (backgroundDiscoveryJob == null || backgroundDiscoveryJob.isCancelled()) {
- this.backgroundDiscoveryJob = scheduler.scheduleWithFixedDelay(this::startScan, 20,
- AUTODISCOVERY_BACKGROUND_SEARCH_INTERVAL_SEC, TimeUnit.SECONDS);
+ this.backgroundDiscoveryJob = scheduler.scheduleWithFixedDelay(this::startScan,
+ AUTODISCOVERY_BACKGROUND_SEARCH_INITIAL_DELAY_SEC, AUTODISCOVERY_BACKGROUND_SEARCH_INTERVAL_SEC,
+ TimeUnit.SECONDS);
}
}
@Override
public void initialize() {
- bridgeUID = thingHandler.getThing().getUID();
- boolean discoveryEnabled = false;
- removeOlderResults(new Date().getTime(), thingHandler.getThing().getUID());
- discoveryEnabled = thingHandler.isBackgroundDiscoveryEnabled();
+ removeOlderResults(Instant.now().toEpochMilli(), thingHandler.getThing().getUID());
thingHandler.registerDiscoveryListener(this);
super.initialize();
- super.modified(Map.of(DiscoveryService.CONFIG_PROPERTY_BACKGROUND_DISCOVERY, discoveryEnabled));
+ super.modified(Map.of(DiscoveryService.CONFIG_PROPERTY_BACKGROUND_DISCOVERY,
+ thingHandler.isBackgroundDiscoveryEnabled()));
}
@Override
public void dispose() {
super.dispose();
- removeOlderResults(getTimestampOfLastScan(), bridgeUID);
+ removeOlderResults(Instant.now().toEpochMilli(), thingHandler.getThing().getUID());
thingHandler.unregisterDiscoveryListener();
}
public void setStatus(List<Status> status) {
statusFuture.complete(status);
}
-
- @Override
- public Set<ThingTypeUID> getSupportedThingTypes() {
- return SUPPORTED_THING_TYPES_UIDS;
- }
}