-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathstookwijzer.cpp
64 lines (55 loc) · 2.21 KB
/
stookwijzer.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
#include <stdbool.h>
#include <ArduinoJson.h>
#include <WiFiClientSecure.h>
#include <ESP8266HTTPClient.h>
#include "stookwijzer.h"
#define printf Serial.printf
static StaticJsonDocument < 256 > filter; // https://arduinojson.org/v6/assistant
static WiFiClient *client;
static HTTPClient http;
void stookwijzer_begin(WiFiClient &wifiClient, const char *user_agent)
{
client = &wifiClient;
// configure HTTP client: follow redirects, non-chunked mode, disable connection re-use
http.setFollowRedirects(HTTPC_STRICT_FOLLOW_REDIRECTS);
http.useHTTP10(true);
http.setReuse(false);
http.setUserAgent(user_agent);
// pre-define the streaming JSON filter
filter["features"][0]["properties"]["pc4"] = true;
filter["features"][0]["properties"]["lki"] = true;
filter["features"][0]["properties"]["wind"] = true;
filter["features"][0]["properties"]["advies_0"] = true;
filter["features"][0]["properties"]["definitief_0"] = true;
printf("JSON filter:\n");
serializeJsonPretty(filter, Serial);
printf("\n");
}
bool stookwijzer_get(double latitude, double longitude, int &score)
{
double delta = 0.00001;
char url[300];
sprintf(url, "https://data.rivm.nl/geo/alo/wms?SERVICE=WMS&VERSION=1.3.0&REQUEST=GetFeatureInfo"
"&QUERY_LAYERS=stookwijzer_v2&LAYERS=stookwijzer_v2&info_format=application/json&feature_count=1"
"&I=0&J=0&WIDTH=1&HEIGHT=1&CRS=CRS:84&BBOX=%.5f,%.5f,%.5f,%.5f", longitude - delta,
latitude - delta, longitude + delta, latitude + delta);
bool result = false;
if (http.begin(*client, url)) {
printf("GET %s... ", url);
int httpCode = http.GET();
printf("%d\n", httpCode);
if (httpCode == HTTP_CODE_OK) {
DynamicJsonDocument doc(2048);
printf("Deserializing from HTTP... ");
DeserializationError error =
deserializeJson(doc, http.getStream(), DeserializationOption::Filter(filter));
printf("%s\n", error.c_str());
serializeJsonPretty(doc, Serial);
printf("\n");
score = doc["features"][0]["properties"]["advies_0"];
result = true;
}
http.end();
}
return result;
}