From f3dd1edb1285f4ee10af80e88dd7734806695ee3 Mon Sep 17 00:00:00 2001 From: Shy Date: Tue, 6 Aug 2024 15:18:51 +0200 Subject: [PATCH 1/3] use for-loop instead of while/shift. --- wled | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/wled b/wled index c99a225..062dd35 100755 --- a/wled +++ b/wled @@ -86,13 +86,13 @@ case "$arg_inst" in esac # Parse and execute command argument against given instances. -while test $# -gt 0; do +for wled_host; do case "$arg_cmnd" in on) # Switch on. - api_resp=$(curl_send '{"on":true}' "http://$1/json") + api_resp=$(curl_send '{"on":true}' "http://$wled_host/json") ;; off) # Switch off. - api_resp=$(curl_send '{"on":false}' "http://$1/json") + api_resp=$(curl_send '{"on":false}' "http://$wled_host/json") ;; -b|--brightness) # Set brightness. if test -z "$arg_param"; then @@ -108,15 +108,15 @@ while test $# -gt 0; do echo "Error: parameter for brightness is out of range." >&2 exit 1 fi - api_resp=$(curl_send "{'bri':$bright}" "http://$1/json") + api_resp=$(curl_send "{'bri':$bright}" "http://$wled_host/json") ;; [1-9]|1[0-7]) # Switch to preset. - api_resp=$(curl_send "{'ps':$arg_cmnd}" "http://$1/json") + api_resp=$(curl_send "{'ps':$arg_cmnd}" "http://$wled_host/json") ;; l|list) # List presets. printf "loading ...\r" - active_ps=$(active_preset "$1") - curl_fetch "http://$1/presets.json" | \ + 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. @@ -130,7 +130,7 @@ while test $# -gt 0; do # Sort. sort -b -n | \ # Prepend description and mark active preset. - sed --sandbox -e "1i\\Presets on $1:" \ + sed --sandbox -e "1i\\Presets on $wled_host:" \ -e "s/^ \?$active_ps:.*/& */" ;; "") # Missing command. @@ -155,7 +155,5 @@ while test $# -gt 0; do if test -n "$api_resp" && echo "$api_resp" | grep -qv '{\s*"success"\s*:\s*true\s*}'; then echo "Warning: unexpected responce from WLED API." >&2 fi - - shift done From dd9231f834514db6a3fbaaac1d6c836814b29e12 Mon Sep 17 00:00:00 2001 From: Shy Date: Tue, 6 Aug 2024 16:26:54 +0200 Subject: [PATCH 2/3] Revised exit status generation. --- wled | 30 +++++++++++++++++++++++------- 1 file changed, 23 insertions(+), 7 deletions(-) diff --git a/wled b/wled index 062dd35..775914e 100755 --- a/wled +++ b/wled @@ -10,6 +10,10 @@ NAME="wled.sh" COMMAND="wled" VERSION="0.1.2" +# 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 + print_usage() { echo "\ Usage: $COMMAND -f(norcenter)|-w(wohnzimmer)|-a(ll) [on|off] @@ -29,7 +33,13 @@ commands: on|off switch on/off [1-17] switch preset l, list list presets - -b,--brightness [1-255] set brightness" + -b,--brightness [1-255] set brightness + +exit status: + 0 no failure + 1 operating error + 2 child command error + 4 unexpected API response" } curl_fetch() { @@ -54,6 +64,7 @@ active_preset() { arg_inst="$1" arg_cmnd="$2" arg_param="$3" +exit_code=0 # Clear positional arguments. set -- @@ -132,6 +143,7 @@ for wled_host; do # Prepend description and mark active preset. sed --sandbox -e "1i\\Presets on $wled_host:" \ -e "s/^ \?$active_ps:.*/& */" + ;; "") # Missing command. echo "Error: missing command." >&2 @@ -145,15 +157,19 @@ for wled_host; do ;; esac - # Check curl exit status - curl_exit=$? - if test $curl_exit -ne 0; then - echo "Error: curl exited with exit code $curl_exit." >&2 + # 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)) fi - # Check API responce. + # Check API response. if test -n "$api_resp" && echo "$api_resp" | grep -qv '{\s*"success"\s*:\s*true\s*}'; then - echo "Warning: unexpected responce from WLED API." >&2 + echo "Warning: unexpected response from WLED API." >&2 + exit_code=$((exit_code|4)) fi done +exit $exit_code + From 38ccaaec15f84767c87a983ab421f0221f35226a Mon Sep 17 00:00:00 2001 From: Shy Date: Tue, 6 Aug 2024 16:47:46 +0200 Subject: [PATCH 3/3] Print brightness if -b is called without parameter. --- README.md | 2 +- wled | 34 +++++++++++++++++++++------------- 2 files changed, 22 insertions(+), 14 deletions(-) diff --git a/README.md b/README.md index a44e363..8a13147 100644 --- a/README.md +++ b/README.md @@ -13,7 +13,7 @@ Switch between presets 1 to 17 or list available presets: `$ wled -f|-w|-a [1-17|l(ist)]` -Change brightness: +Display or change brightness: `$ wled -f|-w|-a -b [1-255]` diff --git a/wled b/wled index 775914e..bb15ae2 100755 --- a/wled +++ b/wled @@ -33,7 +33,7 @@ commands: on|off switch on/off [1-17] switch preset l, list list presets - -b,--brightness [1-255] set brightness + -b,--brightness [1-255] get or set brightness exit status: 0 no failure @@ -61,6 +61,12 @@ active_preset() { sed -n 's/.*"ps"[[:space:]]*:[[:space:]]*\(1\?[0-9]\).*/\1/p' } +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' +} + arg_inst="$1" arg_cmnd="$2" arg_param="$3" @@ -107,19 +113,21 @@ for wled_host; do ;; -b|--brightness) # Set brightness. if test -z "$arg_param"; then - echo "Error: missing parameter for brightness." >&2 - exit 1 + # No parameter given. Print current brightness. + echo "Current brightness of $wled_host:" + current_brightness "$wled_host" + else + # Try to convert 3rd parameter to an integer between 1 an 255. + bright=$(printf '%u' "$arg_param" 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 "{'bri':$bright}" "http://$wled_host/json") fi - # Try to convert 3rd parameter to an integer between 1 an 255. - bright=$(printf '%u' "$arg_param" 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 "{'bri':$bright}" "http://$wled_host/json") ;; [1-9]|1[0-7]) # Switch to preset. api_resp=$(curl_send "{'ps':$arg_cmnd}" "http://$wled_host/json")