114 lines
3.5 KiB
Bash
Executable file
114 lines
3.5 KiB
Bash
Executable file
#!/usr/bin/env bash
|
|
|
|
# author: Daniel Sommer <daniel.sommer@velvettear.de>
|
|
# license: MIT
|
|
|
|
# blinky
|
|
blinky_url="${BLINKY_URL:-localhost:3000}"
|
|
blinky_type="${BLINKY_MODE:-morph}"
|
|
blinky_duration="${BLINKY_DURATON:-2500}"
|
|
blinky_mode="${BLINKY_MODE:-$1}"
|
|
|
|
# temperature
|
|
temperature_high="${BLINKY_TEMPERATURE_HIGH:-85000}"
|
|
temperature_critical="${BLINKY_TEMPERATURE_CRITICAL:-90000}"
|
|
threshold_upper="${BLINKY_THRESHOLD_UPPER:-66}"
|
|
threshold_lower="${BLINKY_THRESHOLD_LOWER:-33}"
|
|
thermal_zones=(
|
|
"*"
|
|
)
|
|
|
|
#############
|
|
# functions #
|
|
#############
|
|
# check for root permissions
|
|
function checkRoot() {
|
|
[[ "$EUID" != 0 ]] && printf "error: permission denied!\n" && exit 1
|
|
}
|
|
|
|
# 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... "
|
|
percentage="$(bc <<< "$1 / ( $temperature_high / 100 )")"
|
|
[[ "$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
|
|
cmd="$cmd \"$blinky_url\" -s"
|
|
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"
|
|
[[ "$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
|
|
}
|
|
|
|
|
|
#############
|
|
# main part #
|
|
#############
|
|
argument_count="$#"
|
|
index=1
|
|
while [[ "$index" -lt $(( $argument_count -1 )) ]]; do
|
|
argument="${@: $index: 1}"
|
|
argument="${argument,,}"
|
|
(( index++ ))
|
|
printf "checking arg $argument\n"
|
|
case "$argument" in
|
|
-u|--url|--blinky-url)
|
|
blinky_url="${@: $index: 1}"
|
|
;;
|
|
-t|--type|--blinky-type)
|
|
blinky_type="${@: $index: 1}"
|
|
;;
|
|
-d|--duration|--blinky-duration)
|
|
blinky_duration="${@: $index: 1}"
|
|
;;
|
|
-m|--mode|--blinky-mode)
|
|
blinky_mode="${@: $index: 1}"
|
|
;;
|
|
esac
|
|
done
|
|
|
|
case "$blinky_mode" in
|
|
t|temp|temperature)
|
|
checkRoot
|
|
getTemperature
|
|
temperatureToRGB "$result"
|
|
sendPOST "color=$result" "mode=$blinky_type" "duration=$blinky_duration"
|
|
;;
|
|
*)
|
|
sendPOST "color=random" "mode=$blinky_type" "duration=$blinky_duration"
|
|
;;
|
|
esac
|