forked from indilib/indi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwunderground.cpp
254 lines (207 loc) · 8.2 KB
/
wunderground.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
/*******************************************************************************
Copyright(c) 2015 Jasem Mutlaq. All rights reserved.
INDI Weather Underground (TM) Weather Driver
This program is free software; you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by the Free
Software Foundation; either version 2 of the License, or (at your option)
any later version.
This program is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
more details.
You should have received a copy of the GNU Library General Public License
along with this library; see the file COPYING.LIB. If not, write to
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
Boston, MA 02110-1301, USA.
The full GNU General Public License is included in this distribution in the
file called LICENSE.
*******************************************************************************/
#include "wunderground.h"
#include "gason.h"
#include "locale_compat.h"
#include <curl/curl.h>
#include <memory>
#include <cstring>
// We declare an auto pointer to WunderGround.
std::unique_ptr<WunderGround> wunderGround(new WunderGround());
static size_t WriteCallback(void *contents, size_t size, size_t nmemb, void *userp)
{
((std::string *)userp)->append((char *)contents, size * nmemb);
return size * nmemb;
}
WunderGround::WunderGround()
{
setVersion(1, 0);
wunderLat = -1000;
wunderLong = -1000;
setWeatherConnection(CONNECTION_NONE);
}
WunderGround::~WunderGround()
{
}
const char *WunderGround::getDefaultName()
{
return (const char *)"WunderGround";
}
bool WunderGround::Connect()
{
if (wunderAPIKeyT[0].text == nullptr)
{
LOG_ERROR("Weather Underground API Key is not available. Please register your API key at "
"www.wunderground.com and save it under Options.");
return false;
}
return true;
}
bool WunderGround::Disconnect()
{
return true;
}
bool WunderGround::initProperties()
{
INDI::Weather::initProperties();
IUFillText(&wunderAPIKeyT[0], "API_KEY", "API Key", nullptr);
IUFillTextVector(&wunderAPIKeyTP, wunderAPIKeyT, 1, getDeviceName(), "WUNDER_API_KEY", "Wunder", OPTIONS_TAB, IP_RW,
60, IPS_IDLE);
addParameter("WEATHER_FORECAST", "Weather", 0, 0, 15);
addParameter("WEATHER_TEMPERATURE", "Temperature (C)", -10, 30, 15);
addParameter("WEATHER_WIND_SPEED", "Wind (kph)", 0, 20, 15);
addParameter("WEATHER_WIND_GUST", "Gust (kph)", 0, 20, 15);
addParameter("WEATHER_RAIN_HOUR", "Precip (mm)", 0, 0, 15);
setCriticalParameter("WEATHER_FORECAST");
setCriticalParameter("WEATHER_TEMPERATURE");
setCriticalParameter("WEATHER_WIND_SPEED");
setCriticalParameter("WEATHER_RAIN_HOUR");
addDebugControl();
return true;
}
void WunderGround::ISGetProperties(const char *dev)
{
INDI::Weather::ISGetProperties(dev);
defineProperty(&wunderAPIKeyTP);
loadConfig(true, "WUNDER_API_KEY");
}
bool WunderGround::ISNewText(const char *dev, const char *name, char *texts[], char *names[], int n)
{
if (dev != nullptr && strcmp(dev, getDeviceName()) == 0)
{
if (!strcmp(wunderAPIKeyTP.name, name))
{
IUUpdateText(&wunderAPIKeyTP, texts, names, n);
wunderAPIKeyTP.s = IPS_OK;
IDSetText(&wunderAPIKeyTP, nullptr);
return true;
}
}
return INDI::Weather::ISNewText(dev, name, texts, names, n);
}
bool WunderGround::updateLocation(double latitude, double longitude, double elevation)
{
INDI_UNUSED(elevation);
wunderLat = latitude;
wunderLong = (longitude > 180) ? (longitude - 360) : longitude;
return true;
}
IPState WunderGround::updateWeather()
{
CURL *curl;
CURLcode res;
std::string readBuffer;
char requestURL[MAXRBUF];
// If location is not updated yet, return busy
if (wunderLat == -1000 || wunderLong == -1000)
return IPS_BUSY;
AutoCNumeric locale;
snprintf(requestURL, MAXRBUF, "http://api.wunderground.com/api/%s/conditions/q/%g,%g.json", wunderAPIKeyT[0].text,
wunderLat, wunderLong);
curl = curl_easy_init();
if (curl)
{
curl_easy_setopt(curl, CURLOPT_URL, requestURL);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &readBuffer);
res = curl_easy_perform(curl);
curl_easy_cleanup(curl);
}
char srcBuffer[readBuffer.size()];
strncpy(srcBuffer, readBuffer.c_str(), readBuffer.size());
char *source = srcBuffer;
// do not forget terminate source string with 0
char *endptr;
JsonValue value;
JsonAllocator allocator;
int status = jsonParse(source, &endptr, &value, allocator);
if (status != JSON_OK)
{
LOGF_ERROR("%s at %zd", jsonStrError(status), endptr - source);
LOGF_DEBUG("%s", requestURL);
LOGF_DEBUG("%s", readBuffer.c_str());
return IPS_ALERT;
}
JsonIterator it;
JsonIterator observationIterator;
for (it = begin(value); it != end(value); ++it)
{
if (!strcmp(it->key, "current_observation"))
{
for (observationIterator = begin(it->value); observationIterator != end(it->value); ++observationIterator)
{
if (!strcmp(observationIterator->key, "weather"))
{
char *value = observationIterator->value.toString();
if (!strcmp(value, "Clear"))
setParameterValue("WEATHER_FORECAST", 0);
else if (!strcmp(value, "Unknown") || !strcmp(value, "Scattered Clouds") ||
!strcmp(value, "Partly Cloudy") || !strcmp(value, "Overcast") ||
!strcmp(value, "Patches of Fog") || !strcmp(value, "Partial Fog") ||
!strcmp(value, "Light Haze"))
setParameterValue("WEATHER_FORECAST", 1);
else
setParameterValue("WEATHER_FORECAST", 2);
LOGF_INFO("Weather condition: %s", value);
}
else if (!strcmp(observationIterator->key, "temp_c"))
{
if (observationIterator->value.isDouble())
setParameterValue("WEATHER_TEMPERATURE", observationIterator->value.toNumber());
else
setParameterValue("WEATHER_TEMPERATURE", atof(observationIterator->value.toString()));
}
else if (!strcmp(observationIterator->key, "wind_kph"))
{
if (observationIterator->value.isDouble())
setParameterValue("WEATHER_WIND_SPEED", observationIterator->value.toNumber());
else
setParameterValue("WEATHER_WIND_SPEED", atof(observationIterator->value.toString()));
}
else if (!strcmp(observationIterator->key, "wind_gust_kph"))
{
if (observationIterator->value.isDouble())
setParameterValue("WEATHER_WIND_GUST", observationIterator->value.toNumber());
else
setParameterValue("WEATHER_WIND_GUST", atof(observationIterator->value.toString()));
}
else if (!strcmp(observationIterator->key, "precip_1hr_metric"))
{
char *value = observationIterator->value.toString();
double mm = -1;
if (!strcmp(value, "--"))
setParameterValue("WEATHER_RAIN_HOUR", 0);
else
{
mm = atof(value);
if (mm >= 0)
setParameterValue("WEATHER_RAIN_HOUR", mm);
}
}
}
}
}
return IPS_OK;
}
bool WunderGround::saveConfigItems(FILE *fp)
{
INDI::Weather::saveConfigItems(fp);
IUSaveConfigText(fp, &wunderAPIKeyTP);
return true;
}