<%@ page language="java" import="java.util.*,java.sql.*,java.sql.Date,java.sql.Time,java.net.*,java.awt.*,java.awt.image.*,javax.swing.*,java.io.*"%>

<%
    String timespan;
    String environmentParameter;
	String eboxId;
	%>
	
<html>
<head>
<title>Untitled</title>
</head>
<jsp:useBean id="em" scope="session" class="EntrakMain"/>
<jsp:useBean id="sql" scope="session" class="SQLStatement"/>
<body>
<%
			timespan=request.getParameter("resolution");
			eboxId=request.getParameter("eboxId");
%>
<table BORDER ="1"  ALIGN="left" CELLSPACING ="1" CELLPADDING="10" WIDTH="160" >
  <tr> 
	<!--td><b>Zone name</b></td-->
	<td><b> Date </b></td>
	<td><b>Time</b></td>
	<td><b>Item</b></td>
	<td><b>Value</b> 
  </tr>
  <%
	String intervalTerm = "1 MINUTE";
	
        switch(Integer.parseInt(timespan)){

        case 1 :
            intervalTerm = "1 MINUTE";
            break;

        case 2 :
            intervalTerm = "5 MINUTE";
            break;

        case 3 :
            intervalTerm = "10 MINUTE";
            break;

        case 4 :
            intervalTerm = "20 MINUTE";
            break;

        case 5 :
            intervalTerm = "30 MINUTE";
            break;

        default:
            intervalTerm = "1 MINUTE";
        }
    
    sql = new SQLStatement(em.con);
	
	String clName = ClassLoaderName.getName(getClassLoader().toString());
				
    sql.setClassLoaderName(clName);
	
   // String rel_refString = eboxId;  
	String command ="select  date, time, ic_item,value from smarthome.indoor_climate " +
                " where ((NOW() - INTERVAL " + intervalTerm + ") <= date_add(date, interval time hour_second))" +
                " AND ((NOW()) >= date_add(date, interval time hour_second))" +
				" AND rel_ref = "+ "\'"+ eboxId + "\'"+ 
				" order by date DESC, time DESC limit 5 ";
    ResultSet rs = sql.executeSqlRs(command); 						 
	
	while (rs.next()){ 
	     
		// String rel_refString = rs.getString("rel_ref");
    //     String zone_idString = rs.getString("ic_code");		 
		 
		 String dateString = rs.getString("date");
		 
		 String timeString =rs.getString("time");
		 String itemString =rs.getString("ic_item");

		 String valueString =rs.getString("value");
%>
  <tr> 
	 
	<td><%=dateString %></td>
	<td><%=timeString %></td>
	<td><%=itemString %></td>
	<td><%=valueString %></td>
  </tr>
  <%	  		  	 		 		 		 
		}	   	 			 
%>
</table>
</body>
</html>
