diff --git a/src/httpserver/http_fns.c b/src/httpserver/http_fns.c index fe15ecdc8..17933bb01 100644 --- a/src/httpserver/http_fns.c +++ b/src/httpserver/http_fns.c @@ -123,6 +123,44 @@ void postFormAction(http_request_t *request, char *action, char *value){ hprintf128(request,"
", action, value); } +/// @brief Generate a pair of label and field elements. +/// @param request +/// @param label +/// @param fieldId This also gets used as the field name +/// @param value +/// @param preContent +void add_label_input(http_request_t *request, char *inputType, char *label, char *fieldId, const char *value, char *preContent){ + if (strlen(preContent) > 0){ + poststr(request, preContent); + } + + //These individual strings should be less than 256 .. yes hprintf128 uses 256 char buffer + hprintf128(request, "
", fieldId, label); + hprintf128(request, "", inputType, fieldId, fieldId, value); +} + +/// @brief Generates a pair of label and text field elements. +/// @param request +/// @param label Label for the field +/// @param fieldId Field id, this also gets used as the name +/// @param value String value +/// @param preContent Content before the label +void add_label_text_field(http_request_t *request, char *label, char *fieldId, const char *value, char *preContent){ + add_label_input(request, "text", label, fieldId, value, preContent); +} + +/// @brief Generate a pair of label and numeric field elements. +/// @param request +/// @param label Label for the field +/// @param fieldId Field id, this also gets used as the name +/// @param value Integer value +/// @param preContent Content before the label +void add_label_numeric_field(http_request_t *request, char *label, char *fieldId, int value, char *preContent){ + char strValue[32]; + sprintf(strValue, "%i", value); + add_label_input(request, "number", label, fieldId, strValue, preContent); +} + int http_fn_testmsg(http_request_t *request) { poststr(request,"This is just a test msg\n\n"); poststr(request, NULL); @@ -141,63 +179,63 @@ int http_fn_index(http_request_t *request) { // use ?state URL parameter to only request current state if(!http_getArg(request->url, "state", tmpA, sizeof(tmpA))) { - http_setup(request, httpMimeTypeHTML); + http_setup(request, httpMimeTypeHTML); http_html_start(request, NULL); poststr(request, "
"); - if(http_getArg(request->url,"tgl",tmpA,sizeof(tmpA))) { - j = atoi(tmpA); - if(j == SPECIAL_CHANNEL_LEDPOWER) { - hprintf128(request,"

Toggled LED power!

",j); - } else { - hprintf128(request,"

Toggled %i!

",j); - } - CHANNEL_Toggle(j); - } - if(http_getArg(request->url,"on",tmpA,sizeof(tmpA))) { - j = atoi(tmpA); - hprintf128(request,"

Enabled %i!

",j); - CHANNEL_Set(j,255,1); - } - if(http_getArg(request->url,"rgb",tmpA,sizeof(tmpA))) { - hprintf128(request,"

Set RGB to %s!

",tmpA); - LED_SetBaseColor(0,"led_basecolor",tmpA,0); - } - - if(http_getArg(request->url,"off",tmpA,sizeof(tmpA))) { - j = atoi(tmpA); - hprintf128(request,"

Disabled %i!

",j); - CHANNEL_Set(j,0,1); - } - if(http_getArg(request->url,"pwm",tmpA,sizeof(tmpA))) { - int newPWMValue = atoi(tmpA); - http_getArg(request->url,"pwmIndex",tmpA,sizeof(tmpA)); - j = atoi(tmpA); - if(j == SPECIAL_CHANNEL_TEMPERATURE) { - hprintf128(request,"

Changed Temperature to %i!

",newPWMValue); - } else { - hprintf128(request,"

Changed pwm %i to %i!

",j,newPWMValue); - } - CHANNEL_Set(j,newPWMValue,1); - } - if(http_getArg(request->url,"dim",tmpA,sizeof(tmpA))) { - int newDimmerValue = atoi(tmpA); - http_getArg(request->url,"dimIndex",tmpA,sizeof(tmpA)); - j = atoi(tmpA); - if(j == SPECIAL_CHANNEL_BRIGHTNESS) { - hprintf128(request,"

Changed LED brightness to %i!

",newDimmerValue); - } else { - hprintf128(request,"

Changed dimmer %i to %i!

",j,newDimmerValue); - } - CHANNEL_Set(j,newDimmerValue,1); - } - if(http_getArg(request->url,"set",tmpA,sizeof(tmpA))) { - int newSetValue = atoi(tmpA); - http_getArg(request->url,"setIndex",tmpA,sizeof(tmpA)); - j = atoi(tmpA); - hprintf128(request,"

Changed channel %i to %i!

",j,newSetValue); - CHANNEL_Set(j,newSetValue,1); - } + if(http_getArg(request->url,"tgl",tmpA,sizeof(tmpA))) { + j = atoi(tmpA); + if(j == SPECIAL_CHANNEL_LEDPOWER) { + hprintf128(request,"

Toggled LED power!

",j); + } else { + hprintf128(request,"

Toggled %i!

",j); + } + CHANNEL_Toggle(j); + } + if(http_getArg(request->url,"on",tmpA,sizeof(tmpA))) { + j = atoi(tmpA); + hprintf128(request,"

Enabled %i!

",j); + CHANNEL_Set(j,255,1); + } + if(http_getArg(request->url,"rgb",tmpA,sizeof(tmpA))) { + hprintf128(request,"

Set RGB to %s!

",tmpA); + LED_SetBaseColor(0,"led_basecolor",tmpA,0); + } + + if(http_getArg(request->url,"off",tmpA,sizeof(tmpA))) { + j = atoi(tmpA); + hprintf128(request,"

Disabled %i!

",j); + CHANNEL_Set(j,0,1); + } + if(http_getArg(request->url,"pwm",tmpA,sizeof(tmpA))) { + int newPWMValue = atoi(tmpA); + http_getArg(request->url,"pwmIndex",tmpA,sizeof(tmpA)); + j = atoi(tmpA); + if(j == SPECIAL_CHANNEL_TEMPERATURE) { + hprintf128(request,"

Changed Temperature to %i!

",newPWMValue); + } else { + hprintf128(request,"

Changed pwm %i to %i!

",j,newPWMValue); + } + CHANNEL_Set(j,newPWMValue,1); + } + if(http_getArg(request->url,"dim",tmpA,sizeof(tmpA))) { + int newDimmerValue = atoi(tmpA); + http_getArg(request->url,"dimIndex",tmpA,sizeof(tmpA)); + j = atoi(tmpA); + if(j == SPECIAL_CHANNEL_BRIGHTNESS) { + hprintf128(request,"

Changed LED brightness to %i!

",newDimmerValue); + } else { + hprintf128(request,"

Changed dimmer %i to %i!

",j,newDimmerValue); + } + CHANNEL_Set(j,newDimmerValue,1); + } + if(http_getArg(request->url,"set",tmpA,sizeof(tmpA))) { + int newSetValue = atoi(tmpA); + http_getArg(request->url,"setIndex",tmpA,sizeof(tmpA)); + j = atoi(tmpA); + hprintf128(request,"

Changed channel %i to %i!

",j,newSetValue); + CHANNEL_Set(j,newSetValue,1); + } if(http_getArg(request->url,"restart",tmpA,sizeof(tmpA))) { poststr(request,"
Module will restart soon
"); RESET_ScheduleModuleReset(3); @@ -376,7 +414,7 @@ int http_fn_index(http_request_t *request) { hprintf128(request,"
",i); hprintf128(request,"",inputName,i,pwmValue); hprintf128(request,"",inputName,i); - hprintf128(request,"
",i); + hprintf128(request,"",i); poststr(request, ""); } } @@ -554,43 +592,22 @@ int http_fn_about(http_request_t *request){ return 0; } - - - int http_fn_cfg_mqtt(http_request_t *request) { - int i; http_setup(request, httpMimeTypeHTML); http_html_start(request, "MQTT"); - poststr(request,"

Use this to connect to your MQTT

"); - poststr(request,"
\ -
\ -
\ -
\ -

\ -
\ -
\ -
\ -
\ -
\ -
\ + poststr(request,"

Use this to connect to your MQTT

"); + add_label_text_field(request, "Host", "host", CFG_GetMQTTHost(), ""); + add_label_numeric_field(request, "Port", "port", CFG_GetMQTTPort(), "
"); + add_label_text_field(request, "Client", "client", CFG_GetMQTTClientId(), "

"); + add_label_text_field(request, "User", "user", CFG_GetMQTTUserName(), "
"); + add_label_text_field(request, "Password", "password", CFG_GetMQTTPass(), "
"); + + poststr(request,"
\ \
"); poststr(request,htmlFooterReturnToCfgLink); http_html_end(request); - poststr(request, NULL); + poststr(request, NULL); return 0; } @@ -632,11 +649,8 @@ 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

"); - poststr(request,"
\ -
\ -
\ + add_label_text_field(request, "Url", "url", CFG_GetWebappRoot(), ""); + poststr(request,"
\ \
"); poststr(request,htmlFooterReturnToCfgLink); @@ -673,8 +687,6 @@ int http_fn_cfg_webapp_set(http_request_t *request) { int http_fn_cfg_ping(http_request_t *request) { char tmpA[128]; - const char *tmp; - int i; int bChanged; http_setup(request, httpMimeTypeHTML); @@ -717,25 +729,10 @@ int http_fn_cfg_ping(http_request_t *request) { \ "); poststr(request,"

Use this to enable pinger

"); - poststr(request,"
\ -
\ -
\ -
\ -
\ -
\ -

\ + add_label_text_field(request, "Host", "host", CFG_GetPingHost(), ""); + add_label_numeric_field(request, "Take action after this number of seconds with no reply", "disconnectTime", + CFG_GetPingDisconnectedSecondsToRestart(), "
"); + poststr(request,"

\ \
"); poststr(request,htmlFooterReturnToCfgLink); @@ -745,7 +742,6 @@ int http_fn_cfg_ping(http_request_t *request) { } int http_fn_cfg_wifi(http_request_t *request) { // for a test, show password as well... - const char *cur_ssid, *cur_pass; char tmpA[128]; http_setup(request, httpMimeTypeHTML); @@ -817,19 +813,9 @@ int http_fn_cfg_wifi(http_request_t *request) { \ "); poststr(request,"

Use this to connect to your WiFi

"); - poststr(request,"
\ -
\ -
\ -
\ -

\ + add_label_text_field(request, "SSID", "ssid", CFG_GetWiFiSSID(), ""); + add_label_text_field(request, "Password", "pass", CFG_GetWiFiPass(), "
"); + poststr(request,"

\ \
"); poststr(request,htmlFooterReturnToCfgLink); @@ -840,7 +826,6 @@ int http_fn_cfg_wifi(http_request_t *request) { int http_fn_cfg_name(http_request_t *request) { // for a test, show password as well... - const char *shortName, *name; char tmpA[128]; http_setup(request, httpMimeTypeHTML); @@ -854,19 +839,9 @@ int http_fn_cfg_name(http_request_t *request) { CFG_SetDeviceName(tmpA); } poststr(request,"

Use this to change device names

"); - poststr(request,"
\ -
\ -
\ -
\ -

"); + add_label_text_field(request, "ShortName", "shortName", CFG_GetShortDeviceName(), ""); + add_label_text_field(request, "Full Name", "name", CFG_GetDeviceName(), "
"); + poststr(request,"

"); poststr(request, "\ -
\ - "); #else - hprintf128(request,"%i",loglevel); + add_label_numeric_field(request, "Loglevel", "loglevel", loglevel, ""); #endif - poststr(request,"\">

\ + poststr(request,"

\ \
"); @@ -975,11 +949,11 @@ int http_fn_cfg_mac(http_request_t *request) { WiFI_GetMacAddress((char *)mac); poststr(request,"

Here you can change MAC address.

"); - poststr(request,"
\ -
\ -

\ + + char macStr[16]; + 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,"

\ \
"); poststr(request,htmlFooterReturnToCfgLink); @@ -1065,12 +1039,9 @@ int http_fn_flash_read_tool(http_request_t *request) { poststr(request," checked"); } poststr(request,">
"); - poststr(request,"
\ -
",ofs); - poststr(request,"
\ - ",len); + + add_label_numeric_field(request, "Offset", "offset", ofs, ""); + add_label_numeric_field(request, "Length", "len", len, "
"); poststr(request,"

\ \ "); @@ -1098,11 +1069,7 @@ int http_fn_cmd_tool(http_request_t *request) { } poststr(request,"
"); } - poststr(request,"
"); - - poststr(request,"
\ - ",tmpA); + add_label_text_field(request, "Command", "cmd", tmpA, ""); poststr(request,"

\ \
"); @@ -1115,7 +1082,6 @@ int http_fn_cmd_tool(http_request_t *request) { int http_fn_startup_command(http_request_t *request) { char tmpA[512]; - const char *cmd; http_setup(request, httpMimeTypeHTML); http_html_start(request, "Set startup command"); poststr(request,"

Set/Change/Clear startup command line

"); @@ -1133,16 +1099,8 @@ int http_fn_startup_command(http_request_t *request) { } else { } - cmd = CFG_GetShortStartupCommand(); - - poststr(request,"
"); - - poststr(request,"
\ -
"); - poststr(request,"
\ + add_label_text_field(request, "Startup command", "data", CFG_GetShortStartupCommand(), ""); + poststr(request,"

\ \
"); @@ -1184,11 +1142,7 @@ int http_fn_uart_tool(http_request_t *request) { strcpy(tmpA,"Hello UART world"); } - poststr(request,"
"); - - poststr(request,"
\ -
",tmpA); + add_label_text_field(request, "Data", "data", tmpA, ""); poststr(request,"
\ \
"); @@ -1245,12 +1199,12 @@ int http_fn_cfg_quick(http_request_t *request) { void get_Relay_PWM_Count(int *relayCount, int *pwmCount){ (*relayCount) = 0; (*pwmCount) = 0; - + for(int i = 0; i < PLATFORM_GPIO_MAX; i++) { int role = PIN_GetPinRoleForPinIndex(i); if(role == IOR_Relay || role == IOR_Relay_n || role == IOR_LED || role == IOR_LED_n) { (*relayCount)++; - } +} else if(role == IOR_PWM || role == IOR_PWM_n) { (*pwmCount)++; } @@ -1263,8 +1217,8 @@ void get_Relay_PWM_Count(int *relayCount, int *pwmCount){ int http_fn_ha_discovery(http_request_t *request) { int i; char topic[32]; - int relayCount=0; - int pwmCount=0; + int relayCount = 0; + int pwmCount = 0; http_setup(request, httpMimeTypeText); get_Relay_PWM_Count(&relayCount, &pwmCount); @@ -1338,7 +1292,7 @@ int http_fn_ha_cfg(http_request_t *request) { poststr(request,"