mimil owns / SenseClient

Description: SenseClient is an API to interact with Sen.se platform. Sen.se is the place where Humans, Machines, Objects, Environments, Information, Physical and Virtual spaces mix up, talk, intertwine, interact, enrich and empower each other.

Embed: (wiki syntax)

« Back to documentation index

SenseClient Class Reference

SenseClient Class Reference

SenseClient is an API to interact with Sen.se platform. More...

#include <SenseClient.h>

Public Member Functions

 SenseClient (const string &senseKey, const string &httpproxy)
 Creates a client to interacte with sen.se platform.
void PostEvent (const string &feedID, const string &value)
 Posts an event from device to sen.se platform to the given feed.
void GetLastFeedEvent (const string &feedID)
 Retrieves the latest event from the given feed.
void GetDeviceLastEvent (const string &deviceID)
 Retrieves the latest event from the given device.
HTTPResult Result ()
 Returns the HTTPClient result of the last HTTP call.
int Response ()
 Returns the HTTP response code of the last HTTP call.
HTTPText ResponseContent ()
 Returns the content payload of the last HTTP call.
char * getParam (char *queryString, const char *name)
 Returns the given parameter value identified by its name from the given http query string.
void startHttpServer (int port)
 Starts an http server to receive messages from sen.se platform.

Detailed Description

SenseClient is an API to interact with Sen.se platform.

 #include "SenseClient.h"
 #include "picojson.h" //eventualy

 SenseClient sense("my sen.se key", "http://proxy:port"); // or "" for no proxy
 string feedID = "a feed id";
 string deviceID = "a device id";

 int main() {
   // init network etc.
 
   sense.PostEvent(feedID, tosend);
   if (sense.Result() == HTTP_OK) {
        const char* response = sense.ResponseContent().gets();
        printf("http response content: %s\r\n", response);
        // ...
    } else {
        printf("Sen.se result  / response code : %d / %d\r\n", sense.Result(), sense.Response());
    }

Definition at line 54 of file SenseClient.h.


Constructor & Destructor Documentation

SenseClient ( const string &  senseKey,
const string &  httpproxy 
)

Creates a client to interacte with sen.se platform.

Parameters:
senseKeyyour credential on sen.se platform.
httpproxyusing format http://<ip addr>="">:<port> or an empty string if using a direct connection.

Definition at line 21 of file SenseClient.cpp.


Member Function Documentation

void GetDeviceLastEvent ( const string &  deviceID )

Retrieves the latest event from the given device.

The response of the call will be available on SenseClient::ResponseContent method and will be a json like this:

 {
   "publish_id": "5234C4C389AC2",
   "value": "{\"aa\":\"tt\",\"bb\":1.660000}",
   "timetag": "2011-09-06T12:35:15+00:00",
   "feed_id": 4293,
   "id": 29768312,
   "unit": ""
 }
Parameters:
deviceIDthe device ID you want to query.

Definition at line 59 of file SenseClient.cpp.

void GetLastFeedEvent ( const string &  feedID )

Retrieves the latest event from the given feed.

The response of the call will be available on SenseClient::ResponseContent method and will be a json like this:

 {
   "publish_id": "5234C4C389AC2",
   "value": "{\"aa\":\"tt\",\"bb\":1.660000}",
   "timetag": "2011-09-06T12:35:15+00:00",
   "feed_id": 4293,
   "id": 29768312,
   "unit": ""
 }
Parameters:
feedIDthe feed ID you want to query.

Definition at line 46 of file SenseClient.cpp.

char * getParam ( char *  queryString,
const char *  name 
)

Returns the given parameter value identified by its name from the given http query string.

Returns:
The parameter value
Parameters:
queryStringThe http query string
nameThe parameter name

Definition at line 79 of file SenseClient.cpp.

void PostEvent ( const string &  feedID,
const string &  value 
)

Posts an event from device to sen.se platform to the given feed.

The value must be a valid json string which means that it must be surrounded by double-quotes and other quotes must be escaped etc.

Some examples of value:

 string value1 = "\"a text\"";
 string value2 = "\"10\"";
 string value3 = "\"{\\"aa\\":\\"tt\\",\\"bb\\":1.660000}\"";

You can also use picojson library (http://mbed.org/users/mimil/libraries/picojson/lwryzp) as the following:

   picojson::object v;
   v["aa"] = picojson::value(string("tt"));
   v["bb"] = picojson::value(double(1.66));
   string str = picojson::value(v).serialize();
   // if you want to send your data as a json value,
   // you have to know that it is in fact a string inside another json
   // so it needs to be escaped as any json string
   string value4 = picojson::value(str).serialize();

The effective json sent to sen.se platform will be:

 {
   "feed_id":4293,
   "value":"{\"aa\":\"tt\",\"bb\":1.660000}"
 }

The response of the call will be available on SenseClient::ResponseContent method and will be a json like this:

 [
  {
    "publish_id": "5234C4C389AC2",
    "value": "{\"aa\":\"tt\",\"bb\":1.660000}",
    "timetag": "2011-09-06T12:35:15.559778+00:00",
    "feed_id": 4293,
    "id": 29768312,
    "unit": ""
  }
 ]
Parameters:
feedIDthe feed ID on which you want to post.
valuea valid json string.

Definition at line 32 of file SenseClient.cpp.

int Response (  )

Returns the HTTP response code of the last HTTP call.

Returns:
The last HTTP code.

Definition at line 137 of file SenseClient.cpp.

HTTPText ResponseContent (  )

Returns the content payload of the last HTTP call.

You can use picojson library to parse the content like this:

    picojson::value v2;
    const char* response = sense.ResponseContent().gets();
    printf("http response content: %s\r\n", response);
    string err = picojson::parse(v2, response, response + strlen(response));
    printf("--> res error: %s\r\n", err.c_str());
    printf("--> res isarray: %d\r\n", v2.is<picojson::array>());
    if(v2.is<picojson::array>()) {
        // this is a response from PostEvent
        printf("--> res date: %s\r\n", v2.get(0).get("timetag").get<string>());
    }
Returns:
The content payload.

Definition at line 141 of file SenseClient.cpp.

HTTPResult Result (  )

Returns the HTTPClient result of the last HTTP call.

Returns:
The last call result.

Definition at line 134 of file SenseClient.cpp.

void startHttpServer ( int  port )

Starts an http server to receive messages from sen.se platform.

A function void parseEvent(char* content) MUST be defined in your main.

Parameters:
portThe port on which to start the http server

Definition at line 124 of file SenseClient.cpp.