c4wled.sh/wled

145 lines
4 KiB
Text
Raw Normal View History

2024-08-02 08:18:32 +02:00
#!/bin/sh
#
2024-08-04 10:11:16 +02:00
# Turn WLED installations in the C4 hackspace on/off, change brightness or
# switch between presets.
2024-08-02 08:18:32 +02:00
#
# Author: Shy
# License: CC0
2024-08-04 10:11:16 +02:00
NAME="wled.sh"
COMMAND="wled"
2024-08-05 18:09:29 +02:00
VERSION="0.1.1"
2024-08-02 08:18:32 +02:00
print_usage() {
echo "\
2024-08-04 10:11:16 +02:00
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"
2024-08-02 08:18:32 +02:00
}
2024-08-03 23:39:46 +02:00
curl_cmd() {
curl --silent --show-error --fail --ipv4 --max-filesize 32K "$@"
}
2024-08-05 18:09:29 +02:00
active_preset() {
# Find number of active preset. API returns -1 if none is active, we return
# an empty string.
preset="$(curl_cmd "http://$1/json" | \
# -1 will be a no-match.
sed -n 's/.*"ps"[[:space:]]*:[[:space:]]*\(1\?[0-9]\).*/\1/p')"
printf '%u' "$preset"
}
2024-08-02 08:18:32 +02:00
# Parse host argument.
case "$1" in
""|-h|--help) # Missing host parameter.
print_help
exit 0
;;
-v|--version)
2024-08-04 10:11:16 +02:00
echo "$NAME version $VERSION"
2024-08-03 22:36:05 +02:00
exit 0
;;
-f|--fnordcenter)
2024-08-02 08:18:32 +02:00
wled_host='wled-fnordcenter.local'
;;
-w|--wohnzimmer)
2024-08-02 08:18:32 +02:00
wled_host='wled-wohnzimmer.local'
;;
2024-08-03 22:36:05 +02:00
*) # Unknown host parameter.
2024-08-02 08:18:32 +02:00
echo "Error: unknown host \"$1\"." >&2
print_usage
exit 1
;;
esac
# Parse and execute command argument.
case "$2" in
2024-08-03 22:36:05 +02:00
on) # Switch on.
2024-08-03 23:39:46 +02:00
api_resp=$(curl_cmd --json '{"on":true}' "http://$wled_host/json")
2024-08-02 08:18:32 +02:00
;;
2024-08-03 22:36:05 +02:00
off) # Switch off.
2024-08-03 23:39:46 +02:00
api_resp=$(curl_cmd --json '{"on":false}' "http://$wled_host/json")
2024-08-02 08:18:32 +02:00
;;
-b|--brightness) # Set brightness.
2024-08-03 22:36:05 +02:00
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
2024-08-03 23:39:46 +02:00
api_resp=$(curl_cmd --json "{'bri':$bright}" "http://$wled_host/json")
2024-08-03 22:36:05 +02:00
;;
[1-9]|1[0-7]) # Switch to preset.
2024-08-04 00:19:34 +02:00
api_resp=$(curl_cmd --json "{'ps':$2}" "http://$wled_host/json")
2024-08-02 08:18:32 +02:00
;;
2024-08-03 22:36:05 +02:00
l|list) # List presets.
2024-08-05 15:51:46 +02:00
printf "loading ...\r"
2024-08-05 18:09:29 +02:00
active_ps=$(active_preset "$wled_host")
2024-08-03 23:39:46 +02:00
curl_cmd "http://$wled_host/presets.json" | \
2024-08-05 19:19:30 +02:00
# Split into lines - one line per preset.
sed --sandbox 's/"1\?[0-9]"[[:space:]]*:/\n&/g' | \
2024-08-05 18:09:29 +02:00
# Prepend description, extract number and name of every preset,
# match active preset, right-align numbers and finally select
# lines to print.
2024-08-05 15:51:46 +02:00
sed --sandbox -n \
2024-08-05 18:09:29 +02:00
-e "1i\\Presets on $wled_host:" \
-e 's/^"\(1\?[0-9]\)"[[:space:]]*:.*"n"[[:space:]]*:[[:space:]]*"\([[:print:]]*\)".*$/\1: \2/' \
-e "s/^$active_ps:.*/& */" \
-e 's/^[0-9]:/ &/' \
2024-08-05 19:19:30 +02:00
-e '/^[1 ]\?[0-9]: [[:print:]]\+/p' | \
# Sort.
sort -b -n
2024-08-02 17:07:08 +02:00
exit $?
;;
2024-08-03 22:36:05 +02:00
dump) # Dump JSON response from API.
2024-08-03 23:39:46 +02:00
curl_cmd -w '\n' "http://$wled_host/json"
2024-08-02 08:18:32 +02:00
exit 0
;;
2024-08-03 22:36:05 +02:00
"") # Missing command.
echo "Error: missing command." >&2
print_usage
exit 1
;;
*) # Unknown command.
2024-08-02 08:18:32 +02:00
echo "Error: unknown command \"$2\"." >&2
print_usage
exit 1
;;
esac
2024-08-04 00:19:34 +02:00
# Check curl exit status
2024-08-02 08:39:05 +02:00
curl_exit=$?
if test $curl_exit -ne 0; then
2024-08-03 22:36:05 +02:00
echo "Error: curl exited with exit code $curl_exit." >&2
2024-08-02 08:39:05 +02:00
exit 1
fi
2024-08-03 23:24:07 +02:00
# Check API responce.
2024-08-05 15:51:46 +02:00
if echo "$api_resp" | grep -qv '{\s*"success"\s*:\s*true\s*}'; then
2024-08-02 08:18:32 +02:00
echo "Error: unexpected responce from WLED API." >&2
exit 1
fi