Ambient Library

Fork of AmbientLib by Takehiko Shimojima

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers Ambient.cpp Source File

Ambient.cpp

00001 #include "Ambient.h"
00002 
00003 #define _DEBUG 0
00004 
00005 #if _DEBUG
00006 #define DBG(...) { printf(__VA_ARGS__); }
00007 #define ERR(...) { printf(__VA_ARGS__); }
00008 #else
00009 #define DBG(...)
00010 #define ERR(...)
00011 #endif /* _DBG */
00012 
00013 const char* AMBIENT_HOST = "54.65.206.59";
00014 const int AMBIENT_PORT = 80;
00015 const char* AMBIENT_HOST_DEV = "192.168.0.8";
00016 const int AMBIENT_PORT_DEV = 4567;
00017 
00018 const char * ambient_keys[] = {"\"d1\":\"", "\"d2\":\"", "\"d3\":\"", "\"d4\":\"", "\"d5\":\"", "\"d6\":\"", "\"d7\":\"", "\"d8\":\"", "\"lat\":\"", "\"lng\":\"", "\"created\":\""};
00019 
00020 AMBIENT::AMBIENT() {
00021 }
00022 
00023 bool
00024 AMBIENT::init(unsigned int channelId, const char * writeKey, TCPSocketConnection * s, int dev) {
00025     this->channelId = channelId;
00026 
00027     if (sizeof(writeKey) > AMBIENT_WRITEKEY_SIZE) {
00028         ERR("writeKey length > AMBIENT_WRITEKEY_SIZE");
00029         return false;
00030     }
00031     strcpy(this->writeKey, writeKey);
00032 
00033     if(NULL == s) {
00034         ERR("Socket Pointer is NULL, open a socket.");
00035         return false;
00036     }
00037     this->s = s;
00038     this->dev = dev;
00039     if (dev) {
00040         strcpy(this->host, AMBIENT_HOST_DEV);
00041         this->port = AMBIENT_PORT_DEV;
00042     } else {
00043         strcpy(this->host, AMBIENT_HOST);
00044         this->port = AMBIENT_PORT;
00045     }
00046     for (int i = 0; i < AMBIENT_NUM_PARAMS; i++) {
00047         this->data[i].set = false;
00048     }
00049     return true;
00050 }
00051 
00052 bool
00053 AMBIENT::set(int field, char * data) {
00054     --field;
00055     if (field < 0 || field >= AMBIENT_NUM_PARAMS) {
00056         return false;
00057     }
00058     if (strlen(data) > AMBIENT_DATA_SIZE) {
00059         return false;
00060     }
00061     this->data[field].set = true;
00062     strcpy(this->data[field].item, data);
00063 
00064     return true;
00065 }
00066 
00067 bool
00068 AMBIENT::clear(int field) {
00069     --field;
00070     if (field < 0 || field >= AMBIENT_NUM_PARAMS) {
00071         return false;
00072     }
00073     this->data[field].set = false;
00074 
00075     return true;
00076 }
00077 
00078 bool
00079 AMBIENT::send() {
00080 
00081     int retry;
00082     for (retry = 0; retry < AMBIENT_MAX_RETRY; retry++) {
00083         int ret;
00084         ret = this->s->connect(this->host, this->port);
00085         if (ret == 0) {
00086             break ;
00087         }
00088     }
00089     if(retry == AMBIENT_MAX_RETRY) {
00090         ERR("Could not connect socket to host\r\n");
00091         return false;
00092     }
00093 
00094     char str[360] = {0};
00095     char header[54] = {0};
00096     char host[32] = {0};
00097     char contentLen[28] = {0};
00098     const char *contentType = "Content-Type: application/json\r\n\r\n";
00099     char body[192] = {0};
00100 
00101     strcat(body, "{\"writeKey\":\"");
00102     strcat(body, this->writeKey);
00103     strcat(body, "\",");
00104 
00105     for (int i = 0; i < AMBIENT_NUM_PARAMS; i++) {
00106         if (this->data[i].set) {
00107             strcat(body, ambient_keys[i]);
00108             strcat(body, this->data[i].item);
00109             strcat(body, "\",");
00110         }
00111     }
00112     body[strlen(body) - 1] = '\0';
00113 
00114     strcat(body, "}\r\n");
00115 
00116     sprintf(header, "POST /api/v2/channels/%d/data HTTP/1.1\r\n", this->channelId);
00117     if (this->port == 80) {
00118         sprintf(host, "Host: %s\r\n", this->host);
00119     } else {
00120         sprintf(host, "Host: %s:%d\r\n", this->host, this->port);
00121     }
00122     sprintf(contentLen, "Content-Length: %d\r\n", strlen(body));
00123     sprintf(str, "%s%s%s%s%s", header, host, contentLen, contentType, body);
00124 
00125     DBG("sending: %d bytes\r\n%s", strlen(str), str);
00126 
00127     int ret;
00128     ret = this->s->send_all(str, strlen(str));
00129     wait_ms(30);
00130     DBG("%d bytes sent\r\n", ret);
00131     if (ret < 0) {
00132         ERR("send failed\r\n");
00133         return false;
00134     }
00135 
00136     ret = this->s->receive(str,sizeof(str));
00137     str[ret]=0;
00138     DBG("Received String : (%d)\r\n%s\r\n",ret, str);
00139 
00140     this->s->close();
00141 
00142     for (int i = 0; i < AMBIENT_NUM_PARAMS; i++) {
00143         this->data[i].set = false;
00144     }
00145 
00146     return true;
00147 }