#!/bin/bash

# see https://open-meteo.com/en/docs/dwd-api

JSONFILE="/tmp/weather.json"
#AQHOME_TOOL="./aqhome-tool.sh"
AQHOME_TOOL="/usr/local/bin/aqhome-tool"

POS_CITY="$1"
POS_LAT="$2"
POS_LONG="$3"


if test -z "$POS_CITY"; then
  echo "Usage: $0 CITY LATITUDE LONGITUDE"
  exit 1;
fi

if test -z "$POS_LAT"; then
  echo "Usage: $0 CITY LATITUDE LONGITUDE"
  exit 1;
fi

if test -z "$POS_LONG"; then
  echo "Usage: $0 CITY LATITUDE LONGITUDE"
  exit 1;
fi



#
# get current weather
#
if ( curl -s -S "https://api.open-meteo.com/v1/dwd-icon?latitude=$POS_LAT&longitude=$POS_LONG&current=temperature_2m,relative_humidity_2m" -o $JSONFILE ); then
  $AQHOME_TOOL adddata -J $JSONFILE -c weather -d "$POS_CITY" -N "Temperature" -v "@current/temperature_2m" -U "@current_units/temperature_2m"
  $AQHOME_TOOL adddata -J $JSONFILE -c weather -d "$POS_CITY" -N "Humidity" -v "@current/relative_humidity_2m" -U "@current_units/relative_humidity_2m"
fi

