Use a function to call curl.

This commit is contained in:
Shy 2024-08-03 23:39:46 +02:00
parent b3109c859e
commit 90933f91a4

18
wled
View file

@ -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,14 +56,14 @@ 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\ ' | \
# Extract number and name of every preset.
@ -69,7 +71,7 @@ case "$2" in
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.