#!/bin/bash

# see https://www.weatherapi.com

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

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

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

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

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


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



#
# get current weather
#
if ( curl -s -S "https://api.weatherapi.com/v1/current.json?key=$API_KEY&q=$POS_LAT,$POS_LONG&aqi=yes" -o $JSONFILE ); then
  $AQHOME_TOOL adddata -J $JSONFILE -c weather -d "$POS_CITY" -N "Temperature" -v "@current/temp_c" -U "C"
  $AQHOME_TOOL adddata -J $JSONFILE -c weather -d "$POS_CITY" -N "Humidity" -v "@current/humidity" -U "%"
  $AQHOME_TOOL adddata -J $JSONFILE -c weather -d "$POS_CITY" -N "Wetbulb" -v "@current/wetbulb_c" -U "C"
  $AQHOME_TOOL adddata -J $JSONFILE -c weather -d "$POS_CITY" -N "pm2_5" -v "@current/air_quality/pm2_5" -U "ug/m3"
  $AQHOME_TOOL adddata -J $JSONFILE -c weather -d "$POS_CITY" -N "pm10" -v "@current/air_quality/pm10" -U "ug/m3"
fi

