Pastebin launched a little side project called VERYVIRAL.com, check it out ;-) Want more features on Pastebin? Sign Up, it's FREE!
Guest

Android Thermostat Control Script

By: a guest on Jan 27th, 2013  |  syntax: Bash  |  size: 4.26 KB  |  views: 60  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. #!/bin/sh
  2. #Script to control Android Thermostat via command line.
  3. #http://androidthermostat.com
  4. # Born: 1/24/2013 -JGB
  5. # Modified:
  6.  
  7. #### Change below to the ip address of your Android Thermostat ###
  8. tstatip=
  9.  
  10.  
  11. ##### DO NOT CHANGE ANYTHING BELOW THIS LINE #####
  12.  
  13. ### Get script arguments ###
  14. mode=$1
  15. temp1=$2
  16. temp2=$3
  17.  
  18. ##### Functions #####
  19.  
  20. script_usage() {
  21.         echo Usage:
  22.         echo ""
  23.         echo This script requires command line options.
  24.         echo The first option is 'Mode'
  25.         echo Valid options for Mode are: Off, Heat, Cool, Auto
  26.         echo ""
  27.         echo The second option is desired temperature
  28.         echo This should be a 2 digit Number
  29.         echo ""
  30.         echo If using Auto Mode, You must specify a third option
  31.         echo ""
  32.         echo Example: ./script.sh Auto LOW_TEMP HIGH_TEMP
  33.         echo Example: ./script.sh Auto 70 75
  34.         echo ""
  35.         echo If using 'Off' no 2nd command line option is required.
  36. exit
  37. };
  38.  
  39. arg_check() {
  40.         ARGS_GOOD=
  41.         if [ "$mode" == 'Heat' ] || [ "$mode" == 'Cool' ]; then
  42.                 if [ -z "$temp1" ] || [ "$temp1" -lt '50' ] || [ "$temp1" -gt '95' ]; then
  43.                         script_usage
  44.                         ARGS_GOOD=0
  45.                 fi
  46.         elif [ "$mode" == 'Auto' ]; then
  47.                 if [ -z "$temp1" ] || [ -z "$temp2" ] || [ "$temp1" -lt '50' ] || [ "$temp1" -gt '95' ] || [ "$temp2" -lt '50' ] || [ "$temp2" -gt '95' ]; then
  48.                         script_usage
  49.                         ARGS_GOOD=0
  50.                 fi
  51.         elif [ "$mode" == 'Off' ]; then
  52.                 ARGS_GOOD=1
  53.         else
  54.         script_usage
  55.         ARGS_GOOD=0
  56. fi
  57. };
  58.  
  59. ##### End Functions #####
  60.  
  61. ### Check script input ###
  62. arg_check
  63.  
  64. ### Get current settings from tstat ###
  65. curloutput=$(curl -s http://$tstatip:8080/api/settings);
  66. #add -S if you want to see errors, but not output
  67.  
  68. #echo Here is what we got from the tstat:
  69. #echo $curloutput
  70. #echo ""
  71.  
  72. ##### Choose sed command based on input #####
  73. if [ "$mode" == 'Heat' ]; then
  74.         target=targetLow
  75.         #sedline=s/\"targetLow\":[0-9]\{1,3\}/\"targetLow\":$temp1/g
  76.         #sedline=s/\"$target\":[0-9]\{1,3\}/\"$target\":$temp1/g";"s/\"mode\":\"[A-z]\{1,4\}\"/\"mode\":\"$mode\"/g
  77.         sedcurloutput=$(echo "$curloutput" | sed -e "s/\"$target\":[0-9]\{1,3\}/\"$target\":$temp1/g" -e "s/\"mode\":\"[A-z]\{1,4\}\"/\"mode\":\"$mode\"/g");
  78. fi
  79. if [ "$mode" == 'Cool' ]; then
  80.         target=targetHigh
  81.         #sedline=s/\"targetHigh\":[0-9]\{1,3\}/\"targetHigh\":$temp1/g
  82.         #sedline=s/\"$target\":[0-9]\{1,3\}/\"$target\":$temp1/g";"s/\"mode\":\"[A-z]\{1,4\}\"/\"mode\":\"$mode\"/g
  83.         sedcurloutput=$(echo "$curloutput" | sed -e "s/\"$target\":[0-9]\{1,3\}/\"$target\":$temp1/g" -e "s/\"mode\":\"[A-z]\{1,4\}\"/\"mode\":\"$mode\"/g");
  84. fi
  85.  
  86. if [ "$mode" == 'Auto' ]; then
  87.         target=
  88.         #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
  89.         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");
  90. fi
  91.  
  92. if [ "$mode" == 'Off' ]; then
  93.         target=
  94.         #sedline=s/\"mode\":\"[A-z]\{1,4\}\"/\"mode\":\"$mode\"/g
  95.         sedcurloutput=$(echo "$curloutput" | sed -e "s/\"mode\":\"[A-z]\{1,4\}\"/\"mode\":\"$mode\"/g");
  96. fi
  97.  
  98. #echo ""
  99. #echo sed curloutput
  100. #echo "$sedcurloutput"
  101.  
  102.  
  103. ### sed patterns ###
  104. #Change only mode
  105. #sedlinemode=s/\"mode\":\"[A-z]\{1,4\}\"/\"mode\":\"$mode\"/g
  106. #Change mode and temp (Heat/Cool)
  107. #sedlinetemp=s/\"$target\":[0-9]\{1,3\}/\"$target\":$temp1/g
  108. #Change mode and both temps (Auto)
  109. #sedlineauto=s/\"targetLow\":[0-9]\{1,3\}/\"targetLow\":$temp1/g";"s/\"targetHigh\":[0-9]\{1,3\}/\"targetHigh\":$temp2/g
  110.  
  111. #echo Sed line variables:
  112. #echo "$sedlinemode"
  113. #echo "$sedlinetemp"
  114. #echo "$sedlineauto"
  115. #echo Variables after if statements
  116. #echo "$target"
  117. #echo "$sedline"
  118.  
  119. #set -x will show you the actual curl line when running. This is useful for debug.
  120. #set -x
  121. #echo ""
  122. #echo curl post
  123. #echo ""
  124. # if &>/dev/null is not at the end of the next line, curl shows the text "output" on the command line.
  125. curl -s -H "Accept:: application/json" -H \"Content-type: application/json\" -X POST -d "$sedcurloutput" http://$tstatip:8080/api/settings &>/dev/null