package Graph;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.mysql.jdbc.Connection;
import com.mysql.jdbc.ResultSetMetaData;
import com.mysql.jdbc.Statement;
/**
* Servlet implementation class Graph
*/
public class Graph extends HttpServlet {
private static final long serialVersionUID = 1L;
private static final String DatabaseConnection = null;
/**
* @see HttpServlet#HttpServlet()
*/
public Graph() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
OutputStreamWriter writer = new OutputStreamWriter(response.getOutputStream());
System.out.println("SensorGraph");
response.setContentType("text/html");
PrintWriter out = response.getWriter();
DatabaseConnection db_connection = new DatabaseConnection()._instance;
Connection connection = null;
try {
connection = db_connection.getConnection();
} catch (SQLException e) {
e.printStackTrace();
}
Statement statement;
ResultSet resultSet;
try {
statement = (Statement) connection.createStatement();
resultSet = statement.executeQuery("select * from Sensor_Entries order by EntryTime asc limit 5)");
String values = "";
while(resultSet.next()) {
String value = resultSet.getString("EntryValue");
values += (value) + ",";
}
System.out.println(values);
values = values.substring(0, values.length()-1);
String image = "http://chart.apis.google.com/chart?" +
"chxr=0,0,1000" +
"&chxs=0,676767,12.167,-0.5,l,676767" +
"&chxt=y" +
"&chs=300x225" +
"&cht=ls" +
"&chco=3D7930" +
"&chds=0,1000" +
"&chd=t:" + values+ \\ magic line - gives the values to the link
"&chg=14.3,-1,1,1" +
"&chls=2,4,0" +
"&chma=0,6%7C3" +
"&chm=B,C5D4B5BB,0,0,0" +
"&chtt=Sensor+Values" +
"&chts=F00D0D,14.5";
if(request.getParameter("img") != null)
out.write("<img src=\"" + image + "\" />");
statement = (Statement) connection.createStatement();
resultSet = statement.executeQuery("select * from Sensors join " +
"(select * from Sensor_Entries order by EntryTime desc) as Sensor_Entries on " +
"(Sensors.SensorID=Sensor_Entries.SensorID) where Sensors.SensorID='" + id + "' " +
"group by Sensors.SensorID;");
ResultSetMetaData resultSetMetaData = (ResultSetMetaData) resultSet.getMetaData();
if(resultSet.next()) {
writer.write("<table>");
for(int i = 1; i < resultSetMetaData.getColumnCount() + 1; i++) {
String value = "";
int type = resultSetMetaData.getColumnType(i);
if(type == Types.INTEGER || type == Types.TINYINT) {
value = "" + resultSet.getInt(i);
} else if(type == Types.VARCHAR) {
value = resultSet.getString(i);
} else if(type == Types.TIMESTAMP) {
value = resultSet.getTimestamp(i).toString();
} else if(type == Types.DOUBLE) {
value = "" + resultSet.getDouble(i);
}
writer.write("<tr>" +
"<td>" + resultSetMetaData.getColumnName(i) + "</td>" +
"<td>" + value + "</td>" +
"</tr>");
}
writer.write("</table></center>");
}
else
out.write(image);
out.flush();
out.close();
} catch (SQLException ignored) { ignored.printStackTrace(); }
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}