From ddaa31a5390c4ae83488706c7708c06a1e85f094 Mon Sep 17 00:00:00 2001 From: TangoCash Date: Thu, 20 Jan 2022 21:24:37 +0100 Subject: [PATCH] add find weather location by postcode todo: add country Origin commit data ------------------ Branch: ni/coolstream Commit: https://github.com/neutrino-images/ni-neutrino/commit/b2b2d605dce9bb9774b41f6cdfaeca5d3ab1b40a Author: TangoCash Date: 2022-01-20 (Thu, 20 Jan 2022) ------------------ This commit was generated by Migit --- data/locale/deutsch.locale | 2 ++ data/locale/english.locale | 2 ++ src/gui/weather.cpp | 36 ++++++++++++++++++++++++++++++++++++ src/gui/weather.h | 1 + src/gui/weather_setup.cpp | 24 ++++++++++++++++++++++++ src/gui/weather_setup.h | 1 + src/system/locals.h | 2 ++ src/system/locals_intern.h | 2 ++ 8 files changed, 70 insertions(+) diff --git a/data/locale/deutsch.locale b/data/locale/deutsch.locale index d26d0cab8..dda604a08 100644 --- a/data/locale/deutsch.locale +++ b/data/locale/deutsch.locale @@ -1904,6 +1904,7 @@ menu.hint_volume_size Wählen Sie die Höhe der Lautstärkeanzeige menu.hint_weather_api_key Geben Sie den OpenWeather API Schlüssel ein. Eine leere Eingabe schaltet die Wetter-Unterstützung aus menu.hint_weather_enabled Schaltet die Wetter-Unterstützung (openweathermap.org) ein bzw. aus menu.hint_weather_location Wählen Sie eine Stadt in ihrer Nähe zur Anzeige der Wetterdaten aus +menu.hint_weather_location_postcode Geben Sie ihre Postleitzahl zur Anzeige der Wetterdaten an menu.hint_webradio_setup Hier konfigurierte WebRadio-Kanäle finden Sie in der Kanalverwaltung. menu.hint_webradio_xml_auto Lädt automatisch alle WebRadio-Dateien aus %s/ und %s/ menu.hint_webtv_setup Hier konfigurierte WebTV-Kanäle finden Sie in der Kanalverwaltung. @@ -3038,6 +3039,7 @@ videomenu.zappingmode_hold Standbild weather.api_key Wetter API Schlüssel (OpenWeather) weather.enabled Wetter-Unterstützung weather.location Wetter-Standort +weather.location_postcode Wetter-Standort per Postleitzahl webchannels.xml.add Hinzufügen webchannels.xml.del Entfernen webchannels.xml.enter Eintragen diff --git a/data/locale/english.locale b/data/locale/english.locale index cb6e81a9a..16f5e3f7d 100644 --- a/data/locale/english.locale +++ b/data/locale/english.locale @@ -1904,6 +1904,7 @@ menu.hint_volume_size Select volume indicator height menu.hint_weather_api_key Type your OpenWeather API key. An empty input disables Weather support menu.hint_weather_enabled Enable or disable Weather support (openweathermap.org) menu.hint_weather_location Select your weather location +menu.hint_weather_location_postcode Select your weather location by postcode menu.hint_webradio_setup WebRadio channels configured here will be available in the standard channel lists. menu.hint_webradio_xml_auto Auto-load all existing WebRadio files from %s/ and %s/ menu.hint_webtv_setup WebTV channels configured here will be available in the standard channel lists. @@ -3038,6 +3039,7 @@ videomenu.zappingmode_hold Hold screen weather.api_key Weather API key (OpenWeather) weather.enabled Weather support weather.location Weather location +weather.location_postcode Weather location by postcode webchannels.xml.add Add webchannels.xml.del Remove webchannels.xml.enter Enter diff --git a/src/gui/weather.cpp b/src/gui/weather.cpp index 9a4617ee5..bd93a617f 100644 --- a/src/gui/weather.cpp +++ b/src/gui/weather.cpp @@ -182,6 +182,42 @@ bool CWeather::GetWeatherDetails() return false; } +bool CWeather::FindCoords(int postcode, std::string country) +{ + std::string data = "http://api.openweathermap.org/geo/1.0/zip?zip=" + std::to_string(postcode) + ","+country+"&appid=" + key; + JSONCPP_STRING answer; + JSONCPP_STRING formattedErrors; + Json::CharReaderBuilder builder; + Json::CharReader *reader = builder.newCharReader(); + Json::Value DataValues; + answer.clear(); + + if (!getUrl(data, answer)) + { + delete reader; + return false; + } + + bool parsedSuccess = reader->parse(answer.c_str(), answer.c_str() + answer.size(), &DataValues, &formattedErrors); + delete reader; + + if (!parsedSuccess) + { + printf("Failed to parse JSON\n"); + printf("%s\n", formattedErrors.c_str()); + return false; + } + + if (DataValues["message"].asString() == "not found") + return false; + + float lat = DataValues["lat"].asFloat(); + float lon = DataValues["lon"].asFloat(); + g_settings.weather_city = DataValues["name"].asString(); + g_settings.weather_location = std::to_string(lat) + "," + std::to_string(lon); + return true; +} + void CWeather::show(int x, int y) { checkUpdate(); diff --git a/src/gui/weather.h b/src/gui/weather.h index a11c3a97a..c060fb54e 100644 --- a/src/gui/weather.h +++ b/src/gui/weather.h @@ -91,6 +91,7 @@ class CWeather ~CWeather(); bool checkUpdate(bool forceUpdate = false); void setCoords(std::string new_coords, std::string new_city = "Unknown"); + bool FindCoords(int postcode, std::string country = "DE"); // globals std::string getCity() diff --git a/src/gui/weather_setup.cpp b/src/gui/weather_setup.cpp index b8b83650d..25104f1fa 100644 --- a/src/gui/weather_setup.cpp +++ b/src/gui/weather_setup.cpp @@ -63,6 +63,10 @@ int CWeatherSetup::exec(CMenuTarget *parent, const std::string &actionKey) { return showSelectWeatherLocation(); } + else if (actionKey == "find_location") + { + return findLocation(); + } res = showWeatherSetup(); @@ -90,6 +94,10 @@ int CWeatherSetup::showWeatherSetup() mf_wl->setHint(NEUTRINO_ICON_HINT_SETTINGS, LOCALE_MENU_HINT_WEATHER_LOCATION); ms_oservices->addItem(mf_wl); + CMenuForwarder *mf_zip = new CMenuForwarder(LOCALE_WEATHER_LOCATION_POSTCODE, g_settings.weather_enabled, NULL, this, "find_location"); + mf_zip->setHint(NEUTRINO_ICON_HINT_SETTINGS, LOCALE_MENU_HINT_WEATHER_LOCATION_POSTCODE); + ms_oservices->addItem(mf_zip); + int res = ms_oservices->exec(NULL, ""); selected = ms_oservices->getSelected(); delete ms_oservices; @@ -146,6 +154,22 @@ int CWeatherSetup::showSelectWeatherLocation() return res; } +int CWeatherSetup::findLocation() +{ + int ret = menu_return::RETURN_REPAINT; + int postcode = 10178; + CIntInput zipcode(LOCALE_WEATHER_LOCATION_POSTCODE, &postcode, (unsigned int)5); + ret = zipcode.exec(NULL, ""); + zipcode.hide(); + + if (CWeather::getInstance()->FindCoords(postcode)) + { + CWeather::getInstance()->setCoords(g_settings.weather_location, g_settings.weather_city); + } + + return ret; +} + bool CWeatherSetup::changeNotify(const neutrino_locale_t OptionName, void */*data*/) { int ret = menu_return::RETURN_NONE; diff --git a/src/gui/weather_setup.h b/src/gui/weather_setup.h index 3fe0bb59b..ed614b799 100644 --- a/src/gui/weather_setup.h +++ b/src/gui/weather_setup.h @@ -44,6 +44,7 @@ class CWeatherSetup : public CMenuTarget, CChangeObserver int showWeatherSetup(); int showSelectWeatherLocation(); + int findLocation(); void loadLocations(std::string filename); public: diff --git a/src/system/locals.h b/src/system/locals.h index 385bc2252..559e5d8d8 100644 --- a/src/system/locals.h +++ b/src/system/locals.h @@ -1931,6 +1931,7 @@ typedef enum LOCALE_MENU_HINT_WEATHER_API_KEY, LOCALE_MENU_HINT_WEATHER_ENABLED, LOCALE_MENU_HINT_WEATHER_LOCATION, + LOCALE_MENU_HINT_WEATHER_LOCATION_POSTCODE, LOCALE_MENU_HINT_WEBRADIO_SETUP, LOCALE_MENU_HINT_WEBRADIO_XML_AUTO, LOCALE_MENU_HINT_WEBTV_SETUP, @@ -3065,6 +3066,7 @@ typedef enum LOCALE_WEATHER_API_KEY, LOCALE_WEATHER_ENABLED, LOCALE_WEATHER_LOCATION, + LOCALE_WEATHER_LOCATION_POSTCODE, LOCALE_WEBCHANNELS_XML_ADD, LOCALE_WEBCHANNELS_XML_DEL, LOCALE_WEBCHANNELS_XML_ENTER, diff --git a/src/system/locals_intern.h b/src/system/locals_intern.h index 393bb2486..4a7df7739 100644 --- a/src/system/locals_intern.h +++ b/src/system/locals_intern.h @@ -1931,6 +1931,7 @@ const char * locale_real_names[] = "menu.hint_weather_api_key", "menu.hint_weather_enabled", "menu.hint_weather_location", + "menu.hint_weather_location_postcode", "menu.hint_webradio_setup", "menu.hint_webradio_xml_auto", "menu.hint_webtv_setup", @@ -3065,6 +3066,7 @@ const char * locale_real_names[] = "weather.api_key", "weather.enabled", "weather.location", + "weather.location_postcode", "webchannels.xml.add", "webchannels.xml.del", "webchannels.xml.enter",