Files
Bachelorarbeit-Rover/lib/Debug/MQTT/debugMqtt.h
T

158 lines
4.5 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 <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.
*/
DebugMqtt(const char* name);
/**
* @brief Send a Message via MQTT
*
* This funtion 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 funtion sends the Data via MQTT with the given topic
* from Loglevel or followed by given String topic.
* Normaly 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
*/
void sendData(Loglevel loglevel, String topic, String data);
void sendData(Loglevel loglevel, String data);
/**
* @brief Send a Message via MQTT for InfluxDB
*
* This funtion 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
*/
void writeToInflux(String measurement_name, String field_set, float measurement, uint64_t nanos);
/**
* @brief Initalize debugMQTT for all instances
*
* You only have to call this function once for your project.
* If you call this funtion again you overwrite the client and
* the loglevel. If you only eant 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;
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