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,29 +8,36 @@
var current_dataset = []; var current_dataset = [];
while (true) { while (true) {
// fetch new data try {
await d3.json("/data").then(function(data) { const controller = new AbortController();
// then append to list const timeoutId = setTimeout(() => controller.abort(), 1000);
current_dataset.push(data); // fetch new data
// Map to a 10s (n S) sliding window await d3.json("/data", {signal: controller.signal}).then(function(data) {
// ie find the most recent value, then reduce/filter anything more than 10s earlier than that. // then append to list
const most_recent_time = current_dataset[current_dataset.length - 1].time; current_dataset.push(data);
const window_start = Math.max(0, most_recent_time - 10); // Map to a 10s (n S) sliding window
// filter the list // ie find the most recent value, then reduce/filter anything more than 10s earlier than that.
current_dataset = current_dataset.filter( const most_recent_time = current_dataset[current_dataset.length - 1].time;
value => value.time >= window_start const window_start = Math.max(0, most_recent_time - 10);
); // filter the list
// render dist graph current_dataset = current_dataset.filter(
var value_graph = Plot.plot({ value => value.time >= window_start
x: { grid: true}, );
y: { grid: true}, // render dist graph
marks: [ var value_graph = Plot.plot({
Plot.line(current_dataset, {x: "time", y: "value", stroke: "red"}), x: { grid: true},
] y: { grid: true},
marks: [
Plot.line(current_dataset, {x: "time", y: "value", stroke: "red"}),
]
});
var graphNode = document.getElementById("value");
graphNode.replaceChild(value_graph, graphNode.firstChild);
}); });
var graphNode = document.getElementById("value"); clearTimeout(timeoutId);
graphNode.replaceChild(value_graph, graphNode.firstChild); } catch (error) {
}); console.log(error.name === 'AbortError');
}
} }
// } // }
</script> </script>

View File

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