diff --git a/main/CMakeLists.txt b/main/CMakeLists.txt index 2c0a75b..3442c6b 100644 --- a/main/CMakeLists.txt +++ b/main/CMakeLists.txt @@ -6,6 +6,7 @@ set(srcs main.c src/HTTPPostCustom.c src/MQTTCustom.c src/AppConfiguration.c + src/CronTimers.c ) set(include "include") diff --git a/main/include/CronTimers.h b/main/include/CronTimers.h new file mode 100644 index 0000000..2dddadc --- /dev/null +++ b/main/include/CronTimers.h @@ -0,0 +1,32 @@ + /* Copyright 2023 Bogdan Pilyugin + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * File name: CronTimers.h + * Project: webguiapp_ref_implement + * Created on: 2023-04-15 + * Author: bogdan + * Description: + */ + +#ifndef MAIN_INCLUDE_CRONTIMERS_H_ +#define MAIN_INCLUDE_CRONTIMERS_H_ +#include "esp_err.h" +#include "cron.h" +#include "jobs.h" + + +esp_err_t InitCronSheduler(); +esp_err_t ReloadCronSheduler(); + +#endif /* MAIN_INCLUDE_CRONTIMERS_H_ */ diff --git a/main/main.c b/main/main.c index e6070d2..340a6fe 100644 --- a/main/main.c +++ b/main/main.c @@ -5,6 +5,7 @@ #include "cron.h" #include "jobs.h" #include "AppConfiguration.h" +#include "CronTimers.h" 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); @@ -25,18 +26,8 @@ void app_main(void) } ESP_ERROR_CHECK(InitAppConfig()); - void test_cron_job_sample_callback(cron_job *job) - { - //int N = *((int*)(job->data)); - int N = 0; - //ESP_LOGW("MAIN", "Cron job executed with data %d", N); - return; - } - - cron_job *jobs[2]; - //jobs[0]=cron_job_create("* * * * * *",test_cron_job_sample_callback,(void *)0); - jobs[1] = cron_job_create("* * * * * *", test_cron_job_sample_callback, (void*) 10000); - cron_start(); + InitCronSheduler(); + ReloadCronSheduler(); while (true) { diff --git a/main/src/CronTimers.c b/main/src/CronTimers.c new file mode 100644 index 0000000..b3e0648 --- /dev/null +++ b/main/src/CronTimers.c @@ -0,0 +1,75 @@ +/* Copyright 2023 Bogdan Pilyugin + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * File name: CronTimers.c + * Project: webguiapp_ref_implement + * Created on: 2023-04-15 + * Author: bogdan + * Description: + */ + +#include "CronTimers.h" +#include "AppConfiguration.h" +#include "esp_log.h" + +static cron_job *jobs[CRON_TIMERS_NUMBER]; + +void custom_cron_job_callback(cron_job *job) +{ + int act = ((cron_timer_t*) job->data)->act; + int obj = ((cron_timer_t*) job->data)->obj; + char *name = ((cron_timer_t*) job->data)->name; + //here call all timers jobs depends on object and action + ESP_LOGI("CRON_TIMER", "Executed timer '%s' action %d under object %d", name, act, obj); + return; +} + +esp_err_t InitCronSheduler() +{ + esp_err_t res = ESP_OK; + return res; +} + +esp_err_t ReloadCronSheduler() +{ + static bool isStarted = false; + esp_err_t res = ESP_OK; + bool isJobsPresents = false; + for (int i = 0; i < CRON_TIMERS_NUMBER; i++) + { + if (jobs[i] != NULL) + { + cron_job_destroy(jobs[i]); + jobs[i] = NULL; + } + if (!GetAppConf()->Timers[i].del && GetAppConf()->Timers[i].enab) + { + + jobs[i] = cron_job_create(GetAppConf()->Timers[i].cron, custom_cron_job_callback, + (void*) &GetAppConf()->Timers[i]); + isJobsPresents = true; + } + + } + + + if (isJobsPresents && !isStarted) + { + cron_start(); + isStarted = true; + } + + return res; +} + diff --git a/main/src/HTTPPostCustom.c b/main/src/HTTPPostCustom.c index db13048..170c721 100644 --- a/main/src/HTTPPostCustom.c +++ b/main/src/HTTPPostCustom.c @@ -23,6 +23,7 @@ #include "webguiapp.h" #include "jRead.h" #include "AppConfiguration.h" +#include "CronTimers.h" const char pg_40[] = "index40.html"; const char pg_42[] = "index42.html"; @@ -73,14 +74,17 @@ static HTTP_IO_RESULT HTTPPostApplication(httpd_req_t *req, char *PostData) T.del = jRead_int(tmp, "{'del'", NULL); memcpy(&GetAppConf()->Timers[T.num-1], &T, sizeof(cron_timer_t)); WriteNVSAppConfig(GetAppConf()); + ReloadCronSheduler(); } } if (httpd_query_key_value(PostData, "deltimer", tmp, sizeof(tmp)) == ESP_OK) { int num = (atoi(tmp) - 1); - if(num >= 0 && num <16) + if(num >= 0 && num < 16) { GetAppConf()->Timers[num].del = true; + WriteNVSAppConfig(GetAppConf()); + ReloadCronSheduler(); } return HTTP_IO_DONE; } @@ -88,10 +92,11 @@ static HTTP_IO_RESULT HTTPPostApplication(httpd_req_t *req, char *PostData) { for(int i = 0; i< CRON_TIMERS_NUMBER; i++) { - if(GetAppConf()->Timers[i].del == 1) + if(GetAppConf()->Timers[i].del == true) { - GetAppConf()->Timers[i].del = 0; - + GetAppConf()->Timers[i].del = false; + WriteNVSAppConfig(GetAppConf()); + ReloadCronSheduler(); return HTTP_IO_DONE; } }