chaudiered.sh 1.96 KB
Newer Older
1 2
#! /usr/bin/env bash

3 4 5 6
set -xeuo pipefail

lastNotifiedFile="@htmlDir@/last_notified"

7
now="$(@coreutils@/bin/date --iso=seconds)"
8 9 10 11 12 13 14 15 16 17 18 19 20 21
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
}
22 23 24 25 26 27 28 29 30 31 32 33 34 35

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)"

36
if [[ "${luminosity}" > 6300 ]]; then
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
  isWorking=1
else
  isWorking=0
fi

cat <<EOF > "@htmlDir@/metrics"
chaudiered_status ${isWorking}
chaudiered_luminosity ${luminosity}
EOF

cat <<EOF > "@htmlDir@/index.html"
<!DOCTYPE html>
<html lang='en'>
<head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>systemd-chaudiered</title>

  <style>
    body {
      background-color: #${backgroundColors[${isWorking}]};
    }

    .small {
      color: #4F4F4F;
    }
    .medium {
      font-size: 50px;
    }
    .big {
      font-size: 100px;
    }

    body > div {
      margin-left: -50px;
      padding-left: 50%;
    }
  </style>
</head>

<body>
  <div>
    <p class="big">${smileys[${isWorking}]}</p>
    <p class="medium">${messages[${isWorking}]}</p>
80
    <p class="small">Last update ${nowFormatted}</p>
81 82 83 84 85
    <img src="/${imageFilename}" />
  </div>
</body>
</html>
EOF
86 87 88 89

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