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/
AxedaWrapper.cpp@2:99baa98f84a3, 2013-12-23 (annotated)
- Committer:
- mfiore
- Date:
- Mon Dec 23 19:17:03 2013 +0000
- Revision:
- 2:99baa98f84a3
- Parent:
- 1:d42aaf6f2e19
- Child:
- 3:134410324a6a
standardized function names, updated doc; added toString function; added some unit tests; added logging of HTTP error code when sending data fails
Who changed what in which revision?
User | Revision | Line number | New 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 | |
mfiore | 2:99baa98f84a3 | 30 | void AxedaWrapper::push(const char* key, const char* value) { |
mfiore | 1:d42aaf6f2e19 | 31 | (*_data)["data"][0]["di"][std::string(key)] = 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 char* key, int value) { |
mfiore | 1:d42aaf6f2e19 | 39 | (*_data)["data"][0]["di"][std::string(key)] = value; |
mfiore | 0:d472df0659a9 | 40 | } |
mfiore | 0:d472df0659a9 | 41 | |
mfiore | 2:99baa98f84a3 | 42 | void AxedaWrapper::push(const std::string& key, double value) { |
mfiore | 1:d42aaf6f2e19 | 43 | (*_data)["data"][0]["di"][key] = value; |
mfiore | 0:d472df0659a9 | 44 | } |
mfiore | 0:d472df0659a9 | 45 | |
mfiore | 2:99baa98f84a3 | 46 | void AxedaWrapper::push(const char* key, double value) { |
mfiore | 1:d42aaf6f2e19 | 47 | (*_data)["data"][0]["di"][std::string(key)] = value; |
mfiore | 0:d472df0659a9 | 48 | } |
mfiore | 0:d472df0659a9 | 49 | |
mfiore | 2:99baa98f84a3 | 50 | void AxedaWrapper::push(const std::string& key, bool value) { |
mfiore | 1:d42aaf6f2e19 | 51 | (*_data)["data"][0]["di"][key] = value; |
mfiore | 0:d472df0659a9 | 52 | } |
mfiore | 0:d472df0659a9 | 53 | |
mfiore | 2:99baa98f84a3 | 54 | void AxedaWrapper::push(const char* key, bool value) { |
mfiore | 1:d42aaf6f2e19 | 55 | (*_data)["data"][0]["di"][std::string(key)] = value; |
mfiore | 0:d472df0659a9 | 56 | } |
mfiore | 0:d472df0659a9 | 57 | |
mfiore | 2:99baa98f84a3 | 58 | std::string AxedaWrapper::toString() { |
mfiore | 2:99baa98f84a3 | 59 | return size() > 0 ? (*_data)["data"][0]["di"].serialize() : "{}"; |
mfiore | 0:d472df0659a9 | 60 | } |
mfiore | 0:d472df0659a9 | 61 | |
mfiore | 2:99baa98f84a3 | 62 | void AxedaWrapper::clear() { |
mfiore | 2:99baa98f84a3 | 63 | if (size()) { |
mfiore | 2:99baa98f84a3 | 64 | delete _data; |
mfiore | 2:99baa98f84a3 | 65 | _data = new MbedJSONValue(); |
mfiore | 2:99baa98f84a3 | 66 | } |
mfiore | 0:d472df0659a9 | 67 | } |
mfiore | 0:d472df0659a9 | 68 | |
mfiore | 2:99baa98f84a3 | 69 | int AxedaWrapper::size() { |
mfiore | 2:99baa98f84a3 | 70 | int size = (*_data)["data"][0]["di"].size(); |
mfiore | 2:99baa98f84a3 | 71 | return size < 0 ? 0 : size; |
mfiore | 2:99baa98f84a3 | 72 | } |
mfiore | 2:99baa98f84a3 | 73 | |
mfiore | 2:99baa98f84a3 | 74 | bool AxedaWrapper::send(const std::string& key, const std::string& 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 char* key, const char* 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::send(const std::string& key, int value) { |
mfiore | 0:d472df0659a9 | 87 | MbedJSONValue data; |
mfiore | 1:d42aaf6f2e19 | 88 | data["data"][0]["di"][key] = value; |
mfiore | 1:d42aaf6f2e19 | 89 | return sendBase(data.serialize()); |
mfiore | 0:d472df0659a9 | 90 | } |
mfiore | 0:d472df0659a9 | 91 | |
mfiore | 2:99baa98f84a3 | 92 | bool AxedaWrapper::send(const char* key, int value) { |
mfiore | 0:d472df0659a9 | 93 | MbedJSONValue data; |
mfiore | 1:d42aaf6f2e19 | 94 | data["data"][0]["di"][key] = value; |
mfiore | 1:d42aaf6f2e19 | 95 | return sendBase(data.serialize()); |
mfiore | 0:d472df0659a9 | 96 | } |
mfiore | 0:d472df0659a9 | 97 | |
mfiore | 2:99baa98f84a3 | 98 | bool AxedaWrapper::send(const std::string& key, double value) { |
mfiore | 0:d472df0659a9 | 99 | MbedJSONValue data; |
mfiore | 1:d42aaf6f2e19 | 100 | data["data"][0]["di"][key] = value; |
mfiore | 1:d42aaf6f2e19 | 101 | return sendBase(data.serialize()); |
mfiore | 0:d472df0659a9 | 102 | } |
mfiore | 0:d472df0659a9 | 103 | |
mfiore | 2:99baa98f84a3 | 104 | bool AxedaWrapper::send(const char* key, double value) { |
mfiore | 0:d472df0659a9 | 105 | MbedJSONValue data; |
mfiore | 1:d42aaf6f2e19 | 106 | data["data"][0]["di"][key] = value; |
mfiore | 1:d42aaf6f2e19 | 107 | return sendBase(data.serialize()); |
mfiore | 0:d472df0659a9 | 108 | } |
mfiore | 0:d472df0659a9 | 109 | |
mfiore | 2:99baa98f84a3 | 110 | bool AxedaWrapper::sendAll(bool clean) { |
mfiore | 1:d42aaf6f2e19 | 111 | std::string data = _data->serialize(); |
mfiore | 1:d42aaf6f2e19 | 112 | bool ret = sendBase(data); |
mfiore | 2:99baa98f84a3 | 113 | if (ret && clean) { |
mfiore | 2:99baa98f84a3 | 114 | clear(); |
mfiore | 0:d472df0659a9 | 115 | } |
mfiore | 0:d472df0659a9 | 116 | return ret; |
mfiore | 0:d472df0659a9 | 117 | } |
mfiore | 0:d472df0659a9 | 118 | |
mfiore | 1:d42aaf6f2e19 | 119 | bool AxedaWrapper::sendBase(const std::string& data) { |
mfiore | 0:d472df0659a9 | 120 | int ret; |
mfiore | 2:99baa98f84a3 | 121 | char buf[128] = {0}; |
mfiore | 1:d42aaf6f2e19 | 122 | HTTPJsonText to_send((char*) data.c_str()); |
mfiore | 1:d42aaf6f2e19 | 123 | HTTPText to_recv(buf, sizeof(buf)); |
mfiore | 1:d42aaf6f2e19 | 124 | ret = _http_client->post(_url.c_str(), to_send, &to_recv, _timeout); |
mfiore | 0:d472df0659a9 | 125 | if (ret != HTTP_OK) { |
mfiore | 2:99baa98f84a3 | 126 | printf("[ERROR] HTTP POST returned %d\n\r", _http_client->getHTTPResponseCode()); |
mfiore | 0:d472df0659a9 | 127 | return false; |
mfiore | 0:d472df0659a9 | 128 | } |
mfiore | 0:d472df0659a9 | 129 | return true; |
mfiore | 0:d472df0659a9 | 130 | } |