From 9cba7f1b9120b0d72d8cd933447e39d867782fbc Mon Sep 17 00:00:00 2001 From: DanielR92 Date: Thu, 11 Jul 2024 18:30:52 +0200 Subject: [PATCH 1/2] Create mqttHelper --- src/utils/mqttHelper.cpp | 101 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 101 insertions(+) create mode 100644 src/utils/mqttHelper.cpp diff --git a/src/utils/mqttHelper.cpp b/src/utils/mqttHelper.cpp new file mode 100644 index 00000000..f92abd7d --- /dev/null +++ b/src/utils/mqttHelper.cpp @@ -0,0 +1,101 @@ +#include "mqttHelper.h" + +namespace mqttHelper { + + /** checkBoolProperty + * TODO: modify header + * @param tmpTopic + * @param subTopic + * @param payload + * @param len + * @param cfg + * @param log + * @returns bool + */ + + bool checkCharProperty(const char *tmpTopic, const char *subTopic, const uint8_t *payload, size_t len, char *cfg, int cfgSize, DynamicJsonHandler *log) { + // Check if tmpTopic starts with subTopic + if (strncmp(tmpTopic, subTopic, strlen(subTopic)) == 0) { + // Convert payload to a String instance + String sPayload = String((const char*)payload).substring(0, len); + + // Copy the String into cfg and ensure cfg is null-terminated + strncpy(cfg, sPayload.c_str(), cfgSize - 1); + cfg[cfgSize - 1] = '\0'; // Ensure cfg is null-terminated + + // Add the property to the log + log->addProperty("v", cfg); + + return true; // Successfully processed the property + } + + return false; // Did not process the property + } + + bool checkBoolProperty(const char *tmpTopic, const char *subTopic, const uint8_t *payload, size_t len, bool *cfg, DynamicJsonHandler *log) { + if (strncmp(tmpTopic, subTopic, strlen(subTopic)) == 0) { + String sPayload = String((const char*)payload).substring(0, len); + + if (sPayload == "1" || sPayload == "true") { + *cfg = true; + log->addProperty("v", *cfg); + } else if (sPayload == "0" || sPayload == "false") { + *cfg = false; + log->addProperty("v", *cfg); + } else { + DBGPRINTLN(F("Payload is not a valid boolean value")); + log->addProperty("v", "payload error"); + } + return true; + } + return false; + } + +} +// --- END Namespace --- + + /** checkProperty + * + */ +/* + // final function what "all" combines other function into one + template + bool checkProperty(const char *tmpTopic, const char *subTopic, const uint8_t *payload, size_t len, T *cfg, DynamicJsonHandler *log) { + if (strncmp(tmpTopic, subTopic, strlen(subTopic)) == 0) { + String sPayload = String((const char*)payload).substring(0, len); + + // Überprüfung und Zuweisung je nach Typ T + if constexpr (std::is_integral_v) { + T value; + sscanf(sPayload.c_str(), "%lld", &value); // Beachte "%lld" für int64_t und uint64_t + if (value <= std::numeric_limits::max() && value >= std::numeric_limits::min()) { + *cfg = value; + } else { + log->addProperty("v", F("Fehler: Der Wert passt nicht in den Ziel-Typ T")); + return false; + } + } else if constexpr (std::is_same_v) { + *cfg = (sPayload == "1" || sPayload == "true"); + if (*cfg) { + log->addProperty("v", "true"); + } else { + log->addProperty("v", "false"); + } + } else if constexpr (std::is_same_v || std::is_same_v) { + strncpy(*cfg, sPayload.c_str(), cfgSize - 1); + (*cfg)[cfgSize - 1] = '\0'; + } else { + // Handle andere Datentypen + return false; + } + + // Füge die Eigenschaft zum Log hinzu + log->addProperty("v", sPayload.c_str()); + + return true; + } + + return false; + } +*/ + From b18dafc6b3a7ab142c0e5424fa0d186b4f8d0450 Mon Sep 17 00:00:00 2001 From: DanielR92 Date: Thu, 11 Jul 2024 18:33:14 +0200 Subject: [PATCH 2/2] add header file --- src/utils/mqttHelper.h | 46 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 src/utils/mqttHelper.h diff --git a/src/utils/mqttHelper.h b/src/utils/mqttHelper.h new file mode 100644 index 00000000..0e86ba28 --- /dev/null +++ b/src/utils/mqttHelper.h @@ -0,0 +1,46 @@ +#ifndef __MQTT_HELPER_H__ +#define __MQTT_HELPER_H__ + +#include +#include +#include +#include +#include +#include "DynamicJsonHandler.h" +#include "helper.h" +#include + +namespace mqttHelper { + template + bool checkIntegerProperty(const char *tmpTopic, const char *subTopic, const uint8_t *payload, size_t len, T *cfg, DynamicJsonHandler *log) { + if (strncmp(tmpTopic, subTopic, strlen(subTopic)) == 0) { + // Konvertiere payload in einen String + String sPayload = String((const char*)payload).substring(0, len); + + // Konvertiere den String in den gewünschten Integer-Typ T + T value; + sscanf(sPayload.c_str(), "%d", &value); // Beispielhaft für int, anpassen je nach T + + // Überprüfung, ob der Wert in den Ziel-Typ T passt + if (sPayload.toInt() <= std::numeric_limits::max() && sPayload.toInt() >= std::numeric_limits::min()) { + // Weise den Wert cfg zu + *cfg = value; + + // Füge die Eigenschaft zum Log hinzu + log->addProperty("v", ah::round1(*cfg)); + + return true; + } else { + // Handle den Fall, wenn der Wert außerhalb des gültigen Bereichs liegt + log->addProperty("v", F("Fehler: Der Wert passt nicht in den Ziel-Typ T")); + return false; + } + } + return false; + } + + bool checkCharProperty(const char *tmpTopic, const char *subTopic, const uint8_t *payload, size_t len, char *cfg, int cfgSize, DynamicJsonHandler *log); + bool checkBoolProperty(const char *tmpTopic, const char *subTopic, const uint8_t *payload, size_t len, bool *cfg, DynamicJsonHandler *log); +} + +#endif /*__MQTT_HELPER_H__*/ \ No newline at end of file