From c66362f87db4d6b94d284903f8ee920a7ebd7588 Mon Sep 17 00:00:00 2001 From: Shy Date: Tue, 6 Aug 2024 11:29:44 +0200 Subject: [PATCH] Removed dependency on curls "--json" flag. --- README.md | 1 - wled | 25 ++++++++++++++++--------- 2 files changed, 16 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index 13c0021..606d17b 100644 --- a/README.md +++ b/README.md @@ -19,6 +19,5 @@ Change brightness: ## Requirements -*curl* version 7.82.0 or newer. You have to be connected to the local network. diff --git a/wled b/wled index 62eb85b..fcb3405 100755 --- a/wled +++ b/wled @@ -31,14 +31,21 @@ commands: -b,--brightness [1-255] set brightness" } -curl_cmd() { - curl --silent --show-error --fail --ipv4 --max-filesize 32K "$@" +curl_fetch() { + curl --silent --show-error --fail --ipv4 --max-filesize 32768 \ + --max-redirs 0 --header 'Accept: application/json' "$@" +} + +curl_send() { + curl --silent --show-error --fail --ipv4 --max-filesize 1024 \ + --max-redirs 0 --header 'Accept: application/json' \ + --header 'Content-Type: application/json' "$@" } active_preset() { # Find number of active preset. API returns -1 if none is active, we return # an empty string. - curl_cmd "http://$1/json" | \ + curl_fetch "http://$1/json" | \ # -1 will be a no-match. sed -n 's/.*"ps"[[:space:]]*:[[:space:]]*\(1\?[0-9]\).*/\1/p' } @@ -69,10 +76,10 @@ esac # Parse and execute command argument. case "$2" in on) # Switch on. - api_resp=$(curl_cmd --json '{"on":true}' "http://$wled_host/json") + api_resp=$(curl_send --data '{"on":true}' "http://$wled_host/json") ;; off) # Switch off. - api_resp=$(curl_cmd --json '{"on":false}' "http://$wled_host/json") + api_resp=$(curl_send --data '{"on":false}' "http://$wled_host/json") ;; -b|--brightness) # Set brightness. if test -z "$3"; then @@ -88,15 +95,15 @@ case "$2" in echo "Error: parameter for brightness is out of range." >&2 exit 1 fi - api_resp=$(curl_cmd --json "{'bri':$bright}" "http://$wled_host/json") + api_resp=$(curl_send --data "{'bri':$bright}" "http://$wled_host/json") ;; [1-9]|1[0-7]) # Switch to preset. - api_resp=$(curl_cmd --json "{'ps':$2}" "http://$wled_host/json") + api_resp=$(curl_send --data "{'ps':$2}" "http://$wled_host/json") ;; l|list) # List presets. printf "loading ...\r" active_ps=$(active_preset "$wled_host") - curl_cmd "http://$wled_host/presets.json" | \ + 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. @@ -115,7 +122,7 @@ case "$2" in exit $? ;; dump) # Dump JSON response from API. - curl_cmd -w '\n' "http://$wled_host/json" + curl_fetch -w '\n' "http://$wled_host/json" exit 0 ;; "") # Missing command.