moving files
This commit is contained in:
@@ -0,0 +1,90 @@
|
||||
/**
|
||||
* @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_SIZE];
|
||||
char DebugMqtt::topic[MQTT_BUFFER_SIZE];
|
||||
|
||||
|
||||
DebugMqtt::DebugMqtt(const char* name) {
|
||||
this->name = name;
|
||||
}
|
||||
|
||||
void DebugMqtt::sendMsg(Loglevel loglevel, String topic, String msg) {
|
||||
snprintf (DebugMqtt::msg, MQTT_BUFFER_SIZE, "%s: %s",this->name ,msg.c_str());
|
||||
this->sendData(loglevel, topic, DebugMqtt::msg);
|
||||
}
|
||||
|
||||
void DebugMqtt::sendMsg(Loglevel loglevel, String msg) {
|
||||
this->sendMsg(loglevel, "", msg);
|
||||
}
|
||||
|
||||
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_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);
|
||||
}
|
||||
}
|
||||
|
||||
void DebugMqtt::sendData(Loglevel loglevel, String data){
|
||||
this->sendData(loglevel, "", data);
|
||||
}
|
||||
|
||||
void DebugMqtt::writeToInflux(String measurement_name, String field_set, float measurement, uint64_t nanos) {
|
||||
// Example String: "weather temperature=82 1465839830100400200";
|
||||
|
||||
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::init(PubSubClient *client, Loglevel max_loglevel) {
|
||||
DebugMqtt::client = client;
|
||||
DebugMqtt::loglevel = loglevel;
|
||||
DebugMqtt::isInit = true;
|
||||
}
|
||||
|
||||
void DebugMqtt::changeLoglevel(Loglevel loglevel) {
|
||||
DebugMqtt::loglevel = loglevel;
|
||||
}
|
||||
|
||||
String DebugMqtt::enum_to_string(Loglevel loglevel) {
|
||||
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;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,158 @@
|
||||
/**
|
||||
* @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
|
||||
Reference in New Issue
Block a user