Files
Bachelorarbeit-Rover/lib/MQTT/debugMqtt.cpp
T
2021-12-13 18:25:47 +01:00

91 lines
2.5 KiB
C++

/**
* @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;
}