From b3109c859ea47baccc0c8480b4a05b500fed29ab Mon Sep 17 00:00:00 2001 From: Shy Date: Sat, 3 Aug 2024 23:24:07 +0200 Subject: [PATCH 1/6] Simplified check of API response. --- wled | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/wled b/wled index 946d282..a35e524 100755 --- a/wled +++ b/wled @@ -10,7 +10,7 @@ CURL_FLAGS="--silent --show-error --fail --ipv4 --max-filesize 64K" print_usage() { echo "Usage: $0 f(nordcenter)|w(ohnzimmer) on|off|1-17|list" - echo " $0 f(nordcenter)|w(ohnzimmer) b(rightness) [1-255]" + echo " $0 f(nordcenter)|w(ohnzimmer) b(rightness) 1-255" } # Parse host argument. @@ -90,8 +90,8 @@ if test $curl_exit -ne 0; then exit 1 fi -# Check API responce. Should be {"success":true}. -if test "$(echo -n "$api_resp"|tr -c -d '[:alpha:]')" != 'successtrue'; then +# Check API responce. +if test "$api_resp" != '{"success":true}'; then echo "Error: unexpected responce from WLED API." >&2 exit 1 fi From 90933f91a477c583ee48cefb325430ff687c7089 Mon Sep 17 00:00:00 2001 From: Shy Date: Sat, 3 Aug 2024 23:39:46 +0200 Subject: [PATCH 2/6] Use a function to call curl. --- wled | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/wled b/wled index a35e524..59cbec4 100755 --- a/wled +++ b/wled @@ -6,13 +6,15 @@ # Author: Shy # License: CC0 -CURL_FLAGS="--silent --show-error --fail --ipv4 --max-filesize 64K" - print_usage() { echo "Usage: $0 f(nordcenter)|w(ohnzimmer) on|off|1-17|list" echo " $0 f(nordcenter)|w(ohnzimmer) b(rightness) 1-255" } +curl_cmd() { + curl --silent --show-error --fail --ipv4 --max-filesize 32K "$@" +} + # Parse host argument. case "$1" in ""|-h|--h) # Missing host parameter. @@ -35,10 +37,10 @@ esac # Parse and execute command argument. case "$2" in on) # Switch on. - api_resp=$(curl $CURL_FLAGS --json '{"on":true}' "http://$wled_host/json") + api_resp=$(curl_cmd --json '{"on":true}' "http://$wled_host/json") ;; off) # Switch off. - api_resp=$(curl $CURL_FLAGS --json '{"on":false}' "http://$wled_host/json") + api_resp=$(curl_cmd --json '{"on":false}' "http://$wled_host/json") ;; b|brightness) # Set brightness. if test -z "$3"; then @@ -54,22 +56,22 @@ case "$2" in 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") + api_resp=$(curl_cmd --json "{'bri':$bright}" "http://$wled_host/json") ;; [1-9]|1[0-7]) # Switch to preset. - api_resp=$(curl $CURL_FLAGS --json "{'on':true,'ps':$2}" "http://$wled_host/json") + api_resp=$(curl_cmd --json "{'on':true,'ps':$2}" "http://$wled_host/json") ;; l|list) # List presets. echo "Presets on $wled_host:" - curl $CURL_FLAGS "http://$wled_host/presets.json" | \ + curl_cmd "http://$wled_host/presets.json" | \ # Split into lines - one line per preset. Append final newline. - sed --sandbox 's/"\(1\?[0-9]\)"/\n"\1"/g; $a\' | \ + 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' exit $? ;; dump) # Dump JSON response from API. - curl $CURL_FLAGS -w '\n' "http://$wled_host/json" + curl_cmd -w '\n' "http://$wled_host/json" exit 0 ;; "") # Missing command. From 4ff0369e761ee0685faeb679e6df6206a56c3ff7 Mon Sep 17 00:00:00 2001 From: Shy Date: Sun, 4 Aug 2024 00:19:34 +0200 Subject: [PATCH 3/6] Dropped unnecessary "on" command. --- wled | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/wled b/wled index 59cbec4..7e1bf1b 100755 --- a/wled +++ b/wled @@ -59,7 +59,7 @@ case "$2" in api_resp=$(curl_cmd --json "{'bri':$bright}" "http://$wled_host/json") ;; [1-9]|1[0-7]) # Switch to preset. - api_resp=$(curl_cmd --json "{'on':true,'ps':$2}" "http://$wled_host/json") + api_resp=$(curl_cmd --json "{'ps':$2}" "http://$wled_host/json") ;; l|list) # List presets. echo "Presets on $wled_host:" @@ -86,6 +86,7 @@ case "$2" in ;; esac +# Check curl exit status curl_exit=$? if test $curl_exit -ne 0; then echo "Error: curl exited with exit code $curl_exit." >&2 From 3081b16a22568e1cf7f56cf576e1aaa36ca9e2d3 Mon Sep 17 00:00:00 2001 From: Shy Date: Sun, 4 Aug 2024 00:54:54 +0200 Subject: [PATCH 4/6] Made command line options more POSIX-like. --- wled | 37 ++++++++++++++++++++++++++++++------- 1 file changed, 30 insertions(+), 7 deletions(-) diff --git a/wled b/wled index 7e1bf1b..5493e9e 100755 --- a/wled +++ b/wled @@ -6,9 +6,28 @@ # Author: Shy # License: CC0 +COMMAND="wled" +VERSION="0.0.1" + print_usage() { - echo "Usage: $0 f(nordcenter)|w(ohnzimmer) on|off|1-17|list" - echo " $0 f(nordcenter)|w(ohnzimmer) b(rightness) 1-255" + echo "\ +Usage: $COMMAND -f|-w [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" } curl_cmd() { @@ -17,14 +36,18 @@ curl_cmd() { # Parse host argument. case "$1" in - ""|-h|--h) # Missing host parameter. - print_usage + ""|-h|--help) # Missing host parameter. + print_help exit 0 ;; - f|fnordcenter) + -v|--version) + echo "wled.sh version $VERSION" + exit 0 + ;; + -f|--fnordcenter) wled_host='wled-fnordcenter.local' ;; - w|wohnzimmer) + -w|--wohnzimmer) wled_host='wled-wohnzimmer.local' ;; *) # Unknown host parameter. @@ -42,7 +65,7 @@ case "$2" in off) # Switch off. api_resp=$(curl_cmd --json '{"on":false}' "http://$wled_host/json") ;; - b|brightness) # Set brightness. + -b|--brightness) # Set brightness. if test -z "$3"; then echo "Error: missing parameter for brightness." >&2 exit 1 From 1f825347abbc75ff1030f057fbf9560891a57837 Mon Sep 17 00:00:00 2001 From: Shy Date: Sun, 4 Aug 2024 10:11:16 +0200 Subject: [PATCH 5/6] Rewrote help and description. --- README.md | 17 ++++++++++------- wled | 9 +++++---- 2 files changed, 15 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index 97d5231..13c0021 100644 --- a/README.md +++ b/README.md @@ -1,21 +1,24 @@ # wled.sh -Simple shell script to turn predefined wled instances on/off, change brightness -and switch presets. - -Requires *curl* version 7.82.0 or newer. +Turn WLED installations in the C4 hackspace on/off, change brightness or switch +between presets. ## Usage Switch on/off: -`wled f(nordcenter)|w(ohnzimmer) on|off` +`$ wled -f(nordcenter)|-w(wohnzimmer) on|off` Switch between presets 1 to 17 or list available presets: -`wled f(nordcenter)|w(ohnzimmer) 1-17|l(ist)` +`$ wled -f|-w 1-17|l` Change brightness: -`wled f(nordcenter)|w(ohnzimmer) b(rightness) 1-255` +`$ wled -f|-w -b 1-255` + +## Requirements + +*curl* version 7.82.0 or newer. +You have to be connected to the local network. diff --git a/wled b/wled index 5493e9e..344f299 100755 --- a/wled +++ b/wled @@ -1,17 +1,18 @@ #!/bin/sh # -# Simpel command line tool to turn predefined wled instances on/off or switch -# presets. +# Turn WLED installations in the C4 hackspace on/off, change brightness or +# switch between presets. # # Author: Shy # License: CC0 +NAME="wled.sh" COMMAND="wled" VERSION="0.0.1" print_usage() { echo "\ -Usage: $COMMAND -f|-w [on|off] +Usage: $COMMAND -f(norcenter)|-w(wohnzimmer) [on|off] $COMMAND -f|-w [1-17|list] $COMMAND -f|-w -b [1-255]" } @@ -41,7 +42,7 @@ case "$1" in exit 0 ;; -v|--version) - echo "wled.sh version $VERSION" + echo "$NAME version $VERSION" exit 0 ;; -f|--fnordcenter) From 4111037d4c2e0c06f5410df4ba2b937f891ae2e1 Mon Sep 17 00:00:00 2001 From: Shy Date: Mon, 5 Aug 2024 15:51:46 +0200 Subject: [PATCH 6/6] Revised regular expressions. --- wled | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/wled b/wled index 344f299..49aff8e 100755 --- a/wled +++ b/wled @@ -86,12 +86,14 @@ case "$2" in api_resp=$(curl_cmd --json "{'ps':$2}" "http://$wled_host/json") ;; l|list) # List presets. - echo "Presets on $wled_host:" + printf "loading ...\r" curl_cmd "http://$wled_host/presets.json" | \ # 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' + sed --sandbox 's/"\(1\?[0-9]\)"[[:space:]]*:/\n&/g; $a\ ' | \ + # Extract number and name of every preset. Prepend description. + sed --sandbox -n \ + -e 's/^"\(1\?[0-9]\)"[[:space:]]*:.*"n"[[:space:]]*:[[:space:]]*"\([[:print:]]*\)".*$/\1: \2/p' \ + -e "1i\\Presets on $wled_host:" exit $? ;; dump) # Dump JSON response from API. @@ -118,7 +120,7 @@ if test $curl_exit -ne 0; then fi # Check API responce. -if test "$api_resp" != '{"success":true}'; then +if echo "$api_resp" | grep -qv '{\s*"success"\s*:\s*true\s*}'; then echo "Error: unexpected responce from WLED API." >&2 exit 1 fi