<?
/*
 * overview.php:
 * status display with positioned layers
 * time resolution: minute
 * parameters given by _GET:
 * - minute to display
 *
 * (c) 2008 alexander.philipp.lintenhofer
 */

    require_once("_includes/db_handle.php");         // wrapper-functions for mysql (not included)
    require_once("_includes/globals.inc.php");

    $arrItems = array(
        array( 'name' => 'Kompressor', 'mask_do_buffer' => 0x02, 'mask_di_buffer' => 0x01, 'icon_on' => 'ico_kompr_on.png', 'icon_fail' => 'ico_kompr_fail.png', 'right' => '347', 'top' => '355'),
        array( 'name' => 'Pumpe Wärmequelle', 'mask_do_buffer' => 0x80, 'mask_di_buffer' => 0x08, 'icon_on' => 'ico_pumpewq_on.png', 'icon_fail' => 'ico_pumpewq_fail.png', 'right' => '740', 'top' => '278'),
        array( 'name' => 'Pumpe Heizung', 'mask_do_buffer' => 0x20, 'mask_di_buffer' => 0x00, 'icon_on' => 'ico_pumpehzg_on.png', 'icon_fail' => 'ico_pumpehzg_fail.png', 'right' => '240', 'top' => '433'),
        array( 'name' => 'Kältemittel Magnetventil', 'mask_do_buffer' => 0x01, 'mask_di_buffer' => 0x00, 'icon_on' => 'ico_kmmv_on.png', 'icon_fail' => 'ico_kmmv_fail.png', 'right' => '479', 'top' => '427'),
        array( 'name' => 'Pumpe/Kugelventil WWasser', 'mask_do_buffer' => 0x40, 'mask_di_buffer' => 0x00, 'icon_on' => 'ico_wwv_on.png', 'icon_fail' => 'ico_wwv_fail.png', 'right' => '100', 'top' => '139'),
        array( 'name' => 'Stufe 2 (Elektrobetrieb)', 'mask_do_buffer' => 0x10, 'mask_di_buffer' => 0x00, 'icon_on' => 'led_green.png', 'icon_fail' => 'led_red.png', 'right' => '360', 'top' => '166')
    );

    $sql = "SELECT m_0000 AS version,
                   m_0001 AS datum_version,
                   m_0002 AS betriebsmodus,
                   m_0100 AS temp_aussen,
                   m_0101 AS temp_aussen24,
                   m_0102 AS temp_aussen1,
                   m_0103 AS temp_RlSoll,
                   m_0104 AS temp_Rl,
                   m_0105 AS temp_Vl,
                   m_0106 AS temp_WWSoll,
                   m_0107 AS temp_WW,
                   m_0108 AS temp_raum,
                   m_0109 AS temp_raum1,
                   m_0110 AS temp_WQein,
                   m_0111 AS temp_WQaus,
                   m_0112 AS temp_Verdampfer,
                   m_0113 AS temp_Kondensator,
                   m_0114 AS temp_Saugleitung,
                   m_0116 AS druck_Verdampfer,
                   m_0117 AS druck_Kondensator,
                   m_0500 AS uhrzeit,
                   m_0501 AS datum,
                   m_0502 AS betriebsstunden,
                   m_0901 AS DO_buffer,
                   m_0902 AS DI_buffer FROM ".$db_getTable;

    // sometimes tsp is newer than db_entry which causes an empty result set
    // so give it a chance otherwise take the newest entry
    for ($i=0,$tsp = intval($_GET['start']);((!$arrQ) && $i<2);$i++)
    {
        if (($tsp>0)&&($i<1)) $sql_trailer = " WHERE tsp_0501_0500 > ".$tsp." ORDER BY id ASC LIMIT 1";
        else $sql_trailer = " ORDER BY id DESC LIMIT 1";

        $arrQ = wrapper_single_query($sql.$sql_trailer);
    }

    $currVl = $arrQ['temp_Vl'];
    $currKW  = 0;

    if (bindec($arrQ['DO_buffer']) & $arrItems[0]['mask_do_buffer'])
    {
        if      ($currVl < 40)               { $currKW = (0.06*$currVl)+0.5; }
        else if (($currVl>40)&&($currVl<50)) { $currKW = (0.08*$currVl)-0.3; }
        else                                 { $currKW = (0.1*$currVl)-1.3; }
    }

    $deltaVlRl = abs($currVl - $arrQ['temp_Rl']);   
    $deltaWQea = abs($arrQ['temp_WQein']-$arrQ['temp_WQaus']);
?>
<html>
<head>
    <title>W&auml;rmepumpen-Monitor &Uuml;bersicht</title>
    <meta name="author" content="Alexander Philipp Lintenhofer">
    <meta name="robots" content="noindex">
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-2">
<? if ($tsp < 1) { ?>
    <meta http-equiv="refresh" content="60">
<? } ?>
    <link rel="stylesheet" href="css/styles.css" type="text/css">
</head>
<style type="text/css">
    div#diagramm { float: left; position:relative; padding:0 0 0 0; margin:0 0 0 35;}
</style>
<body>
    <div id="diagramm">
    <!-- ================================ Umgebungstemperaturen ================================================= -->
        <img src="img/diagramm.png" width="818" height="480" border="0">
        <div style='position:absolute; right:693px; top:25px'><?= $arrQ['temp_aussen'] ?> °C</div>
        <div style='position:absolute; right:693px; top:45px'><?= $arrQ['temp_aussen1'] ?> °C</div>
        <div style='position:absolute; right:693px; top:65px'><?= $arrQ['temp_aussen24'] ?> °C</div>
        <div style='position:absolute; right:535px; top:35px'><?= $arrQ['temp_raum'] ?> °C</div>
        <div style='position:absolute; right:535px; top:55px'><?= $arrQ['temp_raum1'] ?> °C</div>
    <!-- ======================================== WQ ein/aus ===================================================== -->
        <div style='position:absolute; right:679px; top:394px; color:red;'><?= $arrQ['temp_WQein'] ?> °C</div>
        <div style='position:absolute; right:679px; top:421px; color:blue;'><?= $arrQ['temp_WQaus'] ?> °C</div>
        <div style='position:absolute; right:743px; top:407px;'><?= $deltaWQea ?> °C</div>
    <!-- ======================================== Kondensator ==================================================== -->
        <div style='position:absolute; right:271px; top:311px; color:blue;'><?= $arrQ['temp_Kondensator'] ?> °C</div>
        <div style='position:absolute; right:271px; top:331px; color:blue;'><?= $arrQ['druck_Kondensator'] ?> bar</div>
    <!-- ========================================= Verdampfer ==================================================== -->
        <div style='position:absolute; right:521px; top:311px; color:red;'><?= $arrQ['temp_Verdampfer'] ?> °C</div>
        <div style='position:absolute; right:521px; top:331px; color:red;'><?= $arrQ['druck_Verdampfer'] ?> bar</div>
    <!-- ===================================== Vorlauf, Ruecklauf ================================================ -->
        <div style='position:absolute; right:145px; top:261px; color:red;'><?= $currVl ?> °C</div>
        <div style='position:absolute; right:145px; top:286px;'><?= $deltaVlRl ?> °C</div>
        <div style='position:absolute; right:145px; top:311px;'><?= $arrQ['temp_RlSoll'] ?> °C</div>
        <div style='position:absolute; right:145px; top:331px; color:blue;'><?= $arrQ['temp_Rl'] ?> °C</div>
    <!-- ========================================= Warmwasser ==================================================== -->
        <div style='position:absolute; right:24px; top:341px;'><?= $arrQ['temp_WWSoll'] ?> °C</div>
        <div style='position:absolute; right:24px; top:361px;'><?= $arrQ['temp_WW'] ?> °C</div>
    <!-- ========================================== allgemein ==================================================== -->
        <div style='position:absolute; right:5px; top:4px;'><?= substr($arrQ['datum'],8,2).'.'.substr($arrQ['datum'],5,2).'.'.substr($arrQ['datum'],2,2) ?></div>
        <div style='position:absolute; right:5px; top:24px;'><?= $arrQ['uhrzeit'] ?></div>

        <div style='position:absolute; right:5px; top:44px;'><?= $arrQ['version'] ?></div>
        <div style='position:absolute; right:5px; top:64px;'><?= substr($arrQ['datum_version'],8,2).'.'.substr($arrQ['datum_version'],5,2).'.'.substr($arrQ['datum_version'],2,2) ?></div>
        <div style='position:absolute; right:5px; top:84px;'><?= $arrQ['betriebsmodus'] ?></div>

        <div style='position:absolute; right:388px; top:319px;'><?= $arrQ['temp_Saugleitung'] ?> °C</div>
        <div style='position:absolute; right:353px; top:399px;'><? printf("%.2f",$currKW) ?> kW</div>
        <div style='position:absolute; right:353px; top:419px;'><?= $arrQ['betriebsstunden'] ?> h</div>
    <!-- ============================================ icons ======================================================= -->
<?
    foreach ($arrItems as $currItem)
    {
        $currIcon = (bindec($arrQ['DO_buffer']) & $currItem['mask_do_buffer'])?$currItem['icon_on']:'spacer.png';
        if (bindec($arrQ['DI_buffer']) & $currItem['mask_di_buffer']) $currIcon = $currItem['icon_fail'];

        echo "<div style='position:absolute; right:".$currItem['right']."px; top:".$currItem['top']."px;'>" .
             "<img src='img/".$currIcon."' alt='' width='50' height='40' title='".$currItem['name']."'>" .
             "</div>\n";

    }
?>
    </div>
</body>
</html>
