ARM mbed M2X API Client: The ARM mbed client library is used to send/receive data to/from AT&T's M2X service from mbed LPC1768 microcontrollers.
Dependents: m2x-demo-all M2X_MTS_ACCEL_DEMO M2X_MTS_Accel M2X_K64F_ACCEL ... more
M2XStreamClient_template.h@6:e6d66d99dd6f, 2013-11-14 (annotated)
- Committer:
- citrusbyte
- Date:
- Thu Nov 14 15:12:51 2013 +0000
- Revision:
- 6:e6d66d99dd6f
- Child:
- 9:48b088e317aa
Update m2x client library
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
citrusbyte | 6:e6d66d99dd6f | 1 | #ifndef M2XStreamClient_template_h |
citrusbyte | 6:e6d66d99dd6f | 2 | #define M2XStreamClient_template_h |
citrusbyte | 6:e6d66d99dd6f | 3 | |
citrusbyte | 6:e6d66d99dd6f | 4 | // Implementations of template functions |
citrusbyte | 6:e6d66d99dd6f | 5 | |
citrusbyte | 6:e6d66d99dd6f | 6 | int print_encoded_string(Print* print, const char* str); |
citrusbyte | 6:e6d66d99dd6f | 7 | |
citrusbyte | 6:e6d66d99dd6f | 8 | template <class T> |
citrusbyte | 6:e6d66d99dd6f | 9 | int M2XStreamClient::post(const char* feedId, const char* streamName, T value) { |
citrusbyte | 6:e6d66d99dd6f | 10 | if (_client->connect(_host, _port)) { |
citrusbyte | 6:e6d66d99dd6f | 11 | DBGLN("%s", "Connected to M2X server!"); |
citrusbyte | 6:e6d66d99dd6f | 12 | writePostHeader(feedId, streamName, |
citrusbyte | 6:e6d66d99dd6f | 13 | // for {"value": and } |
citrusbyte | 6:e6d66d99dd6f | 14 | _null_print.print(value) + 10); |
citrusbyte | 6:e6d66d99dd6f | 15 | _client->print("{\"value\":"); |
citrusbyte | 6:e6d66d99dd6f | 16 | _client->print(value); |
citrusbyte | 6:e6d66d99dd6f | 17 | _client->print("}"); |
citrusbyte | 6:e6d66d99dd6f | 18 | } else { |
citrusbyte | 6:e6d66d99dd6f | 19 | DBGLN("%s", "ERROR: Cannot connect to M2X server!"); |
citrusbyte | 6:e6d66d99dd6f | 20 | return E_NOCONNECTION; |
citrusbyte | 6:e6d66d99dd6f | 21 | } |
citrusbyte | 6:e6d66d99dd6f | 22 | |
citrusbyte | 6:e6d66d99dd6f | 23 | return readStatusCode(true); |
citrusbyte | 6:e6d66d99dd6f | 24 | } |
citrusbyte | 6:e6d66d99dd6f | 25 | |
citrusbyte | 6:e6d66d99dd6f | 26 | template <class T> |
citrusbyte | 6:e6d66d99dd6f | 27 | inline int write_multiple_values(Print* print, int streamNum, |
citrusbyte | 6:e6d66d99dd6f | 28 | const char* names[], const int counts[], |
citrusbyte | 6:e6d66d99dd6f | 29 | const char* ats[], T values[]) { |
citrusbyte | 6:e6d66d99dd6f | 30 | int bytes = 0, value_index = 0; |
citrusbyte | 6:e6d66d99dd6f | 31 | bytes += print->print("{\"values\":{"); |
citrusbyte | 6:e6d66d99dd6f | 32 | for (int i = 0; i < streamNum; i++) { |
citrusbyte | 6:e6d66d99dd6f | 33 | bytes += print->print("\""); |
citrusbyte | 6:e6d66d99dd6f | 34 | bytes += print->print(names[i]); |
citrusbyte | 6:e6d66d99dd6f | 35 | bytes += print->print("\":["); |
citrusbyte | 6:e6d66d99dd6f | 36 | for (int j = 0; j < counts[i]; j++) { |
citrusbyte | 6:e6d66d99dd6f | 37 | bytes += print->print("{"); |
citrusbyte | 6:e6d66d99dd6f | 38 | if (ats && ats[value_index]) { |
citrusbyte | 6:e6d66d99dd6f | 39 | bytes += print->print("\"at\": \""); |
citrusbyte | 6:e6d66d99dd6f | 40 | bytes += print->print(ats[value_index]); |
citrusbyte | 6:e6d66d99dd6f | 41 | bytes += print->print("\","); |
citrusbyte | 6:e6d66d99dd6f | 42 | } |
citrusbyte | 6:e6d66d99dd6f | 43 | bytes += print->print("\"value\": \""); |
citrusbyte | 6:e6d66d99dd6f | 44 | bytes += print->print(values[value_index]); |
citrusbyte | 6:e6d66d99dd6f | 45 | bytes += print->print("\"}"); |
citrusbyte | 6:e6d66d99dd6f | 46 | if (j < counts[i] - 1) { bytes += print->print(","); } |
citrusbyte | 6:e6d66d99dd6f | 47 | value_index++; |
citrusbyte | 6:e6d66d99dd6f | 48 | } |
citrusbyte | 6:e6d66d99dd6f | 49 | bytes += print->print("]"); |
citrusbyte | 6:e6d66d99dd6f | 50 | if (i < streamNum - 1) { bytes += print->print(","); } |
citrusbyte | 6:e6d66d99dd6f | 51 | } |
citrusbyte | 6:e6d66d99dd6f | 52 | bytes += print->print("}}"); |
citrusbyte | 6:e6d66d99dd6f | 53 | return bytes; |
citrusbyte | 6:e6d66d99dd6f | 54 | } |
citrusbyte | 6:e6d66d99dd6f | 55 | |
citrusbyte | 6:e6d66d99dd6f | 56 | template <class T> |
citrusbyte | 6:e6d66d99dd6f | 57 | int M2XStreamClient::postMultiple(const char* feedId, int streamNum, |
citrusbyte | 6:e6d66d99dd6f | 58 | const char* names[], const int counts[], |
citrusbyte | 6:e6d66d99dd6f | 59 | const char* ats[], T values[]) { |
citrusbyte | 6:e6d66d99dd6f | 60 | if (_client->connect(_host, _port)) { |
citrusbyte | 6:e6d66d99dd6f | 61 | DBGLN("%s", "Connected to M2X server!"); |
citrusbyte | 6:e6d66d99dd6f | 62 | int length = write_multiple_values(&_null_print, streamNum, names, |
citrusbyte | 6:e6d66d99dd6f | 63 | counts, ats, values); |
citrusbyte | 6:e6d66d99dd6f | 64 | _client->print("POST /v1/feeds/"); |
citrusbyte | 6:e6d66d99dd6f | 65 | print_encoded_string(_client, feedId); |
citrusbyte | 6:e6d66d99dd6f | 66 | _client->println(" HTTP/1.0"); |
citrusbyte | 6:e6d66d99dd6f | 67 | writeHttpHeader(length); |
citrusbyte | 6:e6d66d99dd6f | 68 | write_multiple_values(_client, streamNum, names, counts, ats, values); |
citrusbyte | 6:e6d66d99dd6f | 69 | } else { |
citrusbyte | 6:e6d66d99dd6f | 70 | DBGLN("%s", "ERROR: Cannot connect to M2X server!"); |
citrusbyte | 6:e6d66d99dd6f | 71 | return E_NOCONNECTION; |
citrusbyte | 6:e6d66d99dd6f | 72 | } |
citrusbyte | 6:e6d66d99dd6f | 73 | return readStatusCode(true); |
citrusbyte | 6:e6d66d99dd6f | 74 | } |
citrusbyte | 6:e6d66d99dd6f | 75 | |
citrusbyte | 6:e6d66d99dd6f | 76 | template <class T> |
citrusbyte | 6:e6d66d99dd6f | 77 | static int write_location_data(Print* print, const char* name, |
citrusbyte | 6:e6d66d99dd6f | 78 | T latitude, T longitude, |
citrusbyte | 6:e6d66d99dd6f | 79 | T elevation) { |
citrusbyte | 6:e6d66d99dd6f | 80 | int bytes = 0; |
citrusbyte | 6:e6d66d99dd6f | 81 | bytes += print->print("{\"name\":\""); |
citrusbyte | 6:e6d66d99dd6f | 82 | bytes += print->print(name); |
citrusbyte | 6:e6d66d99dd6f | 83 | bytes += print->print("\",\"latitude\":\""); |
citrusbyte | 6:e6d66d99dd6f | 84 | bytes += print->print(latitude); |
citrusbyte | 6:e6d66d99dd6f | 85 | bytes += print->print("\",\"longitude\":\""); |
citrusbyte | 6:e6d66d99dd6f | 86 | bytes += print->print(longitude); |
citrusbyte | 6:e6d66d99dd6f | 87 | bytes += print->print("\",\"elevation\":\""); |
citrusbyte | 6:e6d66d99dd6f | 88 | bytes += print->print(elevation); |
citrusbyte | 6:e6d66d99dd6f | 89 | bytes += print->print("\"}"); |
citrusbyte | 6:e6d66d99dd6f | 90 | return bytes; |
citrusbyte | 6:e6d66d99dd6f | 91 | } |
citrusbyte | 6:e6d66d99dd6f | 92 | |
citrusbyte | 6:e6d66d99dd6f | 93 | static int write_location_data(Print* print, const char* name, |
citrusbyte | 6:e6d66d99dd6f | 94 | double latitude, double longitude, |
citrusbyte | 6:e6d66d99dd6f | 95 | double elevation) { |
citrusbyte | 6:e6d66d99dd6f | 96 | int bytes = 0; |
citrusbyte | 6:e6d66d99dd6f | 97 | bytes += print->print("{\"name\":\""); |
citrusbyte | 6:e6d66d99dd6f | 98 | bytes += print->print(name); |
citrusbyte | 6:e6d66d99dd6f | 99 | bytes += print->print("\",\"latitude\":\""); |
citrusbyte | 6:e6d66d99dd6f | 100 | bytes += print->print(latitude, MAX_DOUBLE_DIGITS); |
citrusbyte | 6:e6d66d99dd6f | 101 | bytes += print->print("\",\"longitude\":\""); |
citrusbyte | 6:e6d66d99dd6f | 102 | bytes += print->print(longitude, MAX_DOUBLE_DIGITS); |
citrusbyte | 6:e6d66d99dd6f | 103 | bytes += print->print("\",\"elevation\":\""); |
citrusbyte | 6:e6d66d99dd6f | 104 | bytes += print->print(elevation); |
citrusbyte | 6:e6d66d99dd6f | 105 | bytes += print->print("\"}"); |
citrusbyte | 6:e6d66d99dd6f | 106 | return bytes; |
citrusbyte | 6:e6d66d99dd6f | 107 | } |
citrusbyte | 6:e6d66d99dd6f | 108 | |
citrusbyte | 6:e6d66d99dd6f | 109 | template <class T> |
citrusbyte | 6:e6d66d99dd6f | 110 | int M2XStreamClient::updateLocation(const char* feedId, |
citrusbyte | 6:e6d66d99dd6f | 111 | const char* name, |
citrusbyte | 6:e6d66d99dd6f | 112 | T latitude, |
citrusbyte | 6:e6d66d99dd6f | 113 | T longitude, |
citrusbyte | 6:e6d66d99dd6f | 114 | T elevation) { |
citrusbyte | 6:e6d66d99dd6f | 115 | if (_client->connect(_host, _port)) { |
citrusbyte | 6:e6d66d99dd6f | 116 | DBGLN("%s", "Connected to M2X server!"); |
citrusbyte | 6:e6d66d99dd6f | 117 | |
citrusbyte | 6:e6d66d99dd6f | 118 | int length = write_location_data(&_null_print, name, latitude, longitude, |
citrusbyte | 6:e6d66d99dd6f | 119 | elevation); |
citrusbyte | 6:e6d66d99dd6f | 120 | _client->print("PUT /v1/feeds/"); |
citrusbyte | 6:e6d66d99dd6f | 121 | print_encoded_string(_client, feedId); |
citrusbyte | 6:e6d66d99dd6f | 122 | _client->println("/location HTTP/1.0"); |
citrusbyte | 6:e6d66d99dd6f | 123 | |
citrusbyte | 6:e6d66d99dd6f | 124 | writeHttpHeader(length); |
citrusbyte | 6:e6d66d99dd6f | 125 | write_location_data(_client, name, latitude, longitude, elevation); |
citrusbyte | 6:e6d66d99dd6f | 126 | } else { |
citrusbyte | 6:e6d66d99dd6f | 127 | DBGLN("%s", "ERROR: Cannot connect to M2X server!"); |
citrusbyte | 6:e6d66d99dd6f | 128 | return E_NOCONNECTION; |
citrusbyte | 6:e6d66d99dd6f | 129 | } |
citrusbyte | 6:e6d66d99dd6f | 130 | return readStatusCode(true); |
citrusbyte | 6:e6d66d99dd6f | 131 | } |
citrusbyte | 6:e6d66d99dd6f | 132 | |
citrusbyte | 6:e6d66d99dd6f | 133 | #endif |
citrusbyte | 6:e6d66d99dd6f | 134 |