astro timers implemented

This commit is contained in:
Bogdan Pilyugin 2024-05-16 14:23:35 +02:00
parent 513c9f6b89
commit 92a1cc3cc7
5 changed files with 43 additions and 121 deletions

View File

@ -180,13 +180,6 @@ menu "WebGUIApp"
endif
endmenu
menu "ASTRO timer settings"
config WEBGUIAPP_ASTRO_ENABLE
bool "Enabled ASTRO relay timer"
default n
help
Include ASTRO timer functionality into firmware
endmenu
menu "SPI settings"
config WEBGUIAPP_SPI_ENABLE

View File

@ -58,28 +58,6 @@ typedef struct
} cron_timer_t;
typedef struct
{
int num; /*!< Index of sheduler */
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;/*!<Sun angle unconditional event issue*/
char name[TIMER_NAME_LENGTH]; /*!< Human readable name of scheduler */
char cron[TIMER_CRONSTRING_LENGTH]; /*!< Cron expression */
char exec[TIMER_EXECSTRING_LENGTH]; /*!< Cron command string */
} astro_timer_t;
typedef struct
{
float lat;
float lon;
astro_timer_t records[CONFIG_WEBGUIAPP_CRON_NUMBER]
} astro_handle_t;
esp_err_t InitCronSheduler();
esp_err_t ReloadCronSheduler();
char* GetCronError();
void DebugTimer();
@ -98,7 +76,7 @@ void AstroRecordsInterface(char *argres, int rw);
* \param act Index of the action
*/
void custom_cron_execute(int obj, int act);
void SetSunTimes(uint32_t t);
void MidnightTimer();
#endif /* COMPONENTS_WEBGUIAPP_INCLUDE_CRONTIMERS_H_ */

View File

@ -25,6 +25,7 @@
#include "string.h"
#include <math.h>
#include <stdlib.h>
#include <freertos/task.h>
#define TAG "CRON_TIMER"
@ -32,6 +33,7 @@ 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);
static int RecalcAstro(uint32_t tt);
char* GetCronError()
{
@ -43,12 +45,6 @@ void custom_cron_job_callback(cron_job *job)
ExecCommand(((cron_timer_t*) job->data)->exec);
}
esp_err_t InitCronSheduler()
{
esp_err_t res = ESP_OK;
return res;
}
const char* check_expr(const char *expr)
{
const char *err = NULL;
@ -109,6 +105,7 @@ static void ExecuteLastAction(obj_struct_t *objarr)
void TimeObtainHandler(struct timeval *tm)
{
ESP_LOGI(TAG, "Current time updated with value %d", (unsigned int )tm->tv_sec);
RecalcAstro(tm->tv_sec);
ReloadCronSheduler();
ExecuteLastAction(GetSystemObjects());
ExecuteLastAction(GetCustomObjects());
@ -182,7 +179,6 @@ void CronRecordsInterface(char *argres, int rw)
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));
@ -351,80 +347,43 @@ static int GetSunEvent(uint8_t event, uint32_t unixt, float ang)
return (int) floor(UT * 60.0);
}
void AstroRecordsInterface(char *argres, int rw)
static int RecalcAstro(uint32_t tt)
{
/*
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);
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);
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);
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);
}
void InitAstro()
{
*/
struct timeval tv_now;
gettimeofday(&tv_now, NULL);
int timers_to_update = 0;
ESP_LOGI(TAG, "Recalculation astronomical events");
for (int i = 0; i < CRON_TIMERS_NUMBER; i++)
{
cron_timer_t *T = &GetSysConf()->Timers[i];
if (T->type == 0 || T->del)
continue;
int min = GetSunEvent((T->type == 1) ? 0 : 1, tt, T->sun_angle);
sprintf(T->cron, "0 %d %d * * *", min % 60, min / 60);
//ESP_LOGI(TAG, "Recalculated astro for rec %d new cron %s", T->num, T->cron);
++timers_to_update;
}
ESP_LOGI(TAG, "Recalculated %d astro timers", timers_to_update);
return timers_to_update;
}
#define MIDNIGHT_CHECK_INTERVAL 10
#define MIDNIGHT_DETECT_WINDOW 60
void MidnightTimer()
{
static int cnt = MIDNIGHT_CHECK_INTERVAL;
if (cnt-- <= 0)
{
cnt = MIDNIGHT_CHECK_INTERVAL;
struct timeval tv_now;
gettimeofday(&tv_now, NULL);
//ESP_LOGI(TAG, "Seconds remains to midnight %d", (int )tv_now.tv_sec % 86400);
if (tv_now.tv_sec % 86400 < MIDNIGHT_DETECT_WINDOW)
{
if (RecalcAstro(tv_now.tv_sec))
ReloadCronSheduler();
cnt = MIDNIGHT_CHECK_INTERVAL + MIDNIGHT_DETECT_WINDOW;
}
}
}

View File

@ -370,14 +370,6 @@ static void funct_sd_block(char *argres, int rw)
}
#endif
static void funct_astro_test(char *argres, int rw)
{
ESP_LOGI("API", "Astro test executed");
uint32_t unix = atoi(argres);
SetSunTimes(unix);
}
static void funct_lat(char *argres, int rw)
{
if(rw)
@ -588,8 +580,6 @@ const rest_var_t SystemVariables[] =
{ 0, "sd_visible", (bool*) (&VAR_FALSE), VAR_BOOL, R, 0, 1 },
#endif
{ 0, "astro_test", &funct_astro_test, VAR_FUNCT, RW, 0, 0 },
};
esp_err_t SetConfVar(char *name, char *val, rest_var_types *tp)

View File

@ -153,6 +153,8 @@ void SetSystemTimeVal(struct timeval *tv, const char* source)
void SecondTickSystem(void *param)
{
++UpTime;
MidnightTimer();
}
uint32_t GetUpTime(void)