From a587d47bd9beed229e01ea762fabb0da8beb64f7 Mon Sep 17 00:00:00 2001
From: Indu Prakash <6459774+iprak@users.noreply.github.com>
Date: Fri, 10 Feb 2023 09:18:49 -0600
Subject: [PATCH] Consolidated some code, removed white spaces (#659)
---
src/httpserver/http_fns.c | 196 ++++++++++++++++++--------------------
1 file changed, 92 insertions(+), 104 deletions(-)
diff --git a/src/httpserver/http_fns.c b/src/httpserver/http_fns.c
index 3fce5f864..6801cefb1 100644
--- a/src/httpserver/http_fns.c
+++ b/src/httpserver/http_fns.c
@@ -19,6 +19,8 @@
#include "../driver/drv_ntp.h"
#include "../driver/drv_local.h"
+static char SUBMIT_AND_END_FORM[] = " ";
+
#ifdef WINDOWS
// nothing
#elif PLATFORM_BL602
@@ -132,6 +134,13 @@ void postFormAction(http_request_t* request, char* action, char* value) {
hprintf255(request, "
", action, value);
}
+void poststr_h2(http_request_t* request, const char *content) {
+ hprintf255(request, "%s ", content);
+}
+void poststr_h4(http_request_t* request, const char *content) {
+ hprintf255(request, "%s ", content);
+}
+
/// @brief Generate a pair of label and field elements for Name type entry. The field is limited to entry of a-zA-Z0-9_- characters.
/// @param request
/// @param label
@@ -944,7 +953,7 @@ int http_fn_index(http_request_t* request) {
int http_fn_about(http_request_t* request) {
http_setup(request, httpMimeTypeHTML);
http_html_start(request, "About");
- poststr(request, "Open source firmware for BK7231N, BK7231T, XR809 and BL602 by OpenSHWProjects ");
+ poststr_h2(request, "Open source firmware for BK7231N, BK7231T, XR809 and BL602 by OpenSHWProjects");
poststr(request, htmlFooterReturnToMenu);
http_html_end(request);
poststr(request, NULL);
@@ -954,11 +963,10 @@ int http_fn_about(http_request_t* request) {
int http_fn_cfg_mqtt(http_request_t* request) {
http_setup(request, httpMimeTypeHTML);
http_html_start(request, "MQTT");
- poststr(request, "Use this to connect to your MQTT ");
-
- hprintf255(request, "Currently, your device command topic is cmnd/%s/[Command] ", CFG_GetMQTTClientId());
- hprintf255(request, "Currently, your device publish data topic is %s/[Channel]/get ", CFG_GetMQTTClientId());
- hprintf255(request, "Currently, your device receive data topic is %s/[Channel]/set ", CFG_GetMQTTClientId());
+ poststr_h2(request, "Use this to connect to your MQTT");
+ hprintf255(request, "Command topic: cmnd/%s/[Command] ", CFG_GetMQTTClientId());
+ hprintf255(request, "Publish data topic: %s/[Channel]/get ", CFG_GetMQTTClientId());
+ hprintf255(request, "Receive data topic: %s/[Channel]/set ", CFG_GetMQTTClientId());
add_label_text_field(request, "Host", "host", CFG_GetMQTTHost(), " ");
+ poststr(request, " ");
poststr(request, htmlFooterReturnToCfgLink);
http_html_end(request);
poststr(request, NULL);
@@ -1006,9 +1012,7 @@ int http_fn_cfg_mqtt_set(http_request_t* request) {
g_mqtt_bBaseTopicDirty = 1;
- poststr(request, " ");
- poststr(request, "Return to MQTT settings ");
- poststr(request, " ");
+ poststr(request, "Return to MQTT settings ");
poststr(request, htmlFooterReturnToCfgLink);
http_html_end(request);
poststr(request, NULL);
@@ -1018,11 +1022,8 @@ int http_fn_cfg_mqtt_set(http_request_t* request) {
int http_fn_cfg_webapp(http_request_t* request) {
http_setup(request, httpMimeTypeHTML);
http_html_start(request, "Set Webapp");
- poststr(request, " Use this to set the URL of the Webapp ");
- add_label_text_field(request, "Url", "url", CFG_GetWebappRoot(), " ");
+ add_label_text_field(request, "URL of the Webapp", "url", CFG_GetWebappRoot(), " ");
- poststr(request, " Use this to enable pinger ");
+ \
+ \
+");
+ poststr_h2(request, "Use this to enable pinger");
add_label_text_field(request, "Host", "host", CFG_GetPingHost(), " ");
+ \
+");
poststr(request, htmlFooterReturnToCfgLink);
http_html_end(request);
poststr(request, NULL);
@@ -1174,20 +1175,20 @@ int http_fn_cfg_wifi(http_request_t* request) {
#endif
}
poststr(request, " ");
- poststr(request, " Use this to disconnect from your WiFi ");
+ \
+ \
+");
+ poststr_h4(request, "Use this to disconnect from your WiFi");
poststr(request, " ");
- poststr(request, " Use this to connect to your WiFi ");
+ \
+ \
+");
+ poststr_h2(request, "Use this to connect to your WiFi");
add_label_text_field(request, "SSID", "ssid", CFG_GetWiFiSSID(), " ");
+ \
+");
poststr(request, htmlFooterReturnToCfgLink);
http_html_end(request);
poststr(request, NULL);
@@ -1201,7 +1202,7 @@ int http_fn_cfg_name(http_request_t* request) {
http_setup(request, httpMimeTypeHTML);
http_html_start(request, "Set name");
- poststr(request, " Change device names for display. ");
+ poststr_h2(request, "Change device names for display");
if (http_getArg(request->url, "shortName", tmpA, sizeof(tmpA))) {
CFG_SetShortDeviceName(tmpA);
}
@@ -1210,7 +1211,7 @@ int http_fn_cfg_name(http_request_t* request) {
}
CFG_Save_IfThereArePendingChanges();
- poststr(request, " Use this to change device names ");
+ poststr_h2(request, "Use this to change device names");
add_label_name_field(request, "ShortName", "shortName", CFG_GetShortDeviceName(), " ");
+ \
+");
- poststr(request, " ");
- poststr(request, "Return to config settings ");
- poststr(request, " ");
+ poststr(request, "Return to config settings ");
poststr(request, htmlFooterReturnToCfgLink);
http_html_end(request);
poststr(request, NULL);
@@ -1314,23 +1311,23 @@ int http_fn_cfg_mac(http_request_t* request) {
}
//sscanf(tmpA,"%02X%02X%02X%02X%02X%02X",&mac[0],&mac[1],&mac[2],&mac[3],&mac[4],&mac[5]);
if (WiFI_SetMacAddress((char*)mac)) {
- poststr(request, " New MAC set! ");
+ poststr_h4(request, "New MAC set!");
}
else {
- poststr(request, " MAC change error? ");
+ poststr_h4(request, "MAC change error?");
}
CFG_Save_IfThereArePendingChanges();
}
WiFI_GetMacAddress((char*)mac);
- poststr(request, " Here you can change MAC address. ");
+ poststr_h2(request, "Here you can change MAC address.");
sprintf(macStr, "%02X%02X%02X%02X%02X%02X", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
add_label_text_field(request, "MAC", "mac", macStr, " ");
+ \
+");
poststr(request, htmlFooterReturnToCfgLink);
http_html_end(request);
poststr(request, NULL);
@@ -1351,7 +1348,7 @@ int http_fn_flash_read_tool(http_request_t* request) {
http_setup(request, httpMimeTypeHTML);
http_html_start(request, "Flash read");
- poststr(request, "Flash Read Tool ");
+ poststr_h4(request, "Flash Read Tool");
if (http_getArg(request->url, "hex", tmpA, sizeof(tmpA))) {
hex = atoi(tmpA);
}
@@ -1420,9 +1417,7 @@ int http_fn_flash_read_tool(http_request_t* request) {
add_label_numeric_field(request, "Offset", "offset", ofs, "");
add_label_numeric_field(request, "Length", "len", len, " ");
- poststr(request, " \
- \
- ");
+ poststr(request, SUBMIT_AND_END_FORM);
poststr(request, htmlFooterReturnToCfgLink);
http_html_end(request);
@@ -1456,7 +1451,7 @@ int http_fn_cmd_tool(http_request_t* request) {
http_setup(request, httpMimeTypeHTML);
http_html_start(request, "Command tool");
- poststr(request, "Command Tool ");
+ poststr_h4(request, "Command Tool");
poststr(request, "This is a basic command line. ");
poststr(request, "Please consider using 'Web Application' console with more options and real time log view. ");
poststr(request, "Remember that some commands are added after a restart when a driver is activated... ");
@@ -1487,9 +1482,7 @@ int http_fn_cmd_tool(http_request_t* request) {
poststr(request, " ");
}
add_label_text_field(request, "Command", "cmd", tmpA, " ");
+ poststr(request, SUBMIT_AND_END_FORM);
poststr(request, htmlFooterReturnToCfgLink);
http_html_end(request);
@@ -1501,7 +1494,7 @@ int http_fn_startup_command(http_request_t* request) {
char tmpA[512];
http_setup(request, httpMimeTypeHTML);
http_html_start(request, "Set startup command");
- poststr(request, "Set/Change/Clear startup command line ");
+ poststr_h4(request, "Set/Change/Clear startup command line");
poststr(request, "Startup command is a shorter, smaller alternative to LittleFS autoexec.bat. "
"The startup commands are ran at device startup. "
"You can use them to init peripherals and drivers, like BL0942 energy sensor. "
@@ -1519,9 +1512,7 @@ int http_fn_startup_command(http_request_t* request) {
}
add_label_text_field(request, "Startup command", "data", CFG_GetShortStartupCommand(), "
");
+ poststr(request, SUBMIT_AND_END_FORM);
poststr(request, htmlFooterReturnToCfgLink);
http_html_end(request);
@@ -1533,7 +1524,7 @@ int http_fn_uart_tool(http_request_t* request) {
int resultLen = 0;
http_setup(request, httpMimeTypeHTML);
http_html_start(request, "UART tool");
- poststr(request, "UART Tool ");
+ poststr_h4(request, "UART Tool");
if (http_getArg(request->url, "data", tmpA, sizeof(tmpA))) {
#ifdef ENABLE_DRIVER_TUYAMCU
@@ -1564,9 +1555,7 @@ int http_fn_uart_tool(http_request_t* request) {
}
add_label_text_field(request, "Data", "data", tmpA, " ");
+ poststr(request, SUBMIT_AND_END_FORM);
poststr(request, htmlFooterReturnToCfgLink);
http_html_end(request);
@@ -1580,7 +1569,7 @@ int http_fn_uart_tool(http_request_t* request) {
int http_fn_cfg_quick(http_request_t* request) {
http_setup(request, httpMimeTypeHTML);
http_html_start(request, "Quick Config");
- poststr(request, "Quick Config ");
+ poststr_h4(request, "Quick Config");
/*
@@ -1598,8 +1587,7 @@ int http_fn_cfg_quick(http_request_t* request) {
*/
- poststr(request, "This is obsolete now - please config through Web App ");
- poststr(request, "");
+ poststr(request, "This is now obsolete. Please config through Web App and refer to the Devices List. ");
/*if (http_getArg(request->url, "dev", tmpA, sizeof(tmpA))) {
@@ -1794,6 +1782,15 @@ void http_generate_cw_cfg(http_request_t* request, const char* clientId) {
hprintf255(request, " color_temp_state_topic: \"%s/led_temperature/get\"\n", clientId);
http_generate_singleColor_cfg(request, clientId);
}
+
+void hprintf_qos_payload(http_request_t* request, const char *clientId){
+ poststr(request, " qos: 1\n");
+ poststr(request, " payload_on: 1\n");
+ poststr(request, " payload_off: 0\n");
+ poststr(request, " retain: true\n");
+ hprintf255(request, " availability:\n");
+ hprintf255(request, " - topic: \"%s/connected\"\n", clientId);
+}
int http_fn_ha_cfg(http_request_t* request) {
int relayCount;
int pwmCount;
@@ -1812,9 +1809,9 @@ int http_fn_ha_cfg(http_request_t* request) {
http_setup(request, httpMimeTypeHTML);
http_html_start(request, "Home Assistant Setup");
- poststr(request, "Home Assistant Cfg ");
+ poststr_h4(request, "Home Assistant Cfg");
hprintf255(request, "Note that your short device name is: %s ", shortDeviceName);
- poststr(request, "Paste this to configuration yaml ");
+ poststr_h4(request, "Paste this to configuration yaml");
poststr(request, "Make sure that you have \"switch:\" keyword only once! Home Assistant doesn't like dup keywords. ");
poststr(request, "You can also use \"switch MyDeviceName:\" to avoid keyword duplication! ");
@@ -1839,12 +1836,7 @@ int http_fn_ha_cfg(http_request_t* request) {
hprintf255(request, " name: \"%s %i\"\n", shortDeviceName, i);
hprintf255(request, " state_topic: \"%s/%i/get\"\n", clientId, i);
hprintf255(request, " command_topic: \"%s/%i/set\"\n", clientId, i);
- poststr(request, " qos: 1\n");
- poststr(request, " payload_on: 1\n");
- poststr(request, " payload_off: 0\n");
- poststr(request, " retain: true\n");
- hprintf255(request, " availability:\n");
- hprintf255(request, " - topic: \"%s/connected\"\n", clientId);
+ hprintf_qos_payload(request, clientId);
}
}
}
@@ -1863,12 +1855,7 @@ int http_fn_ha_cfg(http_request_t* request) {
hass_print_unique_id(request, " - unique_id: \"%s\"\n", BINARY_SENSOR, i);
hprintf255(request, " name: \"%s %i\"\n", shortDeviceName, i);
hprintf255(request, " state_topic: \"%s/%i/get\"\n", clientId, i);
- poststr(request, " qos: 1\n");
- poststr(request, " payload_on: 1\n");
- poststr(request, " payload_off: 0\n");
- poststr(request, " retain: true\n");
- hprintf255(request, " availability:\n");
- hprintf255(request, " - topic: \"%s/connected\"\n", clientId);
+ hprintf_qos_payload(request, clientId);
}
}
}
@@ -1886,10 +1873,10 @@ int http_fn_ha_cfg(http_request_t* request) {
hass_print_unique_id(request, " - unique_id: \"%s\"\n", LIGHT_RGBCW, i);
hprintf255(request, " name: \"%s %i\"\n", shortDeviceName, i);
http_generate_rgb_cfg(request, clientId);
- hprintf255(request, " #brightness_value_template: \"{{ value }}\"\n");
+ //hprintf255(request, " #brightness_value_template: \"{{ value }}\"\n");
hprintf255(request, " color_temp_command_topic: \"cmnd/%s/led_temperature\"\n", clientId);
hprintf255(request, " color_temp_state_topic: \"%s/led_temperature/get\"\n", clientId);
- hprintf255(request, " #color_temp_value_template: \"{{ value }}\"\n");
+ //hprintf255(request, " #color_temp_value_template: \"{{ value }}\"\n");
}
else
if (pwmCount == 3) {
@@ -2317,7 +2304,7 @@ int http_fn_cfg_generic(http_request_t* request) {
poststr(request, "");
}
poststr(request, " ");
- poststr(request, " ");
+ poststr(request, SUBMIT_AND_END_FORM);
add_label_numeric_field(request, "Uptime seconds required to mark boot as ok", "boot_ok_delay",
CFG_GetBootOkSeconds(), "");
+ poststr(request, "");
+ poststr(request, SUBMIT_AND_END_FORM);
}
poststr(request, htmlFooterReturnToCfgLink);
@@ -2518,8 +2506,8 @@ int http_fn_ota(http_request_t* request) {
poststr(request, "Simple OTA system (you should rather use the OTA from App panel where you can drag and drop file easily without setting up server). Use RBL file for OTA. In the OTA below, you should paste link to RBL file (you need HTTP server).
");
add_label_text_field(request, "URL for new bin file", "host", "", " ");
+ \
+");
poststr(request, htmlFooterReturnToMenu);
http_html_end(request);
poststr(request, NULL);