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

Committer:
mimil
Date:
Wed Jul 11 13:55:07 2012 +0000
Revision:
1:0249701444ee
Parent:
0:ed7287a3edbf
new version using a web server to receive messages from the sen.se platform

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mimil 0:ed7287a3edbf 1 /*
mimil 0:ed7287a3edbf 2 Copyright [2011] [mimilowns@gmail.com]
mimil 0:ed7287a3edbf 3
mimil 0:ed7287a3edbf 4 Licensed under the Apache License, Version 2.0 (the "License");
mimil 0:ed7287a3edbf 5 you may not use this file except in compliance with the License.
mimil 0:ed7287a3edbf 6 You may obtain a copy of the License at
mimil 0:ed7287a3edbf 7
mimil 0:ed7287a3edbf 8 http://www.apache.org/licenses/LICENSE-2.0
mimil 0:ed7287a3edbf 9
mimil 0:ed7287a3edbf 10 Unless required by applicable law or agreed to in writing, software
mimil 0:ed7287a3edbf 11 distributed under the License is distributed on an "AS IS" BASIS,
mimil 0:ed7287a3edbf 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
mimil 0:ed7287a3edbf 13 See the License for the specific language governing permissions and
mimil 0:ed7287a3edbf 14 limitations under the License.
mimil 0:ed7287a3edbf 15 */
mimil 0:ed7287a3edbf 16 #include "SenseClient.h"
mimil 0:ed7287a3edbf 17 #include "HTTPClient.h"
mimil 0:ed7287a3edbf 18
mimil 0:ed7287a3edbf 19
mimil 1:0249701444ee 20
mimil 0:ed7287a3edbf 21 SenseClient::SenseClient(const string& senseKey, const string& httpproxy) : _jsonContent("application/json"), _jsonRespContent("application/json") {
mimil 1:0249701444ee 22
mimil 1:0249701444ee 23
mimil 0:ed7287a3edbf 24 _key = senseKey;
mimil 1:0249701444ee 25 _proxy = httpproxy;
mimil 1:0249701444ee 26
mimil 0:ed7287a3edbf 27 }
mimil 0:ed7287a3edbf 28
mimil 0:ed7287a3edbf 29 SenseClient::~SenseClient() {
mimil 0:ed7287a3edbf 30 }
mimil 0:ed7287a3edbf 31
mimil 0:ed7287a3edbf 32 void SenseClient::PostEvent(const string& feedID, const string& value) {
mimil 0:ed7287a3edbf 33 _jsonContent.set("{\"feed_id\":"+feedID+", \"value\":"+value+"}");
mimil 1:0249701444ee 34
mimil 0:ed7287a3edbf 35 string uri = "http://api.sen.se/event_api/events/";
mimil 0:ed7287a3edbf 36 HTTPClient _client;
mimil 0:ed7287a3edbf 37 _client.setRequestHeader("sense_key", _key);
mimil 1:0249701444ee 38 if (!_proxy.empty()) {
mimil 0:ed7287a3edbf 39 _client.setProxy(_proxy.c_str());
mimil 0:ed7287a3edbf 40 }
mimil 1:0249701444ee 41
mimil 0:ed7287a3edbf 42 _result = _client.post(uri.c_str(), _jsonContent, &_jsonRespContent);
mimil 0:ed7287a3edbf 43 _response = _client.getHTTPResponseCode();
mimil 0:ed7287a3edbf 44 }
mimil 1:0249701444ee 45
mimil 0:ed7287a3edbf 46 void SenseClient::GetLastFeedEvent(const string& feedID) {
mimil 0:ed7287a3edbf 47 string uri = "http://api.sen.se/event_api/feeds/"+feedID+"/last_event/";
mimil 0:ed7287a3edbf 48
mimil 0:ed7287a3edbf 49 HTTPClient _client;
mimil 0:ed7287a3edbf 50 _client.setRequestHeader("sense_key", _key);
mimil 1:0249701444ee 51 if (!_proxy.empty()) {
mimil 0:ed7287a3edbf 52 _client.setProxy(_proxy.c_str());
mimil 0:ed7287a3edbf 53 }
mimil 1:0249701444ee 54
mimil 0:ed7287a3edbf 55 _result = _client.get(uri.c_str(), &_jsonRespContent);
mimil 0:ed7287a3edbf 56 _response = _client.getHTTPResponseCode();
mimil 0:ed7287a3edbf 57 }
mimil 0:ed7287a3edbf 58
mimil 0:ed7287a3edbf 59 void SenseClient::GetDeviceLastEvent(const string& deviceID) {
mimil 0:ed7287a3edbf 60 string uri = "http://api.sen.se/devices/"+deviceID+"/last_event/";
mimil 1:0249701444ee 61
mimil 0:ed7287a3edbf 62 HTTPClient _client;
mimil 0:ed7287a3edbf 63 _client.setRequestHeader("sense_key", _key);
mimil 1:0249701444ee 64 if (!_proxy.empty()) {
mimil 0:ed7287a3edbf 65 _client.setProxy(_proxy.c_str());
mimil 0:ed7287a3edbf 66 }
mimil 1:0249701444ee 67
mimil 0:ed7287a3edbf 68 _result = _client.get(uri.c_str(), &_jsonRespContent);
mimil 0:ed7287a3edbf 69 _response = _client.getHTTPResponseCode();
mimil 0:ed7287a3edbf 70 }
mimil 0:ed7287a3edbf 71
mimil 1:0249701444ee 72 /**
mimil 1:0249701444ee 73 * Returns the given parameter value identified by its name from the given http query string.
mimil 1:0249701444ee 74 *
mimil 1:0249701444ee 75 * @return The parameter value
mimil 1:0249701444ee 76 * @param queryString The http query string
mimil 1:0249701444ee 77 * @param name The parameter name
mimil 1:0249701444ee 78 */
mimil 1:0249701444ee 79 char* SenseClient::getParam(char *queryString, const char *name) {
mimil 1:0249701444ee 80 char *pos1 = strstr(queryString, name);
mimil 1:0249701444ee 81
mimil 1:0249701444ee 82 if (pos1) {
mimil 1:0249701444ee 83 pos1 += strlen(name);
mimil 1:0249701444ee 84
mimil 1:0249701444ee 85 if (*pos1 == '=') { // Make sure there is an '=' where we expect it
mimil 1:0249701444ee 86 pos1++;
mimil 1:0249701444ee 87
mimil 1:0249701444ee 88 // compute the size of the buffer
mimil 1:0249701444ee 89 char *pos2 = pos1;
mimil 1:0249701444ee 90 while (*pos2 && *pos2 != '&') {
mimil 1:0249701444ee 91 pos2++;
mimil 1:0249701444ee 92 }
mimil 1:0249701444ee 93 char* value = (char *) malloc(sizeof(char) * (pos2-pos1+1));
mimil 1:0249701444ee 94 char* ret = value;
mimil 1:0249701444ee 95
mimil 1:0249701444ee 96 // store the string in the buffer
mimil 1:0249701444ee 97
mimil 1:0249701444ee 98 while (*pos1 && *pos1 != '&') {
mimil 1:0249701444ee 99 if (*pos1 == '%') { // Convert it to a single ASCII character and store at our Value
mimil 1:0249701444ee 100 *value++ = (char)ToHex(pos1[1]) * 16 + ToHex(pos1[2]);
mimil 1:0249701444ee 101 pos1 += 3;
mimil 1:0249701444ee 102 } else if ( *pos1=='+' ) { // If it's a '+', store a space at our Valueination
mimil 1:0249701444ee 103 *value++ = ' ';
mimil 1:0249701444ee 104 pos1++;
mimil 1:0249701444ee 105 } else {
mimil 1:0249701444ee 106 *value++ = *pos1++; // Otherwise, just store the character at our Value
mimil 1:0249701444ee 107 }
mimil 1:0249701444ee 108 }
mimil 1:0249701444ee 109
mimil 1:0249701444ee 110 *value++ = '\0';
mimil 1:0249701444ee 111 return ret;
mimil 1:0249701444ee 112 }
mimil 1:0249701444ee 113
mimil 1:0249701444ee 114 }
mimil 1:0249701444ee 115
mimil 1:0249701444ee 116 return NULL;
mimil 1:0249701444ee 117 }
mimil 1:0249701444ee 118
mimil 1:0249701444ee 119 /**
mimil 1:0249701444ee 120 * Starts an http server to receive messages from sen.se platform. A function void parseEvent(char* content) MUST be defined in your main.
mimil 1:0249701444ee 121 *
mimil 1:0249701444ee 122 * @param port The port on which to start the http server
mimil 1:0249701444ee 123 */
mimil 1:0249701444ee 124 void SenseClient::startHttpServer(int port) {
mimil 1:0249701444ee 125
mimil 1:0249701444ee 126 _svr.addHandler<SenseHandler>("/");
mimil 1:0249701444ee 127 _svr.bind(port);
mimil 1:0249701444ee 128
mimil 1:0249701444ee 129 printf("Listening on port %d...\r\n", port);
mimil 1:0249701444ee 130
mimil 1:0249701444ee 131 }
mimil 1:0249701444ee 132
mimil 0:ed7287a3edbf 133 // http result and response
mimil 0:ed7287a3edbf 134 HTTPResult SenseClient::Result() {
mimil 0:ed7287a3edbf 135 return _result;
mimil 0:ed7287a3edbf 136 }
mimil 0:ed7287a3edbf 137 int SenseClient::Response() {
mimil 0:ed7287a3edbf 138 return _response;
mimil 0:ed7287a3edbf 139 }
mimil 0:ed7287a3edbf 140
mimil 0:ed7287a3edbf 141 HTTPText SenseClient::ResponseContent() {
mimil 0:ed7287a3edbf 142 return _jsonRespContent;
mimil 0:ed7287a3edbf 143 }