Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependents: axeda_wrapper_dev MTS_Axeda_Example
AxedaWrapper.cpp@0:d472df0659a9, 2013-12-19 (annotated)
- Committer:
- mfiore
- Date:
- Thu Dec 19 21:49:05 2013 +0000
- Revision:
- 0:d472df0659a9
- Child:
- 1:d42aaf6f2e19
initial commit
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| mfiore | 0:d472df0659a9 | 1 | #include "AxedaWrapper.h" |
| mfiore | 0:d472df0659a9 | 2 | #include <string> |
| mfiore | 0:d472df0659a9 | 3 | #include <vector> |
| mfiore | 0:d472df0659a9 | 4 | #include <stdlib.h> |
| mfiore | 0:d472df0659a9 | 5 | |
| mfiore | 0:d472df0659a9 | 6 | using namespace mts; |
| mfiore | 0:d472df0659a9 | 7 | |
| mfiore | 0:d472df0659a9 | 8 | static const char* PATH_BASE = "/ammp/data/1/"; |
| mfiore | 0:d472df0659a9 | 9 | |
| mfiore | 0:d472df0659a9 | 10 | AxedaWrapper::AxedaWrapper(const std::string& serial, const std::string& model, const std::string& host, int port) : _timeout(30000) { |
| mfiore | 0:d472df0659a9 | 11 | _http_client = new HTTPClient(); |
| mfiore | 0:d472df0659a9 | 12 | _data = new MbedJSONValue(); |
| mfiore | 0:d472df0659a9 | 13 | |
| mfiore | 0:d472df0659a9 | 14 | char buf[256]; |
| mfiore | 0:d472df0659a9 | 15 | std::string path = PATH_BASE + model + "/" + serial; |
| mfiore | 0:d472df0659a9 | 16 | sprintf(buf, "http://%s:%d%s", host.c_str(), port, path.c_str()); |
| mfiore | 0:d472df0659a9 | 17 | _url = std::string(buf); |
| mfiore | 0:d472df0659a9 | 18 | } |
| mfiore | 0:d472df0659a9 | 19 | |
| mfiore | 0:d472df0659a9 | 20 | AxedaWrapper::~AxedaWrapper() { |
| mfiore | 0:d472df0659a9 | 21 | delete _http_client; |
| mfiore | 0:d472df0659a9 | 22 | delete _data; |
| mfiore | 0:d472df0659a9 | 23 | } |
| mfiore | 0:d472df0659a9 | 24 | |
| mfiore | 0:d472df0659a9 | 25 | void AxedaWrapper::addKvp(const std::string& key, const std::string& value) { |
| mfiore | 0:d472df0659a9 | 26 | (*_data)[key] = value; |
| mfiore | 0:d472df0659a9 | 27 | } |
| mfiore | 0:d472df0659a9 | 28 | |
| mfiore | 0:d472df0659a9 | 29 | void AxedaWrapper::addKvp(const char* key, const char* value) { |
| mfiore | 0:d472df0659a9 | 30 | (*_data)[std::string(key)] = value; |
| mfiore | 0:d472df0659a9 | 31 | } |
| mfiore | 0:d472df0659a9 | 32 | |
| mfiore | 0:d472df0659a9 | 33 | void AxedaWrapper::addKvp(const std::string& key, int value) { |
| mfiore | 0:d472df0659a9 | 34 | (*_data)[key] = value; |
| mfiore | 0:d472df0659a9 | 35 | } |
| mfiore | 0:d472df0659a9 | 36 | |
| mfiore | 0:d472df0659a9 | 37 | void AxedaWrapper::addKvp(const char* key, int value) { |
| mfiore | 0:d472df0659a9 | 38 | (*_data)[std::string(key)] = value; |
| mfiore | 0:d472df0659a9 | 39 | } |
| mfiore | 0:d472df0659a9 | 40 | |
| mfiore | 0:d472df0659a9 | 41 | void AxedaWrapper::addKvp(const std::string& key, double value) { |
| mfiore | 0:d472df0659a9 | 42 | (*_data)[key] = value; |
| mfiore | 0:d472df0659a9 | 43 | } |
| mfiore | 0:d472df0659a9 | 44 | |
| mfiore | 0:d472df0659a9 | 45 | void AxedaWrapper::addKvp(const char* key, double value) { |
| mfiore | 0:d472df0659a9 | 46 | (*_data)[std::string(key)] = value; |
| mfiore | 0:d472df0659a9 | 47 | } |
| mfiore | 0:d472df0659a9 | 48 | |
| mfiore | 0:d472df0659a9 | 49 | void AxedaWrapper::addKvp(const std::string& key, bool value) { |
| mfiore | 0:d472df0659a9 | 50 | (*_data)[key] = value; |
| mfiore | 0:d472df0659a9 | 51 | } |
| mfiore | 0:d472df0659a9 | 52 | |
| mfiore | 0:d472df0659a9 | 53 | void AxedaWrapper::addKvp(const char* key, bool value) { |
| mfiore | 0:d472df0659a9 | 54 | (*_data)[std::string(key)] = value; |
| mfiore | 0:d472df0659a9 | 55 | } |
| mfiore | 0:d472df0659a9 | 56 | |
| mfiore | 0:d472df0659a9 | 57 | void AxedaWrapper::deleteAllKvp() { |
| mfiore | 0:d472df0659a9 | 58 | delete _data; |
| mfiore | 0:d472df0659a9 | 59 | _data = new MbedJSONValue(); |
| mfiore | 0:d472df0659a9 | 60 | } |
| mfiore | 0:d472df0659a9 | 61 | |
| mfiore | 0:d472df0659a9 | 62 | int AxedaWrapper::kvpMapSize() { |
| mfiore | 0:d472df0659a9 | 63 | return _data->size(); |
| mfiore | 0:d472df0659a9 | 64 | } |
| mfiore | 0:d472df0659a9 | 65 | |
| mfiore | 0:d472df0659a9 | 66 | bool AxedaWrapper::sendKvp(const std::string& key, const std::string& value) { |
| mfiore | 0:d472df0659a9 | 67 | MbedJSONValue data; |
| mfiore | 0:d472df0659a9 | 68 | data[key] = value; |
| mfiore | 0:d472df0659a9 | 69 | return sendBase(data); |
| mfiore | 0:d472df0659a9 | 70 | } |
| mfiore | 0:d472df0659a9 | 71 | |
| mfiore | 0:d472df0659a9 | 72 | bool AxedaWrapper::sendKvp(const char* key, const char* value) { |
| mfiore | 0:d472df0659a9 | 73 | MbedJSONValue data; |
| mfiore | 0:d472df0659a9 | 74 | data[key] = value; |
| mfiore | 0:d472df0659a9 | 75 | return sendBase(data); |
| mfiore | 0:d472df0659a9 | 76 | } |
| mfiore | 0:d472df0659a9 | 77 | |
| mfiore | 0:d472df0659a9 | 78 | bool AxedaWrapper::sendKvp(const std::string& key, int value) { |
| mfiore | 0:d472df0659a9 | 79 | MbedJSONValue data; |
| mfiore | 0:d472df0659a9 | 80 | data[key] = value; |
| mfiore | 0:d472df0659a9 | 81 | return sendBase(data); |
| mfiore | 0:d472df0659a9 | 82 | } |
| mfiore | 0:d472df0659a9 | 83 | |
| mfiore | 0:d472df0659a9 | 84 | bool AxedaWrapper::sendKvp(const char* key, int value) { |
| mfiore | 0:d472df0659a9 | 85 | MbedJSONValue data; |
| mfiore | 0:d472df0659a9 | 86 | data[key] = value; |
| mfiore | 0:d472df0659a9 | 87 | return sendBase(data); |
| mfiore | 0:d472df0659a9 | 88 | } |
| mfiore | 0:d472df0659a9 | 89 | |
| mfiore | 0:d472df0659a9 | 90 | bool AxedaWrapper::sendKvp(const std::string& key, double value) { |
| mfiore | 0:d472df0659a9 | 91 | MbedJSONValue data; |
| mfiore | 0:d472df0659a9 | 92 | data[key] = value; |
| mfiore | 0:d472df0659a9 | 93 | return sendBase(data); |
| mfiore | 0:d472df0659a9 | 94 | } |
| mfiore | 0:d472df0659a9 | 95 | |
| mfiore | 0:d472df0659a9 | 96 | bool AxedaWrapper::sendKvp(const char* key, double value) { |
| mfiore | 0:d472df0659a9 | 97 | MbedJSONValue data; |
| mfiore | 0:d472df0659a9 | 98 | data[key] = value; |
| mfiore | 0:d472df0659a9 | 99 | return sendBase(data); |
| mfiore | 0:d472df0659a9 | 100 | } |
| mfiore | 0:d472df0659a9 | 101 | |
| mfiore | 0:d472df0659a9 | 102 | bool AxedaWrapper::sendAllKvp(bool clear) { |
| mfiore | 0:d472df0659a9 | 103 | bool ret = sendBase(_data); |
| mfiore | 0:d472df0659a9 | 104 | if (ret && clear) { |
| mfiore | 0:d472df0659a9 | 105 | deleteAllKvp(); |
| mfiore | 0:d472df0659a9 | 106 | } |
| mfiore | 0:d472df0659a9 | 107 | return ret; |
| mfiore | 0:d472df0659a9 | 108 | } |
| mfiore | 0:d472df0659a9 | 109 | |
| mfiore | 0:d472df0659a9 | 110 | bool AxedaWrapper::sendBase(MbedJSONValue data) { |
| mfiore | 0:d472df0659a9 | 111 | int ret; |
| mfiore | 0:d472df0659a9 | 112 | char buf[512] = {0}; |
| mfiore | 0:d472df0659a9 | 113 | HTTPMap map; |
| mfiore | 0:d472df0659a9 | 114 | HTTPText text(buf, sizeof(buf)); |
| mfiore | 0:d472df0659a9 | 115 | std::string data_str = data.serialize(); |
| mfiore | 0:d472df0659a9 | 116 | |
| mfiore | 0:d472df0659a9 | 117 | map.put("data", data_str.c_str()); |
| mfiore | 0:d472df0659a9 | 118 | ret = _http_client->post(_url.c_str(), map, &text, _timeout); |
| mfiore | 0:d472df0659a9 | 119 | if (ret != HTTP_OK) { |
| mfiore | 0:d472df0659a9 | 120 | return false; |
| mfiore | 0:d472df0659a9 | 121 | } |
| mfiore | 0:d472df0659a9 | 122 | return true; |
| mfiore | 0:d472df0659a9 | 123 | } |