
<%@ page language="java" import="java.util.*,java.sql.*,java.sql.Date,java.sql.Time,java.net.*,java.io.*"%>
<%!
    String timeScale;
    String environmentParameter="ALL";
    String resolution="6";
    String ic_item;
    String eboxId = null;	
    String entity = "JWM313";
    String periods[] ={"1","6","12","24"};
%>
<script LANGUAGE="JavaScript">

   //create a global variable so the window object can be accessed elsewhere
   function createWindow(url) {

        var winNum = self.opener.winNum;
        var imgWin = self.opener.imgWin[winNum];

        //if the window.name isn't set, create the window
        if(imgWin == null) {
            var targetWin = "ImageWindow_"+winNum.toString();
            imgWin = window.open(url,targetWin,'status=yes,top=20,left=20,dependent=yes,scrollbars=yes,resizable=no,width=400,height=400,menubar=no');
            //imgWin.location.replace(url);
            imgWin.location.reload();

            self.opener.imgWin[winNum] = imgWin;
            winNum++;
            self.opener.winNum = winNum;
        }//
        else {
            if(imgWin.name != null) {
                imgWin.focus();
            }
        }//else

    }//function createWindow(url)

   function loadFrame(url,target) {
       var newWin=window.open(url,target, 'status=yes,top=20,left=20,dependent=yes,scrollbars=yes,resizable=yes,width=400,height=400,menubar=no');		
        newWin.location.replace(url);
     	//updated from newWin.location.reload();
    } //function loadFrame(url,target)
</script>
<html>
<head>
<title>Real time Data monitoring</title>
</head>

<jsp:useBean id="em" scope="session" class="EntrakMain"/>
<jsp:useBean id="sql" scope="session" class="SQLStatement"/>

<body BGCOLOR="#FFFFFF" TEXT="#CCCCCC" LEFTMARGIN="0" TOPMARGIN="0" MARGINWIDTH="0" MARGINHEIGHT="0">
 
// ============================================================================= 
<%
    sql = new SQLStatement(em.con);
	
	String clName = ClassLoaderName.getName(getClassLoader().toString());
   
    Vector listOfEntity = new Vector();
	Vector listOfEbox_id = new Vector(); 		
				
    sql.setClassLoaderName(clName);
	
   // String rel_refString = eboxId;  
	String command = "select entity.name, monitored.m_id from smarthomes.entity, smarthomes.monitored "+
				" where entity.ref =monitored.m_id and datetime >current_date "+ 
				" group by entity.name order by  entity.name DESC ";
				
    ResultSet rs = sql.executeSqlRs(command); 			
	int count=0;				 
	
	while (rs.next()){ 	     
		String entityName  = rs.getString("entity.name");		 		 
		String eboxID = rs.getString("monitored.m_id");
        listOfEntity.add( entityName ) ;
		listOfEbox_id.add( eboxID );  
        count++;
		System.out.println( entityName + " "+ eboxID);
	}	  
    if(count ==0) { 		 	 					
%><script>    
    loadFrame("message_info.jsp?message= There are No Enity Data. Check Connection and Database","imgFrame_c");
 </script>       
<% 
}
%>
 
<% 
    String actionButtonName = request.getParameter("action");

    if(actionButtonName != null) {
        if (actionButtonName.equals("Start")) {
			environmentParameter=request.getParameter("environmentParameterMenu");
			resolution=request.getParameter("resolutionMenu");
			//ic_item=request.getParameter("ic_item");
			eboxId=request.getParameter("eboxId");			
			ic_item=environmentParameter;								
  		    if(environmentParameter.equalsIgnoreCase("ALL")){ %>
                 <script>
                   loadFrame("DrawTable.jsp?environmentParameter=<%=environmentParameter%>&resolution=<%=resolution%>&ic_item=<%=ic_item%>&eboxId=<%=eboxId%>","imgFrame_c");
                 </script>
                <%
               }
			   else{ 
               %>
                  <script>
                   loadFrame("LineChart?environmentParameter=<%=environmentParameter%>&resolution=<%=resolution%>&ic_item=<%=ic_item%>&eboxId=<%=eboxId%>","imgFrame_c");
                  </script>
               <%
              }//end of else                
           
        } //if (actionButtonName.equals("Draw Chart"))
		
		else {

%>
<script>
            loadFrame("blank.jsp","imgFrame_c");
        </script>
<%
        }
	}
			else {

%>
<script>
            loadFrame("blank.jsp","imgFrame_c");
        </script>
<%

	}
%>
<form METHOD="post">
  <table WIDTH="100%" BORDER="0" CELLSPACING="0" CELLPADDING="0" HEIGHT="100%">
	<tr >
	  <td VALIGN="MIDDLE" ALIGN="CENTER"> 
		<table WIDTH="80%" BORDER="0" CELLPADDING="2" BGCOLOR="#000000" ALIGN="CENTER">
		  <tr VALIGN="MIDDLE" BGCOLOR="#999999" BORDERCOLOR="#000000" ALIGN="CENTER"> 
			<td> 
			  <center>
				<font FACE="Arial, Helvetica, sans-serif" SIZE="2" COLOR="#FFFFFF">e-box 
				ID: </font> 
			  </center>
			</td>
			<td> 
			  <center>
			  <!--input TYPE="text" NAME="eboxId" VALUE=<%=eboxId%> -->
			  <select name="eboxId" size="1">
                <%
                   for(int i=0;i < listOfEntity.size(); i++) {
                     if(  listOfEntity.get( i ).toString().equals( entity )) {
                %>
                        <option VALUE= <%= listOfEbox_id.get( i ).toString() %> selected><%= listOfEntity.get( i ).toString() %></option>
                <%
                      } //if(periods[i].equals(period))
                      else {
                %>  
				        <option VALUE=<%=  listOfEbox_id.get( i ).toString() %>><%= listOfEntity.get( i ).toString()%></option>
                <%
                      } //else
                   } //for(int i=0;i< listOfEntity.length;i++)
                %>
                </select>							
			  </center>			  			  			  
			</td>
		 	<td><font FACE="Arial, Helvetica, sans-serif" SIZE="2" COLOR="#FFFFFF"> Items:</font> </td>
			<td> 
			 <!--input TYPE="text" NAME= "environmentParameterMenu"  VALUE="<%=environmentParameter%>"-->
			  <center>
				<font FACE="Arial, Helvetica, sans-serif" SIZE="2" COLOR="#FFFFFF"> 
				<select NAME= "environmentParameterMenu" >
				     <option VALUE="tmp" >temperature</option>        		           		 
        			 <option VALUE="occ">occupancy</option>	 
					 <option VALUE="hmd">humidity</option>
					 <option VALUE="co">CO meter</option>
            	  	 <option VALUE="ALL" selected >ALL</option>
				</select>
				</font> 
			  </center >
			</td>
			<td> 
			  <center>
				<font FACE="Arial, Helvetica, sans-serif" SIZE="2" COLOR="#FFFFFF">TimeSpan:</font> 
			  </center>
			</td>
			<td> 
			  <center>
				<font FACE="Arial, Helvetica, sans-serif" SIZE="2" COLOR="#FFFFFF"> 
				 
				<select name="resolutionMenu" size="1">
                       <%
                            for(int i=0;i < periods.length;i++) {
                                if( periods[i].equals(resolution)) {
                        %>
						           <option selected><%=periods[i]%></option>
                        <%
                                 } //if(periods[i].equals(period))
                                 else {
                        %>  
							       <option><%=periods[i]%></option>
                        <%
                                 } //else
                            } //for(int i=0;i<period.length;i++)
                        %>
                   </select>									
				
				</font> 
			
			 <font FACE="Arial, Helvetica, sans-serif" SIZE="2" COLOR="#FFFFFF">Hours</font>
			    </center>
			</td>
			
		  </tr>
		</table >
	  </td>
	</tr>
	    <td ALIGN="CENTER"><font FACE="Arial, Helvetica, sans-serif" SIZE="2" COLOR="#FFFFFF"> 
       <input TYPE="submit" VALUE="Start" NAME="action">
	   </font></td>
  </table>
  </form>
  
</body>
</html>





