#!/usr/bin/env bash
# JK BMS Web Gateway — read live pack data with curl + jq.
#
# One HTTP GET, no authentication; the gateway answers JSON, or
# {"error": "..."} when the BMS did not respond.
#
# Usage: ./jkbms_realtime.sh [http://jkbms.local]
#   (use the gateway's IP, e.g. http://192.168.1.50, if mDNS is not reachable)
set -euo pipefail

BASE_URL="${1:-http://jkbms.local}"

curl -sS --max-time 5 "$BASE_URL/api/realtime" | jq -r '
  if .error then error("gateway error: " + .error) else
    "Pack voltage: \(.totalVoltage) V\n" +
    "Current:      \(.current) A\n" +
    "SOC:          \(.soc) %"
  end'
