add find weather location by postcode

todo: add country

Signed-off-by: Thilo Graf <dbt@novatux.de>

include fix compile, to_string used from helpers
This commit is contained in:
TangoCash
2022-01-19 18:20:31 +01:00
committed by Thilo Graf
parent 3453645229
commit 025a27c2de
8 changed files with 73 additions and 0 deletions

View File

@@ -1785,6 +1785,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.
@@ -2884,6 +2885,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

View File

@@ -1785,6 +1785,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.
@@ -2884,6 +2885,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

View File

@@ -181,6 +181,45 @@ bool CWeather::GetWeatherDetails()
return false;
}
bool CWeather::FindCoords(int postcode, std::string country, int pc_len)
{
std::string pcode = to_string(postcode);
unsigned int number_of_zeros = pc_len - pcode.length();
pcode.insert(0, number_of_zeros, '0');
std::string data = "http://api.openweathermap.org/geo/1.0/zip?zip=" + pcode + "," + 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 = to_string(lat) + "," + to_string(lon);
return true;
}
void CWeather::show(int x, int y)
{
checkUpdate();

View File

@@ -90,6 +90,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", int pc_len = 5);
// globals
std::string getCity()

View File

@@ -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;

View File

@@ -44,6 +44,7 @@ class CWeatherSetup : public CMenuTarget, CChangeObserver
int showWeatherSetup();
int showSelectWeatherLocation();
int findLocation();
void loadLocations(std::string filename);
public:

View File

@@ -1812,6 +1812,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,
@@ -2911,6 +2912,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,

View File

@@ -1812,6 +1812,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",
@@ -2911,6 +2912,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",