John Lowe
/
WebSockets2
Embedded WebSockets Experiment
HTTPRestHandler.cpp@0:6dee052a3fa4, 2011-07-26 (annotated)
- Committer:
- nandgate
- Date:
- Tue Jul 26 05:30:53 2011 +0000
- Revision:
- 0:6dee052a3fa4
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
nandgate | 0:6dee052a3fa4 | 1 | #include "HTTPRestHandler.h" |
nandgate | 0:6dee052a3fa4 | 2 | #include "stdio.h" |
nandgate | 0:6dee052a3fa4 | 3 | #include "TemperatureSensor.h" |
nandgate | 0:6dee052a3fa4 | 4 | #include "RGBLed.h" |
nandgate | 0:6dee052a3fa4 | 5 | |
nandgate | 0:6dee052a3fa4 | 6 | extern TemperatureSensor sensor; |
nandgate | 0:6dee052a3fa4 | 7 | extern RGBLed rgb; |
nandgate | 0:6dee052a3fa4 | 8 | |
nandgate | 0:6dee052a3fa4 | 9 | HTTPStatus HTTPRestHandler::doGet(char *resource, HTTPConnection *conn) const { |
nandgate | 0:6dee052a3fa4 | 10 | printf("HTTPRestHandler GET: %s\n", resource); |
nandgate | 0:6dee052a3fa4 | 11 | |
nandgate | 0:6dee052a3fa4 | 12 | HTTPRestData *d= new HTTPRestData(); |
nandgate | 0:6dee052a3fa4 | 13 | char *host; |
nandgate | 0:6dee052a3fa4 | 14 | char buffer[20]; |
nandgate | 0:6dee052a3fa4 | 15 | |
nandgate | 0:6dee052a3fa4 | 16 | // The host field string can have junk at the end... |
nandgate | 0:6dee052a3fa4 | 17 | host= (char*) conn->getField("Host"); |
nandgate | 0:6dee052a3fa4 | 18 | for (char *p= host; *p != 0; p++) { |
nandgate | 0:6dee052a3fa4 | 19 | if(!isprint(*p)) { |
nandgate | 0:6dee052a3fa4 | 20 | *p= 0; |
nandgate | 0:6dee052a3fa4 | 21 | } |
nandgate | 0:6dee052a3fa4 | 22 | } |
nandgate | 0:6dee052a3fa4 | 23 | |
nandgate | 0:6dee052a3fa4 | 24 | if (strcmp(resource, "/") == 0) { |
nandgate | 0:6dee052a3fa4 | 25 | strcat(d->response, "{\"temperature\":\"http://"); |
nandgate | 0:6dee052a3fa4 | 26 | strcat(d->response, host); |
nandgate | 0:6dee052a3fa4 | 27 | strcat(d->response, conn->getURL()); |
nandgate | 0:6dee052a3fa4 | 28 | strcat(d->response, "temperature\",\"rgbled\":\"http://"); |
nandgate | 0:6dee052a3fa4 | 29 | strcat(d->response, host); |
nandgate | 0:6dee052a3fa4 | 30 | strcat(d->response, conn->getURL()); |
nandgate | 0:6dee052a3fa4 | 31 | strcat(d->response, "rgbled\"}"); |
nandgate | 0:6dee052a3fa4 | 32 | } else if (strcmp(resource, "/temperature") == 0) { |
nandgate | 0:6dee052a3fa4 | 33 | sensor.measure(); |
nandgate | 0:6dee052a3fa4 | 34 | sprintf(buffer, "%f", sensor.getKelvin()); |
nandgate | 0:6dee052a3fa4 | 35 | |
nandgate | 0:6dee052a3fa4 | 36 | strcat(d->response, "{\"realtime-uri\":\"ws://"); |
nandgate | 0:6dee052a3fa4 | 37 | strcat(d->response, host); |
nandgate | 0:6dee052a3fa4 | 38 | strcat(d->response, "/ws/\",\"format\":\"text/csv\",\"fields\":\"ADC,resistence,temperature\",\"calibration\":\"http:/"); |
nandgate | 0:6dee052a3fa4 | 39 | strcat(d->response, conn->getURL()); |
nandgate | 0:6dee052a3fa4 | 40 | strcat(d->response, "/calibration\","); |
nandgate | 0:6dee052a3fa4 | 41 | strcat(d->response, "\"units\":\"kelvin\",\"value\":"); |
nandgate | 0:6dee052a3fa4 | 42 | strcat(d->response, buffer); |
nandgate | 0:6dee052a3fa4 | 43 | strcat(d->response, "}"); |
nandgate | 0:6dee052a3fa4 | 44 | } else if (strcmp(resource, "/temperature/calibration") == 0) { |
nandgate | 0:6dee052a3fa4 | 45 | strcat(d->response, "{TODO}"); |
nandgate | 0:6dee052a3fa4 | 46 | } else if (strcmp(resource, "/rgbled") == 0) { |
nandgate | 0:6dee052a3fa4 | 47 | strcat(d->response, "{\"web-socket-uri\":\"ws://"); |
nandgate | 0:6dee052a3fa4 | 48 | strcat(d->response, host); |
nandgate | 0:6dee052a3fa4 | 49 | strcat(d->response, "/ws/\",\"format\":\"text/csv\",\"fields\":\"red,green,blue\"}"); |
nandgate | 0:6dee052a3fa4 | 50 | } else return HTTP_NotFound; |
nandgate | 0:6dee052a3fa4 | 51 | |
nandgate | 0:6dee052a3fa4 | 52 | //conn->setHeaderFields("Content-Type: application/json"); |
nandgate | 0:6dee052a3fa4 | 53 | conn->setLength(strlen(d->response)); |
nandgate | 0:6dee052a3fa4 | 54 | conn->data= d; |
nandgate | 0:6dee052a3fa4 | 55 | |
nandgate | 0:6dee052a3fa4 | 56 | //printf("json: [ %s ]\n", static_cast<HTTPRestData*>(conn->data)->response); |
nandgate | 0:6dee052a3fa4 | 57 | return HTTP_OK; |
nandgate | 0:6dee052a3fa4 | 58 | } |
nandgate | 0:6dee052a3fa4 | 59 | |
nandgate | 0:6dee052a3fa4 | 60 | HTTPStatus HTTPRestHandler::doPost(char *resource, HTTPConnection *conn) const { |
nandgate | 0:6dee052a3fa4 | 61 | printf("HTTPRestHandler POST: %s\n", resource); |
nandgate | 0:6dee052a3fa4 | 62 | return HTTP_NotFound; |
nandgate | 0:6dee052a3fa4 | 63 | } |
nandgate | 0:6dee052a3fa4 | 64 | |
nandgate | 0:6dee052a3fa4 | 65 | void HTTPRestHandler::reg(HTTPServer *server) { |
nandgate | 0:6dee052a3fa4 | 66 | server->registerField("Host"); |
nandgate | 0:6dee052a3fa4 | 67 | } |
nandgate | 0:6dee052a3fa4 | 68 | |
nandgate | 0:6dee052a3fa4 | 69 | HTTPStatus HTTPRestHandler::init(HTTPConnection *conn) const { |
nandgate | 0:6dee052a3fa4 | 70 | char *resource= conn->getURL() + prefixLength; |
nandgate | 0:6dee052a3fa4 | 71 | switch (conn->getType()) { |
nandgate | 0:6dee052a3fa4 | 72 | case GET: |
nandgate | 0:6dee052a3fa4 | 73 | return doGet(resource, conn); |
nandgate | 0:6dee052a3fa4 | 74 | |
nandgate | 0:6dee052a3fa4 | 75 | case POST: |
nandgate | 0:6dee052a3fa4 | 76 | return doPost(resource, conn); |
nandgate | 0:6dee052a3fa4 | 77 | } |
nandgate | 0:6dee052a3fa4 | 78 | return HTTP_BadRequest; |
nandgate | 0:6dee052a3fa4 | 79 | } |
nandgate | 0:6dee052a3fa4 | 80 | |
nandgate | 0:6dee052a3fa4 | 81 | |
nandgate | 0:6dee052a3fa4 | 82 | HTTPHandle HTTPRestHandler::data(HTTPConnection *conn, void *data, int len) const { |
nandgate | 0:6dee052a3fa4 | 83 | //printf("REST data()\n"); |
nandgate | 0:6dee052a3fa4 | 84 | return HTTP_SuccessEnded; |
nandgate | 0:6dee052a3fa4 | 85 | } |
nandgate | 0:6dee052a3fa4 | 86 | |
nandgate | 0:6dee052a3fa4 | 87 | HTTPHandle HTTPRestHandler::send(HTTPConnection *conn, int maxData) const { |
nandgate | 0:6dee052a3fa4 | 88 | HTTPRestData *d= static_cast<HTTPRestData*>(conn->data); |
nandgate | 0:6dee052a3fa4 | 89 | const char *str= d->response; |
nandgate | 0:6dee052a3fa4 | 90 | int len= strlen(str); |
nandgate | 0:6dee052a3fa4 | 91 | printf("REST send: %d [ %s ]\n", len, str); |
nandgate | 0:6dee052a3fa4 | 92 | conn->write((void*)str, len); |
nandgate | 0:6dee052a3fa4 | 93 | return HTTP_SuccessEnded; |
nandgate | 0:6dee052a3fa4 | 94 | } |