Progress
Implementation of one user registering and monitoring one sensor WITH displaying history data is done.
1. User interface
Based on the previous version that allows user to register and monitor any one of IUMSC sensors with displaying only most recent data, the current version can display history data and current data in real time.
2. Implementation
The previous version was improved in following aspects:
(1) Server side:
(a) Using ArrayList to store parcel data instead of File
- Read parcel (a parcel is in XML format) data into DOM;
- Parse the parcel, and get values of SensorName,TimeStamp and DoubleData;
- Encapsulate the parcel data to a user-defined Parcel class, and write it to an ArrayList.
Note: this implementation is synchronized.
(b) Using JSON to represent parcel data instead of XML
Definition of Parcel class:
public class Parcel {
public String sensorName;
public String timeStamp;
public String doubleData;
public int parcelID;
public Parcel(String sensorName, String timeStamp, String doubleData, int parcelID) {
this.sensorName = sensorName;
this.timeStamp = timeStamp;
this.doubleData = doubleData;
this.parcelID = parcelID;
}
public JSONObject toJSON() throws JSONException {
JSONObject JObj = new JSONObject();
JObj.put("sensorName", sensorName);
JObj.put("timeStamp", timeStamp);
JObj.put("doubleData", doubleData);
JObj.put("parcelID", parcelID);
return JObj;
}
}
getparcels.jsp is responsible for:
- Handle HTTP request from the client;
- Each parcel data(element in the ArrayList) is converted to a JSON object, and added to a JSONArray which is used to hold a JavaScript representation all the parcels we’ll return;
- Writes representation to client.
Note: Choosing JSON as means of transferring data in our application[4]
Disadvantage of XML:
- We must ensure the XML is sent with the correct header if it is sent from the server. But if the XML is being generated on the fly by a server-side programming language, it is easy to miss this requirement, then responseXML property would be empty;
- It often takes quite a few lines of JavaScript just to generate a small chunk of markup even if the DOM is eminently suited to parsing XML once it reaches the browser.
JSON is designed to store data, and it is lightweight alternative to XML. Anything that can be stored in XML can also be stored in JSON. Both are text-based representations of data, but while XML requires opening and closing tags, JSON just uses colons, commas, and curly braces.
JSON isn’t a data format that needs to be interpreted by JavaScript: JSON is JavaScript.
(2) Client side
The client side is responsible for querying the server for parcels and displaying them on a page. We define a ParcelManager class object-oriented JavaScript with Prototype to handle the fetching, caching and displaying of the parcels.
new Ajax.PeriodicalUpdater({failure:'error'}, "fetchparcels.jsp?start=" + start + "&pageSize=" + pageSize,
{
method: "get",
frequency: monitorRate,
decay: 2,
onSuccess: function(response) {
cacheParcelData(response);
drawParcelDIV(start, pageSize, div, false);
},
on404: function(response) {
alert('Server Status: 404: ' + response.statusText);
},
onFailure: function(response) {
alert('Server Status: ' + response.status + ' - ' + response.statusText);
}
}
);
The core part of the client side is Prototype Ajax.PeriodicalUpdater class which schedules the automatic updates without having to code any scheduling logic or writing timeout handlers.[3]
(a) When the client wants to monitor the parcel data, an Ajax.PeriodicalUpdater object is created, and getparcels.jsp is employed to fetch data on the server side;
(b) onSuccess event handler takes resposiblility to cache the parcel data and display them.
- cacheParcelData()function is invoked only after we’ve fetched parcels from the server. It gets the JSON text we’ve received from the server and creates a JavaScript array out of it;
- drawParcelDIV()function takes the cached parcel data and creates a list in the that we specify.
Discussion & Future work
1. One user can register and monitor multiple sensors;
The hard part is how to tell which data (parcel) belongs to which registration request when the user is allowed to register the same sensor with different data interval.
2. The problem with the interface of unregistering a sensor
Reference
[1] JSP : the complete reference. Hanna, Phil.
[2] Core servlets and JavaServer pages. Hall, Marty; Brown, Larry
[3]
[4] Bulletproof
No comments:
Post a Comment