FreeBSD shell compatible script to control WTI RPS-10 remote power switch from pfSense/OpnSense boxes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
PORT=/dev/${1:-cuau0} | |
BAUD=${2:-9600} | |
# WTI RPS-10 Commands | |
ALL_DEVICES="*" | |
TOGGLE="T" | |
GET_STATE="?" | |
SWITCH_ON="1" | |
SWITCH_OFF="0" | |
DEVICE0="0" | |
TOGGLE_ALL_DEVICES="${ALL_DEVICES}${TOGGLE}" | |
TOGGLE_DEVICE0="0${TOGGLE}" | |
TOGGLE_DEVICE1="1${TOGGLE}" | |
SWITCH_ON_ALL_DEVICES="${ALL_DEVICES}${SWITCH_ON}" | |
SWITCH_ON_DEVICE0="0${SWITCH_ON}" | |
SWITCH_ON_DEVICE1="1${SWITCH_ON}" | |
SWITCH_OFF_ALL_DEVICES="${ALL_DEVICES}${SWITCH_OFF}" | |
SWITCH_OFF_DEVICE0="0${SWITCH_OFF}" | |
SWITCH_OFF_DEVICE1="1${SWITCH_OFF}" | |
GET_STATE_ALL_DEVICES="${ALL_DEVICES}${GET_STATE}" | |
GET_STATE_DEVICE0="0${GET_STATE}" | |
GET_STATE_DEVICE1="1${GET_STATE}" | |
Timestamp () { date "+%F %T"; } | |
Log () { echo "[$(Timestamp)] $1" >&2; } | |
Abort () { Log "$1"; exit ${2:-1}; } | |
ClosePort () { exec 9>&-; Log "Connection closed"; } | |
GetReplay () { | |
local REPLY | |
while true; do | |
read -t ${1:-5} REPLY <&9 || return 1 | |
[ ${#REPLY} -eq 0 ] && continue | |
[ "${REPLY}" = "Invalid" ] && return 1 | |
[ "${REPLY}" = "Complete" ] && sleep 0.5 && return 0 | |
[ "${REPLY}" = "RPS-10 Ready" ] && sleep 0.5 && return 0 | |
Log "$REPLY" | |
done | |
return 1 | |
} | |
OpenPort () { | |
Log "Connecting..." | |
stty -f ${PORT} ${BAUD} raw -echo || return 1 | |
exec 9<>${PORT} || return 1 | |
printf "\r" >&9 | |
GetReplay 20 && Log "Connected to RPS-10 (${PORT}) @ ${BAUD}" && return 0 | |
return 1 | |
} | |
SendCMD () { | |
printf "\002\030\030\002\030\030$1\r" >&9 | |
GetReplay $2 | |
} | |
OpenPort || Abort "Failed to open ${PORT}!" | |
SendCMD "${GET_STATE_DEVICE0}" | |
SendCMD "${GET_STATE_DEVICE1}" | |
#SendCMD "${TOGGLE_ALL_DEVICES}" 25 # Toggle commands need extra long timeout | |
ClosePort | |
exit 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment