From 513c9f6b896f05c26fc69377e5cf2e5127012d03 Mon Sep 17 00:00:00 2001 From: bogd Date: Wed, 15 May 2024 15:49:55 +0200 Subject: [PATCH] astro timer combined with cron --- include/CronTimers.h | 14 ++- include/SysConfiguration.h | 7 +- src/CronTimers.c | 180 +++++++++++++++++++++---------------- src/RestApiHandler.c | 29 ++++-- src/SysConfiguration.c | 20 +---- 5 files changed, 136 insertions(+), 114 deletions(-) diff --git a/include/CronTimers.h b/include/CronTimers.h index cf5830a..97b7c81 100644 --- a/include/CronTimers.h +++ b/include/CronTimers.h @@ -50,6 +50,8 @@ typedef struct bool del; /*!< Flag of non valid record, free for future overwrite */ bool enab; /*!< Enable scheduler */ bool prev; /*!< Enable to execute nearest in the past sheduled action */ + int type; /*!< Type: manual, sunrise, sunset, more types... */ + float sun_angle;/*! +#include #define TAG "CRON_TIMER" static cron_job *JobsList[CONFIG_WEBGUIAPP_CRON_NUMBER]; static char cron_express_error[CRON_EXPRESS_MAX_LENGTH]; +static int GetSunEvent(uint8_t event, uint32_t unixt, float ang); + char* GetCronError() { return cron_express_error; @@ -167,34 +170,47 @@ void CronRecordsInterface(char *argres, int rw) T.del = jRead_int(argres, "[*{'del'", &i); T.enab = jRead_int(argres, "[*{'enab'", &i); T.prev = jRead_int(argres, "[*{'prev'", &i); + T.type = jRead_int(argres, "[*{'type'", &i); + T.sun_angle = (float) jRead_double(argres, "[*{'sun_angle'", &i); jRead_string(argres, "[*{'name'", T.name, sizeof(T.name), &i); jRead_string(argres, "[*{'cron'", T.cron, sizeof(T.cron), &i); jRead_string(argres, "[*{'exec'", T.exec, sizeof(T.exec), &i); + + if (T.type > 0) + { + time_t now; + time(&now); + int min = GetSunEvent((T.type == 1) ? 0 : 1, now, T.sun_angle); + sprintf(T.cron, "0 %d %d * * *", min % 60, min / 60); + //ESP_LOGI(TAG, "Set CRON to sun event with %dh %dm", min / 60, min % 60); + } + memcpy(&GetSysConf()->Timers[T.num - 1], &T, sizeof(cron_timer_t)); } ReloadCronSheduler(); } } - else + + struct jWriteControl jwc; + jwOpen(&jwc, argres, VAR_MAX_VALUE_LENGTH, JW_ARRAY, JW_COMPACT); + for (int idx = 0; idx < CRON_TIMERS_NUMBER; idx++) { - struct jWriteControl jwc; - jwOpen(&jwc, argres, VAR_MAX_VALUE_LENGTH, JW_ARRAY, JW_COMPACT); - for (int idx = 0; idx < CRON_TIMERS_NUMBER; idx++) - { - cron_timer_t T; - memcpy(&T, &GetSysConf()->Timers[idx], sizeof(cron_timer_t)); - jwArr_object(&jwc); - jwObj_int(&jwc, "num", (unsigned int) T.num); - jwObj_int(&jwc, "del", (T.del) ? 1 : 0); - jwObj_int(&jwc, "enab", (T.enab) ? 1 : 0); - jwObj_int(&jwc, "prev", (T.prev) ? 1 : 0); - jwObj_string(&jwc, "name", T.name); - jwObj_string(&jwc, "cron", T.cron); - jwObj_string(&jwc, "exec", T.exec); - jwEnd(&jwc); - } - jwClose(&jwc); + cron_timer_t T; + memcpy(&T, &GetSysConf()->Timers[idx], sizeof(cron_timer_t)); + jwArr_object(&jwc); + jwObj_int(&jwc, "num", (unsigned int) T.num); + jwObj_int(&jwc, "del", (T.del) ? 1 : 0); + jwObj_int(&jwc, "enab", (T.enab) ? 1 : 0); + jwObj_int(&jwc, "prev", (T.prev) ? 1 : 0); + jwObj_int(&jwc, "type", (unsigned int) T.type); + jwObj_double(&jwc, "sun_angle", (double) T.sun_angle); + jwObj_string(&jwc, "name", T.name); + jwObj_string(&jwc, "cron", T.cron); + jwObj_string(&jwc, "exec", T.exec); + jwEnd(&jwc); } + jwClose(&jwc); + } /************************ ASTRO *******************************/ @@ -270,7 +286,7 @@ static int GetSunEvent(uint8_t event, uint32_t unixt, float ang) zen = zenith + (float) ang; //sunrise/set else zen = 90.0 + (float) ang; //twilight - lngHour = GetSysConf()->Astro.lon / 15; + lngHour = GetSysConf()->sntpClient.lon / 15; if (event == 0) t = day + ((6 - lngHour) / 24); else @@ -303,7 +319,8 @@ static int GetSunEvent(uint8_t event, uint32_t unixt, float ang) RA = RA / 15; sinDec = 0.39782 * sin(L * C); cosDec = cos(asin(sinDec)); - cosH = (cos(zen * C) - (sinDec * sin(GetSysConf()->Astro.lat * C))) / (cosDec * cos(GetSysConf()->Astro.lat * C)); + cosH = (cos(zen * C) - (sinDec * sin(GetSysConf()->sntpClient.lat * C))) + / (cosDec * cos(GetSysConf()->sntpClient.lat * C)); if (event == 0) { //rise @@ -336,71 +353,78 @@ static int GetSunEvent(uint8_t event, uint32_t unixt, float ang) void AstroRecordsInterface(char *argres, int rw) { - if (rw) - { - struct jReadElement result; - struct jReadElement arr; - jRead(argres, "", &result); - if (result.dataType == JREAD_OBJECT) - { - GetSysConf()->Astro.lat = (float)jRead_double(argres, "{'lat'", 0); - GetSysConf()->Astro.lon = (float)jRead_double(argres, "{'lon'", 0); + /* + if (rw) + { + struct jReadElement result; + struct jReadElement arr; + jRead(argres, "", &result); + if (result.dataType == JREAD_OBJECT) + { + GetSysConf()->Astro.lat = (float)jRead_double(argres, "{'lat'", 0); + GetSysConf()->Astro.lon = (float)jRead_double(argres, "{'lon'", 0); - jRead(argres, "{'records'", &arr); - char *asto_rec = (char*) arr.pValue; - for (int i = 0; i < arr.elements; i++) - { - astro_timer_t T; - T.num = jRead_int(asto_rec, "[*{'num'", &i); - T.del = jRead_int(asto_rec, "[*{'del'", &i); - T.enab = jRead_int(asto_rec, "[*{'enab'", &i); - T.rise = jRead_int(asto_rec, "[*{'rise'", &i); + jRead(argres, "{'records'", &arr); + char *asto_rec = (char*) arr.pValue; + for (int i = 0; i < arr.elements; i++) + { + astro_timer_t T; + T.num = jRead_int(asto_rec, "[*{'num'", &i); + T.del = jRead_int(asto_rec, "[*{'del'", &i); + T.enab = jRead_int(asto_rec, "[*{'enab'", &i); + T.rise = jRead_int(asto_rec, "[*{'rise'", &i); - T.sensor_enab = jRead_int(asto_rec, "[*{'sensor_enab'", &i); - T.sensor_angle = (float) jRead_double(asto_rec, "[*{'sensor_angle'", &i); - T.main_angle = (float) jRead_double(asto_rec, "[*{'main_angle'", &i); - //T.sensor_time = jRead_int(asto_rec, "[*{'sensor_time'", &i); - //T.main_time = jRead_int(asto_rec, "[*{'main_time'", &i); + T.sensor_enab = jRead_int(asto_rec, "[*{'sensor_enab'", &i); + T.sensor_angle = (float) jRead_double(asto_rec, "[*{'sensor_angle'", &i); + T.main_angle = (float) jRead_double(asto_rec, "[*{'main_angle'", &i); + //T.sensor_time = jRead_int(asto_rec, "[*{'sensor_time'", &i); + //T.main_time = jRead_int(asto_rec, "[*{'main_time'", &i); - jRead_string(asto_rec, "[*{'name'", T.name, sizeof(T.name), &i); - jRead_string(asto_rec, "[*{'exec'", T.exec, sizeof(T.exec), &i); + jRead_string(asto_rec, "[*{'name'", T.name, sizeof(T.name), &i); + jRead_string(asto_rec, "[*{'exec'", T.exec, sizeof(T.exec), &i); - time_t now; - time(&now); - T.sensor_time = GetSunEvent((T.rise) ? 0 : 1, now, T.sensor_angle); - T.main_time = GetSunEvent((T.rise) ? 0 : 1, now, T.main_angle); - memcpy(&GetSysConf()->Astro.records[T.num - 1], &T, sizeof(astro_timer_t)); - } - } - } + time_t now; + time(&now); + T.sensor_time = GetSunEvent((T.rise) ? 0 : 1, now, T.sensor_angle); + T.main_time = GetSunEvent((T.rise) ? 0 : 1, now, T.main_angle); + memcpy(&GetSysConf()->Astro.records[T.num - 1], &T, sizeof(astro_timer_t)); + } + } + } - struct jWriteControl jwc; - jwOpen(&jwc, argres, VAR_MAX_VALUE_LENGTH, JW_OBJECT, JW_COMPACT); - jwObj_double(&jwc, "lat", GetSysConf()->Astro.lat); - jwObj_double(&jwc, "lon", GetSysConf()->Astro.lon); - jwObj_array(&jwc, "records"); - for (int idx = 0; idx < CRON_TIMERS_NUMBER; idx++) - { - astro_timer_t T; - memcpy(&T, &GetSysConf()->Astro.records[idx], sizeof(astro_timer_t)); - jwArr_object(&jwc); - jwObj_int(&jwc, "num", (unsigned int) T.num); - jwObj_int(&jwc, "del", (T.del) ? 1 : 0); - jwObj_int(&jwc, "enab", (T.enab) ? 1 : 0); - jwObj_int(&jwc, "rise", (T.rise) ? 1 : 0); - jwObj_int(&jwc, "sensor_enab", (T.sensor_enab) ? 1 : 0); - jwObj_double(&jwc, "sensor_angle", (double) T.sensor_angle); - jwObj_int(&jwc, "sensor_time", T.sensor_time); + struct jWriteControl jwc; + jwOpen(&jwc, argres, VAR_MAX_VALUE_LENGTH, JW_OBJECT, JW_COMPACT); + jwObj_double(&jwc, "lat", GetSysConf()->Astro.lat); + jwObj_double(&jwc, "lon", GetSysConf()->Astro.lon); + jwObj_array(&jwc, "records"); + for (int idx = 0; idx < CRON_TIMERS_NUMBER; idx++) + { + astro_timer_t T; + memcpy(&T, &GetSysConf()->Astro.records[idx], sizeof(astro_timer_t)); + jwArr_object(&jwc); + jwObj_int(&jwc, "num", (unsigned int) T.num); + jwObj_int(&jwc, "del", (T.del) ? 1 : 0); + jwObj_int(&jwc, "enab", (T.enab) ? 1 : 0); + jwObj_int(&jwc, "rise", (T.rise) ? 1 : 0); + jwObj_int(&jwc, "sensor_enab", (T.sensor_enab) ? 1 : 0); + jwObj_double(&jwc, "sensor_angle", (double) T.sensor_angle); + jwObj_int(&jwc, "sensor_time", T.sensor_time); - jwObj_double(&jwc, "main_angle", (double) T.main_angle); - jwObj_int(&jwc, "main_time", T.main_time); + jwObj_double(&jwc, "main_angle", (double) T.main_angle); + jwObj_int(&jwc, "main_time", T.main_time); - jwObj_string(&jwc, "name", T.name); - jwObj_string(&jwc, "exec", T.exec); - jwEnd(&jwc); - } - jwEnd(&jwc); - jwClose(&jwc); + jwObj_string(&jwc, "name", T.name); + jwObj_string(&jwc, "exec", T.exec); + jwEnd(&jwc); + } + jwEnd(&jwc); + jwClose(&jwc); + + } + + void InitAstro() + { + */ } diff --git a/src/RestApiHandler.c b/src/RestApiHandler.c index b9f1c74..1f0f7a4 100644 --- a/src/RestApiHandler.c +++ b/src/RestApiHandler.c @@ -315,10 +315,6 @@ static void funct_cronrecs(char *argres, int rw) } //CRON implementation END -static void funct_astrorecs(char *argres, int rw) -{ - AstroRecordsInterface(argres, rw); -} static void funct_serial_mode(char *argres, int rw) { @@ -382,6 +378,24 @@ static void funct_astro_test(char *argres, int rw) } +static void funct_lat(char *argres, int rw) +{ + if(rw) + { + GetSysConf()->sntpClient.lat = atof(argres); + } + snprintf(argres, VAR_MAX_VALUE_LENGTH, "%f", GetSysConf()->sntpClient.lat); +} + +static void funct_lon(char *argres, int rw) +{ + if(rw) + { + GetSysConf()->sntpClient.lon = atof(argres); + } + snprintf(argres, VAR_MAX_VALUE_LENGTH, "%f", GetSysConf()->sntpClient.lon); +} + const int hw_rev = CONFIG_BOARD_HARDWARE_REVISION; const bool VAR_TRUE = true; const bool VAR_FALSE = false; @@ -432,6 +446,9 @@ const rest_var_t SystemVariables[] = { 0, "sntp_serv3", &SysConfig.sntpClient.SntpServer3Adr, VAR_STRING, RW, 3, 32 }, { 0, "sntp_enab", &SysConfig.sntpClient.Flags1.bIsGlobalEnabled, VAR_BOOL, RW, 0, 1 }, + { 0, "lat", &funct_lat, VAR_FUNCT, RW, 0, 0 }, + { 0, "lon", &funct_lon, VAR_FUNCT, RW, 0, 0 }, + #if CONFIG_WEBGUIAPP_MQTT_ENABLE { 0, "mqtt_1_enab", &SysConfig.mqttStation[0].Flags1.bIsGlobalEnabled, VAR_BOOL, RW, 0, 1 }, { 0, "mqtt_1_serv", &SysConfig.mqttStation[0].ServerAddr, VAR_STRING, RW, 3, 63 }, @@ -572,10 +589,6 @@ const rest_var_t SystemVariables[] = #endif { 0, "astro_test", &funct_astro_test, VAR_FUNCT, RW, 0, 0 }, - #ifdef CONFIG_WEBGUIAPP_ASTRO_ENABLE - { 0, "astrorecs", &funct_astrorecs, VAR_FUNCT, RW, 0, 0 }, - -#endif }; diff --git a/src/SysConfiguration.c b/src/SysConfiguration.c index 3c8e181..de2c656 100644 --- a/src/SysConfiguration.c +++ b/src/SysConfiguration.c @@ -402,6 +402,8 @@ esp_netif_str_to_ip4(CONFIG_WEBGUIAPP_DNS3_ADDRESS_DEFAULT, (esp_ip4_addr_t*) &C sizeof(CONFIG_WEBGUIAPP_SNTP_HOST_3)); Conf->sntpClient.Flags1.bIsGlobalEnabled = CONFIG_WEBGUIAPP_SNTP_AUTOUPDATE_ENABLE; Conf->sntpClient.TimeZone = CONFIG_WEBGUIAPP_SNTP_TIMEZONE; + Conf->sntpClient.lat = 0.0; + Conf->sntpClient.lon = 0.0; #ifdef CONFIG_WEBGUIAPP_LORAWAN_ENABLE Conf->lorawanSettings.Flags1.bIsLoRaWANEnabled = true; @@ -442,6 +444,8 @@ esp_netif_str_to_ip4(CONFIG_WEBGUIAPP_DNS3_ADDRESS_DEFAULT, (esp_ip4_addr_t*) &C Conf->Timers[i].del = true; Conf->Timers[i].enab = false; Conf->Timers[i].prev = false; + Conf->Timers[i].type = 0; + Conf->Timers[i].sun_angle = 0; strcpy(Conf->Timers[i].name, "Timer Name"); strcpy(Conf->Timers[i].cron, "* * * * * *"); strcpy(Conf->Timers[i].exec, "OBJECT,ACTION,ARGUMENTS"); @@ -449,23 +453,7 @@ esp_netif_str_to_ip4(CONFIG_WEBGUIAPP_DNS3_ADDRESS_DEFAULT, (esp_ip4_addr_t*) &C } #ifdef CONFIG_WEBGUIAPP_ASTRO_ENABLE - Conf->Astro.lat = 0.00; - Conf->Astro.lon = 0.00; - for (int i = 0; i < CONFIG_WEBGUIAPP_CRON_NUMBER; i++) - { - Conf->Astro.records[i].num = i + 1; - Conf-> Astro.records[i].del = true; - Conf-> Astro.records[i].enab = false; - Conf->Astro.records[i].rise = true; - Conf->Astro.records[i].sensor_enab = false; - Conf->Astro.records[i].sensor_angle = 3.00; - Conf->Astro.records[i].sensor_time = 0; - Conf->Astro.records[i].main_angle = 6.00; - Conf->Astro.records[i].main_time = 0; - strcpy(Conf->Astro.records[i].name, "Astro Name"); - strcpy(Conf->Astro.records[i].exec, "OBJECT,ACTION,ARGUMENTS"); - } #endif }