Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: NetServicesProxy
Revision 1:0249701444ee, committed 2012-07-11
- Comitter:
- mimil
- Date:
- Wed Jul 11 13:55:07 2012 +0000
- Parent:
- 0:ed7287a3edbf
- Commit message:
- new version using a web server to receive messages from the sen.se platform
Changed in this revision
diff -r ed7287a3edbf -r 0249701444ee SenseClient.cpp
--- 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;
diff -r ed7287a3edbf -r 0249701444ee SenseClient.h
--- a/SenseClient.h Tue Sep 06 13:31:10 2011 +0000
+++ b/SenseClient.h Wed Jul 11 13:55:07 2012 +0000
@@ -18,6 +18,14 @@
#include "mbed.h"
#include "HTTPClient.h"
+#include "HTTPServer.h"
+#include "SenseHandler.h"
+
+
+
+
+// Helper macro to convert two-character hex strings to character value
+#define ToHex(Y) (Y>='0'&&Y<='9'?Y-'0':Y-'A'+10)
/**
* SenseClient is an API to interact with Sen.se platform.
@@ -182,6 +190,24 @@
* @returns The content payload.
*/
HTTPText SenseClient::ResponseContent();
+
+ /**
+ * 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* getParam(char *queryString, const char *name);
+
+ /**
+ * 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 startHttpServer(int port);
+
+
private:
HTTPText _jsonContent;
@@ -191,6 +217,9 @@
HTTPResult _result;
int _response;
+
+ HTTPServer _svr;
+
};
diff -r ed7287a3edbf -r 0249701444ee SenseHandler.cpp
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/SenseHandler.cpp Wed Jul 11 13:55:07 2012 +0000
@@ -0,0 +1,68 @@
+#include "SenseHandler.h"
+
+
+SenseHandler::SenseHandler(const char* rootPath, const char* path, TCPSocket* pTCPSocket) : HTTPRequestHandler(rootPath, path, pTCPSocket) {}
+
+SenseHandler::~SenseHandler() {}
+
+
+
+void SenseHandler::doGet() {
+ printf("doGet called\r\n");
+ const char* resp = "Hello world !";
+ setContentLen( strlen(resp) );
+ respHeaders()["Connection"] = "close";
+ writeData(resp, strlen(resp));
+}
+
+void SenseHandler::doPost() {
+ printf("doPost called\r\n");
+ int len = dataLen();
+ //string path = path();
+ //string rootPath = rootPath();
+ //readData(char* buf, int len);
+ //respHeaders()["Connection"] = "close";
+ //setContentLen(100);
+ //setErrCode(404);
+ onReadable();
+}
+
+void SenseHandler::doHead() {
+
+}
+
+
+void SenseHandler::onReadable() { //Data has been read
+ printf("onReadable called\r\n");
+ int _datalen = dataLen(); //read POST data length
+
+ //allocate size of readable data
+ char* _send_data = (char *) malloc(sizeof(char) * _datalen + 1);
+
+ //read POST data
+ readData(_send_data, _datalen);
+
+ //char myName[100] = "";
+ //getParam("value", myName);
+ //process POST data here, you can call any subroutine from here
+ //printf("value: %s\r\n", myName);
+
+ //write any response header
+ respHeaders()["Connection"] = "close";
+
+ //http_response contains any html code as my reply
+ //writeData(http_response, strlen(http_response));
+ _send_data[_datalen] = '\0';
+ parseEvent(_send_data);
+
+}
+
+void SenseHandler::onWriteable() { //Data has been written & buf is free
+ close(); //Data written, we can close the connection
+}
+
+void SenseHandler::onClose() { //Connection is closing
+ //Nothing to do
+}
+
+
diff -r ed7287a3edbf -r 0249701444ee SenseHandler.h
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/SenseHandler.h Wed Jul 11 13:55:07 2012 +0000
@@ -0,0 +1,30 @@
+#ifndef SENSEHANDLER_H
+#define SENSEHANDLER_H
+
+#include "HTTPRequestHandler.h"
+
+extern void parseEvent(char* content);
+
+
+class SenseHandler : public HTTPRequestHandler {
+public:
+ SenseHandler(const char* rootPath, const char* path, TCPSocket* pTCPSocket);
+ virtual ~SenseHandler();
+
+//protected:
+ static inline HTTPRequestHandler* inst(const char* rootPath, const char* path, TCPSocket* pTCPSocket) {
+ return new SenseHandler(rootPath, path, pTCPSocket); //if we ever could do static virtual functions, this would be one
+ }
+
+ virtual void doGet();
+ virtual void doPost();
+ virtual void doHead();
+
+ virtual void onReadable(); //Data has been read
+ virtual void onWriteable(); //Data has been written & buf is free
+ virtual void onClose(); //Connection is closing
+
+};
+
+
+#endif
\ No newline at end of file