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"
|
2024-08-04 00:54:54 +02:00
|
|
|
COMMAND="wled"
|
2024-08-06 19:03:06 +02:00
|
|
|
VERSION="0.1.3"
|
2024-08-04 00:54:54 +02:00
|
|
|
|
2024-08-07 18:19:19 +02:00
|
|
|
exit_code=0
|
|
|
|
host_list=
|
|
|
|
|
2024-08-06 16:26:54 +02:00
|
|
|
# Make the exit status of command pipelines the status of the last command
|
|
|
|
# with non-zero exit status - if supported by this shell.
|
|
|
|
(set -o pipefail 2>/dev/null) && set -o pipefail
|
|
|
|
|
2024-08-02 08:18:32 +02:00
|
|
|
print_usage() {
|
2024-08-04 00:54:54 +02:00
|
|
|
echo "\
|
2024-08-06 19:11:38 +02:00
|
|
|
Usage: $COMMAND -f(norcenter)|-w(wohnzimmer)|-a(ll) on|off
|
|
|
|
$COMMAND -f|-w|-a 1-17|list
|
2024-08-06 15:10:53 +02:00
|
|
|
$COMMAND -f|-w|-a -b [1-255]"
|
2024-08-04 00:54:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
print_help() {
|
|
|
|
print_usage
|
|
|
|
echo "
|
2024-08-06 19:11:38 +02:00
|
|
|
Instance selection:
|
2024-08-04 00:54:54 +02:00
|
|
|
-f,--fnordcenter select fnordcenter
|
|
|
|
-w,--wohnzimmer select wohnzimmer
|
2024-08-06 15:10:53 +02:00
|
|
|
-a,--all select all known instances
|
2024-08-04 00:54:54 +02:00
|
|
|
|
2024-08-06 19:11:38 +02:00
|
|
|
Commands:
|
2024-08-04 00:54:54 +02:00
|
|
|
on|off switch on/off
|
2024-08-06 19:11:38 +02:00
|
|
|
1-17 switch preset
|
2024-08-04 00:54:54 +02:00
|
|
|
l, list list presets
|
2024-08-06 16:47:46 +02:00
|
|
|
-b,--brightness [1-255] get or set brightness
|
2024-08-06 16:26:54 +02:00
|
|
|
|
2024-08-06 19:11:38 +02:00
|
|
|
Exit status:
|
2024-08-06 16:26:54 +02:00
|
|
|
0 no failure
|
|
|
|
1 operating error
|
|
|
|
2 child command error
|
|
|
|
4 unexpected API response"
|
2024-08-02 08:18:32 +02:00
|
|
|
}
|
|
|
|
|
2024-08-06 11:29:44 +02:00
|
|
|
curl_fetch() {
|
|
|
|
curl --silent --show-error --fail --ipv4 --max-filesize 32768 \
|
2024-08-06 12:04:17 +02:00
|
|
|
--max-redirs 0 --header 'Accept: application/json' "$1"
|
2024-08-06 11:29:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
curl_send() {
|
|
|
|
curl --silent --show-error --fail --ipv4 --max-filesize 1024 \
|
|
|
|
--max-redirs 0 --header 'Accept: application/json' \
|
2024-08-06 12:04:17 +02:00
|
|
|
--header 'Content-Type: application/json' --data "$1" "$2"
|
2024-08-03 23:39:46 +02:00
|
|
|
}
|
|
|
|
|
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.
|
2024-08-06 11:29:44 +02:00
|
|
|
curl_fetch "http://$1/json" | \
|
2024-08-05 18:09:29 +02:00
|
|
|
# -1 will be a no-match.
|
2024-08-06 10:56:01 +02:00
|
|
|
sed -n 's/.*"ps"[[:space:]]*:[[:space:]]*\(1\?[0-9]\).*/\1/p'
|
2024-08-05 18:09:29 +02:00
|
|
|
}
|
|
|
|
|
2024-08-06 16:47:46 +02:00
|
|
|
current_brightness() {
|
|
|
|
# Find current brightness.
|
|
|
|
curl_fetch "http://$1/json" | \
|
|
|
|
sed -n 's/.*"state"[[:space:]]*:[[:space:]]*{[^}]*"bri"[[:space:]]*:[[:space:]]*\([0-9]\{1,3\}\).*/\1/p'
|
|
|
|
}
|
|
|
|
|
2024-08-07 18:19:19 +02:00
|
|
|
# Parse instance argument and store hostnames.
|
|
|
|
while test $# -gt 0; do
|
|
|
|
case "$1" in
|
|
|
|
""|-h|--help) # Missing instance parameter.
|
|
|
|
print_help
|
|
|
|
exit 0
|
|
|
|
;;
|
|
|
|
-v|--version)
|
|
|
|
echo "$NAME version $VERSION"
|
|
|
|
exit 0
|
|
|
|
;;
|
|
|
|
-a|--all)
|
|
|
|
host_list="wled-fnordcenter.local wled-wohnzimmer.local"
|
|
|
|
;;
|
|
|
|
-f|--fnordcenter)
|
|
|
|
host_list="$host_list wled-fnordcenter.local"
|
|
|
|
;;
|
|
|
|
-w|--wohnzimmer)
|
|
|
|
host_list="$host_list wled-wohnzimmer.local"
|
|
|
|
;;
|
|
|
|
*) # Unknown instance parameter.
|
|
|
|
test -n "$host_list" && break
|
|
|
|
echo "Error: not a valid instance option (\"$1\")." >&2
|
|
|
|
print_usage
|
|
|
|
exit 1
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
shift
|
|
|
|
done
|
2024-08-02 08:18:32 +02:00
|
|
|
|
2024-08-06 15:10:53 +02:00
|
|
|
# Parse and execute command argument against given instances.
|
2024-08-07 18:19:19 +02:00
|
|
|
for wled_host in $host_list; do
|
|
|
|
case "$1" in
|
2024-08-06 15:10:53 +02:00
|
|
|
on) # Switch on.
|
2024-08-06 15:18:51 +02:00
|
|
|
api_resp=$(curl_send '{"on":true}' "http://$wled_host/json")
|
2024-08-06 15:10:53 +02:00
|
|
|
;;
|
|
|
|
off) # Switch off.
|
2024-08-06 15:18:51 +02:00
|
|
|
api_resp=$(curl_send '{"on":false}' "http://$wled_host/json")
|
2024-08-06 15:10:53 +02:00
|
|
|
;;
|
|
|
|
-b|--brightness) # Set brightness.
|
2024-08-07 18:19:19 +02:00
|
|
|
if test -z "$2"; then
|
2024-08-06 16:47:46 +02:00
|
|
|
# No parameter given. Print current brightness.
|
2024-08-06 19:11:38 +02:00
|
|
|
echo "Brightness of $wled_host:"
|
2024-08-06 16:47:46 +02:00
|
|
|
current_brightness "$wled_host"
|
|
|
|
else
|
|
|
|
# Try to convert 3rd parameter to an integer between 1 an 255.
|
2024-08-07 18:19:19 +02:00
|
|
|
bright=$(printf '%u' "$2" 2>/dev/null)
|
2024-08-06 16:47:46 +02:00
|
|
|
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 "{'bri':$bright}" "http://$wled_host/json")
|
2024-08-06 15:10:53 +02:00
|
|
|
fi
|
|
|
|
;;
|
|
|
|
[1-9]|1[0-7]) # Switch to preset.
|
2024-08-07 18:19:19 +02:00
|
|
|
api_resp=$(curl_send "{'ps':$1}" "http://$wled_host/json")
|
2024-08-06 15:10:53 +02:00
|
|
|
;;
|
|
|
|
l|list) # List presets.
|
|
|
|
printf "loading ...\r"
|
2024-08-06 15:18:51 +02:00
|
|
|
active_ps=$(active_preset "$wled_host")
|
|
|
|
curl_fetch "http://$wled_host/presets.json" | \
|
2024-08-07 20:46:57 +02:00
|
|
|
# Insert newline in front of every preset slice.
|
|
|
|
# (Start of loop.)
|
|
|
|
# Match number and name of first preset.
|
|
|
|
# Replace in number: name format.
|
2024-08-07 17:19:20 +02:00
|
|
|
# Match and mark active preset.
|
2024-08-07 20:46:57 +02:00
|
|
|
# Right-align number.
|
|
|
|
# Print out up to next newline.
|
|
|
|
# Delete up to next newline.
|
|
|
|
# Process next preset or end of loop.
|
2024-08-07 17:19:20 +02:00
|
|
|
# Prepend description.
|
2024-08-07 14:24:06 +02:00
|
|
|
sed --sandbox -n '
|
|
|
|
s/"1\?[0-9]"[[:space:]]*:/\n&/g
|
|
|
|
:loop
|
|
|
|
/^"\(1\?[0-9]\)"[[:space:]]*:[^\n]*"n"[[:space:]]*:[[:space:]]*"\([[:alnum:] _-]*\)"[^\n]*/{
|
2024-08-06 15:10:53 +02:00
|
|
|
s//\1: \2/
|
2024-08-07 17:19:20 +02:00
|
|
|
s/^'"$active_ps"':[^\n]*/& */
|
2024-08-06 15:10:53 +02:00
|
|
|
s/^[0-9]:/ &/
|
2024-08-07 14:24:06 +02:00
|
|
|
P
|
|
|
|
}
|
|
|
|
s/^[^\n]*\n//
|
2024-08-07 17:19:20 +02:00
|
|
|
t loop
|
|
|
|
i\
|
|
|
|
'"Presets on $wled_host:" | \
|
2024-08-06 15:10:53 +02:00
|
|
|
# Sort.
|
2024-08-07 17:19:20 +02:00
|
|
|
sort -n -b -t : -k 1,1
|
2024-08-06 15:10:53 +02:00
|
|
|
;;
|
|
|
|
"") # Missing command.
|
|
|
|
echo "Error: missing command." >&2
|
|
|
|
print_usage
|
2024-08-03 22:36:05 +02:00
|
|
|
exit 1
|
2024-08-06 15:10:53 +02:00
|
|
|
;;
|
|
|
|
*) # Unknown command.
|
2024-08-07 18:19:19 +02:00
|
|
|
echo "Error: unknown command \"$1\"." >&2
|
2024-08-06 15:10:53 +02:00
|
|
|
print_usage
|
2024-08-03 22:36:05 +02:00
|
|
|
exit 1
|
2024-08-06 15:10:53 +02:00
|
|
|
;;
|
|
|
|
esac
|
|
|
|
|
2024-08-06 16:26:54 +02:00
|
|
|
# Check curl (or maybe sed) exit status
|
|
|
|
child_exit=$?
|
|
|
|
if test $child_exit -ne 0; then
|
|
|
|
echo "Error: curl or sed exited with exit code $child_exit." >&2
|
|
|
|
exit_code=$((exit_code|2))
|
2024-08-06 15:10:53 +02:00
|
|
|
fi
|
2024-08-02 08:18:32 +02:00
|
|
|
|
2024-08-06 16:26:54 +02:00
|
|
|
# Check API response.
|
2024-08-06 15:10:53 +02:00
|
|
|
if test -n "$api_resp" && echo "$api_resp" | grep -qv '{\s*"success"\s*:\s*true\s*}'; then
|
2024-08-06 19:11:38 +02:00
|
|
|
echo "Error: unexpected response from WLED API." >&2
|
2024-08-06 16:26:54 +02:00
|
|
|
exit_code=$((exit_code|4))
|
2024-08-06 15:10:53 +02:00
|
|
|
fi
|
|
|
|
done
|
2024-08-02 08:18:32 +02:00
|
|
|
|
2024-08-06 16:26:54 +02:00
|
|
|
exit $exit_code
|
|
|
|
|