BeeeOn Gateway  v2020.3.1-2-g6f737dc
Platform to interconnect the IoT world
SonoffDeviceManager.h
1 #pragma once
2 
3 #include <map>
4 
5 #include <Poco/Mutex.h>
6 #include <Poco/Timespan.h>
7 
8 #include "core/DeviceManager.h"
9 #include "model/DeviceID.h"
10 #include "net/MqttClient.h"
11 #include "sonoff/SonoffDevice.h"
12 
13 namespace BeeeOn {
14 
21 public:
23 
24  void run() override;
25  void stop() override;
26  void setMqttClient(MqttClient::Ptr mqttClient);
27  void setMaxLastSeen(const Poco::Timespan &timeout);
28 
29 protected:
30  AsyncWork<>::Ptr startDiscovery(const Poco::Timespan &timeout) override;
32  const DeviceID &id,
33  const Poco::Timespan &) override;
34  void handleAccept(const DeviceAcceptCommand::Ptr cmd) override;
35 
40  void processMQTTMessage(const MqttMessage &message);
41 
46  std::pair<DeviceID, std::string> retrieveDeviceInfo(const std::string &message) const;
47 
52  void createNewDevice(const std::pair<DeviceID, std::string>& deviceInfo);
53 
54 private:
55  Poco::FastMutex m_devicesMutex;
56  std::map<DeviceID, SonoffDevice::Ptr> m_devices;
57 
58  MqttClient::Ptr m_mqttClient;
59  Poco::Timespan m_maxLastSeen;
60 };
61 }
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...
Definition: SonoffDeviceManager.cpp:159
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: SonoffDeviceManager.cpp:200
void createNewDevice(const std::pair< DeviceID, std::string > &deviceInfo)
Creates instance of sonoff device according to device name and appends it into m_devices.
Definition: SonoffDeviceManager.cpp:145
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
void stop() override
Definition: SonoffDeviceManager.cpp:87
Definition: MqttMessage.h:7
std::pair< DeviceID, std::string > retrieveDeviceInfo(const std::string &message) const
Retrieves device information from JSON message received from MQTT broker.
Definition: SonoffDeviceManager.cpp:125
void processMQTTMessage(const MqttMessage &message)
Processes the incoming MQTT message, which means creating a new device or sending the gathered data t...
Definition: SonoffDeviceManager.cpp:93
Definition: DeviceManager.h:48
The class implements the work with Sonoff devices. Allows us to process and execute the commands from...
Definition: SonoffDeviceManager.h:20
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: SonoffDeviceManager.cpp:189
Definition: DeviceID.h:17