From ebc50f0458f36e74a04e3d0749c8decd38f8db2a Mon Sep 17 00:00:00 2001 From: david Date: Tue, 19 Nov 2019 13:55:35 +0100 Subject: [PATCH] added powercontrol.sh which is a wrapper script for hs100.sh from https://github.com/branning/hs100 --- power_control.sh | 139 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 139 insertions(+) create mode 100755 power_control.sh diff --git a/power_control.sh b/power_control.sh new file mode 100755 index 0000000..7e557a6 --- /dev/null +++ b/power_control.sh @@ -0,0 +1,139 @@ +#!/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 ***