try fix restart wrong fonctionnement, old condition fonction nor rm

This commit is contained in:
Aurélien Gauthier 2022-06-07 14:02:42 +02:00
parent 44872fc71e
commit efa325b044

View file

@ -11,6 +11,8 @@
#define STORAGE_NAMESPACE "storage" #define STORAGE_NAMESPACE "storage"
#define RESTART 1 #define RESTART 1
#define REFRESHMEMBOOT 0 #define REFRESHMEMBOOT 0
#define RESTARTTIMES 2
#define REFRESHTIMES 4
using namespace std; using namespace std;
@ -36,6 +38,33 @@ NTPClient TimeClient(NtpUDP, "europe.pool.ntp.org", 7200, 60000);
//-------------------- CONDITION --------------------// //-------------------- CONDITION --------------------//
bool testTime (int hours, int minutes, bool restart){
Serial.println('\n');
Serial.println((REFRESHTIMES * TIME_TO_SLEEP) / 3600,DEC);
Serial.println(((REFRESHTIMES * TIME_TO_SLEEP) % 3600) / 60,DEC);
Serial.println('\n');
if (restart){ // restart parts
if(hours < (RESTARTTIMES * TIME_TO_SLEEP) / 3600) return true;
else{
if((hours = (RESTARTTIMES * TIME_TO_SLEEP) / 3600) && (minutes <= ((RESTARTTIMES * TIME_TO_SLEEP) % 3600) / 60))return true;
return false;
}
} else { // refresh parts
if(hours >= (RESTARTTIMES * TIME_TO_SLEEP) / 3600){
if(hours < (REFRESHTIMES * TIME_TO_SLEEP) / 3600)return true;
else{
if((hours = (REFRESHTIMES * TIME_TO_SLEEP) / 3600) && (minutes <= ((REFRESHTIMES * TIME_TO_SLEEP) % 3600) / 60))return true;
else{
return false;
}
}
}
}
return false;
}
/** /**
* @brief Time hour for restart or time for refresh restart persistant mem * @brief Time hour for restart or time for refresh restart persistant mem
* *
@ -46,10 +75,13 @@ NTPClient TimeClient(NtpUDP, "europe.pool.ntp.org", 7200, 60000);
*/ */
bool ishour(int hours, bool restart){ bool ishour(int hours, bool restart){
if (restart){ if (restart){
return hours <= (2 * TIME_TO_SLEEP) / 3600; Serial.println('\n');
Serial.println((RESTARTTIMES * TIME_TO_SLEEP) / 3600,DEC);
Serial.println('\n');
return hours <= (RESTARTTIMES * TIME_TO_SLEEP) / 3600;
} }
// Then refresh time // Then refresh time
return (hours > (2 * TIME_TO_SLEEP) / 3600) && (hours <= (4 * TIME_TO_SLEEP) / 3600); return (hours >= (RESTARTTIMES * TIME_TO_SLEEP) / 3600) && (hours <= (REFRESHTIMES * TIME_TO_SLEEP) / 3600);
} }
/** /**
@ -62,10 +94,12 @@ bool ishour(int hours, bool restart){
*/ */
bool isminute(int minutes, bool restart){ bool isminute(int minutes, bool restart){
if (restart){ if (restart){
return minutes <= ((2 * TIME_TO_SLEEP) % 3600) / 60; Serial.println(((RESTARTTIMES * TIME_TO_SLEEP) % 3600) / 60,DEC);
Serial.println('\n');
return minutes <= ((RESTARTTIMES * TIME_TO_SLEEP) % 3600) / 60;
} }
// Then refresh time // Then refresh time
return (minutes > ((2 * TIME_TO_SLEEP) % 3600)) / 60 && (minutes <= ((4 * TIME_TO_SLEEP) % 3600) / 60); return (minutes >= ((RESTARTTIMES * TIME_TO_SLEEP) % 3600) / 60) && (minutes <= ((REFRESHTIMES * TIME_TO_SLEEP) % 3600) / 60);
} }
/** /**
@ -76,13 +110,13 @@ bool isminute(int minutes, bool restart){
* @return true right secondes for restart or refresh * @return true right secondes for restart or refresh
* @return false bad secondes for restart or refresh * @return false bad secondes for restart or refresh
*/ */
bool issecond(int secondes, bool restart){ // bool issecond(int secondes, bool restart){
if (restart){ // if (restart){
return secondes <= ((2 * TIME_TO_SLEEP) - 1 ) % 60; // return secondes <= ((2 * TIME_TO_SLEEP) - 1 ) % 60;
} // }
// Then refresh time // // Then refresh time
return (secondes > ((2 * TIME_TO_SLEEP) - 1 ) % 60) && (secondes <= ((4 * TIME_TO_SLEEP) - 1 ) % 60); // return (secondes > ((2 * TIME_TO_SLEEP) - 1 ) % 60) && (secondes <= ((4 * TIME_TO_SLEEP) - 1 ) % 60);
} // }
//-------------------- FONCTIONS --------------------// //-------------------- FONCTIONS --------------------//
@ -249,6 +283,7 @@ bool TestBoot(bool resOrRfsh)
nvs_handle_t my_handle; nvs_handle_t my_handle;
esp_err_t err; esp_err_t err;
bool booted = false; bool booted = false;
//Serial.println("Test");
// Open // Open
err = nvs_open(STORAGE_NAMESPACE, NVS_READWRITE, &my_handle); err = nvs_open(STORAGE_NAMESPACE, NVS_READWRITE, &my_handle);
@ -305,13 +340,13 @@ bool TestBoot(bool resOrRfsh)
// Close // Close
nvs_close(my_handle); nvs_close(my_handle);
Serial.println("not restart"); Serial.println("not restart");
return false; return true;
} else { } else {
// Close // Close
nvs_close(my_handle); nvs_close(my_handle);
Serial.println("already restart"); Serial.println("already restart");
return true; return false;
} }
/* Not needed but finish properly the function */ /* Not needed but finish properly the function */
@ -345,12 +380,12 @@ void setup()
* @brief restart the ESP if there is the time restart * @brief restart the ESP if there is the time restart
* *
*/ */
if(ishour(hours,RESTART) && isminute(minutes,RESTART) && issecond(secondes,RESTART) && TestBoot(RESTART)){ if(TestBoot(RESTART) && testTime(hours, minutes, RESTART)){
ESP.restart(); ESP.restart();
} else if(ishour(hours,REFRESHMEMBOOT) && isminute(minutes,REFRESHMEMBOOT) && issecond(secondes,REFRESHMEMBOOT)){ } else if(testTime(hours, minutes, REFRESHMEMBOOT)){
TestBoot(REFRESHMEMBOOT); TestBoot(REFRESHMEMBOOT);
} }
/* ######### test time part ######### /* ######### test time part ######### */
Serial.println('\n'); Serial.println('\n');
Serial.println(hours,DEC); Serial.println(hours,DEC);
Serial.println(':'); Serial.println(':');
@ -358,7 +393,7 @@ void setup()
Serial.println(':'); Serial.println(':');
Serial.println(secondes,DEC); Serial.println(secondes,DEC);
Serial.println('\n'); Serial.println('\n');
*/
} }