www-thermferm/archives.php

Mon, 07 Jan 2019 13:23:17 +0100

author
Michiel Broek <mbroek@mbse.eu>
date
Mon, 07 Jan 2019 13:23:17 +0100
changeset 568
eee0f52170e8
parent 539
300b5c4cd977
permissions
-rw-r--r--

Switched export server to https

<?php
/**
 * @file archives.php
 * @brief Display as chart or download a fermentation logfile.
 * @author Michiel Broek <mbroek at mbse dot eu>
 *
 * Copyright (C) 2015-2018
 *   
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 2, or (at your option) any
 * later version.
 *
 * ThermFerm is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with ThermFerm; see the file COPYING.  If not, write to the Free
 * Software Foundation, 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
 */


require_once('utilities.php');
$my_style = 'ui-redmond';

if (isset($_GET['action'])) {
    switch ($_GET['action']) {
	case 'dl':              archive_dl();
				break;
	case 'view':		archive_view();
				break;
	default:                break;
    }
} else {
    archive_dir();
}

exit;

/****************************************************************************
 *
 */

/*
 * View an archived file in graph format
 */
function archive_view() {

    global	$my_style;

    $name = urldecode($_GET['name']);
    $return = urldecode($_GET['return']);

    $answer = send_cmd('GLOBAL GET');
    $arr = explode("\r\n", $answer);
    $version = "?";

    if (startsWith($arr[0], "213")) {
	$j = 1;
	while (1) {
	    if (strcmp($arr[$j], ".") == 0)
		break;
	    $f = explode(",", $arr[$j]);

	    if ($f[0] == "RELEASE")
		$version = $f[1];
	    $j++;
	}
    }

    $outstr  = '<!DOCTYPE html>'.PHP_EOL;
    $outstr .= '<html>'.PHP_EOL;
    $outstr .= ' <head>'.PHP_EOL;
    $outstr .= '  <meta http-equiv="content-type" content="text/html; charset=utf-8" />'.PHP_EOL;
    $outstr .= '  <title>Archive View '.$name.'</title>'.PHP_EOL;
    $outstr .= '  <link type="text/css" href="css/style.css" rel="stylesheet" media="all" />'.PHP_EOL;
    $outstr .= '  <link type="text/css" href="jqwidgets/styles/jqx.base.css" rel="stylesheet" />'.PHP_EOL;
    $outstr .= '  <link type="text/css" href="jqwidgets/styles/jqx.'.$my_style.'.css" rel="stylesheet" />'.PHP_EOL;
    $outstr .= '  <script type="text/javascript" src="js/jquery-2.1.1.min.js"></script>'.PHP_EOL;
    $outstr .= '  <script type="text/javascript" src="jqwidgets/jqxcore.js"></script>'.PHP_EOL;
    $outstr .= '  <script type="text/javascript" src="jqwidgets/jqxwindow.js"></script>'.PHP_EOL;
    $outstr .= '  <script type="text/javascript" src="jqwidgets/jqxbuttons.js"></script>'.PHP_EOL;
    $outstr .= '  <script type="text/javascript" src="jqwidgets/jqxchart.core.js"></script>'.PHP_EOL;
    $outstr .= '  <script type="text/javascript" src="jqwidgets/jqxdraw.js"></script>'.PHP_EOL;
    $outstr .= '  <script type="text/javascript" src="jqwidgets/jqxdata.js"></script>'.PHP_EOL;
    $outstr .= '  <script type="text/javascript" src="jqwidgets/jqxdata.export.js"></script>'.PHP_EOL;
    $outstr .= ' </head>'.PHP_EOL;

    $outstr .= ' <body class="default">'.PHP_EOL;
    $outstr .= '  <div id="jqxWidget">'.PHP_EOL;
    $outstr .= '   <div id="header">'.PHP_EOL;
    $outstr .= '    <div id="title">'.PHP_EOL;
    $outstr .= '     ThermFerm '.$version.PHP_EOL;
    $outstr .= '    </div>'.PHP_EOL;
    if ("$return" == "archives") {
    	$outstr .= '    <form action="archives.php" style="margin:30px; float:right">'.PHP_EOL;
    	$outstr .= '     <input type="submit" id="archives" value="Archives directory" />'.PHP_EOL;
    } else {
	$outstr .= '    <form action="index.php" style="margin:30px; float:right">'.PHP_EOL;
	$outstr .= '     <input type="submit" id="archives" value="Dashboard" />'.PHP_EOL;
    }
    $outstr .= '    </form>'.PHP_EOL;
    $outstr .= '   </div> <!-- header -->'.PHP_EOL;
    $outstr .= '   <div id="content">'.PHP_EOL;

    $outstr .= '    <script type="text/javascript">'.PHP_EOL;
    $outstr .= '     $(document).ready(function () {'.PHP_EOL;

    $outstr .= '       function getExportServer() {'.PHP_EOL;
    $outstr .= '         return "https://www.jqwidgets.com/export_server/export.php";'.PHP_EOL;
    $outstr .= '       }'.PHP_EOL;

    $outstr .= '       var sourceA ='.PHP_EOL;
    $outstr .= '       {'.PHP_EOL;
    $outstr .= '          datatype: "json",'.PHP_EOL;
    $outstr .= '          datafields: ['.PHP_EOL;
    $outstr .= '             { name: "Date", type: "date", format: "yyyy-MM-dd HH:mm" },'.PHP_EOL;
    $outstr .= '             { name: "Mode" },'.PHP_EOL;
    $outstr .= '             { name: "Air", type: "float" },'.PHP_EOL;
    $outstr .= '             { name: "Beer", type: "float" },'.PHP_EOL;
    $outstr .= '             { name: "Target_lo", type: "float" },'.PHP_EOL;
    $outstr .= '             { name: "Target_hi", type: "float" },'.PHP_EOL;
    $outstr .= '             { name: "Heater", type: "int" },'.PHP_EOL;
    $outstr .= '             { name: "Cooler", type: "int" },'.PHP_EOL;
    $outstr .= '             { name: "HeatUse", type: "int" },'.PHP_EOL;
    $outstr .= '             { name: "CoolUse", type: "int" },'.PHP_EOL;
    $outstr .= '             { name: "Room", type: "float" },'.PHP_EOL;
    $outstr .= '             { name: "Chiller", type: "float" }'.PHP_EOL;
    $outstr .= '          ],'.PHP_EOL;
    $outstr .= '          url: \'getalog.php?name='.urlencode($name).'.log\''.PHP_EOL;
    $outstr .= '       };'.PHP_EOL;

    $outstr .= '       var dataAdapter = new $.jqx.dataAdapter(sourceA,'.PHP_EOL;
    $outstr .= '       {'.PHP_EOL;
    $outstr .= '          autoBind: true,'.PHP_EOL;
    $outstr .= '          async: false,'.PHP_EOL;
    $outstr .= '          downloadComplete: function () { },'.PHP_EOL;
    $outstr .= '          loadComplete: function () { },'.PHP_EOL;
    $outstr .= '          loadError: function () { }'.PHP_EOL;
    $outstr .= '       });'.PHP_EOL;

    $outstr .= '       var settings = {'.PHP_EOL;
    $outstr .= '         title: "'.$name.'",'.PHP_EOL;
    $outstr .= '         description: "",'.PHP_EOL;
    $outstr .= '         padding: { left: 5, top: 5, right: 5, bottom: 5 },'.PHP_EOL;
    $outstr .= '         titlePadding: { left: 0, top: 0, right: 0, bottom: 10 },'.PHP_EOL;
    $outstr .= '         source: dataAdapter,'.PHP_EOL;
    $outstr .= '         xAxis:'.PHP_EOL;
    $outstr .= '           {'.PHP_EOL;
    $outstr .= '             dataField: \'Date\','.PHP_EOL;
    $outstr .= '             type: \'date\','.PHP_EOL;
    $outstr .= '             formatFunction: function (value) {'.PHP_EOL;
    $outstr .= '                return value.getDate() + \'-\' + (value.getMonth() + 1) + \'-\' + value.getFullYear()'.PHP_EOL;
    $outstr .= '             },'.PHP_EOL;
    $outstr .= '             toolTipFormatFunction: function (value) {'.PHP_EOL;
    $outstr .= '                var h = value.getHours();'.PHP_EOL;
    $outstr .= '                var m = value.getMinutes();'.PHP_EOL;
    $outstr .= '                return value.getDate() + \'-\' + (value.getMonth() + 1) + \'-\' + value.getFullYear()'; 
    $outstr .= ' + \' \' + (h < 10 ? \'0\' + h : h) + \':\' + (m < 10 ? \'0\' + m : m);'.PHP_EOL;
    $outstr .= '             },'.PHP_EOL;
    $outstr .= '             textRotationAngle: 45,'.PHP_EOL;
    $outstr .= '             showGridLines: false'.PHP_EOL;
    $outstr .= '           },'.PHP_EOL;
    $outstr .= '         colorScheme: \'scheme01\','.PHP_EOL;
    $outstr .= '         seriesGroups:'.PHP_EOL;
    $outstr .= '           [{'.PHP_EOL;
    $outstr .= '             type: "line",'.PHP_EOL;
    $outstr .= '             valueAxis:'.PHP_EOL;
    $outstr .= '             {'.PHP_EOL;
    $outstr .= '               minValue: 0,'.PHP_EOL;
    $outstr .= '               maxValue: 100,'.PHP_EOL;
    $outstr .= '               displayValueAxis: false,'.PHP_EOL;
    $outstr .= '               description: "Heat/Cool %"'.PHP_EOL;
    $outstr .= '             },'.PHP_EOL;
    $outstr .= '             series: ['.PHP_EOL;
    $outstr .= '               { dataField: "CoolUse", lineWidth: 1, displayText: "Cool %", opacity: 0.3 },'.PHP_EOL;
    $outstr .= '               { dataField: "HeatUse", lineWidth: 1, displayText: "Heat %", opacity: 0.3 }'.PHP_EOL;
    $outstr .= '             ]'.PHP_EOL;
    $outstr .= '            },'.PHP_EOL;
    $outstr .= '            {'.PHP_EOL;
    $outstr .= '             type: \'spline\','.PHP_EOL;
    $outstr .= '             valueAxis:'.PHP_EOL;
    $outstr .= '             {'.PHP_EOL;
    $outstr .= '               minValue: 0,'.PHP_EOL;
    $outstr .= '               description: \'Degrees C\''.PHP_EOL;
    $outstr .= '             },'.PHP_EOL;
    $outstr .= '             series: ['.PHP_EOL;
    $outstr .= '               { dataField: "Air", lineWidth: 1, displayText: "Air" },'.PHP_EOL;
    $outstr .= '               { dataField: "Beer", lineWidth: 2, displayText: "Beer" },'.PHP_EOL;
    $outstr .= '               { dataField: "Target_lo", lineWidth: 1, displayText: "Target Lo", opacity: 0.7 },'.PHP_EOL;
    $outstr .= '               { dataField: "Target_hi", lineWidth: 1, displayText: "Target Hi", opacity: 0.7 },'.PHP_EOL;
    $outstr .= '               { dataField: "Room", lineWidth: 1, displayText: "Room", opacity: 0.5 },'.PHP_EOL;
    $outstr .= '               { dataField: "Chiller", lineWidth: 1, displayText: "Chiller", color: \'#0000bb\' }'.PHP_EOL;
    $outstr .= '             ]'.PHP_EOL;
    $outstr .= '           }]'.PHP_EOL;
    $outstr .= '       };'.PHP_EOL;
    $outstr .= '       $("#fermentor_chart").jqxChart(settings);'.PHP_EOL;

    $outstr .= '       $("#print").click(function () {'.PHP_EOL;
    $outstr .= '         var content = $("#fermentor_chart")[0].outerHTML;'.PHP_EOL;
    $outstr .= '         var newWindow = window.open("", "", "width=865, height=425"),'.PHP_EOL;
    $outstr .= '         document = newWindow.document.open(),'.PHP_EOL;
    $outstr .= '         pageContent ='.PHP_EOL;
    $outstr .= '         \'<!DOCTYPE html>\' +'.PHP_EOL;
    $outstr .= '         \'<html>\' +'.PHP_EOL;
    $outstr .= '         \'<head>\' +'.PHP_EOL;
    $outstr .= '         \'<link rel="stylesheet" href="jqwidgets/styles/jqx.base.css" type="text/css" />\' +'.PHP_EOL;
    $outstr .= '         \'<meta charset="utf-8" />\' +'.PHP_EOL;
    $outstr .= '         \'<title>jQWidgets Chart</title>\' +'.PHP_EOL;
    $outstr .= '         \'</head>\' +'.PHP_EOL;
    $outstr .= '         \'<body>\' + content + \'</body></html>\';'.PHP_EOL;
    $outstr .= '         document.write(pageContent);'.PHP_EOL;
    $outstr .= '         document.close();'.PHP_EOL;
    $outstr .= '         newWindow.print();'.PHP_EOL;
    $outstr .= '       });'.PHP_EOL;
    $outstr .= '       $("#print").jqxButton({ width: 100, height: 25, theme: "ui-redmond" });'.PHP_EOL;

    $outstr .= '       $("#pdfButton").click(function () {'.PHP_EOL;
    $outstr .= '            $("#fermentor_chart").jqxChart("saveAsPDF", "Chart_'.$name.'.pdf", getExportServer());'.PHP_EOL;
    $outstr .= '       });'.PHP_EOL;
    $outstr .= '       $("#pdfButton").jqxButton({ width: 100, height: 25, theme: "ui-redmond" });'.PHP_EOL;

    $outstr .= '     });'.PHP_EOL;
    $outstr .= '    </script>'.PHP_EOL;
    $outstr .= '    <div id="fermentor">'.PHP_EOL;
    $outstr .= '     <div id="fermentor_chart" style="width:850px; height:410px; float:left"></div>'.PHP_EOL;
    $outstr .= '     <div style="margin-top: 2px; margin-left: 10px;">'.PHP_EOL;
    $outstr .= '      <input style="float: left; margin-left: 10px;" id="print" type="button" value="Print Graph" />'.PHP_EOL;
    $outstr .= '      <input style="float: left; margin-left: 10px; margin-top: 10px;" id="pdfButton" type="button" value="Save as PDF" />'.PHP_EOL;
    $outstr .= '     </div>'.PHP_EOL;
    $outstr .= '    </div> <!-- fermentor -->'.PHP_EOL;

    $outstr .= '    <script type="text/javascript">'.PHP_EOL;
    $outstr .= '     $(document).ready(function () {'.PHP_EOL;
    $outstr .= '      $("#archives").jqxButton({ width: 150, height: 25, theme: \'ui-redmond\' });'.PHP_EOL;
    $outstr .= '     });'.PHP_EOL;
    $outstr .= '    </script>'.PHP_EOL;
    $outstr .= build_footer();
    echo $outstr;
}



/*
 * Download popup. The file contents is dynamic generated from
 * the thermferm server.
 */
function archive_dl() {

    $name = urldecode($_GET['name']) . ".log";

    header('Content-Type: text/plain');
    header('Content-Disposition: attachment; filename="'.$name.'"');

    $answer = send_cmd('ARCHIVE GET '.$name);
    $arr = explode("\r\n", $answer);

    $outstr  = '';
    if (startsWith($arr[0], "212")) {
	$j = 1;
	while (1) {
	    if (strcmp($arr[$j], ".") == 0)
		break;
	    $outstr .= $arr[$j].PHP_EOL;
	    $j++;
	}
    }
    echo $outstr;
    exit();
}



/*
 * Show directory
 */
function archive_dir() {

    $answer = send_cmd("ARCHIVE DIR");
    $reply = explode("\r\n", $answer);

    $outstr  = build_header("Archived logfiles");
    $outstr .= '    <div id="errors">'.PHP_EOL;
    $outstr .= '    </div> <!-- errors -->'.PHP_EOL;
    $outstr .= '    <div id="etable">'.PHP_EOL;
    $outstr .= '     <table class="setup">'.PHP_EOL;
    $outstr .= '      <tr class="trhead">'.PHP_EOL;
    $outstr .= '       <td class="setup" style="width: 340px;">File name</td>'.PHP_EOL;
    $outstr .= '       <td class="setup" style="width:  90px;">Mode</td>'.PHP_EOL;
    $outstr .= '       <td class="setup" style="width: 100px;">Size</td>'.PHP_EOL;
    $outstr .= '       <td class="setup" style="width: 140px;">Date</td>'.PHP_EOL;
    $outstr .= '       <td class="setup" style="width: 110px;">Action</td>'.PHP_EOL;
    $outstr .= '      </tr>'.PHP_EOL;

    if (startsWith($reply[0], "212")) {
	$j = 1;
	while (1) {
	    if (strcmp($reply[$j], ".") == 0)
		break;
	    $f = explode(",", $reply[$j]);
	    $name = urlencode(basename($f[0], ".log"));
	    $outstr .= '      <tr class="setup">'.PHP_EOL;
	    $outstr .= '       <td class="setup">'.$f[0].'</td>'.PHP_EOL;
	    $outstr .= '       <td class="setup">'.$f[1].'</td>'.PHP_EOL;
	    $outstr .= '       <td class="setup">'.$f[2].'</td>'.PHP_EOL;
	    $outstr .= '       <td class="setup">'.$f[3].'</td>'.PHP_EOL;
	    $outstr .= '       <td class="setup"><a href="archives.php?action=dl&amp;name='.$name.'">Download</a>';
	    $outstr .= ' <a href="archives.php?action=view&amp;return=archives&amp;name='.$name.'">View</a></td>'.PHP_EOL;
	    $outstr .= '      </tr>'.PHP_EOL;
	    $j++;
	}
    }

    $outstr .= '     </table>'.PHP_EOL;
    $outstr .= '    </div> <!-- etable -->'.PHP_EOL;
    $outstr .= '    <script type="text/javascript">'.PHP_EOL;
    $outstr .= '     $(document).ready(function () {'.PHP_EOL;
    $outstr .= '      $("#maintenance").jqxButton({ width: 150, height: 25, theme: \'ui-redmond\' });'.PHP_EOL;
    $outstr .= '     });'.PHP_EOL;
    $outstr .= '    </script>'.PHP_EOL;
    $outstr .= build_footer();
    echo $outstr;
}


mercurial