#!/bin/bash
##################################################################################
#                                                                                #
# Generates graphs and images of telemetry data for website                      #
# Run at the same frequency as telemetry data comes in                           #
# For example in the crontab                                                     #
                                                                                 #
# Data format:                                                                   #
# [date],1,2,3,4,5,6,7,8,9,10                                                    #
#                                                                                #
#  1 = temperature outside                                                       #
#  2 = humidity outside                                                          #
#  3 = temperature small greenhouse                                              #
#  4 = humidity small greenhouse                                                 #
#  5 = soil moisture small greenhouse                                            #
#  6 = soil moisture small greenhouse                                            #
#  7 = temperature large greenhouse                                              #
#  8 = humidity large greenhouse                                                 #
#  9 = soil moisture large greenhouse                                            #
# 10 = soil moisture large greenhouse                                            #
#                                                                                #
# (C)2021 M.T. Konstapel https://meezenest.nl/mees                               #
#                                                                                # 
# This file is part of PE1RXF-APRS-server.                                       #
#                                                                                #
#    PE1RXF-APRS-server is free software: you can redistribute it and/or modify  #
#    it under the terms of the GNU General Public License as published by        #
#    the Free Software Foundation, either version 3 of the License, or           #
#    (at your option) any later version.                                         #
#                                                                                #
#    PE1RXF-APRS-server is distributed in the hope that it will be useful,       #
#    but WITHOUT ANY WARRANTY; without even the implied warranty of              #
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               #
#    GNU General Public License for more details.                                #
#                                                                                #  
#    You should have received a copy of the GNU General Public License           #
#    along with PE1RXF-APRS-server. If not, see <https://www.gnu.org/licenses/>. #
#                                                                                #
##################################################################################

APRS_RECEIVED_MESSAGES_DIR=/home/marcel/ham/aprs_utils/aprs_log/
APRS_TELEMETRY_FILE_SUFFIX=_telemetry_PE1RXF-7.dat
APRS_LATEST_TELEMETRY_FILE=latest_telemetry_PE1RXF-7.dat
APRS_MAX_DATA=telemetry_PE1RXF-7_max_values.dat
APRS_MIN_DATA=telemetry_PE1RXF-7_min_values.dat

PUBLISH_DIR=/home/marcel/ham/web-interface/telemetry

# Get current date YYYY-MM-DD
CURRENT_DATE=$(date +"%Y-%m-%d")
YESTERDAY_DATE=$(date --date="yesterday" +"%Y-%m-%d")
MONTH_AGO_DATE=$(date --date="1 month ago" +"%Y-%m-%d")

APRS_TELEMETRY_FILE_TODAY="$CURRENT_DATE$APRS_TELEMETRY_FILE_SUFFIX"
APRS_TELEMETRY_FILE_YESTERDAY="$YESTERDAY_DATE$APRS_TELEMETRY_FILE_SUFFIX"
APRS_TELEMETRY_FILE="TEMP$APRS_TELEMETRY_FILE_SUFFIX"

cat "$APRS_RECEIVED_MESSAGES_DIR$APRS_TELEMETRY_FILE_YESTERDAY" >  "$APRS_RECEIVED_MESSAGES_DIR$APRS_TELEMETRY_FILE"
cat "$APRS_RECEIVED_MESSAGES_DIR$APRS_TELEMETRY_FILE_TODAY" >> "$APRS_RECEIVED_MESSAGES_DIR$APRS_TELEMETRY_FILE"


#Temperatuur buiten en in beide kassen
/usr/bin/gnuplot -persist <<-EOFMarker
    set title "Temperatuur" font ",14" textcolor rgbcolor "royalblue"
    set datafile separator ","
    set timefmt "%Y-%m-%d %H:%M"
    set format x "%H:%M"
    set xdata time
    set xrange ["$YESTERDAY_DATE 00:00":"$CURRENT_DATE 23:59"]
    set yrange [-20:45]
    set xlabel "tijd"
    set ylabel "graden Celcius"
    set ytics 10
    set mytics 2
    set mxtics 2
    set grid ytics lt 0 lw 1 lc rgb "#bbbbbb"
    set grid mytics
    set grid xtics lt 0 lw 1 lc rgb "#bbbbbb"
    set grid mxtics
    set pointsize 1
    set terminal png size 1024,768
    set output "$PUBLISH_DIR/temperature_graph.png"
    plot "$APRS_RECEIVED_MESSAGES_DIR$APRS_TELEMETRY_FILE" using 1:2 with lines linewidth 2 title "buiten", "$APRS_RECEIVED_MESSAGES_DIR$APRS_TELEMETRY_FILE" using 1:8 with lines linewidth 2 title "kleine kas", "$APRS_RECEIVED_MESSAGES_DIR$APRS_TELEMETRY_FILE" using 1:4 with lines linewidth 2 title "grote kas"
EOFMarker

#Luchtvochtigheid buiten en in beide kassen
/usr/bin/gnuplot -persist <<-EOFMarker
    set title "Luchtvochtigheid" font ",14" textcolor rgbcolor "royalblue"
    set datafile separator ","
    set timefmt "%Y-%m-%d %H:%M"
    set format x "%H:%M"
    set xdata time
    set xrange ["$YESTERDAY_DATE 00:00":"$CURRENT_DATE 23:59"]
    set yrange [0:100]
    set xlabel "tijd"
    set ylabel "%"
    set ytics 10
    set mytics 2
    set mxtics 2
    set grid ytics lt 0 lw 1 lc rgb "#bbbbbb"
    set grid mytics
    set grid xtics lt 0 lw 1 lc rgb "#bbbbbb"
    set grid mxtics
    set pointsize 1
    set terminal png size 1024,768
    set output "$PUBLISH_DIR/humidity_graph.png"
    plot "$APRS_RECEIVED_MESSAGES_DIR$APRS_TELEMETRY_FILE" using 1:3 with lines linewidth 2 title "buiten", "$APRS_RECEIVED_MESSAGES_DIR$APRS_TELEMETRY_FILE" using 1:9 with lines linewidth 2 title "kleine kas", "$APRS_RECEIVED_MESSAGES_DIR$APRS_TELEMETRY_FILE" using 1:5 with lines linewidth 2 title "grote kas"
EOFMarker

#Bodemvochtigheid in beide kassen
/usr/bin/gnuplot -persist <<-EOFMarker
    set title "Bodemvochtigheid" font ",14" textcolor rgbcolor "royalblue"
    set datafile separator ","
    set timefmt "%Y-%m-%d %H:%M"
    set format x "%H:%M"
    set xdata time
    set xrange ["$YESTERDAY_DATE 00:00":"$CURRENT_DATE 23:59"]
    set yrange [0:1000]
    set xlabel "tijd"
    set ylabel "absoluut"
    set ytics 100
    set mytics 2
    set mxtics 2
    set grid ytics lt 0 lw 1 lc rgb "#bbbbbb"
    set grid mytics
    set grid xtics lt 0 lw 1 lc rgb "#bbbbbb"
    set grid mxtics
    set pointsize 1
    set terminal png size 1024,768
    set output "$PUBLISH_DIR/soilmoisture_graph.png"
    plot "$APRS_RECEIVED_MESSAGES_DIR$APRS_TELEMETRY_FILE" using 1:10 with lines linewidth 2 title "kleine kas", "$APRS_RECEIVED_MESSAGES_DIR$APRS_TELEMETRY_FILE" using 1:6 with lines linewidth 2 title "grote kas"
EOFMarker

#Min-max temperatuur buiten afgelopen maand
/usr/bin/gnuplot -persist <<-EOFMarker
    set title "Minimale en maximale buitentemperatuur afgelopen maand" font ",14" textcolor rgbcolor "royalblue"
    set datafile separator ","
    set timefmt "%Y-%m-%d"
    set format x "%d %B"
    set xdata time
    set xrange ["$MONTH_AGO_DATE":"$YESTERDAY_DATE"]
    set yrange [-20:45]
    set xlabel "tijd"
    set ylabel "graden Celcius"
    set ytics 10
    set mytics 2
    set mxtics 7
    set grid ytics lt 0 lw 1 lc rgb "#bbbbbb"
    set grid mytics
    set grid xtics lt 0 lw 1 lc rgb "#bbbbbb"
    set grid mxtics
    set pointsize 1
    set terminal png size 1024,768
    set output "$PUBLISH_DIR/minmax_temperature_outside.png"
    plot "$APRS_RECEIVED_MESSAGES_DIR$APRS_MAX_DATA" using 1:2 with lines linewidth 2 title "maxima", "$APRS_RECEIVED_MESSAGES_DIR$APRS_MIN_DATA" using 1:2 with lines linewidth 2 title "minima"
EOFMarker
#Min-max temperatuur kleine kas afgelopen maand
/usr/bin/gnuplot -persist <<-EOFMarker
    set title "Minimale en maximale temperatuur kleine kas afgelopen maand" font ",14" textcolor rgbcolor "royalblue"
    set datafile separator ","
    set timefmt "%Y-%m-%d"
    set format x "%d %B"
    set xdata time
    set xrange ["$MONTH_AGO_DATE":"$YESTERDAY_DATE"]
    set yrange [-20:45]
    set xlabel "tijd"
    set ylabel "graden Celcius"
    set ytics 10
    set mytics 2
    set mxtics 7
    set grid ytics lt 0 lw 1 lc rgb "#bbbbbb"
    set grid mytics
    set grid xtics lt 0 lw 1 lc rgb "#bbbbbb"
    set grid mxtics
    set pointsize 1
    set terminal png size 1024,768
    set output "$PUBLISH_DIR/minmax_temperature_small_greenhouse.png"
    plot "$APRS_RECEIVED_MESSAGES_DIR$APRS_MAX_DATA" using 1:8 with lines linewidth 2 title "maxima", "$APRS_RECEIVED_MESSAGES_DIR$APRS_MIN_DATA" using 1:8 with lines linewidth 2 title "minima"
EOFMarker
#Min-max temperatuur grote kas afgelopen maand
/usr/bin/gnuplot -persist <<-EOFMarker
    set title "Minimale en maximale temperatuur grote kas afgelopen maand" font ",14" textcolor rgbcolor "royalblue"
    set datafile separator ","
    set timefmt "%Y-%m-%d"
    set format x "%d %B"
    set xdata time
    set xrange ["$MONTH_AGO_DATE":"$YESTERDAY_DATE"]
    set yrange [-20:45]
    set xlabel "tijd"
    set ylabel "graden Celcius"
    set ytics 10
    set mytics 2
    set mxtics 7
    set grid ytics lt 0 lw 1 lc rgb "#bbbbbb"
    set grid mytics
    set grid xtics lt 0 lw 1 lc rgb "#bbbbbb"
    set grid mxtics
    set pointsize 1
    set terminal png size 1024,768
    set output "$PUBLISH_DIR/minmax_temperature_large_greenhouse.png"
    plot "$APRS_RECEIVED_MESSAGES_DIR$APRS_MAX_DATA" using 1:4 with lines linewidth 2 title "maxima", "$APRS_RECEIVED_MESSAGES_DIR$APRS_MIN_DATA" using 1:4 with lines linewidth 2 title "minima"
EOFMarker
#Min-max luchtvochtigheid buiten afgelopen maand
/usr/bin/gnuplot -persist <<-EOFMarker
    set title "Minimale en maximale luchtvochtigheid buiten afgelopen maand" font ",14" textcolor rgbcolor "royalblue"
    set datafile separator ","
    set timefmt "%Y-%m-%d"
    set format x "%d %B"
    set xdata time
    set xrange ["$MONTH_AGO_DATE":"$YESTERDAY_DATE"]
    set yrange [0:100]
    set xlabel "tijd"
    set ylabel "%"
    set ytics 10
    set mytics 2
    set mxtics 7
    set grid ytics lt 0 lw 1 lc rgb "#bbbbbb"
    set grid mytics
    set grid xtics lt 0 lw 1 lc rgb "#bbbbbb"
    set grid mxtics
    set pointsize 1
    set terminal png size 1024,768
    set output "$PUBLISH_DIR/minmax_humidity_outside.png"
    plot "$APRS_RECEIVED_MESSAGES_DIR$APRS_MAX_DATA" using 1:3 with lines linewidth 2 title "maxima", "$APRS_RECEIVED_MESSAGES_DIR$APRS_MIN_DATA" using 1:3 with lines linewidth 2 title "minima"
EOFMarker
#Min-max luchtvochtigheid kleine kas afgelopen maand
/usr/bin/gnuplot -persist <<-EOFMarker
    set title "Minimale en maximale luchtvochtigheid kleine kas afgelopen maand" font ",14" textcolor rgbcolor "royalblue"
    set datafile separator ","
    set timefmt "%Y-%m-%d"
    set format x "%d %B"
    set xdata time
    set xrange ["$MONTH_AGO_DATE":"$YESTERDAY_DATE"]
    set yrange [0:100]
    set xlabel "tijd"
    set ylabel "%"
    set ytics 10
    set mytics 2
    set mxtics 7
    set grid ytics lt 0 lw 1 lc rgb "#bbbbbb"
    set grid mytics
    set grid xtics lt 0 lw 1 lc rgb "#bbbbbb"
    set grid mxtics
    set pointsize 1
    set terminal png size 1024,768
    set output "$PUBLISH_DIR/minmax_humidity_small_greenhouse.png"
    plot "$APRS_RECEIVED_MESSAGES_DIR$APRS_MAX_DATA" using 1:9 with lines linewidth 2 title "maxima", "$APRS_RECEIVED_MESSAGES_DIR$APRS_MIN_DATA" using 1:9 with lines linewidth 2 title "minima"
EOFMarker
#Min-max luchtvochtigheid grote kas afgelopen maand
/usr/bin/gnuplot -persist <<-EOFMarker
    set title "Minimale en maximale luchtvochtigheid grote kas afgelopen maand" font ",14" textcolor rgbcolor "royalblue"
    set datafile separator ","
    set timefmt "%Y-%m-%d"
    set format x "%d %B"
    set xdata time
    set xrange ["$MONTH_AGO_DATE":"$YESTERDAY_DATE"]
    set yrange [0:100]
    set xlabel "tijd"
    set ylabel "%"
    set ytics 10
    set mytics 2
    set mxtics 7
    set grid ytics lt 0 lw 1 lc rgb "#bbbbbb"
    set grid mytics
    set grid xtics lt 0 lw 1 lc rgb "#bbbbbb"
    set grid mxtics
    set pointsize 1
    set terminal png size 1024,768
    set output "$PUBLISH_DIR/minmax_humidity_large_greenhouse.png"
    plot "$APRS_RECEIVED_MESSAGES_DIR$APRS_MAX_DATA" using 1:5 with lines linewidth 2 title "maxima", "$APRS_RECEIVED_MESSAGES_DIR$APRS_MIN_DATA" using 1:5 with lines linewidth 2 title "minima"
EOFMarker
# rest of script, after gnuplot exits

# Remove temporary file
rm "$APRS_RECEIVED_MESSAGES_DIR$APRS_TELEMETRY_FILE"

