- weather: naming the member varables as the keywords from json request

So it's easier to add some new stuff as i think.

Signed-off-by: Thilo Graf <dbt@novatux.de>
This commit is contained in:
svenhoefer
2019-03-22 14:25:15 +01:00
committed by Thilo Graf
parent 68ff0bb553
commit e1ed034a9f
3 changed files with 50 additions and 39 deletions

View File

@@ -1066,16 +1066,16 @@ void CLCD4l::ParseInfo(uint64_t parseID, bool newID, bool firstRun)
int forecast = 1; // days for forecast int forecast = 1; // days for forecast
std::string wtemp = CWeather::getInstance()->getActTemp(); std::string wtemp = CWeather::getInstance()->getCurrentTemperature();
for (int i = 0; i < 1 + forecast; i++) for (int i = 0; i < 1 + forecast; i++)
wtemp += "\n" + CWeather::getInstance()->getForecastTemp(i); wtemp += "\n" + CWeather::getInstance()->getForecastTemperatureMax(i);
if (m_wtemp.compare(wtemp)) if (m_wtemp.compare(wtemp))
{ {
WriteFile(WEATHER_TEMP, wtemp); WriteFile(WEATHER_TEMP, wtemp);
m_wtemp = wtemp; m_wtemp = wtemp;
} }
std::string wicon = CWeather::getInstance()->getActIcon(); std::string wicon = CWeather::getInstance()->getCurrentIcon();
for (int i = 0; i < 1 + forecast; i++) for (int i = 0; i < 1 + forecast; i++)
wicon += "\n" + CWeather::getInstance()->getForecastIcon(i); wicon += "\n" + CWeather::getInstance()->getForecastIcon(i);
if (m_wicon.compare(wicon)) if (m_wicon.compare(wicon))

View File

@@ -52,14 +52,13 @@ CWeather *CWeather::getInstance()
CWeather::CWeather() CWeather::CWeather()
{ {
act_temp = 0;
act_wicon = "unknown.png";
key = g_settings.weather_api_key; key = g_settings.weather_api_key;
form = NULL;
v_forecast.clear(); v_forecast.clear();
last_time = 0; last_time = 0;
coords = ""; coords = "";
city = ""; city = "";
form = NULL;
} }
CWeather::~CWeather() CWeather::~CWeather()
@@ -129,32 +128,32 @@ bool CWeather::GetWeatherDetails()
if (found > 0) if (found > 0)
{ {
timezone = DataValues["timezone"].asString(); timezone = DataValues["timezone"].asString();
act_temp = DataValues["currently"].get("temperature", "").asFloat(); current.temperature = DataValues["currently"].get("temperature", "").asFloat();
act_wicon = DataValues["currently"].get("icon", "").asString(); current.icon = DataValues["currently"].get("icon", "").asString();
if (act_wicon.empty()) if (current.icon.empty())
act_wicon = "unknown.png"; current.icon = "unknown.png";
else else
act_wicon = act_wicon + ".png"; current.icon = current.icon + ".png";
printf("[CWeather]: temp in %s (%s): %.1f - %s\n", city.c_str(), timezone.c_str(), act_temp, act_wicon.c_str()); printf("[CWeather]: temp in %s (%s): %.1f - %s\n", city.c_str(), timezone.c_str(), current.temperature, current.icon.c_str());
forecast_data weatherinfo; forecast_data daily_data;
Json::Value elements = DataValues["daily"]["data"]; Json::Value elements = DataValues["daily"]["data"];
for (unsigned int i = 0; i < elements.size(); i++) for (unsigned int i = 0; i < elements.size(); i++)
{ {
weatherinfo.timestamp = elements[i].get("time", 0).asDouble(); daily_data.timestamp = elements[i].get("time", 0).asDouble();
weatherinfo.wicon = elements[i].get("icon", "").asString(); daily_data.icon = elements[i].get("icon", "").asString();
if (weatherinfo.wicon.empty()) if (daily_data.icon.empty())
weatherinfo.wicon = "unknown.png"; daily_data.icon = "unknown.png";
else else
weatherinfo.wicon = weatherinfo.wicon + ".png"; daily_data.icon = daily_data.icon + ".png";
weatherinfo.min_temp = elements[i].get("temperatureMin", "").asFloat(); daily_data.temperatureMin = elements[i].get("temperatureMin", "").asFloat();
weatherinfo.max_temp = elements[i].get("temperatureMax", "").asFloat(); daily_data.temperatureMax = elements[i].get("temperatureMax", "").asFloat();
struct tm *timeinfo; struct tm *timeinfo;
timeinfo = localtime(&weatherinfo.timestamp); timeinfo = localtime(&daily_data.timestamp);
printf("[CWeather]: temp %d.%d.%d: min %.1f - max %.1f -> %s\n", timeinfo->tm_mday, timeinfo->tm_mon, timeinfo->tm_year + 1900, weatherinfo.min_temp, weatherinfo.max_temp, weatherinfo.wicon.c_str()); printf("[CWeather]: temp %d.%d.%d: min %.1f - max %.1f -> %s\n", timeinfo->tm_mday, timeinfo->tm_mon, timeinfo->tm_year + 1900, daily_data.temperatureMin, daily_data.temperatureMax, daily_data.icon.c_str());
v_forecast.push_back(weatherinfo); v_forecast.push_back(daily_data);
} }
return true; return true;
} }
@@ -171,11 +170,11 @@ void CWeather::show(int x, int y)
if (!g_settings.weather_enabled || coords.empty()) if (!g_settings.weather_enabled || coords.empty())
return; return;
CComponentsPicture *ptmp = new CComponentsPicture(RADIUS_MID, RADIUS_MID, getActIcon()); CComponentsPicture *ptmp = new CComponentsPicture(RADIUS_MID, RADIUS_MID, getCurrentIcon());
ptmp->setColorBody(form->getColorBody()); ptmp->setColorBody(form->getColorBody());
form->addCCItem(ptmp); form->addCCItem(ptmp);
CComponentsText *temp = new CComponentsText(ptmp->getWidth() + 2*RADIUS_MID, ptmp->getHeight()/2 + RADIUS_MID - g_Font[SNeutrinoSettings::FONT_TYPE_INFOBAR_NUMBER]->getHeight()/2, 0, 0, getActTemp() + "°C", CTextBox::AUTO_WIDTH, g_Font[SNeutrinoSettings::FONT_TYPE_INFOBAR_NUMBER]); CComponentsText *temp = new CComponentsText(ptmp->getWidth() + 2*RADIUS_MID, ptmp->getHeight()/2 + RADIUS_MID - g_Font[SNeutrinoSettings::FONT_TYPE_INFOBAR_NUMBER]->getHeight()/2, 0, 0, getCurrentTemperature() + "°C", CTextBox::AUTO_WIDTH, g_Font[SNeutrinoSettings::FONT_TYPE_INFOBAR_NUMBER]);
temp->doPaintBg(false); temp->doPaintBg(false);
temp->setTextColor(COL_INFOBAR_TEXT); temp->setTextColor(COL_INFOBAR_TEXT);
form->addCCItem(temp); form->addCCItem(temp);

View File

@@ -34,12 +34,23 @@
#include <gui/components/cc.h> #include <gui/components/cc.h>
struct current_data
{
std::string icon;
float temperature;
current_data():
icon(""),
temperature(0)
{}
};
typedef struct typedef struct
{ {
std::string wicon;
float min_temp;
float max_temp;
time_t timestamp; time_t timestamp;
std::string icon;
float temperatureMin;
float temperatureMax;
} forecast_data; } forecast_data;
class CWeather class CWeather
@@ -48,8 +59,7 @@ class CWeather
std::string coords; std::string coords;
std::string city; std::string city;
std::string timezone; std::string timezone;
std::string act_wicon; current_data current;
float act_temp;
std::vector<forecast_data> v_forecast; std::vector<forecast_data> v_forecast;
CComponentsForm *form; CComponentsForm *form;
std::string key; std::string key;
@@ -62,28 +72,30 @@ class CWeather
~CWeather(); ~CWeather();
bool checkUpdate(bool forceUpdate = false); bool checkUpdate(bool forceUpdate = false);
void setCoords(std::string new_coords, std::string new_city = "Unknown"); void setCoords(std::string new_coords, std::string new_city = "Unknown");
void show(int x = 50, int y = 50);
void hide();
std::string getCity() std::string getCity()
{ {
return city; return city;
}; };
std::string getActTemp() std::string getCurrentTemperature()
{ {
return to_string((int)(act_temp + 0.5)); return to_string((int)(current.temperature + 0.5));
}; };
std::string getForecastTemp(int i = 0) std::string getCurrentIcon()
{ {
return to_string((int)(v_forecast[i].max_temp + 0.5)); return ICONSDIR"/weather/" + current.icon;
}; };
std::string getActIcon() std::string getForecastTemperatureMax(int i = 0)
{ {
return ICONSDIR"/weather/" + act_wicon; return to_string((int)(v_forecast[i].temperatureMax + 0.5));
}; };
std::string getForecastIcon(int i = 0) std::string getForecastIcon(int i = 0)
{ {
return ICONSDIR"/weather/" + v_forecast[i].wicon; return ICONSDIR"/weather/" + v_forecast[i].icon;
}; };
void show(int x = 50, int y = 50);
void hide();
}; };
#endif #endif