1
1
Fork 0

added powercontrol.sh which is a wrapper script for hs100.sh from https://github.com/branning/hs100

This commit is contained in:
david 2019-11-19 13:55:35 +01:00
parent 6d0d1e87a0
commit ebc50f0458
1 changed files with 139 additions and 0 deletions

139
power_control.sh Executable file
View File

@ -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 <on|off|status> <device>"
}
# **** 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 ***