WEB обновление и время

This commit is contained in:
Mit4el
2025-03-26 19:08:53 +03:00
parent fc50947ff3
commit 769d6fc7f1
7 changed files with 77 additions and 0 deletions

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@@ -10,6 +10,8 @@ extern void handleFileUpload();
extern void handleFileDelete(); extern void handleFileDelete();
extern void handleFileCreate(); extern void handleFileCreate();
extern void handleLocalOTA(); extern void handleLocalOTA();
extern void handleUpdateOTA();
extern void handleCors();
extern void handleLocalOTA_Handler(); extern void handleLocalOTA_Handler();
extern void handleFileList(); extern void handleFileList();
//void printDirectory(File dir, String& out); //void printDirectory(File dir, String& out);

View File

@@ -92,6 +92,11 @@ void standWebServerInit() {
HTTP.on("/localota", HTTP_GET, handleLocalOTA); HTTP.on("/localota", HTTP_GET, handleLocalOTA);
HTTP.on("/localota_handler", HTTP_GET, handleLocalOTA_Handler); HTTP.on("/localota_handler", HTTP_GET, handleLocalOTA_Handler);
HTTP.on("/update", HTTP_POST, []() {
HTTP.send(200); // Для CORS
}, handleUpdateOTA);
HTTP.on("/update", HTTP_OPTIONS, handleCors);
// Default handler for all URIs not defined above // Default handler for all URIs not defined above
// Use it to read files from filesystem // Use it to read files from filesystem
@@ -164,6 +169,42 @@ void handleLocalOTA() {
String page = "<form action='/localota' method='POST'><label for='server'>Server Address:</label><input type='text' name='server' value='http://192.168.1.2:5500'><input type='submit' value='Update'></form>"; String page = "<form action='/localota' method='POST'><label for='server'>Server Address:</label><input type='text' name='server' value='http://192.168.1.2:5500'><input type='submit' value='Update'></form>";
HTTP.send(200, "text/html", page);} HTTP.send(200, "text/html", page);}
void handleCors() {
HTTP.sendHeader("Access-Control-Allow-Origin", "*");
HTTP.send(200);
}
void handleUpdateOTA() {
HTTPUpload& upload = HTTP.upload();
if (upload.status == UPLOAD_FILE_START) {
Serial.print("Начало загрузки: ");
Serial.println(upload.filename);
if (!Update.begin(UPDATE_SIZE_UNKNOWN)) {
Update.end();
HTTP.send(500, "text/plain", "Ошибка: Недостаточно памяти");
return;
}
}
else if (upload.status == UPLOAD_FILE_WRITE) {
if (Update.write(upload.buf, upload.currentSize) != upload.currentSize) {
Update.end();
HTTP.send(500, "text/plain", "Ошибка записи данных");
return;
}
}
else if (upload.status == UPLOAD_FILE_END) {
if (Update.end(true)) { // true - перезагрузка после обновления
HTTP.send(200, "text/plain", "Обновление успешно");
Serial.println("Обновление завершено");
ESP.restart();
} else {
Update.end();
HTTP.send(500, "text/plain", "Ошибка завершения обновления");
}
}
}
void handleLocalOTA_Handler() { void handleLocalOTA_Handler() {
String serverValue = HTTP.arg("server"); String serverValue = HTTP.arg("server");
upgrade_firmware(3,serverValue); upgrade_firmware(3,serverValue);

View File

@@ -233,6 +233,40 @@ void webSocketEvent(uint8_t num, WStype_t type, uint8_t* payload, size_t length)
sendStringToWs("settin", settingsFlashJson, num); sendStringToWs("settin", settingsFlashJson, num);
} }
if (headerStr == "/localt|") {
String timeStr = String((char*)payload + 8);
Serial.println("Время с фронта: /localt|" + timeStr);
// Обрезаем дробную часть, если есть
int dotIndex = timeStr.indexOf('.');
if (dotIndex != -1) {
timeStr = timeStr.substring(0, dotIndex);
}
// Парсим UNIX-время в секундах
time_t unixTime = (time_t)timeStr.toInt();
// Создаём структуру timeval
timeval tv;
tv.tv_sec = unixTime; // Секунды эпохи
tv.tv_usec = 0; // Микросекунды
// Устанавливаем время
if (settimeofday(&tv, NULL) == 0) {
Serial.printf("Время установлено: %ld\n", unixTime);
} else {
Serial.printf("Ошибка установки времени: %ld\n", unixTime);
}
// timeval tv2{0, 0};
// timezone tz = timezone{0, 0};
// time_t epoch = 0;
// if (gettimeofday(&tv2, &tz) != -1) {
// epoch = tv2.tv_sec;
// }
// unixTime = epoch;
// SerialPrint("I", F("NTP"), "TIME " + String(unixTime));
}
//----------------------------------------------------------------------// //----------------------------------------------------------------------//
// Страница веб интерфейса dev // Страница веб интерфейса dev
//----------------------------------------------------------------------// //----------------------------------------------------------------------//