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: WNCInterface_M2Xdemo ATT_WNCInterface_Info WNCInterface_HTTP_example Public_IoT_M2X_Cellular_Demo
Fork of M2XStreamClient by
Diff: M2XStreamClient_template.h
- Revision:
- 22:4d895e732765
- Parent:
- 21:6878944d2ce2
- Child:
- 23:f32837239193
--- a/M2XStreamClient_template.h Sat Jan 02 02:29:43 2016 +0000
+++ /dev/null Thu Jan 01 00:00:00 1970 +0000
@@ -1,183 +0,0 @@
-#ifndef M2XStreamClient_template_h
-#define M2XStreamClient_template_h
-
-// Implementations of template functions
-
-int print_encoded_string(Print* print, const char* str);
-
-template <class T>
-int M2XStreamClient::updateStreamValue(const char* deviceId, const char* streamName, T value) {
- if (_client->connect(_host, _port)) {
- DBGLN("%s", "Connected to M2X server!");
- writePutHeader(deviceId, streamName,
- // for {"value":" and "}
- _null_print.print(value) + 12);
- _client->print("{\"value\":\"");
- _client->print(value);
- _client->print("\"}");
- } else {
- DBGLN("%s", "ERROR: Cannot connect to M2X server!");
- return E_NOCONNECTION;
- }
-
- return readStatusCode(true);
-}
-
-template <class T>
-inline int write_multiple_values(Print* print, int streamNum,
- const char* names[], const int counts[],
- const char* ats[], T values[]) {
- int bytes = 0, value_index = 0;
- bytes += print->print("{\"values\":{");
- for (int i = 0; i < streamNum; i++) {
- bytes += print->print("\"");
- bytes += print->print(names[i]);
- bytes += print->print("\":[");
- for (int j = 0; j < counts[i]; j++) {
- bytes += print->print("{\"timestamp\": \"");
- bytes += print->print(ats[value_index]);
- bytes += print->print("\", \"value\": \"");
- bytes += print->print(values[value_index]);
- bytes += print->print("\"}");
- if (j < counts[i] - 1) { bytes += print->print(","); }
- value_index++;
- }
- bytes += print->print("]");
- if (i < streamNum - 1) { bytes += print->print(","); }
- }
- bytes += print->print("}}");
- return bytes;
-}
-
-template <class T>
-int M2XStreamClient::postDeviceUpdates(const char* deviceId, int streamNum,
- const char* names[], const int counts[],
- const char* ats[], T values[]) {
- if (_client->connect(_host, _port)) {
- DBGLN("%s", "Connected to M2X server!");
- int length = write_multiple_values(&_null_print, streamNum, names,
- counts, ats, values);
- _client->print("POST ");
- if (_path_prefix)
- _client->print(_path_prefix);
- _client->print("/v2/devices/");
- print_encoded_string(_client, deviceId);
- _client->println("/updates HTTP/1.0");
- writeHttpHeader(length);
- write_multiple_values(_client, streamNum, names, counts, ats, values);
- } else {
- DBGLN("%s", "ERROR: Cannot connect to M2X server!");
- return E_NOCONNECTION;
- }
- return readStatusCode(true);
-}
-
-template <class T>
-inline int write_single_device_values(Print* print, int streamNum,
- const char* names[], T values[],
- const char* at) {
- int bytes = 0;
- bytes += print->print("{\"values\":{");
- for (int i = 0; i < streamNum; i++) {
- bytes += print->print("\"");
- bytes += print->print(names[i]);
- bytes += print->print("\": \"");
- bytes += print->print(values[i]);
- bytes += print->print("\"");
- if (i < streamNum - 1) { bytes += print->print(","); }
- }
- bytes += print->print("}");
- if (at != NULL) {
- bytes += print->print(",\"timestamp\":\"");
- bytes += print->print(at);
- bytes += print->print("\"");
- }
- bytes += print->print("}");
- return bytes;
-}
-
-template <class T>
-int M2XStreamClient::postSingleDeviceUpdate(const char* deviceId, int streamNum,
- const char* names[], T values[],
- const char* at) {
- if (_client->connect(_host, _port)) {
- DBGLN("%s", "Connected to M2X server!");
- int length = write_single_device_values(&_null_print, streamNum, names,
- values, at);
- _client->print("POST ");
- if (_path_prefix) {
- _client->print(_path_prefix);
- }
- _client->print("/v2/devices/");
- print_encoded_string(_client, deviceId);
- _client->println("/update HTTP/1.0");
- writeHttpHeader(length);
- write_single_device_values(_client, streamNum, names, values, at);
- } else {
- DBGLN("%s", "ERROR: Cannot connect to M2X server!");
- return E_NOCONNECTION;
- }
- return readStatusCode(true);
-}
-
-template <class T>
-static int write_location_data(Print* print, const char* name,
- T latitude, T longitude,
- T elevation) {
- int bytes = 0;
- bytes += print->print("{\"name\":\"");
- bytes += print->print(name);
- bytes += print->print("\",\"latitude\":\"");
- bytes += print->print(latitude);
- bytes += print->print("\",\"longitude\":\"");
- bytes += print->print(longitude);
- bytes += print->print("\",\"elevation\":\"");
- bytes += print->print(elevation);
- bytes += print->print("\"}");
- return bytes;
-}
-
-static int write_location_data(Print* print, const char* name,
- double latitude, double longitude,
- double elevation) {
- int bytes = 0;
- bytes += print->print("{\"name\":\"");
- bytes += print->print(name);
- bytes += print->print("\",\"latitude\":\"");
- bytes += print->print(latitude, MAX_DOUBLE_DIGITS);
- bytes += print->print("\",\"longitude\":\"");
- bytes += print->print(longitude, MAX_DOUBLE_DIGITS);
- bytes += print->print("\",\"elevation\":\"");
- bytes += print->print(elevation);
- bytes += print->print("\"}");
- return bytes;
-}
-
-template <class T>
-int M2XStreamClient::updateLocation(const char* deviceId,
- const char* name,
- T latitude,
- T longitude,
- T elevation) {
- if (_client->connect(_host, _port)) {
- DBGLN("%s", "Connected to M2X server!");
-
- int length = write_location_data(&_null_print, name, latitude, longitude,
- elevation);
- _client->print("PUT ");
- if (_path_prefix)
- _client->print(_path_prefix);
- _client->print("/v2/devices/");
- print_encoded_string(_client, deviceId);
- _client->println("/location HTTP/1.0");
-
- writeHttpHeader(length);
- write_location_data(_client, name, latitude, longitude, elevation);
- } else {
- DBGLN("%s", "ERROR: Cannot connect to M2X server!");
- return E_NOCONNECTION;
- }
- return readStatusCode(true);
-}
-
-#endif
