c4wled.sh/wled

99 lines
2.8 KiB
Text
Raw Normal View History

2024-08-02 08:18:32 +02:00
#!/bin/sh
#
# Simpel command line tool to turn predefined wled instances on/off or switch
# presets.
#
# Author: Shy
# License: CC0
2024-08-02 20:47:17 +02:00
CURL_FLAGS="--silent --show-error --fail --ipv4 --max-filesize 64K"
2024-08-02 08:18:32 +02:00
print_usage() {
2024-08-02 18:11:50 +02:00
echo "Usage: $0 f(nordcenter)|w(ohnzimmer) on|off|1-17|list"
2024-08-03 23:24:07 +02:00
echo " $0 f(nordcenter)|w(ohnzimmer) b(rightness) 1-255"
2024-08-02 08:18:32 +02:00
}
# Parse host argument.
case "$1" in
2024-08-03 22:36:05 +02:00
""|-h|--h) # Missing host parameter.
print_usage
exit 0
;;
2024-08-02 08:18:32 +02:00
f|fnordcenter)
wled_host='wled-fnordcenter.local'
;;
w|wohnzimmer)
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-02 20:47:17 +02:00
api_resp=$(curl $CURL_FLAGS --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-02 20:47:17 +02:00
api_resp=$(curl $CURL_FLAGS --json '{"on":false}' "http://$wled_host/json")
2024-08-02 08:18:32 +02:00
;;
2024-08-03 22:36:05 +02:00
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 $CURL_FLAGS --json "{'bri':$bright}" "http://$wled_host/json")
;;
[1-9]|1[0-7]) # Switch to preset.
2024-08-02 20:47:17 +02:00
api_resp=$(curl $CURL_FLAGS --json "{'on':true,'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-02 17:07:08 +02:00
echo "Presets on $wled_host:"
2024-08-02 20:47:17 +02:00
curl $CURL_FLAGS "http://$wled_host/presets.json" | \
2024-08-03 20:55:59 +02:00
# Split into lines - one line per preset. Append final newline.
sed --sandbox 's/"\(1\?[0-9]\)"/\n"\1"/g; $a\' | \
# Extract number and name of every preset.
sed --sandbox -n 's/^"\(1\?[0-9]\)":.*"n":"\([[:print:]]*\)".*$/\1: \2/p'
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-02 20:47:17 +02:00
curl $CURL_FLAGS -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-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.
if test "$api_resp" != '{"success":true}'; then
2024-08-02 08:18:32 +02:00
echo "Error: unexpected responce from WLED API." >&2
exit 1
fi