Class AbstractDiscoveryService
- All Implemented Interfaces:
DiscoveryService
- Direct Known Subclasses:
AbstractThingHandlerDiscoveryService,SddpDiscoveryService
AbstractDiscoveryService provides methods which handle the DiscoveryListeners.
Subclasses do not have to care about adding and removing those listeners.
They can use the protected methods thingDiscovered(DiscoveryResult) and thingRemoved(ThingUID) in
order to notify the registered DiscoveryListeners.- Author:
- Oliver Libutzki - Initial contribution, Kai Kreuzer - Refactored API, Dennis Nobel - Added background discovery configuration through Configuration Admin, Andre Fuechsel - Added removeOlderResults, Laurent Garnier - Added discovery with an optional input parameter
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected TranslationProviderprotected LocaleProviderprotected @Nullable ScanListenerprotected final ScheduledExecutorServiceFields inherited from interface org.openhab.core.config.discovery.DiscoveryService
CONFIG_PROPERTY_BACKGROUND_DISCOVERY -
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedAbstractDiscoveryService(int timeout) Creates a new instance of this class with the specified parameters and background discovery enabled and no input parameter supported to start the discovery.protectedAbstractDiscoveryService(@Nullable Set<ThingTypeUID> supportedThingTypes, int timeout) Creates a new instance of this class with the specified parameters and background discovery enabled and no input parameter supported to start the discovery.protectedAbstractDiscoveryService(@Nullable Set<ThingTypeUID> supportedThingTypes, int timeout, boolean backgroundDiscoveryEnabledByDefault) Creates a new instance of this class with the specified parameters and no input parameter supported to start the discovery.protectedAbstractDiscoveryService(@Nullable Set<ThingTypeUID> supportedThingTypes, int timeout, boolean backgroundDiscoveryEnabledByDefault, @Nullable String scanInputLabel, @Nullable String scanInputDescription) Creates a new instance of this class with the specified parameters.protectedAbstractDiscoveryService(ScheduledExecutorService scheduler, @Nullable Set<ThingTypeUID> supportedThingTypes, int timeout, boolean backgroundDiscoveryEnabledByDefault, @Nullable String scanInputLabel, @Nullable String scanInputDescription) Creates a new instance of this class with the specified parameters. -
Method Summary
Modifier and TypeMethodDescriptionvoidStops an active scan for devices.
This method must not block any calls such asDiscoveryService.startScan(org.openhab.core.config.discovery.ScanListener)and must return fast.protected voidCalled on component activation, if the implementation of this class is an OSGi declarative service and does not override the method.voidaddDiscoveryListener(@Nullable DiscoveryListener listener) Adds aDiscoveryListenerto the listeners' registry.protected voidCalled on component deactivation, if the implementation of this class is an OSGi declarative service and does not override the method.protected DiscoveryResultgetLocalizedDiscoveryResult(DiscoveryResult discoveryResult, @Nullable org.osgi.framework.Bundle bundle) @Nullable StringReturns the description of the supported input parameter to start the discovery.@Nullable StringReturns the label of the supported input parameter to start the discovery.intReturns the amount of time in seconds after which the discovery service automatically stops its forced discovery process.Returns the list ofThingtypes which are supported by theDiscoveryService.protected InstantGet the timestamp of the last call ofstartScan().booleanReturnstrueif the background discovery mode is enabled, otherwisefalse.booleanReturnstrueif the discovery supports an optional input parameter to run, otherwisefalse.protected voidCalled when the configuration for the discovery service is changed.voidremoveDiscoveryListener(@Nullable DiscoveryListener listener) Removes aDiscoveryListenerfrom the listeners' registry.protected voidremoveOlderResults(Instant timestamp) Call to remove all results of allsupportedThingTypesthat are older than the given timestamp.protected voidremoveOlderResults(Instant timestamp, @Nullable Collection<ThingTypeUID> thingTypeUIDs, @Nullable ThingUID bridgeUID) Call to remove all results of the given types that are older than the given timestamp.protected voidremoveOlderResults(Instant timestamp, @Nullable ThingUID bridgeUID) Call to remove all results of allsupportedThingTypesthat are older than the given timestamp.protected voidCan be overridden to start background discovery logic.protected abstract voidThis method is called by thestartScan(ScanListener)implementation of theAbstractDiscoveryService.protected voidvoidstartScan(String input, @Nullable ScanListener listener) Triggers this service to start an active scan for new devices using an input parameter for that.
This method must not block any calls such asDiscoveryService.abortScan()and must return fast.voidstartScan(@Nullable ScanListener listener) Triggers this service to start an active scan for new devices.
This method must not block any calls such asDiscoveryService.abortScan()and must return fast.protected voidCan be overridden to stop background discovery logic.protected voidstopScan()This method cleans up after a scan, i.e. it removes listeners and other required operations.protected voidthingDiscovered(DiscoveryResult discoveryResult) Notifies the registeredDiscoveryListeners about a discovered device.protected voidthingRemoved(ThingUID thingUID) Notifies the registeredDiscoveryListeners about a removed device.
-
Field Details
-
scheduler
-
scanListener
-
i18nProvider
-
localeProvider
-
-
Constructor Details
-
AbstractDiscoveryService
protected AbstractDiscoveryService(@Nullable Set<ThingTypeUID> supportedThingTypes, int timeout, boolean backgroundDiscoveryEnabledByDefault, @Nullable String scanInputLabel, @Nullable String scanInputDescription) throws IllegalArgumentException Creates a new instance of this class with the specified parameters.- Parameters:
supportedThingTypes- the list of Thing types which are supported (can be null)timeout- the discovery timeout in seconds after which the discovery service automatically stops its forced discovery process (>= 0).backgroundDiscoveryEnabledByDefault- defines, whether the default for this discovery service is to enable background discovery or not.scanInputLabel- the label of the optional input parameter to start the discovery or null if no input parameter supportedscanInputDescription- the description of the optional input parameter to start the discovery or null if no input parameter supported- Throws:
IllegalArgumentException- iftimeout < 0
-
AbstractDiscoveryService
protected AbstractDiscoveryService(ScheduledExecutorService scheduler, @Nullable Set<ThingTypeUID> supportedThingTypes, int timeout, boolean backgroundDiscoveryEnabledByDefault, @Nullable String scanInputLabel, @Nullable String scanInputDescription) throws IllegalArgumentException Creates a new instance of this class with the specified parameters.For use by tests only, allows setting a different
ScheduledExecutorServicelikeSameThreadExecutorServicefor synchronous behavior during testing.- Parameters:
scheduler- theScheduledExecutorServiceto use.supportedThingTypes- the list of Thing types which are supported (can be null)timeout- the discovery timeout in seconds after which the discovery service automatically stops its forced discovery process (>= 0).backgroundDiscoveryEnabledByDefault- defines, whether the default for this discovery service is to enable background discovery or not.scanInputLabel- the label of the optional input parameter to start the discovery or null if no input parameter supportedscanInputDescription- the description of the optional input parameter to start the discovery or null if no input parameter supported- Throws:
IllegalArgumentException- iftimeout < 0
-
AbstractDiscoveryService
protected AbstractDiscoveryService(@Nullable Set<ThingTypeUID> supportedThingTypes, int timeout, boolean backgroundDiscoveryEnabledByDefault) throws IllegalArgumentException Creates a new instance of this class with the specified parameters and no input parameter supported to start the discovery.- Parameters:
supportedThingTypes- the list of Thing types which are supported (can be null)timeout- the discovery timeout in seconds after which the discovery service automatically stops its forced discovery process (>= 0).backgroundDiscoveryEnabledByDefault- defines, whether the default for this discovery service is to enable background discovery or not.- Throws:
IllegalArgumentException- iftimeout < 0
-
AbstractDiscoveryService
protected AbstractDiscoveryService(@Nullable Set<ThingTypeUID> supportedThingTypes, int timeout) throws IllegalArgumentException Creates a new instance of this class with the specified parameters and background discovery enabled and no input parameter supported to start the discovery.- Parameters:
supportedThingTypes- the list of Thing types which are supported (can be null)timeout- the discovery timeout in seconds after which the discovery service automatically stops its forced discovery process (>= 0). If set to 0, disables the automatic stop.- Throws:
IllegalArgumentException- iftimeout < 0
-
AbstractDiscoveryService
Creates a new instance of this class with the specified parameters and background discovery enabled and no input parameter supported to start the discovery.- Parameters:
timeout- the discovery timeout in seconds after which the discovery service automatically stops its forced discovery process (>= 0). If set to 0, disables the automatic stop.- Throws:
IllegalArgumentException- iftimeout < 0
-
-
Method Details
-
getSupportedThingTypes
Returns the list ofThingtypes which are supported by theDiscoveryService.- Specified by:
getSupportedThingTypesin interfaceDiscoveryService- Returns:
- the list of Thing types which are supported by the discovery service (not null, could be empty)
-
isScanInputSupported
public boolean isScanInputSupported()Description copied from interface:DiscoveryServiceReturnstrueif the discovery supports an optional input parameter to run, otherwisefalse.- Specified by:
isScanInputSupportedin interfaceDiscoveryService- Returns:
- true if the discovery supports an optional input parameter to run, otherwise false
-
getScanInputLabel
Description copied from interface:DiscoveryServiceReturns the label of the supported input parameter to start the discovery.- Specified by:
getScanInputLabelin interfaceDiscoveryService- Returns:
- the label of the supported input parameter to start the discovery or null if input parameter not supported
-
getScanInputDescription
Description copied from interface:DiscoveryServiceReturns the description of the supported input parameter to start the discovery.- Specified by:
getScanInputDescriptionin interfaceDiscoveryService- Returns:
- the description of the supported input parameter to start the discovery or null if input parameter not supported
-
getScanTimeout
public int getScanTimeout()Returns the amount of time in seconds after which the discovery service automatically stops its forced discovery process.- Specified by:
getScanTimeoutin interfaceDiscoveryService- Returns:
- the discovery timeout in seconds (>= 0).
-
isBackgroundDiscoveryEnabled
public boolean isBackgroundDiscoveryEnabled()Description copied from interface:DiscoveryServiceReturnstrueif the background discovery mode is enabled, otherwisefalse.- Specified by:
isBackgroundDiscoveryEnabledin interfaceDiscoveryService- Returns:
- true if the background discovery mode is enabled, otherwise false
-
addDiscoveryListener
Description copied from interface:DiscoveryServiceAdds aDiscoveryListenerto the listeners' registry.Directly after registering the listener, it will receive
DiscoveryListener.thingDiscovered(DiscoveryService, DiscoveryResult)notifications about all devices that have been previously discovered by the service already (tracker behaviour). This is also done, if the listener has already been registered previously.When a
DiscoveryResultis created while the discovery process is active (e.g. by starting a scan or through the enabled background discovery mode), the specified listener is notified.This method returns silently if the specified listener is
null.- Specified by:
addDiscoveryListenerin interfaceDiscoveryService- Parameters:
listener- the listener to be added (could be null)
-
removeDiscoveryListener
Description copied from interface:DiscoveryServiceRemoves aDiscoveryListenerfrom the listeners' registry.When this method returns, the specified listener is no longer notified about a created
DiscoveryResultwhile the discovery process is active (e.g. by forcing the startup of the discovery process or while enabling the auto discovery mode)This method returns silently if the specified listener is
nullor has not been registered before.- Specified by:
removeDiscoveryListenerin interfaceDiscoveryService- Parameters:
listener- the listener to be removed (could be null)
-
startScan
Description copied from interface:DiscoveryServiceTriggers this service to start an active scan for new devices.
This method must not block any calls such asDiscoveryService.abortScan()and must return fast.If started, any registered
DiscoveryListenermust be notified aboutDiscoveryResults.If there is already a scan running, it is aborted and a new scan is triggered.
- Specified by:
startScanin interfaceDiscoveryService- Parameters:
listener- a listener that is notified about errors or termination of the scan
-
startScan
Description copied from interface:DiscoveryServiceTriggers this service to start an active scan for new devices using an input parameter for that.
This method must not block any calls such asDiscoveryService.abortScan()and must return fast.If started, any registered
DiscoveryListenermust be notified aboutDiscoveryResults.If there is already a scan running, it is aborted and a new scan is triggered.
- Specified by:
startScanin interfaceDiscoveryService- Parameters:
input- an input parameter to be used during discovery scanlistener- a listener that is notified about errors or termination of the scan
-
abortScan
public void abortScan()Description copied from interface:DiscoveryServiceStops an active scan for devices.
This method must not block any calls such asDiscoveryService.startScan(org.openhab.core.config.discovery.ScanListener)and must return fast.After this method returns, no further notifications about
DiscoveryResults are allowed to be sent to any registered listener, exceptional the background discovery mode is active.This method returns silently, if the scan has not been started before.
- Specified by:
abortScanin interfaceDiscoveryService
-
startScan
protected abstract void startScan()This method is called by thestartScan(ScanListener)implementation of theAbstractDiscoveryService. The abstract class schedules a call ofstopScan()aftergetScanTimeout()seconds. If this behavior is not appropriate, thestartScan(ScanListener)method should be overridden. -
startScan
-
stopScan
protected void stopScan()This method cleans up after a scan, i.e. it removes listeners and other required operations. -
thingDiscovered
Notifies the registeredDiscoveryListeners about a discovered device.- Parameters:
discoveryResult- Holds the information needed to identify the discovered device.
-
thingRemoved
Notifies the registeredDiscoveryListeners about a removed device.- Parameters:
thingUID- The UID of the removed thing.
-
removeOlderResults
Call to remove all results of allsupportedThingTypesthat are older than the given timestamp. To remove all left over results after a full scan, this method could be calledgetTimestampOfLastScan()as timestamp.- Parameters:
timestamp- timestamp, older results will be removed
-
removeOlderResults
Call to remove all results of allsupportedThingTypesthat are older than the given timestamp. To remove all left over results after a full scan, this method could be calledgetTimestampOfLastScan()as timestamp.- Parameters:
timestamp- timestamp, older results will be removedbridgeUID- if notnullonly results of that bridge are being removed
-
removeOlderResults
protected void removeOlderResults(Instant timestamp, @Nullable Collection<ThingTypeUID> thingTypeUIDs, @Nullable ThingUID bridgeUID) Call to remove all results of the given types that are older than the given timestamp. To remove all left over results after a full scan, this method could be calledgetTimestampOfLastScan()as timestamp.- Parameters:
timestamp- timestamp, older results will be removedthingTypeUIDs- collection ofThingTypes, only results of theseThingTypes will be removed; ifnullthenDiscoveryService.getSupportedThingTypes()will be used insteadbridgeUID- if notnullonly results of that bridge are being removed
-
activate
Called on component activation, if the implementation of this class is an OSGi declarative service and does not override the method. The method implementation callsstartBackgroundDiscovery()if background discovery is enabled by default and not overridden by the configuration.- Parameters:
configProperties- configuration properties
-
modified
Called when the configuration for the discovery service is changed. If background discovery should be enabled and is currently disabled, the methodstartBackgroundDiscovery()is called. If background discovery should be disabled and is currently enabled, the methodstopBackgroundDiscovery()is called. In all other cases, nothing happens.- Parameters:
configProperties- configuration properties
-
deactivate
protected void deactivate()Called on component deactivation, if the implementation of this class is an OSGi declarative service and does not override the method. The method implementation callsstopBackgroundDiscovery()if background discovery is enabled at the time of component deactivation. -
startBackgroundDiscovery
protected void startBackgroundDiscovery()Can be overridden to start background discovery logic. This method is called if background discovery is enabled when the component is being activated (seeactivate(java.util.Map<java.lang.String, java.lang.Object>). -
stopBackgroundDiscovery
protected void stopBackgroundDiscovery()Can be overridden to stop background discovery logic. This method is called if background discovery is enabled when the component is being deactivated (seedeactivate(). -
getTimestampOfLastScan
Get the timestamp of the last call ofstartScan().- Returns:
- timestamp as
Instant
-
getLocalizedDiscoveryResult
protected DiscoveryResult getLocalizedDiscoveryResult(DiscoveryResult discoveryResult, @Nullable org.osgi.framework.Bundle bundle)
-