Skip to content

Commit 5db64cc

Browse files
committed
diagnostics reporting
1 parent 7d9a71c commit 5db64cc

File tree

2 files changed

+86
-24
lines changed

2 files changed

+86
-24
lines changed

src/ocpp.cpp

Lines changed: 82 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@
99

1010
#include <ArduinoOcpp.h> // Facade for ArduinoOcpp
1111
#include <ArduinoOcpp/SimpleOcppOperationFactory.h> // define behavior for incoming req messages
12+
1213
#include <HTTPUpdate.h>
14+
#include <MongooseHttpClient.h> //for HTTP update
1315

1416
#include <ArduinoOcpp/Core/OcppEngine.h>
1517

@@ -66,35 +68,91 @@ void ArduinoOcppTask::initializeArduinoOcpp() {
6668

6769
ArduinoOcpp::FirmwareService *fwService = ArduinoOcpp::getFirmwareService();
6870
if (fwService) {
69-
70-
}
71+
fwService->setOnInstall([lcd = lcd, &updateUserNotified = updateUserNotified, &updateUrl = updateUrl](String location) {
7172

72-
//TODO HTTPUpdate has added the onProgress cb to its own class definition in Jun '21. Replace when available (https://github.com/espressif/arduino-esp32/commit/db4e7667afe0e169c5f00567f4b59ab8e0fc1532)
73-
Update.onProgress([lcd = lcd, &updateUserNotified = updateUserNotified](size_t index, size_t total) {
74-
if (!updateUserNotified && index > 0) {
75-
updateUserNotified = true;
73+
updateUrl = location;
7674

77-
DEBUG_PORT.printf("Update via OCPP start\n");
75+
#if 0 //TODO finish when HTTP FW download will be available in the OpenEVSE core
7876

79-
lcd->display(F("Updating WiFi"), 0, 0, 10 * 1000, LCD_CLEAR_LINE | LCD_DISPLAY_NOW);
80-
lcd->display(F(""), 0, 1, 10 * 1000, LCD_CLEAR_LINE | LCD_DISPLAY_NOW);
81-
}
82-
83-
if (index < total) {
84-
unsigned int percent = 0;
85-
if (total / 100U != 0) {
86-
percent = index / (total / 100U);
77+
//TODO HTTPUpdate has added the onProgress cb to its own class definition in Jun '21. Replace when available (https://github.com/espressif/arduino-esp32/commit/db4e7667afe0e169c5f00567f4b59ab8e0fc1532)
78+
Update.onProgress([lcd = lcd, &updateUserNotified = updateUserNotified](size_t index, size_t total) {
79+
if (!updateUserNotified && index > 0) {
80+
updateUserNotified = true;
81+
82+
DEBUG_PORT.printf("Update via OCPP start\n");
83+
84+
lcd->display(F("Updating WiFi"), 0, 0, 10 * 1000, LCD_CLEAR_LINE | LCD_DISPLAY_NOW);
85+
lcd->display(F(""), 0, 1, 10 * 1000, LCD_CLEAR_LINE | LCD_DISPLAY_NOW);
86+
}
87+
88+
if (index < total) {
89+
unsigned int percent = 0;
90+
if (total / 100U != 0) {
91+
percent = index / (total / 100U);
92+
} else {
93+
percent = 99;
94+
}
95+
DBUGVAR(percent);
96+
String text = String(percent) + F("%");
97+
if (lcd) lcd->display(text, 0, 1, 10 * 1000, LCD_DISPLAY_NOW);
98+
DEBUG_PORT.printf("Update: %d%%\n", percent);
99+
} else {
100+
lcd->display(F("Complete"), 0, 1, 10 * 1000, LCD_CLEAR_LINE | LCD_DISPLAY_NOW);
101+
}
102+
});
103+
104+
// MongooseHttpClient client = MongooseHttpClient();
105+
//
106+
// client.get(updateUrl.c_str(), [](MongooseHttpClientResponse *response) {
107+
// //if (response->contentLength() <
108+
// //Update.begin
109+
// });
110+
111+
#endif
112+
DEBUG_PORT.println(F("[ocpp] FW download not implemented yet! Ignore request"));
113+
114+
return true;
115+
});
116+
}
117+
118+
ArduinoOcpp::DiagnosticsService *diagService = ArduinoOcpp::getDiagnosticsService();
119+
if (diagService) {
120+
diagService->setOnUploadStatusSampler([this] () {
121+
if (diagFailure) {
122+
return ArduinoOcpp::UploadStatus::UploadFailed;
123+
} else if (diagSuccess) {
124+
return ArduinoOcpp::UploadStatus::Uploaded;
87125
} else {
88-
percent = 99;
126+
return ArduinoOcpp::UploadStatus::NotUploaded;
89127
}
90-
DBUGVAR(percent);
91-
String text = String(percent) + F("%");
92-
if (lcd) lcd->display(text, 0, 1, 10 * 1000, LCD_DISPLAY_NOW);
93-
DEBUG_PORT.printf("Update: %d%%\n", percent);
94-
} else {
95-
lcd->display(F("Complete"), 0, 1, 10 * 1000, LCD_CLEAR_LINE | LCD_DISPLAY_NOW);
96-
}
97-
});
128+
});
129+
130+
diagService->setOnUpload([this] (String &location, ArduinoOcpp::OcppTimestamp &startTime, ArduinoOcpp::OcppTimestamp &stopTime) {
131+
132+
//reset reported state
133+
diagSuccess = false;
134+
diagFailure = false;
135+
136+
//check if input URL is valid (maybe add Same-origin policy?)
137+
unsigned int port_i = 0;
138+
struct mg_str scheme, query, fragment;
139+
if (mg_parse_uri(mg_mk_str(location.c_str()), &scheme, NULL, NULL, &port_i, NULL, &query, &fragment)) {
140+
DEBUG_PORT.print(F("[ocpp] Diagnostics upload, invalid URL: "));
141+
DEBUG_PORT.print(location);
142+
diagFailure = true;
143+
return false;
144+
}
145+
146+
//create file to upload
147+
148+
149+
150+
diagClient.post("abc", "def", "ghj");
151+
152+
153+
return true;
154+
});
155+
}
98156

99157
DynamicJsonDocument *evseDetailsDoc = new DynamicJsonDocument(JSON_OBJECT_SIZE(6));
100158
JsonObject evseDetails = evseDetailsDoc->to<JsonObject>();

src/ocpp.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,10 @@ class ArduinoOcppTask: public MicroTasks::Task {
3838
ulong resetTime;
3939

4040
bool updateUserNotified = false;
41+
String updateUrl = String('\0');
42+
43+
MongooseHttpClient diagClient = MongooseHttpClient();
44+
bool diagSuccess, diagFailure = false;
4145

4246
void initializeArduinoOcpp();
4347
bool arduinoOcppInitialized = false;

0 commit comments

Comments
 (0)