feature: Including MQTT client
This commit is contained in:
@@ -0,0 +1,65 @@
|
||||
/**
|
||||
* @file mqttClient.hpp
|
||||
* @author Gustice
|
||||
* @brief Wrapper class for TCP/IP based MQTT APIs
|
||||
*
|
||||
* @copyright Copyright (c) 2024
|
||||
*/
|
||||
#pragma once
|
||||
|
||||
#include "esp_event.h"
|
||||
#include "esp_system.h"
|
||||
#include "mqtt_client.h"
|
||||
#include <functional>
|
||||
#include <string>
|
||||
#include <string_view>
|
||||
|
||||
/**
|
||||
* @brief MqttClient wrapper (Singleton)
|
||||
*/
|
||||
class MqttClient {
|
||||
public:
|
||||
/// @brief Init and mount file system
|
||||
/// @param broker URI to web MQTT broker server
|
||||
static MqttClient &init(std::string broker, std::string prefix);
|
||||
void addSubscription(std::string topic, std::function<void(std::string)> call);
|
||||
void publish(std::string topic, std::string data);
|
||||
void start();
|
||||
bool isRunning() { return _state != State::Init; }
|
||||
|
||||
/// @brief Get instance, ensure initialization
|
||||
/// @param broker URI to web MQTT broker server
|
||||
/// @returnb
|
||||
static MqttClient &getInstance();
|
||||
|
||||
// Disable move and assignment
|
||||
MqttClient(MqttClient &other) = delete;
|
||||
void operator=(const MqttClient &) = delete;
|
||||
|
||||
// protected:
|
||||
MqttClient(std::string broker, std::string pefix);
|
||||
~MqttClient();
|
||||
|
||||
private:
|
||||
enum class State {
|
||||
Init,
|
||||
Started,
|
||||
Disconnected,
|
||||
Connected
|
||||
};
|
||||
struct Subscription {
|
||||
std::string topic;
|
||||
std::function<void(std::string)> callback;
|
||||
bool active{};
|
||||
int id{};
|
||||
};
|
||||
|
||||
std::string _broker;
|
||||
std::string _prefix;
|
||||
State _state{};
|
||||
static MqttClient *_instance;
|
||||
esp_mqtt_client_handle_t _client;
|
||||
std::vector<Subscription> _subscriptions;
|
||||
|
||||
static void eventHandler(void *handler_args, esp_event_base_t base, int32_t event_id, void *event_data);
|
||||
};
|
||||
@@ -269,6 +269,9 @@ class Syslog {
|
||||
if (_Socket == nullptr) {
|
||||
return;
|
||||
}
|
||||
// if (severity > _severityThresh) {
|
||||
// return;
|
||||
// }
|
||||
|
||||
char buffer[512];
|
||||
// V T H A P M D Msg
|
||||
@@ -286,6 +289,7 @@ class Syslog {
|
||||
if (_Socket) {
|
||||
return;
|
||||
}
|
||||
ESP_LOGI("UDP", "Starting syslog session to %s", target.getIp().c_str());
|
||||
_Facility = Facility::Syslog;
|
||||
_HostName = "ESP";
|
||||
_AppName = "App";
|
||||
@@ -301,4 +305,5 @@ class Syslog {
|
||||
static std::string _AppName;
|
||||
static UdpSocket *_Socket;
|
||||
static IpAdr _Target;
|
||||
static Severity _severityThresh;
|
||||
};
|
||||
Reference in New Issue
Block a user