From dbc2cc4b49c2fe53989b066146b06448baf6e797 Mon Sep 17 00:00:00 2001 From: Tester23 Date: Sun, 1 Dec 2024 13:08:16 +0100 Subject: [PATCH] proj --- openBeken_win32_mvsc2017.vcxproj | 1 + openBeken_win32_mvsc2017.vcxproj.filters | 1 + src/driver/drv_local.h | 1 + src/driver/drv_widget.c | 72 ++++++++++++++++++++++-- src/httpserver/http_fns.c | 7 +++ 5 files changed, 77 insertions(+), 5 deletions(-) diff --git a/openBeken_win32_mvsc2017.vcxproj b/openBeken_win32_mvsc2017.vcxproj index 4ba0f678f..b1e14f256 100644 --- a/openBeken_win32_mvsc2017.vcxproj +++ b/openBeken_win32_mvsc2017.vcxproj @@ -205,6 +205,7 @@ + true true diff --git a/openBeken_win32_mvsc2017.vcxproj.filters b/openBeken_win32_mvsc2017.vcxproj.filters index fba5ff623..ae9cf1bd0 100644 --- a/openBeken_win32_mvsc2017.vcxproj.filters +++ b/openBeken_win32_mvsc2017.vcxproj.filters @@ -342,6 +342,7 @@ + diff --git a/src/driver/drv_local.h b/src/driver/drv_local.h index 4c047f303..e50ccb572 100644 --- a/src/driver/drv_local.h +++ b/src/driver/drv_local.h @@ -96,6 +96,7 @@ void DRV_Toggler_QuickTick(); void DRV_InitPWMToggler(); void DRV_Widget_AddToHtmlPage(http_request_t *request); +void DRV_Widget_BeforeState(http_request_t* request); void DRV_Widget_Init(); void DRV_HTTPButtons_ProcessChanges(http_request_t* request); diff --git a/src/driver/drv_widget.c b/src/driver/drv_widget.c index ec3afc5d7..bf7a08df5 100644 --- a/src/driver/drv_widget.c +++ b/src/driver/drv_widget.c @@ -11,6 +11,7 @@ /* startDriver widget +widget_create 0 0 demo1.html */ const char *demo = @@ -24,19 +25,80 @@ const char *demo = "}" ""; +typedef enum { + WIDGET_STATIC, + WIDGET_STATE, +} WidgetLocation; -void DRV_Widget_AddToHtmlPage(http_request_t *request) { - poststr(request, demo); +typedef struct widget_s { + char *fname; + WidgetLocation location; + char *cached; + byte bAllowCache; + struct widget_s *next; +} widget_t; + +widget_t *g_widgets = 0; + +void DRV_Widget_Display(http_request_t *request, widget_t *w) { + // fast path - cached + if (w->cached) { + poststr(request, w->cached); + return; + } + char *data = (char*)LFS_ReadFile(w->fname); + if (data == 0) { + return; + } + poststr(request, data); + + if (w->bAllowCache) { + w->cached = data; + return; + } + free(data); +} +void DRV_Widget_DisplayList(http_request_t* request, WidgetLocation loc) { + widget_t *w = g_widgets; + while (w) { + if (w->location == loc) { + DRV_Widget_Display(request, w); + } + w = w->next; + } +} +void DRV_Widget_BeforeState(http_request_t* request) { + DRV_Widget_DisplayList(request, WIDGET_STATIC); } +void DRV_Widget_AddToHtmlPage(http_request_t *request) { + DRV_Widget_DisplayList(request, WIDGET_STATE); +} +void Widget_Add(widget_t **first, widget_t *n) { + if (*first == 0) { + *first = n; + } + else { + widget_t *tmp = *first; + while (tmp->next) { + tmp = tmp->next; + } + tmp->next = n; + } +} static commandResult_t CMD_Widget_Create(const void *context, const char *cmd, const char *args, int flags) { Tokenizer_TokenizeString(args, TOKENIZER_ALLOW_QUOTES); - if(Tokenizer_GetArgsCount()<=1) { + if(Tokenizer_GetArgsCount()<3) { return CMD_RES_NOT_ENOUGH_ARGUMENTS; } - const char *fname = Tokenizer_GetArg(0); - + widget_t *n = malloc(sizeof(widget_t)); + memset(n, 0, sizeof(widget_t)); + n->location = Tokenizer_GetArgInteger(0); + n->bAllowCache = Tokenizer_GetArgInteger(1); + const char *fname = Tokenizer_GetArg(2); + n->fname = strdup(fname); + Widget_Add(&g_widgets, n); return CMD_RES_OK; } void DRV_Widget_Init() { diff --git a/src/httpserver/http_fns.c b/src/httpserver/http_fns.c index 7285ebad6..42e803982 100644 --- a/src/httpserver/http_fns.c +++ b/src/httpserver/http_fns.c @@ -312,6 +312,13 @@ int http_fn_index(http_request_t* request) { MAIN_ScheduleUnsafeInit(3); } poststr(request, ""); // end div#change + + +#if defined(ENABLE_DRIVER_WIDGET) + if (DRV_IsRunning("Widget")) { + DRV_Widget_BeforeState(request); + } +#endif poststr(request, "
"); // replaceable content follows }