<?php
/*
********************************************************************************

    filename:     currentburnsimulationprocess.php
    description:  process current burn simulation
    inputs:       probes 1-8 new temps, steps
    outputs:      none
    actions:      none
    redirections: currentburnsimulation.php

********************************************************************************
*/
            include 'functions.php';
            $conn = connectToDB();
            if ($_POST["cancel"]) {
                header('Location: currentburnsimulation.php');
                exit();
            } elseif ($_POST["simulate"]) {
                $probeNewTemps = array(0, 0, 0, 0, 0, 0, 0, 0, 0);
                $probeStepTemps = array(0, 0, 0, 0, 0, 0, 0, 0, 0);
                $probeNewTemps[1]= $_POST["probe1NewTemp"];
                $probeStepTemps[1] = $_POST["probe1StepTemp"];
                $probeNewTemps[2]= $_POST["probe2NewTemp"];
                $probeStepTemps[2] = $_POST["probe2StepTemp"];
                $probeNewTemps[3]= $_POST["probe3NewTemp"];
                $probeStepTemps[3] = $_POST["probe3StepTemp"];
                $probeNewTemps[4]= $_POST["probe4NewTemp"];
                $probeStepTemps[4] = $_POST["probe4StepTemp"];
                $probeNewTemps[5]= $_POST["probe5NewTemp"];
                $probeStepTemps[5] = $_POST["probe5StepTemp"];
                $probeNewTemps[6]= $_POST["probe6NewTemp"];
                $probeStepTemps[6] = $_POST["probe6StepTemp"];
                $probeNewTemps[7]= $_POST["probe7NewTemp"];
                $probeStepTemps[7] = $_POST["probe7StepTemp"];
                $probeNewTemps[8]= $_POST["probe8NewTemp"];
                $probeStepTemps[8] = $_POST["probe8StepTemp"];
/*
                ... get current burn
*/
                $sql = "SELECT burnId, burnState, numberProbes FROM currentburn WHERE currentBurnId=1";
                $result = mysqli_query($conn, $sql);            
                $row = mysqli_fetch_assoc($result);
                if (mysqli_num_rows($result) == 0) {
                    exit("no current burn<br>");
                }
                $burnId= $row[burnId];
                $burnState = $row[burnState];
                $numberProbes= $row[numberProbes];

/*
                ... get current probes
*/
                $probeIds = array(0, 0, 0, 0, 0, 0, 0, 0, 0);
                $probeCurrentTemps = array(0, 0, 0, 0, 0, 0, 0, 0, 0);
                $targetTemps = array(0, 0, 0, 0, 0, 0, 0, 0, 0);
                $highTemps = array(0, 0, 0, 0, 0, 0, 0, 0, 0);
                $lowTemps = array(0, 0, 0, 0, 0, 0, 0, 0, 0);
                $sql = "SELECT probePosition, probeId, probeTemp, targetTemp, highTemp, lowTemp FROM currentprobes";
                $results = mysqli_query($conn, $sql);
                if (mysqli_num_rows($results) > 0) {
                    while($row = mysqli_fetch_assoc($results)) {
                        $probePosition = $row["probePosition"];
                        $probeIds[$probePosition] = $row["probeId"];
                        $probeCurrentTemps[$probePosition]= $row["probeTemp"];
                        $targetTemps[$probePosition]= $row["targetTemp"];
                        $highTemps[$probePosition]= $row["highTemp"];
                        $lowTemps[$probePosition]= $row["lowTemp"];
                    }
                } else {            
                   echo "no probes assigned<br>";
                }
/*
                ... set step to negative if new temp less than current temp
*/
                for ($i = 1; $i <= 8; $i++) {
                    if ($probeNewTemps[$i] < $probeCurrentTemps[$i]) {
                        $probeStepTemps[$i] = -$probeStepTemps[$i];
                    }  
                }
/*
                ... initialize start
*/
                $unixTime = time();
                $statusDateTime = date("Y-m-d H:i:s", $unixTime);
                $probeTemps = array(0, 0, 0, 0, 0, 0, 0, 0, 0);
                for ($i = 1; $i <= 8; $i++) {
                    $probeTemps[$i] = $probeCurrentTemps[$i];
                }
/*
                ... determine max number of steps
*/
                $steps = array(0, 0, 0, 0, 0, 0, 0, 0, 0);
                for ($i = 1; $i <= 8; $i++) {
                    if ($probeStepTemps[$i] != 0) {
                        $steps[$i] = abs(($probeNewTemps[$i] - $probeCurrentTemps[$i]) / $probeStepTemps[$i]) + 1;
                    } else {
                       $steps[$i] = 0;
                   }
               }
               $maxSteps = 1;
               for ($i = 1; $i <= 8; $i++) {
                   if ($steps[$i] > $maxSteps) {
                       $maxSteps = $steps[$i];
                   }
               }
/*
               ... generate statuses
*/
               for ($x =1; $x <= $maxSteps; $x++) {      
                    for ($p = 1; $p <= $numberProbes; $p++) {
                        if ($p == 1) {
                            $blowerSpeed = '100';
                            $ventPosition = '100';
                        } else {
                            $blowerSpeed = 'null';
                            $ventPosition = 'null';
                        }
                        $sql = "INSERT INTO statuses (burnId, burnState, probeId, probePosition, statusDateTime, probeTemp, targetTemp, highTemp, lowTemp, blowerSpeed, ventPosition) VALUES ($burnId, '$burnState', $probeIds[$p], $p, '$statusDateTime', $probeTemps[$p], $targetTemps[$p], $highTemps[$p], $lowTemps[$p], $blowerSpeed, $ventPosition)";
                        if (!mysqli_query($conn, $sql)) {
                            exit("Error: " . $sql . " " . mysqli_error($conn) . "<br>");
                        }
                        $sql = "UPDATE currentprobes SET probeTemp=$probeTemps[$p] WHERE probePosition=$p";
                        if (!mysqli_query($conn, $sql)) {
                             exit("Error: " . $sql . " " . mysqli_error($conn) . "<br>");
                        }
                    }
                    for ($y = 1; $y<=8; $y++) {
                         $probeTemps[$y] += $probeStepTemps[$y];
                        if ($probeStepTemps[$y] > 0 && $probeTemps[$y] > $probeNewTemps[$y]) {
                             $probeTemps[$y] = $probeNewTemps[$y];
                        } elseif ($probeStepTemps[$y] < 0 and $probeTemps[$y] < $probeNewTemps[$y]) {
                            $probeTemps[$y] = $probeNewTemps[$y];
                        }
                    }
                    $unixTime+=1;
                    $statusDateTime = date('Y-m-d H:i:s', $unixTime);
                }
            }
            mysqli_close($conn);
            header('Location: burnchart.php?burnId=' . $burnId);
?>
