#!/usr/bin/env bash # author: Daniel Sommer # license: MIT # blinky blinky_url="127.0.0.1:3000" blinky_mode="morph" blinky_duration="2500" # temperature max_temp="65000" crit_temp="70000" threshold_upper="66" threshold_lower="33" thermal_zones=( "3" ) # argument to lowercase arg="${1,,}" # functions 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 / ( $max_temp / 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\"" 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 "$crit_temp" ]] && printf "warning: critical temperature reached\n" && blinky_mode="pulse" && blinky_duration="500" && return 0 [[ "$result" -ge "$max_temp" ]] && printf "warning: maximum temperature reached\n" && blinky_mode="pulse" && blinky_duration="1500" && return 0 } # main part case "$arg" in -t|--temp|--temperature) checkRoot getTemperature temperatureToRGB "$result" sendPOST "color=$result" "mode=$blinky_mode" "duration=$blinky_duration" ;; *) printf "error: no argument given or argument unknown!\n" && exit 1 ;; esac