-
Notifications
You must be signed in to change notification settings - Fork 4
/
DBAPI.cpp
260 lines (245 loc) · 7.4 KB
/
DBAPI.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
255
256
257
258
259
260
#include "DBAPI.h"
#include <WiFiClientSecure.h>
#ifdef DEBUG_ESP_PORT
#define DB_DEBUG_MSG(...) DEBUG_ESP_PORT.printf( __VA_ARGS__ )
#else
#define DB_DEBUG_MSG(...)
#endif
DBAPI::DBAPI() {
}
String DBAPI::getXMLParam(String haystack, const char* param) {
String p = String(param) + "=\"";
int16_t pos = haystack.indexOf(p);
if (pos == -1) {
return String();
}
pos += p.length();
return haystack.substring(pos, haystack.indexOf('"', pos));
}
String DBAPI::getIParam(String haystack, const char* param) {
String p = String(param) + "=";
int16_t pos = haystack.indexOf(p);
if (pos == -1) {
return String();
}
pos += p.length();
return haystack.substring(pos, haystack.indexOf('@', pos));
}
DBstation* DBAPI::getStation(
const char* name,
const char* address,
uint8_t num
) {
while (stations != NULL) {
DBstation* next = stations->next;
free(stations);
stations = next;
}
bool isStation;
if (name != NULL) {
isStation = true;
} else if (address != NULL) {
isStation = false;
} else {
return NULL;
}
WiFiClientSecure client;
client.setInsecure(); // Don't check fingerprint
if (!client.connect(host, 443)) {
DB_DEBUG_MSG("DBAPI: Connection to Host failed.\n");
return NULL;
}
String xml = String("<?xml version=\"1.0\" encoding=\"utf-8\" ?>\r\n<ReqC ver=\"1.1\" prod=\"String\" lang=\"DE\">\r\n") +
"<LocValReq id=\"001\" maxNr=\"" + num + "\" sMode=\"1\">\r\n" +
"<ReqLoc type=\"" + (isStation ? "ST" : "ALLTYPE") + "\" match=\"" + (isStation ? name : address) + "\" />\r\n" +
"</LocValReq>\r\n" +
"</ReqC>";
client.print(String("POST /bin/query.exe/dn HTTP/1.1\r\nConnection: close\r\nContent-Type: text/xml\r\nHost: ") + host +
"\r\nContent-Length: " + xml.length() + "\r\n\r\n" +
xml
);
while (client.connected()) {
String line = client.readStringUntil('\n');
if (line == "\r") {
break;
}
}
DBstation* prev = NULL;
while (client.available()) {
while (client.available()) {
if (client.read() == '<') {
if (client.read() == 'S') {
DB_DEBUG_MSG("DBAPI: Parsing new Station.\n");
break; // hopefully got <S (tation >) beginning
}
}
}
if (!client.available()) break;
DBstation* station = new DBstation();
String st = client.readStringUntil('>');
getXMLParam(st, "name").toCharArray(station->name, sizeof(DBstation::name));
getXMLParam(st, "externalStationNr").toCharArray(station->stationId, sizeof(DBstation::stationId));
station->longitude = atol(getXMLParam(st, "x").c_str());
station->latitude = atol(getXMLParam(st, "y").c_str());
station->next = NULL;
if (prev == NULL) {
stations = station;
} else {
prev->next = station;
}
prev = station;
}
return stations;
}
DBstation* DBAPI::getStationByCoord(
uint32_t latitude,
uint32_t longitude,
uint8_t num,
uint16_t maxDistance
) {
//String("https://reiseauskunft.bahn.de/bin/query.exe/dol?performLocating=2&L=vs_java&look_nv=del_doppelt%7Cyes&look_x=" + longitude + "&look_y=" + latitude + "&look_maxno=" + num + "0&look_maxdist=" + maxDistance);
return NULL;
}
DBdeparr* DBAPI::getStationBoard(
const char type[4],
const char* stationId,
const char* target,
const char* Dtime,
const char* Ddate,
uint8_t num,
uint16_t productFilter
) {
while (deparr != NULL) {
DBdeparr* next = deparr->next;
free(deparr);
deparr = next;
}
String qString;
if (Dtime != NULL) {
qString += String("&time=") + Dtime;
if (Ddate != NULL) {
qString += String("&date=") + Ddate;
}
}
if (target != NULL) {
qString += String("&dirInput=") + target;
}
if (num != 0) {
qString += String("&maxJourneys=") + num;
}
char products[11];
for (uint8_t i = 0; i < 10; i++)
products[i] = productFilter & (1 << (9 - i)) ? '1' : '0';
products[10] = '\0';
WiFiClientSecure client;
client.setInsecure(); // Don't check fingerprint
if (!client.connect(host, 443)) {
DB_DEBUG_MSG("DBAPI: Connection to Host failed.\n");
return NULL;
}
DB_DEBUG_MSG("DBAPI: Requesting StationBoard.\n");
client.print(String("GET /bin/stboard.exe/dn?start=yes&L=vs_java3&productsFilter=") + products + "&input=" + stationId + "&boardType=" + type + qString + " HTTP/1.1\r\n" +
"Host: " + host + "\r\n" +
"Connection: close\r\n\r\n");
while (client.connected()) {
String line = client.readStringUntil('\n');
if (line == "\r") {
break;
}
}
DBdeparr* prev = NULL;
while (client.available()) {
while (client.available()) {
if (client.read() == '<') {
if (client.read() == 'J') {
DB_DEBUG_MSG("DBAPI: Parsing new Journey.\n");
break; // hopefully got <J (ourney >) beginning
}
}
}
if (!client.available()) {
break;
}
DBdeparr* da = new DBdeparr();
String journey = client.readStringUntil('>');
getXMLParam(journey, "fpTime").toCharArray(da->time, sizeof(DBdeparr::time));
getXMLParam(journey, "fpDate").toCharArray(da->date, sizeof(DBdeparr::date));
String targ = getXMLParam(journey, "targetLoc");
targ.replace("(", "(");
targ.replace(")", ")");
// Replace Umlauts for Adafruit GFX
targ.replace("\xdf", agfx ? "\xE0" : "ss");
targ.replace("\xc4", agfx ? "\x8E" : "Ae");
targ.replace("\xd6", agfx ? "\x99" : "Oe");
targ.replace("\xdc", agfx ? "\x9A" : "Ue");
targ.replace("\xe4", agfx ? "\x84" : "ae");
targ.replace("\xf6", agfx ? "\x94" : "oe");
targ.replace("\xfc", agfx ? "\x81" : "ue");
targ.toCharArray(da->target, sizeof(DBdeparr::target));
String prod = getXMLParam(journey, "prod");
// Extract number from prod tag
uint8_t chpos = 255;
for (char c = '1'; c <= '9'; c++) {
uint8_t pos = prod.indexOf(c);
if (pos < chpos) {
chpos = pos;
}
}
if ((uint8_t)prod.indexOf(' ') < chpos) {
chpos = prod.indexOf(' ');
}
// Extract line
da->line = atol(prod.substring(chpos, prod.indexOf('#')).c_str());
// Lines could be text like SEV, so also outputting the textform
// For backwards compatibility not replacing line param
String textline = prod.substring(chpos, prod.indexOf('#'));
textline.trim();
textline.toCharArray(da->textline, sizeof(DBdeparr::textline));
// Reduce to product name
prod = prod.substring(0, chpos);
prod.trim();
// If no number found, clip #...
prod.substring(0, prod.indexOf('#')).toCharArray(da->product, sizeof(DBdeparr::product));
// Extract delay String
getXMLParam(journey, "delay").toCharArray(da->textdelay, sizeof(DBdeparr::textdelay));
// Extract numeric delay
da->delay = atoi(getXMLParam(journey, "e_delay").c_str());
// Extract platform
getXMLParam(journey, "platform").toCharArray(da->platform, sizeof(DBdeparr::platform));
// Extract changed platform if there is any
getXMLParam(journey, "newpl").toCharArray(da->newPlatform, sizeof(DBdeparr::newPlatform));
da->next = NULL;
if (prev == NULL) {
DB_DEBUG_MSG("DBAPI: Got first depature.");
deparr = da;
} else {
DB_DEBUG_MSG("DBAPI: Got next depature.");
prev->next = da;
}
prev = da;
}
return deparr;
}
DBdeparr* DBAPI::getDepatures(
const char* stationId,
const char* target,
const char* Dtime,
const char* Ddate,
uint8_t num,
uint16_t productFilter
) {
return getStationBoard("dep", stationId, target, Dtime, Ddate, num, productFilter);
}
DBdeparr* DBAPI::getArrivals(
const char* stationId,
const char* target,
const char* Dtime,
const char* Ddate,
uint8_t num,
uint16_t productFilter
) {
return getStationBoard("arr", stationId, target, Dtime, Ddate, num, productFilter);
}
void DBAPI::setAGFXOutput(bool gfx) {
agfx = gfx;
}