Home Alert System

Dependencies:   PWM_Tone_Library DHT

Committer:
ethaderu
Date:
Tue Mar 05 02:34:44 2019 +0000
Revision:
3:78f223d34f36
Publish 1

Who changed what in which revision?

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