Friday, October 23, 2009

A basic .net Client for Reading and Writing Nimbits Data

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 C# developers. If you want to do the same thing in JAVA, check out this post.

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 C# .net 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



using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.IO;

namespace MyClient
{


class Program
{
private static String soaKey = "drGcVC5mJOLiCDEFAIKf288yCDighMKtq02mzy0oT0aSiP1vscLlaHABT4GInFmC";

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

private static void recordValue(double value, DateTime timestamp, String pointName)
{

DateTime epoch = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
long ms = (long)(DateTime.UtcNow - epoch).TotalMilliseconds;
long result = ms;

HttpWebRequest request = null;
Stream stream = null;
String postData = "key=" + soaKey + "&pointname=" + pointName + "&timestamp=" + result + "&value=" + value;
StreamReader streamReader = null;
HttpWebResponse response = null;
request = (HttpWebRequest)WebRequest.Create(serviceURL);
request.Method = "POST";
ASCIIEncoding encoding = new ASCIIEncoding();
byte[] data = encoding.GetBytes(postData);
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = data.Length;
Stream newStream = request.GetRequestStream();
// Send the data.
newStream.Write(data, 0, data.Length);
newStream.Close();
response = (HttpWebResponse)request.GetResponse();
stream = response.GetResponseStream();
streamReader = new StreamReader(stream);
string feedData = streamReader.ReadToEnd();
}


private static Double getCurrentValue(String pointName)
{


String postData = "key=" + soaKey + "&pointname=" + pointName;

HttpWebRequest request = null;
HttpWebResponse response = null;
Stream stream = null;
StreamReader streamReader = null;
request = (HttpWebRequest)WebRequest.Create(serviceURL + "?" + postData);
request.Method = "GET";
request.ContentLength = 0;
response = (HttpWebResponse)request.GetResponse();
stream = response.GetResponseStream();
streamReader = new StreamReader(stream);
string feedData = streamReader.ReadToEnd();
response.Close();
stream.Dispose();
streamReader.Dispose();
return Convert.ToDouble(feedData);


}
}
}

No comments:

Post a Comment