date("e"), 'i18n' => "nl" ); } if( $_GET['tz'] ) $tz_info['tz'] = $_GET['tz']; if( $_GET['lc'] ) $tz_info['i18n'] = $_GET['lc']; $zkl_info['tz'] = $tz_info['tz']; $zkl_info['i18n'] = $tz_info['i18n']; $zkl_info['mcu_versie'] = zkl_get_fw_version($zkl_info['mcu_versie']); $zkl_info['wcpu_versie'] = zkl_get_fw_version($zkl_info['wcpu_versie']); putenv("TZ=" . $zkl_info['tz']); i18n_settext_language($zkl_info['i18n']); setlocale(LC_ALL, $zkl_info['i18n']); // temperature range $T_max = 50; $T_min = -10; if( isset($_GET['Trange']) ) { list($T_min,$T_max) = explode(",", $_GET['Trange']); } $T_range = $T_max - $T_min; // get timeframe and log files $logfiles = array(); if( isset($_GET['tmin']) ) { $tmin = strtotime($_GET['tmin']); if( isset($_GET['tmax']) && $_GET['tmax'] ) $tmax = strtotime($_GET['tmax']); else $tmax = time(); if( isset($_GET['logfile']) ) { if( is_array($_GET['logfile']) ) foreach( $_GET['logfile'] as $logfile ) { list($sdcard,$rpgmcount,$startup) = explode(',', $logfile); $query = "SELECT MIN(t) AS tmin,MAX(t) AS tmax "; $query .= "FROM log_zkl "; $query .= "WHERE "; $query .= " zkl=" . $_GET['zkl'] . " AND "; $query .= " sdcard=" . $sdcard . " AND "; $query .= " rpgmcount=" . $rpgmcount . " AND "; $query .= " startup=" . $startup; $result = mysql_run($query, $db_data_handle); $row = mysql_fetch_assoc($result); if( $row['tmax'] > time() ) { error_log($_SERVER['PHP_SELF'] . ": logfile " . $_GET['zkl'] . "-" . $sdcard . "," . $rpgmcount . "," . $startup . " has bogus 'max(t)'; using 'now'"); $row['tmax'] = time(); } array_push($logfiles, array('sdcard' => $sdcard, 'rpgmcount' => $rpgmcount, 'startup' => $startup, 'tmin' => $row['tmin'], 'tmax' => $row['tmax'])); } else { list($sdcard,$rpgmcount,$startup) = explode(',', $_GET['logfile']); $query = "SELECT MIN(t) AS tmin,MAX(t) AS tmax "; $query .= "FROM log_zkl "; $query .= "WHERE "; $query .= " zkl=" . $_GET['zkl'] . " AND "; $query .= " sdcard=" . $sdcard . " AND "; $query .= " rpgmcount=" . $rpgmcount . " AND "; $query .= " startup=" . $startup; $result = mysql_run($query, $db_data_handle); $row = mysql_fetch_assoc($result); if( $row['tmax'] > time() ) { error_log($_SERVER['PHP_SELF'] . ": logfile " . $_GET['zkl'] . "-" . $sdcard . "," . $rpgmcount . "," . $startup . " has bogus 'max(t)'; using 'now'"); $row['tmax'] = time(); } array_push($logfiles, array('sdcard' => $sdcard, 'rpgmcount' => $rpgmcount, 'startup' => $startup, 'tmin' => $row['tmin'], 'tmax' => $row['tmax'])); } } else if( $use_log_realtime ) { // fake a log file for the drawing loop below array_push($logfiles, array( 'sdcard' => 0, 'rpgmcount' => 0, 'startup' => 0, 'tmin' => $tmin, 'tmax' => $tmax ) ); } else if( !isset($_GET['file']) ) { // retrieve logfiles from the database $query = "SELECT sdcard,rpgmcount,startup,MIN(t) AS tmin,MIN(t_log) AS tlog_min,MAX(t) AS tmax "; $query .= "FROM log_zkl "; $query .= "WHERE "; $query .= " zkl=" . $_GET['zkl'] . " AND "; $query .= " t BETWEEN '" . $tmin . "' AND '". $tmax . "' "; $query .= "GROUP BY sdcard,rpgmcount,startup"; $result = mysql_run($query, $db_data_handle); while( $row = mysql_fetch_assoc($result) ) { array_push($logfiles, $row); } } } else if( !isset($_GET['file']) ) { if( !isset($_GET['logfile']) ) { error_log($_SERVER['PHP_SELF'] . ": no log file or time range defined"); exit(1); } list($sdcard,$rpgmcount,$startup) = explode(',', $_GET['logfile']); $query = "SELECT MIN(t) AS tmin,MAX(t) AS tmax "; $query .= "FROM log_zkl "; $query .= "WHERE "; $query .= " zkl=" . $_GET['zkl'] . " AND "; $query .= " sdcard=" . $sdcard . " AND "; $query .= " rpgmcount=" . $rpgmcount . " AND "; $query .= " startup=" . $startup; $result = mysql_run($query, $db_data_handle); $row = mysql_fetch_assoc($result); if( $row['tmin'] < strtotime("2000-1-1") ) { error_log($_SERVER['PHP_SELF'] . ": logfile " . $_GET['zkl'] . "-" . $sdcard . "," . $rpgmcount . "," . $startup . " has bogus 'min(t)'; using only data from 2000"); $result = mysql_run($query . " AND t >= " . strtotime('2000-1-1'), $db_data_handle); $row = mysql_fetch_assoc($result); } $tmin = $row['tmin']; if( isset($_GET['tmax']) ) $tmax = strtotime($_GET['tmax']); else { if( $row['tmax'] > time() ) { error_log($_SERVER['PHP_SELF'] . ": logfile " . $_GET['zkl'] . "-" . $sdcard . "," . $rpgmcount . "," . $startup . " has bogus 'max(t)'; using 'now'"); $tmax = time(); } else $tmax = $row['tmax']; } array_push($logfiles, array('sdcard' => $sdcard, 'rpgmcount' => $rpgmcount, 'startup' => $startup, 'tmin' => $tmin, 'tmax' => $tmax)); } if( $tmin >= $tmax ) { error_log($_SERVER['PHP_SELF'] . ": empty time range: " . strftime("%D %T", $tmin) . "-" . strftime("%D %T", $tmax)); exit(1); } $tz_offset = date("Z", $tmin); $duration = $tmax - $tmin; // // Draw the graph // header("Content-type: image/png"); header('Pragma: no-cache'); header('Expires: ' . date("r")); // create the image $im = imagecreatetruecolor($width,$height); if( function_exists("imageantialias") ) imageantialias($im, true); $black = imagecolorallocate($im, 0, 0, 0); $white = imagecolorallocate($im, 255, 255, 255); $grey = imagecolorallocate($im, 192, 192, 192); $lgrey = imagecolorallocate($im, 240, 240, 240); $dgrey = imagecolorallocate($im, 128, 128, 128); $green = imagecolorallocate($im, 0, 192, 0); $lgreen = imagecolorallocate($im, 192, 255, 192); $llgreen = imagecolorallocate($im, 248, 255, 248); $dgreen = imagecolorallocate($im, 0, 160, 0); $red = imagecolorallocate($im, 255, 0, 0); $lred = imagecolorallocate($im, 255, 192, 192); $dred = imagecolorallocate($im, 160, 0, 0); $blue = imagecolorallocate($im, 0, 0, 224); $dblue = imagecolorallocate($im, 0, 0, 160); $purple = imagecolorallocate($im, 224, 0, 224); $dpurple = imagecolorallocate($im, 160, 0, 160); $font = 2; // 6x13 $bfont = 3; // 7x13 medium bold $tfont = 1; // 5x8 // // Colors and font to use // $bgcolor = imagecolorallocatealpha($im, 255, 255, 255, $_GET['transparent'] ? 127 : 0); $bgcolor_transp = imagecolorallocatealpha($im, 255, 255, 255, 96); $gridcolor = $black; // main grid and markers $sgridcolor = $lgrey; // guides in the grid $nextdaycolor = $grey; // next day line $linecolor = array( 'on-board' => $blue, 'ntc' => $purple, 'switch3000' => $dgreen ); $zero_color = $lred; // 0 degrees line $info_color = $red; // additional info // // Font selection (dependent on image size) // // labels and legenda if( $width <= 320 || $height <= 240 ) { $labelfont = 1; $infofont = 1; } else if( $width >= 2048 && $height >= 1536 ) { $labelfont = 5; $infofont = 3; } else if( $width >= 1280 && $height >= 1024 ) { $labelfont = 3; $infofont = 2; } else { $labelfont = 2; $infofont = 1; } // // Constants // $sensors = array(); $sensors[0] = 'on-board'; $sensors[1] = 'ntc'; $sensors[2] = 'switch3000'; // // Draw the grid // // draw background imagefill($im, 0, 0, $bgcolor); // offsets with the drawing grid // largest value label: 1.000 // x: left to right // y: bottom to top global $x_off, $x_ext, $y_off, $y_ext; $x_off = 3 * imagefontwidth($labelfont) // value labels + + imagefontheight($labelfont) // legend + + (imagefontheight($labelfont) >> 1); // extra spacing $x_ext = $width - $x_off - (imagefontheight($labelfont) >> 1); // + extent of the last date label $y_off = $height - 3 - strlen(strftime("%X")) * imagefontwidth($labelfont); // date/time $y_ext = $y_off; if( !isset($_GET['hide_title']) ) { $y_ext -= imagefontheight($labelfont); // title if( $use_log_realtime ) $y_ext -= imagefontheight($labelfont); } else { $y_ext -= imagefontheight($labelfont) >> 1; // extent of the top value label } // The grid is drawn _under_ the lines and ornaments like the title etc // Y tick marks every 5.0 C, range -10 to +50 for( $T = $T_min; $T <= $T_max; $T += 5 ) { $y = ($y_ext * ($T - $T_min) / $T_range); imageline($im, $x_off, $y_off - $y, $x_off + $x_ext, $y_off - $y, $T == 0 ? $zero_color : $lgrey); imageline($im, $x_off - 2, $y_off - $y, $x_off, $y_off - $y, $black); imagestringalign($im, $labelfont, $x_off - 2, $y_off - $y, sprintf("%d", $T), $gridcolor, ALV_MIDDLE | ALH_RIGHT ); } // X tick marks // find a correct stepping for the date labels first $x_tick_sec = array(5,15,1*60,5*60,15*60,30*60,60*60,4*60*60,6*60*60,8*60*60,12*60*60,24*60*60,7*24*60*60,30*24*60*60,365*24*60*60); for( $step = 0, $x_tick_step = 0; $x_tick_step < (imagefontheight($labelfont) << 1) && $step < count($x_tick_sec); $step++ ) { $x_tick_off = $x_tick_sec[$step]; $x_tick_step = ($x_ext * $x_tick_off) / $duration; $x_tick_val = $x_tick_off - (($tmin + $tz_offset) % $x_tick_off); if( $x_tick_val == $x_tick_off ) $x_tick_val = 0; } if( $x_tick_off >= (24*60*60) ) { if( strtotime("first day of next year", time()) !== FALSE ) { // big steps; use days/weeks/months/years for DST and leap years if( $x_tick_off == (365*24*60*60) ) { $t = strtotime("this year January 01 midnight", $tmin); $t_step = "first day of next year"; } else if( $x_tick_off == (30*24*60*60) ) { $t = strtotime("first day of this month midnight", $tmin); $t_step = "first day of next month"; } else if( $x_tick_off == (7*24*60*60) ) { $t = strtotime("this week Monday midnight", $tmin); $t_step = "+1 weeks"; } else { $t = strtotime("midnight", $tmin); $t_step = "tomorrow"; } } else { // "nice" arithmatic is not supported if( $x_tick_off == (365*24*60*60) ) { $t = strtotime("this year January 01 midnight", $tmin); $t_step = "+1 year"; } else if( $x_tick_off == (30*24*60*60) ) { $t = strtotime("first day of this month midnight", $tmin); $t_step = "+1 month"; } else if( $x_tick_off == (7*24*60*60) ) { $t = strtotime("this week Monday midnight", $tmin); $t_step = "+1 weeks"; } else { $t = strtotime("midnight", $tmin); $t_step = "tomorrow"; } } } else { if( $x_tick_off >= 60*60 ) { $t_step = "+" . ($x_tick_off / (60*60)) . " hours"; } else if( $x_tick_off >= 60 ) { $t_step = "+" . ($x_tick_off / 60) . " minutes"; } else { $t_step = "+" . $x_tick_off . " seconds"; } $t = $tmin; if( $x_tick_val > 0 ) { //error_log("correction " . $x_tick_val . " s"); $t = strtotime("+" . $x_tick_val . " seconds", $t); } } //error_log(date("Y-m-d H:i:s", $t) . "(" . $t . ")->" . date("Y-m-d H:i:s", $tmax) . "(" . $tmax . "), " . $t_step . "," . $x_tick_off . "," . $x_step); while( $t <= $tmax ) { if( $t >= $tmin ) { $x = ((($t - $tmin) / $duration) * $x_ext); if( $x_tick_off >= (24*60*60) ) { if( $x_tick_off == (30*24*60*60) ) { // month name imageline($im, $x_off + $x, $y_off , $x_off + $x, $y_off - $y_ext - 1, $nextdaycolor); imageline($im, $x_off + $x, $y_off + 2, $x_off + $x, $y_off, $gridcolor); imagestringupalign($im, $labelfont, $x_off + $x, $y_off - 2, strftime("%B", $t), $nextdaycolor, ALV_BOTTOM | ALH_LEFT); } else if( $x_tick_off == (7*24*60*60) ) { // month name imageline($im, $x_off + $x, $y_off , $x_off + $x, $y_off - $y_ext - 1, $nextdaycolor); imageline($im, $x_off + $x, $y_off + 2, $x_off + $x, $y_off, $gridcolor); imagestringupalign($im, $labelfont, $x_off + $x, $y_off - 2, strftime("%V", $t), $nextdaycolor, ALV_BOTTOM | ALH_LEFT); } else { imageline($im, $x_off + $x, $y_off , $x_off + $x, $y_off - $y_ext - 1, $sgridcolor); imageline($im, $x_off + $x, $y_off + 2, $x_off + $x, $y_off, $gridcolor); } imagestringupalign($im, $labelfont, $x_off + $x, $y_off + 2, strftime("%x", $t), $gridcolor, ALV_TOP | ALH_CENTER); } else { if( strftime("%H%M%S", $t) == "000000" ) { imageline($im, $x_off + $x, $y_off , $x_off + $x, $y_off - $y_ext - 1, $nextdaycolor); imageline($im, $x_off + $x, $y_off + 2, $x_off + $x, $y_off, $gridcolor); imagestringupalign($im, $labelfont, $x_off + $x, $y_off - 2, strftime("%x", $t), $nextdaycolor, ALV_BOTTOM | ALH_LEFT); } else { imageline($im, $x_off + $x, $y_off , $x_off + $x, $y_off - $y_ext - 1, $sgridcolor); imageline($im, $x_off + $x, $y_off + 2, $x_off + $x, $y_off, $gridcolor); } imagestringupalign($im, $labelfont, $x_off + $x, $y_off + 2, strftime("%X", $t), $gridcolor, ALV_TOP | ALH_CENTER); } } // increment $t (yup, that's what the line below does) $t = strtotime($t_step, $t); if( $t === FALSE ) { trigger_error("Date/time error when adding \"" . $t_step . "\"", E_USER_ERROR); } } if( $x_tick_off < (24*60*60) && (($x_tick_val / $duration) * $x_ext) > imagefontheight($labelfont) ) { // show start day only on detailed plots (it is already in the titles) imagestringupalign($im, $labelfont, $x_off, $y_off - 2, strftime("%x", $tmin), $grey, ALV_BOTTOM | ALH_LEFT); } // // Draw the legend // $y_legend_off = $y_off; $y_legend_off -= ($y_ext - (12+4+7+4+($zkl_info['mcu_versie']['devtype'] == 0x0004 ? 14+4+4 : 0)) * imagefontwidth($labelfont)) >> 1; imagestringupalign($im, $labelfont, 0, $y_legend_off - 8 * imagefontwidth($labelfont), "on-board", $gridcolor, ALV_TOP | ALH_LEFT); imageline($im, imagefontheight($labelfont) >> 1, $y_legend_off - 9 * imagefontwidth($labelfont), /* space */ imagefontheight($labelfont) >> 1, $y_legend_off - 12 * imagefontwidth($labelfont), /* line length */ $linecolor['on-board'] ); $y_legend_off -= (12+4) * imagefontwidth($labelfont); imagestringupalign($im, $labelfont, 0, $y_legend_off - 3 * imagefontwidth($labelfont), "ntc", $gridcolor, ALV_TOP | ALH_LEFT); imageline($im, imagefontheight($labelfont) >> 1, $y_legend_off - 4 * imagefontwidth($labelfont), /* space */ imagefontheight($labelfont) >> 1, $y_legend_off - 7 * imagefontwidth($labelfont), /* line length */ $linecolor['ntc'] ); $y_legend_off -= (7+4) * imagefontwidth($labelfont); if( $zkl_info['mcu_versie']['devtype'] == 0x0004 ) { imagestringupalign($im, $labelfont, 0, $y_legend_off - 10 * imagefontwidth($labelfont), "switch3000", $gridcolor, ALV_TOP | ALH_LEFT); imageline($im, imagefontheight($labelfont) >> 1, $y_legend_off - 11 * imagefontwidth($labelfont), /* space */ imagefontheight($labelfont) >> 1, $y_legend_off - 14 * imagefontwidth($labelfont), /* line length */ $linecolor['switch3000'] ); $y_legend_off -= (14+4) * imagefontwidth($labelfont); } imagestringupalign($im, $labelfont, 0, $y_legend_off - 4 * imagefontwidth($labelfont), "(\xB0C)", $gridcolor, ALV_TOP | ALH_LEFT); // // Draw the border to the left and bottom of the canvas // imageline($im, $x_off, $y_off, $x_off, $y_off - $y_ext + 1, $gridcolor); imageline($im, $x_off, $y_off, $x_off + $x_ext, $y_off, $gridcolor); // // Plot the data // foreach( $logfiles as $logfile ) { // get tmin,tmax for this logfile $log_tmin = $logfile['tmin']; $log_tmax = $logfile['tmax']; if( $log_tmin < $tmin ) $log_tmin = $tmin; if( $log_tmax > $tmax ) $log_tmax = $tmax; //error_log("logfile(" . $logfile['sdcard'] . "," . $logfile['rpgmcount'] . "," . $logfile['startup'] . ") " . date("Y-m-d H:i:s", $log_tmin) . "->" . date("Y-m-d H:i:s", $log_tmax)); // $sdcard = $logfile['sdcard']; $rpgmcount = $logfile['rpgmcount']; $startup = $logfile['startup']; // cache minimum and maximum id; this is a lot faster for some reason if( $use_log_realtime ) { $query = "SELECT MIN(id),MAX(ID) FROM log_realtime WHERE zkl=" . $_GET['zkl'] . " AND t BETWEEN " . $log_tmin . " AND " . $log_tmax; $result = mysql_run($query, $db_data_handle); if( ($result = mysql_run($query, $db_data_handle)) ) { $row = mysql_fetch_array($result); $id_min = $row[0]; $id_max = $row[1]; } else exit(im_print_error($im, "No data")); $query = "SELECT MAX(id) FROM log_realtime WHERE zkl=" . $_GET['zkl'] . " AND t <= " . $log_tmin; if( ($result = mysql_run($query, $db_data_handle)) ) { $row = mysql_fetch_array($result); $id_tmin = $row[0]; } else { // no suitable minimum $query = "SELECT MIN(id) FROM log_realtime WHERE zkl=" . $_GET['zkl']; $result = mysql_run($query, $db_data_handle); $row = mysql_fetch_array($result); $id_tmin = $row[0]; } } // get the data if( $use_log_realtime ) { $query = "SELECT id,t,temp_onboard,temp_ntc "; $query .= "FROM log_realtime "; $query .= "WHERE "; $query .= " zkl=" . $_GET['zkl'] . " AND "; $query .= " ("; if( $id_tmin ) $query .= " id=" . $id_tmin . " OR "; $query .= " ("; $query .= " id BETWEEN " . $id_min . " AND " . $id_max . " AND "; $query .= " (changes & 0x3000) != 0"; $query .= " )"; $query .= " ) "; $query .= "ORDER BY id"; $result = mysql_run($query, $db_data_handle); } else { $query = "SELECT log_zkl.id,t,log_temp.sensor,log_temp.temp "; $query .= "FROM log_zkl,log_temp "; $query .= "WHERE "; $query .= " log_zkl.id=log_temp.id AND "; $query .= " log_zkl.zkl=" . $_GET['zkl'] . " AND "; $query .= " log_zkl.sdcard=" . $sdcard . " AND "; $query .= " log_zkl.rpgmcount=" . $rpgmcount . " AND "; $query .= " log_zkl.startup=" . $startup . " "; $query .= "ORDER BY log_zkl.id"; $result = mysql_run($query, $db_data_handle); } //error_log(($use_log_realtime ? "log_realtime" : "log_zkl") . ": " . mysql_num_rows($result) . " rows"); unset($t_begin['on-board']); unset($t_begin['ntc']); unset($t_begin['switch3000']); $T = array(); while( $row = mysql_fetch_assoc($result) ) { if( !$use_log_realtime ) $s_start = $s_end = array_search($row['sensor'], $sensors); else { $s_start = 0; $s_end = 1; } for( ; $s_start <= $s_end; $s_start++ ) { $sensor = $sensors[$s_start]; if( !$use_log_realtime ) $T_db = $row['temp']; else { $T_db = $row['temp_' . str_replace('-', '', $sensor)]; if( !isset($T_db) ) continue; } if( !isset($t_begin[$sensor]) ) { $t_begin[$sensor] = $row['t']; $T[$sensor] = $T_db; } else { $t_start = $t_begin[$sensor]; $t_this = $t_end = $row['t']; if( $t_end >= $log_tmin && $t_start <= $log_tmax ) { if( $t_start < $log_tmin ) { $t_start = $log_tmin; } if( $t_end < $log_tmin ) break; else if( $t_end > $log_tmax ) { $t_end = $log_tmax; } $prev_level = $T[$sensor]; imageline($im, $x_off + ((($t_start - $tmin) / $duration) * $x_ext), $y_off - ($y_ext * ($prev_level - $T_min) / $T_range), $x_off + ((($t_end - $tmin) / $duration) * $x_ext), $y_off - ($y_ext * ($prev_level - $T_min) / $T_range), $linecolor[$sensor] ); imageline($im, $x_off + ((($t_end - $tmin) / $duration) * $x_ext), $y_off - ($y_ext * ($prev_level - $T_min) / $T_range), $x_off + ((($t_end - $tmin) / $duration) * $x_ext), $y_off - ($y_ext * ($T_db - $T_min) / $T_range), $linecolor[$sensor] ); } $t_begin[$sensor] = $t_this; $T[$sensor] = $T_db; } } } // draw last segment foreach( $sensors as $sensor ) if( isset($t_begin[$sensor]) ) { $t_start = $t_begin[$sensor]; $t_end = $log_tmax; if( $t_end >= $log_tmin && $t_start <= $log_tmax ) { if( $t_start < $log_tmin ) { $t_start = $log_tmin; } if( $t_end > $log_tmin ) { if( $t_end > $log_tmax ) { $t_end = $log_tmax; } imageline($im, $x_off + ((($t_start - $tmin) / $duration) * $x_ext), $y_off - ($y_ext * ($T[$sensor] - $T_min) / $T_range), $x_off + ((($t_end - $tmin) / $duration) * $x_ext), $y_off - ($y_ext * ($T[$sensor] - $T_min) / $T_range), $linecolor[$sensor] ); } } } } // // Draw the title, on top of everything else // if( !isset($_GET['hide_title']) ) { imagestringborderalign($im, $labelfont, $width >> 1, 0, sprintf(_("%s - Temperatuur - %s tot %s"), $zkl_info['idcode'], strftime("%c", $tmin), strftime("%c", $tmax)), $gridcolor, $bgcolor_transp, ALV_TOP | ALH_CENTER); if( $use_log_realtime ) imagestringborderalign($im, $labelfont, $width >> 1, imagefontheight($labelfont), _("(real-time data log)"), $gridcolor, $bgcolor_transp, ALV_TOP | ALH_CENTER); } // // Output the final image // imagepng($im); imagedestroy($im); ?>