Skip to content

Commit

Permalink
add find weather location by postcode
Browse files Browse the repository at this point in the history
todo: add country
  • Loading branch information
TangoCash committed Jan 19, 2022
1 parent 6420018 commit 8d6903f
Show file tree
Hide file tree
Showing 8 changed files with 70 additions and 0 deletions.
2 changes: 2 additions & 0 deletions data/locale/deutsch.locale
Expand Up @@ -1844,6 +1844,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. (https://openweathermap.org/appid) Eine leere Eingabe schaltet die Wetter-Unterstützung aus
menu.hint_weather_enabled Schaltet die Wetter-Unterstützung (OpenWeather) 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.
Expand Down Expand Up @@ -2911,6 +2912,7 @@ videomenu.zappingmode_holdtilllock Standbild bis lock
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
Expand Down
2 changes: 2 additions & 0 deletions data/locale/english.locale
Expand Up @@ -1844,6 +1844,7 @@ menu.hint_volume_size Select volume indicator height
menu.hint_weather_api_key Type your OpenWeather API key. (https://openweathermap.org/appid) An empty input disables Weather support
menu.hint_weather_enabled Enable or disable Weather support (OpenWeather)
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.
Expand Down Expand Up @@ -2911,6 +2912,7 @@ videomenu.zappingmode_holdtilllock Hold screen till locked
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
Expand Down
36 changes: 36 additions & 0 deletions src/gui/weather.cpp
Expand Up @@ -177,6 +177,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();
Expand Down
1 change: 1 addition & 0 deletions src/gui/weather.h
Expand Up @@ -87,6 +87,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()
Expand Down
24 changes: 24 additions & 0 deletions src/gui/weather_setup.cpp
Expand Up @@ -63,6 +63,10 @@ int CWeatherSetup::exec(CMenuTarget *parent, const std::string &actionKey)
{
return showSelectWeatherLocation();
}
else if (actionKey == "find_location")
{
return findLocation();
}

res = showWeatherSetup();

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
1 change: 1 addition & 0 deletions src/gui/weather_setup.h
Expand Up @@ -44,6 +44,7 @@ class CWeatherSetup : public CMenuTarget, CChangeObserver

int showWeatherSetup();
int showSelectWeatherLocation();
int findLocation();
void loadLocations(std::string filename);

public:
Expand Down
2 changes: 2 additions & 0 deletions src/system/locals.h
Expand Up @@ -1868,6 +1868,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,
Expand Down Expand Up @@ -2935,6 +2936,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,
Expand Down
2 changes: 2 additions & 0 deletions src/system/locals_intern.h
Expand Up @@ -1868,6 +1868,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",
Expand Down Expand Up @@ -2935,6 +2936,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",
Expand Down

0 comments on commit 8d6903f

Please sign in to comment.