This code is fault tolerant

This commit is contained in:
Danny Staple 2022-04-03 23:08:22 +01:00
parent c9b57659dc
commit 81e44ce35b
2 changed files with 38 additions and 24 deletions

View File

@ -8,8 +8,11 @@
var current_dataset = [];
while (true) {
try {
const controller = new AbortController();
const timeoutId = setTimeout(() => controller.abort(), 1000);
// fetch new data
await d3.json("/data").then(function(data) {
await d3.json("/data", {signal: controller.signal}).then(function(data) {
// then append to list
current_dataset.push(data);
// Map to a 10s (n S) sliding window
@ -31,6 +34,10 @@
var graphNode = document.getElementById("value");
graphNode.replaceChild(value_graph, graphNode.firstChild);
});
clearTimeout(timeoutId);
} catch (error) {
console.log(error.name === 'AbortError');
}
}
// }
</script>

View File

@ -1,5 +1,6 @@
import time
import json
import traceback
from adafruit_esp32spi import adafruit_esp32spi_wsgiserver
from adafruit_wsgi.wsgi_app import WSGIApp
@ -16,8 +17,10 @@ class RandomWalkSensors:
self.start_time = time.monotonic()
self.last_time = self.start_time
# self.app = None
def setup_wifi(self, app):
# self.app = app
print("Setting up wifi.")
self.wifi, esp = robot_wifi.connect_to_wifi()
self.server = adafruit_esp32spi_wsgiserver.WSGIServer(80, application=app)
@ -28,6 +31,10 @@ class RandomWalkSensors:
ip_int = ".".join(str(int(n)) for n in esp.ip_address)
print(f"IP Address is {ip_int}")
def reconnect(self):
self.wifi.connect()
self.server.start()
def data(self, request):
return (
@ -66,9 +73,9 @@ class RandomWalkSensors:
self.update()
self.server.update_poll()
except RuntimeError as e:
print(f"Server poll error: {type(e)}, {e}")
print(f"Resetting ESP...")
traceback.print_exception(BaseException, e, e.__traceback__)
self.wifi.reset()
self.reconnect()
print("Reset complete.")
def start(self):