Skip to content

Commit c4de3b5

Browse files
committed
Send error 500 on CAN com error when fetching json #23
1 parent a0255ad commit c4de3b5

File tree

3 files changed

+14
-7
lines changed

3 files changed

+14
-7
lines changed

esp32-web-interface.ino

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -472,7 +472,8 @@ static void handleCommand() {
472472
digitalWrite(LED_BUILTIN, HIGH);
473473

474474
if (cmd == "json") {
475-
OICan::SendJson(server.client());
475+
if (!OICan::SendJson(server.client()))
476+
server.send(500, "text/plain", "CAN communication error");
476477
}
477478
else if (cmd.startsWith("set")) {
478479
String str(cmd);

src/oi_can.cpp

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -325,8 +325,8 @@ int GetCurrentUpdatePage() {
325325
return currentPage;
326326
}
327327

328-
void SendJson(WiFiClient client) {
329-
if (state != IDLE) return;
328+
bool SendJson(WiFiClient client) {
329+
if (state != IDLE) return false;
330330

331331
DynamicJsonDocument doc(30000);
332332
twai_message_t rxframe;
@@ -338,10 +338,11 @@ void SendJson(WiFiClient client) {
338338
if (result != DeserializationError::Ok) {
339339
SPIFFS.remove(jsonFileName); //if json file is invalid, remove it and trigger re-download
340340
updstate == REQUEST_JSON;
341-
return;
341+
return false;
342342
}
343343

344344
JsonObject root = doc.as<JsonObject>();
345+
int failed = 0;
345346

346347
for (JsonPair kv : root) {
347348
int id = kv.value()["id"].as<int>();
@@ -351,11 +352,16 @@ void SendJson(WiFiClient client) {
351352

352353
if (twai_receive(&rxframe, pdMS_TO_TICKS(10)) == ESP_OK) {
353354
kv.value()["value"] = ((double)*(int32_t*)&rxframe.data[4]) / 32;
355+
} else {
356+
failed++;
354357
}
355358
}
356359
}
357-
WriteBufferingStream bufferedWifiClient{client, 1000};
358-
serializeJson(doc, bufferedWifiClient);
360+
if (failed < 5) {
361+
WriteBufferingStream bufferedWifiClient{client, 1000};
362+
serializeJson(doc, bufferedWifiClient);
363+
}
364+
return failed < 5;
359365
}
360366

361367
void SendCanMapping(WiFiClient client) {

src/oi_can.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ enum SetResult { Ok, UnknownIndex, ValueOutOfRange, CommError };
2727

2828
void Init(uint8_t nodeId);
2929
void Loop();
30-
void SendJson(WiFiClient c);
30+
bool SendJson(WiFiClient c);
3131
void SendCanMapping(WiFiClient c);
3232
SetResult AddCanMapping(String json);
3333
SetResult RemoveCanMapping(String json);

0 commit comments

Comments
 (0)