spoolbuddy-idle.sh 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. #!/bin/bash
  2. # SpoolBuddy kiosk display idle watchdog.
  3. #
  4. # Powers the HDMI output off via wlopm after the configured inactivity
  5. # timeout, driven by swayidle inside the labwc Wayland session. The timeout
  6. # value is fetched once from the Bambuddy backend on startup so it matches
  7. # whatever the user picked in SpoolBuddy Settings → Display. Changes made
  8. # in the UI take effect on the next reboot / kiosk restart.
  9. #
  10. # Runs in labwc's autostart file as the kiosk user — needs access to
  11. # WAYLAND_DISPLAY, which it inherits from the parent labwc process.
  12. set -u
  13. DEFAULT_TIMEOUT=300
  14. ENV_FILE="${SPOOLBUDDY_ENV_FILE:-/opt/bambuddy/spoolbuddy/.env}"
  15. OUTPUT="${SPOOLBUDDY_DISPLAY_OUTPUT:-HDMI-A-1}"
  16. if [ -r "$ENV_FILE" ]; then
  17. set -a
  18. # shellcheck disable=SC1090
  19. . "$ENV_FILE"
  20. set +a
  21. fi
  22. BACKEND_URL="${SPOOLBUDDY_BACKEND_URL:-}"
  23. API_KEY="${SPOOLBUDDY_API_KEY:-}"
  24. DEVICE_ID="${SPOOLBUDDY_DEVICE_ID:-}"
  25. # Derive device_id from the first non-loopback NIC MAC address, the same
  26. # algorithm daemon/config.py uses so installs without an explicit
  27. # SPOOLBUDDY_DEVICE_ID still match.
  28. if [ -z "$DEVICE_ID" ]; then
  29. for iface in $(ls -1 /sys/class/net/ 2>/dev/null | sort); do
  30. [ "$iface" = "lo" ] && continue
  31. addr_file="/sys/class/net/$iface/address"
  32. [ -r "$addr_file" ] || continue
  33. mac=$(tr -d ':' < "$addr_file" 2>/dev/null)
  34. if [ -n "$mac" ] && [ "$mac" != "000000000000" ]; then
  35. DEVICE_ID="sb-$mac"
  36. break
  37. fi
  38. done
  39. fi
  40. TIMEOUT="$DEFAULT_TIMEOUT"
  41. if [ -n "$BACKEND_URL" ] && [ -n "$API_KEY" ] && [ -n "$DEVICE_ID" ]; then
  42. response=$(curl -fsS --max-time 10 \
  43. -H "Authorization: Bearer $API_KEY" \
  44. "$BACKEND_URL/api/v1/spoolbuddy/devices/$DEVICE_ID/display" 2>/dev/null || true)
  45. if [ -n "$response" ]; then
  46. fetched=$(printf '%s' "$response" | jq -r '.blank_timeout // empty' 2>/dev/null || true)
  47. if [ -n "$fetched" ] && [ "$fetched" -eq "$fetched" ] 2>/dev/null; then
  48. TIMEOUT="$fetched"
  49. fi
  50. fi
  51. fi
  52. if [ "$TIMEOUT" -le 0 ]; then
  53. # Blanking explicitly disabled — don't launch swayidle at all.
  54. exec sleep infinity
  55. fi
  56. exec swayidle -w \
  57. timeout "$TIMEOUT" "wlopm --off $OUTPUT" \
  58. resume "wlopm --on $OUTPUT"