mqtt wrapper first version working
This commit is contained in:
58
src/mqtt.h
Normal file
58
src/mqtt.h
Normal file
@@ -0,0 +1,58 @@
|
||||
#pragma once
|
||||
|
||||
#define DEBUGLOG_DEFAULT_LOG_LEVEL_DEBUG
|
||||
|
||||
#include <DebugLog.h>
|
||||
#include <Arduino.h>
|
||||
#include <ArduinoJson.h>
|
||||
#include <Network.h>
|
||||
#include <PubSubClient.h>
|
||||
|
||||
#include <config.h>
|
||||
|
||||
#include <mutex>
|
||||
|
||||
typedef std::string topic_t;
|
||||
typedef std::function<void(const ArduinoJson::JsonDocument &)> action_t; // the actions receive a JsonObject containing the received message
|
||||
typedef std::map<topic_t, action_t> action_map_t;
|
||||
|
||||
class MQTTwrapper
|
||||
{
|
||||
|
||||
private:
|
||||
static MQTTwrapper *getInstance(MQTTwrapper *inst = nullptr)
|
||||
{
|
||||
static std::unique_ptr<MQTTwrapper> m_instance;
|
||||
if (inst)
|
||||
m_instance.reset(inst);
|
||||
if (m_instance)
|
||||
return m_instance.get();
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
public:
|
||||
MQTTwrapper();
|
||||
~MQTTwrapper();
|
||||
|
||||
const bool connect();
|
||||
const bool disconnect();
|
||||
|
||||
const bool subscribe(topic_t topic, action_t action);
|
||||
const bool unsubscribe(topic_t topic);
|
||||
|
||||
const bool publish(topic_t topic, const ArduinoJson::JsonDocument obj);
|
||||
|
||||
private:
|
||||
static void callback(char *topic, uint8_t *payload, unsigned int length); // C-style callback only to invoke onMessage
|
||||
void onMessage(const std::string topic, const std::string message);
|
||||
|
||||
// infinite loop to call the client loop method in a taskHandle
|
||||
static void clientLoop(void *params);
|
||||
|
||||
private:
|
||||
const Config &m_config;
|
||||
action_map_t m_actionMap;
|
||||
NetworkClient m_tcp;
|
||||
PubSubClient m_client;
|
||||
TaskHandle_t m_loopHandle;
|
||||
};
|
||||
Reference in New Issue
Block a user