-
Notifications
You must be signed in to change notification settings - Fork 1
/
ISKOJsonClient.cpp
executable file
·129 lines (104 loc) · 2.59 KB
/
ISKOJsonClient.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
#include "ISKOJsonClient.h"
ISKOJsonClient::ISKOJsonClient() {}
void ISKOJsonClient::parseISKOJson() {
JsonStreamingParser parser;
parser.setListener(this);
WiFiClient client;
// http://portal.chmi.cz/files/portal/docs/uoco/web_generator/aqindex_cze.json
const char host[] = "portal.chmi.cz";
String url = "/files/portal/docs/uoco/web_generator/aqindex_cze.json";
const int httpPort = 80;
if (!client.connect(host, httpPort)) {
Serial.println("connection failed");
return;
}
Serial.print("Requesting URL: ");
Serial.println(url);
// This will send the request to the server
client.print(String("GET ") + url + " HTTP/1.1\r\n" +
"Host: " + host + "\r\n" +
"Connection: close\r\n\r\n");
int retryCounter = 0;
while(!client.available()) {
Serial.println(".");
delay(1000);
retryCounter++;
if (retryCounter > 10) {
noNewData = true;
return;
}
}
noNewData = true;
char c;
int size = 0;
client.setNoDelay(false);
while(client.connected()) {
while((size = client.available()) > 0) {
c = client.read();
parser.parse(c);
}
}
}
int ISKOJsonClient::getIndex() {
if (noNewData == true) {
return -1;
} else if (finIndex == "0") {
return 0;
} else {
int indexValue = finIndex.toInt();
if (indexValue == 0) {
return -2;
} else {
return indexValue;
}
}
}
void ISKOJsonClient::whitespace(char c) {
//Serial.println("whitespace");
}
void ISKOJsonClient::startDocument() {
//Serial.println("start document");
}
void ISKOJsonClient::key(String key) {
currentKey = key;
//Serial.println("key: " + key);
}
void ISKOJsonClient::value(String value) {
if (currentKey == "Code" && ignore == false) {
code = value;
}
if (currentKey == "Ix" && ignore == false) {
ix = value;
}
if (currentKey == "Actualized") {
Serial.println("Aktualizovano: " + value);
}
//Serial.println("value: " + value);
}
void ISKOJsonClient::endArray() {
ignore = false;
}
void ISKOJsonClient::endObject() {
if (code == "ALIBA" && ignore == false) {
Serial.println("Index je v Libusi " + ix);
noNewData = false;
finIndex = ix;
}
//Serial.println("end object. ");
}
void ISKOJsonClient::endDocument() {
//Serial.println("end document. ");
}
void ISKOJsonClient::startArray() {
if (currentKey == "Components") {
ignore = true;
}
//Serial.println("start array. ");
}
void ISKOJsonClient::startObject() {
if (ignore == false) {
code = "";
ix = "";
}
//Serial.println("start object. ");
}