BeeeOn Gateway  v2020.3.1-2-g6f737dc
Platform to interconnect the IoT world
Public Types | Public Member Functions | Protected Member Functions | Protected Attributes
BeeeOn::DeviceManager Class Reference

#include <DeviceManager.h>

Inheritance diagram for BeeeOn::DeviceManager:
BeeeOn::CommandHandler BeeeOn::CommandSender BeeeOn::DeviceStatusHandler BeeeOn::Loggable BeeeOn::StoppableRunnable BeeeOn::BelkinWemoDeviceManager BeeeOn::ConradDeviceManager BeeeOn::DongleDeviceManager BeeeOn::FitpDeviceManager BeeeOn::JablotronDeviceManager BeeeOn::PhilipsHueDeviceManager BeeeOn::PressureSensorManager BeeeOn::SonoffDeviceManager BeeeOn::VektivaDeviceManager BeeeOn::VirtualDeviceManager BeeeOn::VPTDeviceManager BeeeOn::ZWaveDeviceManager

Public Types

typedef Poco::SharedPtr
< DeviceManager
Ptr
 
- Public Types inherited from BeeeOn::DeviceStatusHandler
typedef Poco::SharedPtr
< DeviceStatusHandler
Ptr
 
typedef std::map< DeviceID,
std::map< ModuleID, double > > 
DeviceValues
 
- Public Types inherited from BeeeOn::StoppableRunnable
typedef Poco::SharedPtr
< StoppableRunnable
Ptr
 

Public Member Functions

 DeviceManager (const DevicePrefix &prefix, const std::initializer_list< std::type_index > &acceptable={})
 
DevicePrefix prefix () const override
 
void stop () override
 
void setDeviceCache (DeviceCache::Ptr cache)
 
void setDistributor (Poco::SharedPtr< Distributor > distributor)
 
bool accept (const Command::Ptr cmd) override
 
void handle (Command::Ptr cmd, Answer::Ptr answer) override
 
void handleRemoteStatus (const DevicePrefix &prefix, const std::set< DeviceID > &devices, const DeviceStatusHandler::DeviceValues &values) override
 Called when devices from a remote server are fetched and so the pairing status of them can be reconsidered. The default implementation just updates the device cache appropriately.
 
- Public Member Functions inherited from BeeeOn::CommandSender
void setCommandDispatcher (CommandDispatcher *dispatcher)
 
void dispatch (Poco::AutoPtr< Command > cmd, Poco::AutoPtr< Answer > answer)
 
void dispatch (Poco::AutoPtr< Command > cmd)
 
AnswerQueueanswerQueue ()
 

Protected Member Functions

std::set< DeviceIDwaitRemoteStatus (const Poco::Timespan &timeout)
 Wait until the remote status is delivered or timeout exceeds. DO NOT USE this method, it is intended as a transition mechanism from calling of the deprecated method deviceList(). More...
 
virtual void handleGeneric (const Command::Ptr cmd, Result::Ptr result)
 
virtual void handleAccept (const DeviceAcceptCommand::Ptr)
 Generic handler of the DeviceAcceptCommand. It might be helpful to override this method in case we need to make some technology-specific check of the device to be accepted. The default implementation simply marks the given device as paired.
 
virtual AsyncWork::Ptr startDiscovery (const Poco::Timespan &timeout)
 Starts device discovery process in a technology-specific way. This method is always called inside a critical section and so its implementation does not have to be thread-safe nor reentrant (unless it cooperates with other threads itself). More...
 
void handleListen (const GatewayListenCommand::Ptr cmd)
 Implements handling of the listen command in a generic way. The method ensures that only 1 thread can execute the discovery process at a time. More...
 
virtual AsyncWork::Ptr startSearch (const Poco::Timespan &timeout, const Poco::Net::IPAddress &address)
 Start searching a device by IP address in a technology-specific way. This method is always called inside a critical section and so its implementation does not have to be thread-safe nor reentrant (unless it cooperates with other threads itself). More...
 
virtual AsyncWork::Ptr startSearch (const Poco::Timespan &timeout, const MACAddress &address)
 Start searching a device by MAC address in a technology-specific way. This method is always called inside a critical section and so its implementation does not have to be thread-safe nor reentrant (unless it cooperates with other threads itself). More...
 
virtual AsyncWork::Ptr startSearch (const Poco::Timespan &timeout, const uint64_t serialNumber)
 Start searching a device by serial number in a technology-specific way. This method is always called inside a critical section and so its implementation does not have to be thread-safe nor reentrant (unless it cooperates with other threads itself). More...
 
void handleSearch (const DeviceSearchCommand::Ptr cmd)
 Implements handling of the search command in a generic way. The method ensures that only 1 thread can exactly the search process at a time. It is also mutual exclusive to the discovery process.
 
virtual AsyncWork< std::set
< DeviceID > >::Ptr 
startUnpair (const DeviceID &id, const Poco::Timespan &timeout)
 Starts device unpair process in a technology-specific way. This method is always called inside a critical section and so its implementation does not have to be thread-safe nor reentrant (unless it cooperates with other threads itself). More...
 
std::set< DeviceIDhandleUnpair (const DeviceUnpairCommand::Ptr cmd)
 Implements handling of the unpair command in a generic way. The method ensures that only 1 thread can execute the unpair process at a time. More...
 
virtual AsyncWork< double >::Ptr startSetValue (const DeviceID &id, const ModuleID &module, const double value, const Poco::Timespan &timeout)
 Starts set-value operation in a technology specific way. The method is always called inside a critical section and so its implementation does not have to be thread-save nor reentrant (unless it cooperates with other threads itself). More...
 
AsyncWork< double >::Ptr startSetValueByMode (const DeviceID &id, const ModuleID &module, const double value, const OpMode &mode, const Poco::Timespan &timeout)
 Call an implementation of startSetValue() based on the given operation mode.
 
virtual AsyncWork< double >::Ptr startSetValueTryHarder (const DeviceID &id, const ModuleID &module, const double value, const Poco::Timespan &timeout)
 Default implementation just calls startSetValue().
 
virtual AsyncWork< double >::Ptr startSetValueRepeatOnFail (const DeviceID &id, const ModuleID &module, const double value, const Poco::Timespan &timeout)
 Default implementation calls startSetValue() again if a Poco::IOException is thrown until timeout exceeds. However, due to asynchronous behaviour, this way of repeating on fail might be inappropriate.
 
void handleSetValue (const DeviceSetValueCommand::Ptr cmd)
 Implements handling of the set-value command in a generic way. The method ensures that only 1 thread can execute set-value process at a time. If the set-value operation succeeds, it ships the set value. More...
 
void ship (const SensorData &sensorData)
 
DeviceCache::Ptr deviceCache () const
 
CancellableSetcancellable ()
 
Poco::Timespan checkDelayedOperation (const std::string &opname, const Poco::Clock &started, const Poco::Timespan &duration) const
 When starting an asynchronous operation, it might happen we sleep too long on a lock because the previous operation did not finished yet. This method performs such checks and also tests for global stop request. If everything is in order it just fixes the duration by the time elapsed by waiting. Otherwise, it throws an exception. More...
 
bool manageUntilFinished (const std::string &opname, AnyAsyncWork::Ptr work, const Poco::Timespan &timeout)
 Manage an AsyncWork after it is started. If it does not finish in the given timeout, it is cancelled explicitly. More...
 
- Protected Member Functions inherited from BeeeOn::Loggable
void setupLogger (Poco::Logger *logger=0) const
 
Poco::Logger & logger () const
 
 Loggable (const ClassInfo &info)
 
 Loggable (const std::type_info &info)
 

Protected Attributes

StopControl m_stopControl
 

Additional Inherited Members

- Protected Types inherited from BeeeOn::StoppableRunnable
typedef Poco::SharedPtr
< StoppableRunnable
Ptr
 
- Static Protected Member Functions inherited from BeeeOn::Loggable
static Poco::Logger & forMethod (const char *name)
 
static Poco::Logger & forClass (const ClassInfo &info)
 
static Poco::Logger & forClass (const std::type_info &info)
 
template<typename T >
static Poco::Logger & forInstance (const T *i)
 
static void configureSimple (Poco::Logger &logger, const std::string &level)
 
static void logException (Poco::Logger &logger, const Poco::Message::Priority priority, const Poco::Exception &e, const char *file, size_t line)
 

Detailed Description

All classes that manage devices should inherit from this abstract class. It provides a common functionality for this purpose.

There is usually a main thread that performs communication with the physical devices and translates the specific device communication protocol into the Command & Answer interface or into SensorData interface.

Communication in the direction to physical devices is served via the CommandHandler interface. By accepting Commands asking for specific tasks, the physical devices can be queried as expected by the server.

Member Function Documentation

bool DeviceManager::accept ( const Command::Ptr  cmd)
overridevirtual

Generic implementation of the CommandHandler::accept() method. If the m_acceptable set is initialized appropriately, this generic implementation can be used directly.

Implements BeeeOn::CommandHandler.

CancellableSet & DeviceManager::cancellable ( )
protected
Returns
reference to the CancellableSet instance that is cancelled on stop(). This allows to manage asynchronous jobs and cancel them when required.
Timespan DeviceManager::checkDelayedOperation ( const std::string &  opname,
const Poco::Clock &  started,
const Poco::Timespan &  duration 
) const
protected

When starting an asynchronous operation, it might happen we sleep too long on a lock because the previous operation did not finished yet. This method performs such checks and also tests for global stop request. If everything is in order it just fixes the duration by the time elapsed by waiting. Otherwise, it throws an exception.

Exceptions
IllegalStateExceptionwhen stop has been requested
DeviceCache::Ptr DeviceManager::deviceCache ( ) const
protected
Returns
the underlying DeviceCache instance
void DeviceManager::handle ( Command::Ptr  cmd,
Answer::Ptr  answer 
)
overridevirtual

Generic implementation of the CommandHandler::handle() method. It works with respect to the accept() method and handles commands generically if possible. It also catches all exceptions and fails such command executions properly.

To override handle, please use handleGeneric() instead.

Implements BeeeOn::CommandHandler.

void DeviceManager::handleGeneric ( const Command::Ptr  cmd,
Result::Ptr  result 
)
protectedvirtual

Generic implementation of the handle(). If any device manager needs to override the handle() method, it is more desirable to override handleGeneric().

Reimplemented in BeeeOn::FitpDeviceManager, BeeeOn::VirtualDeviceManager, and BeeeOn::VPTDeviceManager.

void DeviceManager::handleListen ( const GatewayListenCommand::Ptr  cmd)
protected

Implements handling of the listen command in a generic way. The method ensures that only 1 thread can execute the discovery process at a time.

It uses the method startDiscovery() to initialize and start a new discovery process. It is guaranteed that the startDiscovery() method is always called exactly once at a time and until it finishes, no other discovery is started. The minimal timeout for the discovery is at least 1 second which is enforced by the implementation.

void DeviceManager::handleSetValue ( const DeviceSetValueCommand::Ptr  cmd)
protected

Implements handling of the set-value command in a generic way. The method ensures that only 1 thread can execute set-value process at a time. If the set-value operation succeeds, it ships the set value.

It uses the method startSetValue() to initialize and start the set-value process. The method startSetValue() is always called exactly once at a time and until it finishes, no the set-value is started.

set< DeviceID > DeviceManager::handleUnpair ( const DeviceUnpairCommand::Ptr  cmd)
protected

Implements handling of the unpair command in a generic way. The method ensures that only 1 thread can execute the unpair process at a time.

It uses the method startUnpair() to initialize and start the unpairing process. The method startUnpair() is always called exactly once at a time and until it finishes, no other unpair is started.

bool DeviceManager::manageUntilFinished ( const std::string &  opname,
AnyAsyncWork::Ptr  work,
const Poco::Timespan &  timeout 
)
protected

Manage an AsyncWork after it is started. If it does not finish in the given timeout, it is cancelled explicitly.

Returns
false when cancellled (timeout)
DevicePrefix DeviceManager::prefix ( ) const
overridevirtual
Returns
prefix managed by this device manager

Implements BeeeOn::DeviceStatusHandler.

void DeviceManager::ship ( const SensorData sensorData)
protected

Ship data received from a physical device into a collection point.

AsyncWork::Ptr DeviceManager::startDiscovery ( const Poco::Timespan &  timeout)
protectedvirtual

Starts device discovery process in a technology-specific way. This method is always called inside a critical section and so its implementation does not have to be thread-safe nor reentrant (unless it cooperates with other threads itself).

The purpose of this call is to initialize and start the discovery process which might be a non-blocking operation. The caller uses the provided AsyncWork<> instance to wait until it finishes or to cancel it earlier if needed.

Reimplemented in BeeeOn::ZWaveDeviceManager, BeeeOn::JablotronDeviceManager, BeeeOn::BLESmartDeviceManager, BeeeOn::IQRFDeviceManager, BeeeOn::VPTDeviceManager, BeeeOn::BluetoothAvailabilityManager, BeeeOn::PhilipsHueDeviceManager, BeeeOn::PressureSensorManager, BeeeOn::BelkinWemoDeviceManager, BeeeOn::VektivaDeviceManager, BeeeOn::ConradDeviceManager, and BeeeOn::SonoffDeviceManager.

virtual AsyncWork ::Ptr BeeeOn::DeviceManager::startSearch ( const Poco::Timespan &  timeout,
const Poco::Net::IPAddress &  address 
)
protectedvirtual

Start searching a device by IP address in a technology-specific way. This method is always called inside a critical section and so its implementation does not have to be thread-safe nor reentrant (unless it cooperates with other threads itself).

The purpose of this call is to initialize and start the searching process which might be a non-blocking operation. The caller uses the provided AsyncWork<> instance to wait until it finishes or to cancel it earlier if needed.

virtual AsyncWork ::Ptr BeeeOn::DeviceManager::startSearch ( const Poco::Timespan &  timeout,
const MACAddress address 
)
protectedvirtual

Start searching a device by MAC address in a technology-specific way. This method is always called inside a critical section and so its implementation does not have to be thread-safe nor reentrant (unless it cooperates with other threads itself).

The purpose of this call is to initialize and start the searching process which might be a non-blocking operation. The caller uses the provided AsyncWork<> instance to wait until it finishes or to cancel it earlier if needed.

virtual AsyncWork ::Ptr BeeeOn::DeviceManager::startSearch ( const Poco::Timespan &  timeout,
const uint64_t  serialNumber 
)
protectedvirtual

Start searching a device by serial number in a technology-specific way. This method is always called inside a critical section and so its implementation does not have to be thread-safe nor reentrant (unless it cooperates with other threads itself).

The purpose of this call is to initialize and start the searching process which might be a non-blocking operation. The caller uses the provided AsyncWork<> instance to wait until it finishes or to cancel it earlier if needed.

Reimplemented in BeeeOn::JablotronDeviceManager.

AsyncWork< double >::Ptr DeviceManager::startSetValue ( const DeviceID id,
const ModuleID module,
const double  value,
const Poco::Timespan &  timeout 
)
protectedvirtual

Starts set-value operation in a technology specific way. The method is always called inside a critical section and so its implementation does not have to be thread-save nor reentrant (unless it cooperates with other threads itself).

The set-value process might a be a non-blocking operation. The value set by the set-value is expected as a result of the returned AsyncWork instance.

Reimplemented in BeeeOn::JablotronDeviceManager, BeeeOn::ZWaveDeviceManager, BeeeOn::BLESmartDeviceManager, BeeeOn::PhilipsHueDeviceManager, BeeeOn::BelkinWemoDeviceManager, BeeeOn::VektivaDeviceManager, and BeeeOn::ConradDeviceManager.

AsyncWork< set< DeviceID > >::Ptr DeviceManager::startUnpair ( const DeviceID id,
const Poco::Timespan &  timeout 
)
protectedvirtual

Starts device unpair process in a technology-specific way. This method is always called inside a critical section and so its implementation does not have to be thread-safe nor reentrant (unless it cooperates with other threads itself).

The unpairing process might be a non-blocking operation. The unpaired devices are provided via the result of the returned AsyncWork instance.

Reimplemented in BeeeOn::JablotronDeviceManager, BeeeOn::ZWaveDeviceManager, BeeeOn::BLESmartDeviceManager, BeeeOn::IQRFDeviceManager, BeeeOn::VPTDeviceManager, BeeeOn::BluetoothAvailabilityManager, BeeeOn::PhilipsHueDeviceManager, BeeeOn::PressureSensorManager, BeeeOn::BelkinWemoDeviceManager, BeeeOn::VektivaDeviceManager, BeeeOn::ConradDeviceManager, and BeeeOn::SonoffDeviceManager.

void DeviceManager::stop ( )
overridevirtual
set< DeviceID > DeviceManager::waitRemoteStatus ( const Poco::Timespan &  timeout)
protected

Wait until the remote status is delivered or timeout exceeds. DO NOT USE this method, it is intended as a transition mechanism from calling of the deprecated method deviceList().

Returns
list of paired devices as received from the remote status

The documentation for this class was generated from the following files: