152 lines
4.3 KiB
Bash
Executable file
152 lines
4.3 KiB
Bash
Executable file
#!/bin/sh
|
|
#
|
|
# Turn WLED installations in the C4 hackspace on/off, change brightness or
|
|
# switch between presets.
|
|
#
|
|
# Author: Shy
|
|
# License: CC0
|
|
|
|
NAME="wled.sh"
|
|
COMMAND="wled"
|
|
VERSION="0.1.1"
|
|
|
|
print_usage() {
|
|
echo "\
|
|
Usage: $COMMAND -f(norcenter)|-w(wohnzimmer) [on|off]
|
|
$COMMAND -f|-w [1-17|list]
|
|
$COMMAND -f|-w -b [1-255]"
|
|
}
|
|
|
|
print_help() {
|
|
print_usage
|
|
echo "
|
|
host selection:
|
|
-f,--fnordcenter select fnordcenter
|
|
-w,--wohnzimmer select wohnzimmer
|
|
|
|
commands:
|
|
on|off switch on/off
|
|
[1-17] switch preset
|
|
l, list list presets
|
|
-b,--brightness [1-255] set brightness"
|
|
}
|
|
|
|
curl_fetch() {
|
|
curl --silent --show-error --fail --ipv4 --max-filesize 32768 \
|
|
--max-redirs 0 --header 'Accept: application/json' "$@"
|
|
}
|
|
|
|
curl_send() {
|
|
curl --silent --show-error --fail --ipv4 --max-filesize 1024 \
|
|
--max-redirs 0 --header 'Accept: application/json' \
|
|
--header 'Content-Type: application/json' "$@"
|
|
}
|
|
|
|
active_preset() {
|
|
# Find number of active preset. API returns -1 if none is active, we return
|
|
# an empty string.
|
|
curl_fetch "http://$1/json" | \
|
|
# -1 will be a no-match.
|
|
sed -n 's/.*"ps"[[:space:]]*:[[:space:]]*\(1\?[0-9]\).*/\1/p'
|
|
}
|
|
|
|
# Parse host argument.
|
|
case "$1" in
|
|
""|-h|--help) # Missing host parameter.
|
|
print_help
|
|
exit 0
|
|
;;
|
|
-v|--version)
|
|
echo "$NAME version $VERSION"
|
|
exit 0
|
|
;;
|
|
-f|--fnordcenter)
|
|
wled_host='wled-fnordcenter.local'
|
|
;;
|
|
-w|--wohnzimmer)
|
|
wled_host='wled-wohnzimmer.local'
|
|
;;
|
|
*) # Unknown host parameter.
|
|
echo "Error: unknown host \"$1\"." >&2
|
|
print_usage
|
|
exit 1
|
|
;;
|
|
esac
|
|
|
|
# Parse and execute command argument.
|
|
case "$2" in
|
|
on) # Switch on.
|
|
api_resp=$(curl_send --data '{"on":true}' "http://$wled_host/json")
|
|
;;
|
|
off) # Switch off.
|
|
api_resp=$(curl_send --data '{"on":false}' "http://$wled_host/json")
|
|
;;
|
|
-b|--brightness) # Set brightness.
|
|
if test -z "$3"; then
|
|
echo "Error: missing parameter for brightness." >&2
|
|
exit 1
|
|
fi
|
|
# Try to convert 3rd parameter to an integer between 1 an 255.
|
|
bright=$(printf '%u' "$3" 2>/dev/null)
|
|
if test $? -gt 0; then # Conversion failed.
|
|
echo "Error: unable to parse parameter for brightness." >&2
|
|
exit 1
|
|
elif test "$bright" -lt 1 -o "$bright" -gt 255; then
|
|
echo "Error: parameter for brightness is out of range." >&2
|
|
exit 1
|
|
fi
|
|
api_resp=$(curl_send --data "{'bri':$bright}" "http://$wled_host/json")
|
|
;;
|
|
[1-9]|1[0-7]) # Switch to preset.
|
|
api_resp=$(curl_send --data "{'ps':$2}" "http://$wled_host/json")
|
|
;;
|
|
l|list) # List presets.
|
|
printf "loading ...\r"
|
|
active_ps=$(active_preset "$wled_host")
|
|
curl_fetch "http://$wled_host/presets.json" | \
|
|
# Split into lines - one line per preset.
|
|
sed --sandbox 's/"1\?[0-9]"[[:space:]]*:/\n&/g' | \
|
|
# Extract number and name of every preset. Right-align numbers.
|
|
# Drop everything else.
|
|
sed --sandbox -n \
|
|
'/^"\(1\?[0-9]\)"[[:space:]]*:.*"n"[[:space:]]*:[[:space:]]*"\([[:alnum:] _-]*\)".*$/{
|
|
s//\1: \2/
|
|
s/^[0-9]:/ &/
|
|
p
|
|
}' | \
|
|
# Sort.
|
|
sort -b -n | \
|
|
# Prepend description and mark active preset.
|
|
sed --sandbox -e "1i\\Presets on $wled_host:" \
|
|
-e "s/^ \?$active_ps:.*/& */"
|
|
exit $?
|
|
;;
|
|
dump) # Dump JSON response from API.
|
|
curl_fetch -w '\n' "http://$wled_host/json"
|
|
exit 0
|
|
;;
|
|
"") # Missing command.
|
|
echo "Error: missing command." >&2
|
|
print_usage
|
|
exit 1
|
|
;;
|
|
*) # Unknown command.
|
|
echo "Error: unknown command \"$2\"." >&2
|
|
print_usage
|
|
exit 1
|
|
;;
|
|
esac
|
|
|
|
# Check curl exit status
|
|
curl_exit=$?
|
|
if test $curl_exit -ne 0; then
|
|
echo "Error: curl exited with exit code $curl_exit." >&2
|
|
exit 1
|
|
fi
|
|
|
|
# Check API responce.
|
|
if echo "$api_resp" | grep -qv '{\s*"success"\s*:\s*true\s*}'; then
|
|
echo "Error: unexpected responce from WLED API." >&2
|
|
exit 1
|
|
fi
|
|
|