|
9 | 9 |
|
10 | 10 | #include <ArduinoOcpp.h> // Facade for ArduinoOcpp |
11 | 11 | #include <ArduinoOcpp/SimpleOcppOperationFactory.h> // define behavior for incoming req messages |
| 12 | + |
12 | 13 | #include <HTTPUpdate.h> |
| 14 | +#include <MongooseHttpClient.h> //for HTTP update |
13 | 15 |
|
14 | 16 | #include <ArduinoOcpp/Core/OcppEngine.h> |
15 | 17 |
|
@@ -66,35 +68,91 @@ void ArduinoOcppTask::initializeArduinoOcpp() { |
66 | 68 |
|
67 | 69 | ArduinoOcpp::FirmwareService *fwService = ArduinoOcpp::getFirmwareService(); |
68 | 70 | if (fwService) { |
69 | | - |
70 | | - } |
| 71 | + fwService->setOnInstall([lcd = lcd, &updateUserNotified = updateUserNotified, &updateUrl = updateUrl](String location) { |
71 | 72 |
|
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; |
76 | 74 |
|
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 |
78 | 76 |
|
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; |
87 | 125 | } else { |
88 | | - percent = 99; |
| 126 | + return ArduinoOcpp::UploadStatus::NotUploaded; |
89 | 127 | } |
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 | + } |
98 | 156 |
|
99 | 157 | DynamicJsonDocument *evseDetailsDoc = new DynamicJsonDocument(JSON_OBJECT_SIZE(6)); |
100 | 158 | JsonObject evseDetails = evseDetailsDoc->to<JsonObject>(); |
|
0 commit comments