tested calculation of previous and next event's absolute time

This commit is contained in:
Bogdan Pilyugin 2023-04-23 08:33:03 +02:00
parent efd9318c65
commit 50e224c4d9
6 changed files with 48 additions and 14 deletions

View File

@ -29,6 +29,7 @@ timarr = [
~crontmr(15)~ ~crontmr(15)~
]; ];
function showcronerr(err) {if(err != "") alert(err);} function showcronerr(err) {if(err != "") alert(err);}
function dbgtmr(){PostData("tmrdbg=1", "application.html", false, false, false);}
</script> </script>
</head> </head>
<body> <body>
@ -45,6 +46,7 @@ function showcronerr(err) {if(err != "") alert(err);}
<div id="timer"></div> <div id="timer"></div>
</div> </div>
<button type="button" class ='btn' id='bt2' onclick="addtm()">Add timer</button> <button type="button" class ='btn' id='bt2' onclick="addtm()">Add timer</button>
<button type="button" class ='btn' id='bt2' onclick="dbgtmr()">Debug</button>
</form> </form>
</div> </div>
</div> </div>

@ -1 +1 @@
Subproject commit 37d410623cc54b37fc94f5d33dd5e9d660ed5f95 Subproject commit 4c2dd92bd130859ce3deabd0b9e361b1d603ed73

View File

@ -29,5 +29,6 @@
esp_err_t InitCronSheduler(); esp_err_t InitCronSheduler();
esp_err_t ReloadCronSheduler(); esp_err_t ReloadCronSheduler();
char* GetCronError(); char* GetCronError();
void DebugTimer();
#endif /* MAIN_INCLUDE_CRONTIMERS_H_ */ #endif /* MAIN_INCLUDE_CRONTIMERS_H_ */

View File

@ -10,6 +10,8 @@
int HTTPPrintCustom(httpd_req_t *req, char *buf, char *var, int arg); int HTTPPrintCustom(httpd_req_t *req, char *buf, char *var, int arg);
HTTP_IO_RESULT AfterPostHandlerCustom(httpd_req_t *req, const char *filename, char *PostData); HTTP_IO_RESULT AfterPostHandlerCustom(httpd_req_t *req, const char *filename, char *PostData);
void UserMQTTEventHndlr(int idx, void *handler_args, esp_event_base_t base, int32_t event_id, void *event_data); void UserMQTTEventHndlr(int idx, void *handler_args, esp_event_base_t base, int32_t event_id, void *event_data);
void TimeObtainHandler(struct timeval* tm);
const char my_context_data[] = "MyContextDataPassedIntoMQTTHandler"; const char my_context_data[] = "MyContextDataPassedIntoMQTTHandler";
@ -18,6 +20,8 @@ void app_main(void)
regHTTPPrintCustom(&HTTPPrintCustom); regHTTPPrintCustom(&HTTPPrintCustom);
regAfterPostHandlerCustom(&AfterPostHandlerCustom); regAfterPostHandlerCustom(&AfterPostHandlerCustom);
regUserEventHandler(&UserMQTTEventHndlr, (void*)my_context_data); regUserEventHandler(&UserMQTTEventHndlr, (void*)my_context_data);
regTimeSyncCallback(&TimeObtainHandler);
WebGuiAppInit(); WebGuiAppInit();
if (GetUserAppNeedReset()) if (GetUserAppNeedReset())
{ {
@ -26,8 +30,6 @@ void app_main(void)
} }
ESP_ERROR_CHECK(InitAppConfig()); ESP_ERROR_CHECK(InitAppConfig());
InitCronSheduler();
ReloadCronSheduler();
while (true) while (true)
{ {

View File

@ -39,7 +39,9 @@ void custom_cron_job_callback(cron_job *job)
int obj = ((cron_timer_t*) job->data)->obj; int obj = ((cron_timer_t*) job->data)->obj;
char *name = ((cron_timer_t*) job->data)->name; char *name = ((cron_timer_t*) job->data)->name;
//here call all timers jobs depends on object and action //here call all timers jobs depends on object and action
ESP_LOGI(TAG, "Executed timer '%s' action %d under object %d", name, act, obj); time_t now;
time(&now);
ESP_LOGI(TAG, "Executed timer '%s' action %d under object %d at time %d", name, act, obj, (unsigned int)now);
return; return;
} }
@ -49,8 +51,8 @@ esp_err_t InitCronSheduler()
return res; return res;
} }
const char* check_expr(const char *expr)
const char* check_expr(const char* expr) { {
const char *err = NULL; const char *err = NULL;
cron_expr test; cron_expr test;
memset(&test, 0, sizeof(test)); memset(&test, 0, sizeof(test));
@ -58,6 +60,29 @@ const char* check_expr(const char* expr) {
return err; return err;
} }
void TimeObtainHandler(struct timeval *tm)
{
ESP_LOGW(TAG, "Current time received with value %d", (unsigned int )tm->tv_sec);
ReloadCronSheduler();
}
void DebugTimer()
{
time_t now;
time(&now);
ESP_LOGW(TAG, "Timestamp %d", (unsigned int)now);
for (int i = 0; i < CRON_TIMERS_NUMBER; i++)
{
if(GetAppConf()->Timers[i].del) continue;
ESP_LOGW(TAG, "Cron expression:%s", GetAppConf()->Timers[i].cron);
cron_expr cron_exp = {0};
cron_parse_expr(GetAppConf()->Timers[i].cron, &cron_exp, NULL);
ESP_LOGW(TAG, "Timer %d prev: %u", i, (uint32_t)cron_prev(&cron_exp, now));
ESP_LOGW(TAG, "Timer %d next: %u", i, (uint32_t)cron_next(&cron_exp, now));
}
}
esp_err_t ReloadCronSheduler() esp_err_t ReloadCronSheduler()
{ {
//remove all jobs //remove all jobs
@ -81,7 +106,8 @@ esp_err_t ReloadCronSheduler()
(void*) &GetAppConf()->Timers[i]); (void*) &GetAppConf()->Timers[i]);
} }
} }
if(!isExpressError) cron_express_error[0] = 0x00; //clear last cron expression parse if (!isExpressError)
cron_express_error[0] = 0x00; //clear last cron expression parse
int jobs_num = cron_job_node_count(); int jobs_num = cron_job_node_count();
ESP_LOGI(TAG, "In config presents %d jobs", jobs_num); ESP_LOGI(TAG, "In config presents %d jobs", jobs_num);

View File

@ -101,7 +101,10 @@ static HTTP_IO_RESULT HTTPPostApplication(httpd_req_t *req, char *PostData)
} }
} }
} }
if (httpd_query_key_value(PostData, "tmrdbg", tmp, sizeof(tmp)) == ESP_OK)
{
DebugTimer();
}
return HTTP_IO_DONE; return HTTP_IO_DONE;
} }