#!/bin/bash # # control my hs100 power switches # VERSION="0.1" AUTHOR="david@socialnerds.org" DEVICES="media ghost" is_status_on() { if [ "$1" ] && [ $(./hs100/hs100.sh -i power-$1.socialner.ds check | awk '{print $2}') == "ON" ]; then return 0 else return 1 fi } is_status_off() { if [ "$1" ] && [ $(./hs100/hs100.sh -i power-$1.socialner.ds check | awk '{print $2}') == "OFF" ]; then return 0 else return 1 fi } does_device_exist() { if [ "$1" ]; then for DEVICE in $DEVICES; do if [ "$1" == $DEVICE ]; then return 0 fi done return 1 else return 1 fi } switch_device_on() { if [ "$1" ]; then ./hs100/hs100.sh -i power-$1.socialner.ds on if [ $? -ne 0 ]; then return 1 fi return 0 else return 1 fi } switch_device_off() { if [ "$1" ]; then ./hs100/hs100.sh -i power-$1.socialner.ds off if [ $? -ne 0 ]; then return 1 fi return 0 else return 1 fi } print_usage() { echo "usage: ./power_control " } # **** start of script **** if [ "$1" ] && [[ "$1" == @("on"|"off"|"status") ]] && [ "$2" ]; then if does_device_exist $2; then case $1 in "on") if is_status_off $2; then echo "switching on $2" if ! switch_device_on $2; then echo "error: something went wrong while switching on device" exit 1 fi sleep 2 if is_status_on $2; then echo "$2 switched on successfully" else echo "warning: device still appears off" fi else if is_status_on $2; then echo "warning: device is already switched on" else echo "error: something went wrong while checking device status" exit 1 fi fi ;; "off") if is_status_on $2; then echo "switching off $2" if ! switch_device_off $2; then echo "error: something went wrong while switching off device" exit 1 fi sleep 2 if is_status_off $2; then echo "$2 switched off successfully" else echo "warning: device still appears on" fi else if is_status_off $2; then echo "warning: device is already switched off" else echo "error: something went wrong while checking device status" exit 1 fi fi ;; "status") if is_status_on $2; then echo "$2 is on" elif is_status_off $2; then echo "$2 is off" else echo "error: something went wrong while getting device status" exit 1 fi ;; esac else echo "error: device unknown" print_usage exit 1 fi else echo "error: command unknown" print_usage exit 1 fi # **** end of script ***