#!/bin/sh POWER_PATH="/proc/power" NUM_OUTPUTS=$(find $POWER_PATH -name 'output*' | wc -l) valid_output() { [ "$1" -gt "$NUM_OUTPUTS" ] && return 1 [ "$1" -lt "1" ] && return 1 return 0 } check_output() { ! valid_output $1 && error "Invalid output number" } error() { echo "{\"error\":\"$1\"}" exit 1 } set_output() { echo $2 > $POWER_PATH/relay$1 } clear_all() { for i in $(seq 1 $NUM_OUTPUTS); do echo 1 > $POWER_PATH/clear_ae$i done } report() { echo "[" for i in $(seq 1 $NUM_OUTPUTS); do set -- $(cat "/dev/power$i") # Convert to JSON booleans state="false"; [ "$1" = "on" ] && state="true" echo -e "\t{" echo -e "\t\t\"output\": $i," echo -e "\t\t\"engaged\": $state," echo -e "\t\t\"active_power\": $2," echo -e "\t\t\"energy_sum\": $6," echo -e "\t\t\"current_rms\": $3," echo -e "\t\t\"voltage_rms\": $4," echo -e "\t\t\"power_factor\": $5" echo -en "\t}" [ $i -ne $NUM_OUTPUTS ] && echo -e "," done echo "]" } disable_junk_processes() { # Otherwise init will continue to respawn them sed -i \ -e '/ubnt-websockets/s/^/#/' \ -e '/telnetd/s/^/#/' \ -e '/mca[-d]/s/^/#/' \ -e '/lighttpd/s/^/#/' \ /etc/inittab kill -HUP 1 # Most of these kill cleanly but a few are stubborn so don't ask, tell. pkill -9 ubnt-websockets pkill -9 lighttpd pkill upnpd pkill telnetd pkill mca-monitor pkill mcad pkill avahi-daemon } disable_blinking() { echo 0 > /proc/led/freq echo 1 > /proc/led/status } if ! grep -E '^#.*mcad$' /etc/inittab 2>&1 > /dev/null; then disable_junk_processes fi # Always disable blinking disable_blinking case $1 in on) check_output $2 set_output $2 1 report ;; off) check_output $2 set_output $2 0 report ;; report) report ;; clear) clear_all ;; disable_blinking) disable_blinking ;; *) error "Invalid command" ;; esac