#! /usr/bin/env bash set -xeuo pipefail lastNotifiedFile="@htmlDir@/last_notified" now="$(@coreutils@/bin/date --iso=seconds)" nowTs="$(@coreutils@/bin/date +"%s")" nowFormatted="$(@coreutils@/bin/date +"on %F, at %T")" if [[ ! -f "${lastNotifiedFile}" ]]; then echo "${nowTs}" > "${lastNotifiedFile}" fi doNotify() { lastNotified="$(cat "${lastNotifiedFile}")" if [[ "$(( ${nowTs} - ${lastNotified} ))" < "$(( 10 * 60 ))" ]]; then @curl@/bin/curl -i -X POST -H 'Content-Type: application/json' -d '{"text": "'"The boiler broke down ${nowFormatted} :/"'"}' "${MATTERMOST_WEBHOOK}" echo "${nowTs}" > "${lastNotifiedFile}" fi } imageFilename="chaudiered-${now}.jpg" imageFile="@htmlDir@/${imageFilename}" smileys=( ":/" ":D" ) messages=( "The fucking boiler is broken, again..." "The boiler is working properly." ) backgroundColors=( "FCC" "CCF" ) isWorking=0 @fswebcam@/bin/fswebcam -d /dev/video0 --save "${imageFile}" luminosity="$(@imagemagick@/bin/identify -format %[mean] "${imageFile}" | @coreutils@/bin/cut -d'.' -f1)" if [[ "${luminosity}" > 6300 ]]; then isWorking=1 else isWorking=0 fi cat < "@htmlDir@/metrics" chaudiered_status ${isWorking} chaudiered_luminosity ${luminosity} EOF cat < "@htmlDir@/index.html" systemd-chaudiered

${smileys[${isWorking}]}

${messages[${isWorking}]}

Last update ${nowFormatted}

EOF if [[ "${isWorking}" == 0 ]]; then doNotify fi