-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGS32ReportWeb.cpp
More file actions
108 lines (92 loc) · 2.88 KB
/
Copy pathGS32ReportWeb.cpp
File metadata and controls
108 lines (92 loc) · 2.88 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
#include "GS32ReportWeb.h"
GS32ReportWeb::GS32ReportWeb() {
_serverUrl = "";
}
GS32ReportWeb::GS32ReportWeb(const char* serverUrl) {
_serverUrl = String(serverUrl);
}
void GS32ReportWeb::begin(const char* serverUrl) {
_serverUrl = String(serverUrl);
}
void GS32ReportWeb::setServerUrl(const char* serverUrl) {
_serverUrl = String(serverUrl);
}
void GS32ReportWeb::setConfig(const SystemReportConfig& config) {
_config = config;
}
void GS32ReportWeb::onCustomData(UserReportCallback callback) {
_customCallback = callback;
}
void GS32ReportWeb::appendSystemData(JsonObject sys) {
if (_config.chipModel) {
sys["chip_model"] = ESP.getChipModel();
}
if (_config.chipCores) {
sys["chip_cores"] = ESP.getChipCores();
}
if (_config.chipRevision) {
sys["chip_revision"] = ESP.getChipRevision();
}
if (_config.cpuFreq) {
sys["cpu_freq_mhz"] = ESP.getCpuFreqMHz();
}
if (_config.chipId) {
uint64_t chipid = ESP.getEfuseMac();
char idStr[17];
snprintf(idStr, sizeof(idStr), "%04X%08X", (uint16_t)(chipid >> 32), (uint32_t)chipid);
sys["chip_id"] = idStr;
}
if (_config.heap) {
sys["free_heap"] = ESP.getFreeHeap();
}
if (_config.minHeap) {
sys["min_free_heap"] = ESP.getMinFreeHeap();
}
if (_config.maxAllocHeap) {
sys["max_alloc_heap"] = ESP.getMaxAllocHeap();
}
if (_config.flashSize) {
sys["flash_size"] = ESP.getFlashChipSize();
}
if (_config.flashSpeed) {
sys["flash_speed"] = ESP.getFlashChipSpeed();
}
if (_config.sdkVersion) {
sys["sdk_version"] = ESP.getSdkVersion();
}
if (_config.sketchSize) {
sys["sketch_size"] = ESP.getSketchSize();
}
if (_config.freeSketch) {
sys["free_sketch_space"] = ESP.getFreeSketchSpace();
}
if (_config.uptime) {
sys["uptime_ms"] = millis();
}
}
bool GS32ReportWeb::sendReport() {
if (_serverUrl.length() == 0) {
return false;
}
// Автоматический размер для ArduinoJson v7
JsonDocument doc;
JsonObject sysObj = doc["system"].to<JsonObject>();
appendSystemData(sysObj);
if (_customCallback != nullptr) {
JsonObject customObj = doc["custom"].to<JsonObject>();
_customCallback(customObj);
}
String jsonString;
serializeJson(doc, jsonString);
HTTPClient http;
http.begin(_serverUrl);
http.addHeader("Content-Type", "application/json");
int httpResponseCode = http.POST(jsonString);
bool success = false;
if (httpResponseCode > 0) {
String response = http.getString();
success = (httpResponseCode == HTTP_CODE_OK || httpResponseCode == HTTP_CODE_CREATED);
}
http.end();
return success;
}