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.
Fork of M2XStreamClient by
Diff: M2XStreamClient_template.h
- Revision:
- 19:4dfa28d37b8f
- Parent:
- 15:2610823f7f2e
- Child:
- 20:32d24a6355b5
--- a/M2XStreamClient_template.h	Mon Dec 28 14:30:33 2015 +0000
+++ b/M2XStreamClient_template.h	Fri Jan 01 15:48:18 2016 +0000
@@ -73,6 +73,54 @@
 }
 
 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[value_index]);
+    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) {
    