BeeeOn Gateway  v2020.3.1-2-g6f737dc
Platform to interconnect the IoT world
Data Structures | Public Member Functions | Protected Member Functions
BeeeOn::VektivaDeviceManager Class Reference

Vektiva device manager provides an easy way to manage devices that are compatible with implemented interface. In the current state it's Smarwi windows maintainer. More...

#include <VektivaDeviceManager.h>

Inheritance diagram for BeeeOn::VektivaDeviceManager:
BeeeOn::DeviceManager BeeeOn::CommandHandler BeeeOn::CommandSender BeeeOn::DeviceStatusHandler BeeeOn::Loggable BeeeOn::StoppableRunnable

Data Structures

class  VektivaSeeker
 Provides searching of Vektiva devices on network via MQTT messages in an own thread. More...
 

Public Member Functions

void run () override
 
void stop () override
 
void setMqttClient (MqttClient::Ptr mqttClient)
 
void setStatusMqttClient (MqttClient::Ptr mqttClient)
 
void setReceiveTimeout (const Poco::Timespan &timeout)
 
- Public Member Functions inherited from BeeeOn::DeviceManager
 DeviceManager (const DevicePrefix &prefix, const std::initializer_list< std::type_index > &acceptable={})
 
DevicePrefix prefix () const 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

AsyncWork::Ptr startDiscovery (const Poco::Timespan &timeout) override
 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...
 
AsyncWork< std::set< DeviceID >
>::Ptr 
startUnpair (const DeviceID &id, const Poco::Timespan &) override
 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...
 
AsyncWork< double >::Ptr startSetValue (const DeviceID &id, const ModuleID &module, const double value, const Poco::Timespan &) override
 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...
 
void handleAccept (const DeviceAcceptCommand::Ptr cmd) override
 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.
 
bool updateDevice (VektivaSmarwi::Ptr newDevice)
 New device is being processed. If true is returned, NewDeviceCommand can be dispatched. More...
 
void modifyValue (const DeviceID &deviceID, const ModuleID &moduleID, const double value)
 Finds a device with the corresponding device ID and attempts to change the state of the selected module ID. More...
 
void statusMessageAction (MqttMessage &mqttMessage)
 Groups all actions that are done when "status" message is received. More...
 
void onlineMessageAction (MqttMessage &mqttMessage)
 Groups all actions that are done when "online" message is received. More...
 
void clearMqttMessageBuffer ()
 Clears all messages buffered in the MQTT client to assure there are no previous messages when attempting to contact a device.
 
void analyzeMessage (MqttMessage &mqttMessage)
 Parses the received message and according to its' content, correct actions are performed. More...
 
MqttMessage messageReceivedInTime (const std::string &lastSegment, VektivaSmarwi::Ptr device)
 Waits for a specified amount of time for a message with the last segment of the topic equal to lastSegment argument and device properties. More...
 
void shipSmarwiStatus (const DeviceID &deviceId, const VektivaSmarwiStatus &smarwiStatus)
 Sends Smarwi status to the exporters. More...
 
void dispatchNewDevice (VektivaSmarwi::Ptr device)
 Dispatches NewDeviceCommand. More...
 
bool receiveStatusMessageAndUpdate (VektivaSmarwi::Ptr device)
 When status message is updated, info is parsed and device pointer passed in argument is updated with info from the message. More...
 
void updateDeviceInfo (VektivaSmarwi::Ptr device, const VektivaSmarwiStatus &smarwiStatus)
 Function to update any relevant info that can be updated (e.g. IP address) More...
 
bool isTopicValid (const std::string &topic, const std::string &lastSegment, const std::string &remoteId="", const std::string &macAddr="")
 Function to validate topic in received message Last two parameters are optional and can either specify the exact Remote ID and MAC address or if left blank, they'll check for rules that topic have to have. More...
 
std::pair< std::string,
std::string > 
retrieveDeviceInfoFromTopic (const std::string &topic)
 Exctracts Remote ID and MAC address from MQTT message' topic. More...
 
void escapeRegexString (std::string &regexString)
 Escapes inputted string from regex control characters. More...
 
- Protected Member Functions inherited from BeeeOn::DeviceManager
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)
 
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.
 
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...
 
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)
 

Additional Inherited Members

- Public Types inherited from BeeeOn::DeviceManager
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
 
- 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)
 
- Protected Attributes inherited from BeeeOn::DeviceManager
StopControl m_stopControl
 

Detailed Description

Vektiva device manager provides an easy way to manage devices that are compatible with implemented interface. In the current state it's Smarwi windows maintainer.

Member Function Documentation

void VektivaDeviceManager::analyzeMessage ( MqttMessage mqttMessage)
protected

Parses the received message and according to its' content, correct actions are performed.

Parameters
messagea message received by the MQTT client
void VektivaDeviceManager::dispatchNewDevice ( VektivaSmarwi::Ptr  device)
protected

Dispatches NewDeviceCommand.

Parameters
devicedevice to dispatch
void VektivaDeviceManager::escapeRegexString ( std::string &  regexString)
protected

Escapes inputted string from regex control characters.

Parameters
regexStringstring to be escaped
bool VektivaDeviceManager::isTopicValid ( const std::string &  topic,
const std::string &  lastSegment,
const std::string &  remoteId = "",
const std::string &  macAddr = "" 
)
protected

Function to validate topic in received message Last two parameters are optional and can either specify the exact Remote ID and MAC address or if left blank, they'll check for rules that topic have to have.

Regex explained ^ - regex from the beginning ion/ - basic prefix for Smarwi [^#\/]+ OR <REMOTEID - can input anything as RemoteID except # and / OR topic has to be equal RemoteId specified as the parameter /% - delimiter [a-fA-F0-9]{12} OR <MACADDR> - any MAC address OR exact MAC address specified in the parameter /<LASTSEGMENT> - type of the message e.g. status / online / cmd $ - until the end

Examples: isTopicValid( "ion/dowarogxby/%aabbccaabbcc/online", "online", "dowarogxby", "aabbccaabbcc") returns true because every segment of the topic is equal to the corresponding segments.

isTopicValid( "ion/dowarogxby/%aabbccaabbcc/status", "online") returns false because the last segment doesn't match

Parameters
topictopic of received message
lastSegmentis what should topic contain as the last word
remoteIdremoteId that should topic be equal to
macAddrMAC address that should topic be equal to
Returns
true if valid, false otherwise
MqttMessage VektivaDeviceManager::messageReceivedInTime ( const std::string &  lastSegment,
VektivaSmarwi::Ptr  device 
)
protected

Waits for a specified amount of time for a message with the last segment of the topic equal to lastSegment argument and device properties.

Parameters
timeouthow long to wait for the status message
lastSegmentin the topic
deviceVektiva device
Exceptions
TimeoutExceptionif timeout occured
Returns
MqttMessage
void VektivaDeviceManager::modifyValue ( const DeviceID deviceID,
const ModuleID moduleID,
const double  value 
)
protected

Finds a device with the corresponding device ID and attempts to change the state of the selected module ID.

Parameters
deviceIDID of the device to be modified
moduleIDID of the module to be modified
valuevalue to change to
Exceptions
InvalidArgumentExceptionthrown when no device is found
void VektivaDeviceManager::onlineMessageAction ( MqttMessage mqttMessage)
protected

Groups all actions that are done when "online" message is received.

Parameters
mqttMessagereceived by MQTT client
bool VektivaDeviceManager::receiveStatusMessageAndUpdate ( VektivaSmarwi::Ptr  device)
protected

When status message is updated, info is parsed and device pointer passed in argument is updated with info from the message.

Parameters
timeouthow long to wait for the status message
deviceVektiva device
Returns
true if status message arrived, false otherwise
pair< string, string > VektivaDeviceManager::retrieveDeviceInfoFromTopic ( const std::string &  topic)
protected

Exctracts Remote ID and MAC address from MQTT message' topic.

Parameters
topictopic of received message
Returns
std::pair<std::string, std::string> First is a Remote ID, the second is a MAC address.
void VektivaDeviceManager::shipSmarwiStatus ( const DeviceID deviceId,
const VektivaSmarwiStatus smarwiStatus 
)
protected

Sends Smarwi status to the exporters.

Parameters
deviceIddevice ID of the device that has changed status
smarwiStatusstatus of Smarwi
AsyncWork ::Ptr BeeeOn::VektivaDeviceManager::startDiscovery ( const Poco::Timespan &  timeout)
overrideprotectedvirtual

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 from BeeeOn::DeviceManager.

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

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 from BeeeOn::DeviceManager.

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

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 from BeeeOn::DeviceManager.

void VektivaDeviceManager::statusMessageAction ( MqttMessage mqttMessage)
protected

Groups all actions that are done when "status" message is received.

Parameters
mqttMessagereceived by MQTT client
void BeeeOn::VektivaDeviceManager::stop ( )
overridevirtual

A generic stop implementation to be used by most DeviceManager implementations.

Reimplemented from BeeeOn::DeviceManager.

bool VektivaDeviceManager::updateDevice ( VektivaSmarwi::Ptr  newDevice)
protected

New device is being processed. If true is returned, NewDeviceCommand can be dispatched.

Parameters
newDevicethe device to process
Returns
true on success, false otherwise
void VektivaDeviceManager::updateDeviceInfo ( VektivaSmarwi::Ptr  device,
const VektivaSmarwiStatus smarwiStatus 
)
protected

Function to update any relevant info that can be updated (e.g. IP address)

Parameters
devicedevice to update
smarwiStatusupdate with this data

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