main.py 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486
  1. #!/usr/bin/env python3
  2. """SpoolBuddy daemon — reads NFC tags and scale, pushes events to Bambuddy backend."""
  3. import asyncio
  4. import logging
  5. import os
  6. import socket
  7. import subprocess
  8. import sys
  9. import time
  10. from pathlib import Path
  11. from . import __version__, system_stats
  12. from .api_client import APIClient
  13. from .config import Config
  14. from .display_control import DisplayControl
  15. from .nfc_reader import NFCReader, NFCState
  16. from .scale_reader import ScaleReader
  17. logging.basicConfig(
  18. level=logging.INFO,
  19. format="%(asctime)s [%(name)s] %(levelname)s: %(message)s",
  20. datefmt="%H:%M:%S",
  21. )
  22. logger = logging.getLogger("spoolbuddy")
  23. logging.getLogger("daemon.pn5180").setLevel(logging.DEBUG)
  24. def _spoolbuddy_env_path() -> Path:
  25. # installer writes this at <install>/spoolbuddy/.env; allow override for custom setups/tests
  26. override = os.environ.get("SPOOLBUDDY_ENV_FILE", "").strip()
  27. if override:
  28. return Path(override)
  29. return Path(__file__).resolve().parent.parent / ".env"
  30. def _set_env_value(path: Path, key: str, value: str):
  31. lines: list[str] = []
  32. if path.exists():
  33. lines = path.read_text(encoding="utf-8").splitlines()
  34. updated = False
  35. new_lines: list[str] = []
  36. for line in lines:
  37. if line.startswith(f"{key}="):
  38. new_lines.append(f"{key}={value}")
  39. updated = True
  40. else:
  41. new_lines.append(line)
  42. if not updated:
  43. new_lines.append(f"{key}={value}")
  44. path.parent.mkdir(parents=True, exist_ok=True)
  45. path.write_text("\n".join(new_lines) + "\n", encoding="utf-8")
  46. def _get_ip() -> str:
  47. try:
  48. s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
  49. s.connect(("8.8.8.8", 80))
  50. ip = s.getsockname()[0]
  51. s.close()
  52. return ip
  53. except Exception:
  54. return "unknown"
  55. SSH_KEY_TAG = "bambuddy-spoolbuddy"
  56. def _deploy_ssh_key(public_key: str) -> None:
  57. """Sync Bambuddy's SSH public key into authorized_keys.
  58. Replaces any prior key tagged ``bambuddy-spoolbuddy`` so the file always
  59. reflects Bambuddy's *current* keypair. Without this, every Bambuddy key
  60. rotation (data dir wipe, container recreate, etc.) leaves a stale entry
  61. behind and the file grows unbounded.
  62. """
  63. target = public_key.strip()
  64. home = Path.home()
  65. ssh_dir = home / ".ssh"
  66. auth_keys = ssh_dir / "authorized_keys"
  67. try:
  68. ssh_dir.mkdir(mode=0o700, exist_ok=True)
  69. existing_lines: list[str] = []
  70. if auth_keys.exists():
  71. existing_lines = auth_keys.read_text().splitlines()
  72. kept = [line for line in existing_lines if SSH_KEY_TAG not in line]
  73. new_lines = kept + [target]
  74. # Already in sync — current key present and no stale Bambuddy entries.
  75. if existing_lines == new_lines:
  76. return
  77. auth_keys.write_text("\n".join(new_lines) + "\n")
  78. auth_keys.chmod(0o600)
  79. removed = len(existing_lines) - len(kept)
  80. if removed:
  81. logger.info("SSH public key updated in %s (replaced %d stale entries)", auth_keys, removed)
  82. else:
  83. logger.info("SSH public key deployed to %s", auth_keys)
  84. except Exception as e:
  85. logger.warning("Failed to deploy SSH key: %s", e)
  86. async def nfc_poll_loop(config: Config, api: APIClient, shared: dict):
  87. """Continuous NFC polling loop — runs in asyncio with blocking reads offloaded."""
  88. display: DisplayControl = shared["display"]
  89. try:
  90. while True:
  91. if shared.get("nfc_scan_paused", False):
  92. await asyncio.sleep(config.nfc_poll_interval)
  93. continue
  94. nfc: NFCReader | None = shared.get("nfc")
  95. if not nfc or not nfc.ok:
  96. await asyncio.sleep(config.nfc_poll_interval)
  97. continue
  98. event_type, event_data = await asyncio.to_thread(nfc.poll)
  99. if event_type == "tag_detected":
  100. display.wake()
  101. await api.tag_scanned(
  102. device_id=config.device_id,
  103. tag_uid=event_data["tag_uid"],
  104. tray_uuid=event_data.get("tray_uuid"),
  105. sak=event_data.get("sak"),
  106. tag_type=event_data.get("tag_type"),
  107. )
  108. elif event_type == "tag_removed":
  109. await api.tag_removed(
  110. device_id=config.device_id,
  111. tag_uid=event_data["tag_uid"],
  112. )
  113. # Check for pending write command
  114. pending = shared.get("pending_write")
  115. if pending and nfc.state == NFCState.TAG_PRESENT:
  116. if nfc.current_sak in (0x00, 0x04):
  117. logger.info("Executing pending tag write for spool %d", pending["spool_id"])
  118. success, msg = await asyncio.to_thread(nfc.write_ntag, pending["ndef_data"])
  119. await api.write_tag_result(
  120. device_id=config.device_id,
  121. spool_id=pending["spool_id"],
  122. tag_uid=nfc.current_uid or "",
  123. success=success,
  124. message=msg,
  125. )
  126. shared.pop("pending_write", None)
  127. else:
  128. # Fail fast when a non-NTAG is presented during write mode.
  129. # Without this, UI can appear stuck on "waiting for SpoolBuddy".
  130. sak = nfc.current_sak
  131. await api.write_tag_result(
  132. device_id=config.device_id,
  133. spool_id=pending["spool_id"],
  134. tag_uid=nfc.current_uid or "",
  135. success=False,
  136. message=f"Incompatible tag type (SAK=0x{sak:02X}). Place an NTAG tag to write.",
  137. )
  138. logger.warning(
  139. "Write aborted for spool %d: incompatible tag type SAK=0x%02X",
  140. pending["spool_id"],
  141. sak,
  142. )
  143. shared.pop("pending_write", None)
  144. await asyncio.sleep(config.nfc_poll_interval)
  145. finally:
  146. nfc: NFCReader | None = shared.get("nfc")
  147. if nfc:
  148. nfc.close()
  149. async def scale_poll_loop(config: Config, api: APIClient, shared: dict):
  150. """Continuous scale reading loop — reads at 100ms, reports at 1s intervals."""
  151. scale: ScaleReader = shared["scale"]
  152. display: DisplayControl = shared["display"]
  153. if not scale.ok:
  154. logger.warning("Scale not available, skipping scale polling")
  155. return
  156. last_report = 0.0
  157. last_reported_grams: float | None = None
  158. last_wake_grams: float | None = None
  159. REPORT_THRESHOLD = 2.0 # Only report if weight changed by more than this (grams)
  160. WAKE_THRESHOLD = 50.0 # Only wake display on large changes (spool placed/removed, not sensor bounce)
  161. try:
  162. while True:
  163. result = await asyncio.to_thread(scale.read)
  164. if result is not None:
  165. grams, stable, raw_adc = result
  166. now = time.monotonic()
  167. if now - last_report >= config.scale_report_interval:
  168. # Only send when weight changed meaningfully
  169. weight_changed = last_reported_grams is None or abs(grams - last_reported_grams) >= REPORT_THRESHOLD
  170. if weight_changed:
  171. # Wake display only on STABLE large weight changes (spool
  172. # placed/removed). Without the stability gate, a noisy load
  173. # cell that bounces ≥50g around a midpoint repeatedly trips
  174. # the threshold AND advances last_wake_grams to the noisy
  175. # value, so the next bounce back also exceeds the threshold
  176. # — wake fires every few seconds forever and the kiosk
  177. # screen never stays blanked. The `stable` flag is True
  178. # only when readings agree within 2g over a 1s window, so
  179. # gating on it ensures last_wake_grams only advances to
  180. # settled values.
  181. wake_changed = stable and (
  182. last_wake_grams is None or abs(grams - last_wake_grams) >= WAKE_THRESHOLD
  183. )
  184. if wake_changed:
  185. display.wake()
  186. last_wake_grams = grams
  187. await api.scale_reading(
  188. device_id=config.device_id,
  189. weight_grams=grams,
  190. stable=stable,
  191. raw_adc=raw_adc,
  192. )
  193. last_reported_grams = grams
  194. last_report = now
  195. await asyncio.sleep(config.scale_read_interval)
  196. finally:
  197. scale.close()
  198. async def heartbeat_loop(config: Config, api: APIClient, start_time: float, shared: dict):
  199. """Periodic heartbeat to keep device registered and pick up commands."""
  200. display: DisplayControl = shared["display"]
  201. ip = _get_ip()
  202. while True:
  203. await asyncio.sleep(config.heartbeat_interval)
  204. nfc = shared.get("nfc")
  205. scale = shared.get("scale")
  206. uptime = int(time.monotonic() - start_time)
  207. stats = await asyncio.to_thread(system_stats.collect)
  208. result = await api.heartbeat(
  209. device_id=config.device_id,
  210. nfc_ok=nfc.ok if nfc else False,
  211. scale_ok=scale.ok if scale else False,
  212. uptime_s=uptime,
  213. ip_address=ip,
  214. firmware_version=__version__,
  215. nfc_reader_type=nfc.reader_type if nfc else None,
  216. nfc_connection=nfc.connection if nfc else None,
  217. backend_url=config.backend_url,
  218. system_stats=stats,
  219. )
  220. if result:
  221. ssh_key = result.get("ssh_public_key")
  222. if ssh_key:
  223. _deploy_ssh_key(ssh_key)
  224. cmd = result.get("pending_command")
  225. if cmd == "tare":
  226. scale = shared.get("scale")
  227. if scale and scale.ok:
  228. new_offset = await asyncio.to_thread(scale.tare)
  229. logger.info("Tare executed: offset=%d", new_offset)
  230. await api.update_tare(config.device_id, new_offset)
  231. config.tare_offset = new_offset
  232. else:
  233. logger.warning("Tare command received but scale not available")
  234. # Skip calibration sync — this heartbeat response predates the tare
  235. continue
  236. elif cmd == "apply_system_config":
  237. payload = result.get("pending_system_payload") or {}
  238. backend_url = str(payload.get("backend_url", "")).strip()
  239. api_key_value = payload.get("api_key")
  240. api_key = str(api_key_value).strip() if api_key_value is not None else ""
  241. if not backend_url:
  242. await api.system_command_result(
  243. config.device_id,
  244. "apply_system_config",
  245. False,
  246. "Missing backend_url payload",
  247. )
  248. continue
  249. try:
  250. env_path = _spoolbuddy_env_path()
  251. await asyncio.to_thread(_set_env_value, env_path, "SPOOLBUDDY_BACKEND_URL", backend_url)
  252. if api_key:
  253. await asyncio.to_thread(_set_env_value, env_path, "SPOOLBUDDY_API_KEY", api_key)
  254. await api.system_command_result(
  255. config.device_id,
  256. "apply_system_config",
  257. True,
  258. f"Updated {env_path}",
  259. )
  260. logger.info("Applied system config update")
  261. except Exception as e:
  262. logger.exception("Failed to apply system config")
  263. await api.system_command_result(
  264. config.device_id,
  265. "apply_system_config",
  266. False,
  267. str(e),
  268. )
  269. continue
  270. elif cmd in ("run_nfc_diag", "run_scale_diag", "run_read_tag_diag"):
  271. if cmd == "run_scale_diag":
  272. diagnostic = "scale"
  273. script_name = "scale_diag.py"
  274. elif cmd == "run_read_tag_diag":
  275. diagnostic = "read_tag"
  276. script_name = "read_tag.py"
  277. else:
  278. diagnostic = "nfc"
  279. script_name = "pn5180_diag.py"
  280. script_path = Path(__file__).resolve().parent.parent / "scripts" / script_name
  281. if diagnostic in ("nfc", "read_tag"):
  282. logger.info("Pausing NFC continuous scan for diagnostic")
  283. shared["nfc_scan_paused"] = True
  284. nfc_for_diag = shared.get("nfc")
  285. if nfc_for_diag:
  286. await asyncio.to_thread(nfc_for_diag.close)
  287. shared["nfc"] = None
  288. logger.info("Running %s diagnostic via %s", diagnostic, script_path)
  289. try:
  290. proc = await asyncio.to_thread(
  291. subprocess.run,
  292. [sys.executable, str(script_path)],
  293. capture_output=True,
  294. text=True,
  295. timeout=45,
  296. )
  297. output = (proc.stdout or "") + (("\n" + proc.stderr) if proc.stderr else "")
  298. await api.diagnostic_result(
  299. config.device_id,
  300. diagnostic,
  301. proc.returncode == 0,
  302. output,
  303. proc.returncode,
  304. )
  305. except subprocess.TimeoutExpired:
  306. await api.diagnostic_result(
  307. config.device_id,
  308. diagnostic,
  309. False,
  310. "Diagnostic timed out after 45 seconds",
  311. -1,
  312. )
  313. except Exception as e:
  314. await api.diagnostic_result(
  315. config.device_id,
  316. diagnostic,
  317. False,
  318. f"Diagnostic execution failed: {e}",
  319. -1,
  320. )
  321. finally:
  322. if diagnostic in ("nfc", "read_tag"):
  323. logger.info("Reinitializing NFC continuous scan after diagnostic")
  324. shared["nfc"] = NFCReader()
  325. shared["nfc_scan_paused"] = False
  326. continue
  327. elif cmd == "write_tag":
  328. write_payload = result.get("pending_write_payload")
  329. if write_payload:
  330. shared["pending_write"] = {
  331. "spool_id": write_payload["spool_id"],
  332. "ndef_data": bytes.fromhex(write_payload["ndef_data_hex"]),
  333. }
  334. logger.info("Write tag command received for spool %d", write_payload["spool_id"])
  335. elif cmd in ("reboot", "shutdown", "restart_daemon", "restart_browser"):
  336. logger.info("System command received: %s", cmd)
  337. try:
  338. await api.system_command_result(config.device_id, cmd, True, f"Executing {cmd}")
  339. except Exception:
  340. pass # Best effort — we're about to restart/shutdown anyway
  341. if cmd == "reboot":
  342. await asyncio.to_thread(subprocess.run, ["sudo", "reboot"], check=False)
  343. elif cmd == "shutdown":
  344. await asyncio.to_thread(subprocess.run, ["sudo", "shutdown", "-h", "now"], check=False)
  345. elif cmd == "restart_daemon":
  346. await asyncio.to_thread(
  347. subprocess.run, ["sudo", "systemctl", "restart", "spoolbuddy.service"], check=False
  348. )
  349. elif cmd == "restart_browser":
  350. await asyncio.to_thread(
  351. subprocess.run, ["sudo", "systemctl", "restart", "getty@tty1.service"], check=False
  352. )
  353. continue
  354. tare = result.get("tare_offset", config.tare_offset)
  355. cal = result.get("calibration_factor", config.calibration_factor)
  356. if tare != config.tare_offset or cal != config.calibration_factor:
  357. config.tare_offset = tare
  358. config.calibration_factor = cal
  359. scale = shared.get("scale")
  360. if scale:
  361. scale.update_calibration(tare, cal)
  362. logger.info("Calibration updated from backend: tare=%d, factor=%.6f", tare, cal)
  363. # Apply display settings from backend
  364. brightness = result.get("display_brightness")
  365. blank_timeout = result.get("display_blank_timeout")
  366. if brightness is not None:
  367. display.set_brightness(brightness)
  368. if blank_timeout is not None:
  369. display.set_blank_timeout(blank_timeout)
  370. display.tick()
  371. async def main():
  372. config = Config.load()
  373. logger.info(
  374. "SpoolBuddy daemon v%s starting (device=%s, backend=%s)", __version__, config.device_id, config.backend_url
  375. )
  376. api = APIClient(config.backend_url, config.api_key)
  377. ip = _get_ip()
  378. start_time = time.monotonic()
  379. # Initialize hardware before registration so we can report capabilities
  380. nfc = NFCReader()
  381. scale = ScaleReader(
  382. tare_offset=config.tare_offset,
  383. calibration_factor=config.calibration_factor,
  384. )
  385. display = DisplayControl()
  386. # Register with backend (retries until success)
  387. reg = await api.register_device(
  388. device_id=config.device_id,
  389. hostname=config.hostname,
  390. ip_address=ip,
  391. firmware_version=__version__,
  392. has_nfc=True,
  393. has_scale=True,
  394. tare_offset=config.tare_offset,
  395. calibration_factor=config.calibration_factor,
  396. nfc_reader_type=nfc.reader_type,
  397. nfc_connection=nfc.connection,
  398. backend_url=config.backend_url,
  399. has_backlight=display.has_backlight,
  400. )
  401. # Use server-side calibration if available
  402. if reg:
  403. config.tare_offset = reg.get("tare_offset", config.tare_offset)
  404. config.calibration_factor = reg.get("calibration_factor", config.calibration_factor)
  405. scale.update_calibration(config.tare_offset, config.calibration_factor)
  406. # Auto-deploy Bambuddy's SSH public key for remote updates
  407. ssh_key = reg.get("ssh_public_key")
  408. if ssh_key:
  409. _deploy_ssh_key(ssh_key)
  410. logger.info("Device registered, starting poll loops")
  411. shared: dict = {"nfc": nfc, "scale": scale, "display": display, "nfc_scan_paused": False}
  412. try:
  413. await asyncio.gather(
  414. nfc_poll_loop(config, api, shared),
  415. scale_poll_loop(config, api, shared),
  416. heartbeat_loop(config, api, start_time, shared),
  417. )
  418. except KeyboardInterrupt:
  419. logger.info("Shutting down")
  420. finally:
  421. await api.close()
  422. if __name__ == "__main__":
  423. asyncio.run(main())