-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsumc-api.h
175 lines (145 loc) · 6.47 KB
/
sumc-api.h
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
#include <HTTPClient.h>
#include <WiFiClient.h>
#include <WiFiClientSecure.h>
#include <TimeLib.h>
#include <ArduinoJson.h>
#include <Vector.h>
namespace Constants {
const char CERT[] = \
"-----BEGIN CERTIFICATE-----\n" \
"MIIFazCCA1OgAwIBAgIRAIIQz7DSQONZRGPgu2OCiwAwDQYJKoZIhvcNAQELBQAw\n" \
"TzELMAkGA1UEBhMCVVMxKTAnBgNVBAoTIEludGVybmV0IFNlY3VyaXR5IFJlc2Vh\n" \
"cmNoIEdyb3VwMRUwEwYDVQQDEwxJU1JHIFJvb3QgWDEwHhcNMTUwNjA0MTEwNDM4\n" \
"WhcNMzUwNjA0MTEwNDM4WjBPMQswCQYDVQQGEwJVUzEpMCcGA1UEChMgSW50ZXJu\n" \
"ZXQgU2VjdXJpdHkgUmVzZWFyY2ggR3JvdXAxFTATBgNVBAMTDElTUkcgUm9vdCBY\n" \
"MTCCAiIwDQYJKoZIhvcNAQEBBQADggIPADCCAgoCggIBAK3oJHP0FDfzm54rVygc\n" \
"h77ct984kIxuPOZXoHj3dcKi/vVqbvYATyjb3miGbESTtrFj/RQSa78f0uoxmyF+\n" \
"0TM8ukj13Xnfs7j/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57iaztXOoJwTdwJx4nLCgdNbOhdjsnvzqvHu7Ur\n" \
"TkXWStAmzOVyyghqpZXjFaH3pO3JLF+l+/+sKAIuvtd7u+Nxe5AW0wdeRlN8NwdC\n" \
"jNPElpzVmbUq4JUagEiuTDkHzsxHpFKVK7q4+63SM1N95R1NbdWhscdCb+ZAJzVc\n" \
"oyi3B43njTOQ5yOf+1CceWxG1bQVs5ZufpsMljq4Ui0/1lvh+wjChP4kqKOJ2qxq\n" \
"4RgqsahDYVvTH9w7jXbyLeiNdd8XM2w9U/t7y0Ff/9yi0GE44Za4rF2LN9d11TPA\n" \
"mRGunUHBcnWEvgJBQl9nJEiU0Zsnvgc/ubhPgXRR4Xq37Z0j4r7g1SgEEzwxA57d\n" \
"emyPxgcYxn/eR44/KJ4EBs+lVDR3veyJm+kXQ99b21/+jh5Xos1AnX5iItreGCc=\n" \
"-----END CERTIFICATE-----\n";
const char TIME_FORMAT[] = "%d-%d-%d %d:%d:%d";
const char TIME_FORMAT_TIME_ONLY[] = "%d:%d:%d";
const char API_ENDPOINT[] = "https://cukii.me/sumc/api/timing/";
}
struct Line {
String name;
time_t nextArrival;
};
struct StopInfo {
Vector<Line> lines;
time_t calculatedTime;
bool isValid;
};
Line LINES[100];
String httpsGET(const char* address, const char* certificate) {
WiFiClient* client;
String result = "";
if (certificate) {
client = new WiFiClientSecure;
((WiFiClientSecure*)client)->setCACert(certificate);
} else {
client = new WiFiClient;
}
{
// Add a scoping block for HTTPClient https to make sure it is destroyed before WiFiClientSecure *client is
HTTPClient https;
Serial.println("httpsGET|[HTTPS] begin...");
if (https.begin(*client, address)) { // HTTPS
Serial.println("httpsGET|[HTTPS] GET...");
// start connection and send HTTP header
int httpCode = https.GET();
// httpCode will be negative on error
if (httpCode > 0) {
// HTTP header has been send and Server response header has been handled
Serial.println(String("httpsGET|[HTTPS] GET... code: ") + httpCode);
// file found at server
if (httpCode == HTTP_CODE_OK || httpCode == HTTP_CODE_MOVED_PERMANENTLY) {
result = https.getString();
return result;
}
} else {
Serial.println("httpsGET|[HTTPS] GET... failed, error: " + https.errorToString(httpCode));
}
https.end();
} else {
Serial.println("httpsGET|[HTTPS] Unable to connect");
}
// End extra scoping block
}
delete client;
return result;
}
tmElements_t buildTime(int year, int month, int day, int h, int m, int s) {
return {s, m, h, 0, day, month, CalendarYrToTm(year)};
}
tmElements_t parseTime(const char* timeStr) {
int year, month, day, hour, minute, second;
sscanf(timeStr, Constants::TIME_FORMAT, &year, &month, &day, &hour, &minute, &second);
return buildTime(year, month, day, hour, minute, second);
}
tmElements_t parseTime(const char* timeStr, time_t relative) {
int hour, minute, second;
sscanf(timeStr, Constants::TIME_FORMAT_TIME_ONLY, &hour, &minute, &second);
return buildTime(year(relative), month(relative), day(relative), hour, minute, second);
}
StopInfo deserializeApiOutput(const String& data) {
Serial.println("deserializeApiOutput|Deserializing data...");
DynamicJsonDocument doc(16384);
DeserializationError error = deserializeJson(doc, data);
if (error) {
Serial.print("deserializeApiOutput|deserializeJson() failed: ");
Serial.println(error.c_str());
return {};
}
const char* code = doc["code"]; // "1194"
const char* name = doc["name"]; // "УМБАЛ СВ. АННА"
const char* timestamp_calculated = doc["timestamp_calculated"]; // "2022-05-30 23:45:22"
Serial.println(String("deserializeApiOutput|timestamp_calculated: ") + timestamp_calculated);
time_t calculatedTime = timestamp_calculated
? makeTime(parseTime(timestamp_calculated))
: 0;
const JsonArray jlines = doc["lines"];
Serial.println(String("deserializeApiOutput|JLines count: ") + jlines.size());
Vector<Line> lines(LINES);
Serial.println(String("deserializeApiOutput|Vector: ") + lines.size() + " " + lines.max_size());
for (const auto& line : jlines) {
String name = line["name"];
const char* time = line["arrivals"].as<JsonArray>()[0]["time"];
Serial.println(String("deserializeApiOutput|time: ") + time);
if (!time) {
continue;
}
time_t nextArrival = makeTime(parseTime(time, calculatedTime));
Serial.println(String("deserializeApiOutput|Line name: ") + name);
Serial.println(String("deserializeApiOutput|Line next arrival: ") + nextArrival);
lines.push_back({name, nextArrival});
}
Serial.println(String("deserializeApiOutput|Calculated time: ") + calculatedTime);
Serial.println(String("deserializeApiOutput|Lines count: ") + lines.size());
return {lines, calculatedTime, true};
}
StopInfo getStopInfo(const String& stopId) {
String address(Constants::API_ENDPOINT + stopId);
String output = httpsGET(address.c_str(), Constants::CERT);
return deserializeApiOutput(output);
}