Skip to content

Commit

Permalink
Adapt to core changes (#557)
Browse files Browse the repository at this point in the history
Signed-off-by: Jan N. Klug <[email protected]>
  • Loading branch information
J-N-K committed Jan 2, 2024
1 parent 50269fc commit 315858c
Show file tree
Hide file tree
Showing 7 changed files with 70 additions and 205 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,25 +16,22 @@
import static org.smarthomej.binding.amazonechocontrol.internal.AmazonEchoControlBindingConstants.*;

import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
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.core.config.discovery.AbstractDiscoveryService;
import org.openhab.core.config.discovery.AbstractThingHandlerDiscoveryService;
import org.openhab.core.config.discovery.DiscoveryResult;
import org.openhab.core.config.discovery.DiscoveryResultBuilder;
import org.openhab.core.thing.ThingTypeUID;
import org.openhab.core.thing.ThingUID;
import org.openhab.core.thing.binding.ThingHandler;
import org.openhab.core.thing.binding.ThingHandlerService;
import org.osgi.service.component.annotations.Activate;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.ServiceScope;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.smarthomej.binding.amazonechocontrol.internal.connection.Connection;
Expand All @@ -49,66 +46,39 @@
* @author Michael Geramb - Initial contribution
* @author Jan N. Klug - Refactored to ThingHandlerService
*/
@Component(scope = ServiceScope.PROTOTYPE, service = AmazonEchoDiscovery.class)
@NonNullByDefault
public class AmazonEchoDiscovery extends AbstractDiscoveryService implements ThingHandlerService {
public class AmazonEchoDiscovery extends AbstractThingHandlerDiscoveryService<AccountHandler> {
private static final int BACKGROUND_INTERVAL = 10; // in seconds
private @Nullable AccountHandler accountHandler;
private final Logger logger = LoggerFactory.getLogger(AmazonEchoDiscovery.class);
private final Set<List<EnabledFeedTO>> discoveredFlashBriefings = new HashSet<>();

private @Nullable ScheduledFuture<?> startScanStateJob;
private @Nullable Long activateTimeStamp;

public AmazonEchoDiscovery() {
super(SUPPORTED_ECHO_THING_TYPES_UIDS, 5);
}

@Override
public void setThingHandler(ThingHandler thingHandler) {
this.accountHandler = (AccountHandler) thingHandler;
}

@Override
public @Nullable ThingHandler getThingHandler() {
return accountHandler;
}

public void activate() {
activate(new HashMap<>());
}

@Override
public void deactivate() {
super.deactivate();
super(AccountHandler.class, SUPPORTED_ECHO_THING_TYPES_UIDS, 5);
}

@Override
protected void startScan() {
AccountHandler accountHandler = this.accountHandler;
if (accountHandler == null) {
return;
}
stopScanJob();
final Long activateTimeStamp = this.activateTimeStamp;
if (activateTimeStamp != null) {
removeOlderResults(activateTimeStamp);
}
setDevices(accountHandler.updateDeviceList());
setDevices(thingHandler.updateDeviceList());

List<EnabledFeedTO> currentFlashBriefingConfiguration = accountHandler.updateFlashBriefingHandlers();
List<EnabledFeedTO> currentFlashBriefingConfiguration = thingHandler.updateFlashBriefingHandlers();
discoverFlashBriefingProfiles(currentFlashBriefingConfiguration);
}

protected void startAutomaticScan() {
AccountHandler accountHandler = this.accountHandler;
if (accountHandler == null) {
return;
}
if (!accountHandler.getThing().getThings().isEmpty()) {
if (!thingHandler.getThing().getThings().isEmpty()) {
stopScanJob();
return;
}
Connection connection = accountHandler.getConnection();
Connection connection = thingHandler.getConnection();
// do discovery only if logged in and last login is more than 10 s ago
Date verifyTime = connection.getVerifyTime();
if (verifyTime == null || System.currentTimeMillis() < (verifyTime.getTime() + 10000)) {
Expand Down Expand Up @@ -140,22 +110,14 @@ void stopScanJob() {
}

@Override
@Activate
public void activate(@Nullable Map<String, Object> config) {
super.activate(config);
if (config != null) {
modified(config);
}
public void initialize() {
if (activateTimeStamp == null) {
activateTimeStamp = new Date().getTime();
}
super.initialize();
}

private synchronized void setDevices(List<DeviceTO> deviceList) {
AccountHandler accountHandler = this.accountHandler;
if (accountHandler == null) {
return;
}
for (DeviceTO device : deviceList) {
String serialNumber = device.serialNumber;
if (serialNumber != null) {
Expand All @@ -180,7 +142,7 @@ private synchronized void setDevices(List<DeviceTO> deviceList) {
continue;
}

ThingUID bridgeThingUID = accountHandler.getThing().getUID();
ThingUID bridgeThingUID = thingHandler.getThing().getUID();
ThingUID thingUID = new ThingUID(thingTypeId, bridgeThingUID, serialNumber);

DiscoveryResult result = DiscoveryResultBuilder.create(thingUID).withLabel(device.accountName)
Expand All @@ -201,18 +163,17 @@ private synchronized void setDevices(List<DeviceTO> deviceList) {
}

private synchronized void discoverFlashBriefingProfiles(List<EnabledFeedTO> enabledFeeds) {
AccountHandler accountHandler = this.accountHandler;
if (accountHandler == null || enabledFeeds.isEmpty()) {
if (enabledFeeds.isEmpty()) {
return;
}

if (!discoveredFlashBriefings.contains(enabledFeeds)) {
ThingUID bridgeThingUID = accountHandler.getThing().getUID();
ThingUID bridgeThingUID = thingHandler.getThing().getUID();
ThingUID freeThingUID = new ThingUID(THING_TYPE_FLASH_BRIEFING_PROFILE, bridgeThingUID,
Integer.toString(enabledFeeds.hashCode()));
DiscoveryResult result = DiscoveryResultBuilder.create(freeThingUID).withLabel("FlashBriefing")
.withProperty(DEVICE_PROPERTY_FLASH_BRIEFING_PROFILE, enabledFeeds)
.withBridge(accountHandler.getThing().getUID()).build();
.withBridge(thingHandler.getThing().getUID()).build();
logger.debug("Flash Briefing {} discovered", enabledFeeds);
thingDiscovered(result);
discoveredFlashBriefings.add(enabledFeeds);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@

import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.openhab.core.config.discovery.AbstractDiscoveryService;
import org.openhab.core.config.discovery.AbstractThingHandlerDiscoveryService;
import org.openhab.core.config.discovery.DiscoveryResult;
import org.openhab.core.config.discovery.DiscoveryResultBuilder;
import org.openhab.core.thing.ThingUID;
import org.openhab.core.thing.binding.ThingHandler;
import org.openhab.core.thing.binding.ThingHandlerService;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.ServiceScope;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.smarthomej.binding.amazonechocontrol.internal.dto.smarthome.JsonSmartHomeDevice;
Expand All @@ -46,43 +46,20 @@
* @author Lukas Knoeller - Initial contribution
* @author Jan N. Klug - Refactored to ThingHandlerService
*/
@Component(scope = ServiceScope.PROTOTYPE, service = SmartHomeDevicesDiscovery.class)
@NonNullByDefault
public class SmartHomeDevicesDiscovery extends AbstractDiscoveryService implements ThingHandlerService {
private @Nullable AccountHandler accountHandler;
public class SmartHomeDevicesDiscovery extends AbstractThingHandlerDiscoveryService<AccountHandler> {
private final Logger logger = LoggerFactory.getLogger(SmartHomeDevicesDiscovery.class);

private @Nullable ScheduledFuture<?> discoveryJob;

public SmartHomeDevicesDiscovery() {
super(SUPPORTED_SMART_HOME_THING_TYPES_UIDS, 10);
}

@Override
public void setThingHandler(ThingHandler thingHandler) {
this.accountHandler = (AccountHandler) thingHandler;
}

@Override
public @Nullable ThingHandler getThingHandler() {
return accountHandler;
}

public void activate() {
super.activate(null);
}

@Override
public void deactivate() {
super.deactivate();
super(AccountHandler.class, SUPPORTED_SMART_HOME_THING_TYPES_UIDS, 10);
}

@Override
protected void startScan() {
AccountHandler accountHandler = this.accountHandler;
if (accountHandler == null) {
return;
}
setSmartHomeDevices(accountHandler.updateSmartHomeDeviceList(false));
setSmartHomeDevices(thingHandler.updateSmartHomeDeviceList(false));
}

@Override
Expand All @@ -109,17 +86,13 @@ protected void stopBackgroundDiscovery() {
}

private synchronized void setSmartHomeDevices(List<SmartHomeBaseDevice> deviceList) {
AccountHandler accountHandler = this.accountHandler;
if (accountHandler == null) {
return;
}
int smartHomeDeviceDiscoveryMode = accountHandler.getSmartHomeDevicesDiscoveryMode();
int smartHomeDeviceDiscoveryMode = thingHandler.getSmartHomeDevicesDiscoveryMode();
if (smartHomeDeviceDiscoveryMode == 0) {
return;
}

for (Object smartHomeDevice : deviceList) {
ThingUID bridgeThingUID = accountHandler.getThing().getUID();
ThingUID bridgeThingUID = thingHandler.getThing().getUID();
ThingUID thingUID = null;
String deviceName = null;
Map<String, Object> props = new HashMap<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
import org.openhab.core.thing.binding.ThingActions;
import org.openhab.core.thing.binding.ThingActionsScope;
import org.openhab.core.thing.binding.ThingHandler;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.ServiceScope;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand All @@ -28,6 +30,7 @@
*
* @author Tom Blum - Initial contribution
*/
@Component(scope = ServiceScope.PROTOTYPE, service = NotificationsForFireTVThingActions.class)
@ThingActionsScope(name = "notificationsforfiretv")
@NonNullByDefault
public class NotificationsForFireTVThingActions implements ThingActions {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,13 @@

import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.openhab.core.config.discovery.AbstractDiscoveryService;
import org.openhab.core.config.discovery.AbstractThingHandlerDiscoveryService;
import org.openhab.core.config.discovery.DiscoveryResult;
import org.openhab.core.config.discovery.DiscoveryResultBuilder;
import org.openhab.core.thing.ThingTypeUID;
import org.openhab.core.thing.ThingUID;
import org.openhab.core.thing.binding.ThingHandler;
import org.openhab.core.thing.binding.ThingHandlerService;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.ServiceScope;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.smarthomej.binding.telenot.internal.handler.TelenotBridgeHandler;
Expand All @@ -38,56 +38,34 @@
*
* @author Ronny Grun - Initial contribution
*/
@Component(scope = ServiceScope.PROTOTYPE, service = TelenotDiscoveryService.class)
@NonNullByDefault
public class TelenotDiscoveryService extends AbstractDiscoveryService implements ThingHandlerService {
public class TelenotDiscoveryService extends AbstractThingHandlerDiscoveryService<TelenotBridgeHandler> {

private final Logger logger = LoggerFactory.getLogger(TelenotDiscoveryService.class);

private @Nullable ScheduledFuture<?> scanningJob;
private @Nullable TelenotBridgeHandler bridgeHandler;
private @Nullable ThingUID bridgeUID;
private @NonNullByDefault({}) ThingUID bridgeUID;

public TelenotDiscoveryService() {
super(DISCOVERABLE_DEVICE_TYPE_UIDS, 30, true);
super(TelenotBridgeHandler.class, DISCOVERABLE_DEVICE_TYPE_UIDS, 30, true);
}

@Override
public void setThingHandler(ThingHandler thingHandler) {
if (thingHandler instanceof TelenotBridgeHandler) {
this.bridgeHandler = (TelenotBridgeHandler) thingHandler;
this.bridgeUID = thingHandler.getThing().getUID();
}
public void initialize() {
this.bridgeUID = thingHandler.getThing().getUID();
super.initialize();
}

@Override
public Set<ThingTypeUID> getSupportedThingTypes() {
return DISCOVERABLE_DEVICE_TYPE_UIDS;
}

@Override
public @Nullable ThingHandler getThingHandler() {
return bridgeHandler;
}

@Override
public void activate() {
super.activate(null);
}

@Override
public void deactivate() {
super.deactivate();
}

@Override
protected void startScan() {
TelenotBridgeHandler bridgeHandler = this.bridgeHandler;
if (bridgeHandler == null) {
logger.warn("Tried to scan for results but bridge handler is not set in discovery service");
return;
}
stopScan();
bridgeHandler.getUsedSecurityArea().forEach(this::buildDiscoveryResult);
thingHandler.getUsedSecurityArea().forEach(this::buildDiscoveryResult);

// we clear all older results, they are not valid any longer and we created new results
removeOlderResults(getTimestampOfLastScan());
Expand All @@ -111,12 +89,6 @@ protected void stopBackgroundDiscovery() {
}

private void buildDiscoveryResult(String address) {
ThingUID bridgeUID = this.bridgeUID;
if (bridgeUID == null) {
logger.warn("BridgeUid is not set but a discovery result has been produced. This should not happen.");
return;
}

ThingUID uid = new ThingUID(THING_TYPE_SB, bridgeUID, address);
Map<String, Object> properties = Map.ofEntries( //
entry(PROPERTY_ADDRESS, Integer.parseInt(address)), //
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
import org.openhab.core.thing.binding.ThingActions;
import org.openhab.core.thing.binding.ThingActionsScope;
import org.openhab.core.thing.binding.ThingHandler;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.ServiceScope;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.smarthomej.binding.telenot.internal.handler.TelenotBridgeHandler;
Expand All @@ -28,6 +30,7 @@
*
* @author Ronny Grun - Initial contribution
*/
@Component(scope = ServiceScope.PROTOTYPE, service = BridgeActions.class)
@ThingActionsScope(name = "telenot")
@NonNullByDefault
public class BridgeActions implements ThingActions {
Expand Down
Loading

0 comments on commit 315858c

Please sign in to comment.