documentation checked and edit

for all files in lib folder
This commit is contained in:
2023-10-12 18:45:12 +02:00
parent 61a95e4601
commit db74898b72
41 changed files with 1030 additions and 569 deletions
+48 -35
View File
@@ -5,101 +5,115 @@
* @see debugMqtt.h
* @version 0.1
* @date 2021-12-13
*
*
* @copyright Copyright (c) 2021
*
*
*/
#include "debugMqtt.h"
PubSubClient* DebugMqtt::client;
PubSubClient *DebugMqtt::client;
Loglevel DebugMqtt::loglevel;
bool DebugMqtt::isInit = false;
char DebugMqtt::msg[MQTT_BUFFER_SIZE];
char DebugMqtt::topic[MQTT_BUFFER_SIZE];
DebugMqtt::DebugMqtt(const char* name, uint8_t bufSize)
: name {name}
DebugMqtt::DebugMqtt(const char *name, uint8_t bufSize)
: name{name}
{
if (bufSize != 0) {
if (bufSize != 0)
{
this->bufSize = bufSize;
}
this->buf = new char[this->bufSize];
}
DebugMqtt::~DebugMqtt() {
DebugMqtt::~DebugMqtt()
{
delete this->buf;
}
void DebugMqtt::sendMsg(Loglevel loglevel, String topic, String msg) {
snprintf (static_cast<char*>(DebugMqtt::msg), MQTT_BUFFER_SIZE, static_cast<const char*>("%s: %s"),this->name ,msg.c_str());
this->sendData(loglevel, topic, static_cast<const char*>(DebugMqtt::msg));
void DebugMqtt::sendMsg(Loglevel loglevel, String topic, String msg)
{
snprintf(static_cast<char *>(DebugMqtt::msg), MQTT_BUFFER_SIZE, static_cast<const char *>("%s: %s"), this->name, msg.c_str());
this->sendData(loglevel, topic, static_cast<const char *>(DebugMqtt::msg));
}
void DebugMqtt::sendMsg(Loglevel loglevel, String msg) {
void DebugMqtt::sendMsg(Loglevel loglevel, String msg)
{
this->sendMsg(loglevel, "", msg);
}
void DebugMqtt::sendData(Loglevel loglevel, String topic, String data) {
if (!DebugMqtt::isInit) {
void DebugMqtt::sendData(Loglevel loglevel, String topic, String data)
{
if (!DebugMqtt::isInit)
{
return;
}
if (loglevel <= DebugMqtt::loglevel && loglevel > Loglevel::none) {
snprintf (static_cast<char*>(DebugMqtt::topic), MQTT_BUFFER_SIZE, static_cast<const char*>("%s%s"), DebugMqtt::enum_to_string(loglevel).c_str(), topic.c_str());
snprintf (static_cast<char*>(DebugMqtt::msg), MQTT_BUFFER_SIZE, static_cast<const char*>("%s"), data.c_str());
client->publish(DebugMqtt::topic, static_cast<const char*>(DebugMqtt::msg));
if (loglevel <= DebugMqtt::loglevel && loglevel > Loglevel::none)
{
snprintf(static_cast<char *>(DebugMqtt::topic), MQTT_BUFFER_SIZE, static_cast<const char *>("%s%s"), DebugMqtt::enum_to_string(loglevel).c_str(), topic.c_str());
snprintf(static_cast<char *>(DebugMqtt::msg), MQTT_BUFFER_SIZE, static_cast<const char *>("%s"), data.c_str());
client->publish(DebugMqtt::topic, static_cast<const char *>(DebugMqtt::msg));
}
}
void DebugMqtt::sendData(Loglevel loglevel, String data){
void DebugMqtt::sendData(Loglevel loglevel, String data)
{
DebugMqtt::sendData(loglevel, "", data);
}
void DebugMqtt::writeToInflux(String measurement_name, String field_set, float measurement, uint64_t nanos) {
void DebugMqtt::writeToInflux(String measurement_name, String field_set, float measurement, uint64_t nanos)
{
// Example String: "weather temperature=82 1465839830100400200";
snprintf(static_cast<char*>(DebugMqtt::msg), MQTT_BUFFER_SIZE, static_cast<const char*>("%s %s=%f %llu"), measurement_name.c_str(), field_set.c_str(), measurement, nanos);
this->sendData(Loglevel::influx, static_cast<const char*>(DebugMqtt::msg));
snprintf(static_cast<char *>(DebugMqtt::msg), MQTT_BUFFER_SIZE, static_cast<const char *>("%s %s=%f %llu"), measurement_name.c_str(), field_set.c_str(), measurement, nanos);
this->sendData(Loglevel::influx, static_cast<const char *>(DebugMqtt::msg));
}
void DebugMqtt::addCharacter(char character) {
void DebugMqtt::addCharacter(char character)
{
this->buf[this->bufPos] = character;
this->bufPos++;
if (character == '\n' || this->bufPos >= this->bufSize - 1) {
if (character == '\n' || this->bufPos >= this->bufSize - 1)
{
this->buf[this->bufPos - 1] = '\0';
this->sendMsg(Loglevel::info, buf);
this->bufPos = 0;
}
}
void DebugMqtt::init(PubSubClient *client, Loglevel max_loglevel) {
void DebugMqtt::init(PubSubClient *client, Loglevel max_loglevel)
{
DebugMqtt::client = client;
DebugMqtt::loglevel = max_loglevel;
DebugMqtt::isInit = true;
}
void DebugMqtt::changeLoglevel(Loglevel loglevel) {
void DebugMqtt::changeLoglevel(Loglevel loglevel)
{
DebugMqtt::loglevel = loglevel;
}
String DebugMqtt::enum_to_string(Loglevel loglevel) {
String DebugMqtt::enum_to_string(Loglevel loglevel)
{
String topic = "";
topic += MQTT_DEBUG_TOPIC;
switch(loglevel){
case Loglevel::error :
switch (loglevel)
{
case Loglevel::error:
topic += "/Error";
break;
case Loglevel::warn :
case Loglevel::warn:
topic += "/Warn";
break;
case Loglevel::info :
case Loglevel::info:
topic += "/Info";
break;
case Loglevel::debug :
case Loglevel::debug:
topic += "/Debug";
break;
case Loglevel::influx :
case Loglevel::influx:
topic += "/Influx";
break;
default:
@@ -107,5 +121,4 @@ String DebugMqtt::enum_to_string(Loglevel loglevel) {
break;
}
return topic;
}
}
+120 -117
View File
@@ -4,9 +4,9 @@
* @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
@@ -14,10 +14,9 @@
#include <iostream>
#include <PubSubClient.h>
/**
* @brief
*
* @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.
@@ -28,151 +27,155 @@
/**
* @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
#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};
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);
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();
~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 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
*
* 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 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 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 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);
/**
* @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;
private:
const char *name;
char *buf;
uint8_t bufPos = 0;
uint8_t bufSize = 100;
static String enum_to_string(Loglevel loglevel);
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];
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