Http adapted library to connect the DISCO_F746NG board to thethings.iO

Dependents:   thethingsiO-DISCO_F746NG-http

Committer:
charlyo
Date:
Tue Sep 06 08:44:29 2016 +0000
Revision:
0:2f025da7eef3
Updated methods

Who changed what in which revision?

UserRevisionLine numberNew contents of line
charlyo 0:2f025da7eef3 1 #include "ThethingsiO_DISCO_F746NG.h"
charlyo 0:2f025da7eef3 2
charlyo 0:2f025da7eef3 3 ThethingsiO_DISCO_F746NG::ThethingsiO_DISCO_F746NG()
charlyo 0:2f025da7eef3 4 {
charlyo 0:2f025da7eef3 5 this->interface.init();
charlyo 0:2f025da7eef3 6 this->interface.connect();
charlyo 0:2f025da7eef3 7 this->buffer = network::Buffer(1024);
charlyo 0:2f025da7eef3 8 }
charlyo 0:2f025da7eef3 9
charlyo 0:2f025da7eef3 10 ThethingsiO_DISCO_F746NG::ThethingsiO_DISCO_F746NG(string token)
charlyo 0:2f025da7eef3 11 {
charlyo 0:2f025da7eef3 12 this->interface.init();
charlyo 0:2f025da7eef3 13 this->interface.connect();
charlyo 0:2f025da7eef3 14 this->thingToken = token;
charlyo 0:2f025da7eef3 15 this->buffer = network::Buffer(1024);
charlyo 0:2f025da7eef3 16 }
charlyo 0:2f025da7eef3 17
charlyo 0:2f025da7eef3 18 ThethingsiO_DISCO_F746NG::~ThethingsiO_DISCO_F746NG()
charlyo 0:2f025da7eef3 19 {
charlyo 0:2f025da7eef3 20
charlyo 0:2f025da7eef3 21 }
charlyo 0:2f025da7eef3 22
charlyo 0:2f025da7eef3 23 string ThethingsiO_DISCO_F746NG::thingReadAll(int limit)
charlyo 0:2f025da7eef3 24 {
charlyo 0:2f025da7eef3 25 std::string request("GET /v2/things/");
charlyo 0:2f025da7eef3 26 request.append(thingToken);
charlyo 0:2f025da7eef3 27 request.append("/all_resources?limit=");
charlyo 0:2f025da7eef3 28 stringstream convert;
charlyo 0:2f025da7eef3 29 convert << limit;
charlyo 0:2f025da7eef3 30 request.append(convert.str());
charlyo 0:2f025da7eef3 31 request.append(" HTTP/1.1\r\nHost: %s\r\nUser-Agent: curl/7.43.0\r\nAccept: application/json\r\n\r\n");
charlyo 0:2f025da7eef3 32 if (socket.open() < 0) {
charlyo 0:2f025da7eef3 33 printf("Failed to open TCP Socket\n\r");
charlyo 0:2f025da7eef3 34 return "Failed to open TCP Socket\n\r";
charlyo 0:2f025da7eef3 35 }
charlyo 0:2f025da7eef3 36 if (socket.connect("api.thethings.io", 80) < 0) {
charlyo 0:2f025da7eef3 37 printf("Failed to connect with thethings.iO\n\r");
charlyo 0:2f025da7eef3 38 return "Failed to connect with thethings.iO\n\r";
charlyo 0:2f025da7eef3 39 }
charlyo 0:2f025da7eef3 40 if (socket.write((void *)request.data(), request.size()) < 0) {
charlyo 0:2f025da7eef3 41 printf("Failed to write HTTP request\n\r");
charlyo 0:2f025da7eef3 42 return "Failed to write HTTP request\n\r";
charlyo 0:2f025da7eef3 43 }
charlyo 0:2f025da7eef3 44 int result = this->socket.read(buffer);
charlyo 0:2f025da7eef3 45 //printf("Received %d bytes:\n\r%s\n\r", string string string string string string string int result, (char *)buffer.data());
charlyo 0:2f025da7eef3 46 string response = string((char*)buffer.data()).substr(string((char*)buffer.data()).find("\r\n\r\n"));
charlyo 0:2f025da7eef3 47 buffer.flush();
charlyo 0:2f025da7eef3 48 socket.close();
charlyo 0:2f025da7eef3 49 return response;
charlyo 0:2f025da7eef3 50 }
charlyo 0:2f025da7eef3 51
charlyo 0:2f025da7eef3 52 string ThethingsiO_DISCO_F746NG::thingRead(string resource, int limit)
charlyo 0:2f025da7eef3 53 {
charlyo 0:2f025da7eef3 54 std::string request("GET /v2/things/");
charlyo 0:2f025da7eef3 55 request.append(thingToken);
charlyo 0:2f025da7eef3 56 request.append("/resources/");
charlyo 0:2f025da7eef3 57 request.append(resource);
charlyo 0:2f025da7eef3 58 request.append("?limit=");
charlyo 0:2f025da7eef3 59 stringstream convert;
charlyo 0:2f025da7eef3 60 convert << limit;
charlyo 0:2f025da7eef3 61 request.append(convert.str());
charlyo 0:2f025da7eef3 62 request.append(" HTTP/1.1\r\nHost: %s\r\nUser-Agent: curl/7.43.0\r\nAccept: application/json\r\n\r\n");
charlyo 0:2f025da7eef3 63 if (socket.open() < 0) {
charlyo 0:2f025da7eef3 64 printf("Failed to open TCP Socket\n\r");
charlyo 0:2f025da7eef3 65 return "Failed to open TCP Socket\n\r";
charlyo 0:2f025da7eef3 66 }
charlyo 0:2f025da7eef3 67
charlyo 0:2f025da7eef3 68 if (socket.connect("api.thethings.io", 80) < 0) {
charlyo 0:2f025da7eef3 69 printf("Failed to connect with thethings.iO\n\r");
charlyo 0:2f025da7eef3 70 return "Failed to connect with thethings.iO\n\r";
charlyo 0:2f025da7eef3 71 }
charlyo 0:2f025da7eef3 72
charlyo 0:2f025da7eef3 73 if (socket.write((void *)request.data(), request.size()) < 0) {
charlyo 0:2f025da7eef3 74 printf("Failed to write HTTP request\n\r");
charlyo 0:2f025da7eef3 75 return "Failed to write HTTP request\n\r";
charlyo 0:2f025da7eef3 76 }
charlyo 0:2f025da7eef3 77 int result = this->socket.read(this->buffer);
charlyo 0:2f025da7eef3 78 //printf("Received %d bytes:\n\r%s\n\r", string string string string string string string int result, (char *)buffer.data());
charlyo 0:2f025da7eef3 79 string response = string((char*)this->buffer.data()).substr(string((char*)this->buffer.data()).find("\r\n\r\n"));
charlyo 0:2f025da7eef3 80 this->buffer.flush();
charlyo 0:2f025da7eef3 81 socket.close();
charlyo 0:2f025da7eef3 82 return response;
charlyo 0:2f025da7eef3 83 }
charlyo 0:2f025da7eef3 84
charlyo 0:2f025da7eef3 85 int ThethingsiO_DISCO_F746NG::thingWrite(string resource, string value)
charlyo 0:2f025da7eef3 86 {
charlyo 0:2f025da7eef3 87 string body;
charlyo 0:2f025da7eef3 88 body.append("{\"values\":[{\"key\":\"");
charlyo 0:2f025da7eef3 89 body.append(resource);
charlyo 0:2f025da7eef3 90 body.append("\",\"value\":\"");
charlyo 0:2f025da7eef3 91 body.append(value);
charlyo 0:2f025da7eef3 92 body.append("\"}]}");
charlyo 0:2f025da7eef3 93 std::string writerequest("POST /v2/things/");
charlyo 0:2f025da7eef3 94 writerequest.append(thingToken);
charlyo 0:2f025da7eef3 95 writerequest.append(" HTTP/1.1\r\nHost: %s\r\nUser-Agent: curl/7.43.0\r\nAccept: application/json\r\nContent-Type: application/json\r\nContent-Length: ");
charlyo 0:2f025da7eef3 96 stringstream convert;
charlyo 0:2f025da7eef3 97 convert << body.length();
charlyo 0:2f025da7eef3 98 writerequest.append(convert.str());
charlyo 0:2f025da7eef3 99 writerequest.append("\r\n\r\n");
charlyo 0:2f025da7eef3 100 writerequest.append(body);
charlyo 0:2f025da7eef3 101 writerequest.append("\r\n");
charlyo 0:2f025da7eef3 102 if (socket.open() < 0) {
charlyo 0:2f025da7eef3 103 printf("Failed to open TCP Socket\n\r");
charlyo 0:2f025da7eef3 104 return -1;
charlyo 0:2f025da7eef3 105 }
charlyo 0:2f025da7eef3 106 if (socket.connect("api.thethings.io", 80) < 0) {
charlyo 0:2f025da7eef3 107 printf("Failed to connect with thethings.iO\n\r");
charlyo 0:2f025da7eef3 108 return -1;
charlyo 0:2f025da7eef3 109 }
charlyo 0:2f025da7eef3 110 if (socket.write((void *)writerequest.data(), writerequest.size()) < 0) {
charlyo 0:2f025da7eef3 111 printf("Failed to connect with thethings.iO\n\r");
charlyo 0:2f025da7eef3 112 return -1;
charlyo 0:2f025da7eef3 113 }
charlyo 0:2f025da7eef3 114 int result = socket.read(buffer);
charlyo 0:2f025da7eef3 115 //printf("Received %d bytes:\n\r%s\n\r", string string string string string string string int result, (char *)buffer.data());
charlyo 0:2f025da7eef3 116 string response = string((char *) buffer.data());
charlyo 0:2f025da7eef3 117 //printf("%s\n", (char *) buffer.data());
charlyo 0:2f025da7eef3 118 int ret = response.find("status:created");
charlyo 0:2f025da7eef3 119 buffer.flush();
charlyo 0:2f025da7eef3 120 socket.close();
charlyo 0:2f025da7eef3 121 if (ret >= 0) return 0;
charlyo 0:2f025da7eef3 122 else return -1;
charlyo 0:2f025da7eef3 123 }
charlyo 0:2f025da7eef3 124
charlyo 0:2f025da7eef3 125 string ThethingsiO_DISCO_F746NG::thingActivate(string activationCode)
charlyo 0:2f025da7eef3 126 {
charlyo 0:2f025da7eef3 127 string body;
charlyo 0:2f025da7eef3 128 body.append("{\"activationCode\":\"");
charlyo 0:2f025da7eef3 129 body.append(activationCode);
charlyo 0:2f025da7eef3 130 body.append("\"}");
charlyo 0:2f025da7eef3 131 std::string writerequest("POST /v2/things/");
charlyo 0:2f025da7eef3 132 writerequest.append(" HTTP/1.1\r\nHost: %s\r\nUser-Agent: curl/7.43.0\r\nAccept: application/json\r\nContent-Type: application/json\r\nContent-Length: ");
charlyo 0:2f025da7eef3 133 stringstream convert;
charlyo 0:2f025da7eef3 134 convert << body.length();
charlyo 0:2f025da7eef3 135 writerequest.append(convert.str());
charlyo 0:2f025da7eef3 136 writerequest.append("\r\n\r\n");
charlyo 0:2f025da7eef3 137 writerequest.append(body);
charlyo 0:2f025da7eef3 138 writerequest.append("\r\n");
charlyo 0:2f025da7eef3 139 if (socket.open() < 0) {
charlyo 0:2f025da7eef3 140 printf("Failed to open TCP Socket\n\r");
charlyo 0:2f025da7eef3 141 return "Failed to open TCP Socket\n\r";
charlyo 0:2f025da7eef3 142 }
charlyo 0:2f025da7eef3 143 if (socket.connect("api.thethings.io", 80) < 0) {
charlyo 0:2f025da7eef3 144 printf("Failed to connect with thethings.iO\n\r");
charlyo 0:2f025da7eef3 145 return "Failed to connect with thethings.iO\n\r";
charlyo 0:2f025da7eef3 146 }
charlyo 0:2f025da7eef3 147 if (socket.write((void *)writerequest.data(), writerequest.size()) < 0) {
charlyo 0:2f025da7eef3 148 printf("Failed to write HTTP request\n\r");
charlyo 0:2f025da7eef3 149 return "Failed to write HTTP request\n\r";
charlyo 0:2f025da7eef3 150 }
charlyo 0:2f025da7eef3 151 int result = socket.read(buffer);
charlyo 0:2f025da7eef3 152 //printf("Received %d bytes:\n\r%s\n\r", string string string string string string string int result, (char *)buffer.data());
charlyo 0:2f025da7eef3 153 string response = string((char *) buffer.data());
charlyo 0:2f025da7eef3 154 response = response.substr(response.find("\r\n\r\n"));
charlyo 0:2f025da7eef3 155 buffer.flush();
charlyo 0:2f025da7eef3 156 socket.close();
charlyo 0:2f025da7eef3 157
charlyo 0:2f025da7eef3 158 //string string string int result = "{\"status\":\"created\",\"message\":\"thing activated\",\"thingToken\":\"5RWZ6xFogVSJEla3b2gbToHGF1Ko-R8vTNoS3FMl8mM\"}"
charlyo 0:2f025da7eef3 159 return response;
charlyo 0:2f025da7eef3 160 }
charlyo 0:2f025da7eef3 161
charlyo 0:2f025da7eef3 162 string ThethingsiO_DISCO_F746NG::getToken()
charlyo 0:2f025da7eef3 163 {
charlyo 0:2f025da7eef3 164 return this->thingToken;
charlyo 0:2f025da7eef3 165 }
charlyo 0:2f025da7eef3 166
charlyo 0:2f025da7eef3 167 void ThethingsiO_DISCO_F746NG::setToken(string token)
charlyo 0:2f025da7eef3 168 {
charlyo 0:2f025da7eef3 169 this->thingToken = token;
charlyo 0:2f025da7eef3 170 }