<?php require_once('Connections/osense.php'); ?>
<?php
session_start();
#mysql_select_db($database_osense, $osense);
$query_lsense = "SELECT * FROM nodebusy, lightsense where (nodebusy.node_id=lightsense.node_id) ORDER BY lightsense.node_no ASC";
$lsense = mysqli_query($osense,$query_lsense );# or die(mysql_error());
$row_lsense = mysqli_fetch_assoc($lsense);
$totalRows_lsense = mysqli_num_rows($lsense);
$critical = 1;

$numOfCol = 17;
$numOfRow = 9;
$numOfCellInGrid = $numOfCol * $numOfRow;
//echo $totalRows_lsense
define("NODE_DOESNOT_EXIST_ID", 10000);
?>
<html>
<head>
<title>NetEye Light Sensor</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<style type="text/css">
<!--
.style1 {
	font-size: xx-large;
	font-weight: bold;
	color: #336666;
}
.border {
	border: medium solid #336666;
}
.coff {background-color:#669966;}
.con {background-color:#FF3333;font-size: 12;}
.cdead {background-color:#848484;font-size: 12;}
-->
</style>
<link rel="stylesheet" type="text/css" href="sdmenu/sdmenu.css" />
<script type="text/javascript" src="sdmenu/sdmenu.js"></script>
<script type="text/javascript" src="jquery-1.2.2.pack.js"></script>

	<script type="text/javascript">
	// <![CDATA[
	var myMenu;
	window.onload = function() {
		myMenu = new SDMenu("my_menu");
		myMenu.init();
	};
	// ]]>
	</script>
<script type="text/javascript">
<!-- Begin
function reFresh() {
  location.reload(true)
}
/* Set the number below to the amount of delay, in milliseconds,
you want between page reloads: 1 minute = 60000 milliseconds. */
window.setInterval("reFresh()",10000);
// End -->
</script>

</head>

<body>
<table width="1000" height="736" align="center" class="border">
  <tr>
    <td width="157"><div align="center"><img src="wname.jpg" width="150"><span class="style1"> </span></div></td>
    <td width="672"><div align="center"><span class="style1">NetEye</span></div></td>
    <td width="149"><span class="style1"><img src="wicon.jpg"></span></td>
  </tr>
  <tr height="640">
    <td width="157">
	<div style="float: left" id="my_menu" class="sdmenu">
      <div>
        <span>NetEye</span>
        <a href="home.php">Home</a>
        <a href="topology.php">Topology</a>
		<!--a href="registration.php">Registration Form</a-->
      </div>
      <div>
        <span>MySpace</span>
        <?php
		if ($_SESSION['ilog']!="yes")
		{
		?> 
		<a href="login.php">Login</a>
		<?php
		}
		else
		{
		?> 
		<!--a href="logout.php">Logout</a-->
		<?php
		}
		?> 
		<a href="sjobs.php">Schedule jobs</a>
        <a href="myjobs.php">Manage jobs</a>
        <a href="nodebusy.php">Doctor (node status)</a>
        <a href="cnodegrp1.php">Create node group</a>
        <a href="logout.php">Logout</a>
       <!--a href="sjobs1.php">Schedule new tier2 jobs</a-->
        <!--a href="myjobs1.php">Manage tier2 jobs</a-->
      </div>
	<?php
       if ($_SESSION['MM_UserGroup']==1)		{ ?>
       <div>
        <span>AdminSpace</span>
		<a href="inlogadd.php">Create Users</a>
		<a href="regman.php">Manage Registration</a>
      </div>
	<?php	}	?> 

      <div class="collapsed">
        <span>Support</span>
        <a href=http://www.cs.wayne.edu/saifullah/CRI_Lab.html>CRI Research Lab</a>
      </div>

    </div>
	</td>
	<td colspan="2"><div align="center">
       <span align="center"><strong><font face="Arial" size="3" color="black">Realtime nodes status ...</font></strong></span><br><br>
      <table width="320" height="302" border="2" cellspacing="5" align="center" class="border">
		<tr>
			<td>&nbsp;</td>
			 <?php 
			  $i = 0;
			  do {
				echo "<td>".$i."</td>";			  				  
				$i = $i + 1;
			  } while($i < $numOfCol); ?>
			<td>&nbsp;</td>                 
        </tr>

		<?php
			$rowIndex = 0;
			$cellId = 0;	
            $ii = -1;
			do {										    
				$rowIndex = floor($cellId / $numOfCol); // numofcol=17
				
				$inRowPos = $cellId % $numOfCol;
								
				$posX = $rowIndex;
				$posY = $inRowPos;				
				$query_status = "SELECT busy, node_no, hstatus FROM nodebusy where pos_X = ".$posX." and pos_Y = ".$posY.";";
				$qset = mysqli_query( $osense,$query_status);# or die(mysql_error());
				
				// $noPosCode = -99; // big bug, why??
				$node_no = NODE_DOESNOT_EXIST_ID;
				$status  = NODE_DOESNOT_EXIST_ID; 
				if ($row = mysqli_fetch_assoc($qset)) {
					$node_no = $row['node_no'];
                    $status  = $row['hstatus'];
				} else {                
                    // node does not exist
				}		
					 
				if ($inRowPos == 0){
					echo "<tr>"; 
					echo "<td>".$rowIndex."</td>";
                    showStatus($status, $node_no);

				} else if($inRowPos == ($numOfCol - 1)){

                    showStatus($status, $node_no);
					echo "<td>".$rowIndex."</td>";
					echo "</tr>";
				} else {
                    showStatus($status, $node_no);
				}
				
				$cellId++;
			} while($cellId < $numOfCellInGrid);
			
			mysqli_close($lsense);
		?>
	 
<?php
    function showStatus($status, $node_no) {
        switch($status) {
            case 4:
	            echo "<td><strong><div align='center' class='con'>".$node_no."</div></td>";
                break;
            case 5:
	            echo "<td><strong><div align='center' class='coff'>".$node_no."</div></td>";
                break;
            case -1:
	            echo "<td><strong><div align='center' class='cdead'>".$node_no."</div></td>";
                break;
            // -----
            case NODE_DOESNOT_EXIST_ID:
                echo "<td>&nbsp</td>";
                break;
            default:
                echo "<td><strong>BUG</td>";
                
      }
  }
?>        
		<tr>
			<td>&nbsp;</td>
			 <?php 
			  $i = 0;
			  do {
				echo "<td>".$i."</td>";			  				  
				$i = $i + 1;
			  } while($i < $numOfCol); ?>
			<td>&nbsp;</td>                 
        </tr>
		
      </table
      <p>
    <span align="left"><strong><font color="red">       [Red:Busy]&nbsp;|&nbsp;</font></strong></span>
    <span align="left"><strong><font color="green">     [Green]:Free&nbsp;|&nbsp;</font></strong></span>
    <span align="left"><strong><font color="dimgray">   [Grey]:Unavailable&nbsp;|&nbsp;</font></strong></span>
      
      </td>
   
</body>
  </tr>

</table>

</html>
<?php
mysqli_free_result($lsense);
?>
