Class AbstractDiscoveryService
- All Implemented Interfaces:
DiscoveryService
- Direct Known Subclasses:
AbstractThingHandlerDiscoveryService
,SddpDiscoveryService
AbstractDiscoveryService
provides methods which handle the DiscoveryListener
s.
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 DiscoveryListener
s.- 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
Modifier and TypeFieldDescriptionprotected TranslationProvider
protected LocaleProvider
protected @Nullable ScanListener
protected final ScheduledExecutorService
Fields inherited from interface org.openhab.core.config.discovery.DiscoveryService
CONFIG_PROPERTY_BACKGROUND_DISCOVERY
-
Constructor Summary
ModifierConstructorDescriptionprotected
AbstractDiscoveryService
(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.protected
AbstractDiscoveryService
(@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.protected
AbstractDiscoveryService
(@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.protected
AbstractDiscoveryService
(@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 TypeMethodDescriptionvoid
Stops 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 void
Called on component activation, if the implementation of this class is an OSGi declarative service and does not override the method.void
addDiscoveryListener
(@Nullable DiscoveryListener listener) Adds aDiscoveryListener
to the listeners' registry.protected void
Called on component deactivation, if the implementation of this class is an OSGi declarative service and does not override the method.protected DiscoveryResult
getLocalizedDiscoveryResult
(DiscoveryResult discoveryResult, @Nullable org.osgi.framework.Bundle bundle) @Nullable String
Returns the description of the supported input parameter to start the discovery.@Nullable String
Returns the label of the supported input parameter to start the discovery.int
Returns the amount of time in seconds after which the discovery service automatically stops its forced discovery process.Returns the list ofThing
types which are supported by theDiscoveryService
.protected long
Get the timestamp of the last call ofstartScan()
.boolean
Returnstrue
if the background discovery mode is enabled, otherwisefalse
.boolean
Returnstrue
if the discovery supports an optional input parameter to run, otherwisefalse
.protected void
Called when the configuration for the discovery service is changed.void
removeDiscoveryListener
(@Nullable DiscoveryListener listener) Removes aDiscoveryListener
from the listeners' registry.protected void
removeOlderResults
(long timestamp) Call to remove all results of allsupportedThingTypes
that are older than the given timestamp.protected void
removeOlderResults
(long 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 void
removeOlderResults
(long timestamp, @Nullable ThingUID bridgeUID) Call to remove all results of allsupportedThingTypes
that are older than the given timestamp.protected void
Can be overridden to start background discovery logic.protected abstract void
This method is called by thestartScan(ScanListener)
implementation of theAbstractDiscoveryService
.protected void
void
startScan
(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.void
startScan
(@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 void
Can be overridden to stop background discovery logic.protected void
stopScan()
This method cleans up after a scan, i.e.protected void
thingDiscovered
(DiscoveryResult discoveryResult) Notifies the registeredDiscoveryListener
s about a discovered device.protected void
thingRemoved
(ThingUID thingUID) Notifies the registeredDiscoveryListener
s 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(@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 ofThing
types which are supported by theDiscoveryService
.- Specified by:
getSupportedThingTypes
in 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:DiscoveryService
Returnstrue
if the discovery supports an optional input parameter to run, otherwisefalse
.- Specified by:
isScanInputSupported
in interfaceDiscoveryService
- Returns:
- true if the discovery supports an optional input parameter to run, otherwise false
-
getScanInputLabel
Description copied from interface:DiscoveryService
Returns the label of the supported input parameter to start the discovery.- Specified by:
getScanInputLabel
in 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:DiscoveryService
Returns the description of the supported input parameter to start the discovery.- Specified by:
getScanInputDescription
in 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:
getScanTimeout
in interfaceDiscoveryService
- Returns:
- the discovery timeout in seconds (>= 0).
-
isBackgroundDiscoveryEnabled
public boolean isBackgroundDiscoveryEnabled()Description copied from interface:DiscoveryService
Returnstrue
if the background discovery mode is enabled, otherwisefalse
.- Specified by:
isBackgroundDiscoveryEnabled
in interfaceDiscoveryService
- Returns:
- true if the background discovery mode is enabled, otherwise false
-
addDiscoveryListener
Description copied from interface:DiscoveryService
Adds aDiscoveryListener
to 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
DiscoveryResult
is 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:
addDiscoveryListener
in interfaceDiscoveryService
- Parameters:
listener
- the listener to be added (could be null)
-
removeDiscoveryListener
Description copied from interface:DiscoveryService
Removes aDiscoveryListener
from the listeners' registry.When this method returns, the specified listener is no longer notified about a created
DiscoveryResult
while 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
null
or has not been registered before.- Specified by:
removeDiscoveryListener
in interfaceDiscoveryService
- Parameters:
listener
- the listener to be removed (could be null)
-
startScan
Description copied from interface:DiscoveryService
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.If started, any registered
DiscoveryListener
must be notified aboutDiscoveryResult
s.If there is already a scan running, it is aborted and a new scan is triggered.
- Specified by:
startScan
in interfaceDiscoveryService
- Parameters:
listener
- a listener that is notified about errors or termination of the scan
-
startScan
Description copied from interface:DiscoveryService
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.If started, any registered
DiscoveryListener
must be notified aboutDiscoveryResult
s.If there is already a scan running, it is aborted and a new scan is triggered.
- Specified by:
startScan
in 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:DiscoveryService
Stops 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
DiscoveryResult
s 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:
abortScan
in 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 registeredDiscoveryListener
s about a discovered device.- Parameters:
discoveryResult
- Holds the information needed to identify the discovered device.
-
thingRemoved
Notifies the registeredDiscoveryListener
s about a removed device.- Parameters:
thingUID
- The UID of the removed thing.
-
removeOlderResults
protected void removeOlderResults(long timestamp) Call to remove all results of allsupportedThingTypes
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 removed
-
removeOlderResults
Call to remove all results of allsupportedThingTypes
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 removedbridgeUID
- if notnull
only results of that bridge are being removed
-
removeOlderResults
protected void removeOlderResults(long 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 ofThingType
s, only results of theseThingType
s will be removed; ifnull
thenDiscoveryService.getSupportedThingTypes()
will be used insteadbridgeUID
- if notnull
only 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
protected long getTimestampOfLastScan()Get the timestamp of the last call ofstartScan()
.- Returns:
- timestamp as long
-
getLocalizedDiscoveryResult
protected DiscoveryResult getLocalizedDiscoveryResult(DiscoveryResult discoveryResult, @Nullable org.osgi.framework.Bundle bundle)
-