Jim Flynn / M2XStreamClient-JMF

Dependents:   WNCInterface_M2Xdemo ATT_WNCInterface_Info WNCInterface_HTTP_example Public_IoT_M2X_Cellular_Demo

Fork of M2XStreamClient by AT&T M2X Team

Files at this revision

API Documentation at this revision

Comitter:
citrusbyte
Date:
Tue Dec 09 14:36:16 2014 +0000
Parent:
12:debf4b2f7960
Child:
14:205076b587fe
Commit message:
Update client library to latest version

Changed in this revision

M2XStreamClient.cpp Show annotated file Show diff for this revision Revisions of this file
M2XStreamClient.h Show annotated file Show diff for this revision Revisions of this file
M2XStreamClient_template.h Show annotated file Show diff for this revision Revisions of this file
StreamParseFunctions.h Show annotated file Show diff for this revision Revisions of this file
--- a/M2XStreamClient.cpp	Fri Sep 26 09:35:10 2014 +0000
+++ b/M2XStreamClient.cpp	Tue Dec 09 14:36:16 2014 +0000
@@ -37,36 +37,23 @@
                                              _null_print() {
 }
 
-#define WRITE_QUERY_PART(client_, started_, name_, str_) { \
-  if (str_) { \
-    if (started_) { \
-      (client_)->print("&"); \
-    } else { \
-      (client_)->print("?"); \
-      started_ = true; \
-    } \
-    (client_)->print(name_ "="); \
-    (client_)->print(str_); \
-  } \
-  }
-
-int M2XStreamClient::fetchValues(const char* feedId, const char* streamName,
-                                 stream_value_read_callback callback, void* context,
-                                 const char* startTime, const char* endTime,
-                                 const char* limit) {
+int M2XStreamClient::listStreamValues(const char* deviceId, const char* streamName,
+                                      stream_value_read_callback callback, void* context,
+                                      const char* query) {
   if (_client->connect(_host, _port)) {
-    bool query_started = false;
-
     DBGLN("%s", "Connected to M2X server!");
-    _client->print("GET /v1/feeds/");
-    print_encoded_string(_client, feedId);
+    _client->print("GET /v2/devices/");
+    print_encoded_string(_client, deviceId);
     _client->print("/streams/");
     print_encoded_string(_client, streamName);
-    _client->print("/values");
+    _client->print("/values.json");
 
-    WRITE_QUERY_PART(_client, query_started, "start", startTime);
-    WRITE_QUERY_PART(_client, query_started, "end", endTime);
-    WRITE_QUERY_PART(_client, query_started, "limit", limit);
+    if (query) {
+      if (query[0] != '?') {
+        _client->print('?');
+      }
+      _client->print(query);
+    }
 
     _client->println(" HTTP/1.0");
     writeHttpHeader(-1);
@@ -83,13 +70,13 @@
   return status;
 }
 
-int M2XStreamClient::readLocation(const char* feedId,
+int M2XStreamClient::readLocation(const char* deviceId,
                                   location_read_callback callback,
                                   void* context) {
   if (_client->connect(_host, _port)) {
     DBGLN("%s", "Connected to M2X server!");
-    _client->print("GET /v1/feeds/");
-    print_encoded_string(_client, feedId);
+    _client->print("GET /v2/devices/");
+    print_encoded_string(_client, deviceId);
     _client->println("/location HTTP/1.0");
 
     writeHttpHeader(-1);
@@ -106,12 +93,12 @@
   return status;
 }
 
-int M2XStreamClient::deleteValues(const char* feedId, const char* streamName,
+int M2XStreamClient::deleteValues(const char* deviceId, const char* streamName,
                                   const char* from, const char* end) {
   if (_client->connect(_host, _port)) {
     DBGLN("%s", "Connected to M2X server!");
     int length = write_delete_values(&_null_print, from, end);
-    writeDeleteHeader(feedId, streamName, length);
+    writeDeleteHeader(deviceId, streamName, length);
     write_delete_values(_client, from, end);
   } else {
     DBGLN("%s", "ERROR: Cannot connect to M2X server!");
@@ -153,11 +140,11 @@
   return bytes;
 }
 
-void M2XStreamClient::writePutHeader(const char* feedId,
+void M2XStreamClient::writePutHeader(const char* deviceId,
                                      const char* streamName,
                                      int contentLength) {
-  _client->print("PUT /v1/feeds/");
-  print_encoded_string(_client, feedId);
+  _client->print("PUT /v2/devices/");
+  print_encoded_string(_client, deviceId);
   _client->print("/streams/");
   print_encoded_string(_client, streamName);
   _client->println("/value HTTP/1.0");
@@ -165,11 +152,11 @@
   writeHttpHeader(contentLength);
 }
 
-void M2XStreamClient::writeDeleteHeader(const char* feedId,
+void M2XStreamClient::writeDeleteHeader(const char* deviceId,
                                         const char* streamName,
                                         int contentLength) {
-  _client->print("DELETE /v1/feeds/");
-  print_encoded_string(_client, feedId);
+  _client->print("DELETE /v2/devices/");
+  print_encoded_string(_client, deviceId);
   _client->print("/streams/");
   print_encoded_string(_client, streamName);
   _client->print("/values");
--- a/M2XStreamClient.h	Fri Sep 26 09:35:10 2014 +0000
+++ b/M2XStreamClient.h	Tue Dec 09 14:36:16 2014 +0000
@@ -8,13 +8,13 @@
 #ifdef ARDUINO_PLATFORM
 #include "Arduino.h"
 
-#define USER_AGENT "User-Agent: M2X Arduino Client/0.1"
+#define USER_AGENT "User-Agent: M2X Arduino Client/2.0.0"
 #endif
 
 #ifdef MBED_PLATFORM
 #include "mbed.h"
 
-#define USER_AGENT "User-Agent: M2X Mbed Client/0.1"
+#define USER_AGENT "User-Agent: M2X Mbed Client/2.0.0"
 #endif
 
 #include "Client.h"
@@ -82,10 +82,10 @@
 
   // Push data stream value using PUT request, returns the HTTP status code
   template <class T>
-  int put(const char* feedId, const char* streamName, T value);
+  int updateStreamValue(const char* deviceId, const char* streamName, T value);
 
   // Post multiple values to M2X all at once.
-  // +feedId+ - id of the feed to post values
+  // +deviceId+ - id of the device to post values
   // +streamNum+ - Number of streams to post
   // +names+ - Array of stream names, the length of the array should
   // be exactly +streamNum+
@@ -103,9 +103,9 @@
   // for the second stream, etc. The length of this array should be
   // the sum of all values in +counts+ array.
   template <class T>
-  int postMultiple(const char* feedId, int streamNum,
-                   const char* names[], const int counts[],
-                   const char* ats[], T values[]);
+  int postDeviceUpdates(const char* deviceId, int streamNum,
+                        const char* names[], const int counts[],
+                        const char* ats[], T values[]);
 
   // Fetch values for a particular data stream. Since memory is
   // very limited on an Arduino, we cannot parse and get all the
@@ -119,10 +119,9 @@
   // For each data point, the callback will be called once. The HTTP
   // status code will be returned. And the content is only parsed when
   // the status code is 200.
-  int fetchValues(const char* feedId, const char* streamName,
-                  stream_value_read_callback callback, void* context,
-                  const char* startTime = NULL, const char* endTime = NULL,
-                  const char* limit = NULL);
+  int listStreamValues(const char* deviceId, const char* streamName,
+                       stream_value_read_callback callback, void* context,
+                       const char* query = NULL);
 
   // Update datasource location
   // NOTE: On an Arduino Uno and other ATMEGA based boards, double has
@@ -139,13 +138,13 @@
   // without any precision problems.
   // Returned value is the http status code.
   template <class T>
-  int updateLocation(const char* feedId, const char* name,
+  int updateLocation(const char* deviceId, const char* name,
                      T latitude, T longitude, T elevation);
 
-  // Read location information for a feed. Also used callback to process
+  // Read location information for a device. Also used callback to process
   // data points for memory reasons. The HTTP status code is returned,
   // response is only parsed when the HTTP status code is 200
-  int readLocation(const char* feedId, location_read_callback callback,
+  int readLocation(const char* deviceId, location_read_callback callback,
                    void* context);
 
   // Delete values from a data stream
@@ -162,7 +161,7 @@
   // The status code is 204 on success and 400 on a bad request (e.g. the
   // timestamp is not in ISO8601 format or the from timestamp is not less than
   // or equal to the end timestamp.
-  int deleteValues(const char* feedId, const char* streamName,
+  int deleteValues(const char* deviceId, const char* streamName,
                    const char* from, const char* end);
 private:
   Client* _client;
@@ -173,11 +172,11 @@
   NullPrint _null_print;
 
   // Writes the HTTP header part for updating a stream value
-  void writePutHeader(const char* feedId,
+  void writePutHeader(const char* deviceId,
                       const char* streamName,
                       int contentLength);
   // Writes the HTTP header part for deleting stream values
-  void writeDeleteHeader(const char* feedId,
+  void writeDeleteHeader(const char* deviceId,
                          const char* streamName,
                          int contentLength);
   // Writes HTTP header lines including M2X API Key, host, content
--- a/M2XStreamClient_template.h	Fri Sep 26 09:35:10 2014 +0000
+++ b/M2XStreamClient_template.h	Tue Dec 09 14:36:16 2014 +0000
@@ -6,10 +6,10 @@
 int print_encoded_string(Print* print, const char* str);
 
 template <class T>
-int M2XStreamClient::put(const char* feedId, const char* streamName, T value) {
+int M2XStreamClient::updateStreamValue(const char* deviceId, const char* streamName, T value) {
   if (_client->connect(_host, _port)) {
     DBGLN("%s", "Connected to M2X server!");
-    writePutHeader(feedId, streamName,
+    writePutHeader(deviceId, streamName,
                    //  for {"value": and }
                    _null_print.print(value) + 10);
     _client->print("{\"value\":");
@@ -34,7 +34,7 @@
     bytes += print->print(names[i]);
     bytes += print->print("\":[");
     for (int j = 0; j < counts[i]; j++) {
-      bytes += print->print("{\"at\": \"");
+      bytes += print->print("{\"timestamp\": \"");
       bytes += print->print(ats[value_index]);
       bytes += print->print("\", \"value\": \"");
       bytes += print->print(values[value_index]);
@@ -50,16 +50,16 @@
 }
 
 template <class T>
-int M2XStreamClient::postMultiple(const char* feedId, int streamNum,
-                                  const char* names[], const int counts[],
-                                  const char* ats[], T values[]) {
+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 /v1/feeds/");
-    print_encoded_string(_client, feedId);
-    _client->println(" HTTP/1.0");
+    _client->print("POST /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 {
@@ -103,7 +103,7 @@
 }
 
 template <class T>
-int M2XStreamClient::updateLocation(const char* feedId,
+int M2XStreamClient::updateLocation(const char* deviceId,
                                     const char* name,
                                     T latitude,
                                     T longitude,
@@ -113,8 +113,8 @@
 
     int length = write_location_data(&_null_print, name, latitude, longitude,
                                      elevation);
-    _client->print("PUT /v1/feeds/");
-    print_encoded_string(_client, feedId);
+    _client->print("PUT /v2/devices/");
+    print_encoded_string(_client, deviceId);
     _client->println("/location HTTP/1.0");
 
     writeHttpHeader(length);
--- a/StreamParseFunctions.h	Fri Sep 26 09:35:10 2014 +0000
+++ b/StreamParseFunctions.h	Tue Dec 09 14:36:16 2014 +0000
@@ -32,7 +32,7 @@
 {
   stream_parsing_context_state* state =
       (stream_parsing_context_state*) context->client_state;
-  if (strncmp((const char*) token->start, "at", 2) == 0) {
+  if (strncmp((const char*) token->start, "timestamp", 2) == 0) {
     state->state |= WAITING_AT;
   } else if ((strncmp((const char*) token->start, "value", 5) == 0) &&
              (token->start[5] != 's')) { // get rid of "values"