2021-04-06 16:41:49 +02:00
|
|
|
#!/usr/bin/env bash
|
|
|
|
|
|
|
|
# author: Daniel Sommer <daniel.sommer@velvettear.de>
|
|
|
|
# license: MIT
|
|
|
|
|
|
|
|
# blinky
|
2021-04-09 02:04:53 +02:00
|
|
|
blinky_url="${BLINKY_URL:-localhost:3000}"
|
2021-04-14 23:11:15 +02:00
|
|
|
blinky_type="${BLINKY_TYPE:-morph}"
|
2021-04-09 02:04:53 +02:00
|
|
|
blinky_duration="${BLINKY_DURATON:-2500}"
|
|
|
|
blinky_mode="${BLINKY_MODE:-$1}"
|
2021-04-06 16:41:49 +02:00
|
|
|
|
|
|
|
# temperature
|
2021-04-09 02:04:53 +02:00
|
|
|
temperature_high="${BLINKY_TEMPERATURE_HIGH:-85000}"
|
|
|
|
temperature_critical="${BLINKY_TEMPERATURE_CRITICAL:-90000}"
|
|
|
|
threshold_upper="${BLINKY_THRESHOLD_UPPER:-66}"
|
|
|
|
threshold_lower="${BLINKY_THRESHOLD_LOWER:-33}"
|
2021-04-06 16:41:49 +02:00
|
|
|
thermal_zones=(
|
2021-04-14 23:11:15 +02:00
|
|
|
"0"
|
|
|
|
"1"
|
|
|
|
"2"
|
|
|
|
"3"
|
2021-04-06 16:41:49 +02:00
|
|
|
)
|
|
|
|
|
2021-04-08 17:09:44 +02:00
|
|
|
#############
|
|
|
|
# functions #
|
|
|
|
#############
|
|
|
|
# check for root permissions
|
2021-04-06 17:07:06 +02:00
|
|
|
function checkRoot() {
|
|
|
|
[[ "$EUID" != 0 ]] && printf "error: permission denied!\n" && exit 1
|
|
|
|
}
|
|
|
|
|
2021-04-06 16:41:49 +02:00
|
|
|
# convert a temperature value to a rgb color value
|
|
|
|
function temperatureToRGB() {
|
|
|
|
[[ ! "$1" ]] && printf "error: did not receive a temperature value to convert to a rgb value\n" && exit 1
|
|
|
|
printf "converting temperature to rgb value... "
|
2021-04-09 02:04:53 +02:00
|
|
|
percentage="$(bc <<< "$1 / ( $temperature_high / 100 )")"
|
2021-04-06 16:41:49 +02:00
|
|
|
[[ "$percentage" -gt "100" ]] && percentage="100"
|
|
|
|
color_main="$(bc <<< "$percentage * 2.55")"
|
|
|
|
color_supplement="$(bc <<< "255 - $color_main")"
|
|
|
|
color_unused="0"
|
|
|
|
if [[ "$percentage" -ge "$threshold_upper" ]]; then
|
|
|
|
result="$color_main, $color_supplement, $color_unused"
|
|
|
|
elif [[ "$percentage" -ge "33" ]]; then
|
|
|
|
result="$color_supplement, $color_main, $color_unused"
|
|
|
|
else
|
|
|
|
result="$color_unused, $color_supplement, $color_main"
|
|
|
|
fi
|
|
|
|
printf "result: '$result'\n"
|
|
|
|
}
|
|
|
|
|
|
|
|
# send http post request to blinky
|
|
|
|
function sendPOST() {
|
|
|
|
[[ ! "$1" ]] && printf "error: did not receive any arguments for post request\n" && exit 1
|
|
|
|
cmd="curl -X POST"
|
|
|
|
for arg in "$@"; do
|
|
|
|
cmd="$cmd -d \"$arg\""
|
|
|
|
done
|
2021-04-09 02:04:53 +02:00
|
|
|
cmd="$cmd \"$blinky_url\" -s"
|
2021-04-06 16:41:49 +02:00
|
|
|
printf "sending post request '$cmd'...\n"
|
|
|
|
eval "$cmd"
|
|
|
|
}
|
|
|
|
|
|
|
|
# get (average) temperature from defined thermal zones
|
|
|
|
function getTemperature() {
|
|
|
|
counter="0"
|
|
|
|
printf "getting temperature value from thermal zone(s) "
|
|
|
|
for zone in "${thermal_zones[@]}"; do
|
|
|
|
printf "'"$zone"'... "
|
|
|
|
for value in $(cat "/sys/devices/virtual/thermal/thermal_zone"$zone"/temp"); do
|
|
|
|
[[ ! "$temp" ]] && temp="$value" || temp=$(( $temp + $value ))
|
|
|
|
(( counter++ ))
|
|
|
|
done
|
|
|
|
done
|
|
|
|
result="$(( $temp / $counter ))"
|
|
|
|
printf "result: '$result'\n"
|
2021-04-09 02:04:53 +02:00
|
|
|
[[ "$result" -ge "$temperature_critical" ]] && printf "warning: critical temperature reached\n" && blinky_type="pulse" && blinky_duration="500" && return 0
|
|
|
|
[[ "$result" -ge "$temperature_high" ]] && printf "warning: maximum temperature reached\n" && blinky_type="pulse" && blinky_duration="1500" && return 0
|
2021-04-06 16:41:49 +02:00
|
|
|
}
|
|
|
|
|
2021-04-08 17:09:44 +02:00
|
|
|
|
|
|
|
#############
|
|
|
|
# main part #
|
|
|
|
#############
|
|
|
|
argument_count="$#"
|
|
|
|
index=1
|
|
|
|
while [[ "$index" -lt $(( $argument_count -1 )) ]]; do
|
|
|
|
argument="${@: $index: 1}"
|
|
|
|
argument="${argument,,}"
|
|
|
|
(( index++ ))
|
2021-04-09 02:04:53 +02:00
|
|
|
printf "checking arg $argument\n"
|
2021-04-08 17:09:44 +02:00
|
|
|
case "$argument" in
|
2021-04-09 02:04:53 +02:00
|
|
|
-u|--url|--blinky-url)
|
|
|
|
blinky_url="${@: $index: 1}"
|
|
|
|
;;
|
|
|
|
-t|--type|--blinky-type)
|
|
|
|
blinky_type="${@: $index: 1}"
|
2021-04-08 17:09:44 +02:00
|
|
|
;;
|
2021-04-09 02:04:53 +02:00
|
|
|
-d|--duration|--blinky-duration)
|
2021-04-08 17:09:44 +02:00
|
|
|
blinky_duration="${@: $index: 1}"
|
|
|
|
;;
|
2021-04-09 02:04:53 +02:00
|
|
|
-m|--mode|--blinky-mode)
|
|
|
|
blinky_mode="${@: $index: 1}"
|
2021-04-08 17:09:44 +02:00
|
|
|
;;
|
|
|
|
esac
|
|
|
|
done
|
|
|
|
|
2021-04-09 02:04:53 +02:00
|
|
|
case "$blinky_mode" in
|
|
|
|
t|temp|temperature)
|
2021-04-06 17:07:06 +02:00
|
|
|
checkRoot
|
2021-04-06 16:41:49 +02:00
|
|
|
getTemperature
|
|
|
|
temperatureToRGB "$result"
|
2021-04-09 02:04:53 +02:00
|
|
|
sendPOST "color=$result" "mode=$blinky_type" "duration=$blinky_duration"
|
2021-04-06 16:41:49 +02:00
|
|
|
;;
|
|
|
|
*)
|
2021-04-09 02:04:53 +02:00
|
|
|
sendPOST "color=random" "mode=$blinky_type" "duration=$blinky_duration"
|
2021-04-06 16:41:49 +02:00
|
|
|
;;
|
|
|
|
esac
|