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.h@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 | #ifndef AXEDAWRAPPER_H |
mfiore | 0:d472df0659a9 | 2 | #define AXEDAWRAPPER_H |
mfiore | 0:d472df0659a9 | 3 | |
mfiore | 0:d472df0659a9 | 4 | #include "HTTPClient.h" |
mfiore | 0:d472df0659a9 | 5 | #include "MbedJSONValue.h" |
mfiore | 0:d472df0659a9 | 6 | |
mfiore | 0:d472df0659a9 | 7 | #define MAX_KVP 32 |
mfiore | 0:d472df0659a9 | 8 | namespace mts { |
mfiore | 0:d472df0659a9 | 9 | |
mfiore | 0:d472df0659a9 | 10 | class AxedaWrapper { |
mfiore | 0:d472df0659a9 | 11 | public: |
mfiore | 0:d472df0659a9 | 12 | /** Constructor |
mfiore | 0:d472df0659a9 | 13 | * Creates an AxedaWrapper object |
mfiore | 0:d472df0659a9 | 14 | * |
mfiore | 0:d472df0659a9 | 15 | * @param serial A made up serial number for your device. It needs to be unique! |
mfiore | 0:d472df0659a9 | 16 | * A good format to use is <your initials>-<a random string of numbers>, |
mfiore | 0:d472df0659a9 | 17 | * e.g. "JTA-862653" |
mfiore | 0:d472df0659a9 | 18 | */ |
mfiore | 0:d472df0659a9 | 19 | AxedaWrapper(const std::string& serial, const std::string& model = "mbed", const std::string& host = "dev6-connect.axeda.com", int port = 52689); |
mfiore | 0:d472df0659a9 | 20 | |
mfiore | 0:d472df0659a9 | 21 | /** Destructor |
mfiore | 0:d472df0659a9 | 22 | * Deletes the AxedaWrapper object and returns all allocated memory |
mfiore | 0:d472df0659a9 | 23 | */ |
mfiore | 0:d472df0659a9 | 24 | ~AxedaWrapper(); |
mfiore | 0:d472df0659a9 | 25 | |
mfiore | 2:99baa98f84a3 | 26 | /** push |
mfiore | 0:d472df0659a9 | 27 | * Adds a key-value pair to the internal map. |
mfiore | 0:d472df0659a9 | 28 | * |
mfiore | 0:d472df0659a9 | 29 | * @param key The "name" of the value. |
mfiore | 0:d472df0659a9 | 30 | * @param value The value to be stored. |
mfiore | 0:d472df0659a9 | 31 | */ |
mfiore | 2:99baa98f84a3 | 32 | void push(const std::string& key, const std::string& value); |
mfiore | 2:99baa98f84a3 | 33 | void push(const char* key, const char* value); |
mfiore | 2:99baa98f84a3 | 34 | void push(const std::string& key, int value); |
mfiore | 2:99baa98f84a3 | 35 | void push(const char* key, int value); |
mfiore | 2:99baa98f84a3 | 36 | void push(const std::string& key, double value); |
mfiore | 2:99baa98f84a3 | 37 | void push(const char* key, double value); |
mfiore | 2:99baa98f84a3 | 38 | void push(const std::string& key, bool value); |
mfiore | 2:99baa98f84a3 | 39 | void push(const char* key, bool value); |
mfiore | 0:d472df0659a9 | 40 | |
mfiore | 2:99baa98f84a3 | 41 | /** toString |
mfiore | 2:99baa98f84a3 | 42 | * Returns a std::string containing the serialized json contents of the internal map |
mfiore | 2:99baa98f84a3 | 43 | * If the internal map is empty, "{}" will be returned |
mfiore | 2:99baa98f84a3 | 44 | */ |
mfiore | 2:99baa98f84a3 | 45 | std::string toString(); |
mfiore | 2:99baa98f84a3 | 46 | |
mfiore | 2:99baa98f84a3 | 47 | /** clear |
mfiore | 0:d472df0659a9 | 48 | * Removes all key-value pairs from the internal map. |
mfiore | 0:d472df0659a9 | 49 | * If the internal map is empty, no action is taken. |
mfiore | 0:d472df0659a9 | 50 | */ |
mfiore | 2:99baa98f84a3 | 51 | void clear(); |
mfiore | 0:d472df0659a9 | 52 | |
mfiore | 2:99baa98f84a3 | 53 | /** size |
mfiore | 0:d472df0659a9 | 54 | * @returns the current size of the map |
mfiore | 0:d472df0659a9 | 55 | */ |
mfiore | 2:99baa98f84a3 | 56 | int size(); |
mfiore | 0:d472df0659a9 | 57 | |
mfiore | 2:99baa98f84a3 | 58 | /** send |
mfiore | 0:d472df0659a9 | 59 | * Sends a key-value pair to the Axeda platform |
mfiore | 0:d472df0659a9 | 60 | * |
mfiore | 0:d472df0659a9 | 61 | * @param key The "name" of the value. |
mfiore | 0:d472df0659a9 | 62 | * @param value The value to be sent. |
mfiore | 0:d472df0659a9 | 63 | * @return true if the value was sent successfully, false otherwise |
mfiore | 0:d472df0659a9 | 64 | */ |
mfiore | 2:99baa98f84a3 | 65 | bool send(const std::string& key, const std::string& value); |
mfiore | 2:99baa98f84a3 | 66 | bool send(const char* key, const char* value); |
mfiore | 2:99baa98f84a3 | 67 | bool send(const std::string& key, int value); |
mfiore | 2:99baa98f84a3 | 68 | bool send(const char* key, int value); |
mfiore | 2:99baa98f84a3 | 69 | bool send(const std::string& key, double value); |
mfiore | 2:99baa98f84a3 | 70 | bool send(const char* key, double value); |
mfiore | 2:99baa98f84a3 | 71 | bool send(const std::string& key, bool value); |
mfiore | 2:99baa98f84a3 | 72 | bool send(const char* key, bool value); |
mfiore | 0:d472df0659a9 | 73 | |
mfiore | 2:99baa98f84a3 | 74 | /** sendAll |
mfiore | 0:d472df0659a9 | 75 | * Sends all the key-value pairs in the internal map to the Axeda platform |
mfiore | 0:d472df0659a9 | 76 | * |
mfiore | 0:d472df0659a9 | 77 | * @param clear If true, clear the internal map if the send is successful, |
mfiore | 0:d472df0659a9 | 78 | * if false, don't clear the internal map |
mfiore | 0:d472df0659a9 | 79 | * @return true if successful, false otherwise |
mfiore | 0:d472df0659a9 | 80 | */ |
mfiore | 2:99baa98f84a3 | 81 | bool sendAll(bool clean = true); |
mfiore | 0:d472df0659a9 | 82 | |
mfiore | 0:d472df0659a9 | 83 | private: |
mfiore | 1:d42aaf6f2e19 | 84 | bool sendBase(const std::string& data); |
mfiore | 0:d472df0659a9 | 85 | |
mfiore | 0:d472df0659a9 | 86 | HTTPClient* _http_client; |
mfiore | 0:d472df0659a9 | 87 | std::string _url; |
mfiore | 0:d472df0659a9 | 88 | MbedJSONValue* _data; |
mfiore | 0:d472df0659a9 | 89 | int _timeout; |
mfiore | 0:d472df0659a9 | 90 | }; |
mfiore | 0:d472df0659a9 | 91 | |
mfiore | 0:d472df0659a9 | 92 | } |
mfiore | 0:d472df0659a9 | 93 | |
mfiore | 0:d472df0659a9 | 94 | #endif |