diff --git a/src/modules/virtual/owmWeather/modinfo.json b/src/modules/virtual/owmWeather/modinfo.json index 520e0761..140f748e 100644 --- a/src/modules/virtual/owmWeather/modinfo.json +++ b/src/modules/virtual/owmWeather/modinfo.json @@ -1,5 +1,6 @@ { "menuSection": "virtual_elments", + "configItem": [ { "global": 0, @@ -13,7 +14,7 @@ "descr": "Температура на улице", "int": 30, "API_key": "", - "сity": "Moscow", + "city": "Moscow", "lon": "37.54", "lat": "57.74", "lang": "ru", @@ -23,13 +24,14 @@ "debug": 0 } ], + "about": { "authorName": "Serghei Crasnicov, v2.0 Mikhail Bubnov", - "authorContact": "https://t.me/Serghei63", + "authorContact": "https://t.me/Serghei63 https://t.me/Mit4bmw", "authorGit": "https://github.com/Serghei63", "specialThanks": "AVAKS", "moduleName": "owmWeather", - "moduleVersion": "2.0", + "moduleVersion": "2.1", "usedRam": { "esp32_4mb": 15, "esp8266_4mb": 15 @@ -58,14 +60,36 @@ "lon": "Долгота, при использовании координат, будет автоматически выбран ближайший город", "lat": "Широта, при использовании координат, будет автоматически выбран ближайший город", "lang": "Язык используемый в ответах OpenWetaherMap", - "debug": "1 - выводить дополнительный лог в сериал" - } + "debug":"1 - выводить дополнительный лог в сериал" + }, + "funcInfo": [ + { + "name": "sunrise", + "descr": "Функция астрологического таймера, можно задать время до/после рассвета, вернётся количество минут до сработки, когда 0 - значит время подошло или больше установленной разницы", + "params": ["количество минут до(-)/после(+) рассвета"] + }, + { + "name": "sunset", + "descr": "Функция астрологического таймера, можно задать время до/после заката, вернётся количество минут до сработки, когда 0 - значит время подошло или больше установленной разницы", + "params": ["количество минут до(-)/после(+) заката"] + } + ] }, - "defActive": false, + + "defActive": true, + "usedLibs": { "esp32_4mb": [], "esp32_4mb3f": [], "esp32s2_4mb": [], - "esp32_16mb": [] + "esp32_16mb": [], + "esp8266_4mb": [], + "esp8266_16mb": [], + "esp8266_1mb": [], + "esp8266_1mb_ota": [], + "esp8285_1mb": [], + "esp8285_1mb_ota": [], + "esp8266_2mb": [], + "esp8266_2mb_ota": [] } -} \ No newline at end of file +} diff --git a/src/modules/virtual/owmWeather/owmWeather.cpp b/src/modules/virtual/owmWeather/owmWeather.cpp index 90699baf..c754b583 100644 --- a/src/modules/virtual/owmWeather/owmWeather.cpp +++ b/src/modules/virtual/owmWeather/owmWeather.cpp @@ -22,9 +22,12 @@ private: String _lang = ""; bool _debug = false; DynamicJsonDocument Weatherdoc1; + unsigned long _sunsetTime = 0; + unsigned long _sunriseTime = 0; + int _tzone = 0; public: - owmWeather(String parameters) : Weatherdoc1(1024), IoTItem(parameters) + owmWeather(String parameters) : Weatherdoc1(1024), IoTItem(parameters) { _API_key = jsonReadStr(parameters, "API_key"); // _ID_sity = jsonReadStr(parameters, "ID_sity"); @@ -82,7 +85,8 @@ public: deserializeJson(Weatherdoc1, payload); // ret += payload; - SerialPrint("i", "Weatherdoc1", "memoryUsage: " + String(Weatherdoc1.memoryUsage())); + if (_debug) + SerialPrint("i", "Weatherdoc1", "memoryUsage: " + String(Weatherdoc1.memoryUsage())); } } else @@ -104,6 +108,11 @@ public: getWeather(); if (jsonReadStr(Weatherdoc1["main"], "temp", true) != "null") { + _tzone = Weatherdoc1["timezone"].as(); + _sunriseTime = std::atoll(jsonReadStr(Weatherdoc1["sys"], "sunrise", true).c_str()); + _sunriseTime = _sunriseTime + _tzone; + _sunsetTime = std::atoll(jsonReadStr(Weatherdoc1["sys"], "sunset", true).c_str()); + _sunsetTime = _sunsetTime + _tzone; publishNew("main", "temp"); publishNew("main", "pressure"); @@ -121,66 +130,110 @@ public: if (_param == "temp") { value.valS = jsonReadStr(Weatherdoc1["main"], "temp", true); + regEvent(value.valS, "owmWeather"); } else if (_param == "pressure") { - value.valS = jsonReadStr(Weatherdoc1["main"], "pressure", true); + // value.valS = jsonReadStr(Weatherdoc1["main"], "pressure", true); + int tval; + jsonRead(Weatherdoc1["main"], "pressure", tval, true); + regEvent(tval / 1.333, "owmWeather"); } else if (_param == "humidity") { value.valS = jsonReadStr(Weatherdoc1["main"], "humidity", true); + regEvent(value.valS, "owmWeather"); } else if (_param == "speed") { value.valS = jsonReadStr(Weatherdoc1["wind"], "speed", true); + regEvent(value.valS, "owmWeather"); } else if (_param == "deg") { value.valS = jsonReadStr(Weatherdoc1["wind"], "deg", true); + regEvent(value.valS, "owmWeather"); } else if (_param == "all") { value.valS = jsonReadStr(Weatherdoc1["clouds"], "all", true); + regEvent(value.valS, "owmWeather"); } else if (_param == "main") { value.valS = jsonReadStr(Weatherdoc1["weather"][0], "main", true); + regEvent(value.valS, "owmWeather"); } else if (_param == "description") { value.valS = jsonReadStr(Weatherdoc1["weather"][0], "description", true); + regEvent(value.valS, "owmWeather"); } else if (_param == "icon") { value.valS = jsonReadStr(Weatherdoc1["weather"][0], "icon", true); + regEvent(value.valS, "owmWeather"); } else if (_param == "sunrise") { - value.valS = getTimeDotFormatedFromUnix(std::atoll(jsonReadStr(Weatherdoc1["sys"], "sunrise", true).c_str())); + value.valS = getTimeDotFormatedFromUnix(_sunriseTime); + regEvent(value.valS, "owmWeather"); } else if (_param == "sunset") { - value.valS = getTimeDotFormatedFromUnix(std::atoll(jsonReadStr(Weatherdoc1["sys"], "sunset", true).c_str())); + value.valS = getTimeDotFormatedFromUnix(_sunsetTime); + regEvent(value.valS, "owmWeather"); } else if (_param == "sunset") { value.valS = Weatherdoc1["name"].as(); + regEvent(value.valS, "owmWeather"); } // value.isDecimal = false; - regEvent(value.valS, "owmWeather"); + // regEvent(value.valS, "owmWeather"); } } IoTValue execute(String command, std::vector ¶m) { + IoTValue value = {}; if (command == "get") { // getWeather(); doByInterval(); } + else if (command == "sunset") + { + if (param.size()) + { + if (param[0].isDecimal) + if (getSystemTime() + _tzone >= _sunsetTime + param[0].valD * 60) + value.valD = 0; + else + value.valD = (int)((_sunsetTime + param[0].valD * 60) - getSystemTime() + _tzone) / 60; + } + if (_debug) + { + SerialPrint("i", ("owmWeather"), "time: " + String(getSystemTime() + _tzone) + " sunset: " + String(_sunsetTime) + " Dt: " + String(param[0].valD) + " diff: " + String(value.valD)); + } + } + else if (command == "sunrise") + { + if (param.size()) + { + if (getSystemTime() + _tzone >= _sunriseTime + param[0].valD * 60) + value.valD = 0; + else + value.valD = (int)((_sunriseTime + param[0].valD * 60) - getSystemTime() + _tzone) / 60; + } + if (_debug) + { + SerialPrint("i", ("owmWeather"), "time: " + String(getSystemTime() + _tzone) + " sunset: " + String(_sunriseTime) + " Dt: " + String(param[0].valD) + " diff: " + String(value.valD)); + } + } - return {}; + return value; } // проверяем если пришедшее значение отличается от предыдущего регистрируем событие @@ -230,14 +283,33 @@ public: { if (Weatherdoc1[root][param].as() != tmp->value.valS) { - tmp->setValue(getTimeDotFormatedFromUnix(std::atoll(jsonReadStr(Weatherdoc1[root], param, true).c_str())), true); + if (param == "sunrise") + { + tmp->setValue(getTimeDotFormatedFromUnix(_sunriseTime), true); + } + else if (param == "sunset") + { + tmp->setValue(getTimeDotFormatedFromUnix(_sunsetTime), true); + } + else + { + tmp->setValue(Weatherdoc1[root][param].as(), true); + } } } else { if (Weatherdoc1[root][param].as() != tmp->value.valS) { - tmp->setValue(Weatherdoc1[root][param].as(), true); + if (param == "pressure") + { + int tval = Weatherdoc1[root][param].as(); + tmp->setValue(String(tval / 1.333), true); + } + else + { + tmp->setValue(Weatherdoc1[root][param].as(), true); + } } } }