Browse Source

atualização automática

main
Luis Fernando Sauthier 4 months ago
parent
commit
cc1fd0c458
  1. 3
      src/controle.h
  2. 17
      src/loop.h
  3. 85
      src/webserver.h

3
src/controle.h

@ -178,7 +178,6 @@ void abastecimento() @@ -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() @@ -314,6 +313,8 @@ void abastecimento()
file.close();
}
enviosPendentes();
LOGLN(F("Enviando dados de abastecimento..."));
if (MQTT_connected == true)

17
src/loop.h

@ -50,15 +50,15 @@ void loop() @@ -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() @@ -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() @@ -119,9 +127,6 @@ void loop()
#ifdef DEBUG
printDateTime();
#endif
#ifdef DEBUG
printLOG();
#endif

85
src/webserver.h

@ -432,7 +432,8 @@ void atualizaEnviosPendentes( String mensagem) @@ -432,7 +432,8 @@ void atualizaEnviosPendentes( String mensagem)
return;
}
if(Json[F("recebido")] == false)
bool recebidoAck = Json[F("recebido")].as<bool>();
if(!recebidoAck)
{
LOGLN(F("Recebido = false"));
return;
@ -476,13 +477,7 @@ void atualizaEnviosPendentes( String mensagem) @@ -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() @@ -555,8 +550,9 @@ void enviarAbastecimento()
{
LOGLN(F("Enviando dados de abastecimento pendente"));
StaticJsonDocument<MQTT_MAX_PACKET_SIZE> 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() @@ -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() @@ -613,10 +657,7 @@ void enviarAbastecimento()
{
LOGLN(F("MQTT desconectado!"));
}
}
else
{
LOGLN(F("Aparentemente a pasta esta vazia"));
break;
}
}
if(file.isOpen())

Loading…
Cancel
Save