Friday, October 23, 2009

A basic Java Client for Reading and Writing Nimbits Data

Update 7/2010 - this code is out of date now because we're no longer supporting the SOA key. Check out my more recent postings that have the Java code you need to authenticate directly to Nimbits with your Google Account.



One of the most common tasks with Nimbits is to write a value to a data point, and request the most recent value. So much so, there is an entire web service dedicated to this task.

You can call these services from any platform or programing language. This example is for Java developers. If you want to do the same thing in C# check out this post. I would love to see someone post a variation in another language like python.

A developer can read and write data to nimbits by calling our REST based web services. In order to do this, you need to log into app.nimbits.com and create your data points in advance. Then get your SOA key. Learn more about SOA keys here in my blog.

With a SOA key, you can access your Nimbits data from other applications you develop without being logged into Nimbits with your Google Account.

Here is a java class you can add to any project to read and write data to and from your data points. It's just two functions, getCurrentValue and recordValue

the magic happens when you go to nimbits and set up calculations and alerts to trigger every time you write to a data point. Other users around the world who you share your data with will see your changes in real time.


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;
import java.util.Date;


public class NimbitsClient {

private final static String soaKey = "drGcVC5mJOLiCDEFAIKf288yCDighMKtq02mzy0oT0aSiP1vscLlaHABT4GInFmC";
private final static String serviceURL = "http://app.nimbits.com/nimbits/Service/value";

public static void recordValue(double value, Date timestamp, String pointName) throws IOException
{
String data = URLEncoder.encode("key", "UTF-8") + "=" + URLEncoder.encode(soaKey, "UTF-8");
data += "&" + URLEncoder.encode("pointname", "UTF-8") + "=" + URLEncoder.encode("TempF", "UTF-8");
data += "&" + URLEncoder.encode("value", "UTF-8") + "=" + value;
data += "&" + URLEncoder.encode("timestamp", "UTF-8") + "=" + timestamp.getTime();
// Send data
URL url = new URL(serviceURL);
URLConnection conn = url.openConnection();
conn.setDoOutput(true);
OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
wr.write(data);
wr.flush();
BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
wr.close();
rd.close();
}

public static Double getCurrentValue(String pointName) throws IOException
{
Double retVal;

String data = URLEncoder.encode("key", "UTF-8") + "=" + URLEncoder.encode(soaKey, "UTF-8");
data += "&" + URLEncoder.encode("pointname", "UTF-8") + "=" + URLEncoder.encode(pointName, "UTF-8");
URL url = new URL(serviceURL + "?" + data);
URLConnection conn = url.openConnection();
conn.setDoOutput(true);
BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String line;
String result ="";

while ((line = rd.readLine()) != null) {
result +=line;
}
rd.close();
retVal = Double.valueOf(result);

return retVal;

}


}

No comments:

Post a Comment