From 90933f91a477c583ee48cefb325430ff687c7089 Mon Sep 17 00:00:00 2001 From: Shy Date: Sat, 3 Aug 2024 23:39:46 +0200 Subject: [PATCH] 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.