- weather: add more data to struct

Signed-off-by: Thilo Graf <dbt@novatux.de>
This commit is contained in:
svenhoefer
2019-03-23 13:33:35 +01:00
committed by Thilo Graf
parent e04de3136e
commit 34a4954ab5
2 changed files with 51 additions and 5 deletions

View File

@@ -42,9 +42,10 @@
#define UPDATE_CYCLE 15 // minutes #define UPDATE_CYCLE 15 // minutes
CWeather *weather = NULL;
CWeather *CWeather::getInstance() CWeather *CWeather::getInstance()
{ {
static CWeather *weather = NULL;
if (!weather) if (!weather)
weather = new CWeather(); weather = new CWeather();
return weather; return weather;
@@ -73,7 +74,7 @@ void CWeather::setCoords(std::string new_coords, std::string new_city)
{ {
coords = new_coords; coords = new_coords;
city = new_city; city = new_city;
GetWeatherDetails(); checkUpdate(true);
} }
} }
@@ -130,8 +131,10 @@ bool CWeather::GetWeatherDetails()
timezone = DataValues["timezone"].asString(); timezone = DataValues["timezone"].asString();
current.timestamp = DataValues["currently"].get("time", 0).asDouble(); current.timestamp = DataValues["currently"].get("time", 0).asDouble();
current.temperature = DataValues["currently"].get("temperature", "").asFloat(); current.temperature = DataValues["currently"].get("temperature", "").asFloat();
current.windSpeed = DataValues["currently"].get("windSpeed", 0).asFloat(); current.pressure = DataValues["currently"].get("pressure", "").asFloat();
current.windBearing = DataValues["currently"].get("windBearing", 0).asDouble(); current.humidity = DataValues["currently"].get("humidity", "").asFloat();
current.windSpeed = DataValues["currently"].get("windSpeed", "").asFloat();
current.windBearing = DataValues["currently"].get("windBearing", "").asDouble();
current.icon = DataValues["currently"].get("icon", "").asString(); current.icon = DataValues["currently"].get("icon", "").asString();
if (current.icon.empty()) if (current.icon.empty())
current.icon = "unknown.png"; current.icon = "unknown.png";
@@ -144,6 +147,7 @@ bool CWeather::GetWeatherDetails()
for (unsigned int i = 0; i < elements.size(); i++) for (unsigned int i = 0; i < elements.size(); i++)
{ {
daily_data.timestamp = elements[i].get("time", 0).asDouble(); daily_data.timestamp = elements[i].get("time", 0).asDouble();
daily_data.weekday = (int)(localtime(&daily_data.timestamp)->tm_wday);
daily_data.icon = elements[i].get("icon", "").asString(); daily_data.icon = elements[i].get("icon", "").asString();
if (daily_data.icon.empty()) if (daily_data.icon.empty())
daily_data.icon = "unknown.png"; daily_data.icon = "unknown.png";
@@ -151,6 +155,8 @@ bool CWeather::GetWeatherDetails()
daily_data.icon = daily_data.icon + ".png"; daily_data.icon = daily_data.icon + ".png";
daily_data.temperatureMin = elements[i].get("temperatureMin", "").asFloat(); daily_data.temperatureMin = elements[i].get("temperatureMin", "").asFloat();
daily_data.temperatureMax = elements[i].get("temperatureMax", "").asFloat(); daily_data.temperatureMax = elements[i].get("temperatureMax", "").asFloat();
daily_data.sunriseTime = elements[i].get("sunriseTime", 0).asDouble();
daily_data.sunsetTime = elements[i].get("sunsetTime", 0).asDouble();
daily_data.windSpeed = elements[i].get("windSpeed", 0).asFloat(); daily_data.windSpeed = elements[i].get("windSpeed", 0).asFloat();
daily_data.windBearing = elements[i].get("windBearing", 0).asDouble(); daily_data.windBearing = elements[i].get("windBearing", 0).asDouble();

View File

@@ -39,13 +39,17 @@ struct current_data
time_t timestamp; time_t timestamp;
std::string icon; std::string icon;
float temperature; float temperature;
float humidity;
float pressure;
float windSpeed; float windSpeed;
int windBearing; int windBearing;
current_data(): current_data():
timestamp(0), timestamp(0),
icon(""), icon("unknown.png"),
temperature(0), temperature(0),
humidity(0),
pressure(0),
windSpeed(0), windSpeed(0),
windBearing(0) windBearing(0)
{} {}
@@ -54,9 +58,12 @@ struct current_data
typedef struct typedef struct
{ {
time_t timestamp; time_t timestamp;
int weekday; // 0=Sunday, 1=Monday, ...
std::string icon; std::string icon;
float temperatureMin; float temperatureMin;
float temperatureMax; float temperatureMax;
time_t sunriseTime;
time_t sunsetTime;
float windSpeed; float windSpeed;
int windBearing; int windBearing;
} forecast_data; } forecast_data;
@@ -81,10 +88,13 @@ class 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");
// globals
std::string getCity() std::string getCity()
{ {
return city; return city;
}; };
// current conditions
#if 0 #if 0
std::string getCurrentTimestamp() std::string getCurrentTimestamp()
{ {
@@ -99,6 +109,14 @@ class CWeather
{ {
return to_string((int)(current.temperature + 0.5)); return to_string((int)(current.temperature + 0.5));
}; };
std::string getCurrentHumidity()
{
return to_string((int)(current.humidity * 100.0));
};
std::string getCurrentPressure()
{
return to_string(current.pressure);
};
std::string getCurrentWindSpeed() std::string getCurrentWindSpeed()
{ {
return to_string(current.windSpeed); return to_string(current.windSpeed);
@@ -111,24 +129,46 @@ class CWeather
{ {
return ICONSDIR"/weather/" + current.icon; return ICONSDIR"/weather/" + current.icon;
}; };
// forecast conditions
int getForecastSize()
{
return (int)v_forecast.size();
};
int getForecastWeekday(int i = 0)
{
if (i > (int)v_forecast.size())
i = (int)v_forecast.size();
return v_forecast[i].weekday;
};
std::string getForecastTemperatureMin(int i = 0) std::string getForecastTemperatureMin(int i = 0)
{ {
if (i > (int)v_forecast.size())
i = (int)v_forecast.size();
return to_string((int)(v_forecast[i].temperatureMin + 0.5)); return to_string((int)(v_forecast[i].temperatureMin + 0.5));
}; };
std::string getForecastTemperatureMax(int i = 0) std::string getForecastTemperatureMax(int i = 0)
{ {
if (i > (int)v_forecast.size())
i = (int)v_forecast.size();
return to_string((int)(v_forecast[i].temperatureMax + 0.5)); return to_string((int)(v_forecast[i].temperatureMax + 0.5));
}; };
std::string getForecastWindSpeed(int i = 0) std::string getForecastWindSpeed(int i = 0)
{ {
if (i > (int)v_forecast.size())
i = (int)v_forecast.size();
return to_string(v_forecast[i].windSpeed); return to_string(v_forecast[i].windSpeed);
}; };
std::string getForecastWindBearing(int i = 0) std::string getForecastWindBearing(int i = 0)
{ {
if (i > (int)v_forecast.size())
i = (int)v_forecast.size();
return to_string(v_forecast[i].windBearing); return to_string(v_forecast[i].windBearing);
}; };
std::string getForecastIcon(int i = 0) std::string getForecastIcon(int i = 0)
{ {
if (i > (int)v_forecast.size())
i = (int)v_forecast.size();
return ICONSDIR"/weather/" + v_forecast[i].icon; return ICONSDIR"/weather/" + v_forecast[i].icon;
}; };