AstroTimer owmWeather

This commit is contained in:
Mit4el
2023-10-17 21:44:09 +03:00
parent 59d7821040
commit 35b184c2a3
2 changed files with 113 additions and 17 deletions

View File

@@ -1,5 +1,6 @@
{ {
"menuSection": "virtual_elments", "menuSection": "virtual_elments",
"configItem": [ "configItem": [
{ {
"global": 0, "global": 0,
@@ -13,7 +14,7 @@
"descr": "Температура на улице", "descr": "Температура на улице",
"int": 30, "int": 30,
"API_key": "", "API_key": "",
"сity": "Moscow", "city": "Moscow",
"lon": "37.54", "lon": "37.54",
"lat": "57.74", "lat": "57.74",
"lang": "ru", "lang": "ru",
@@ -23,13 +24,14 @@
"debug": 0 "debug": 0
} }
], ],
"about": { "about": {
"authorName": "Serghei Crasnicov, v2.0 Mikhail Bubnov", "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", "authorGit": "https://github.com/Serghei63",
"specialThanks": "AVAKS", "specialThanks": "AVAKS",
"moduleName": "owmWeather", "moduleName": "owmWeather",
"moduleVersion": "2.0", "moduleVersion": "2.1",
"usedRam": { "usedRam": {
"esp32_4mb": 15, "esp32_4mb": 15,
"esp8266_4mb": 15 "esp8266_4mb": 15
@@ -58,14 +60,36 @@
"lon": "Долгота, при использовании координат, будет автоматически выбран ближайший город", "lon": "Долгота, при использовании координат, будет автоматически выбран ближайший город",
"lat": "Широта, при использовании координат, будет автоматически выбран ближайший город", "lat": "Широта, при использовании координат, будет автоматически выбран ближайший город",
"lang": "Язык используемый в ответах OpenWetaherMap", "lang": "Язык используемый в ответах OpenWetaherMap",
"debug": "1 - выводить дополнительный лог в сериал" "debug":"1 - выводить дополнительный лог в сериал"
} },
"funcInfo": [
{
"name": "sunrise",
"descr": "Функция астрологического таймера, можно задать время до/после рассвета, вернётся количество минут до сработки, когда 0 - значит время подошло или больше установленной разницы",
"params": ["количество минут до(-)/после(+) рассвета"]
},
{
"name": "sunset",
"descr": "Функция астрологического таймера, можно задать время до/после заката, вернётся количество минут до сработки, когда 0 - значит время подошло или больше установленной разницы",
"params": ["количество минут до(-)/после(+) заката"]
}
]
}, },
"defActive": false,
"defActive": true,
"usedLibs": { "usedLibs": {
"esp32_4mb": [], "esp32_4mb": [],
"esp32_4mb3f": [], "esp32_4mb3f": [],
"esp32s2_4mb": [], "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": []
} }
} }

View File

@@ -22,9 +22,12 @@ private:
String _lang = ""; String _lang = "";
bool _debug = false; bool _debug = false;
DynamicJsonDocument Weatherdoc1; DynamicJsonDocument Weatherdoc1;
unsigned long _sunsetTime = 0;
unsigned long _sunriseTime = 0;
int _tzone = 0;
public: public:
owmWeather(String parameters) : Weatherdoc1(1024), IoTItem(parameters) owmWeather(String parameters) : Weatherdoc1(1024), IoTItem(parameters)
{ {
_API_key = jsonReadStr(parameters, "API_key"); _API_key = jsonReadStr(parameters, "API_key");
// _ID_sity = jsonReadStr(parameters, "ID_sity"); // _ID_sity = jsonReadStr(parameters, "ID_sity");
@@ -82,7 +85,8 @@ public:
deserializeJson(Weatherdoc1, payload); deserializeJson(Weatherdoc1, payload);
// ret += payload; // ret += payload;
SerialPrint("i", "Weatherdoc1", "memoryUsage: " + String(Weatherdoc1.memoryUsage())); if (_debug)
SerialPrint("i", "Weatherdoc1", "memoryUsage: " + String(Weatherdoc1.memoryUsage()));
} }
} }
else else
@@ -104,6 +108,11 @@ public:
getWeather(); getWeather();
if (jsonReadStr(Weatherdoc1["main"], "temp", true) != "null") if (jsonReadStr(Weatherdoc1["main"], "temp", true) != "null")
{ {
_tzone = Weatherdoc1["timezone"].as<int>();
_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", "temp");
publishNew("main", "pressure"); publishNew("main", "pressure");
@@ -121,66 +130,110 @@ public:
if (_param == "temp") if (_param == "temp")
{ {
value.valS = jsonReadStr(Weatherdoc1["main"], "temp", true); value.valS = jsonReadStr(Weatherdoc1["main"], "temp", true);
regEvent(value.valS, "owmWeather");
} }
else if (_param == "pressure") 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") else if (_param == "humidity")
{ {
value.valS = jsonReadStr(Weatherdoc1["main"], "humidity", true); value.valS = jsonReadStr(Weatherdoc1["main"], "humidity", true);
regEvent(value.valS, "owmWeather");
} }
else if (_param == "speed") else if (_param == "speed")
{ {
value.valS = jsonReadStr(Weatherdoc1["wind"], "speed", true); value.valS = jsonReadStr(Weatherdoc1["wind"], "speed", true);
regEvent(value.valS, "owmWeather");
} }
else if (_param == "deg") else if (_param == "deg")
{ {
value.valS = jsonReadStr(Weatherdoc1["wind"], "deg", true); value.valS = jsonReadStr(Weatherdoc1["wind"], "deg", true);
regEvent(value.valS, "owmWeather");
} }
else if (_param == "all") else if (_param == "all")
{ {
value.valS = jsonReadStr(Weatherdoc1["clouds"], "all", true); value.valS = jsonReadStr(Weatherdoc1["clouds"], "all", true);
regEvent(value.valS, "owmWeather");
} }
else if (_param == "main") else if (_param == "main")
{ {
value.valS = jsonReadStr(Weatherdoc1["weather"][0], "main", true); value.valS = jsonReadStr(Weatherdoc1["weather"][0], "main", true);
regEvent(value.valS, "owmWeather");
} }
else if (_param == "description") else if (_param == "description")
{ {
value.valS = jsonReadStr(Weatherdoc1["weather"][0], "description", true); value.valS = jsonReadStr(Weatherdoc1["weather"][0], "description", true);
regEvent(value.valS, "owmWeather");
} }
else if (_param == "icon") else if (_param == "icon")
{ {
value.valS = jsonReadStr(Weatherdoc1["weather"][0], "icon", true); value.valS = jsonReadStr(Weatherdoc1["weather"][0], "icon", true);
regEvent(value.valS, "owmWeather");
} }
else if (_param == "sunrise") 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") 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") else if (_param == "sunset")
{ {
value.valS = Weatherdoc1["name"].as<String>(); value.valS = Weatherdoc1["name"].as<String>();
regEvent(value.valS, "owmWeather");
} }
// value.isDecimal = false; // value.isDecimal = false;
regEvent(value.valS, "owmWeather"); // regEvent(value.valS, "owmWeather");
} }
} }
IoTValue execute(String command, std::vector<IoTValue> &param) IoTValue execute(String command, std::vector<IoTValue> &param)
{ {
IoTValue value = {};
if (command == "get") if (command == "get")
{ {
// getWeather(); // getWeather();
doByInterval(); 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<String>() != tmp->value.valS) if (Weatherdoc1[root][param].as<String>() != 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<String>(), true);
}
} }
} }
else else
{ {
if (Weatherdoc1[root][param].as<String>() != tmp->value.valS) if (Weatherdoc1[root][param].as<String>() != tmp->value.valS)
{ {
tmp->setValue(Weatherdoc1[root][param].as<String>(), true); if (param == "pressure")
{
int tval = Weatherdoc1[root][param].as<int>();
tmp->setValue(String(tval / 1.333), true);
}
else
{
tmp->setValue(Weatherdoc1[root][param].as<String>(), true);
}
} }
} }
} }