comments for Doxygen all private Libs
This commit is contained in:
@@ -1,12 +1,21 @@
|
||||
/**
|
||||
* @file debugMqtt.cpp
|
||||
* @author Alexander Klein (alex@kleiax.de)
|
||||
* @brief Implemention of the class debugMqtt.h
|
||||
* @see debugMqtt.h
|
||||
* @version 0.1
|
||||
* @date 2021-12-13
|
||||
*
|
||||
* @copyright Copyright (c) 2021
|
||||
*
|
||||
*/
|
||||
#include "debugMqtt.h"
|
||||
|
||||
PubSubClient* DebugMqtt::client;
|
||||
Loglevel DebugMqtt::loglevel;
|
||||
bool DebugMqtt::isInit = false;
|
||||
char DebugMqtt::msg[MQTT_BUFFER_SITE];
|
||||
char DebugMqtt::topic[MQTT_BUFFER_SITE];
|
||||
unsigned long long int DebugMqtt::real_millis = 0;
|
||||
unsigned long int DebugMqtt::last_millis = 0;
|
||||
char DebugMqtt::msg[MQTT_BUFFER_SIZE];
|
||||
char DebugMqtt::topic[MQTT_BUFFER_SIZE];
|
||||
|
||||
|
||||
DebugMqtt::DebugMqtt(const char* name) {
|
||||
@@ -14,7 +23,7 @@ DebugMqtt::DebugMqtt(const char* name) {
|
||||
}
|
||||
|
||||
void DebugMqtt::sendMsg(Loglevel loglevel, String topic, String msg) {
|
||||
snprintf (DebugMqtt::msg, MQTT_BUFFER_SITE, "%s: %s",this->name ,msg.c_str());
|
||||
snprintf (DebugMqtt::msg, MQTT_BUFFER_SIZE, "%s: %s",this->name ,msg.c_str());
|
||||
this->sendData(loglevel, topic, DebugMqtt::msg);
|
||||
}
|
||||
|
||||
@@ -26,8 +35,8 @@ void DebugMqtt::sendData(Loglevel loglevel, String topic, String data) {
|
||||
if (!DebugMqtt::isInit) {return;}
|
||||
|
||||
if (loglevel <= DebugMqtt::loglevel && loglevel > Loglevel::none) {
|
||||
snprintf (DebugMqtt::topic, MQTT_BUFFER_SITE, "%s%s", DebugMqtt::enum_to_string(loglevel).c_str(), topic.c_str());
|
||||
snprintf (DebugMqtt::msg, MQTT_BUFFER_SITE, "%s", data.c_str());
|
||||
snprintf (DebugMqtt::topic, MQTT_BUFFER_SIZE, "%s%s", DebugMqtt::enum_to_string(loglevel).c_str(), topic.c_str());
|
||||
snprintf (DebugMqtt::msg, MQTT_BUFFER_SIZE, "%s", data.c_str());
|
||||
client->publish(DebugMqtt::topic, DebugMqtt::msg);
|
||||
}
|
||||
}
|
||||
@@ -36,24 +45,14 @@ void DebugMqtt::sendData(Loglevel loglevel, String data){
|
||||
this->sendData(loglevel, "", data);
|
||||
}
|
||||
|
||||
void DebugMqtt::writeToInflux(String measurement_name, String field_set, float measurement) {
|
||||
void DebugMqtt::writeToInflux(String measurement_name, String field_set, float measurement, uint64_t nanos) {
|
||||
// Example String: "weather temperature=82 1465839830100400200";
|
||||
|
||||
unsigned long long int nanos = DebugMqtt::getUpdatedRealMillis() * 1000000;
|
||||
snprintf(DebugMqtt::msg, MQTT_BUFFER_SITE, "%s %s=%f %llu", measurement_name.c_str(), field_set.c_str(), measurement, nanos);
|
||||
snprintf(DebugMqtt::msg, MQTT_BUFFER_SIZE, "%s %s=%f %llu", measurement_name.c_str(), field_set.c_str(), measurement, nanos);
|
||||
this->sendData(Loglevel::influx, DebugMqtt::msg);
|
||||
}
|
||||
|
||||
void DebugMqtt::sendTime() {
|
||||
struct tm timeinfo;
|
||||
if(!getLocalTime(&timeinfo)){
|
||||
snprintf(DebugMqtt::msg, MQTT_BUFFER_SITE, "Failed to obtain time");
|
||||
}
|
||||
snprintf(DebugMqtt::msg, MQTT_BUFFER_SITE, "%d %d %d %d %d %d %llu", timeinfo.tm_year, timeinfo.tm_mon, timeinfo.tm_mday, timeinfo.tm_hour, timeinfo.tm_min, timeinfo.tm_sec, DebugMqtt::getUpdatedRealMillis());
|
||||
this->sendMsg(Loglevel::info, DebugMqtt::msg);
|
||||
}
|
||||
|
||||
void DebugMqtt::init(PubSubClient *client, Loglevel loglevel) {
|
||||
void DebugMqtt::init(PubSubClient *client, Loglevel max_loglevel) {
|
||||
DebugMqtt::client = client;
|
||||
DebugMqtt::loglevel = loglevel;
|
||||
DebugMqtt::isInit = true;
|
||||
@@ -63,40 +62,29 @@ void DebugMqtt::changeLoglevel(Loglevel loglevel) {
|
||||
DebugMqtt::loglevel = loglevel;
|
||||
}
|
||||
|
||||
void DebugMqtt::initRealMillis() {
|
||||
time_t now;
|
||||
struct tm timeinfo;
|
||||
if(!getLocalTime(&timeinfo)){
|
||||
snprintf(DebugMqtt::msg, MQTT_BUFFER_SITE, "Failed to obtain time in initRealMillis()");
|
||||
DebugMqtt::client->publish(DebugMqtt::enum_to_string(Loglevel::error).c_str(), DebugMqtt::msg);
|
||||
return;
|
||||
}
|
||||
time(&now);
|
||||
DebugMqtt::last_millis = millis();
|
||||
DebugMqtt::real_millis = now;
|
||||
DebugMqtt::real_millis = DebugMqtt::real_millis * 1000 + DebugMqtt::last_millis;
|
||||
}
|
||||
|
||||
unsigned long long int DebugMqtt::getUpdatedRealMillis() {
|
||||
DebugMqtt::real_millis = DebugMqtt::real_millis + (millis() - DebugMqtt::last_millis);
|
||||
DebugMqtt::last_millis = millis();
|
||||
return DebugMqtt::real_millis;
|
||||
}
|
||||
|
||||
String DebugMqtt::enum_to_string(Loglevel loglevel) {
|
||||
switch(loglevel){
|
||||
case Loglevel::error :
|
||||
return "Rover/Error";
|
||||
case Loglevel::warn :
|
||||
return "Rover/Warn";
|
||||
case Loglevel::info :
|
||||
return "Rover/Info";
|
||||
case Loglevel::debug :
|
||||
return "Rover/Debug";
|
||||
case Loglevel::influx :
|
||||
return "Rover/Influx";
|
||||
default:
|
||||
return "INVALID ENUM";
|
||||
}
|
||||
String topic = "";
|
||||
topic += MQTT_DEBUG_TOPIC;
|
||||
switch(loglevel){
|
||||
case Loglevel::error :
|
||||
topic += "/Error";
|
||||
break;
|
||||
case Loglevel::warn :
|
||||
topic += "/Warn";
|
||||
break;
|
||||
case Loglevel::info :
|
||||
topic += "/Info";
|
||||
break;
|
||||
case Loglevel::debug :
|
||||
topic += "/Debug";
|
||||
break;
|
||||
case Loglevel::influx :
|
||||
topic += "/Influx";
|
||||
break;
|
||||
default:
|
||||
topic += "/NoLoglovel";
|
||||
break;
|
||||
}
|
||||
return topic;
|
||||
}
|
||||
|
||||
|
||||
+123
-12
@@ -1,10 +1,51 @@
|
||||
/**
|
||||
* @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>
|
||||
|
||||
#include "config.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,
|
||||
@@ -12,21 +53,95 @@ enum Loglevel { none,
|
||||
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);
|
||||
void writeToInflux(String measurement_name, String field_set, float measurement);
|
||||
|
||||
void sendTime();
|
||||
/**
|
||||
* @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);
|
||||
|
||||
static void init(PubSubClient *client, Loglevel loglevel);
|
||||
/**
|
||||
* @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);
|
||||
static void initRealMillis();
|
||||
static unsigned long long int getUpdatedRealMillis();
|
||||
|
||||
private:
|
||||
const char* name;
|
||||
@@ -36,12 +151,8 @@ class DebugMqtt {
|
||||
static PubSubClient* client;
|
||||
static Loglevel loglevel;
|
||||
static bool isInit;
|
||||
static char msg[MQTT_BUFFER_SITE];
|
||||
static char topic[MQTT_BUFFER_SITE];
|
||||
|
||||
static unsigned long long int real_millis;
|
||||
static unsigned long int last_millis;
|
||||
|
||||
static char msg[MQTT_BUFFER_SIZE];
|
||||
static char topic[MQTT_BUFFER_SIZE];
|
||||
};
|
||||
|
||||
#endif // DEBUG_MQTT_H
|
||||
Reference in New Issue
Block a user