#!/usr/bin/env bash # author: Daniel Sommer # license: MIT # default_ # 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" ) ############# # 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 / ( $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 # ############# argument_count="$#" index=1 [[ "$argument_count" -lt 1 ]] && printf "error: missing arguments\n" && exit 1 url="${@: $argument_count: 1}" while [[ "$index" -lt $(( $argument_count -1 )) ]]; do argument="${@: $index: 1}" argument="${argument,,}" (( index++ )) case "$argument" in -bm|--blinky-mode) blinky_mode="${@: $index: 1}" ;; -bd|--blinky-duration) blinky_duration="${@: $index: 1}" ;; -m|--mode) mode="${@: $index: 1}" ;; esac done printf "checking mode '$mode'...\n" case "$mode" in -t|--temp|--temperature) checkRoot getTemperature temperatureToRGB "$result" sendPOST "color=$result" "mode=$blinky_mode" "duration=$blinky_duration" ;; *) sendPOST "color=random" "mode=$blinky_mode" "duration=$blinky_duration" ;; esac