implemented backend restrictions for cron object's actions

This commit is contained in:
Bogdan Pilyugin 2023-05-19 14:23:57 +02:00
parent d4da408541
commit 578093ad23
13 changed files with 1171 additions and 1696 deletions

2547
.project

File diff suppressed because it is too large Load Diff

View File

@ -8,26 +8,9 @@
<script src="res/main.js"></script>
<script src="res/res.js"></script>
<script src="res/cron.js"></script>
<script src="res/crondata.js"></script>
<title>~name~</title>
<script type="text/javascript">
timarr = [
~crontmr(0)~,
~crontmr(1)~,
~crontmr(2)~,
~crontmr(3)~,
~crontmr(4)~,
~crontmr(5)~,
~crontmr(6)~,
~crontmr(7)~,
~crontmr(8)~,
~crontmr(9)~,
~crontmr(10)~,
~crontmr(11)~,
~crontmr(12)~,
~crontmr(13)~,
~crontmr(14)~,
~crontmr(15)~
];
function showcronerr(err) {if(err != "") alert(err);}
function dbgtmr(){PostData("tmrdbg=1", "application.html", false, false, false);}
</script>

View File

@ -20,6 +20,7 @@ res
res/appstyles.css
res/ca_cert.pem
res/cron.js
res/crondata.js
res/iconsfont.css
res/iconsfont.woff2
res/logo.png

View File

@ -3,7 +3,8 @@
--bgr-color: #1D2327;
--panel-bgr-color: #2c3338;
--menu-bgr-color: #3c434a;
--header-bgr-color: #646970;
/* --header-bgr-color: #646970;
--header-bgr-color: #dba617;*/
--accent-color: #dba617;
--text-color:#F0F0F1;

47
HTML/res/crondata.js Normal file
View File

@ -0,0 +1,47 @@
timarr = [
~crontmr(0)~,
~crontmr(1)~,
~crontmr(2)~,
~crontmr(3)~,
~crontmr(4)~,
~crontmr(5)~,
~crontmr(6)~,
~crontmr(7)~,
~crontmr(8)~,
~crontmr(9)~,
~crontmr(10)~,
~crontmr(11)~,
~crontmr(12)~,
~crontmr(13)~,
~crontmr(14)~,
~crontmr(15)~
];
cron_objs= [
~cronobjs(0)~,
~cronobjs(1)~,
~cronobjs(2)~,
~cronobjs(3)~,
~cronobjs(4)~,
~cronobjs(5)~,
~cronobjs(6)~,
~cronobjs(7)~,
~cronobjs(8)~,
~cronobjs(9)~,
~cronobjs(10)~,
~cronobjs(11)~,
~cronobjs(12)~,
~cronobjs(13)~,
~cronobjs(14)~,
~cronobjs(15)~
];
cron_acts = [
~cronacts(0)~,
~cronacts(1)~,
~cronacts(2)~,
~cronacts(3)~,
~cronacts(4)~,
~cronacts(5)~,
~cronacts(6)~
];

View File

@ -48,7 +48,8 @@ img {
top: 0;
left: 0;
width: 100%;
background-color: var(--header-bgr-color);
background-color: var(--accent-color);
padding: 0 1rem;
z-index: var(--z-fixed);
}
@ -67,8 +68,12 @@ img {
}
.header__logo {
color: var(--text-color);
color: var(--menu-bgr-color);
font-weight: var(--font-semi-bold);
/*color: var(--text-color);
font-weight: var(--font-medium);
*/
font-size: var(--large-font-size);
margin-left: 50px;

@ -1 +0,0 @@
Subproject commit e6cb1512fd3fb4fdb07ee1132d625f861331447d

View File

@ -4,6 +4,6 @@ dependencies:
source:
type: idf
version: 4.4.4
manifest_hash: 31dd4ec84ade1450fc168388f4adce2efacd1516170670735140bc772e9d72bd
manifest_hash: f9b3d78d7d56685d1c543701158a0fab7a56c908117ed70644f77e4247c831ae
target: esp32
version: 1.0.0

View File

@ -27,6 +27,7 @@
#include <stdint.h>
#include <stdbool.h>
#include "esp_netif.h"
#include "CronTimers.h"
/// Application-dependent structure used to contain address information
@ -38,34 +39,11 @@
* saved one. If find difference (due to eeprom memory distortions),\n
* the default values will be loaded.
*/
#define CRON_TIMERS_NUMBER (16)
#define TIMER_NAME_LENGTH (16)
#define TIMER_CRONSTRING_LENGTH (32)
#define CRON_EXPRESS_MAX_LENGTH (128)
/**
* Cron scheduler configuration structure
*/
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 */
char name[TIMER_NAME_LENGTH]; /*!< Human readable name of scheduler */
int obj; /*!< Index of object scheduler affected on */
int act; /*!< Index of action with the object*/
char cron[TIMER_CRONSTRING_LENGTH]; /*!< Cron expression */
} cron_timer_t;
typedef struct appconf
{
int test;
cron_timer_t Timers[CRON_TIMERS_NUMBER];
cron_obj_t CronObjects[CRON_OBJECTS_NUMBER];
} APP_CONFIG;
void UserInitIO(void);

View File

@ -26,11 +26,46 @@
#include "jobs.h"
#include "ccronexpr.h"
#define CRON_TIMERS_NUMBER (16)
#define TIMER_NAME_LENGTH (16)
#define TIMER_CRONSTRING_LENGTH (32)
#define CRON_EXPRESS_MAX_LENGTH (128)
#define CRON_OBJECTS_NUMBER (16)
#define CRON_OBJECT_NAME_LENGTH (16)
typedef struct
{
int idx;
char objname[CRON_OBJECT_NAME_LENGTH];
} cron_obj_t;
/**
* Cron scheduler configuration structure
*/
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 */
char name[TIMER_NAME_LENGTH]; /*!< Human readable name of scheduler */
int obj; /*!< Index of object scheduler affected on */
int act; /*!< Index of action with the object*/
char cron[TIMER_CRONSTRING_LENGTH]; /*!< Cron expression */
} cron_timer_t;
esp_err_t InitCronSheduler();
esp_err_t ReloadCronSheduler();
char* GetCronError();
void DebugTimer();
char* GetCronObjectNameDef(int idx);
char* GetCronObjectName(int idx);
char* GetCronActionName(int idx);
char* GetCronActAvail(int idx);
/**
* \brief Handle all actions under all objects
* \param obj Index of the object

View File

@ -125,6 +125,12 @@ void ResetAppConfig(APP_CONFIG *Conf)
strcpy(GetAppConf()->Timers[i].cron, "* * * * * *");
}
for (int i = 0; i < CRON_OBJECTS_NUMBER; i++ )
{
strcpy(GetAppConf()->CronObjects[i].objname, GetCronObjectNameDef(i));
GetAppConf()->CronObjects[i].idx = i;
}
}
esp_err_t ReadNVSAppConfig(APP_CONFIG *AppConf)

View File

@ -26,8 +26,57 @@
#define TAG "CRON_TIMER"
const char *cron_objects[] = { "RELAY1", "RELAY2", "RELAY3", "RELAY4", "RELAY5", "SYSTEM" };
const char *cron_actions[] = { "ON", "OFF", "REBOOT" };
const char *cron_actions[] = { "ON", "OFF", "TOGGLE", "REBOOT" };
const char *cron_objects[] = {
"RELAY1",
"RELAY2",
"RELAY3",
"RELAY4",
"RELAY5",
"RELAY6",
"RELAY7",
"RELAY8",
"SYSTEM" };
const char *cron_act_avail[] = {
"[0,1,2]",
"[0,1,2]",
"[0,1,2]",
"[0,1,2]",
"[0,1,2]",
"[0,1,2]",
"[0,1,2]",
"[0,1,2]",
"[3]" };
char* GetCronObjectNameDef(int idx)
{
if(idx < 0 || idx >= sizeof(cron_objects)/sizeof(char*))
return "";
return (char*)cron_objects[idx];
}
char* GetCronObjectName(int idx)
{
if(idx < 0 || idx >= sizeof(cron_objects)/sizeof(char*))
return "";
return GetAppConf()->CronObjects[idx].objname;
}
char* GetCronActionName(int idx)
{
if(idx < 0 || idx >= sizeof(cron_actions)/sizeof(char*))
return "";
return (char*)cron_actions[idx];
}
char* GetCronActAvail(int idx)
{
if(idx < 0 || idx >= sizeof(cron_act_avail)/sizeof(char*))
return "[]";
return (char*)cron_act_avail[idx];
}
static cron_job *JobsList[CRON_TIMERS_NUMBER];
static char cron_express_error[CRON_EXPRESS_MAX_LENGTH];
@ -114,9 +163,7 @@ static void ExecuteLastAction()
LogFile("cron.log", "Execute last action %d under object %d", act, obj);
custom_cron_execute(obj, act);
}
}
}
void TimeObtainHandler(struct timeval *tm)
@ -129,21 +176,6 @@ void TimeObtainHandler(struct timeval *tm)
void DebugTimer()
{
/*
time_t now;
time(&now);
ESP_LOGW(TAG, "Timestamp %d", (unsigned int )now);
for (int i = 0; i < CRON_TIMERS_NUMBER; i++)
{
if (GetAppConf()->Timers[i].del)
continue;
ESP_LOGW(TAG, "Cron expression:%s", GetAppConf()->Timers[i].cron);
cron_expr cron_exp = { 0 };
cron_parse_expr(GetAppConf()->Timers[i].cron, &cron_exp, NULL);
ESP_LOGW(TAG, "Timer %d prev: %u", i, (unsigned int )cron_prev(&cron_exp, now));
ESP_LOGW(TAG, "Timer %d next: %u", i, (unsigned int )cron_next(&cron_exp, now));
}
*/
ExecuteLastAction();
}

View File

@ -30,7 +30,7 @@ static void HTTPPrint_crontmr(char *VarData, void *arg)
int idx = *((int*)(arg));
if(idx < CRON_TIMERS_NUMBER)
{
char data[256];
char data[MAX_DYNVAR_LENGTH];
cron_timer_t T;
memcpy(&T, &GetAppConf()->Timers[idx], sizeof(cron_timer_t));
jwOpen(data, sizeof(data), JW_OBJECT, JW_COMPACT);
@ -52,6 +52,26 @@ static void HTTPPrint_cronerr(char *VarData, void *arg)
{
snprintf(VarData, MAX_DYNVAR_LENGTH, GetCronError());
}
static void HTTPPrint_cronobjs(char *VarData, void *arg)
{
char data[MAX_DYNVAR_LENGTH];
int idx = *((int*)(arg));
if(idx < CRON_OBJECTS_NUMBER)
{
jwOpen(data, sizeof(data), JW_OBJECT, JW_COMPACT);
jwObj_string("name", GetAppConf()->CronObjects[idx].objname);
jwObj_raw("acts", GetCronActAvail(idx));
jwEnd();
jwClose();
snprintf(VarData, MAX_DYNVAR_LENGTH, "%s", data);
}
}
static void HTTPPrint_cronacts(char *VarData, void *arg)
{
int idx = *((int*) (arg));
snprintf(VarData, MAX_DYNVAR_LENGTH, "\"%s\"", GetCronActionName(idx));
}
//Default string if not found handler
static void HTTPPrint_DEF(char *VarData, void *arg)
@ -68,6 +88,9 @@ dyn_var_handler_t HANDLERS_ARRAY_CUST[] = {
{ "crontmr", sizeof("crontmr") - 1, &HTTPPrint_crontmr },
{ "cronerr", sizeof("cronerr") - 1, &HTTPPrint_cronerr },
{ "cronobjs", sizeof("cronobjs") - 1, &HTTPPrint_cronobjs },
{ "cronacts", sizeof("cronacts") - 1, &HTTPPrint_cronacts },
/*ERROR report*/
{ "status_fail", sizeof("status_fail") - 1, &HTTPPrint_status_fail },