diff --git a/wled b/wled index 260c8be..946d282 100755 --- a/wled +++ b/wled @@ -10,22 +10,22 @@ 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]" } -if test $# -ne 2; then - print_usage - exit 0 -fi - # Parse host argument. case "$1" in + ""|-h|--h) # Missing host parameter. + print_usage + exit 0 + ;; f|fnordcenter) wled_host='wled-fnordcenter.local' ;; w|wohnzimmer) wled_host='wled-wohnzimmer.local' ;; - *) + *) # Unknown host parameter. echo "Error: unknown host \"$1\"." >&2 print_usage exit 1 @@ -34,16 +34,32 @@ esac # Parse and execute command argument. case "$2" in - on) + on) # Switch on. api_resp=$(curl $CURL_FLAGS --json '{"on":true}' "http://$wled_host/json") ;; - off) + off) # Switch off. api_resp=$(curl $CURL_FLAGS --json '{"on":false}' "http://$wled_host/json") ;; - [1-9]|[1-9][0-7]) + 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. api_resp=$(curl $CURL_FLAGS --json "{'on':true,'ps':$2}" "http://$wled_host/json") ;; - l|list) + l|list) # List presets. echo "Presets on $wled_host:" curl $CURL_FLAGS "http://$wled_host/presets.json" | \ # Split into lines - one line per preset. Append final newline. @@ -52,11 +68,16 @@ case "$2" in sed --sandbox -n 's/^"\(1\?[0-9]\)":.*"n":"\([[:print:]]*\)".*$/\1: \2/p' exit $? ;; - dump) + dump) # Dump JSON response from API. curl $CURL_FLAGS -w '\n' "http://$wled_host/json" exit 0 ;; - *) + "") # Missing command. + echo "Error: missing command." >&2 + print_usage + exit 1 + ;; + *) # Unknown command. echo "Error: unknown command \"$2\"." >&2 print_usage exit 1 @@ -65,7 +86,7 @@ esac curl_exit=$? if test $curl_exit -ne 0; then - echo "Error: curl exited with exit code $curl_exit." + echo "Error: curl exited with exit code $curl_exit." >&2 exit 1 fi