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.

Dependencies:   NetServicesProxy

Dependents:   SenseClientSample

Revision:
1:0249701444ee
Parent:
0:ed7287a3edbf
--- a/SenseClient.cpp	Tue Sep 06 13:31:10 2011 +0000
+++ b/SenseClient.cpp	Wed Jul 11 13:55:07 2012 +0000
@@ -17,9 +17,13 @@
 #include "HTTPClient.h"
 
 
+
 SenseClient::SenseClient(const string& senseKey, const string& httpproxy) :  _jsonContent("application/json"), _jsonRespContent("application/json") {
+
+
     _key = senseKey;
-    _proxy = httpproxy; 
+    _proxy = httpproxy;
+
 }
 
 SenseClient::~SenseClient() {
@@ -27,44 +31,105 @@
 
 void SenseClient::PostEvent(const string& feedID, const string& value) {
     _jsonContent.set("{\"feed_id\":"+feedID+", \"value\":"+value+"}");
-    
+
     string uri = "http://api.sen.se/event_api/events/";
     HTTPClient _client;
     _client.setRequestHeader("sense_key", _key);
-    if(!_proxy.empty()) {
+    if (!_proxy.empty()) {
         _client.setProxy(_proxy.c_str());
     }
-    
+
     _result = _client.post(uri.c_str(), _jsonContent, &_jsonRespContent);
     _response = _client.getHTTPResponseCode();
 }
-  
+
 void SenseClient::GetLastFeedEvent(const string& feedID) {
     string uri = "http://api.sen.se/event_api/feeds/"+feedID+"/last_event/";
 
     HTTPClient _client;
     _client.setRequestHeader("sense_key", _key);
-    if(!_proxy.empty()) {
+    if (!_proxy.empty()) {
         _client.setProxy(_proxy.c_str());
     }
-    
+
     _result = _client.get(uri.c_str(), &_jsonRespContent);
     _response = _client.getHTTPResponseCode();
 }
 
 void SenseClient::GetDeviceLastEvent(const string& deviceID) {
     string uri = "http://api.sen.se/devices/"+deviceID+"/last_event/";
-    
+
     HTTPClient _client;
     _client.setRequestHeader("sense_key", _key);
-    if(!_proxy.empty()) {
+    if (!_proxy.empty()) {
         _client.setProxy(_proxy.c_str());
     }
-    
+
     _result = _client.get(uri.c_str(), &_jsonRespContent);
     _response = _client.getHTTPResponseCode();
 }
 
+/**
+* Returns the given parameter value identified by its name from the given http query string.
+*
+* @return The parameter value 
+* @param queryString The http query string
+* @param name The parameter name
+*/
+char* SenseClient::getParam(char *queryString, const char *name) {
+    char *pos1 = strstr(queryString, name);
+
+    if (pos1) {
+        pos1 += strlen(name);
+
+        if (*pos1 == '=') { // Make sure there is an '=' where we expect it
+            pos1++;
+
+            // compute the size of the buffer
+            char *pos2 = pos1;
+            while (*pos2 && *pos2 != '&') {
+                pos2++;
+            }
+            char* value = (char *) malloc(sizeof(char) * (pos2-pos1+1));
+            char* ret = value;
+
+            // store the string in the buffer
+
+            while (*pos1 && *pos1 != '&') {
+                if (*pos1 == '%') { // Convert it to a single ASCII character and store at our Value
+                    *value++ = (char)ToHex(pos1[1]) * 16 + ToHex(pos1[2]);
+                    pos1 += 3;
+                } else if ( *pos1=='+' ) { // If it's a '+', store a space at our Valueination
+                    *value++ = ' ';
+                    pos1++;
+                } else {
+                    *value++ = *pos1++; // Otherwise, just store the character at our Value
+                }
+            }
+
+            *value++ = '\0';
+            return ret;
+        }
+
+    }
+
+    return NULL;
+}
+
+/**
+* Starts an http server to receive messages from sen.se platform. A function void parseEvent(char* content) MUST be defined in your main.
+*
+* @param port The port on which to start the http server
+*/
+void SenseClient::startHttpServer(int port) {
+
+    _svr.addHandler<SenseHandler>("/");
+    _svr.bind(port);
+
+    printf("Listening on port %d...\r\n", port);
+
+}
+
 // http result and response
 HTTPResult SenseClient::Result() {
     return _result;