astro timers implemented
This commit is contained in:
parent
513c9f6b89
commit
92a1cc3cc7
7
Kconfig
7
Kconfig
|
|
@ -180,13 +180,6 @@ menu "WebGUIApp"
|
||||||
endif
|
endif
|
||||||
endmenu
|
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"
|
menu "SPI settings"
|
||||||
config WEBGUIAPP_SPI_ENABLE
|
config WEBGUIAPP_SPI_ENABLE
|
||||||
|
|
|
||||||
|
|
@ -58,28 +58,6 @@ typedef struct
|
||||||
|
|
||||||
} cron_timer_t;
|
} 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();
|
esp_err_t ReloadCronSheduler();
|
||||||
char* GetCronError();
|
char* GetCronError();
|
||||||
void DebugTimer();
|
void DebugTimer();
|
||||||
|
|
@ -98,7 +76,7 @@ void AstroRecordsInterface(char *argres, int rw);
|
||||||
* \param act Index of the action
|
* \param act Index of the action
|
||||||
*/
|
*/
|
||||||
void custom_cron_execute(int obj, int act);
|
void custom_cron_execute(int obj, int act);
|
||||||
|
|
||||||
void SetSunTimes(uint32_t t);
|
void SetSunTimes(uint32_t t);
|
||||||
|
void MidnightTimer();
|
||||||
|
|
||||||
#endif /* COMPONENTS_WEBGUIAPP_INCLUDE_CRONTIMERS_H_ */
|
#endif /* COMPONENTS_WEBGUIAPP_INCLUDE_CRONTIMERS_H_ */
|
||||||
|
|
|
||||||
121
src/CronTimers.c
121
src/CronTimers.c
|
|
@ -25,6 +25,7 @@
|
||||||
#include "string.h"
|
#include "string.h"
|
||||||
#include <math.h>
|
#include <math.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
#include <freertos/task.h>
|
||||||
|
|
||||||
#define TAG "CRON_TIMER"
|
#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 char cron_express_error[CRON_EXPRESS_MAX_LENGTH];
|
||||||
|
|
||||||
static int GetSunEvent(uint8_t event, uint32_t unixt, float ang);
|
static int GetSunEvent(uint8_t event, uint32_t unixt, float ang);
|
||||||
|
static int RecalcAstro(uint32_t tt);
|
||||||
|
|
||||||
char* GetCronError()
|
char* GetCronError()
|
||||||
{
|
{
|
||||||
|
|
@ -43,12 +45,6 @@ void custom_cron_job_callback(cron_job *job)
|
||||||
ExecCommand(((cron_timer_t*) job->data)->exec);
|
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* check_expr(const char *expr)
|
||||||
{
|
{
|
||||||
const char *err = NULL;
|
const char *err = NULL;
|
||||||
|
|
@ -109,6 +105,7 @@ static void ExecuteLastAction(obj_struct_t *objarr)
|
||||||
void TimeObtainHandler(struct timeval *tm)
|
void TimeObtainHandler(struct timeval *tm)
|
||||||
{
|
{
|
||||||
ESP_LOGI(TAG, "Current time updated with value %d", (unsigned int )tm->tv_sec);
|
ESP_LOGI(TAG, "Current time updated with value %d", (unsigned int )tm->tv_sec);
|
||||||
|
RecalcAstro(tm->tv_sec);
|
||||||
ReloadCronSheduler();
|
ReloadCronSheduler();
|
||||||
ExecuteLastAction(GetSystemObjects());
|
ExecuteLastAction(GetSystemObjects());
|
||||||
ExecuteLastAction(GetCustomObjects());
|
ExecuteLastAction(GetCustomObjects());
|
||||||
|
|
@ -182,7 +179,6 @@ void CronRecordsInterface(char *argres, int rw)
|
||||||
time(&now);
|
time(&now);
|
||||||
int min = GetSunEvent((T.type == 1) ? 0 : 1, now, T.sun_angle);
|
int min = GetSunEvent((T.type == 1) ? 0 : 1, now, T.sun_angle);
|
||||||
sprintf(T.cron, "0 %d %d * * *", min % 60, min / 60);
|
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));
|
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);
|
return (int) floor(UT * 60.0);
|
||||||
}
|
}
|
||||||
|
|
||||||
void AstroRecordsInterface(char *argres, int rw)
|
static int RecalcAstro(uint32_t tt)
|
||||||
{
|
{
|
||||||
/*
|
struct timeval tv_now;
|
||||||
if (rw)
|
gettimeofday(&tv_now, NULL);
|
||||||
{
|
int timers_to_update = 0;
|
||||||
struct jReadElement result;
|
ESP_LOGI(TAG, "Recalculation astronomical events");
|
||||||
struct jReadElement arr;
|
for (int i = 0; i < CRON_TIMERS_NUMBER; i++)
|
||||||
jRead(argres, "", &result);
|
{
|
||||||
if (result.dataType == JREAD_OBJECT)
|
cron_timer_t *T = &GetSysConf()->Timers[i];
|
||||||
{
|
if (T->type == 0 || T->del)
|
||||||
GetSysConf()->Astro.lat = (float)jRead_double(argres, "{'lat'", 0);
|
continue;
|
||||||
GetSysConf()->Astro.lon = (float)jRead_double(argres, "{'lon'", 0);
|
int min = GetSunEvent((T->type == 1) ? 0 : 1, tt, T->sun_angle);
|
||||||
|
sprintf(T->cron, "0 %d %d * * *", min % 60, min / 60);
|
||||||
jRead(argres, "{'records'", &arr);
|
//ESP_LOGI(TAG, "Recalculated astro for rec %d new cron %s", T->num, T->cron);
|
||||||
char *asto_rec = (char*) arr.pValue;
|
++timers_to_update;
|
||||||
for (int i = 0; i < arr.elements; i++)
|
}
|
||||||
{
|
ESP_LOGI(TAG, "Recalculated %d astro timers", timers_to_update);
|
||||||
astro_timer_t T;
|
return timers_to_update;
|
||||||
T.num = jRead_int(asto_rec, "[*{'num'", &i);
|
}
|
||||||
T.del = jRead_int(asto_rec, "[*{'del'", &i);
|
|
||||||
T.enab = jRead_int(asto_rec, "[*{'enab'", &i);
|
#define MIDNIGHT_CHECK_INTERVAL 10
|
||||||
T.rise = jRead_int(asto_rec, "[*{'rise'", &i);
|
#define MIDNIGHT_DETECT_WINDOW 60
|
||||||
|
void MidnightTimer()
|
||||||
T.sensor_enab = jRead_int(asto_rec, "[*{'sensor_enab'", &i);
|
{
|
||||||
T.sensor_angle = (float) jRead_double(asto_rec, "[*{'sensor_angle'", &i);
|
static int cnt = MIDNIGHT_CHECK_INTERVAL;
|
||||||
T.main_angle = (float) jRead_double(asto_rec, "[*{'main_angle'", &i);
|
if (cnt-- <= 0)
|
||||||
//T.sensor_time = jRead_int(asto_rec, "[*{'sensor_time'", &i);
|
{
|
||||||
//T.main_time = jRead_int(asto_rec, "[*{'main_time'", &i);
|
cnt = MIDNIGHT_CHECK_INTERVAL;
|
||||||
|
struct timeval tv_now;
|
||||||
jRead_string(asto_rec, "[*{'name'", T.name, sizeof(T.name), &i);
|
gettimeofday(&tv_now, NULL);
|
||||||
jRead_string(asto_rec, "[*{'exec'", T.exec, sizeof(T.exec), &i);
|
//ESP_LOGI(TAG, "Seconds remains to midnight %d", (int )tv_now.tv_sec % 86400);
|
||||||
|
if (tv_now.tv_sec % 86400 < MIDNIGHT_DETECT_WINDOW)
|
||||||
time_t now;
|
{
|
||||||
time(&now);
|
if (RecalcAstro(tv_now.tv_sec))
|
||||||
T.sensor_time = GetSunEvent((T.rise) ? 0 : 1, now, T.sensor_angle);
|
ReloadCronSheduler();
|
||||||
T.main_time = GetSunEvent((T.rise) ? 0 : 1, now, T.main_angle);
|
cnt = MIDNIGHT_CHECK_INTERVAL + MIDNIGHT_DETECT_WINDOW;
|
||||||
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()
|
|
||||||
{
|
|
||||||
*/
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -370,14 +370,6 @@ static void funct_sd_block(char *argres, int rw)
|
||||||
}
|
}
|
||||||
#endif
|
#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)
|
static void funct_lat(char *argres, int rw)
|
||||||
{
|
{
|
||||||
if(rw)
|
if(rw)
|
||||||
|
|
@ -588,8 +580,6 @@ const rest_var_t SystemVariables[] =
|
||||||
{ 0, "sd_visible", (bool*) (&VAR_FALSE), VAR_BOOL, R, 0, 1 },
|
{ 0, "sd_visible", (bool*) (&VAR_FALSE), VAR_BOOL, R, 0, 1 },
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
{ 0, "astro_test", &funct_astro_test, VAR_FUNCT, RW, 0, 0 },
|
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
esp_err_t SetConfVar(char *name, char *val, rest_var_types *tp)
|
esp_err_t SetConfVar(char *name, char *val, rest_var_types *tp)
|
||||||
|
|
|
||||||
|
|
@ -153,6 +153,8 @@ void SetSystemTimeVal(struct timeval *tv, const char* source)
|
||||||
void SecondTickSystem(void *param)
|
void SecondTickSystem(void *param)
|
||||||
{
|
{
|
||||||
++UpTime;
|
++UpTime;
|
||||||
|
MidnightTimer();
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
uint32_t GetUpTime(void)
|
uint32_t GetUpTime(void)
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user