Home Alert System
Dependencies: PWM_Tone_Library DHT
M2XStreamClient/M2XStreamClient.h@3:78f223d34f36, 2019-03-05 (annotated)
- Committer:
- ethaderu
- Date:
- Tue Mar 05 02:34:44 2019 +0000
- Revision:
- 3:78f223d34f36
Publish 1
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ethaderu | 3:78f223d34f36 | 1 | #ifndef M2XStreamClient_h |
ethaderu | 3:78f223d34f36 | 2 | #define M2XStreamClient_h |
ethaderu | 3:78f223d34f36 | 3 | |
ethaderu | 3:78f223d34f36 | 4 | #define MIN(a, b) (((a) > (b))?(b):(a)) |
ethaderu | 3:78f223d34f36 | 5 | |
ethaderu | 3:78f223d34f36 | 6 | #define MBED_PLATFORM |
ethaderu | 3:78f223d34f36 | 7 | |
ethaderu | 3:78f223d34f36 | 8 | #ifdef ARDUINO_PLATFORM |
ethaderu | 3:78f223d34f36 | 9 | #include "Arduino.h" |
ethaderu | 3:78f223d34f36 | 10 | |
ethaderu | 3:78f223d34f36 | 11 | #define USER_AGENT "User-Agent: M2X Arduino Client/2.0.2" |
ethaderu | 3:78f223d34f36 | 12 | #endif |
ethaderu | 3:78f223d34f36 | 13 | |
ethaderu | 3:78f223d34f36 | 14 | #ifdef MBED_PLATFORM |
ethaderu | 3:78f223d34f36 | 15 | #include "mbed.h" |
ethaderu | 3:78f223d34f36 | 16 | |
ethaderu | 3:78f223d34f36 | 17 | #define USER_AGENT "User-Agent: M2X Mbed Client/2.0.0" |
ethaderu | 3:78f223d34f36 | 18 | #endif |
ethaderu | 3:78f223d34f36 | 19 | |
ethaderu | 3:78f223d34f36 | 20 | #include "Client.h" |
ethaderu | 3:78f223d34f36 | 21 | #include "NullPrint.h" |
ethaderu | 3:78f223d34f36 | 22 | |
ethaderu | 3:78f223d34f36 | 23 | #ifdef DEBUG |
ethaderu | 3:78f223d34f36 | 24 | #ifdef ARDUINO_PLATFORM |
ethaderu | 3:78f223d34f36 | 25 | #define DBG(fmt_, data_) Serial.print(data_) |
ethaderu | 3:78f223d34f36 | 26 | #define DBGLN(fmt_, data_) Serial.println(data_) |
ethaderu | 3:78f223d34f36 | 27 | #define DBGLNEND Serial.println() |
ethaderu | 3:78f223d34f36 | 28 | #endif // ARDUINO_PLATFORM |
ethaderu | 3:78f223d34f36 | 29 | |
ethaderu | 3:78f223d34f36 | 30 | #ifdef MBED_PLATFORM |
ethaderu | 3:78f223d34f36 | 31 | #define DBG(fmt_, data_) printf((fmt_), (data_)) |
ethaderu | 3:78f223d34f36 | 32 | #define DBGLN(fmt_, data_) printf((fmt_), (data_)); printf("\n") |
ethaderu | 3:78f223d34f36 | 33 | #define DBGLNEND printf("\n") |
ethaderu | 3:78f223d34f36 | 34 | #endif // MBED_PLATFORM |
ethaderu | 3:78f223d34f36 | 35 | #else |
ethaderu | 3:78f223d34f36 | 36 | #define DBG(fmt_, data_) |
ethaderu | 3:78f223d34f36 | 37 | #define DBGLN(fmt_, data_) |
ethaderu | 3:78f223d34f36 | 38 | #define DBGLNEND |
ethaderu | 3:78f223d34f36 | 39 | #endif // DEBUG |
ethaderu | 3:78f223d34f36 | 40 | |
ethaderu | 3:78f223d34f36 | 41 | #define HEX(t_) ((char) (((t_) > 9) ? ((t_) - 10 + 'A') : ((t_) + '0'))) |
ethaderu | 3:78f223d34f36 | 42 | #define MAX_DOUBLE_DIGITS 7 |
ethaderu | 3:78f223d34f36 | 43 | |
ethaderu | 3:78f223d34f36 | 44 | static const int E_OK = 0; |
ethaderu | 3:78f223d34f36 | 45 | static const int E_NOCONNECTION = -1; |
ethaderu | 3:78f223d34f36 | 46 | static const int E_DISCONNECTED = -2; |
ethaderu | 3:78f223d34f36 | 47 | static const int E_NOTREACHABLE = -3; |
ethaderu | 3:78f223d34f36 | 48 | static const int E_INVALID = -4; |
ethaderu | 3:78f223d34f36 | 49 | static const int E_JSON_INVALID = -5; |
ethaderu | 3:78f223d34f36 | 50 | |
ethaderu | 3:78f223d34f36 | 51 | /* |
ethaderu | 3:78f223d34f36 | 52 | * +type+ indicates the value type: 1 for string, 2 for number |
ethaderu | 3:78f223d34f36 | 53 | * NOTE that the value type here only contains a hint on how |
ethaderu | 3:78f223d34f36 | 54 | * you can use the value. Even though 2 is returned, the value |
ethaderu | 3:78f223d34f36 | 55 | * is still stored in (const char *), and atoi/atof is needed to |
ethaderu | 3:78f223d34f36 | 56 | * get the actual value |
ethaderu | 3:78f223d34f36 | 57 | */ |
ethaderu | 3:78f223d34f36 | 58 | typedef void (*stream_value_read_callback)(const char* at, |
ethaderu | 3:78f223d34f36 | 59 | const char* value, |
ethaderu | 3:78f223d34f36 | 60 | int index, |
ethaderu | 3:78f223d34f36 | 61 | void* context, |
ethaderu | 3:78f223d34f36 | 62 | int type); |
ethaderu | 3:78f223d34f36 | 63 | |
ethaderu | 3:78f223d34f36 | 64 | typedef void (*location_read_callback)(const char* name, |
ethaderu | 3:78f223d34f36 | 65 | double latitude, |
ethaderu | 3:78f223d34f36 | 66 | double longitude, |
ethaderu | 3:78f223d34f36 | 67 | double elevation, |
ethaderu | 3:78f223d34f36 | 68 | const char* timestamp, |
ethaderu | 3:78f223d34f36 | 69 | int index, |
ethaderu | 3:78f223d34f36 | 70 | void* context); |
ethaderu | 3:78f223d34f36 | 71 | |
ethaderu | 3:78f223d34f36 | 72 | class M2XStreamClient { |
ethaderu | 3:78f223d34f36 | 73 | public: |
ethaderu | 3:78f223d34f36 | 74 | static const char* kDefaultM2XHost; |
ethaderu | 3:78f223d34f36 | 75 | static const int kDefaultM2XPort = 80; |
ethaderu | 3:78f223d34f36 | 76 | |
ethaderu | 3:78f223d34f36 | 77 | M2XStreamClient(Client* client, |
ethaderu | 3:78f223d34f36 | 78 | const char* key, |
ethaderu | 3:78f223d34f36 | 79 | int case_insensitive = 1, |
ethaderu | 3:78f223d34f36 | 80 | const char* host = kDefaultM2XHost, |
ethaderu | 3:78f223d34f36 | 81 | int port = kDefaultM2XPort, |
ethaderu | 3:78f223d34f36 | 82 | const char* path_prefix = NULL); |
ethaderu | 3:78f223d34f36 | 83 | |
ethaderu | 3:78f223d34f36 | 84 | // Push data stream value using PUT request, returns the HTTP status code |
ethaderu | 3:78f223d34f36 | 85 | template <class T> |
ethaderu | 3:78f223d34f36 | 86 | int updateStreamValue(const char* deviceId, const char* streamName, T value); |
ethaderu | 3:78f223d34f36 | 87 | |
ethaderu | 3:78f223d34f36 | 88 | // Post multiple values to M2X all at once. |
ethaderu | 3:78f223d34f36 | 89 | // +deviceId+ - id of the device to post values |
ethaderu | 3:78f223d34f36 | 90 | // +streamNum+ - Number of streams to post |
ethaderu | 3:78f223d34f36 | 91 | // +names+ - Array of stream names, the length of the array should |
ethaderu | 3:78f223d34f36 | 92 | // be exactly +streamNum+ |
ethaderu | 3:78f223d34f36 | 93 | // +counts+ - Array of +streamNum+ length, each item in this array |
ethaderu | 3:78f223d34f36 | 94 | // containing the number of values we want to post for each stream |
ethaderu | 3:78f223d34f36 | 95 | // +ats+ - Timestamps for each value, the length of this array should |
ethaderu | 3:78f223d34f36 | 96 | // be the some of all values in +counts+, for the first +counts[0]+ |
ethaderu | 3:78f223d34f36 | 97 | // items, the values belong to the first stream, for the following |
ethaderu | 3:78f223d34f36 | 98 | // +counts[1]+ number of items, the values belong to the second stream, |
ethaderu | 3:78f223d34f36 | 99 | // etc. Notice that timestamps are required here: you must provide |
ethaderu | 3:78f223d34f36 | 100 | // a timestamp for each value posted. |
ethaderu | 3:78f223d34f36 | 101 | // +values+ - Values to post. This works the same way as +ats+, the |
ethaderu | 3:78f223d34f36 | 102 | // first +counts[0]+ number of items contain values to post to the first |
ethaderu | 3:78f223d34f36 | 103 | // stream, the succeeding +counts[1]+ number of items contain values |
ethaderu | 3:78f223d34f36 | 104 | // for the second stream, etc. The length of this array should be |
ethaderu | 3:78f223d34f36 | 105 | // the sum of all values in +counts+ array. |
ethaderu | 3:78f223d34f36 | 106 | template <class T> |
ethaderu | 3:78f223d34f36 | 107 | int postDeviceUpdates(const char* deviceId, int streamNum, |
ethaderu | 3:78f223d34f36 | 108 | const char* names[], const int counts[], |
ethaderu | 3:78f223d34f36 | 109 | const char* ats[], T values[]); |
ethaderu | 3:78f223d34f36 | 110 | |
ethaderu | 3:78f223d34f36 | 111 | // Fetch values for a particular data stream. Since memory is |
ethaderu | 3:78f223d34f36 | 112 | // very limited on an Arduino, we cannot parse and get all the |
ethaderu | 3:78f223d34f36 | 113 | // data points in memory. Instead, we use callbacks here: whenever |
ethaderu | 3:78f223d34f36 | 114 | // a new data point is parsed, we call the callback using the values, |
ethaderu | 3:78f223d34f36 | 115 | // after that, the values will be thrown away to make space for new |
ethaderu | 3:78f223d34f36 | 116 | // values. |
ethaderu | 3:78f223d34f36 | 117 | // Note that you can also pass in a user-specified context in this |
ethaderu | 3:78f223d34f36 | 118 | // function, this context will be passed to the callback function |
ethaderu | 3:78f223d34f36 | 119 | // each time we get a data point. |
ethaderu | 3:78f223d34f36 | 120 | // For each data point, the callback will be called once. The HTTP |
ethaderu | 3:78f223d34f36 | 121 | // status code will be returned. And the content is only parsed when |
ethaderu | 3:78f223d34f36 | 122 | // the status code is 200. |
ethaderu | 3:78f223d34f36 | 123 | int listStreamValues(const char* deviceId, const char* streamName, |
ethaderu | 3:78f223d34f36 | 124 | stream_value_read_callback callback, void* context, |
ethaderu | 3:78f223d34f36 | 125 | const char* query = NULL); |
ethaderu | 3:78f223d34f36 | 126 | |
ethaderu | 3:78f223d34f36 | 127 | // Update datasource location |
ethaderu | 3:78f223d34f36 | 128 | // NOTE: On an Arduino Uno and other ATMEGA based boards, double has |
ethaderu | 3:78f223d34f36 | 129 | // 4-byte (32 bits) precision, which is the same as float. So there's |
ethaderu | 3:78f223d34f36 | 130 | // no natural double-precision floating number on these boards. With |
ethaderu | 3:78f223d34f36 | 131 | // a float value, we have a precision of roughly 7 digits, that means |
ethaderu | 3:78f223d34f36 | 132 | // either 5 or 6 digits after the floating point. According to wikipedia, |
ethaderu | 3:78f223d34f36 | 133 | // a difference of 0.00001 will give us ~1.1132m distance. If this |
ethaderu | 3:78f223d34f36 | 134 | // precision is good for you, you can use the double-version we provided |
ethaderu | 3:78f223d34f36 | 135 | // here. Otherwise, you may need to use the string-version and do the |
ethaderu | 3:78f223d34f36 | 136 | // actual conversion by yourselves. |
ethaderu | 3:78f223d34f36 | 137 | // However, with an Arduino Due board, double has 8-bytes (64 bits) |
ethaderu | 3:78f223d34f36 | 138 | // precision, which means you are free to use the double-version only |
ethaderu | 3:78f223d34f36 | 139 | // without any precision problems. |
ethaderu | 3:78f223d34f36 | 140 | // Returned value is the http status code. |
ethaderu | 3:78f223d34f36 | 141 | template <class T> |
ethaderu | 3:78f223d34f36 | 142 | int updateLocation(const char* deviceId, const char* name, |
ethaderu | 3:78f223d34f36 | 143 | T latitude, T longitude, T elevation); |
ethaderu | 3:78f223d34f36 | 144 | |
ethaderu | 3:78f223d34f36 | 145 | // Read location information for a device. Also used callback to process |
ethaderu | 3:78f223d34f36 | 146 | // data points for memory reasons. The HTTP status code is returned, |
ethaderu | 3:78f223d34f36 | 147 | // response is only parsed when the HTTP status code is 200 |
ethaderu | 3:78f223d34f36 | 148 | int readLocation(const char* deviceId, location_read_callback callback, |
ethaderu | 3:78f223d34f36 | 149 | void* context); |
ethaderu | 3:78f223d34f36 | 150 | |
ethaderu | 3:78f223d34f36 | 151 | // Delete values from a data stream |
ethaderu | 3:78f223d34f36 | 152 | // You will need to provide from and end date/time strings in the ISO8601 |
ethaderu | 3:78f223d34f36 | 153 | // format "yyyy-mm-ddTHH:MM:SS.SSSZ" where |
ethaderu | 3:78f223d34f36 | 154 | // yyyy: the year |
ethaderu | 3:78f223d34f36 | 155 | // mm: the month |
ethaderu | 3:78f223d34f36 | 156 | // dd: the day |
ethaderu | 3:78f223d34f36 | 157 | // HH: the hour (24 hour format) |
ethaderu | 3:78f223d34f36 | 158 | // MM: the minute |
ethaderu | 3:78f223d34f36 | 159 | // SS.SSS: the seconds (to the millisecond) |
ethaderu | 3:78f223d34f36 | 160 | // NOTE: the time is given in Zulu (GMT) |
ethaderu | 3:78f223d34f36 | 161 | // M2X will delete all values within the from to end date/time range. |
ethaderu | 3:78f223d34f36 | 162 | // The status code is 204 on success and 400 on a bad request (e.g. the |
ethaderu | 3:78f223d34f36 | 163 | // timestamp is not in ISO8601 format or the from timestamp is not less than |
ethaderu | 3:78f223d34f36 | 164 | // or equal to the end timestamp. |
ethaderu | 3:78f223d34f36 | 165 | int deleteValues(const char* deviceId, const char* streamName, |
ethaderu | 3:78f223d34f36 | 166 | const char* from, const char* end); |
ethaderu | 3:78f223d34f36 | 167 | private: |
ethaderu | 3:78f223d34f36 | 168 | Client* _client; |
ethaderu | 3:78f223d34f36 | 169 | const char* _key; |
ethaderu | 3:78f223d34f36 | 170 | int _case_insensitive; |
ethaderu | 3:78f223d34f36 | 171 | const char* _host; |
ethaderu | 3:78f223d34f36 | 172 | int _port; |
ethaderu | 3:78f223d34f36 | 173 | const char* _path_prefix; |
ethaderu | 3:78f223d34f36 | 174 | NullPrint _null_print; |
ethaderu | 3:78f223d34f36 | 175 | |
ethaderu | 3:78f223d34f36 | 176 | // Writes the HTTP header part for updating a stream value |
ethaderu | 3:78f223d34f36 | 177 | void writePutHeader(const char* deviceId, |
ethaderu | 3:78f223d34f36 | 178 | const char* streamName, |
ethaderu | 3:78f223d34f36 | 179 | int contentLength); |
ethaderu | 3:78f223d34f36 | 180 | // Writes the HTTP header part for deleting stream values |
ethaderu | 3:78f223d34f36 | 181 | void writeDeleteHeader(const char* deviceId, |
ethaderu | 3:78f223d34f36 | 182 | const char* streamName, |
ethaderu | 3:78f223d34f36 | 183 | int contentLength); |
ethaderu | 3:78f223d34f36 | 184 | // Writes HTTP header lines including M2X API Key, host, content |
ethaderu | 3:78f223d34f36 | 185 | // type and content length(if the body exists) |
ethaderu | 3:78f223d34f36 | 186 | void writeHttpHeader(int contentLength); |
ethaderu | 3:78f223d34f36 | 187 | // Parses HTTP response header and return the content length. |
ethaderu | 3:78f223d34f36 | 188 | // Note that this function does not parse all http headers, as long |
ethaderu | 3:78f223d34f36 | 189 | // as the content length is found, this function will return |
ethaderu | 3:78f223d34f36 | 190 | int readContentLength(); |
ethaderu | 3:78f223d34f36 | 191 | // Skips all HTTP response header part. Return minus value in case |
ethaderu | 3:78f223d34f36 | 192 | // the connection is closed before we got all headers |
ethaderu | 3:78f223d34f36 | 193 | int skipHttpHeader(); |
ethaderu | 3:78f223d34f36 | 194 | // Parses and returns the HTTP status code, note this function will |
ethaderu | 3:78f223d34f36 | 195 | // return immediately once it gets the status code |
ethaderu | 3:78f223d34f36 | 196 | int readStatusCode(bool closeClient); |
ethaderu | 3:78f223d34f36 | 197 | // Waits for a certain string pattern in the HTTP header, and returns |
ethaderu | 3:78f223d34f36 | 198 | // once the pattern is found. In the pattern, you can use '*' to denote |
ethaderu | 3:78f223d34f36 | 199 | // any character |
ethaderu | 3:78f223d34f36 | 200 | int waitForString(const char* str); |
ethaderu | 3:78f223d34f36 | 201 | // Closes the connection |
ethaderu | 3:78f223d34f36 | 202 | void close(); |
ethaderu | 3:78f223d34f36 | 203 | // Parses JSON response of stream value API, and calls callback function |
ethaderu | 3:78f223d34f36 | 204 | // once we get a data point |
ethaderu | 3:78f223d34f36 | 205 | int readStreamValue(stream_value_read_callback callback, void* context); |
ethaderu | 3:78f223d34f36 | 206 | // Parses JSON response of location API, and calls callback function once |
ethaderu | 3:78f223d34f36 | 207 | // we get a data point |
ethaderu | 3:78f223d34f36 | 208 | int readLocation(location_read_callback callback, void* context); |
ethaderu | 3:78f223d34f36 | 209 | }; |
ethaderu | 3:78f223d34f36 | 210 | |
ethaderu | 3:78f223d34f36 | 211 | #include "M2XStreamClient_template.h" |
ethaderu | 3:78f223d34f36 | 212 | |
ethaderu | 3:78f223d34f36 | 213 | #endif /* M2XStreamClient_h */ |