<?php
/*****************************************************************************
 * Copyright (C) 2024
 *   
 * Michiel Broek <mbroek at mbse dot eu>
 *
 * This file is part of mbsePi-apps
 *
 * 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.
 *
 * BrewCloud 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.
 *****************************************************************************/

/*
 * Look for the style names in the jqwidgets/styles directory.
 */
//$my_style = 'ui-redmond';
$my_style = 'ui-mbse';

if (isset($_GET['uuid']))
        $my_uuid = $_GET['uuid'];
else
        $my_uuid = '';

require_once($_SERVER['DOCUMENT_ROOT'].'/version.php');


function open_socket()
{
    $sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);

    if (!($sock === false)) {
        if (socket_connect($sock, "localhost", 6554)) {
            socket_set_option($sock, SOL_SOCKET, SO_RCVTIMEO, array('sec' => 15, 'usec' => 0));
        } else {
            socket_close($sock);
        }
    }
    return $sock;
}


/**
 * @param string $command to send to the server.
 * @return string with the complete reply from the
 *         server. This can be a multiline reply.
 */
function send_cmd($command)
{
    $sock = open_socket();
    if ($sock == false) {
        return "";
    }
    socket_write($sock, $command . "\r\n", 4096);

    $answer = "";
    while (1) {
        $line = socket_read($sock, 4096);
        if ($line === '')
            break;
        $answer .= $line;
    }
    socket_close($sock);

    return $answer;
}


function startsWith($haystack, $needle)
{
    return !strncmp($haystack, $needle, strlen($needle));
}


function page_header($title, $loadjs) {
	global $my_style;
	global $my_version;
	global $my_uuid;
?>
<!DOCTYPE html>
<html>
 <head>
  <meta charset="utf-8">
  <title>ThermFerm v<?php echo $my_version;?> - <?php echo $title;?></title>
  <link type="text/css" href="css/style.css" rel="stylesheet" media="all" />
  <link type="text/css" href="jqwidgets/styles/jqx.base.css" rel="stylesheet" />
  <link type="text/css" href="jqwidgets/styles/jqx.<?php echo $my_style; ?>.css" rel="stylesheet" />
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <script>
    var theme = "<?php echo $my_style; ?>", my_uuid = "<?php echo $my_uuid; ?>";
  </script>
  <script src="js/jquery-2.1.1.min.js"></script>
  <script src="js/reconnecting-websocket.min.js"></script>
  <script src="jqwidgets/jqxcore.js"></script>
  <script src="jqwidgets/jqxwindow.js"></script>
  <script src="jqwidgets/jqxmenu.js"></script>
  <script src="jqwidgets/jqxtabs.js"></script>
  <script src="jqwidgets/jqxdata.js"></script>
  <script src="jqwidgets/jqxdata.export.js"></script>
  <script src="jqwidgets/jqxloader.js"></script>
  <script src="jqwidgets/jqxbuttons.js"></script>
  <script src="jqwidgets/jqxscrollbar.js"></script>
  <script src="jqwidgets/jqxgrid.js"></script>
  <script src="jqwidgets/jqxgrid.grouping.js"></script>
  <script src="jqwidgets/jqxgrid.edit.js"></script>
  <script src="jqwidgets/jqxgrid.sort.js"></script>
  <script src="jqwidgets/jqxgrid.pager.js"></script>
  <script src="jqwidgets/jqxgrid.filter.js"></script>
  <script src="jqwidgets/jqxgrid.selection.js"></script>
  <script src="jqwidgets/jqxnumberinput.js"></script>
  <script src="jqwidgets/jqxlistbox.js"></script>
  <script src="jqwidgets/jqxdropdownlist.js"></script>
  <script src="jqwidgets/jqxdropdownbutton.js"></script>
  <script src="jqwidgets/jqxinput.js"></script>
  <script src="jqwidgets/jqxeditor.js"></script>
  <script src="jqwidgets/jqxtooltip.js"></script>
  <script src="jqwidgets/jqxswitchbutton.js"></script>
  <script src="jqwidgets/jqxradiobutton.js"></script>
  <script src="jqwidgets/jqxcheckbox.js"></script>
  <script src="jqwidgets/jqxgauge.js"></script>
  <script src="jqwidgets/jqxdraw.js"></script>
  <script src="jqwidgets/jqxpasswordinput.js"></script>
  <script src="jqwidgets/globalization/globalize.js"></script>
  <script src="js/global.js"></script>
<?php
if (strlen($loadjs))
    echo  '  <script src="js/'.$loadjs.'.js"></script>'.PHP_EOL;
?>
 </head>

 <body class="default">
  <div id="jqxWidget">
   <div id="jqxLoader">
   </div>

   <!-- Menu -->
   <div id="jqxMenu" style='float: left;'>
    <ul>
     <li style='width: 80px;'><a href="index.php">Home</a></li>
     <li style='width: 80px;'>Fermenters
      <ul style='width: 200px;'>
<?php
    $answer = send_cmd("LIST");
    if (strlen($answer)) {
	$arr = explode("\r\n", $answer);

        if (startsWith($arr[0], "212")) {
            $i = 1;
	    while (1) {
                if (strcmp($arr[$i], ".") == 0)
                        break;
		$parts = explode(",", $arr[$i]);
		echo "       <li><img style='float: left; margin-right: 5px;' src='images/fridge.png' />";
		echo "<a href='fermenter.php?uuid=".$parts[0]."'>".$parts[1]."</a></li>".PHP_EOL;
                $i++;
            }
        }
    }
?>
      </ul>
     </li>
     <li style='width: 80px;'>Setup
      <ul style='width: 200px;'>
       <li><img style='float: left; margin-right: 5px;' src='images/preferences.png' /><a href="set_global.php">Global</a></li>
       <li><img style='float: left; margin-right: 5px;' src='images/database.png' /><a href="set_devices.php">Devices</a></li>
       <li><img style='float: left; margin-right: 5px;' src='images/fermenter.png' /><a href="set_fermenters.php">Fermenters</a></li>
       <li><img style='float: left; margin-right: 5px;' src='images/computer.png' /><a href="set_simulators.php">Simulators</a></li>
       <li><img style='float: left; margin-right: 5px;' src='images/database.png' /><a href="list_onewire.php">One-wire bus</a></li>
      </ul>
     </li>
     <li style='width: 80px;'><a href="gen_about.php">Over</a></li>
    </ul>
   </div> <!-- End menu -->

<?php
}


function confirm_delete() {
?>
   <div id="eventWindow">
    <div>
     Confirm delete
    </div>
    <div>
     <div>
      Press "OK" to confirm to delete this record.<br>
      Press "Cancel" to close without deleting this record.
     </div>
     <div>
      <div style="float: right; margin-top: 45px; margin-bottom: 10px;">
       <input type="button" id="delOk" value="OK" style="margin-right: 15px" />
       <input type="button" id="delCancel" value="Cancel" style="margin-right: 125px" />
      </div>
     </div>
    </div>
   </div>

<?php
}


function page_footer() {
?>
  </div> <!-- jqxWidget -->
 </body>
</html>
<?php
}
?>
