BeeeOn Gateway  v2020.3.1-2-g6f737dc
Platform to interconnect the IoT world
IQRFMqttConnector.h
1 #pragma once
2 
3 #include <map>
4 #include <string>
5 
6 #include <Poco/Clock.h>
7 #include <Poco/Mutex.h>
8 #include <Poco/SharedPtr.h>
9 #include <Poco/Timespan.h>
10 
11 #include "iqrf/IQRFJsonResponse.h"
12 #include "loop/StopControl.h"
13 #include "loop/StoppableRunnable.h"
14 #include "model/GlobalID.h"
15 #include "net/MqttClient.h"
16 #include "util/Loggable.h"
17 #include "util/WaitCondition.h"
18 
19 namespace BeeeOn {
20 
27 class IQRFMqttConnector final:
28  public StoppableRunnable,
29  private Loggable {
30 public:
31  typedef Poco::SharedPtr<IQRFMqttConnector> Ptr;
32 
34 
35  void run() override;
36  void stop() override;
37 
38  void setPublishTopic(const std::string &topic);
39 
40  void setMqttClient(
41  MqttClient::Ptr mqttClient);
42 
43  void setDataTimeout(const Poco::Timespan &timeout);
44  void setReceiveTimeout(const Poco::Timespan &timeout);
45  void checkPublishTopic();
46 
50  void send(const std::string &msg);
51 
56  IQRFJsonResponse::Ptr receive(
57  const GlobalID &id, const Poco::Timespan &timeout);
58 
59 private:
60  void removeExpiredMessages(const Poco::Timespan &timeout);
61 
62 private:
66  struct ReceivedData {
67  Poco::Clock receivedAt;
68  IQRFJsonResponse::Ptr message;
69  };
70 
71 private:
72  StopControl m_stopControl;
73  std::map<GlobalID, ReceivedData> m_data;
74  Poco::Timespan m_messageTimeout;
75  Poco::Timespan m_receiveTimeout;
76 
77  WaitCondition m_waitCondition;
78  Poco::FastMutex m_dataLock;
79 
80  MqttClient::Ptr m_mqttClient;
81  std::string m_publishTopic;
82 };
83 
84 }
IQRFJsonResponse::Ptr receive(const GlobalID &id, const Poco::Timespan &timeout)
Receive message with given timeout and identification of message.
Definition: IQRFMqttConnector.cpp:114
void stop() override
Definition: IQRFMqttConnector.cpp:103
Definition: GlobalID.h:10
StopControl implements the stop mechanism generically.
Definition: StopControl.h:12
Definition: Loggable.h:19
void send(const std::string &msg)
Send message using mqtt client to specific topic.
Definition: IQRFMqttConnector.cpp:109
Definition: StoppableRunnable.h:8
WaitCondition works as a barrier that waits until some condition is met. When the condition is met an...
Definition: WaitCondition.h:19
IQRFMqttConnector provides sending and receiving messages. During data receiving, it is necessary to ...
Definition: IQRFMqttConnector.h:27