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.cpp
- Revision:
- 10:4ce9eba38dbe
- Parent:
- 4:ba0d02be2835
- Child:
- 11:a11af0c81cfa
--- a/M2XStreamClient.cpp Sun Sep 07 17:56:18 2014 +0000
+++ b/M2XStreamClient.cpp Wed Sep 10 13:07:34 2014 +0000
@@ -7,6 +7,7 @@
const char* M2XStreamClient::kDefaultM2XHost = "api-m2x.att.com";
+static int write_delete_values(Print* print, const char* from, const char* end);
int print_encoded_string(Print* print, const char* str);
int tolower(int ch);
@@ -105,6 +106,32 @@
return status;
}
+int M2XStreamClient::deleteValues(const char* feedId, 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);
+ write_delete_values(_client, from, end);
+ } else {
+ DBGLN("%s", "ERROR: Cannot connect to M2X server!");
+ return E_NOCONNECTION;
+ }
+
+ return readStatusCode(true);
+}
+
+static int write_delete_values(Print* print, const char* from,
+ const char* end) {
+ int bytes = 0;
+ bytes += print->print("{\"from\":\"");
+ bytes += print->print(from);
+ bytes += print->print("\",\"end\":\"");
+ bytes += print->print(end);
+ bytes += print->print("\"}");
+ return bytes;
+}
+
// Encodes and prints string using Percent-encoding specified
// in RFC 1738, Section 2.2
int print_encoded_string(Print* print, const char* str) {
@@ -126,9 +153,9 @@
return bytes;
}
-void M2XStreamClient::writePostHeader(const char* feedId,
- const char* streamName,
- int contentLength) {
+void M2XStreamClient::writePutHeader(const char* feedId,
+ const char* streamName,
+ int contentLength) {
_client->print("PUT /v1/feeds/");
print_encoded_string(_client, feedId);
_client->print("/streams/");
@@ -138,6 +165,19 @@
writeHttpHeader(contentLength);
}
+void M2XStreamClient::writeDeleteHeader(const char* feedId,
+ const char* streamName,
+ int contentLength) {
+ _client->print("DELETE /v1/feeds/");
+ print_encoded_string(_client, feedId);
+ _client->print("/streams/");
+ print_encoded_string(_client, streamName);
+ _client->print("/values");
+ _client->println(" HTTP/1.0");
+
+ writeHttpHeader(contentLength);
+}
+
void M2XStreamClient::writeHttpHeader(int contentLength) {
_client->println(USER_AGENT);
_client->print("X-M2X-KEY: ");
@@ -310,8 +350,8 @@
jsonlite_parser_callbacks cbs = jsonlite_default_callbacks;
cbs.key_found = on_stream_key_found;
+ cbs.number_found = on_stream_number_found;
cbs.string_found = on_stream_string_found;
- cbs.number_found = on_stream_string_found;
cbs.context.client_state = &state;
jsonlite_parser p = jsonlite_parser_init(jsonlite_parser_estimate_size(5));
@@ -320,6 +360,7 @@
jsonlite_result result = jsonlite_result_unknown;
while (index < length) {
int i = 0;
+
DBG("%s", "Received Data: ");
while ((i < BUF_LEN) && _client->available()) {
buf[i++] = _client->read();
@@ -342,7 +383,7 @@
close();
return E_JSON_INVALID;
}
-
+
index += i;
}
