<?php
include '/var/www/rennergy/conf/global_config.php';     // globale Konfiguration
include '/var/www/rennergy/conf/con_conf.php';      // SQL Abfrage

//Prüfung der Funktion HK1
//$hk1i = $hk1i - 5;
//$pf1 = "0";

//Prüfung der Funktion Kesseltemperatur
//$ktemp = "90";
//$rgass = "0";

//Prüfung der Frischwassertemperatur
//$fwt = "39";

//Sollwert für Frischwassertemperatur (nicht auskommentieren!)
$fwts = "40";

$i = "0";
$check = array(4);
$txt = array(4);
$filepath = "/var/www/rennergy/files/";

$file = fopen($filepath."check.chk", "r") or die("Unable to open file for reading!");
while (! feof($file)){
    $check[$i] = fgetc($file);
    $i = $i + 1;
    }
fclose($file);

if ( !isset($check[0]) ) { $check[0] = "0"; };
if ( !isset($check[1]) ) { $check[1] = "0"; };
if ( !isset($check[2]) ) { $check[2] = "0"; };

//print_r($check);

if ( $check[0] == "0" or $check[0] == "1") {
    if ( $check[0] == "0" ) {
        if ( $hk1i < $hk1s and $pf1 < $hk1s ) {
            $message = "HK1 Soll nicht erreicht! <b><font color='#00CC00'>$hk1s"."°C</font> / <font color='red'>$hk1i"."°C</font></b>";
            $txt[0] = "1";
            echo 'Sende Nachricht: "HK1 Soll nicht erreicht!"'."\n";
            curl_setopt_array($ch = curl_init(), array(CURLOPT_URL => "https://api.pushover.net/1/messages.json",CURLOPT_POSTFIELDS => array("token" => "$token","user" => "$user","message" => "$message","sound" => "siren","priority" => "1","html" => "1"),CURLOPT_SAFE_UPLOAD => true,));
            curl_exec($ch);
            curl_close($ch);
        }
        else {
            $txt[0] = "0";
            echo "Negativprüfung HK nicht abgeschlossen. \n";
        };
    }
    elseif ( $check[0] == "1" ) {
        if ( $hk1i > $hk1s or $pf1 > $hk1s ) {
            $message = "<font color='#00CC00'>HK1 Soll kann wieder erreicht werden!</font>";
            $txt[0] = "0";
            echo 'Sende Nachricht: "HK1 Soll kann wieder erreicht werden!"'."\n";
            curl_setopt_array($ch = curl_init(), array(CURLOPT_URL => "https://api.pushover.net/1/messages.json",CURLOPT_POSTFIELDS => array("token" => "$token","user" => "$user","message" => "$message","sound" => "magic","html" => "1"),CURLOPT_SAFE_UPLOAD => true,));
            curl_exec($ch);
            curl_close($ch);
        }
        else {
                $txt[0] = "1";
                echo "Positivprüfung HK nicht abgeschlossen. \n";
        };
    };
};

if( $check[1] == "0" or $check[1] == "1") {
    if ( $check[1] == "0" ) {
        if ( $ktemp > "87" and $rgass == "0" ) {
                $message = "Übertemperatur!!! Kessel: <b><font color='red'>$ktemp"."°C</b></font>";
                $txt[1] = "1";
                echo 'Sende Nachricht: "Übertemperatur!!!"'."\n";
                curl_setopt_array($ch = curl_init(), array(CURLOPT_URL => "https://api.pushover.net/1/messages.json",CURLOPT_POSTFIELDS => array("token" => "$token","user" => "$user","message" => "$message","sound" => "siren", "priority" => "1","html" => "1"),CURLOPT_SAFE_UPLOAD => true,));
                curl_exec($ch);
                curl_close($ch);
        }
        else {
            $txt[1] = "0";
                        echo "Negativprüfung KT nicht abgeschlossen. \n";
        };
    }
    elseif ( $check[1] == "1" ) {
        if ( $ktemp < "87" ) {
            $message = "<font color='#00CC00'>Kesseltemperatur normal.</font>";
            $txt[1] = "0";
            echo 'Sende Nachricht: "Kesseltemperatur normal."'."\n";
            curl_setopt_array($ch = curl_init(), array(CURLOPT_URL => "https://api.pushover.net/1/messages.json",CURLOPT_POSTFIELDS => array("token" => "$token","user" => "$user","message" => "$message","sound" => "magic","html" => "1",),CURLOPT_SAFE_UPLOAD => true,));
            curl_exec($ch);
            curl_close($ch);
        }
        else {
            $txt[1] = "1";
            echo "Positivprüfung KT nicht abgeschlossen. \n";
        };
    };
};

if( $check[2] == "0" or $check[2] == "1") {
    if ( $check[2] == "0" ) {
        if ( $fwts > $fwt ) {
                $message = "Frischwasssertemperatur nicht erreicht: <b><font color='#00CC00'>$fwts"."°C</font> / <font color='red'>$fwt"."°C</font></b>";
                $txt[2] = "1";
                echo 'Sende Nachricht: "Friwa nicht erreicht"'."\n";
                curl_setopt_array($ch = curl_init(), array(CURLOPT_URL => "https://api.pushover.net/1/messages.json",CURLOPT_POSTFIELDS => array("token" => "$token","user" => "$user","message" => "$message","sound" => "siren", "priority" => "1","html" => "1"),CURLOPT_SAFE_UPLOAD => true,));
                curl_exec($ch);
                curl_close($ch);
        }
        else {
            $txt[2] = "0";
                        echo "Negativprüfung Friwa nicht abgeschlossen. \n";
        };
    }
    elseif ( $check[2] == "1" ) {
        if ( $fwts < $fwt ) {
            $message = "<font color='#00CC00'>Frischwasssertemperatur kann wieder erreicht werden!</font>";
            $txt[2] = "0";
            echo 'Sende Nachricht: "Frischwasssertemperatur normal."'."\n";
            curl_setopt_array($ch = curl_init(), array(CURLOPT_URL => "https://api.pushover.net/1/messages.json",CURLOPT_POSTFIELDS => array("token" => "$token","user" => "$user","message" => "$message","sound" => "magic","html" => "1",),CURLOPT_SAFE_UPLOAD => true,));
            curl_exec($ch);
            curl_close($ch);
        }
        else {
            $txt[2] = "1";
            echo "Positivprüfung Friwa nicht abgeschlossen. \n";
        };
    };
};


$file = fopen($filepath."check.chk", "w") or die ("Unable to open file for writing!");

//print_r($txt);

$i = "0";

while ( $i != "3" ) {
    if ( is_null($txt[$i]) ) {
        if ( $check[$i] == "0" ) { $txt[$i] = "0"; }
        elseif ( $check[$i] == "1" ) { $txt[$i] = "1"; };
        };
    $i = $i + 1;
    }

//print_r($txt);

$i = "0";
while ( $i != "3") {
    fwrite($file, $txt[$i]);
    $i = $i + 1;
    }
fclose($file);

$file = fopen($filepath."lcd.vw", "w") or die ("Unable to open file for writing!");
fwrite($file, $lcd);
fclose($file);

?>
