#!/bin/sh
#Script to control Android Thermostat via command line.
#http://androidthermostat.com
# Born: 1/24/2013 -JGB
# Modified:
#### Change below to the ip address of your Android Thermostat ###
tstatip=
##### DO NOT CHANGE ANYTHING BELOW THIS LINE #####
### Get script arguments ###
mode=$1
temp1=$2
temp2=$3
##### Functions #####
script_usage() {
echo Usage:
echo ""
echo This script requires command line options.
echo The first option is 'Mode'
echo Valid options for Mode are: Off, Heat, Cool, Auto
echo ""
echo The second option is desired temperature
echo This should be a 2 digit Number
echo ""
echo If using Auto Mode, You must specify a third option
echo ""
echo Example: ./script.sh Auto LOW_TEMP HIGH_TEMP
echo Example: ./script.sh Auto 70 75
echo ""
echo If using 'Off' no 2nd command line option is required.
exit
};
arg_check() {
ARGS_GOOD=
if [ "$mode" == 'Heat' ] || [ "$mode" == 'Cool' ]; then
if [ -z "$temp1" ] || [ "$temp1" -lt '50' ] || [ "$temp1" -gt '95' ]; then
script_usage
ARGS_GOOD=0
fi
elif [ "$mode" == 'Auto' ]; then
if [ -z "$temp1" ] || [ -z "$temp2" ] || [ "$temp1" -lt '50' ] || [ "$temp1" -gt '95' ] || [ "$temp2" -lt '50' ] || [ "$temp2" -gt '95' ]; then
script_usage
ARGS_GOOD=0
fi
elif [ "$mode" == 'Off' ]; then
ARGS_GOOD=1
else
script_usage
ARGS_GOOD=0
fi
};
##### End Functions #####
### Check script input ###
arg_check
### Get current settings from tstat ###
curloutput=$(curl -s http://$tstatip:8080/api/settings);
#add -S if you want to see errors, but not output
#echo Here is what we got from the tstat:
#echo $curloutput
#echo ""
##### Choose sed command based on input #####
if [ "$mode" == 'Heat' ]; then
target=targetLow
#sedline=s/\"targetLow\":[0-9]\{1,3\}/\"targetLow\":$temp1/g
#sedline=s/\"$target\":[0-9]\{1,3\}/\"$target\":$temp1/g";"s/\"mode\":\"[A-z]\{1,4\}\"/\"mode\":\"$mode\"/g
sedcurloutput=$(echo "$curloutput" | sed -e "s/\"$target\":[0-9]\{1,3\}/\"$target\":$temp1/g" -e "s/\"mode\":\"[A-z]\{1,4\}\"/\"mode\":\"$mode\"/g");
fi
if [ "$mode" == 'Cool' ]; then
target=targetHigh
#sedline=s/\"targetHigh\":[0-9]\{1,3\}/\"targetHigh\":$temp1/g
#sedline=s/\"$target\":[0-9]\{1,3\}/\"$target\":$temp1/g";"s/\"mode\":\"[A-z]\{1,4\}\"/\"mode\":\"$mode\"/g
sedcurloutput=$(echo "$curloutput" | sed -e "s/\"$target\":[0-9]\{1,3\}/\"$target\":$temp1/g" -e "s/\"mode\":\"[A-z]\{1,4\}\"/\"mode\":\"$mode\"/g");
fi
if [ "$mode" == 'Auto' ]; then
target=
#sedline=s/\"targetLow\":[0-9]\{1,3\}/\"targetLow\":$temp1/g";"s/\"targetHigh\":[0-9]\{1,3\}/\"targetHigh\":$temp2/g";"s/\"mode\":\"[A-z]\{1,4\}\"/\"mode\":\"$mode\"/g
sedcurloutput=$(echo "$curloutput" | sed -e "s/\"targetLow\":[0-9]\{1,3\}/\"targetLow\":$temp1/g" -e "s/\"targetHigh\":[0-9]\{1,3\}/\"targetHigh\":$temp2/g" -e "s/\"mode\":\"[A-z]\{1,4\}\"/\"mode\":\"$mode\"/g");
fi
if [ "$mode" == 'Off' ]; then
target=
#sedline=s/\"mode\":\"[A-z]\{1,4\}\"/\"mode\":\"$mode\"/g
sedcurloutput=$(echo "$curloutput" | sed -e "s/\"mode\":\"[A-z]\{1,4\}\"/\"mode\":\"$mode\"/g");
fi
#echo ""
#echo sed curloutput
#echo "$sedcurloutput"
### sed patterns ###
#Change only mode
#sedlinemode=s/\"mode\":\"[A-z]\{1,4\}\"/\"mode\":\"$mode\"/g
#Change mode and temp (Heat/Cool)
#sedlinetemp=s/\"$target\":[0-9]\{1,3\}/\"$target\":$temp1/g
#Change mode and both temps (Auto)
#sedlineauto=s/\"targetLow\":[0-9]\{1,3\}/\"targetLow\":$temp1/g";"s/\"targetHigh\":[0-9]\{1,3\}/\"targetHigh\":$temp2/g
#echo Sed line variables:
#echo "$sedlinemode"
#echo "$sedlinetemp"
#echo "$sedlineauto"
#echo Variables after if statements
#echo "$target"
#echo "$sedline"
#set -x will show you the actual curl line when running. This is useful for debug.
#set -x
#echo ""
#echo curl post
#echo ""
# if &>/dev/null is not at the end of the next line, curl shows the text "output" on the command line.
curl -s -H "Accept:: application/json" -H \"Content-type: application/json\" -X POST -d "$sedcurloutput" http://$tstatip:8080/api/settings &>/dev/null