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@15:2610823f7f2e, 2015-04-27 (annotated)
- Committer:
- citrusbyte
- Date:
- Mon Apr 27 15:40:13 2015 +0000
- Revision:
- 15:2610823f7f2e
- Parent:
- 14:205076b587fe
- Child:
- 19:4dfa28d37b8f
Fix content length bug
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
jb8414 | 0:f479e4f4db0e | 1 | #ifndef M2XStreamClient_template_h |
jb8414 | 0:f479e4f4db0e | 2 | #define M2XStreamClient_template_h |
jb8414 | 0:f479e4f4db0e | 3 | |
jb8414 | 0:f479e4f4db0e | 4 | // Implementations of template functions |
jb8414 | 0:f479e4f4db0e | 5 | |
jb8414 | 0:f479e4f4db0e | 6 | int print_encoded_string(Print* print, const char* str); |
jb8414 | 0:f479e4f4db0e | 7 | |
jb8414 | 0:f479e4f4db0e | 8 | template <class T> |
citrusbyte | 13:0d574742208f | 9 | int M2XStreamClient::updateStreamValue(const char* deviceId, const char* streamName, T value) { |
jb8414 | 0:f479e4f4db0e | 10 | if (_client->connect(_host, _port)) { |
jb8414 | 0:f479e4f4db0e | 11 | DBGLN("%s", "Connected to M2X server!"); |
citrusbyte | 13:0d574742208f | 12 | writePutHeader(deviceId, streamName, |
citrusbyte | 15:2610823f7f2e | 13 | // for {"value":" and "} |
citrusbyte | 15:2610823f7f2e | 14 | _null_print.print(value) + 12); |
citrusbyte | 15:2610823f7f2e | 15 | _client->print("{\"value\":\""); |
jb8414 | 0:f479e4f4db0e | 16 | _client->print(value); |
citrusbyte | 15:2610823f7f2e | 17 | _client->print("\"}"); |
jb8414 | 0:f479e4f4db0e | 18 | } else { |
jb8414 | 0:f479e4f4db0e | 19 | DBGLN("%s", "ERROR: Cannot connect to M2X server!"); |
jb8414 | 0:f479e4f4db0e | 20 | return E_NOCONNECTION; |
jb8414 | 0:f479e4f4db0e | 21 | } |
jb8414 | 0:f479e4f4db0e | 22 | |
jb8414 | 0:f479e4f4db0e | 23 | return readStatusCode(true); |
jb8414 | 0:f479e4f4db0e | 24 | } |
jb8414 | 0:f479e4f4db0e | 25 | |
jb8414 | 0:f479e4f4db0e | 26 | template <class T> |
jb8414 | 0:f479e4f4db0e | 27 | inline int write_multiple_values(Print* print, int streamNum, |
jb8414 | 0:f479e4f4db0e | 28 | const char* names[], const int counts[], |
jb8414 | 0:f479e4f4db0e | 29 | const char* ats[], T values[]) { |
jb8414 | 0:f479e4f4db0e | 30 | int bytes = 0, value_index = 0; |
jb8414 | 0:f479e4f4db0e | 31 | bytes += print->print("{\"values\":{"); |
jb8414 | 0:f479e4f4db0e | 32 | for (int i = 0; i < streamNum; i++) { |
jb8414 | 0:f479e4f4db0e | 33 | bytes += print->print("\""); |
jb8414 | 0:f479e4f4db0e | 34 | bytes += print->print(names[i]); |
jb8414 | 0:f479e4f4db0e | 35 | bytes += print->print("\":["); |
jb8414 | 0:f479e4f4db0e | 36 | for (int j = 0; j < counts[i]; j++) { |
citrusbyte | 13:0d574742208f | 37 | bytes += print->print("{\"timestamp\": \""); |
citrusbyte | 10:4ce9eba38dbe | 38 | bytes += print->print(ats[value_index]); |
citrusbyte | 10:4ce9eba38dbe | 39 | bytes += print->print("\", \"value\": \""); |
jb8414 | 0:f479e4f4db0e | 40 | bytes += print->print(values[value_index]); |
jb8414 | 0:f479e4f4db0e | 41 | bytes += print->print("\"}"); |
jb8414 | 0:f479e4f4db0e | 42 | if (j < counts[i] - 1) { bytes += print->print(","); } |
jb8414 | 0:f479e4f4db0e | 43 | value_index++; |
jb8414 | 0:f479e4f4db0e | 44 | } |
jb8414 | 0:f479e4f4db0e | 45 | bytes += print->print("]"); |
jb8414 | 0:f479e4f4db0e | 46 | if (i < streamNum - 1) { bytes += print->print(","); } |
jb8414 | 0:f479e4f4db0e | 47 | } |
jb8414 | 0:f479e4f4db0e | 48 | bytes += print->print("}}"); |
jb8414 | 0:f479e4f4db0e | 49 | return bytes; |
jb8414 | 0:f479e4f4db0e | 50 | } |
jb8414 | 0:f479e4f4db0e | 51 | |
jb8414 | 0:f479e4f4db0e | 52 | template <class T> |
citrusbyte | 13:0d574742208f | 53 | int M2XStreamClient::postDeviceUpdates(const char* deviceId, int streamNum, |
citrusbyte | 13:0d574742208f | 54 | const char* names[], const int counts[], |
citrusbyte | 13:0d574742208f | 55 | const char* ats[], T values[]) { |
jb8414 | 0:f479e4f4db0e | 56 | if (_client->connect(_host, _port)) { |
jb8414 | 0:f479e4f4db0e | 57 | DBGLN("%s", "Connected to M2X server!"); |
jb8414 | 0:f479e4f4db0e | 58 | int length = write_multiple_values(&_null_print, streamNum, names, |
jb8414 | 0:f479e4f4db0e | 59 | counts, ats, values); |
NetArc | 14:205076b587fe | 60 | _client->print("POST "); |
NetArc | 14:205076b587fe | 61 | if (_path_prefix) |
NetArc | 14:205076b587fe | 62 | _client->print(_path_prefix); |
NetArc | 14:205076b587fe | 63 | _client->print("/v2/devices/"); |
citrusbyte | 13:0d574742208f | 64 | print_encoded_string(_client, deviceId); |
citrusbyte | 13:0d574742208f | 65 | _client->println("/updates HTTP/1.0"); |
jb8414 | 0:f479e4f4db0e | 66 | writeHttpHeader(length); |
jb8414 | 0:f479e4f4db0e | 67 | write_multiple_values(_client, streamNum, names, counts, ats, values); |
jb8414 | 0:f479e4f4db0e | 68 | } else { |
jb8414 | 0:f479e4f4db0e | 69 | DBGLN("%s", "ERROR: Cannot connect to M2X server!"); |
jb8414 | 0:f479e4f4db0e | 70 | return E_NOCONNECTION; |
jb8414 | 0:f479e4f4db0e | 71 | } |
jb8414 | 0:f479e4f4db0e | 72 | return readStatusCode(true); |
jb8414 | 0:f479e4f4db0e | 73 | } |
jb8414 | 0:f479e4f4db0e | 74 | |
jb8414 | 0:f479e4f4db0e | 75 | template <class T> |
jb8414 | 0:f479e4f4db0e | 76 | static int write_location_data(Print* print, const char* name, |
jb8414 | 0:f479e4f4db0e | 77 | T latitude, T longitude, |
jb8414 | 0:f479e4f4db0e | 78 | T elevation) { |
jb8414 | 0:f479e4f4db0e | 79 | int bytes = 0; |
jb8414 | 0:f479e4f4db0e | 80 | bytes += print->print("{\"name\":\""); |
jb8414 | 0:f479e4f4db0e | 81 | bytes += print->print(name); |
jb8414 | 0:f479e4f4db0e | 82 | bytes += print->print("\",\"latitude\":\""); |
jb8414 | 0:f479e4f4db0e | 83 | bytes += print->print(latitude); |
jb8414 | 0:f479e4f4db0e | 84 | bytes += print->print("\",\"longitude\":\""); |
jb8414 | 0:f479e4f4db0e | 85 | bytes += print->print(longitude); |
jb8414 | 0:f479e4f4db0e | 86 | bytes += print->print("\",\"elevation\":\""); |
jb8414 | 0:f479e4f4db0e | 87 | bytes += print->print(elevation); |
jb8414 | 0:f479e4f4db0e | 88 | bytes += print->print("\"}"); |
jb8414 | 0:f479e4f4db0e | 89 | return bytes; |
jb8414 | 0:f479e4f4db0e | 90 | } |
jb8414 | 0:f479e4f4db0e | 91 | |
jb8414 | 0:f479e4f4db0e | 92 | static int write_location_data(Print* print, const char* name, |
jb8414 | 0:f479e4f4db0e | 93 | double latitude, double longitude, |
jb8414 | 0:f479e4f4db0e | 94 | double elevation) { |
jb8414 | 0:f479e4f4db0e | 95 | int bytes = 0; |
jb8414 | 0:f479e4f4db0e | 96 | bytes += print->print("{\"name\":\""); |
jb8414 | 0:f479e4f4db0e | 97 | bytes += print->print(name); |
jb8414 | 0:f479e4f4db0e | 98 | bytes += print->print("\",\"latitude\":\""); |
jb8414 | 0:f479e4f4db0e | 99 | bytes += print->print(latitude, MAX_DOUBLE_DIGITS); |
jb8414 | 0:f479e4f4db0e | 100 | bytes += print->print("\",\"longitude\":\""); |
jb8414 | 0:f479e4f4db0e | 101 | bytes += print->print(longitude, MAX_DOUBLE_DIGITS); |
jb8414 | 0:f479e4f4db0e | 102 | bytes += print->print("\",\"elevation\":\""); |
jb8414 | 0:f479e4f4db0e | 103 | bytes += print->print(elevation); |
jb8414 | 0:f479e4f4db0e | 104 | bytes += print->print("\"}"); |
jb8414 | 0:f479e4f4db0e | 105 | return bytes; |
jb8414 | 0:f479e4f4db0e | 106 | } |
jb8414 | 0:f479e4f4db0e | 107 | |
jb8414 | 0:f479e4f4db0e | 108 | template <class T> |
citrusbyte | 13:0d574742208f | 109 | int M2XStreamClient::updateLocation(const char* deviceId, |
jb8414 | 0:f479e4f4db0e | 110 | const char* name, |
jb8414 | 0:f479e4f4db0e | 111 | T latitude, |
jb8414 | 0:f479e4f4db0e | 112 | T longitude, |
jb8414 | 0:f479e4f4db0e | 113 | T elevation) { |
jb8414 | 0:f479e4f4db0e | 114 | if (_client->connect(_host, _port)) { |
jb8414 | 0:f479e4f4db0e | 115 | DBGLN("%s", "Connected to M2X server!"); |
jb8414 | 0:f479e4f4db0e | 116 | |
jb8414 | 0:f479e4f4db0e | 117 | int length = write_location_data(&_null_print, name, latitude, longitude, |
jb8414 | 0:f479e4f4db0e | 118 | elevation); |
NetArc | 14:205076b587fe | 119 | _client->print("PUT "); |
NetArc | 14:205076b587fe | 120 | if (_path_prefix) |
NetArc | 14:205076b587fe | 121 | _client->print(_path_prefix); |
NetArc | 14:205076b587fe | 122 | _client->print("/v2/devices/"); |
citrusbyte | 13:0d574742208f | 123 | print_encoded_string(_client, deviceId); |
jb8414 | 0:f479e4f4db0e | 124 | _client->println("/location HTTP/1.0"); |
jb8414 | 0:f479e4f4db0e | 125 | |
jb8414 | 0:f479e4f4db0e | 126 | writeHttpHeader(length); |
jb8414 | 0:f479e4f4db0e | 127 | write_location_data(_client, name, latitude, longitude, elevation); |
jb8414 | 0:f479e4f4db0e | 128 | } else { |
jb8414 | 0:f479e4f4db0e | 129 | DBGLN("%s", "ERROR: Cannot connect to M2X server!"); |
jb8414 | 0:f479e4f4db0e | 130 | return E_NOCONNECTION; |
jb8414 | 0:f479e4f4db0e | 131 | } |
jb8414 | 0:f479e4f4db0e | 132 | return readStatusCode(true); |
jb8414 | 0:f479e4f4db0e | 133 | } |
jb8414 | 0:f479e4f4db0e | 134 | |
jb8414 | 0:f479e4f4db0e | 135 | #endif |