#!/bin/bash

# WeeWX  Ventilador Utility by Miguel H 
__version__="1.0.0"


#### *** MUST CUSTOMIZE ***
#
ventilador_system=1         # 1 means the system is working. 0 if is not to be used. Before stopping it, make sure the fan is off.
debug=0                     # 1 means that debug is ongoing. Program echoes certain values
sendemail=0                 # 1 means that email must  be sent
# e-mail address to notify
email="nobody@example.com"    #must be set to valid email if sendemail was set to 1

####################################################
#### *** Recommend that you customize ***
#
# command to send the order  for fan  activation. Must be adjusted to your script name and path location
ventilador_on="python /usr/local/etc/weewx/Ventilador_Encender"

doventiladoroff=1        # 1 means the fan must be swuitched off at the end of the interval. Not necessary if the switch is configured to automatic switch_off

# command to send the order  for fan  de_activation. Must be adjusted to your script name and path location
# NOTE: not needed if doventiladoroff is set to 0.
ventilador_off="python /usr/local/etc/weewx/Ventilador_Apagar"

#### *** Optional customizations
# Number of minutes the fan must be working a maximum in an interval
# Note: this time must be set even if the switch is setting the fan off automatically. Used by the program to calculate times
ventiladoronmin=9 # 10 minutes

# Time for fan off after an on interval. Should be a multiple of the execution time (5 or 10 minutes deppending of the trigerring o the script)
waitingintervalmin=19       # 20 minutes efective

max_number_intervals=5      # Maximum number of intervals in a day

# Control targeted to heating  and de-humidification (winter) 
starthour_heating=13        # hour (UTC) to start ventilation for heating if conditions are met. Before this hour is not optimal, mainly in winter.
maxtemp_heating=18          # ventillation for heating will not continue when this internal temperature is reached
mintemp_heating_diff=3      # temperature difference required to start heating
min_humidity_diff=5         # fan if not started if the external humidity is not lower than internal at least in this value



# Control targeted to cooling (summer) 
starthour_cooling=4         # hour (UTC)to start ventilation for cooling if conditions are met. Do not forguet summer time (effective UTC+2) 
                            # Before this hour is not optimal, mainly in summer.
mintemp_cooling=25          # ventillation for cooling will not continue when the internal temperature is below this point
mintemp_cooling_diff=3      # temperature difference required to start cooling
max_humidity_ext=65         # ventillation is not started if the humidity is above this value

# Main weewx log file
weewx_main_log=/var/log/weewx.log

# Supplemental log. Used to store the data needed to control the status and intervals

weewx_suppl_log=/var/log/weewx_ventilador.log

# File with the data generated by weewx reports following the specific skin (similar to the one for meteoclimatic.htm)
# Must be adjusted to the weewx installation paths. Typically /var/www/html/weewx/

weewx_ventilador_file=/var/www/html/weewx/ventilador.htm

#
### *** End of user customizations
##################################

ventiladoronsecs=$((ventiladoronmin * 60))         # converting minutes to seconds
waitingintervalsec=$((waitingintervalmin * 60))

### check if the program is off
if [ "$ventilador_system" -eq 0 ] ; then         # progran is off. Used to stop the whole progran
	exit
fi

### Check to see that email address was in fact customized if it must be used.  ;
if [ "$sendemail" -eq 1 ] ; then
    if [ "$email" = "nobody@example.com" ] ; then
	    echo "ERROR:  WeeWX Watchdog -- email still set to fake address. Edit the setting in the script."
	    exit 200
    fi

### Check to see that mailx utilty is found
    mailx_check=$(which mailx 2>/dev/null)
    if [ -z "$mailx_check" ] ; then
	    echo  "ERROR:  WeeWX Watchdog -- mailx utility not found; please install it (see readme.txt)."
	    exit 255
    fi
fi
#############################
# Fan Status Detection

# set -xv # command to debug and trace execution

# Get the current timestamp since the epoc and the day

currstamp="$(date +%s)"
currdaystamp="$(date +%y%m%d)"
currhourstamp="$(date +%H)"

# Normalize the log messages the current time for this time through the checks
msgstamp="$(date +%Y/%m/%d' '%H:%M:%S)"

# Get the data and the timestamp of the last time ventilador logged a record
start_logstamp="$(sed -n 's/.*WeeWX: Start VENTILADOR event timestamp: (\([0-9]*\)) .*/\1/p' ${weewx_suppl_log}.1 ${weewx_suppl_log} 2>/dev/null | tail -1)"
stop_logstamp="$(sed -n 's/.*WeeWX: Stop VENTILADOR event timestamp: (\([0-9]*\)) .*/\1/p' ${weewx_suppl_log}.1 ${weewx_suppl_log} 2>/dev/null | tail -1)"
day_logstamp="$(sed -n 's/.*Day timestamp: (\([0-9]*\)) .*/\1/p' ${weewx_suppl_log}.1 ${weewx_suppl_log} 2>/dev/null | tail -1)"
interv_logstamp="$(sed -n 's/.*Interval: (\([0-9]*\)) .*/\1/p' ${weewx_suppl_log}.1 ${weewx_suppl_log} 2>/dev/null | tail -1)"

if [ -z $interv_logstamp ] ; then  # no data for start in files
   interv_logstamp=0
   start_logstamp=0
   day_logstamp="200101"           #fake date when no data in files
fi
  
if [ -z $stop_logstamp ] ; then    # no data for stop in files
   stop_logstamp=0                 # set fake value
fi


#  printinf values for testing purposes
if [ $debug -eq 1 ] ; then                         # if debug is ongoing
    echo "start.............$start_logstamp" 
    echo "stop..............$stop_logstamp"
    echo "day_logstamp..... $day_logstamp"
    echo "Interval logstamp.. $interv_logstamp"
fi


# Verify that data in log are from today  otherwise set them to default values
if [ $day_logstamp -lt $currdaystamp ] ; then
   start_logstamp=0
   stop_logstamp=0
   interv_logstamp=0
   # assumption: ventilador is not running at the end of the day
fi


# ventilador is in ON or in guard period
if [ $start_logstamp -gt $stop_logstamp ] ; then                          # ventilador is on
	if [ $((start_logstamp + ventiladoronsecs)) -lt $currstamp ] ; then     # end of on period
		# log stop in all cases, even if the fan sets off authomatically
                echo "$msgstamp WeeWX: Stop VENTILADOR event timestamp: ($currstamp) " >>$weewx_suppl_log   

		if [ $doventiladoroff -eq 1 ] ; then
			# stop ventilador
		        stopmsg=`$ventilador_off 2>&1`
		fi
	fi

	exit                                                                    # No more actins needed if it was on
else
	if [ $((stop_logstamp + waitingintervalsec)) -gt $currstamp ] ; then    # guard period ongoing
                exit
	fi
fi

# check if maximum number of intervals already passed
if [ $interv_logstamp -ge $max_number_intervals ] ; then
	exit                     # no more checks
fi

# Read the data for temperature and humidity from file

sed 's/\,.*$//g' ${weewx_ventilador_file} 1>${weewx_ventilador_file}.1   #removing comma and tailing digit(s)

Temperature_external="$(sed -n 's/.*ETMP=*\([0-9]*\)/\1/p' ${weewx_ventilador_file}.1 2>/dev/null )"
Temperature_internal="$(sed -n 's/.*ITMP=*\([0-9]*\)/\1/p' ${weewx_ventilador_file}.1 2>/dev/null )"
Humidity_external="$(sed -n 's/.*EHUM=*\([0-9]*\)/\1/p' ${weewx_ventilador_file}.1 2>/dev/null )"
Humidity_internal="$(sed -n 's/.*IHUM=*\([0-9]*\)/\1/p' ${weewx_ventilador_file}.1 2>/dev/null )"
date_infile="$(sed -n 's/.*UPD=*\([0-9]*\)/\1/p' ${weewx_ventilador_file}.1 2>/dev/null )"

Date_infile=${date_infile:3:2}"/"${date_infile:0:2}"/"${date_infile:6:4}  #converting the format of the date

Date_Infile=$(date --date="$Date_infile" +%s)                             # converting the date in file to epoch
Date_Stamp=$(date --date=$"(date +%d/%m/%Y)" +%s)                         # converting to today date to epoch

Diff_temp=$(($Temperature_external - $Temperature_internal))
Diff_hum=$(($Humidity_internal - $Humidity_external))
info_text="T.ext.$Temperature_external  T.int.$Temperature_internal  H.ext.$Humidity_external  H.int.$Humidity_internal.."
# set -xv        #start debugging
# set +xv        #stop debugging

# printing values for testing purposes
if [ $debug -eq 1 ] ; then                         # if debug is ongoing
     echo "$info_text"
     echo "Temperatura externa... $Temperature_external"
     echo "Temperatura interna... $Temperature_internal"
     echo "Humedad externa........$Humidity_external"
     echo "Humedad interna....... $Humidity_internal"
     echo "Date in file...........$Date_infile... $Date_Infile"
fi

# check conditions for temperature versus time

# check that temperature data are from today
if [ $Date_Infile -ne $Date_Stamp ] ; then
	exit
fi

# check heating
if [ $Temperature_internal -lt $maxtemp_heating ] ; then                        # heating conditions (winter or cold)
   if [ $currhourstamp -ge $starthour_heating ] ; then                          # correct hour
	if [ $((Temperature_internal + mintemp_heating_diff))  -le $Temperature_external ] ; then   # temperature conditions met
	   if  [ $((Humidity_internal - Humidity_external)) -ge $min_humidity_diff ] ; then         # humidity conditions met
	         # start order
	        interv_logstamp=$((interv_logstamp+1))                          # incrementing interval value
                startmsg=`$ventilador_on 2>&1`                                  # execute script to set the fan on
                logtext="$msgstamp WeeWX: Start VENTILADOR event timestamp: ($currstamp) Day timestamp: ($currdaystamp) Interval: ($interv_logstamp) $info_text "
                echo "$logtext"  >>$weewx_suppl_log                             # log the start in the  ventilador log
        	 if [ $interv_logstamp -eq 1 ] ; then
	              # Log the message to the main Weewx log the first time the fan is started in a day
                      echo "$msgstamp WeeWX: Start VENTILADOR first time today. Timestamp: ($currstamp)" >>$weewx_main_log
		      if [ $sendemail -eq 1 ] ; then
		            # send email for first start in the day
		            echo "$logtext" | mailx -s "Weewx VENTILADOR encendido $msgstamp" $email
	       	      fi
		 fi
           fi
	fi
   fi
  exit            #no mor actions needed
fi


if [ $Temperature_internal -ge $mintemp_cooling ] ; then                                            # heating conditions (summer)
    if [ $currhourstamp -ge $starthour_cooling ] ; then                                             # correct hour
	if [ $((Temperature_internal - mintemp_cooling_diff))  -ge $Temperature_external ] ; then   # temperature conditions met
	   if  [ $Humidity_external -lt $max_humidity_ext ] ; then                                  # humidity conditions met
	   	#orden de encendido
        	interv_logstamp=$((interv_logstamp+1)) 
                startmsg=`$ventilador_on 2>&1` #order to start fan
	        logtext="$msgstamp WeeWX: Start VENTILADOR event timestamp: ($currstamp) Day timestamp: ($currdaystamp) Interval: ($interv_logstamp) $info_text "
                echo "$logtext" >> $weewx_suppl_log           #info added to ventilador log
	   	if [ $interv_logstamp -eq 1 ] ; then
		     # Log the message to the main Weewx log the first time in a day the fan is started
                     echo "$msgstamp WeeWX: Start VENTILADOR first time today. Timestamp: ($currstamp)" >>$weewx_main_log
		     if [ $sendemail -eq 1 ] ; then
	     	          #send email for first start in the day
			  echo "$logtext" | mailx -s "Weewx VENTILADOR encendido $msgstamp" $email
		     fi
         	fi
	   fi
	fi
    fi
fi


#  set +xv

exit
