mirror of
https://github.com/l5yth/potato-mesh.git
synced 2026-07-13 21:31:09 +02:00
feat: load nodes from json for tests (#8)
* test: verify web query uses populated db * test: add test script
This commit is contained in:
+20
-5
@@ -1,12 +1,17 @@
|
||||
import json, sqlite3, time, threading
|
||||
import json, os, sqlite3, time, threading
|
||||
from dataclasses import asdict, is_dataclass
|
||||
from pathlib import Path
|
||||
from meshtastic.serial_interface import SerialInterface
|
||||
from meshtastic.mesh_interface import MeshInterface
|
||||
|
||||
DB = "nodes.db"
|
||||
try: # meshtastic is optional for tests
|
||||
from meshtastic.serial_interface import SerialInterface
|
||||
from meshtastic.mesh_interface import MeshInterface
|
||||
except ModuleNotFoundError: # pragma: no cover - imported lazily for hardware usage
|
||||
SerialInterface = None # type: ignore
|
||||
MeshInterface = None # type: ignore
|
||||
|
||||
schema = Path("nodes.sql").read_text()
|
||||
DB = os.environ.get("MESH_DB", "nodes.db")
|
||||
|
||||
schema = Path(__file__).with_name("nodes.sql").read_text()
|
||||
conn = sqlite3.connect(DB, check_same_thread=False)
|
||||
conn.executescript(schema)
|
||||
conn.commit()
|
||||
@@ -76,6 +81,14 @@ def upsert_node(node_id, n):
|
||||
altitude=excluded.altitude, node_json=excluded.node_json
|
||||
""", row)
|
||||
|
||||
|
||||
def load_nodes_from_file(path: str | Path):
|
||||
"""Populate the database from a nodes.json file."""
|
||||
nodes = json.loads(Path(path).read_text())
|
||||
for node_id, node in nodes.items():
|
||||
upsert_node(node_id, node)
|
||||
conn.commit()
|
||||
|
||||
def snapshot_nodes_periodically(iface: MeshInterface, every_sec=30):
|
||||
time.sleep(5) # let the library sync initial node DB
|
||||
while True:
|
||||
@@ -88,6 +101,8 @@ def snapshot_nodes_periodically(iface: MeshInterface, every_sec=30):
|
||||
time.sleep(every_sec)
|
||||
|
||||
def main():
|
||||
if SerialInterface is None:
|
||||
raise RuntimeError("meshtastic library not installed")
|
||||
iface = SerialInterface(devPath="/dev/ttyACM0")
|
||||
threading.Thread(target=snapshot_nodes_periodically, args=(iface, 30), daemon=True).start()
|
||||
print("Nodes ingestor running. Ctrl+C to stop.")
|
||||
|
||||
Reference in New Issue
Block a user