astro data backend implemented

This commit is contained in:
Bogdan Pilyugin 2024-05-14 17:07:26 +02:00
parent 96a7238065
commit 7346de2bdf
5 changed files with 217 additions and 124 deletions

View File

@ -77,7 +77,7 @@ typedef struct
{ {
float lat; float lat;
float lon; float lon;
astro_timer_t timers[CONFIG_WEBGUIAPP_CRON_NUMBER] astro_timer_t records[CONFIG_WEBGUIAPP_CRON_NUMBER]
} astro_handle_t; } astro_handle_t;
@ -92,7 +92,8 @@ char* GetCronActionName(int idx);
char* GetCronActAvail(int idx); char* GetCronActAvail(int idx);
void TimeObtainHandler(struct timeval *tm); void TimeObtainHandler(struct timeval *tm);
void CronRecordsInterface(char *argres, int rw);
void AstroRecordsInterface(char *argres, int rw);
/** /**
* \brief Handle all actions under all objects * \brief Handle all actions under all objects
* \param obj Index of the object * \param obj Index of the object

View File

@ -235,7 +235,7 @@ typedef struct
cron_timer_t Timers[CONFIG_WEBGUIAPP_CRON_NUMBER]; cron_timer_t Timers[CONFIG_WEBGUIAPP_CRON_NUMBER];
#ifdef CONFIG_WEBGUIAPP_ASTRO_ENABLE #ifdef CONFIG_WEBGUIAPP_ASTRO_ENABLE
astro_handle_t Astro;
#endif #endif
} SYS_CONFIG; } SYS_CONFIG;

View File

@ -25,7 +25,6 @@
#include "string.h" #include "string.h"
#include <math.h> #include <math.h>
#define TAG "CRON_TIMER" #define TAG "CRON_TIMER"
static cron_job *JobsList[CONFIG_WEBGUIAPP_CRON_NUMBER]; static cron_job *JobsList[CONFIG_WEBGUIAPP_CRON_NUMBER];
@ -152,6 +151,52 @@ esp_err_t ReloadCronSheduler()
return ESP_OK; return ESP_OK;
} }
void CronRecordsInterface(char *argres, int rw)
{
if (rw)
{
struct jReadElement result;
cron_timer_t T = { 0 };
jRead(argres, "", &result);
if (result.dataType == JREAD_ARRAY)
{
int i;
for (i = 0; i < result.elements; i++)
{
T.num = jRead_int(argres, "[*{'num'", &i);
T.del = jRead_int(argres, "[*{'del'", &i);
T.enab = jRead_int(argres, "[*{'enab'", &i);
T.prev = jRead_int(argres, "[*{'prev'", &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);
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++)
{
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);
}
}
/************************ ASTRO *******************************/ /************************ ASTRO *******************************/
#define C (3.14159265/180.0) #define C (3.14159265/180.0)
@ -167,14 +212,12 @@ static uint16_t ssTime = 0;
static float Lat, Lon, Ang; static float Lat, Lon, Ang;
static int GetSunEvent(uint8_t event, uint32_t unixt, float ang);
static float GetSunEvent(uint8_t event, uint16_t day);
uint16_t NumberDayFromUnix(uint32_t t) uint16_t NumberDayFromUnix(uint32_t t)
{ {
time_t clock; time_t clock;
struct tm * tp; struct tm *tp;
clock = t; clock = t;
tp = gmtime(&clock); tp = gmtime(&clock);
return ((uint16_t) tp->tm_yday) + 1; return ((uint16_t) tp->tm_yday) + 1;
@ -189,24 +232,21 @@ void SetSunConditions(double lat, double lon, double ang)
void SetSunTimes(uint32_t t) void SetSunTimes(uint32_t t)
{ {
if (1)
{
double tt; double tt;
tt = GetSunEvent(0, NumberDayFromUnix(t)); tt = GetSunEvent(0, t, SUN_ANG);
if (tt > 0) if (tt > 0)
srTime = (uint16_t) (60.0 * tt); srTime = tt;
else else
srTime = 0xffff; //no valid sinrise time srTime = 0xffff; //no valid sinrise time
tt = GetSunEvent(1, NumberDayFromUnix(t)); tt = GetSunEvent(1, t, SUN_ANG);
if (tt > 0) if (tt > 0)
ssTime = (uint16_t) (60.0 * tt); ssTime = tt;
else else
ssTime = 0xffff; //no valid sunset time ssTime = 0xffff; //no valid sunset time
}
ESP_LOGI("ASTRO", "Day number %d", NumberDayFromUnix(t)); ESP_LOGI("ASTRO", "Day number %d", NumberDayFromUnix(t));
ESP_LOGI("ASTRO", "Sanrise %dh %dm", srTime/60 + 2, srTime - (srTime/60 * 60)); ESP_LOGI("ASTRO", "Sanrise %dh %dm", srTime / 60 + 2, srTime - (srTime / 60 * 60));
ESP_LOGI("ASTRO", "Sanset %dh %dm", ssTime/60 + 2 , ssTime - (ssTime/60 * 60)); ESP_LOGI("ASTRO", "Sanset %dh %dm", ssTime / 60 + 2, ssTime - (ssTime / 60 * 60));
} }
@ -220,16 +260,16 @@ uint16_t GetSunset(void)
return ssTime; return ssTime;
} }
static int GetSunEvent(uint8_t event, uint32_t unixt, float ang)
static float GetSunEvent(uint8_t event, uint16_t day)
{ {
float lngHour, t, M, L, RA, sinDec, cosDec, cosH, H, T, UT; float lngHour, t, M, L, RA, sinDec, cosDec, cosH, H, T, UT;
float Lquadrant, RAquadrant; float Lquadrant, RAquadrant;
float zen; float zen;
if (SUN_ANG == 0) int day = NumberDayFromUnix(unixt);
zen = zenith + (float) SUN_ANG; //sunrise/set if (ang == 0)
zen = zenith + (float) ang; //sunrise/set
else else
zen = 90.0 + (float) SUN_ANG; //twilight zen = 90.0 + (float) ang; //twilight
lngHour = LON / 15; lngHour = LON / 15;
if (event == 0) if (event == 0)
t = day + ((6 - lngHour) / 24); t = day + ((6 - lngHour) / 24);
@ -291,8 +331,78 @@ static float GetSunEvent(uint8_t event, uint16_t day)
{ {
UT = UT + 24; UT = UT + 24;
} }
return UT; return (int) floor(UT * 60.0);
} }
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 = jRead_double(argres, "{'lat'", 0);
GetSysConf()->Astro.lon = 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.sensor_time = jRead_int(asto_rec, "[*{'sensor_time'", &i);
T.main_angle = (float) jRead_double(asto_rec, "[*{'main_angle'", &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);
}

View File

@ -311,52 +311,15 @@ static void funct_ota_newver(char *argres, int rw)
//CRON implementation BEGIN //CRON implementation BEGIN
static void funct_cronrecs(char *argres, int rw) static void funct_cronrecs(char *argres, int rw)
{ {
if (rw) CronRecordsInterface(argres, rw);
{
struct jReadElement result;
cron_timer_t T = { 0 };
jRead(argres, "", &result);
if (result.dataType == JREAD_ARRAY)
{
int i;
for (i = 0; i < result.elements; i++)
{
T.num = jRead_int(argres, "[*{'num'", &i);
T.del = jRead_int(argres, "[*{'del'", &i);
T.enab = jRead_int(argres, "[*{'enab'", &i);
T.prev = jRead_int(argres, "[*{'prev'", &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);
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++)
{
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 implementation END //CRON implementation END
static void funct_astrorecs(char *argres, int rw)
{
AstroRecordsInterface(argres, rw);
}
static void funct_serial_mode(char *argres, int rw) static void funct_serial_mode(char *argres, int rw)
{ {
@ -411,7 +374,6 @@ static void funct_sd_block(char *argres, int rw)
} }
#endif #endif
static void funct_astro_test(char *argres, int rw) static void funct_astro_test(char *argres, int rw)
{ {
ESP_LOGI("API", "Astro test executed"); ESP_LOGI("API", "Astro test executed");
@ -420,7 +382,6 @@ static void funct_astro_test(char *argres, int rw)
} }
const int hw_rev = CONFIG_BOARD_HARDWARE_REVISION; const int hw_rev = CONFIG_BOARD_HARDWARE_REVISION;
const bool VAR_TRUE = true; const bool VAR_TRUE = true;
const bool VAR_FALSE = false; const bool VAR_FALSE = false;
@ -611,7 +572,8 @@ const rest_var_t SystemVariables[] =
#endif #endif
{ 0, "astro_test", &funct_astro_test, VAR_FUNCT, RW, 0, 0 }, { 0, "astro_test", &funct_astro_test, VAR_FUNCT, RW, 0, 0 },
#ifdef CONFIG_WEBGUIAPP_ASTRO_ENABLE #ifdef CONFIG_WEBGUIAPP_ASTRO_ENABLE
{ 0, "astrorecs", &funct_astrorecs, VAR_FUNCT, RW, 0, 0 },
#endif #endif

View File

@ -447,6 +447,26 @@ esp_netif_str_to_ip4(CONFIG_WEBGUIAPP_DNS3_ADDRESS_DEFAULT, (esp_ip4_addr_t*) &C
strcpy(Conf->Timers[i].exec, "OBJECT,ACTION,ARGUMENTS"); strcpy(Conf->Timers[i].exec, "OBJECT,ACTION,ARGUMENTS");
} }
#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
} }
esp_err_t ReadNVSSysConfig(SYS_CONFIG *SysConf) esp_err_t ReadNVSSysConfig(SYS_CONFIG *SysConf)