/
Sender.cpp
320 lines (271 loc) · 7.18 KB
/
Sender.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
/*
---------------------------------------------------------------------------------------------------------
Special thanks to Samuel Lang.
This library is borrowed from his iSpindel project (https://github.com/universam1/iSpindel).
---------------------------------------------------------------------------------------------------------
*/
#include "Sender.h"
#include <ArduinoJson.h>
#define UBISERVER "things.ubidots.com"
#define CONNTIMEOUT 2000
SenderClass::SenderClass()
{
_jsonVariant = _jsonBuffer.createObject();
}
bool SenderClass::hasData()
{
return _jsonVariant.success();
}
void SenderClass::add(String id, float value)
{
_jsonVariant[id] = value;
}
void SenderClass::add(String id, String value)
{
_jsonVariant[id] = value;
}
void SenderClass::add(String id, uint32_t value)
{
_jsonVariant[id] = value;
}
void SenderClass::add(String id, int32_t value)
{
_jsonVariant[id] = value;
}
void SenderClass::add(String id, ulong value)
{
_jsonVariant[id] = value;
}
void SenderClass::sendTCP(String server, uint16_t port)
{
_jsonVariant.printTo(Serial);
if (_client.connect(server.c_str(), port))
{
_jsonVariant.printTo(_client);
_client.println();
}
else
{
}
int timeout = 0;
while (!_client.available() && timeout < CONNTIMEOUT)
{
timeout++;
delay(1);
}
while (_client.available())
{
char c = _client.read();
Serial.write(c);
}
// currentValue = 0;
_client.stop();
delay(100); // allow gracefull session close
}
void SenderClass::sendGenericPost(String server, String url, uint16_t port)
{
_jsonVariant.printTo(Serial);
HTTPClient http;
// configure traged server and url
http.begin(server, port, url);
http.addHeader("User-Agent", "iSpindel");
http.addHeader("Connection", "close");
http.addHeader("Content-Type", "application/json");
String json;
_jsonVariant.printTo(json);
auto httpCode = http.POST(json);
// httpCode will be negative on error
if (httpCode > 0)
{
if (httpCode == HTTP_CODE_OK)
{
}
}
else
{
}
http.end();
}
void SenderClass::sendInfluxDB(String server, uint16_t port, String db, String name)
{
HTTPClient http;
String uri = "/write?db=";
uri += db;
// configure traged server and url
http.begin(server, port, uri);
http.addHeader("User-Agent", "iSpindel");
http.addHeader("Connection", "close");
http.addHeader("Content-Type", "application/x-www-form-urlencoded");
String msg;
msg += "measurements,source=";
msg += name;
msg += " ";
for (const auto &kv : _jsonVariant.as<JsonObject>())
{
msg += kv.key;
msg += "=";
msg += kv.value.as<String>();
msg += ",";
}
msg.remove(msg.length() - 1);
auto httpCode = http.POST(msg);
// httpCode will be negative on error
if (httpCode > 0)
{
if (httpCode == HTTP_CODE_OK)
{
}
}
else
{
}
http.end();
}
void SenderClass::sendPrometheus(String server, uint16_t port, String job, String instance)
{
HTTPClient http;
// the path looks like /metrics/job/<JOBNAME>[/instance/<INSTANCENAME>]
String uri = "/metrics/job/";
uri += job;
uri += "/instance/";
uri += instance;
// configure traged server and url
http.begin(server, port, uri);
http.addHeader("User-Agent", "iSpindel");
http.addHeader("Connection", "close");
http.addHeader("Content-Type", "text/plain");
String msg;
//Build up the data for the Prometheus Pushgateway
//A gauge is a metric that represents a single numerical value that can arbitrarily go up and down.
for (const auto &kv : _jsonVariant.as<JsonObject>())
{
msg += "# TYPE ";
msg += kv.key;
msg += " gauge\n";
msg += "# HELP ";
msg += kv.key;
msg += " The approximate value of ";
msg += kv.key;
msg += ".\n";
msg += kv.key;
msg += " ";
msg += kv.value.as<String>();
msg += "\n";
}
auto httpCode = http.POST(msg);
// httpCode will be 202 on success
if (httpCode == 202)
{
}
else
{
}
http.end();
}
void SenderClass::sendUbidots(String token, String name)
{
_jsonVariant.printTo(Serial);
if (_client.connect(UBISERVER, 80))
{
String msg = F("POST /api/v1.6/devices/");
msg += name;
msg += "?token=";
msg += token;
msg += F(" HTTP/1.1\r\nHost: things.ubidots.com\r\nUser-Agent: GlyCon\r\nConnection: close\r\nContent-Type: application/json\r\nContent-Length: ");
msg += _jsonVariant.measureLength();
msg += "\r\n";
_client.println(msg);
_jsonVariant.printTo(_client);
_client.println();
}
else
{
}
int timeout = 0;
while (!_client.available() && timeout < CONNTIMEOUT)
{
timeout++;
delay(1);
}
while (_client.available())
{
char c = _client.read();
Serial.write(c);
}
// currentValue = 0;
_client.stop();
delay(100); // allow gracefull session close
}
void SenderClass::sendFHEM(String server, uint16_t port, String name)
{
_jsonVariant.printTo(Serial);
if (_client.connect(server.c_str(), port))
{
String msg = String("GET /fhem?cmd.Test=set%20");
msg += name;
for (const auto &kv : _jsonVariant.as<JsonObject>())
{
msg += "%20";
msg += kv.value.as<String>();
}
msg += F("&XHR=1 HTTP/1.1\r\nHost: ");
msg += server;
msg += ":";
msg += port;
msg += "\r\nUser-Agent: ";
msg += name;
msg += F("\r\nConnection: close\r\n");
_client.println(msg);
}
else
{
}
int timeout = 0;
while (!_client.available() && timeout < CONNTIMEOUT)
{
timeout++;
delay(1);
}
while (_client.available())
{
char c = _client.read();
Serial.write(c);
}
// currentValue = 0;
_client.stop();
delay(100); // allow gracefull session close
}
void SenderClass::sendTCONTROL(String server, uint16_t port)
{
_jsonVariant.printTo(Serial);
if (_client.connect(server.c_str(), port))
{
String msg;
for (const auto &kv : _jsonVariant.as<JsonObject>())
{
msg += kv.key;
msg += ": ";
msg += kv.value.as<String>();
msg += " ";
}
msg.remove(msg.length() - 1);
_client.println(msg);
}
else
{
}
int timeout = 0;
while (!_client.available() && timeout < CONNTIMEOUT)
{
timeout++;
delay(1);
}
while (_client.available())
{
char c = _client.read();
Serial.write(c);
}
// currentValue = 0;
_client.stop();
delay(100); // allow gracefull session close
}