diff --git a/src/modules/virtual/Math/Math.cpp b/src/modules/virtual/Math/Math.cpp index 09a4996a..7b14c396 100644 --- a/src/modules/virtual/Math/Math.cpp +++ b/src/modules/virtual/Math/Math.cpp @@ -1,18 +1,62 @@ #include "Global.h" #include "classes/IoTItem.h" +#include class IoTMath : public IoTItem { private: + + time_t convertTime(float day, float month, float year, float hour, float minute) { + // Преобразование из float в int + int d = static_cast(day); + int m = static_cast(month); + int y = static_cast(year); + int h = static_cast(hour); + int min = static_cast(minute); + + if (d < 1 || d > 31 || m < 1 || m > 12 || y < 1900 || h < 0 || h > 23 || min < 0 || min > 59) { + SerialPrint("E", F("IoTMath"), F("Invalid date or time parameters!")); + return -1; + } + + // Структура для хранения даты и времени + struct tm t; + t.tm_year = y - 1900; + t.tm_mon = m - 1; + t.tm_mday = d; + t.tm_hour = h; + t.tm_min = min; + t.tm_sec = 0; + t.tm_isdst = -1; // Пусть система сама определяет DST + + return mktime(&t); + } + public: IoTMath(String parameters) : IoTItem(parameters) {} IoTValue execute(String command, std::vector ¶m) { - if(command == "map" & param.size() == 5) { + if(command == "map" && param.size() == 5) { IoTValue valTmp; valTmp.isDecimal = true; valTmp.valD = map(param[0].valD, param[1].valD, param[2].valD, param[3].valD, param[4].valD); + //SerialPrint("i", F("IoTMath"), F("Mapping value done.")); + return valTmp; + } else if(command == "convertTime" && param.size() == 5) { + time_t unixTime = convertTime(param[0].valD, param[1].valD, param[2].valD, param[3].valD, param[4].valD); + + if (unixTime == -1) { + SerialPrint("E", F("IoTMath"), F("Failed to convert time.")); + return {}; + } + + IoTValue valTmp; + valTmp.isDecimal = true; + valTmp.valD = unixTime; + //SerialPrint("i", F("IoTMath"), F("Time conversion done.")); return valTmp; } + + SerialPrint("E", F("IoTMath"), F("Unknown command or wrong parameters.")); return {}; } }; diff --git a/src/modules/virtual/Math/modinfo.json b/src/modules/virtual/Math/modinfo.json index c9d1dfe4..6265e85b 100644 --- a/src/modules/virtual/Math/modinfo.json +++ b/src/modules/virtual/Math/modinfo.json @@ -35,10 +35,17 @@ "params": [ "Значение1", "Значение2", "Значение3", "Значение4" ] + }, + { + "name": "convertTime", + "descr": "Перевести время из формата d-m-Y H:i:s например, 13-08-2023 16:24:00 в юникс-время ", + "params": [ + "tm.convertTime(13, 08, 2023, 16, 24); - передаем пять целых чисел. секунды подставятся в ноль" + ] } ] }, - "defActive": false, + "defActive": true, "usedLibs": { "esp32_4mb": [], "esp32_4mb3f": [],