Wednesday, February 18, 2009

A taste of Java from Hackystat

 This week we finally got to get a taste of Hackystat, and it taste a lot like Java.  The class was given the task to create two simple functions to retrieve data from the hackystat server.

 

For our first task, the java function was to retrieve sensor data for everyday of November 2008.  At first, I presumed the task to be a fairly simple, with the help of our professor’s web cast video tutorials. Later when coding the function, I hit a brick wall; having trouble getting to the month of November, for data retrieval.  I later then broke down that wall by just basically inputting the XMLGregorianCalendar date statically for a start date. 

  

Just when I thought when I was in the clear, I fell in a ditch; I couldn’t get the correct data I wanted from the server.  I then proceeded to read the Javadocs for the Hackystat program.  Spending hours and hours of reading through the documentations, I finally burned out my eyes.  After taking a break from reading and staring at code, the answer just occurred to me.  Having input the start date statically, I just simply ran a loop with the number of days in November and statically inputting those days in the start date.  Finally all the data showed for each day of November, and also for the second part of that task; the total for the month of February 2009.

  

The second task was to retrieve sensor data from the server, just like the first task; but the catch was to have a user input the desired month and year.  I wasn’t able to accomplish this task, due to having been stuck for so long on the first one. 


Wow, the hackystat program is bigger than I thought.  Unfortunately I wasn’t able to finish the second task, as I mentioned earlier.  I spent much of my time on reading the program documentations to resolve the first task.  I was looking in all the right places, but not thinking out of the box.  I guess that can happen when you stare and think of the same problem for to long.  A professor once told me “even Einstein needed a break”.  The good thing from looking over so much of the documents was learning more of the internal functionality for the program.  This teaches me a lesson though, not to slack off or take a simple task so lightly, and to take a break when needed. 


My code for task one:




import java.math.BigInteger;
import javax.xml.datatype.XMLGregorianCalendar;
import org.hackystat.sensorbase.client.SensorBaseClient;
import org.hackystat.sensorbase.resource.projects.jaxb.ProjectSummary;
import org.hackystat.sensorbase.resource.sensordata.jaxb.SensorDataIndex;
import org.hackystat.sensorshell.SensorShellProperties;
import org.hackystat.utilities.tstamp.Tstamp;

/**
* This class will retrieve data instances from the hackystat server for everyday in the month of
* November 2008. Also will retrieve the total of data instances for Feburary 2009.
*
* @author robin
*/
public class DailySDI {

public static void main (String[] args) throws Exception{

String host = "http://dasha.ics.hawaii.edu:9876/sensorbase";

// get user info
SensorShellProperties properties = new SensorShellProperties();
String user = properties.getSensorBaseUser();
String password = properties.getSensorBasePassword();

SensorBaseClient client = new SensorBaseClient(host, user, password);
String project = "Default";

System.out.println("===Data for November 2008===");

for (int i = 1; i < 31; i++) {

// Set start and end time stamp for november
String startTime = "2008-11-0" + i +"T00:00:00.000-10:00";
String endTime = "2008-11-0" + i +"T24:00:00.000-10:00";
XMLGregorianCalendar start = Tstamp.makeTimestamp(startTime);
XMLGregorianCalendar end = Tstamp.makeTimestamp(endTime);

// Retrieve data instances from hacky stat for specific days
ProjectSummary summaryNov = client.getProjectSummary(user, project, start, end);
BigInteger totalNov = summaryNov.getSensorDataSummaries().getNumInstances();
String dateAndTotal = String.format("%s/%s: %s", summaryNov.getStartTime().getMonth(),
summaryNov.getStartTime().getDay(), totalNov);
System.out.println(dateAndTotal);
}

// set start and end for feburary
XMLGregorianCalendar febStart = Tstamp.makeTimestamp("2009-02-01T00:00:00.000-10:00");
XMLGregorianCalendar febEnd = Tstamp.makeTimestamp();

// get sensor data for feburary
SensorDataIndex febData = client.getProjectSensorData(user, project, febStart, febEnd);
int febDataSize = febData.getSensorDataRef().size();
System.out.println("Feburary 2009 Data Sensors: " + febDataSize);
}
}


No comments: