forked from rszimm/sprinklers_pi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DarkSky.cpp
138 lines (113 loc) · 3.27 KB
/
DarkSky.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
// DarkSky.cpp
// This file manages the retrieval of Weather related information and adjustment of durations
// from DarkSky
#include "config.h"
#ifdef WEATHER_DARKSKY
#include "DarkSky.h"
#include "core.h"
#include "port.h"
#include <string.h>
#include <stdlib.h>
#include <fstream>
#include "json.hpp"
using json = nlohmann::json;
DarkSky::DarkSky(void)
{
m_darkSkyAPIHost="api.darksky.net";
}
static void ParseResponse(json &data, Weather::ReturnVals * ret)
{
freeMemory();
ret->valid = false;
ret->maxhumidity = -999;
ret->minhumidity = 999;
float temp=0;
float wind=0;
float precip=0;
float uv=0;
short humidity;
short i=0;
try {
for (auto &hour : data["hourly"]["data"]) {
temp += hour["temperature"].get<float>();
wind += hour["windSpeed"].get<float>();
precip += hour["precipIntensity"].get<float>();
uv += hour["uvIndex"].get<float>();
humidity = (short) std::round(hour["humidity"].get<float>() * 100.0);
if (humidity > ret->maxhumidity) {
ret->maxhumidity = humidity;
}
if (humidity < ret->minhumidity) {
ret->minhumidity = humidity;
}
i++;
}
if (i > 0) {
ret->valid = true;
ret->meantempi = (short) std::round(temp/i);
ret->windmph = (short) std::round(wind/i * WIND_FACTOR);
ret->precipi = (short) std::round(precip * PRECIP_FACTOR); // we want total not average
ret->UV = (short) std::round(uv/i * UV_FACTOR);
}
} catch(std::exception &err) {
trace(err.what());
}
if (ret->maxhumidity == -999 || ret->maxhumidity > 100) {
ret->maxhumidity = NEUTRAL_HUMIDITY;
}
if (ret->minhumidity == 999 || ret->minhumidity < 0) {
ret->minhumidity = NEUTRAL_HUMIDITY;
}
trace("Parsed the following values:\ntemp: %d\nwind: %0.2f\nprecip: %0.2f\nuv: %0.2f\n",
ret->meantempi, ret->windmph/WIND_FACTOR, ret->precipi/PRECIP_FACTOR, ret->UV/UV_FACTOR);
}
static void GetData(const Weather::Settings & settings,const char *m_darkSkyAPIHost,time_t timestamp, Weather::ReturnVals * ret)
{
char cmd[255];
snprintf(cmd, sizeof(cmd), "/usr/bin/curl -sS -o /tmp/darksky.json 'https://%s/forecast/%s/%s,%ld?exclude=currently,daily,minutely,flags'", m_darkSkyAPIHost, settings.apiSecret, settings.location, timestamp);
// trace("cmd: %s\n",cmd);
FILE *fh;
char buf[255];
buf[0]=0;
if ((fh = popen(cmd, "r")) != NULL) {
size_t byte_count = fread(buf, 1, sizeof(buf) - 1, fh);
buf[byte_count] = 0;
}
(void) pclose(fh);
trace("curl error output: %s\n",buf);
json j;
std::ifstream ifs("/tmp/darksky.json");
ifs >> j;
ParseResponse(j, ret);
ifs.close();
if (!ret->valid)
{
if (ret->keynotfound)
trace("Invalid DarkSky Key\n");
else
trace("Bad DarkSky Response\n");
}
}
Weather::ReturnVals DarkSky::InternalGetVals(const Weather::Settings & settings) const
{
ReturnVals vals = {0};
const time_t local_now = nntpTimeServer.LocalNow();
// today
trace("Get Today's Weather\n");
GetData(settings, m_darkSkyAPIHost, local_now, &vals);
if (vals.valid) {
// save today's values
short precip_today = vals.precipi;
short uv_today = vals.UV;
// yesterday
trace("Get Yesterday's Weather\n");
GetData(settings, m_darkSkyAPIHost, local_now - 24 * 3600, &vals);
if (vals.valid) {
// restore today's values
vals.precip_today = precip_today;
vals.UV = uv_today;
}
}
return vals;
}
#endif