From cc1fd0c45859febd6f31c50614ef4c47d2c298e1 Mon Sep 17 00:00:00 2001 From: Luis Fernando Sauthier Date: Fri, 12 Dec 2025 17:19:29 -0300 Subject: [PATCH] =?UTF-8?q?atualiza=E2=94=9C=C2=BA=E2=94=9C=C3=BAo=20autom?= =?UTF-8?q?=E2=94=9C=C3=ADtica?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/controle.h | 3 +- src/loop.h | 19 +++++++---- src/webserver.h | 87 ++++++++++++++++++++++++++++++++++++------------- 3 files changed, 78 insertions(+), 31 deletions(-) diff --git a/src/controle.h b/src/controle.h index 2d63334..3ccda09 100644 --- a/src/controle.h +++ b/src/controle.h @@ -178,7 +178,6 @@ void abastecimento() setFont(MEDIUM, 7, 117, 132, 255, 255, 255); myGLCD.print((char*)"Enviando dados...", CENTER, 302); - qntEnvPend++; delayYield(2000); @@ -314,6 +313,8 @@ void abastecimento() file.close(); } + enviosPendentes(); + LOGLN(F("Enviando dados de abastecimento...")); if (MQTT_connected == true) diff --git a/src/loop.h b/src/loop.h index d3268cc..d39a86e 100644 --- a/src/loop.h +++ b/src/loop.h @@ -50,15 +50,15 @@ void loop() if (indiceMenu == 0) { if (MQTT_connected == true) { - if ((millis() - envAbastecimentoMillis) > (INTERVALO_ENVIO_ABASTECIMENTO * 60000UL)) { + unsigned long intervaloEnvio = (qntEnvPend > 0) ? 10000UL : (INTERVALO_ENVIO_ABASTECIMENTO * 60000UL); + if ((millis() - envAbastecimentoMillis) > intervaloEnvio) { enviarAbastecimento(); envAbastecimentoMillis = millis(); } } - //Envia a cada X minutos se MQTT está conectado ou 5 segundos após ligar if ((primeiroEnvioPendente && (millis() - envPendentesMillis) > 10000) - || ((millis() - envPendentesMillis) > (INTERVALO_ENVIOS_PENDENTES * 60000UL) && MQTT_connected == true)) + || ((millis() - envPendentesMillis) > (INTERVALO_ENVIOS_PENDENTES * 60000UL))) { primeiroEnvioPendente = false; enviosPendentes(); @@ -111,6 +111,14 @@ void loop() } } + if (MQTT_connected == true) { + unsigned long intervaloEnvio = (qntEnvPend > 0) ? 10000UL : (INTERVALO_ENVIO_ABASTECIMENTO * 60000UL); + if ((millis() - envAbastecimentoMillis) > intervaloEnvio) { + enviarAbastecimento(); + envAbastecimentoMillis = millis(); + } + } + if ((millis() - previousMillis) > 5000) // Executa funções a cada 5 segundos { now = rtc.GetDateTime(); @@ -119,9 +127,6 @@ void loop() #ifdef DEBUG printDateTime(); - #endif - - #ifdef DEBUG printLOG(); #endif @@ -283,4 +288,4 @@ void loop() menuRedrawPending = false; menuPrincipal(true); } -} \ No newline at end of file +} diff --git a/src/webserver.h b/src/webserver.h index 5ca6ef7..b704e77 100644 --- a/src/webserver.h +++ b/src/webserver.h @@ -432,7 +432,8 @@ void atualizaEnviosPendentes( String mensagem) return; } - if(Json[F("recebido")] == false) + bool recebidoAck = Json[F("recebido")].as(); + if(!recebidoAck) { LOGLN(F("Recebido = false")); return; @@ -476,13 +477,7 @@ void atualizaEnviosPendentes( String mensagem) resp += " foi removido"; resp += "\"}"; - if (qntEnvPend > 0) { - qntEnvPend--; - //Menu Principal - if (indiceMenu == 0) { - menuRedrawPending = true; - } - } + enviosPendentes(); if(MQTT_connected == true) { @@ -555,8 +550,9 @@ void enviarAbastecimento() { LOGLN(F("Enviando dados de abastecimento pendente")); - StaticJsonDocument Json; - char pub_message[MQTT_MAX_PACKET_SIZE]; + StaticJsonDocument<600> Json; + char in_message[MQTT_MAX_PACKET_SIZE]; + char out_message[MQTT_MAX_PACKET_SIZE]; byte tentativas = 0; @@ -584,26 +580,74 @@ void enviarAbastecimento() LOGLN(F("Nao foi possivel abrir a pasta ou ela não existe")); return; } - - if(file.openNext(&root, O_RDONLY)) +//modificacapo + while(file.openNext(&root, O_RDONLY)) { - DeserializationError deserializeError = deserializeJson(Json, file); + if (file.isHidden()) + { + file.close(); + continue; + } + + char currentFile[64]; + file.getName(currentFile, 64); + bool isDir = file.isDir(); file.close(); + if (isDir) + { + continue; + } + + if (strstr(currentFile, ".txt") == 0) + { + continue; + } + + char pathFile[96]; + snprintf(pathFile, sizeof(pathFile), "%s/%s", "envios", currentFile); + file = SD.open(pathFile, O_RDONLY); + if (!file) + { + continue; + } + + size_t n = file.read(in_message, sizeof(in_message) - 1); + file.close(); + in_message[n] = '\0'; + + LOG(F("Lendo pendente: ")); + LOGLN(pathFile); + LOG(F("Tamanho payload: ")); + LOGLN(n); + + DeserializationError deserializeError = deserializeJson(Json, in_message); if (deserializeError) { - LOG(F("deserializeJson() failed: ")); - LOGLN(deserializeError.f_str()); - return; + LOG(F("Falha ao desserializar, movendo para envios_corrompidos: ")); + LOGLN(pathFile); + + if(!SD.exists("envios_corrompidos")) + { + SD.mkdir("envios_corrompidos"); + } + char newPath[128]; + snprintf(newPath, sizeof(newPath), "%s/%s", "envios_corrompidos", currentFile); + SD.rename(pathFile, newPath); + continue; } if(MQTT_connected == true) { - serializeJson(Json, pub_message); - MQTT.publish(TOPICO_PUB_ENVIO_ABASTECIMENTO, pub_message, false); + LOG(F("Publicando em: ")); + LOGLN(TOPICO_PUB_ENVIO_ABASTECIMENTO); + serializeJson(Json, out_message); + MQTT.publish(TOPICO_PUB_ENVIO_ABASTECIMENTO, out_message, false); #ifdef DEBUG + LOG(F("Publicando pendente: ")); + LOGLN(pathFile); serializeJsonPretty(Json, Serial); #endif @@ -613,10 +657,7 @@ void enviarAbastecimento() { LOGLN(F("MQTT desconectado!")); } - } - else - { - LOGLN(F("Aparentemente a pasta esta vazia")); + break; } } if(file.isOpen()) @@ -1940,4 +1981,4 @@ void lerRotaSD() { root.close(); } -} \ No newline at end of file +}