added duplicate console to mqtt

This commit is contained in:
2022-02-14 19:31:24 +01:00
parent 6cacea2df3
commit 3a2e51713b
19 changed files with 262 additions and 166 deletions
+19 -2
View File
@@ -20,6 +20,11 @@ char DebugMqtt::topic[MQTT_BUFFER_SIZE];
DebugMqtt::DebugMqtt(const char* name) {
this->name = name;
this->buf = new char[this->bufSitze];
}
DebugMqtt::~DebugMqtt() {
delete this->buf;
}
void DebugMqtt::sendMsg(Loglevel loglevel, String topic, String msg) {
@@ -32,7 +37,9 @@ void DebugMqtt::sendMsg(Loglevel loglevel, String msg) {
}
void DebugMqtt::sendData(Loglevel loglevel, String topic, String data) {
if (!DebugMqtt::isInit) {return;}
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());
@@ -52,9 +59,19 @@ void DebugMqtt::writeToInflux(String measurement_name, String field_set, float m
this->sendData(Loglevel::influx, DebugMqtt::msg);
}
void DebugMqtt::addCharacter(char c) {
this->buf[this->bufPos] = c;
this->bufPos++;
if (c == '\n' || this->bufPos >= this->bufSitze - 1) {
this->buf[this->bufPos - 1] = '\0';
this->sendMsg(Loglevel::info, buf);
this->bufPos = 0;
}
}
void DebugMqtt::init(PubSubClient *client, Loglevel max_loglevel) {
DebugMqtt::client = client;
DebugMqtt::loglevel = loglevel;
DebugMqtt::loglevel = max_loglevel;
DebugMqtt::isInit = true;
}
+10 -2
View File
@@ -11,6 +11,7 @@
#ifndef DEBUG_MQTT_H
#define DEBUG_MQTT_H
#include <iostream>
#include <PubSubClient.h>
/**
@@ -71,6 +72,8 @@ class DebugMqtt {
*/
DebugMqtt(const char* name);
~DebugMqtt();
/**
* @brief Send a Message via MQTT
*
@@ -120,6 +123,8 @@ class DebugMqtt {
*/
void writeToInflux(String measurement_name, String field_set, float measurement, uint64_t nanos);
void addCharacter(char c);
/**
* @brief Initalize debugMQTT for all instances
*
@@ -133,7 +138,7 @@ class DebugMqtt {
* @param client PubSubClient
* @param max_loglevel Max loglevel to send.
*/
static void init(PubSubClient *client, Loglevel max_loglevel);
static void init(PubSubClient* client, Loglevel max_loglevel);
/**
* @brief Change loglevel
@@ -146,6 +151,9 @@ class DebugMqtt {
private:
const char* name;
char* buf;
uint8_t bufPos = 0;
uint8_t bufSitze = 100;
static String enum_to_string(Loglevel loglevel);
@@ -156,4 +164,4 @@ class DebugMqtt {
static char topic[MQTT_BUFFER_SIZE];
};
#endif // DEBUG_MQTT_H
#endif // DEBUG_MQTT_H
+30
View File
@@ -0,0 +1,30 @@
/**
* @file outputStreamMqtt.cpp
* @author Alexander Klein (alex@kleiax.de)
* @brief
* @version 0.1
* @date 2022-02-14
*
* @copyright Copyright (c) 2022
*
*/
#include "outputBufMqtt.h"
OutputBufMqtt::OutputBufMqtt(DebugMqtt* debugMqtt)
: std::streambuf() {
this->debugMqtt = debugMqtt;
}
std::streambuf::int_type OutputBufMqtt::overflow(std::streambuf::int_type c) {
if (c != EOF) {
// c = std::toupper(static_cast<char>(c), getloc());
this->debugMqtt->addCharacter((char) c);
Serial.print((char) c);
// if (putchar(c) == EOF) {
// return EOF;
// }
}
return c;
}
+33
View File
@@ -0,0 +1,33 @@
/**
* @file outputStreamMqtt.h
* @author Alexander Klein (alex@kleiax.de)
* @brief
* @version 0.1
* @date 2022-02-14
*
* @copyright Copyright (c) 2022
*
*/
#ifndef OUTPUT_STREAM_MQTT_H
#define OUTPUT_STREAM_MQTT_H
#include <iostream>
#include <streambuf>
#include <locale>
#include <cstdio>
#include "debugMqtt.h"
class OutputBufMqtt : public std::streambuf {
public:
OutputBufMqtt(DebugMqtt* debugMqtt);
protected:
virtual std::streambuf::int_type overflow(std::streambuf::int_type c);
public:
DebugMqtt* debugMqtt;
};
#endif // OUTPUT_STREAM_MQTT_H