Added --all flag.

This commit is contained in:
Shy 2024-08-06 15:10:53 +02:00
parent 471e5dae50
commit 6f99f677e6
2 changed files with 96 additions and 87 deletions

View file

@ -7,15 +7,15 @@ between presets.
Switch on/off: Switch on/off:
`$ wled -f(nordcenter)|-w(wohnzimmer) on|off` `$ wled --fnordcenter|--wohnzimmer|--all on|off`
Switch between presets 1 to 17 or list available presets: Switch between presets 1 to 17 or list available presets:
`$ wled -f|-w 1-17|l` `$ wled -f|-w|-a [1-17|l(ist)]`
Change brightness: Change brightness:
`$ wled -f|-w -b 1-255` `$ wled -f|-w|-a -b [1-255]`
## Requirements ## Requirements

75
wled
View file

@ -8,21 +8,22 @@
NAME="wled.sh" NAME="wled.sh"
COMMAND="wled" COMMAND="wled"
VERSION="0.1.1" VERSION="0.1.2"
print_usage() { print_usage() {
echo "\ echo "\
Usage: $COMMAND -f(norcenter)|-w(wohnzimmer) [on|off] Usage: $COMMAND -f(norcenter)|-w(wohnzimmer)|-a(ll) [on|off]
$COMMAND -f|-w [1-17|list] $COMMAND -f|-w|-a [1-17|list]
$COMMAND -f|-w -b [1-255]" $COMMAND -f|-w|-a -b [1-255]"
} }
print_help() { print_help() {
print_usage print_usage
echo " echo "
host selection: instance selection:
-f,--fnordcenter select fnordcenter -f,--fnordcenter select fnordcenter
-w,--wohnzimmer select wohnzimmer -w,--wohnzimmer select wohnzimmer
-a,--all select all known instances
commands: commands:
on|off switch on/off on|off switch on/off
@ -50,9 +51,16 @@ active_preset() {
sed -n 's/.*"ps"[[:space:]]*:[[:space:]]*\(1\?[0-9]\).*/\1/p' sed -n 's/.*"ps"[[:space:]]*:[[:space:]]*\(1\?[0-9]\).*/\1/p'
} }
# Parse host argument. arg_inst="$1"
case "$1" in arg_cmnd="$2"
""|-h|--help) # Missing host parameter. arg_param="$3"
# Clear positional arguments.
set --
# Parse instance argument and store hostnames as positional arguments.
case "$arg_inst" in
""|-h|--help) # Missing instance parameter.
print_help print_help
exit 0 exit 0
;; ;;
@ -60,34 +68,39 @@ case "$1" in
echo "$NAME version $VERSION" echo "$NAME version $VERSION"
exit 0 exit 0
;; ;;
-a|--all)
set -- 'wled-fnordcenter.local' \
'wled-wohnzimmer.local'
;;
-f|--fnordcenter) -f|--fnordcenter)
wled_host='wled-fnordcenter.local' set -- 'wled-fnordcenter.local'
;; ;;
-w|--wohnzimmer) -w|--wohnzimmer)
wled_host='wled-wohnzimmer.local' set -- 'wled-wohnzimmer.local'
;; ;;
*) # Unknown host parameter. *) # Unknown instance parameter.
echo "Error: unknown host \"$1\"." >&2 echo "Error: unknown instance \"$arg_inst\"." >&2
print_usage print_usage
exit 1 exit 1
;; ;;
esac esac
# Parse and execute command argument. # Parse and execute command argument against given instances.
case "$2" in while test $# -gt 0; do
case "$arg_cmnd" in
on) # Switch on. on) # Switch on.
api_resp=$(curl_send '{"on":true}' "http://$wled_host/json") api_resp=$(curl_send '{"on":true}' "http://$1/json")
;; ;;
off) # Switch off. off) # Switch off.
api_resp=$(curl_send '{"on":false}' "http://$wled_host/json") api_resp=$(curl_send '{"on":false}' "http://$1/json")
;; ;;
-b|--brightness) # Set brightness. -b|--brightness) # Set brightness.
if test -z "$3"; then if test -z "$arg_param"; then
echo "Error: missing parameter for brightness." >&2 echo "Error: missing parameter for brightness." >&2
exit 1 exit 1
fi fi
# Try to convert 3rd parameter to an integer between 1 an 255. # Try to convert 3rd parameter to an integer between 1 an 255.
bright=$(printf '%u' "$3" 2>/dev/null) bright=$(printf '%u' "$arg_param" 2>/dev/null)
if test $? -gt 0; then # Conversion failed. if test $? -gt 0; then # Conversion failed.
echo "Error: unable to parse parameter for brightness." >&2 echo "Error: unable to parse parameter for brightness." >&2
exit 1 exit 1
@ -95,15 +108,15 @@ case "$2" in
echo "Error: parameter for brightness is out of range." >&2 echo "Error: parameter for brightness is out of range." >&2
exit 1 exit 1
fi fi
api_resp=$(curl_send "{'bri':$bright}" "http://$wled_host/json") api_resp=$(curl_send "{'bri':$bright}" "http://$1/json")
;; ;;
[1-9]|1[0-7]) # Switch to preset. [1-9]|1[0-7]) # Switch to preset.
api_resp=$(curl_send "{'ps':$2}" "http://$wled_host/json") api_resp=$(curl_send "{'ps':$arg_cmnd}" "http://$1/json")
;; ;;
l|list) # List presets. l|list) # List presets.
printf "loading ...\r" printf "loading ...\r"
active_ps=$(active_preset "$wled_host") active_ps=$(active_preset "$1")
curl_fetch "http://$wled_host/presets.json" | \ curl_fetch "http://$1/presets.json" | \
# Split into lines - one line per preset. # Split into lines - one line per preset.
sed --sandbox 's/"1\?[0-9]"[[:space:]]*:/\n&/g' | \ sed --sandbox 's/"1\?[0-9]"[[:space:]]*:/\n&/g' | \
# Extract number and name of every preset. Right-align numbers. # Extract number and name of every preset. Right-align numbers.
@ -117,13 +130,8 @@ case "$2" in
# Sort. # Sort.
sort -b -n | \ sort -b -n | \
# Prepend description and mark active preset. # Prepend description and mark active preset.
sed --sandbox -e "1i\\Presets on $wled_host:" \ sed --sandbox -e "1i\\Presets on $1:" \
-e "s/^ \?$active_ps:.*/& */" -e "s/^ \?$active_ps:.*/& */"
exit $?
;;
dump) # Dump JSON response from API.
curl_fetch -w '\n' "http://$wled_host/json"
exit 0
;; ;;
"") # Missing command. "") # Missing command.
echo "Error: missing command." >&2 echo "Error: missing command." >&2
@ -131,7 +139,7 @@ case "$2" in
exit 1 exit 1
;; ;;
*) # Unknown command. *) # Unknown command.
echo "Error: unknown command \"$2\"." >&2 echo "Error: unknown command \"$arg_cmnd\"." >&2
print_usage print_usage
exit 1 exit 1
;; ;;
@ -141,12 +149,13 @@ esac
curl_exit=$? curl_exit=$?
if test $curl_exit -ne 0; then if test $curl_exit -ne 0; then
echo "Error: curl exited with exit code $curl_exit." >&2 echo "Error: curl exited with exit code $curl_exit." >&2
exit 1
fi fi
# Check API responce. # Check API responce.
if echo "$api_resp" | grep -qv '{\s*"success"\s*:\s*true\s*}'; then if test -n "$api_resp" && echo "$api_resp" | grep -qv '{\s*"success"\s*:\s*true\s*}'; then
echo "Error: unexpected responce from WLED API." >&2 echo "Warning: unexpected responce from WLED API." >&2
exit 1
fi fi
shift
done