Skip to content

Commit

Permalink
push more weather data
Browse files Browse the repository at this point in the history
  • Loading branch information
mpoegel committed Sep 27, 2023
1 parent 086f13b commit 1ad3d41
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 12 deletions.
79 changes: 69 additions & 10 deletions chestnut/chestnut.cpp
Expand Up @@ -18,15 +18,43 @@ namespace chestnut {
namespace {
const std::string DEGREE_FAHRENHEIT = "℉";

std::string dayOfWeek(fwoop::DateTime dt, int ahead = 0)
{
dt.addDays(ahead);
switch (dt.dayOfWeek()) {
case fwoop::DateTime::Sunday:
return "Sun";
case fwoop::DateTime::Monday:
return "Mon";
case fwoop::DateTime::Tuesday:
return "Tue";
case fwoop::DateTime::Wednesday:
return "Wed";
case fwoop::DateTime::Thursday:
return "Thu";
case fwoop::DateTime::Friday:
return "Fri";
case fwoop::DateTime::Saturday:
return "Sat";
default:
return "N/A";
}
}

std::string getFormattedTime(const fwoop::DateTime &t)
{
std::string res;
std::string res = dayOfWeek(t);
res += " ";
res += (t.hour() < 10 ? "0" : "") + std::to_string(t.hour());
res += ":";
res += (t.minute() < 10 ? "0" : "") + std::to_string(t.minute());
return res;
}

std::string getFormattedTemp(double temp) { return std::to_string(int(std::round(temp))) + DEGREE_FAHRENHEIT; }

std::string getFormattedPercent(double num) { return std::to_string(int(std::round(num))) + "%"; }

} // namespace

Chestnut::Chestnut()
Expand Down Expand Up @@ -61,27 +89,58 @@ void Chestnut::serveHttp()
fwoop::HttpServerEvent &serverEvent) {
unsigned int t = 60;
while (!d_needStop) {
auto now = fwoop::DateTime::now();
// update every 60 seconds
if (t % 60 == 0) {
fwoop::Log::Debug("server side event update");
auto data = d_weatherClient->data();
auto today = data.getDaily(0);
if (!serverEvent.pushEvent("today-temp", std::to_string(int(std::round(today.getTemperature()))) +
DEGREE_FAHRENHEIT) ||
!serverEvent.pushEvent("today-max",
std::to_string(int(std::round(today.getMaxTemp()))) + DEGREE_FAHRENHEIT) ||
!serverEvent.pushEvent("today-min",
std::to_string(int(std::round(today.getMinTemp()))) + DEGREE_FAHRENHEIT) ||
if (!serverEvent.pushEvent("today-temp", getFormattedTemp(today.getTemperature())) ||
!serverEvent.pushEvent("today-max", getFormattedTemp(today.getMaxTemp())) ||
!serverEvent.pushEvent("today-min", getFormattedTemp(today.getMinTemp())) ||
!serverEvent.pushEvent("today-humidity", std::to_string(today.getHumidity()) + "%") ||
!serverEvent.pushEvent("today-precip",
std::to_string(int(std::round(today.getPrecipChance() * 100))) + "%")) {
!serverEvent.pushEvent("today-precip", getFormattedPercent(today.getPrecipChance() * 100)) ||
!serverEvent.pushEvent("forecast-plus-1", dayOfWeek(now, 1)) ||
!serverEvent.pushEvent("forecast-plus-1-temp-max",
getFormattedTemp(data.getDaily(1).getMaxTemp())) ||
!serverEvent.pushEvent("forecast-plus-1-temp-min",
getFormattedTemp(data.getDaily(1).getMinTemp())) ||
!serverEvent.pushEvent("forecast-plus-1-precip",
getFormattedPercent(data.getDaily(1).getPrecipChance() * 100)) ||
!serverEvent.pushEvent("forecast-plus-2", dayOfWeek(now, 2)) ||
!serverEvent.pushEvent("forecast-plus-2-temp-max",
getFormattedTemp(data.getDaily(2).getMaxTemp())) ||
!serverEvent.pushEvent("forecast-plus-2-temp-min",
getFormattedTemp(data.getDaily(2).getMinTemp())) ||
!serverEvent.pushEvent("forecast-plus-2-precip",
getFormattedPercent(data.getDaily(2).getPrecipChance() * 100)) ||
!serverEvent.pushEvent("forecast-plus-3", dayOfWeek(now, 3)) ||
!serverEvent.pushEvent("forecast-plus-3-temp-max",
getFormattedTemp(data.getDaily(3).getMaxTemp())) ||
!serverEvent.pushEvent("forecast-plus-3-temp-min",
getFormattedTemp(data.getDaily(3).getMinTemp())) ||
!serverEvent.pushEvent("forecast-plus-3-precip",
getFormattedPercent(data.getDaily(3).getPrecipChance() * 100)) ||
!serverEvent.pushEvent("forecast-plus-4", dayOfWeek(now, 4)) ||
!serverEvent.pushEvent("forecast-plus-4-temp-max",
getFormattedTemp(data.getDaily(4).getMaxTemp())) ||
!serverEvent.pushEvent("forecast-plus-4-temp-min",
getFormattedTemp(data.getDaily(4).getMinTemp())) ||
!serverEvent.pushEvent("forecast-plus-4-precip",
getFormattedPercent(data.getDaily(4).getPrecipChance() * 100)) ||
!serverEvent.pushEvent("forecast-plus-5", dayOfWeek(now, 5)) ||
!serverEvent.pushEvent("forecast-plus-5-temp-max",
getFormattedTemp(data.getDaily(5).getMaxTemp())) ||
!serverEvent.pushEvent("forecast-plus-5-temp-min",
getFormattedTemp(data.getDaily(5).getMinTemp())) ||
!serverEvent.pushEvent("forecast-plus-5-precip",
getFormattedPercent(data.getDaily(5).getPrecipChance() * 100))) {
break;
}
t = 0;
}
t++;
// always update the time
auto now = fwoop::DateTime::now();
now.addHours(Config::timezone());
if (!serverEvent.pushEvent("timestamp", getFormattedTime(now))) {
return;
Expand Down
6 changes: 4 additions & 2 deletions static/index.css
Expand Up @@ -35,7 +35,8 @@ body {
}

#feed-item-temp-today .feed-item,
#feed-item-wet-today .feed-item {
#feed-item-wet-today .feed-item,
#feed-item-forecast .feed-item {
display: flex;
flex-flow: column wrap;
justify-content: center;
Expand All @@ -48,7 +49,8 @@ body {
}

#feed-item-temp-today,
#feed-item-wet-today {
#feed-item-wet-today,
#feed-item-forecast {
display: flex;
flex-flow: column wrap;
justify-content: center;
Expand Down
33 changes: 33 additions & 0 deletions static/index.html
Expand Up @@ -40,6 +40,39 @@
</div>
</div>

<div id="feed-item-forecast" class="feed-item">
<div class="feed-item">
<div class="label" sse-swap="forecast-plus-1"></div>
<div class="value" sse-swap="forecast-plus-1-temp-max"></div>
<div class="value" sse-swap="forecast-plus-1-temp-min"></div>
<div class="value" sse-swap="forecast-plus-1-precip"></div>
</div>
<div class="feed-item">
<div class="label" sse-swap="forecast-plus-2"></div>
<div class="value" sse-swap="forecast-plus-2-temp-max"></div>
<div class="value" sse-swap="forecast-plus-2-temp-min"></div>
<div class="value" sse-swap="forecast-plus-2-precip"></div>
</div>
<div class="feed-item">
<div class="label" sse-swap="forecast-plus-3"></div>
<div class="value" sse-swap="forecast-plus-3-temp-max"></div>
<div class="value" sse-swap="forecast-plus-3-temp-min"></div>
<div class="value" sse-swap="forecast-plus-3-precip"></div>
</div>
<div class="feed-item">
<div class="label" sse-swap="forecast-plus-4"></div>
<div class="value" sse-swap="forecast-plus-4-temp-max"></div>
<div class="value" sse-swap="forecast-plus-4-temp-min"></div>
<div class="value" sse-swap="forecast-plus-4-precip"></div>
</div>
<div class="feed-item">
<div class="label" sse-swap="forecast-plus-5"></div>
<div class="value" sse-swap="forecast-plus-5-temp-max"></div>
<div class="value" sse-swap="forecast-plus-5-temp-min"></div>
<div class="value" sse-swap="forecast-plus-5-precip"></div>
</div>
</div>

</div>

</body>
Expand Down

0 comments on commit 1ad3d41

Please sign in to comment.