diff --git a/bridge/README.md b/bridge/README.md index 5a61b4c..d07fb7a 100644 --- a/bridge/README.md +++ b/bridge/README.md @@ -17,24 +17,55 @@ Tested against an Aira Home heat pump (Europe). Python 3.9+. ## Install +One command. It clones this repository into `~/harbour-aira`, creates a private +Python environment, asks for your Aira email and password (typed without echo), +checks that it can read your plant, and installs itself as a service that starts +by itself at boot. + +```bash +curl -fsSL https://git.hackatoniclife.com/kaneda/harbour-aira/raw/branch/main/bridge/install.sh | bash +``` + +It never needs root and it changes nothing outside your home directory. When it +finishes, it prints two things to copy into the app: the address and the token. +On a Raspberry Pi the slow part is installing the Python libraries, a few +minutes; everything else is seconds. + +To update later, run the same command again. + +### If something goes wrong + +Everything is a normal user service, so you can look at it: + +```bash +systemctl --user status harbour-aira-bridge +journalctl --user -u harbour-aira-bridge -f +systemctl --user restart harbour-aira-bridge +``` + +If your machine has no systemd user session, the script says so and prints the +command to start the bridge by hand. + +### Doing it manually + +If you would rather see every step, the install is short: + ```bash git clone https://git.hackatoniclife.com/kaneda/harbour-aira.git cd harbour-aira/bridge python3 -m venv .venv -. .venv/bin/activate -pip install -r requirements.txt +.venv/bin/python -m pip install -r requirements.txt -# where credentials and bridge configuration will live export AIRA_CONFIG_DIR="$HOME/.config/aira" mkdir -p "$AIRA_CONFIG_DIR" # your Aira email + password: typed without echo, saved in a file readable # only by your user (600). Nothing is printed, nothing is logged. -python setup_creds.py +.venv/bin/python setup_creds.py # sanity check: logs in and prints your plant state (read-only) -python aira_probe.py +.venv/bin/python aira_probe.py ``` If `aira_probe.py` prints your temperatures and modes, the hard part is done. diff --git a/bridge/install.sh b/bridge/install.sh new file mode 100755 index 0000000..7c3b954 --- /dev/null +++ b/bridge/install.sh @@ -0,0 +1,181 @@ +#!/usr/bin/env bash +# +# One-command install of the Aira bridge for the harbour-aira Sailfish app. +# +# What it does, in order: +# 1. clones (or updates) this repository into ~/harbour-aira +# 2. creates a private Python environment and installs the bridge +# 3. asks for your Aira email and password, typed without echo, and saves +# them in a file only you can read (never in the shell history, never in +# the logs, never on the phone) +# 4. checks that the bridge can log in and read your plant +# 5. installs and starts it as a systemd user service +# 6. prints the address and the token to type into the app +# +# Run it with: bash install.sh +# +# It changes nothing outside your home directory, and it never needs root. +set -euo pipefail + +REPO_URL="${AIRA_REPO_URL:-https://git.hackatoniclife.com/kaneda/harbour-aira.git}" +INSTALL_DIR="${AIRA_INSTALL_DIR:-$HOME/harbour-aira}" +CONFIG_DIR="${AIRA_CONFIG_DIR:-$HOME/.config/aira}" +PORT="${AIRA_PORT:-8790}" +SKIP_SERVICE="${AIRA_SKIP_SERVICE:-0}" +SKIP_CREDS="${AIRA_SKIP_CREDS:-0}" + +say() { printf '\n\033[1m%s\033[0m\n' "$*"; } +ok() { printf ' ok %s\n' "$*"; } +warn() { printf ' note %s\n' "$*"; } +die() { printf '\n error: %s\n\n' "$*" >&2; exit 1; } + +# ---------------------------------------------------------------- prerequisites +say "Checking what this machine has" + +command -v git >/dev/null 2>&1 || die "git is not installed. On Debian/Raspberry Pi OS: apt install git" +command -v python3 >/dev/null 2>&1 || die "python3 is not installed. On Debian/Raspberry Pi OS: apt install python3 python3-venv" + +PY_OK=$(python3 -c 'import sys; print(1 if sys.version_info >= (3, 9) else 0)') +[ "$PY_OK" = "1" ] || die "python3 is $(python3 -V 2>&1 | cut -d' ' -f2), but the bridge needs 3.9 or newer." + +python3 -c 'import venv' 2>/dev/null || die "the venv module is missing. On Debian/Raspberry Pi OS: apt install python3-venv" +ok "git $(git --version | cut -d' ' -f3), $(python3 -V 2>&1)" + +# ------------------------------------------------------------------- the bridge +say "Fetching the bridge into $INSTALL_DIR" +if [ -d "$INSTALL_DIR/.git" ]; then + git -C "$INSTALL_DIR" pull --ff-only >/dev/null || warn "could not update the existing copy; using it as it is" + ok "already there, brought up to date" +else + mkdir -p "$(dirname "$INSTALL_DIR")" + git clone --quiet "$REPO_URL" "$INSTALL_DIR" + ok "cloned" +fi + +BRIDGE="$INSTALL_DIR/bridge" +[ -f "$BRIDGE/aira_bridge.py" ] || die "the bridge is not where it should be ($BRIDGE)." + +say "Installing the bridge and its libraries" +if [ ! -x "$BRIDGE/.venv/bin/python" ]; then + python3 -m venv "$BRIDGE/.venv" +fi +"$BRIDGE/.venv/bin/python" -m pip install --quiet --upgrade pip +# pyairahome pulls in grpcio, protobuf, pycognito, cryptography: on a Raspberry +# Pi this step is the slow one, a few minutes. Progress is hidden on purpose, +# the alternative is a page of compiler output that means nothing to a user. +"$BRIDGE/.venv/bin/python" -m pip install --quiet -r "$BRIDGE/requirements.txt" +ok "libraries in place" + +# ------------------------------------------------------------- your credentials +mkdir -p "$CONFIG_DIR" +chmod 700 "$CONFIG_DIR" + +if [ "$SKIP_CREDS" = "1" ]; then + warn "skipping the credentials step (AIRA_SKIP_CREDS=1)" +elif [ -f "$CONFIG_DIR/aira.json" ]; then + say "Aira credentials already present" + ok "$CONFIG_DIR/aira.json, left untouched" +else + say "Your Aira account" + printf ' Use the same email and password as the Aira app.\n' + printf ' The password is typed without echo: it is not shown, not saved in\n' + printf ' the shell history, and it stays on this machine.\n' + AIRA_CONFIG_DIR="$CONFIG_DIR" "$BRIDGE/.venv/bin/python" "$BRIDGE/setup_creds.py" \ + || die "credentials not saved." +fi + +# ------------------------------------------------------------------ sanity check +if [ "$SKIP_CREDS" != "1" ]; then + say "Talking to your heat pump (read-only)" + printf ' This logs in to Aira and reads the current state. Nothing is changed.\n\n' + if AIRA_CONFIG_DIR="$CONFIG_DIR" "$BRIDGE/.venv/bin/python" "$BRIDGE/aira_probe.py"; then + ok "the bridge can reach your plant" + else + die "the bridge could not read your plant. Check the email and password, and that this machine can reach the internet." + fi +fi + +# ------------------------------------------------------------------ the service +started=0 +if [ "$SKIP_SERVICE" = "1" ]; then + warn "skipping the service step (AIRA_SKIP_SERVICE=1)" +elif command -v systemctl >/dev/null 2>&1 && systemctl --user show-environment >/dev/null 2>&1; then + say "Installing the service, so it starts by itself" + UNIT_DIR="$HOME/.config/systemd/user" + mkdir -p "$UNIT_DIR" + cat > "$UNIT_DIR/harbour-aira-bridge.service" </dev/null 2>&1 || warn "could not enable lingering: the service may stop when you log out" + systemctl --user daemon-reload + systemctl --user enable --now harbour-aira-bridge.service >/dev/null + started=1 + ok "service installed and started" +else + warn "no systemd user session here: start the bridge by hand when you need it" + printf ' AIRA_CONFIG_DIR=%s %s/.venv/bin/python %s/aira_bridge.py --port %s\n' \ + "$CONFIG_DIR" "$BRIDGE" "$BRIDGE" "$PORT" +fi + +# ---------------------------------------------------------- what the app needs +if [ "$started" = "1" ]; then + printf ' waiting for the bridge to answer' + for _ in $(seq 1 30); do + if curl -fsS "http://127.0.0.1:$PORT/health" >/dev/null 2>&1; then break; fi + printf '.'; sleep 1 + done + printf '\n' + if curl -fsS "http://127.0.0.1:$PORT/health" >/dev/null 2>&1; then + ok "the bridge is answering" + else + warn "the bridge is not answering yet; check: systemctl --user status harbour-aira-bridge" + fi +fi + +TOKEN="" +if [ -f "$CONFIG_DIR/aira_bridge.json" ]; then + TOKEN=$(python3 -c 'import json,sys; print(json.load(open(sys.argv[1])).get("token",""))' "$CONFIG_DIR/aira_bridge.json" 2>/dev/null || true) +fi + +# The address the phone should use: this machine's address on the LAN. +ADDR=$(hostname -I 2>/dev/null | awk '{print $1}') +[ -n "$ADDR" ] || ADDR="" + +cat <} + + Both are private: the token is the key to your plant on your own + network. Do not share it, and do not forward this port from the + internet. Nothing here is exposed outside your LAN. + + Useful commands: + systemctl --user status harbour-aira-bridge + systemctl --user restart harbour-aira-bridge + journalctl --user -u harbour-aira-bridge -f + + To update later, run this script again: it pulls the new version. + ------------------------------------------------------------------ + +LAST diff --git a/bridge/setup_creds.py b/bridge/setup_creds.py index 8a8d342..cd97014 100644 --- a/bridge/setup_creds.py +++ b/bridge/setup_creds.py @@ -1,10 +1,10 @@ #!/usr/bin/env python3 -"""Salva le credenziali Aira in $AIRA_CONFIG_DIR/aira.json (permessi 600). +"""Save the Aira credentials in $AIRA_CONFIG_DIR/aira.json (permissions 600). -Va eseguito da te, nel tuo terminale: la password viene digitata senza eco e non -compare ne' nei log ne' nella cronologia della shell. +Run it yourself, in your own terminal: the password is typed without echo and +never appears in the logs or in the shell history. -Uso: +Usage: cd bridge .venv/bin/python setup_creds.py """ @@ -20,14 +20,14 @@ TARGET = BASE / "aira.json" def main() -> int: - email = input("Email account Aira: ").strip() + email = input("Aira account email: ").strip() if not email: - print("email vuota, annullo", file=sys.stderr) + print("empty email, aborting", file=sys.stderr) return 1 - password = getpass.getpass("Password (non verra' mostrata): ") + password = getpass.getpass("Password (will not be shown): ") if not password: - print("password vuota, annullo", file=sys.stderr) + print("empty password, aborting", file=sys.stderr) return 1 TARGET.parent.mkdir(parents=True, exist_ok=True) @@ -36,8 +36,8 @@ def main() -> int: os.chmod(tmp, stat.S_IRUSR | stat.S_IWUSR) tmp.replace(TARGET) - print(f"\nOK: credenziali salvate in {TARGET} (permessi 600)") - print("Ora lancia: .venv/bin/python aira_probe.py") + print(f"\nOK: credentials saved in {TARGET} (permissions 600)") + print("Next: .venv/bin/python aira_probe.py") return 0