branch with improvemnts
Fork of M2XStreamClient by
Embed:
(wiki syntax)
Show/hide line numbers
M2XStreamClient_template.h
00001 #ifndef M2XStreamClient_template_h 00002 #define M2XStreamClient_template_h 00003 00004 // Implementations of template functions 00005 00006 int print_encoded_string(Print* print, const char* str); 00007 00008 template <class T> 00009 int M2XStreamClient::put(const char* feedId, const char* streamName, T value) { 00010 if (_client->connect(_host, _port)) { 00011 DBGLN("%s", "Connected to M2X server!"); 00012 writePutHeader(feedId, streamName, 00013 // for {"value": and } 00014 _null_print.print(value) + 10); 00015 _client->print("{\"value\":"); 00016 _client->print(value); 00017 _client->print("}"); 00018 } else { 00019 DBGLN("%s", "ERROR: Cannot connect to M2X server!"); 00020 return E_NOCONNECTION; 00021 } 00022 00023 return readStatusCode(true); 00024 } 00025 00026 template <class T> 00027 inline int write_multiple_values(Print* print, int streamNum, 00028 const char* names[], const int counts[], 00029 const char* ats[], T values[]) { 00030 int bytes = 0, value_index = 0; 00031 bytes += print->print("{\"values\":{"); 00032 for (int i = 0; i < streamNum; i++) { 00033 bytes += print->print("\""); 00034 bytes += print->print(names[i]); 00035 bytes += print->print("\":["); 00036 for (int j = 0; j < counts[i]; j++) { 00037 bytes += print->print("{\"at\": \""); 00038 bytes += print->print(ats[value_index]); 00039 bytes += print->print("\", \"value\": \""); 00040 bytes += print->print(values[value_index]); 00041 bytes += print->print("\"}"); 00042 if (j < counts[i] - 1) { bytes += print->print(","); } 00043 value_index++; 00044 } 00045 bytes += print->print("]"); 00046 if (i < streamNum - 1) { bytes += print->print(","); } 00047 } 00048 bytes += print->print("}}"); 00049 return bytes; 00050 } 00051 00052 template <class T> 00053 int M2XStreamClient::postMultiple(const char* feedId, int streamNum, 00054 const char* names[], const int counts[], 00055 const char* ats[], T values[]) { 00056 if (_client->connect(_host, _port)) { 00057 DBGLN("%s", "Connected to M2X server!"); 00058 int length = write_multiple_values(&_null_print, streamNum, names, 00059 counts, ats, values); 00060 _client->print("POST /v1/feeds/"); 00061 print_encoded_string(_client, feedId); 00062 _client->println(" HTTP/1.0"); 00063 writeHttpHeader(length); 00064 write_multiple_values(_client, streamNum, names, counts, ats, values); 00065 } else { 00066 DBGLN("%s", "ERROR: Cannot connect to M2X server!"); 00067 return E_NOCONNECTION; 00068 } 00069 return readStatusCode(true); 00070 } 00071 00072 template <class T> 00073 static int write_location_data(Print* print, const char* name, 00074 T latitude, T longitude, 00075 T elevation) { 00076 int bytes = 0; 00077 bytes += print->print("{\"name\":\""); 00078 bytes += print->print(name); 00079 bytes += print->print("\",\"latitude\":\""); 00080 bytes += print->print(latitude); 00081 bytes += print->print("\",\"longitude\":\""); 00082 bytes += print->print(longitude); 00083 bytes += print->print("\",\"elevation\":\""); 00084 bytes += print->print(elevation); 00085 bytes += print->print("\"}"); 00086 return bytes; 00087 } 00088 00089 static int write_location_data(Print* print, const char* name, 00090 double latitude, double longitude, 00091 double elevation) { 00092 int bytes = 0; 00093 bytes += print->print("{\"name\":\""); 00094 bytes += print->print(name); 00095 bytes += print->print("\",\"latitude\":\""); 00096 bytes += print->print(latitude, MAX_DOUBLE_DIGITS); 00097 bytes += print->print("\",\"longitude\":\""); 00098 bytes += print->print(longitude, MAX_DOUBLE_DIGITS); 00099 bytes += print->print("\",\"elevation\":\""); 00100 bytes += print->print(elevation); 00101 bytes += print->print("\"}"); 00102 return bytes; 00103 } 00104 00105 template <class T> 00106 int M2XStreamClient::updateLocation(const char* feedId, 00107 const char* name, 00108 T latitude, 00109 T longitude, 00110 T elevation) { 00111 if (_client->connect(_host, _port)) { 00112 DBGLN("%s", "Connected to M2X server!"); 00113 00114 int length = write_location_data(&_null_print, name, latitude, longitude, 00115 elevation); 00116 _client->print("PUT /v1/feeds/"); 00117 print_encoded_string(_client, feedId); 00118 _client->println("/location HTTP/1.0"); 00119 00120 writeHttpHeader(length); 00121 write_location_data(_client, name, latitude, longitude, elevation); 00122 } else { 00123 DBGLN("%s", "ERROR: Cannot connect to M2X server!"); 00124 return E_NOCONNECTION; 00125 } 00126 return readStatusCode(true); 00127 } 00128 00129 #endif
Generated on Wed Jul 13 2022 05:32:06 by 1.7.2