diff --git a/simulator/app.py b/simulator/app.py index 2cf4154..cc10fa0 100644 --- a/simulator/app.py +++ b/simulator/app.py @@ -80,6 +80,18 @@ def toggle_sensor_fault(name): return jsonify(response) + +@app.route("/config") +def get_config(): + return jsonify(esp32.load_config()) + + +@app.route("/config", methods=["POST"]) +def save_config(): + config = request.get_json(force=True) + return jsonify(esp32.save_config(config)) + + @app.route("/comms") def comms(): return jsonify(pico.get_comms()) diff --git a/simulator/esp32_sim.py b/simulator/esp32_sim.py index 9a7d68c..bfd47f1 100644 --- a/simulator/esp32_sim.py +++ b/simulator/esp32_sim.py @@ -35,11 +35,19 @@ class ESP32Simulator: self.soc = 82.0 self.last_update = time.time() + def config_path(self): + return Path(__file__).parent / "config.json" + def load_config(self): - config_path = Path(__file__).parent / "config.json" - with config_path.open() as f: + with self.config_path().open() as f: return json.load(f) + def save_config(self, config): + with self.config_path().open("w") as f: + json.dump(config, f, indent=2) + + return self.load_config() + def update_battery(self): now = time.time() elapsed_hours = (now - self.last_update) / 3600 diff --git a/simulator/templates/index.html b/simulator/templates/index.html index c19ddd3..3b96a87 100644 --- a/simulator/templates/index.html +++ b/simulator/templates/index.html @@ -171,15 +171,19 @@ let activeAlarmKey = null; let acknowledgedAlarms = new Set(); let screenWakeTimeout = null; -let alarmConfig = { - rear_seat_warning: 85, - rear_seat_critical: 95, - fridge_zone_1_warm: 45, - fridge_zone_2_warm: 15, - battery_low: 20, - audible_alarms: true +let appConfig = { + alarms: { + rear_seat_warning: 85, + rear_seat_critical: 95, + fridge_zone_1_warm: 45, + fridge_zone_2_warm: 15, + battery_low: 20, + audible_alarms: true + } }; +let alarmConfig = appConfig.alarms; + function showScreen(id) { document.querySelectorAll('.screen').forEach(screen => { screen.classList.remove('active'); @@ -479,9 +483,10 @@ async function enableWifi() { loadStatus(); } -loadAlarmSettings(); -loadStatus(); -setInterval(loadStatus, 2000); +loadAlarmSettings().then(() => { + loadStatus(); + setInterval(loadStatus, 2000); +});