Added capability to enable / disable background discovery service as well as tune its frenquency (#15222)

Signed-off-by: clinique <gael@lhopital.org>
This commit is contained in:
Gaël L'hopital 2023-07-12 21:27:34 +02:00 committed by GitHub
parent 28e6da6342
commit 9814047e21
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 46 additions and 34 deletions

View File

@ -54,13 +54,14 @@ FreeboxOS binding has the following configuration parameters:
### API bridge ### API bridge
| Parameter Label | Parameter ID | Description | Required | Default | | Parameter Label | Parameter ID | Description | Required | Default |
|--------------------------|-------------------|--------------------------------------------------------|----------|----------------------| |-------------------------------|-------------------|--------------------------------------------------------|----------|----------------------|
| Freebox Server Address | apiDomain | The domain to use in place of hardcoded Freebox ip | No | mafreebox.freebox.fr | | Freebox Server Address | apiDomain | The domain to use in place of hardcoded Freebox ip | No | mafreebox.freebox.fr |
| Application Token | appToken | Token generated by the Freebox Server. | Yes | | | Application Token | appToken | Token generated by the Freebox Server. | Yes | |
| Network Device Discovery | discoverNetDevice | Enable the discovery of network device things. | No | false | | Network Device Discovery | discoverNetDevice | Enable the discovery of network device things. | No | false |
| HTTPS Available | httpsAvailable | Tells if https has been configured on the Freebox | No | false | | Background Discovery Interval | discoveryInterval | Interval in minutes - 0 disables background discovery | No | 10 |
| HTTPS port | httpsPort | Port to use for remote https access to the Freebox Api | No | 15682 | | HTTPS Available | httpsAvailable | Tells if https has been configured on the Freebox | No | false |
| HTTPS port | httpsPort | Port to use for remote https access to the Freebox Api | No | 15682 |
If the parameter *apiDomain* is not set, the binding will use the default address used by Free to access your Freebox Server (mafreebox.freebox.fr). If the parameter *apiDomain* is not set, the binding will use the default address used by Free to access your Freebox Server (mafreebox.freebox.fr).
The bridge thing will initialize only if a valid application token (parameter *appToken*) is filled. The bridge thing will initialize only if a valid application token (parameter *appToken*) is filled.

View File

@ -33,6 +33,7 @@ public class FreeboxOsConfiguration {
private String apiDomain = FreeboxTlsCertificateProvider.DEFAULT_NAME; private String apiDomain = FreeboxTlsCertificateProvider.DEFAULT_NAME;
public String appToken = ""; public String appToken = "";
public boolean discoverNetDevice; public boolean discoverNetDevice;
public int discoveryInterval = 10;
private int httpsPort = 15682; private int httpsPort = 15682;
private boolean httpsAvailable; private boolean httpsAvailable;

View File

@ -70,7 +70,6 @@ import inet.ipaddr.mac.MACAddress;
@NonNullByDefault @NonNullByDefault
public class FreeboxOsDiscoveryService extends AbstractDiscoveryService implements ThingHandlerService { public class FreeboxOsDiscoveryService extends AbstractDiscoveryService implements ThingHandlerService {
private static final int DISCOVERY_TIME_SECONDS = 10; private static final int DISCOVERY_TIME_SECONDS = 10;
private static final int BACKGROUND_SCAN_REFRESH_MINUTES = 1;
private final Logger logger = LoggerFactory.getLogger(FreeboxOsDiscoveryService.class); private final Logger logger = LoggerFactory.getLogger(FreeboxOsDiscoveryService.class);
@ -89,8 +88,8 @@ public class FreeboxOsDiscoveryService extends AbstractDiscoveryService implemen
@Override @Override
public void setThingHandler(@Nullable ThingHandler handler) { public void setThingHandler(@Nullable ThingHandler handler) {
if (handler instanceof FreeboxOsHandler) { if (handler instanceof FreeboxOsHandler freeboxosHandler) {
bridgeHandler = (FreeboxOsHandler) handler; bridgeHandler = freeboxosHandler;
activate(null); activate(null);
} }
} }
@ -103,8 +102,14 @@ public class FreeboxOsDiscoveryService extends AbstractDiscoveryService implemen
@Override @Override
protected void startBackgroundDiscovery() { protected void startBackgroundDiscovery() {
stopBackgroundDiscovery(); stopBackgroundDiscovery();
backgroundFuture = Optional.of(scheduler.scheduleWithFixedDelay(this::startScan, FreeboxOsHandler handler = bridgeHandler;
BACKGROUND_SCAN_REFRESH_MINUTES, BACKGROUND_SCAN_REFRESH_MINUTES, TimeUnit.MINUTES)); if (handler != null) {
int interval = handler.getConfiguration().discoveryInterval;
if (interval > 0) {
backgroundFuture = Optional
.of(scheduler.scheduleWithFixedDelay(this::startScan, 1, interval, TimeUnit.MINUTES));
}
}
} }
@Override @Override
@ -116,23 +121,23 @@ public class FreeboxOsDiscoveryService extends AbstractDiscoveryService implemen
@Override @Override
protected void startScan() { protected void startScan() {
logger.debug("Starting Freebox discovery scan"); logger.debug("Starting Freebox discovery scan");
FreeboxOsHandler localHandler = bridgeHandler; FreeboxOsHandler handler = bridgeHandler;
if (localHandler != null && localHandler.getThing().getStatus() == ThingStatus.ONLINE) { if (handler != null && handler.getThing().getStatus() == ThingStatus.ONLINE) {
try { try {
ThingUID bridgeUID = localHandler.getThing().getUID(); ThingUID bridgeUID = handler.getThing().getUID();
List<LanHost> lanHosts = localHandler.getManager(LanBrowserManager.class).getHosts().stream() List<LanHost> lanHosts = handler.getManager(LanBrowserManager.class).getHosts().stream()
.filter(LanHost::reachable).collect(Collectors.toList()); .filter(LanHost::reachable).toList();
discoverServer(localHandler.getManager(SystemManager.class), bridgeUID); discoverServer(handler.getManager(SystemManager.class), bridgeUID);
discoverPhone(localHandler.getManager(PhoneManager.class), bridgeUID); discoverPhone(handler.getManager(PhoneManager.class), bridgeUID);
discoverPlugs(localHandler.getManager(FreeplugManager.class), bridgeUID); discoverPlugs(handler.getManager(FreeplugManager.class), bridgeUID);
discoverRepeater(localHandler.getManager(RepeaterManager.class), bridgeUID, lanHosts); discoverRepeater(handler.getManager(RepeaterManager.class), bridgeUID, lanHosts);
discoverPlayer(localHandler.getManager(PlayerManager.class), bridgeUID, lanHosts); discoverPlayer(handler.getManager(PlayerManager.class), bridgeUID, lanHosts);
discoverVM(localHandler.getManager(VmManager.class), bridgeUID, lanHosts); discoverVM(handler.getManager(VmManager.class), bridgeUID, lanHosts);
discoverHome(localHandler.getManager(HomeManager.class), bridgeUID); discoverHome(handler.getManager(HomeManager.class), bridgeUID);
if (localHandler.getConfiguration().discoverNetDevice) { if (handler.getConfiguration().discoverNetDevice) {
discoverHosts(localHandler, bridgeUID, lanHosts); discoverHosts(handler, bridgeUID, lanHosts);
} }
} catch (FreeboxException e) { } catch (FreeboxException e) {
logger.warn("Error while requesting data for things discovery: {}", e.getMessage()); logger.warn("Error while requesting data for things discovery: {}", e.getMessage());
@ -181,10 +186,9 @@ public class FreeboxOsDiscoveryService extends AbstractDiscoveryService implemen
throws FreeboxException { throws FreeboxException {
try { try {
List<MACAddress> wifiMacs = new ArrayList<>(); List<MACAddress> wifiMacs = new ArrayList<>();
wifiMacs.addAll(localHandler.getManager(APManager.class).getStations().stream().map(Station::mac) wifiMacs.addAll(localHandler.getManager(APManager.class).getStations().stream().map(Station::mac).toList());
.collect(Collectors.toList())); wifiMacs.addAll(
wifiMacs.addAll(localHandler.getManager(RepeaterManager.class).getHosts().stream().map(LanHost::getMac) localHandler.getManager(RepeaterManager.class).getHosts().stream().map(LanHost::getMac).toList());
.collect(Collectors.toList()));
lanHosts.forEach(lanHost -> { lanHosts.forEach(lanHost -> {
MACAddress mac = lanHost.getMac(); MACAddress mac = lanHost.getMac();
@ -253,9 +257,8 @@ public class FreeboxOsDiscoveryService extends AbstractDiscoveryService implemen
logger.debug("Adding new Freebox Server {} to inbox", thingUID); logger.debug("Adding new Freebox Server {} to inbox", thingUID);
DiscoveryResult discoveryResult = DiscoveryResultBuilder.create(thingUID).withBridge(bridgeUID) DiscoveryResult discoveryResult = DiscoveryResultBuilder.create(thingUID).withBridge(bridgeUID)
.withProperty(Thing.PROPERTY_MAC_ADDRESS, config.mac())
.withRepresentationProperty(Thing.PROPERTY_MAC_ADDRESS).withLabel(config.modelInfo().prettyName()) .withRepresentationProperty(Thing.PROPERTY_MAC_ADDRESS).withLabel(config.modelInfo().prettyName())
.build(); .withProperty(Thing.PROPERTY_MAC_ADDRESS, config.mac()).build();
thingDiscovered(discoveryResult); thingDiscovered(discoveryResult);
} catch (PermissionException e) { } catch (PermissionException e) {
logger.warn("Missing permission to discover Server {}", e.getPermission()); logger.warn("Missing permission to discover Server {}", e.getPermission());
@ -270,9 +273,8 @@ public class FreeboxOsDiscoveryService extends AbstractDiscoveryService implemen
ThingUID thingUID = new ThingUID(player.apiAvailable() ? THING_TYPE_ACTIVE_PLAYER : THING_TYPE_PLAYER, ThingUID thingUID = new ThingUID(player.apiAvailable() ? THING_TYPE_ACTIVE_PLAYER : THING_TYPE_PLAYER,
bridgeUID, Integer.toString(player.id())); bridgeUID, Integer.toString(player.id()));
DiscoveryResult discoveryResult = DiscoveryResultBuilder.create(thingUID).withBridge(bridgeUID) DiscoveryResult discoveryResult = DiscoveryResultBuilder.create(thingUID).withBridge(bridgeUID)
.withLabel(player.deviceName())
.withProperty(Thing.PROPERTY_MAC_ADDRESS, player.mac().toColonDelimitedString()) .withProperty(Thing.PROPERTY_MAC_ADDRESS, player.mac().toColonDelimitedString())
.withProperty(ClientConfiguration.ID, player.id()) .withProperty(ClientConfiguration.ID, player.id()).withLabel(player.deviceName())
.withRepresentationProperty(Thing.PROPERTY_MAC_ADDRESS).build(); .withRepresentationProperty(Thing.PROPERTY_MAC_ADDRESS).build();
thingDiscovered(discoveryResult); thingDiscovered(discoveryResult);
} }

View File

@ -17,6 +17,14 @@
<context>password</context> <context>password</context>
<description>Token generated by the Freebox server</description> <description>Token generated by the Freebox server</description>
</parameter> </parameter>
<parameter name="discoveryInterval" type="integer" min="0" max="10080" required="false">
<label>Background Discovery Interval</label>
<description>
Background discovery interval in minutes (default 10 - 0 disables background discovery)
</description>
<default>10</default>
<advanced>true</advanced>
</parameter>
<parameter name="discoverNetDevice" type="boolean"> <parameter name="discoverNetDevice" type="boolean">
<label>Network Device Discovery</label> <label>Network Device Discovery</label>
<description>Enable the discovery of network device things</description> <description>Enable the discovery of network device things</description>