179 lines
5.0 KiB
C++
179 lines
5.0 KiB
C++
/**
|
|
* @file debugMqtt.h
|
|
* @author Alexander Klein (alex@kleiax.de)
|
|
* @brief Inherits a class to send debug messages over MQTT
|
|
* @version 0.1
|
|
* @date 2021-12-13
|
|
*
|
|
* @copyright Copyright (c) 2021
|
|
*
|
|
*/
|
|
#ifndef DEBUG_MQTT_H
|
|
#define DEBUG_MQTT_H
|
|
|
|
#include <iostream>
|
|
#include <PubSubClient.h>
|
|
|
|
|
|
/**
|
|
* @brief
|
|
*
|
|
* If you want to change the default topic
|
|
* to an other value, than you have to define this define
|
|
* in your code befor you include this File.
|
|
*/
|
|
#ifndef MQTT_DEBUG_TOPIC
|
|
#define MQTT_DEBUG_TOPIC "Rover"
|
|
#endif // MQTT_DEBUG_TOPIC
|
|
|
|
/**
|
|
* @brief Defualt for max message size
|
|
*
|
|
* If you want to change the default size of 128 Byte
|
|
* to an other value, than you have to define this define
|
|
* in your code befor you include this File.
|
|
*/
|
|
#ifndef MQTT_BUFFER_SIZE
|
|
#define MQTT_BUFFER_SIZE 128
|
|
#endif //MQTT_BUFFER_SIZE
|
|
|
|
/**
|
|
* @brief An enum to set the log level
|
|
*
|
|
* The log level is the last part of the MQTT topic.
|
|
* Unless you give sendMsg() or sendData() a additional
|
|
* topic as String.
|
|
*
|
|
* @see sendMsg()
|
|
* @see sendData()
|
|
*
|
|
*/
|
|
enum Loglevel { none,
|
|
error,
|
|
warn,
|
|
info,
|
|
debug,
|
|
influx};
|
|
|
|
/**
|
|
* @brief A class to send debug messages over MQTT
|
|
*
|
|
* This class send debug messages over MQTT with different topics
|
|
* by the enum Loglevel. Besides that this class supports to send
|
|
* data via Telegraf into Grafana.
|
|
*
|
|
* @see Loglevel
|
|
*/
|
|
class DebugMqtt {
|
|
public:
|
|
/**
|
|
* @brief Construct a new Debug Mqtt object.
|
|
*
|
|
* @param name A String with send with every Message.
|
|
* @param bufSize for the addCharacter function.
|
|
*/
|
|
DebugMqtt(const char* name, uint8_t bufSize = 0);
|
|
|
|
~DebugMqtt();
|
|
|
|
/**
|
|
* @brief Send a Message via MQTT
|
|
*
|
|
* This function uses sendData to send the given string and
|
|
* add the name to the message given by the constructer.
|
|
*
|
|
* @see sendData()
|
|
* @see Loglevel
|
|
*
|
|
* @param loglevel Loglevel given by the enum Loglevel
|
|
* @param topic Additional topic behind loglevel
|
|
* @param msg The message to send as String
|
|
*/
|
|
void sendMsg(Loglevel loglevel, String topic, String msg);
|
|
void sendMsg(Loglevel loglevel, String msg);
|
|
|
|
/**
|
|
* @brief Send a Message via MQTT
|
|
*
|
|
* This function sends the Data via MQTT with the given topic
|
|
* from Loglevel or followed by given String topic.
|
|
* Normally this function is called by sendMsg() or by
|
|
* writeToInflux()
|
|
*
|
|
* @see sendData()
|
|
* @see writeToInflux()
|
|
* @see Loglevel
|
|
*
|
|
* @param loglevel Loglevel given by the enum Loglevel
|
|
* @param topic Additional topic behind loglevel
|
|
* @param data The message to send as String
|
|
*/
|
|
static void sendData(Loglevel loglevel, String topic, String data);
|
|
static void sendData(Loglevel loglevel, String data);
|
|
|
|
/**
|
|
* @brief Send a Message via MQTT for InfluxDB
|
|
*
|
|
* This function sends a MQTT message which is intended for
|
|
* Telegraf. Telegraf can listen on MQTT messages and put
|
|
* them in an Influx Database.
|
|
*
|
|
* @param measurement_name like a category
|
|
* @param field_set the name of the value e.g temperature
|
|
* @param measurement the real value
|
|
* @param nanos Current time in nanoseconds
|
|
*/
|
|
void writeToInflux(String measurement_name, String field_set, float measurement, uint64_t nanos);
|
|
|
|
/**
|
|
* @brief Adds a single character to the buf
|
|
*
|
|
* The buf will be flushed out:
|
|
* 1. when the buffer is full
|
|
* 2. when the character is '\n'
|
|
*
|
|
* @param character
|
|
*/
|
|
void addCharacter(char character);
|
|
|
|
/**
|
|
* @brief Initialize debugMQTT for all instances
|
|
*
|
|
* You only have to call this function once for your project.
|
|
* If you call this function again you overwrite the client and
|
|
* the loglevel. If you only want du overwrite the max_loglevel
|
|
* use changeLoglevel()
|
|
*
|
|
* @see changeLoglevel()
|
|
*
|
|
* @param client PubSubClient
|
|
* @param max_loglevel Max loglevel to send.
|
|
*/
|
|
static void init(PubSubClient* client, Loglevel max_loglevel);
|
|
|
|
/**
|
|
* @brief Change loglevel
|
|
*
|
|
* This function changes the maximum loglevel which be send.
|
|
*
|
|
* @param loglevel
|
|
*/
|
|
static void changeLoglevel(Loglevel loglevel);
|
|
|
|
private:
|
|
const char* name;
|
|
char* buf;
|
|
uint8_t bufPos = 0;
|
|
uint8_t bufSize = 100;
|
|
|
|
static String enum_to_string(Loglevel loglevel);
|
|
|
|
static PubSubClient* client;
|
|
static Loglevel loglevel;
|
|
static bool isInit;
|
|
static char msg[MQTT_BUFFER_SIZE];
|
|
static char topic[MQTT_BUFFER_SIZE];
|
|
};
|
|
|
|
#endif // DEBUG_MQTT_H
|