#!/usr/bin/perl

my $temp_warning = 5;                         # Indoor1 temp warning threshhold
my $temp_time_warning = 60*60*2;              # 2h
my $ack1_high = 70;                           # Turn off heater and solar panel

my $temp_control_table = "nattsjo_control";
my $elpatron_program = "/root/bin/elpatron.pl";
my $tdtool_program = "/usr/bin/tdtool";

my $return_path_address = "josefk\@rotary.nu";
my $address = "daniel\@nattsjo.se";

my $DBHOST = "localhost";
my $DBNAME = "temperature";


my $indoor_sql  = "select id, ROUND(temp/10,1), time as temp_indoor1, unix_timestamp(now())-unix_timestamp(time) as indoor1_secs from nattsjo_indoor1 order by -time limit 1";

my $outdoor_sql = "select id, ROUND(temp/10,1), time as temp_outdoor1, unix_timestamp(now())-unix_timestamp(time) as outdoor1_secs from nattsjo_outdoor1 order by -time limit 1";

my $triggers_sql = "select (SELECT ROUND(temp/10,0) AS temp FROM nattsjo_control WHERE type = 'indoor1_high') as indoor1_high, (SELECT ROUND(temp/10,0) AS temp FROM nattsjo_control WHERE type = 'indoor1_low') as indoor1_low";

my $ack1_sql = "select ROUND(temp/10,1) from nattsjo_ack1 order by -time limit 1";

my $solarin_sql = "select ROUND(temp/10,1) from nattsjo_solarin order by -time limit 1";

use DBI;

#
# Retrieve data from DB
my $db = DBI->connect("DBI:mysql:$DBNAME:$DBHOST", $DBUSER, $DBPASS);
$DBI::result = $db->prepare($indoor_sql);
$DBI::result->execute();
my ($indoor1_id,  $indoor1_temp,  $indoor1_time,  $indoor1_secs)  = $DBI::result->fetchrow_array;

$DBI::result = $db->prepare($outdoor_sql);
$DBI::result->execute();
my ($outdoor1_id,  $outdoor1_temp,  $outdoor1_time,  $outdoor1_secs)  = $DBI::result->fetchrow_array;

$DBI::result = $db->prepare($triggers_sql);
$DBI::result->execute();
my ($indoor1_high, $indoor1_low)  = $DBI::result->fetchrow_array;

$DBI::result = $db->prepare($ack1_sql);
$DBI::result->execute();
my ($ack1_temp)  = $DBI::result->fetchrow_array;

$DBI::result = $db->prepare($solarin_sql);
$DBI::result->execute();
my ($solarin_temp)  = $DBI::result->fetchrow_array;



#
# Control logic


# -- Turn off --
if ($indoor1_temp >= $indoor1_high) {
    print `$elpatron_program off`;
}

# -- Turn on --
if ($indoor1_temp <= $indoor1_low) {
    print `$elpatron_program on x`;
}

# -- El heater running, turn off solar panel --
my $state =  `$elpatron_program`;
if ($state =~ ': on') {
    `$tdtool_program --off 5`;
}

# -- Boiler running, turn off el heater
if ($ack1_temp >= $ack1_high) {
    `$elpatron_program off`;
    `$tdtool_program --off 5`;
}

# -- Boiler running, turn off solar panel
if ($ack1_temp >= $solarin_temp) {
#    `$tdtool_program --off 5`;
}



#if ($state =~ ': off') {
#    print "State off\n";
#}
#print "\n\n in_low: $indoor1_low in_hi: $indoor1_high in: $indoor1_temp \n";
#print "out_secs: $outdoor1_secs in_secs: $indoor1_secs\n\n";


#
# Warning emails

if ($indoor1_secs > $temp_time_warning) {
    open(MAIL, "| /usr/sbin/sendmail -f $return_path_address -oi $address") || die "Pipe failed: $!\n";
    select MAIL;
    print "To: $address\n";
    print "From: $return_path_adress\n";
    print "Subject: Nattsjo 1:15 temperature warning\n\n";
    print "WARNING: indoor1 temperature not updating!\n\n";
    print "Last update: $indoor1_temp C, $indoor1_time\n\n";
    close MAIL;
}

if ($outdoor1_secs > $temp_time_warning) {
    open(MAIL, "| /usr/sbin/sendmail -f $return_path_address -oi $address") || die "Pipe failed: $!\n";
    select MAIL;
    print "To: $address\n";
    print "From: $return_path_adress\n";
    print "Subject: Nattsjo 1:15 temperature warning\n\n";
    print "WARNING: outdoor1 temperature not updating!\n\n";
    print "Last update: $outdoor1_temp C, $outdoor1_time\n\n";
    close MAIL;
}

if ($indoor1_temp <= $temp_warning) {
    open(MAIL, "| /usr/sbin/sendmail -f $return_path_address -oi $address") || die "Pipe failed: $!\n";
    select MAIL;
    print "To: $address\n";
    print "From: $return_path_adress\n";
    print "Subject: Nattsjo 1:15 temperature warning\n\n";
    print "WARNING: indoor1 temperature critical!\n\n";
    print "Last update: $indoor1_temp C, $indoor1_time\n\n";
    close MAIL;
}