BeeeOn Gateway  v2020.3.1-2-g6f737dc
Platform to interconnect the IoT world
VektivaDeviceManager.h
1 #pragma once
2 
3 #include <vector>
4 
5 #include <Poco/Mutex.h>
6 
7 #include "core/AbstractSeeker.h"
8 #include "core/DeviceManager.h"
9 #include "model/DeviceID.h"
10 #include "net/MqttClient.h"
11 #include "net/MACAddress.h"
12 #include "util/BlockingAsyncWork.h"
13 #include "vektiva/VektivaSmarwi.h"
14 
15 namespace BeeeOn {
16 
23 public:
24 
29  class VektivaSeeker : public AbstractSeeker {
30  public:
31  typedef Poco::SharedPtr<VektivaSeeker> Ptr;
33  const Poco::Timespan &duration);
34 
35  protected:
36 
42  void seekLoop(StopControl &) override;
43 
44  private:
45  VektivaDeviceManager &m_parent;
46  };
47 
49  void run() override;
50  void stop() override;
51  void setMqttClient(MqttClient::Ptr mqttClient);
52  void setStatusMqttClient(MqttClient::Ptr mqttClient);
53  void setReceiveTimeout(const Poco::Timespan &timeout);
54 
55 protected:
56  AsyncWork<>::Ptr startDiscovery(const Poco::Timespan &timeout) override;
58  const DeviceID &id,
59  const Poco::Timespan &) override;
60  AsyncWork<double>::Ptr startSetValue(
61  const DeviceID &id,
62  const ModuleID &module,
63  const double value,
64  const Poco::Timespan &) override;
65  void handleAccept(const DeviceAcceptCommand::Ptr cmd) override;
66 
73  bool updateDevice(VektivaSmarwi::Ptr newDevice);
74 
83  void modifyValue(
84  const DeviceID &deviceID,
85  const ModuleID &moduleID,
86  const double value);
87 
93  void statusMessageAction(MqttMessage &mqttMessage);
94 
100  void onlineMessageAction(MqttMessage &mqttMessage);
101 
106  void clearMqttMessageBuffer();
107 
113  void analyzeMessage(MqttMessage &mqttMessage);
114 
126  const std::string &lastSegment,
127  VektivaSmarwi::Ptr device);
128 
134  void shipSmarwiStatus(
135  const DeviceID &deviceId,
136  const VektivaSmarwiStatus &smarwiStatus);
137 
142  void dispatchNewDevice(VektivaSmarwi::Ptr device);
143 
152  VektivaSmarwi::Ptr device);
153 
160  void updateDeviceInfo(
161  VektivaSmarwi::Ptr device,
162  const VektivaSmarwiStatus &smarwiStatus);
163 
200  bool isTopicValid(
201  const std::string &topic,
202  const std::string &lastSegment,
203  const std::string &remoteId = "",
204  const std::string &macAddr = "");
205 
212  std::pair<std::string, std::string>
213  retrieveDeviceInfoFromTopic(const std::string &topic);
214 
219  void escapeRegexString(std::string &regexString);
220 
221 private:
225  Poco::FastMutex m_devicesMutex;
226 
231  Poco::FastMutex m_clientMqttMutex;
232 
236  std::map<DeviceID, VektivaSmarwi::Ptr> m_devices;
237 
241  MqttClient::Ptr m_mqttClient;
242 
248  MqttClient::Ptr m_mqttStatusClient;
249  Poco::Timespan m_receiveTimeout;
250 };
251 }
void updateDeviceInfo(VektivaSmarwi::Ptr device, const VektivaSmarwiStatus &smarwiStatus)
Function to update any relevant info that can be updated (e.g. IP address)
Definition: VektivaDeviceManager.cpp:389
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 lastSe...
Definition: VektivaDeviceManager.cpp:326
void statusMessageAction(MqttMessage &mqttMessage)
Groups all actions that are done when &quot;status&quot; message is received.
Definition: VektivaDeviceManager.cpp:289
void seekLoop(StopControl &) override
Definition: VektivaDeviceManager.cpp:102
Provides searching of Vektiva devices on network via MQTT messages in an own thread.
Definition: VektivaDeviceManager.h:29
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 critica...
Definition: VektivaDeviceManager.cpp:246
Vektiva device manager provides an easy way to manage devices that are compatible with implemented in...
Definition: VektivaDeviceManager.h:22
void clearMqttMessageBuffer()
Clears all messages buffered in the MQTT client to assure there are no previous messages when attempt...
Definition: VektivaDeviceManager.cpp:374
AsyncWork::Ptr startDiscovery(const Poco::Timespan &timeout) override
Starts device discovery process in a technology-specific way. This method is always called inside a c...
AsyncWork is an interface allowing to wait until an asynchronous operation finishes. It is also possible to force-stop it by calling cancel().
Definition: AsyncWork.h:21
AbstractSeeker represents an asynchronous process that seeks for new devices in a certain network...
Definition: AbstractSeeker.h:25
Definition: MqttMessage.h:7
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 modu...
Definition: VektivaDeviceManager.cpp:264
StopControl implements the stop mechanism generically.
Definition: StopControl.h:12
Definition: ModuleID.h:12
The class represents the status of the Smarwi device. The status information is obtained by MQTT clie...
Definition: VektivaSmarwiStatus.h:11
void onlineMessageAction(MqttMessage &mqttMessage)
Groups all actions that are done when &quot;online&quot; message is received.
Definition: VektivaDeviceManager.cpp:311
Poco::Timespan duration() const
Definition: AbstractSeeker.cpp:17
void escapeRegexString(std::string &regexString)
Escapes inputted string from regex control characters.
Definition: VektivaDeviceManager.cpp:240
void handleAccept(const DeviceAcceptCommand::Ptr cmd) override
Generic handler of the DeviceAcceptCommand. It might be helpful to override this method in case we ne...
Definition: VektivaDeviceManager.cpp:142
Definition: DeviceManager.h:48
void analyzeMessage(MqttMessage &mqttMessage)
Parses the received message and according to its&#39; content, correct actions are performed.
Definition: VektivaDeviceManager.cpp:280
void dispatchNewDevice(VektivaSmarwi::Ptr device)
Dispatches NewDeviceCommand.
Definition: VektivaDeviceManager.cpp:437
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 crit...
Definition: VektivaDeviceManager.cpp:160
std::pair< std::string, std::string > retrieveDeviceInfoFromTopic(const std::string &topic)
Exctracts Remote ID and MAC address from MQTT message&#39; topic.
Definition: VektivaDeviceManager.cpp:190
bool updateDevice(VektivaSmarwi::Ptr newDevice)
New device is being processed. If true is returned, NewDeviceCommand can be dispatched.
Definition: VektivaDeviceManager.cpp:110
void shipSmarwiStatus(const DeviceID &deviceId, const VektivaSmarwiStatus &smarwiStatus)
Sends Smarwi status to the exporters.
Definition: VektivaDeviceManager.cpp:415
Definition: DeviceID.h:17
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 specif...
Definition: VektivaDeviceManager.cpp:211
bool receiveStatusMessageAndUpdate(VektivaSmarwi::Ptr device)
When status message is updated, info is parsed and device pointer passed in argument is updated with ...
Definition: VektivaDeviceManager.cpp:396