Friday, November 4, 2011

Shopping For Electricity at WattDepot

     With the previous introduction of energy comes the idea of utilizing the data centered around it. Why do such a thing you may ask? Well interestingly, energy data could be used to research different areas while gathering data on the amount of energy consumed or produce. With this information, there could be a way to use it such that in the future when a hypothetical smart grid is developed, energy could be dispersed based on the need of a certain area. Could this be possible? Most certainly, but maybe not today.
     Today on the other hand, I introduce to you WattDepot. This web service collects electricity data from meters and stores it in a data base. This data can then be retrieved and analyzed then possibly become the basis to developing applications for a future smart grid.
     Like previous applications introduced to you in my blog, before we decide to use an application, we must learn how to use it ourselves. See the pattern? Here we go… Code Katas. The following code katas were designed to help one become proficient in utilizing the WattDepot API.

Kata 1: SourceListing
     Implement a class called SourceListing, whose main() method accepts one argument (the URL of the WattDepot server) and which writes out the URL argument and a list of all sources defined on that server and their descriptions, sorted in alphabetical order by source name.  Use the System.out.format method to provide a nicely formatted list. 
  • Using the wattdepot-simpleapp distribution as a basis, I built this kata off of it. I took out some unnecessary code such as retrieving sensor data and just left it pruning out the sources and descriptions.
  • Time to completion: 10 Minutes.


Kata 2: SourceLatency
     Implement a class called SourceLatency, whose main() method accepts one argument (the URL of the WattDepot server) and which writes out the URL argument and a list of all sources defined on that server and the number of seconds since data was received for that source, sorted in ascending order by this latency value.  If no data has every been received for that source, indicate that.  Use the System.out.format method to provide a nicely formatted list.
  • This kata was a bit tricky. I could calculate the latencies and print them along with the list, but this kata states that the output must be sorted by the latency values. My first though was to create an array and then sort it, but it might take too much time and wouldn't be efficient. Ultimately, the best solution was to create a class that implements comparable. I called this call WattDepotUtility and created it such that I could benefit from using it for the next katas, hopefully. I also note there is a delay in getting the latencies before showing the list. A user might think the program might be stalled, but it's actually doing the calculations.
  • Time to completion: 2 hours (Estimate. Most of time was spent on researching how to create my WattDepotUtility class).


Kata 3: SourceHierarchy
     Implement a class called SourceHierarchy, whose main() method accepts one argument (the URL of the WattDepot server) and which writes out the URL argument and a hierarchical list of all sources defined on that server.  The hierarchy represents the source and subsource relationship between sources.
  • Building off from the previous katas, I discovered that there is a method in the API to retrieve sub sources of a source. The problem is that this method returned the path of the sub source instead of only the name. What I did to solve this problem was to split the string at the last index of the '/' character to get the name. I also created a method that would generate a tab character (2 spaces) when the hierarchy needs to be branched out.
  • Time to completion: 45 minutes.


Kata 4: EnergyYesterday
     Implement a class called EnergyYesterday, whose main() method accepts one argument (the URL of the WattDepot server) and which writes out the URL argument and a list of all sources defined on that server and the amount of energy in watt-hours consumed by that source during the previous day, sorted in ascending order by watt-hours of consumption.  If no energy has every been consumed by that source, indicate zero.
  • Most of my time spent building this kata was learning how to retrieve the energy data and getting yesterday date. I know I need to use the getEnergyConsumed method, but I always kept getting errors. What I discovered with some testing was that if the XMLGregorianCalendar being passed it had a time with seconds and milliseconds that were not zero, it would produce a resource not found error, so these time always had to be set to zero. First problem solved. For getting yesterdays date, I went through many methods such as Calendar and SimpleDateFormat. After browsing through the API, I could my answer in using the Tstamp class.
  • Time to completion: 3 hours.


Kata 5: HighestRecordedPowerYesterday
Shopping For Electricity at WattDepotImplement a class called HighestRecordedPowerYesterday, whose main() method accepts one argument (the URL of the WattDepot server) and which writes out the URL argument and a list of all sources defined on that server and the highest recorded power associated with that source during the previous day, sorted in ascending order by watts.  Also indicate the time when that power value was observed. If no power data is associated with that source, indicate that.  Use the System.out.format method to provide a nicely formatted list.
  • In this kata, it's very similar to the previous one. I just added in a method that would get all the power values from the previous date and get the highest value. My utility class only holds a string and data (double), but the output also wants the time. I could have added another field to my WattDepotUtility class, but instead, I concatenate the time with the name and the deconstruct it before printing the result. Same like the second kata, there is a delay in showing the list, but is much, much longer to the point most users would think it froze. So I added in print statements that would tell users when it is getting data from a certain source, in this test, there where 64 sources, so there were 64 messages printed to ensure the program is doing something in the background.
  • Time to completion: 35 minutes.


Kata 6: MondayAverageEnergy
     Implement a class called MondayAverageEnergy, whose main() method accepts one argument (the URL of the WattDepot server) and which writes out the URL argument and a list of all sources defined on that server and the average energy consumed by that source during the previous two Mondays, sorted in ascending order by watt-hours.  Use the System.out.format method to provide a nicely formatted list.
  • For the final kata, I think the trickiest part is determining when the previous two Mondays were. Playing with the Tstamp class and writing a switch statement product the correct result.
  • Time to completion: 30 minutes.


     Overall with my experience using WattDepot I think it's very simple, though what you do with the data however, can get really complex. I completed all the katas listed above and most where not easy to complete as described. WattDepot is straight forward on getting energy data with many methods that probably sound what they will do such as getEnergyConsumed (getting the data for the amount of energy consumed). The katas can teach you how to sort data in a way that is easy to compare data from one source to another. In the end, this could be applied to any type of data and is not limited to energy data. Data manipulation may take some work to produce the correct output, but what really maters is how one will use this information.

Links:

2 comments:

  1. Why would you need a switch statement for the last kata?

    ReplyDelete
  2. I haven't finished this the assignment completely yet when posting this up. I made some updates to some katas. The last kata does not need the switch statement. Probably wasn't thinking when writing the code. All in the learning process making your code better.

    ReplyDelete