CanGrow/include/Webserver/Webserver_Common.h

190 lines
5.8 KiB
C

/*
*
* include/Webserver/Common.h - header file with common webserver functions
* HTML header or footer to a String()
*
*
*
*/
#include "Webserver_Common_HTML.h"
/*
* global char constants for various HTML tags and stuff
*
*/
/* return type */
const char TEXT_HTML[] PROGMEM = "text/html";
/*
* TestHeaderFooter - checks if the given var from the webserver processor
* is actual a template variable from header or footer.
*/
bool TestHeaderFooter(const String& var) {
const static char LogLoc[] PROGMEM = "[Webserver:Common:TestHeaderFooter]";
#ifdef DEBUG3
Log.verbose(F("%s var: %s" CR), LogLoc, var);
#endif
if(
(var == "HEADER") ||
(var == "FOOTER") ||
(var == "CGVER") ||
(var == "CGBUILD") ||
(var == "GROWNAME") ||
(var == "CANGROW_CSS") ||
(var == "TIME") ||
(var == "NEED_RESTART") ||
(var == "ACTIVE_NAV_GROW") ||
(var == "ACTIVE_NAV_SYSTEM") ||
(var == "ACTIVE_NAV_WIFI") ||
(var == "ACTIVE_NAV_HELP") ||
(var == "PLACEHOLDER")) {
return true;
} else {
return false;
}
}
/*
* AddHeaderFooter - processor for header and footer template variables
*
* String& var:
* the string we receive from the processor is the actual
* variable name we replace here.
* byte activeNav:
* contains the number representing which page is active
* 1 - grow settings
* 2 - system settings
* 3 - wifi settings
* 4 - help page
*/
String AddHeaderFooter(const String& var, byte activeNav = 0) {
String activeNav_ClassName = F("activeNav");
if(var == "HEADER") {
return String(Header_HTML);
} else if(var == "FOOTER") {
return String(Footer_HTML);
} else if(var == "CGVER") {
return String(CANGROW_VER);
} else if(var == "CGBUILD") {
return String(CANGROW_BUILD);
} else if(var == "GROWNAME") {
return String(config.grow.name);
} else if(var == "CANGROW_CSS") {
return String(File_cangrow_CSS);
} else if(var == "TIME") {
return Str_TimeNow();
} else if((var == "ACTIVE_NAV_GROW") && (activeNav == 1)) {
return activeNav_ClassName;
} else if((var == "ACTIVE_NAV_SYSTEM") && (activeNav == 2)) {
return activeNav_ClassName;
} else if((var == "ACTIVE_NAV_WIFI") && (activeNav == 3)) {
return activeNav_ClassName;
} else if((var == "ACTIVE_NAV_HELP") && (activeNav == 4)) {
return activeNav_ClassName;
} else if(var == "NEED_RESTART") {
if(needRestart == true) {
return String(Common_HTML_NEED_RESTART);
} else {
return String();
}
} else {
return String();
}
}
/*
* Html_SelectOpt_GPIOindex
*
* returns <option> list as string with available gpios
*/
String Html_SelectOpt_GPIOindex(byte selectId = 255, bool input = false) {
String gpioIndex_html;
// iterate through through all available GPIOs in index
for(byte i = 1; i <= GPIOindex_length; i++) {
bool gpioUsed = Check_GPIOindex_Used(i);
gpioIndex_html += F("<option value='");
gpioIndex_html += i;
gpioIndex_html += F("'");
// set disabled option for gpio which are already in use or incompatible // or only inputs when configuring sensor ADC
// || ( (input == true) && ((GPIOindex[i].note != INPUT_ONLY) || (GPIOindex[i].note != INT_ADC)) )
/* when GPIO is already in use AND not selected OR
* input is false AND GPIO is Input only OR
* input is true AND GPIO is not INT_ADC AND not INPUT_only*/
if( ((gpioUsed == true) && (i != selectId)) || ((input == false) && (GPIOindex[i].note == INPUT_ONLY))
#ifdef ESP32
/* If we are on ESP32, we check our input GPIOs - we dont need this on ESP8266, as it only has 1 ADC */
|| ((input == true) && ((GPIOindex[i].note != INT_ADC) && GPIOindex[i].note != INPUT_ONLY))
#endif
) {
//|| ((input == true) && ((GPIOindex[i].note != INPUT_ONLY) || (GPIOindex[i].note != INT_ADC) ))
gpioIndex_html += F(" disabled");
}
if(i == selectId) {
gpioIndex_html += F(" selected");
}
gpioIndex_html += F(">GPIO ");
gpioIndex_html += GPIOindex[i].gpio;
//add gpio note if there is some
//if(GPIOindex[i].note > 0) {
gpioIndex_html += F(" ");
gpioIndex_html += FPSTR(GPIO_Index_note_descr[GPIOindex[i].note]);
// disable output incompatible gpio
if((GPIOindex[i].note == INPUT_ONLY) && (input == false)) {
gpioIndex_html += F(" (N/A)");
// add USED if gpio is already in use
} else if((gpioUsed == true) && (i != selectId)) {
gpioIndex_html += F(" (used)");
}
gpioIndex_html += F("</option>");
}
return gpioIndex_html;
}
String Html_SelectOpt_bool(byte selectVal = 255, String trueStr = "Yes", String falseStr = "No") {
String html;
html += F("<option value='1'");
html += ((selectVal > 0) && (selectVal < 255)) ? F(" selected") : F("");
html += F(">");
html += trueStr;
html += F("</option>");
html += F("<option value='0'");
html += (selectVal == 0) ? F(" selected") : F("");
html += F(">");
html += falseStr;
html += F("</option>");
return html;
}
String Html_SelectOpt_array(byte total, const char * descr[] , byte selectVal = 255) {
const static char LogLoc[] PROGMEM= "[Webserver:Common:Html_Select_Opt_array]";
String html;
// go through all available array entries, skip 0 because it means unconfigured
for(byte i = 1; i <= total; i++) {
//Log.notice(F("%s i: %d selectVal: %d descr: %S" CR), LogLoc, i, selectVal, descr[i]);
html += F("<option value='");
html += i;
html += F("'");
if(i == selectVal) {
html += F(" selected");
}
html += F(">");
// use FPSTR because our descr is stored in PROGMEM
html += FPSTR(descr[i]);
html += F("</option>");
}
return html;
}