forked from vitotai/BrewPiLess
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDataLogger.cpp
More file actions
134 lines (109 loc) · 3.09 KB
/
Copy pathDataLogger.cpp
File metadata and controls
134 lines (109 loc) · 3.09 KB
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
#ifdef ESP8266
#include <ESP8266WiFi.h>
#include <WiFiClientSecure.h>
#include <ESP8266HTTPClient.h>
//#include <WiFiClientSecureBearSSL.h>
#elif defined(ESP32)
#include <WiFi.h>
#include <HTTPClient.h>
#include <AsyncTCP.h>
#include <WiFiClientSecure.h>
#endif
#include <ESPAsyncWebServer.h>
#include "DataLogger.h"
#include "Config.h"
#include "LogFormatter.h"
#if SupportPressureTransducer
#include "PressureMonitor.h"
#endif
#define GSLOG_JSON_BUFFER_SIZE 256
#define MAX_GSLOG_CONFIG_LEN 1024
void DataLogger::reportNow(void)
{
_lastUpdate=0;
}
void DataLogger::loop(time_t now)
{
if(!_loggingInfo->enabled) return;
if((now - _lastUpdate) < _loggingInfo->period) return;
sendData();
_lastUpdate=now;
}
#define BUFFERSIZE 512
void DataLogger::sendData(void)
{
char data[BUFFERSIZE];
int len=0;
if(_loggingInfo->service == ServiceNonNullJson){
len = nonNullJson(data,BUFFERSIZE);
}else{
if(_loggingInfo->service == ServiceHTTPNullString){
len =dataSprintf(data,_loggingInfo->format,"\"\"");
}else{
len =dataSprintf(data,_loggingInfo->format,"null");
}
}
if(len==0){
DBG_PRINTF("Invalid format\n");
return;
}
DBG_PRINTF("url=\"%s\"\n",_loggingInfo->url);
DBG_PRINTF("data= %d, \"%s\"\n",len,data);
int code;
#if ESP32
WiFiClient wifiClient;
WiFiClientSecure sClient;
bool isHttps=false;
if( strncasecmp(_loggingInfo->url,"https",5) ==0){
// sClient.setBufferSizes(1024, 1024);
// sClient.setInsecure();
isHttps=true;
}
#else
WiFiClient wifiClient;
#endif
HTTPClient _http;
_http.setUserAgent(F("ESP8266"));
DBG_PRINTF("[HTTP] %d...\n",_loggingInfo->method);
DBG_PRINTF("Content-Type:\"%s\"\n", _loggingInfo->contentType);
if(_loggingInfo->method == mHTTP_POST
|| _loggingInfo->method== mHTTP_PUT ){
// post
#if ESP32
if(isHttps) _http.begin(sClient,_loggingInfo->url);
else
#endif
_http.begin(wifiClient,_loggingInfo->url);
if(_loggingInfo->contentType && _loggingInfo->contentType[0]!='\0'){
_http.addHeader("Content-Type", _loggingInfo->contentType);
}else{
_http.addHeader("Content-Type", "application/x-www-form-urlencoded");
}
// start connection and send HTTP header
code = _http.sendRequest((_loggingInfo->method == mHTTP_POST)? "POST":"PUT",(uint8_t*)data,len);
}else{
#if ESP32
if(isHttps) _http.begin(sClient,String(_loggingInfo->url) + String("?") + String(data));
else
#endif
_http.begin(wifiClient,String(_loggingInfo->url) + String("?") + String(data));
code = _http.GET();
}
if(code <= 0) {
DBG_PRINTF("HTTP error: %s\n", _http.errorToString(code).c_str());
_http.end();
return;
}
// HTTP header has been send and Server response header has been handled
DBG_PRINTF("[HTTP] result code: %d\n", code);
if(code == HTTP_CODE_OK){
}else if((code / 100) == 3 && _http.hasHeader("Location")){
String location=_http.header("Location");
DBG_PRINTF("redirect:%s\n",location.c_str());
}else{
DBG_PRINTF("error, unhandled code:%d",code);
}
String output=_http.getString();
DBG_PRINTF("output:\n%s\n",output.c_str());
_http.end();
}