From 821aeb5ad2a9b2809c5606b613b97b135e1f6e67 Mon Sep 17 00:00:00 2001 From: avaksru Date: Mon, 26 Dec 2022 13:01:08 +0300 Subject: [PATCH] =?UTF-8?q?=D0=9C=D0=BE=D0=B4=D1=83=D0=BB=D1=8C=20=D0=9F?= =?UTF-8?q?=D0=BE=D0=B3=D0=BE=D0=B4=D0=B0=20(Weather)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- myProfile.json | 8 ++ src/modules/virtual/Weather/Weather.cpp | 153 +++++++++++++++++++++++ src/modules/virtual/Weather/modinfo.json | 53 ++++++++ 3 files changed, 214 insertions(+) create mode 100644 src/modules/virtual/Weather/Weather.cpp create mode 100644 src/modules/virtual/Weather/modinfo.json diff --git a/myProfile.json b/myProfile.json index a7c149a4..8d86cea2 100644 --- a/myProfile.json +++ b/myProfile.json @@ -53,6 +53,10 @@ { "path": "src/modules/virtual/VButton", "active": true + }, + { + "path": "src/modules/virtual/Weather", + "active": false } ], "Сенсоры": [ @@ -182,6 +186,10 @@ "path": "src/modules/exec/IoTServo", "active": true }, + { + "path": "src/modules/exec/Mcp23008", + "active": false + }, { "path": "src/modules/exec/Mcp23017", "active": true diff --git a/src/modules/virtual/Weather/Weather.cpp b/src/modules/virtual/Weather/Weather.cpp new file mode 100644 index 00000000..2ff85df4 --- /dev/null +++ b/src/modules/virtual/Weather/Weather.cpp @@ -0,0 +1,153 @@ +#include "Global.h" +#include "classes/IoTItem.h" +#include + +long prevWeatherMillis = millis() - 60001; +StaticJsonDocument Weatherdoc; + +extern IoTGpio IoTgpio; +class Weather : public IoTItem +{ +private: + String _location; + String _param; + long interval; + +public: + Weather(String parameters) : IoTItem(parameters) + { + _location = jsonReadStr(parameters, "location"); + _param = jsonReadStr(parameters, "param"); + jsonRead(parameters, F("int"), interval); + interval = interval * 1000 * 60 * 60; // интервал проверки погоды в часах + } + + void getWeather() + { + String ret; + + if (WiFi.status() == WL_CONNECTED) + { + // char c; + String payload; + WiFiClient client; + HTTPClient http; + http.begin(client, "http://live-control.com/iotm/weather.php"); + http.addHeader("Content-Type", "application/x-www-form-urlencoded"); + String httpRequestData = "loc=" + _location; + int httpResponseCode = http.POST(httpRequestData); + if (httpResponseCode > 0) + { + ret = httpResponseCode; + + if (httpResponseCode == HTTP_CODE_OK) + { + payload = http.getString(); + + deserializeJson(Weatherdoc, payload); + // ret += payload; + } + } + else + { + ret = http.errorToString(httpResponseCode).c_str(); + } + SerialPrint("<-", F("getWeather"), httpRequestData); + SerialPrint("->", F("getWeather"), "server: " + ret); + + http.end(); + } + } + + void doByInterval() + { + + if (prevWeatherMillis + 60000 < millis()) + { + getWeather(); + prevWeatherMillis = millis(); + } + if (jsonReadStr(Weatherdoc["current_condition"][0], "temp_C", true) != "null") + { + if (_param == "temp_C") + { + value.valS = jsonReadStr(Weatherdoc["current_condition"][0], "temp_C", true); + } + if (_param == "avgtempC") + { + value.valS = jsonReadStr(Weatherdoc["weather"][0], "avgtempC", true); + } + if (_param == "humidity") + { + value.valS = jsonReadStr(Weatherdoc["current_condition"][0], "humidity", true); + } + if (_param == "weatherCode") + { + value.valS = jsonReadStr(Weatherdoc["current_condition"][0], "weatherCode", true); + } + if (_param == "sunrise") + { + + value.valS = jsonReadStr(Weatherdoc["weather"][0]["astronomy"][0], "sunrise", true); + } + if (_param == "sunset") + { + value.valS = jsonReadStr(Weatherdoc["weather"][0]["astronomy"][0], "sunset", true); + } + + if (_param == "rangetempC") + { + value.valS = jsonReadStr(Weatherdoc["weather"][0], "mintempC", true) + "..." + jsonReadStr(Weatherdoc["weather"][0], "maxtempC", true); + } + + // погода на завтра + if (_param == "temp_C_tomorrow") + { + value.valS = jsonReadStr(Weatherdoc["weather"][1], "avgtempC", true); + } + if (_param == "rangetempC_tomorrow") + { + value.valS = jsonReadStr(Weatherdoc["weather"][1], "mintempC", true) + "..." + jsonReadStr(Weatherdoc["weather"][1], "maxtempC", true); + } + + regEvent(value.valS, "Weather"); + } + } + void loop() + { + if (enableDoByInt) + { + currentMillis = millis(); + difference = currentMillis - prevMillis; + if (difference >= interval) + { + prevMillis = millis(); + this->doByInterval(); + } + } + } + IoTValue execute(String command, std::vector ¶m) + { + if (command == "get") + { + getWeather(); + doByInterval(); + } + + return {}; + } + + ~Weather(){}; +}; + +void *getAPI_Weather(String subtype, String param) +{ + if (subtype == F("Weather")) + { + return new Weather(param); + } + else + { + return nullptr; + } +} diff --git a/src/modules/virtual/Weather/modinfo.json b/src/modules/virtual/Weather/modinfo.json new file mode 100644 index 00000000..0530ebdf --- /dev/null +++ b/src/modules/virtual/Weather/modinfo.json @@ -0,0 +1,53 @@ +{ + "menuSection": "Виртуальные элементы", + + "configItem": [ + { + "global": 0, + "name": "Погода", + "type": "Reading", + "subtype": "Weather", + "id": "Weather", + "needSave": 0, + "widget": "anydataDef", + "page": "Погода", + "descr": "", + "int": 3, + "location": "Moscow", + "param": "temp_C", + "round": 0, + "val": "..." + } + ], + + "about": { + "authorName": "AVAKS", + "authorContact": "https://t.me/@avaks_dev", + "authorGit": "https://github.com/avaksru", + "specialThanks": "", + "moduleName": "Weather", + "moduleVersion": "1", + "usedRam": { + "esp32_4mb": 15, + "esp8266_4mb": 15 + }, + "title": "Погода", + "moduleDesc": "Получение погоды из интернет", + "propInfo": { + "location": "Город.", + "param": "temp_C - температура, humidity - влажность, weatherCode - погодный код, sunrise - рассвет, sunset - закат, rangetempC - (-3...-10 C), temp_C_tomorrow - температура завтра, rangetempC_tomorrow - (-3...-10 C) на завтра", + "int": "Интервал запроса погоды в часах" + } + }, + + "defActive": false, + + "usedLibs": { + "esp32_4mb": [], + "esp8266_4mb": [], + "esp8266_1mb": [], + "esp8266_1mb_ota": [], + "esp8285_1mb": [], + "esp8285_1mb_ota": [] + } +}