A wrapper class for talking to Axeda from MBED devices. Uses HTTPClient and MbedJSONValue classes.

Dependents:   axeda_wrapper_dev MTS_Axeda_Example

AxedaWrapper simplifies pushing data to Axeda's cloud.

Uses HTTPClient and MbedJSONValue libs:

http://mbed.org/users/donatien/code/HTTPClient/

http://mbed.org/users/samux/code/MbedJSONValue/

Committer:
mfiore
Date:
Sat Jan 04 08:00:38 2014 +0000
Revision:
6:aa0594410aa9
Parent:
5:dd1e00e3eba5
shorten HTTPClient timeout to 5 seconds

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mfiore 0:d472df0659a9 1 #include "AxedaWrapper.h"
mfiore 1:d42aaf6f2e19 2 #include "HTTPJsonText.h"
mfiore 0:d472df0659a9 3 #include <string>
mfiore 0:d472df0659a9 4 #include <vector>
mfiore 0:d472df0659a9 5 #include <stdlib.h>
mfiore 0:d472df0659a9 6
mfiore 0:d472df0659a9 7 using namespace mts;
mfiore 0:d472df0659a9 8
mfiore 0:d472df0659a9 9 static const char* PATH_BASE = "/ammp/data/1/";
mfiore 0:d472df0659a9 10
mfiore 0:d472df0659a9 11 AxedaWrapper::AxedaWrapper(const std::string& serial, const std::string& model, const std::string& host, int port) : _timeout(30000) {
mfiore 0:d472df0659a9 12 _http_client = new HTTPClient();
mfiore 0:d472df0659a9 13 _data = new MbedJSONValue();
mfiore 0:d472df0659a9 14
mfiore 0:d472df0659a9 15 char buf[256];
mfiore 1:d42aaf6f2e19 16 std::string path = PATH_BASE + model + "!" + serial;
mfiore 0:d472df0659a9 17 sprintf(buf, "http://%s:%d%s", host.c_str(), port, path.c_str());
mfiore 0:d472df0659a9 18 _url = std::string(buf);
mfiore 0:d472df0659a9 19 }
mfiore 0:d472df0659a9 20
mfiore 0:d472df0659a9 21 AxedaWrapper::~AxedaWrapper() {
mfiore 0:d472df0659a9 22 delete _http_client;
mfiore 0:d472df0659a9 23 delete _data;
mfiore 0:d472df0659a9 24 }
mfiore 0:d472df0659a9 25
mfiore 2:99baa98f84a3 26 void AxedaWrapper::push(const std::string& key, const std::string& value) {
mfiore 1:d42aaf6f2e19 27 (*_data)["data"][0]["di"][key] = value;
mfiore 0:d472df0659a9 28 }
mfiore 0:d472df0659a9 29
sgodinez 3:134410324a6a 30 void AxedaWrapper::push(const std::string& key, const char* value) {
sgodinez 3:134410324a6a 31 (*_data)["data"][0]["di"][key] = std::string(value);
mfiore 0:d472df0659a9 32 }
mfiore 0:d472df0659a9 33
mfiore 2:99baa98f84a3 34 void AxedaWrapper::push(const std::string& key, int value) {
mfiore 1:d42aaf6f2e19 35 (*_data)["data"][0]["di"][key] = value;
mfiore 0:d472df0659a9 36 }
mfiore 0:d472df0659a9 37
mfiore 2:99baa98f84a3 38 void AxedaWrapper::push(const std::string& key, double value) {
mfiore 1:d42aaf6f2e19 39 (*_data)["data"][0]["di"][key] = value;
mfiore 0:d472df0659a9 40 }
mfiore 0:d472df0659a9 41
mfiore 2:99baa98f84a3 42 void AxedaWrapper::push(const std::string& key, bool value) {
mfiore 1:d42aaf6f2e19 43 (*_data)["data"][0]["di"][key] = value;
mfiore 0:d472df0659a9 44 }
mfiore 0:d472df0659a9 45
mfiore 2:99baa98f84a3 46 std::string AxedaWrapper::toString() {
mfiore 2:99baa98f84a3 47 return size() > 0 ? (*_data)["data"][0]["di"].serialize() : "{}";
mfiore 0:d472df0659a9 48 }
mfiore 0:d472df0659a9 49
mfiore 2:99baa98f84a3 50 void AxedaWrapper::clear() {
mfiore 2:99baa98f84a3 51 if (size()) {
mfiore 2:99baa98f84a3 52 delete _data;
mfiore 2:99baa98f84a3 53 _data = new MbedJSONValue();
mfiore 2:99baa98f84a3 54 }
mfiore 0:d472df0659a9 55 }
mfiore 0:d472df0659a9 56
mfiore 2:99baa98f84a3 57 int AxedaWrapper::size() {
mfiore 2:99baa98f84a3 58 int size = (*_data)["data"][0]["di"].size();
mfiore 2:99baa98f84a3 59 return size < 0 ? 0 : size;
mfiore 2:99baa98f84a3 60 }
mfiore 2:99baa98f84a3 61
mfiore 2:99baa98f84a3 62 bool AxedaWrapper::send(const std::string& key, const std::string& value) {
mfiore 0:d472df0659a9 63 MbedJSONValue data;
mfiore 1:d42aaf6f2e19 64 data["data"][0]["di"][key] = value;
mfiore 1:d42aaf6f2e19 65 return sendBase(data.serialize());
mfiore 0:d472df0659a9 66 }
mfiore 0:d472df0659a9 67
sgodinez 3:134410324a6a 68 bool AxedaWrapper::send(const std::string& key, const char* value) {
mfiore 0:d472df0659a9 69 MbedJSONValue data;
sgodinez 3:134410324a6a 70 data["data"][0]["di"][key] = std::string(value);
mfiore 1:d42aaf6f2e19 71 return sendBase(data.serialize());
mfiore 0:d472df0659a9 72 }
mfiore 0:d472df0659a9 73
mfiore 2:99baa98f84a3 74 bool AxedaWrapper::send(const std::string& key, int value) {
mfiore 0:d472df0659a9 75 MbedJSONValue data;
mfiore 1:d42aaf6f2e19 76 data["data"][0]["di"][key] = value;
mfiore 1:d42aaf6f2e19 77 return sendBase(data.serialize());
mfiore 0:d472df0659a9 78 }
mfiore 0:d472df0659a9 79
mfiore 2:99baa98f84a3 80 bool AxedaWrapper::send(const std::string& key, double value) {
mfiore 0:d472df0659a9 81 MbedJSONValue data;
mfiore 1:d42aaf6f2e19 82 data["data"][0]["di"][key] = value;
mfiore 1:d42aaf6f2e19 83 return sendBase(data.serialize());
mfiore 0:d472df0659a9 84 }
mfiore 0:d472df0659a9 85
mfiore 2:99baa98f84a3 86 bool AxedaWrapper::sendAll(bool clean) {
mfiore 1:d42aaf6f2e19 87 std::string data = _data->serialize();
mfiore 1:d42aaf6f2e19 88 bool ret = sendBase(data);
mfiore 2:99baa98f84a3 89 if (ret && clean) {
mfiore 2:99baa98f84a3 90 clear();
mfiore 0:d472df0659a9 91 }
mfiore 0:d472df0659a9 92 return ret;
mfiore 0:d472df0659a9 93 }
mfiore 0:d472df0659a9 94
mfiore 1:d42aaf6f2e19 95 bool AxedaWrapper::sendBase(const std::string& data) {
mfiore 0:d472df0659a9 96 int ret;
mfiore 2:99baa98f84a3 97 char buf[128] = {0};
mfiore 1:d42aaf6f2e19 98 HTTPJsonText to_send((char*) data.c_str());
mfiore 1:d42aaf6f2e19 99 HTTPText to_recv(buf, sizeof(buf));
mfiore 1:d42aaf6f2e19 100 ret = _http_client->post(_url.c_str(), to_send, &to_recv, _timeout);
mfiore 0:d472df0659a9 101 if (ret != HTTP_OK) {
mfiore 5:dd1e00e3eba5 102 printf("[ERROR] HTTP POST failed\n\r");
mfiore 0:d472df0659a9 103 return false;
mfiore 5:dd1e00e3eba5 104 } else {
mfiore 5:dd1e00e3eba5 105 printf("[INFO] HTTP POST returned %d\r\n", _http_client->getHTTPResponseCode());
mfiore 0:d472df0659a9 106 }
mfiore 0:d472df0659a9 107 return true;
mfiore 0:d472df0659a9 108 }