lte service (backend) and ui handling

This commit is contained in:
Mikkeli Matlock
2026-02-09 02:36:03 +09:00
parent 12a0d58800
commit 47b3427e63
5 changed files with 261 additions and 3 deletions

View File

@@ -9,6 +9,7 @@ from flask_socketio import SocketIO, emit
from gps_service import GPSService
from arduino_service import ArduinoService
from gpio_service import GPIOService
from lte_service import LteService
from throttle import Throttle
app = Flask(__name__)
@@ -21,10 +22,12 @@ socketio = SocketIO(app, async_mode="gevent", cors_allowed_origins="*")
gps = GPSService()
arduino = ArduinoService()
gpio = GPIOService()
lte = LteService()
# Throttles for emission rate limiting (20Hz for arduino, 1Hz for GPS)
# Throttles for emission rate limiting (20Hz for arduino, 1Hz for GPS, 5s for LTE)
arduino_throttle = Throttle(min_interval=0.05) # 20Hz max
gps_throttle = Throttle(min_interval=1.0) # 1Hz max
lte_throttle = Throttle(min_interval=5.0) # Every 5s — signal doesn't need real-time
# Track connected clients
connected_clients = set()
@@ -57,6 +60,10 @@ def handle_connect():
if "error" not in gps_data:
emit("gps", gps_data)
lte_data = lte.get_latest()
if "error" not in lte_data:
emit("lte", lte_data)
@socketio.on("disconnect")
def handle_disconnect():
@@ -147,6 +154,14 @@ def on_gps_data(data):
gps_throttle.maybe_emit(data, emit_fn)
def on_lte_data(data):
"""Called by LteService when new status polled."""
def emit_fn(d):
socketio.emit("lte", d)
lte_throttle.maybe_emit(data, emit_fn)
def on_arduino_ack(cmd, status, extra):
"""Called by ArduinoService when ACK received from Arduino."""
socketio.emit("ack", {
@@ -172,6 +187,9 @@ def throttle_flusher():
if gps_throttle.has_pending:
gps_throttle.flush(lambda d: socketio.emit("gps", d))
if lte_throttle.has_pending:
lte_throttle.flush(lambda d: socketio.emit("lte", d))
# -----------------------------------------------------------------------------
# REST API (backward compatibility)
@@ -181,11 +199,14 @@ def throttle_flusher():
def health():
"""Health check endpoint."""
gps_latest = gps.get_latest()
lte_latest = lte.get_latest()
return jsonify({
"status": "ok",
"gps_connected": gps.connected,
"gps_state": gps_latest.get("gps_state", "acquiring"),
"arduino_connected": arduino.connected,
"lte_connected": lte.connected,
"lte_signal": lte_latest.get("signal"),
"ws_clients": len(connected_clients),
})
@@ -202,6 +223,12 @@ def gps_history():
return jsonify(gps.get_buffer())
@app.route("/lte")
def lte_data():
"""Current LTE modem status."""
return jsonify(lte.get_latest())
@app.route("/arduino")
def arduino_data():
"""Current Arduino telemetry (voltage, rpm, etc)."""
@@ -224,11 +251,13 @@ def main():
arduino.set_on_data(on_arduino_data)
arduino.set_on_ack(on_arduino_ack)
gps.set_on_data(on_gps_data)
lte.set_on_data(on_lte_data)
# Start services
gps.start()
arduino.start()
gpio.start()
lte.start()
# Start throttle flusher in background
socketio.start_background_task(throttle_flusher)
@@ -241,6 +270,7 @@ def main():
arduino.stop()
gps.stop()
gpio.stop()
lte.stop()
if __name__ == "__main__":