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

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

curl -sS --max-time 5 "$BASE_URL/api/realtime" | jq -r '
  if .error then error("gateway error: " + .error) else
    "State:    \(.runStateText)\n" +
    "Battery:  \(.batteryVoltage) V  \(.batteryCurrent) A  SOC \(.batterySoc) %\n" +
    "PV:       \(.pvPowerTotal) W\n" +
    "Load:     \(.loadPowerTotal) W\n" +
    "Grid:     \(.gridPowerTotal) W  (positive = importing)"
  end'
