@@ -19,12 +19,15 @@ DBAPI::DBAPI() {
1919 * @param address Address to request nearest stations from - currently not implemented, leave as NULL
2020 * @param num Maxmium of stations to request
2121 * @return DBstation* array, possibly NULL if no results were found
22+ * @note see getError to check if request was successful and is just empty because no station was found
2223 */
2324DBstation* DBAPI::getStation (
2425 const char * name,
2526 const char * address,
2627 uint8_t num
2728 ) {
29+ // Assume all is good at first
30+ err = DBERR_NONE;
2831 while (stations != NULL ) {
2932 DBstation* next = stations->next ;
3033 free (stations);
@@ -44,6 +47,7 @@ DBstation* DBAPI::getStation(
4447 client.setInsecure (); // Don't check fingerprint
4548 if (!client.connect (host, 443 )) {
4649 DB_DEBUG_MSG (" DBAPI: Connection to Host failed.\n " );
50+ err = DBERR_REQ_FAILED;
4751 return NULL ;
4852 }
4953 String json = String (" {\" locationTypes\" :[\" ST\" ],\" searchTerm\" :\" " ) + name + " \" }" ;
@@ -55,17 +59,19 @@ DBstation* DBAPI::getStation(
5559 char endOfHeaders[] = " \r\n\r\n " ;
5660 if (!client.find (endOfHeaders)) {
5761 DB_DEBUG_MSG (" Did not find headers\n " );
62+ err = DBERR_RESP_INVALID;
5863 return stations;
5964 }
6065 JsonDocument doc;
6166 DeserializationError error = deserializeJson (doc, client);
6267 if (error) {
6368 DB_DEBUG_MSG (" deserializeJson() on Station failed" );
6469 DB_DEBUG_MSG (error.c_str ());
70+ err = DBERR_DESERIALIZATION_FAILED;
6571 return stations;
6672 }
6773 DBstation* prev = NULL ;
68- for (uint8_t i = 0 ; i < doc.size (); i++) {
74+ for (uint8_t i = 0 ; i < doc.size () && i < num ; i++) {
6975 DBstation* station = new DBstation ();
7076 JsonObject st = doc[i];
7177 String stationname = st[" name" ];
@@ -120,6 +126,17 @@ DBstation* DBAPI::getStationByCoord(
120126 return NULL ;
121127}
122128
129+ /* *
130+ * Get last errorcode to check if the request was successful.
131+ * Most likely to be useful, when NULL is returned, but it can't be distinguished,
132+ * if the request was malformed, any function or request failed or there are simply
133+ * no more results available with the given parameters.
134+ * @return DBerror enum errorcode
135+ */
136+ DBerror DBAPI::getError () {
137+ return err;
138+ }
139+
123140/* *
124141 * Requests departures/arrivals from/at given stationID.
125142 * @param type abfahrt or ankunft
@@ -130,6 +147,7 @@ DBstation* DBAPI::getStationByCoord(
130147 * @param maxDuration maximum of hours to request (each hour will possibly generate a new http request)
131148 * @param productFilter any combination of DBprod values for service types to request
132149 * @return DBdeparr array, possibly NULL if no service is available with the requested parameters or an error occured
150+ * @note see getError to check if request was successful and is just empty because no service is available
133151 */
134152DBdeparr* DBAPI::getStationBoard (
135153 const char type[8 ],
@@ -140,8 +158,12 @@ DBdeparr* DBAPI::getStationBoard(
140158 uint8_t maxDuration,
141159 uint16_t productFilter
142160 ) {
161+ // Assume all is good at first
162+ err = DBERR_NONE;
143163 // sanity check, if no station is supplied, a request would crash ArduinJSON later.
144164 if (stationId == NULL || !strlen (stationId)) {
165+ DB_DEBUG_MSG (" DBAPI: StationID was not supplied.\n " );
166+ err = DBERR_NOSTATIONID;
145167 return NULL ;
146168 }
147169 while (deparr != NULL ) {
@@ -196,6 +218,7 @@ DBdeparr* DBAPI::getStationBoard(
196218 if (!client.connect (host, 443 )) {
197219 DB_DEBUG_MSG (" DBAPI: Connection to Host failed.\n " );
198220 free (output);
221+ err = DBERR_REQ_FAILED;
199222 return NULL ;
200223 }
201224
@@ -211,19 +234,22 @@ DBdeparr* DBAPI::getStationBoard(
211234 free (output);
212235 char endOfHeaders[] = " \r\n\r\n " ;
213236 if (!client.find (endOfHeaders)) {
214- DB_DEBUG_MSG (" Did not find headers\n " );
237+ DB_DEBUG_MSG (" DBAPI: Did not find headers\n " );
238+ err = DBERR_RESP_INVALID;
239+ // Return so far accumulated array
215240 return deparr;
216241 }
217242 JsonDocument doc;
218243 if (!client.find (" :[" )) { // Skip to first element
244+ err = DBERR_NO_JSON_FOUND;
219245 return deparr;
220246 }
221247 do {
222248 DeserializationError error = deserializeJson (doc, client);
223249 if (error) {
224- DB_DEBUG_MSG (" deserializeJson() on departures/arrivals failed" );
225250 DB_DEBUG_MSG (" DBAPI: deserializeJson() on departures/arrivals failed" );
226251 DB_DEBUG_MSG (error.c_str ());
252+ // err = DBERR_DESERIALIZATION_FAILED;
227253 // return deparr;
228254 // No data in array, continue with next hour if selected
229255 continue ;
0 commit comments