77 lines
1.9 KiB
Bash
Executable file
77 lines
1.9 KiB
Bash
Executable file
#!/bin/sh
|
|
#
|
|
# Simpel command line tool to turn predefined wled instances on/off or switch
|
|
# presets.
|
|
#
|
|
# 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"
|
|
}
|
|
|
|
if test $# -ne 2; then
|
|
print_usage
|
|
exit 0
|
|
fi
|
|
|
|
# Parse host argument.
|
|
case "$1" in
|
|
f|fnordcenter)
|
|
wled_host='wled-fnordcenter.local'
|
|
;;
|
|
w|wohnzimmer)
|
|
wled_host='wled-wohnzimmer.local'
|
|
;;
|
|
*)
|
|
echo "Error: unknown host \"$1\"." >&2
|
|
print_usage
|
|
exit 1
|
|
;;
|
|
esac
|
|
|
|
# Parse and execute command argument.
|
|
case "$2" in
|
|
on)
|
|
api_resp=$(curl $CURL_FLAGS --json '{"on":true}' "http://$wled_host/json")
|
|
;;
|
|
off)
|
|
api_resp=$(curl $CURL_FLAGS --json '{"on":false}' "http://$wled_host/json")
|
|
;;
|
|
[1-9]|[1-9][0-7])
|
|
api_resp=$(curl $CURL_FLAGS --json "{'on':true,'ps':$2}" "http://$wled_host/json")
|
|
;;
|
|
l|list)
|
|
echo "Presets on $wled_host:"
|
|
curl $CURL_FLAGS "http://$wled_host/presets.json" | \
|
|
# Split into lines. One line per preset.
|
|
sed --sandbox -n 's/"\([0-9]\{1,2\}\)"/\n"\1"/gp' | \
|
|
# Extract number and name. Append final newline.
|
|
sed --sandbox -n 's/^"\([0-9]\{1,2\}\)":.*"n":"\([[:print:]]*\)".*$/\1: \2/p; a\'
|
|
exit $?
|
|
;;
|
|
dump)
|
|
curl $CURL_FLAGS -w '\n' "http://$wled_host/json"
|
|
exit 0
|
|
;;
|
|
*)
|
|
echo "Error: unknown command \"$2\"." >&2
|
|
print_usage
|
|
exit 1
|
|
;;
|
|
esac
|
|
|
|
curl_exit=$?
|
|
if test $curl_exit -ne 0; then
|
|
echo "Error: curl exited with exit code $curl_exit."
|
|
exit 1
|
|
fi
|
|
|
|
# Check API responce. Should be {"success":true}.
|
|
if test "$(echo -n "$api_resp"|tr -c -d '[:alpha:]')" != 'successtrue'; then
|
|
echo "Error: unexpected responce from WLED API." >&2
|
|
exit 1
|
|
fi
|
|
|