Class BaseThingHandler
- All Implemented Interfaces:
ThingHandler
- Direct Known Subclasses:
BaseBridgeHandler
,ConfigStatusThingHandler
,MagicActionModuleThingHandler
,MagicBridgedThingHandler
,MagicButtonHandler
,MagicChattyThingHandler
,MagicColorLightHandler
,MagicConfigurableThingHandler
,MagicContactHandler
,MagicDelayedOnlineHandler
,MagicDimmableLightHandler
,MagicDynamicStateDescriptionThingHandler
,MagicExtensibleThingHandler
,MagicFirmwareUpdateThingHandler
,MagicImageHandler
,MagicLocationThingHandler
,MagicOnlineOfflineHandler
,MagicOnOffLightHandler
,MagicPlayerHandler
,MagicRollershutterHandler
,MagicThermostatThingHandler
,MagicTimeSeriesHandler
BaseThingHandler
provides a base implementation for the ThingHandler
interface.
The default behavior for Thing
updates is to dispose()
this handler first, exchange the
Thing
and ThingHandler.initialize()
it again. Override the method thingUpdated(Thing)
to change the
default behavior.
It is recommended to extend this abstract base class, because it covers a lot of common logic.
- Author:
- Dennis Nobel - Initial contribution, Michael Grammling - Added dynamic configuration update, Thomas Höfer - Added thing properties and config description validation, Stefan Bußweiler - Added new thing status handling, refactorings thing/bridge life cycle, Kai Kreuzer - Refactored isLinked method to not use deprecated functions anymore, Christoph Weitkamp - Moved OSGI ServiceTracker from BaseThingHandler to ThingHandlerCallback, Jan N. Klug - added time series support
-
Field Summary
-
Constructor Summary
ConstructorDescriptionBaseThingHandler
(Thing thing) Creates a new instance of this class for theThing
. -
Method Summary
Modifier and TypeMethodDescriptionvoid
bridgeStatusChanged
(ThingStatusInfo bridgeStatusInfo) Notifies the handler that the bridge's status has changed.protected void
changeThingType
(ThingTypeUID thingTypeUID, Configuration configuration) void
channelLinked
(ChannelUID channelUID) Notifies the handler that a channel was linked.void
channelUnlinked
(ChannelUID channelUID) Notifies the handler that a channel was unlinked.void
dispose()
Disposes the thing handler, e.g.protected Configuration
Returns a copy of the configuration, that can be modified.Returns a copy of the properties map, that can be modified.protected ThingBuilder
Creates a thing builder, which allows to modify the thing.protected @Nullable Bridge
Returns the bridge of the thing.protected @Nullable ThingHandlerCallback
Get theThingHandlerCallback
instance.protected Configuration
Returns the configuration of the thing.protected <T> T
getConfigAs
(Class<T> configurationClass) Returns the configuration of the thing and transforms it to the given class.protected @Nullable ConfigDescription
Get theConfigDescription
of the thingprotected @Nullable ConfigDescription
getConfigDescription
(ChannelTypeUID channelTypeUID) Get theConfigDescription
for aChannelTypeUID
getThing()
Returns theThing
, which belongs to the handler.void
handleConfigurationUpdate
(Map<String, Object> configurationParameters) Handles a configuration update.void
This method is called before a thing is removed.protected boolean
Returns whether the handler has already been initialized.protected boolean
Returns whether at least one item is linked for the given channel ID.protected boolean
isLinked
(ChannelUID channelUID) Returns whether at least one item is linked for the given UID of the channel.protected boolean
isModifyingCurrentConfig
(Map<String, Object> configurationParameters) Checks whether a given list of parameters would mean any change to the existing Thing configuration if applied to it.protected void
postCommand
(String channelID, Command command) Sends a command for a channel of the thing.protected void
postCommand
(ChannelUID channelUID, Command command) Sends a command for a channel of the thing.protected void
sendTimeSeries
(String channelID, TimeSeries timeSeries) Send a time series to the channel.protected void
sendTimeSeries
(ChannelUID channelUID, TimeSeries timeSeries) Send a time series to the channel.void
setCallback
(@Nullable ThingHandlerCallback thingHandlerCallback) Sets theThingHandlerCallback
of the handler, which must be used to inform the framework about changes.void
thingUpdated
(Thing thing) Notifies the handler about an updatedThing
.protected void
triggerChannel
(String channelUID) Emits an event for the given channel.protected void
triggerChannel
(String channelID, String event) Emits an event for the given channel.protected void
triggerChannel
(ChannelUID channelUID) Emits an event for the given channel.protected void
triggerChannel
(ChannelUID channelUID, String event) Emits an event for the given channel.protected void
updateConfiguration
(Configuration configuration) Updates the configuration of the thing and informs the framework about it.protected void
updateProperties
(@Nullable Map<String, String> properties) Informs the framework, that the given properties map of the thing was updated.protected void
updateProperty
(String name, @Nullable String value) Updates the given property value for the thing that is handled by this thing handler instance.protected void
updateState
(String channelID, State state) Updates the state of the thing.protected void
updateState
(ChannelUID channelUID, State state) Updates the state of the thing.protected void
updateStatus
(ThingStatus status) Updates the status of the thing.protected void
updateStatus
(ThingStatus status, ThingStatusDetail statusDetail) Updates the status of the thing.protected void
updateStatus
(ThingStatus status, ThingStatusDetail statusDetail, @Nullable String description) Updates the status of the thing.protected void
updateThing
(Thing thing) Informs the framework, that a thing was updated.protected void
validateConfigurationParameters
(Map<String, Object> configurationParameters) Validates the given configuration parameters against the configuration description.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface org.openhab.core.thing.binding.ThingHandler
getServices, handleCommand, initialize
-
Field Details
-
scheduler
-
thing
-
-
Constructor Details
-
BaseThingHandler
Creates a new instance of this class for theThing
.- Parameters:
thing
- the thing that should be handled, not null
-
-
Method Details
-
handleRemoval
public void handleRemoval()Description copied from interface:ThingHandler
This method is called before a thing is removed. An implementing class can handle the removal in order to trigger some tidying work for a thing.The framework expects this method to be non-blocking and return quickly. For longer running tasks, the implementation has to take care of scheduling a separate job.
The
Thing
is inThingStatus.REMOVING
when this method is called. Implementations of this method must signal to the framework that the handling has been completed by setting theThing
s state toThingStatus.REMOVED
. Only then it will be removed completely.- Specified by:
handleRemoval
in interfaceThingHandler
-
handleConfigurationUpdate
Description copied from interface:ThingHandler
Handles a configuration update.Note: An implementing class needs to persist the configuration changes if necessary.
- Specified by:
handleConfigurationUpdate
in interfaceThingHandler
- Parameters:
configurationParameters
- map of changed configuration parameters
-
isModifyingCurrentConfig
Checks whether a given list of parameters would mean any change to the existing Thing configuration if applied to it. Note that the passed parameters might be a subset of the existing configuration.- Parameters:
configurationParameters
- the parameters to check against the current configuration- Returns:
- true if the parameters would result in a modified configuration, false otherwise
-
dispose
public void dispose()Description copied from interface:ThingHandler
Disposes the thing handler, e.g. deallocate resources.The framework expects this method to be non-blocking and return quickly.
- Specified by:
dispose
in interfaceThingHandler
-
getThing
Description copied from interface:ThingHandler
Returns theThing
, which belongs to the handler.- Specified by:
getThing
in interfaceThingHandler
- Returns:
Thing
, which belongs to the handler
-
thingUpdated
Description copied from interface:ThingHandler
Notifies the handler about an updatedThing
.This method will only be called once the
ThingHandler.initialize()
method returned.- Specified by:
thingUpdated
in interfaceThingHandler
- Parameters:
thing
- theThing
, that has been updated
-
setCallback
Description copied from interface:ThingHandler
Sets theThingHandlerCallback
of the handler, which must be used to inform the framework about changes.The callback is added after the handler instance has been tracked by the framework and before
ThingHandler.initialize()
is called. The callback is removed (set to null) after the handler instance is no longer tracked and afterThingHandler.dispose()
is called.- Specified by:
setCallback
in interfaceThingHandler
- Parameters:
thingHandlerCallback
- the callback (can be null)
-
getCallback
Get theThingHandlerCallback
instance.- Returns:
- the
ThingHandlerCallback
instance. Only returnsnull
while the handler is not initialized.
-
channelLinked
Description copied from interface:ThingHandler
Notifies the handler that a channel was linked.This method is only called, if the thing has been initialized (status ONLINE/OFFLINE/UNKNOWN).
- Specified by:
channelLinked
in interfaceThingHandler
- Parameters:
channelUID
- UID of the linked channel
-
channelUnlinked
Description copied from interface:ThingHandler
Notifies the handler that a channel was unlinked.This method is only called, if the thing has been initialized (status ONLINE/OFFLINE/UNKNOWN).
- Specified by:
channelUnlinked
in interfaceThingHandler
- Parameters:
channelUID
- UID of the unlinked channel
-
validateConfigurationParameters
Validates the given configuration parameters against the configuration description.- Parameters:
configurationParameters
- the configuration parameters to be validated- Throws:
ConfigValidationException
- if one or more of the given configuration parameters do not match their declarations in the configuration description
-
getConfigDescription
Get theConfigDescription
of the thing- Returns:
- the config description (or null if not found or handler disposed)
-
getConfigDescription
Get theConfigDescription
for aChannelTypeUID
- Returns:
- the config description (or null if not found or handler disposed)
-
getConfig
Returns the configuration of the thing.- Returns:
- configuration of the thing
-
getConfigAs
Returns the configuration of the thing and transforms it to the given class.- Parameters:
configurationClass
- configuration class- Returns:
- configuration of thing in form of the given class
-
updateState
Updates the state of the thing.- Parameters:
channelUID
- unique id of the channel, which was updatedstate
- new state
-
updateState
Updates the state of the thing. Will use the thing UID to infer the unique channel UID from the given ID.- Parameters:
channelID
- id of the channel, which was updatedstate
- new state
-
sendTimeSeries
Send a time series to the channel. This can be used to transfer historic data or forecasts.- Parameters:
channelUID
- unique id of the channeltimeSeries
- theTimeSeries
that is sent
-
sendTimeSeries
Send a time series to the channel. This can be used to transfer historic data or forecasts.- Parameters:
channelID
- id of the channeltimeSeries
- theTimeSeries
that is sent
-
triggerChannel
Emits an event for the given channel.- Parameters:
channelUID
- UID of the channel over which the event will be emittedevent
- Event to emit
-
triggerChannel
Emits an event for the given channel. Will use the thing UID to infer the unique channel UID from the given ID.- Parameters:
channelID
- ID of the channel over which the event will be emittedevent
- Event to emit
-
triggerChannel
Emits an event for the given channel. Will use the thing UID to infer the unique channel UID.- Parameters:
channelUID
- UID of the channel over which the event will be emitted
-
triggerChannel
Emits an event for the given channel. Will use the thing UID to infer the unique channel UID.- Parameters:
channelUID
- UID of the channel over which the event will be emitted
-
postCommand
Sends a command for a channel of the thing.- Parameters:
channelID
- id of the channel, which sends the commandcommand
- command
-
postCommand
Sends a command for a channel of the thing.- Parameters:
channelUID
- unique id of the channel, which sends the commandcommand
- command
-
updateStatus
protected void updateStatus(ThingStatus status, ThingStatusDetail statusDetail, @Nullable String description) Updates the status of the thing.- Parameters:
status
- the statusstatusDetail
- the detail of the statusdescription
- the description of the status
-
updateStatus
Updates the status of the thing.- Parameters:
status
- the statusstatusDetail
- the detail of the status
-
updateStatus
Updates the status of the thing. The detail of the status will be 'NONE'.- Parameters:
status
- the status
-
editThing
Creates a thing builder, which allows to modify the thing. The methodupdateThing(Thing)
must be called to persist the changes.- Returns:
ThingBuilder
which builds an exact copy of the thing (not null)
-
updateThing
Informs the framework, that a thing was updated. This method must be called after the configuration or channels was changed. Any method overriding this method has to make sure that only things with valid configurations are passed to the callback. This can be achieved by callingThingHandlerCallback.validateConfigurationParameters(Thing, Map)
. It is also necessary to ensure that all channel configurations are valid by callingThingHandlerCallback.validateConfigurationParameters(Channel, Map)
.- Parameters:
thing
- thing, that was updated and should be persisted
-
editConfiguration
Returns a copy of the configuration, that can be modified. The methodupdateConfiguration(Configuration)
must be called to persist the configuration.- Returns:
- copy of the thing configuration (not null)
-
updateConfiguration
Updates the configuration of the thing and informs the framework about it. Any method overriding this method has to make sure that only valid configurations are passed to the callback. This can be achieved by callingThingHandlerCallback.validateConfigurationParameters(Thing, Map)
.- Parameters:
configuration
- configuration, that was updated and should be persisted
-
editProperties
Returns a copy of the properties map, that can be modified. The methodupdateProperties(Map)
must be called to persist the properties.- Returns:
- copy of the thing properties (not null)
-
updateProperties
Informs the framework, that the given properties map of the thing was updated. This method performs a check, if the properties were updated. If the properties did not change, the framework is not informed about changes.- Parameters:
properties
- properties map, that was updated and should be persisted (all properties cleared if null)
-
updateProperty
Updates the given property value for the thing that is handled by this thing handler instance. The value is only set for the given property name if there has not been set any value yet or if the value has been changed. If the value of the property to be set is null then the property is removed. This method also informs the framework about the updated thing, which in fact will persists the changes. So, if multiple properties should be changed at the same time, the
editProperties()
method should be used.- Parameters:
name
- the name of the property to be setvalue
- the value of the property
-
getBridge
Returns the bridge of the thing.- Returns:
- returns the bridge of the thing or null if the thing has no bridge
-
isLinked
Returns whether at least one item is linked for the given channel ID.- Parameters:
channelId
- channel ID (must not be null)- Returns:
- true if at least one item is linked, false otherwise
-
isLinked
Returns whether at least one item is linked for the given UID of the channel.- Parameters:
channelUID
- UID of the channel (must not be null)- Returns:
- true if at least one item is linked, false otherwise
-
isInitialized
protected boolean isInitialized()Returns whether the handler has already been initialized.- Returns:
- true if handler is initialized, false otherwise
-
bridgeStatusChanged
Description copied from interface:ThingHandler
Notifies the handler that the bridge's status has changed.This method is called, when the status of the bridge has been changed to
ThingStatus.ONLINE
,ThingStatus.OFFLINE
orThingStatus.UNKNOWN
, i.e. after a bridge has been initialized. If the thing of this handler does not have a bridge, this method is never called.If the bridge's status has changed to
ThingStatus.OFFLINE
, the status of the handled thing must be updated toThingStatus.OFFLINE
with detailThingStatusDetail.BRIDGE_OFFLINE
. If the bridge returns toThingStatus.ONLINE
, the thing status must be changed at least toThingStatus.OFFLINE
with detailThingStatusDetail.NONE
.- Specified by:
bridgeStatusChanged
in interfaceThingHandler
- Parameters:
bridgeStatusInfo
- the status info of the bridge
-
changeThingType
-