gpio configuration via web interface

This commit is contained in:
technyon
2023-04-07 13:10:44 +02:00
parent 29c664e3ba
commit e3237a648c
6 changed files with 264 additions and 55 deletions

View File

@@ -7,11 +7,12 @@
#include "RestartReason.h"
#include <esp_task_wdt.h>
WebCfgServer::WebCfgServer(NukiWrapper* nuki, NukiOpenerWrapper* nukiOpener, Network* network, EthServer* ethServer, Preferences* preferences, bool allowRestartToPortal)
WebCfgServer::WebCfgServer(NukiWrapper* nuki, NukiOpenerWrapper* nukiOpener, Network* network, Gpio* gpio, EthServer* ethServer, Preferences* preferences, bool allowRestartToPortal)
: _server(ethServer),
_nuki(nuki),
_nukiOpener(nukiOpener),
_network(network),
_gpio(gpio),
_preferences(preferences),
_allowRestartToPortal(allowRestartToPortal)
{
@@ -93,6 +94,14 @@ void WebCfgServer::initialize()
buildNukiConfigHtml(response);
_server.send(200, "text/html", response);
});
_server.on("/gpiocfg", [&]() {
if (_hasCredentials && !_server.authenticate(_credUser, _credPassword)) {
return _server.requestAuthentication();
}
String response = "";
buildGpioConfigHtml(response);
_server.send(200, "text/html", response);
});
_server.on("/wifi", [&]() {
if (_hasCredentials && !_server.authenticate(_credUser, _credPassword)) {
return _server.requestAuthentication();
@@ -128,7 +137,8 @@ void WebCfgServer::initialize()
_network->reconfigureDevice();
}
});
_server.on("/savecfg", [&]() {
_server.on("/savecfg", [&]()
{
if (_hasCredentials && !_server.authenticate(_credUser, _credPassword)) {
return _server.requestAuthentication();
}
@@ -152,6 +162,22 @@ void WebCfgServer::initialize()
waitAndProcess(false, 1000);
}
});
_server.on("/savegpiocfg", [&]()
{
if (_hasCredentials && !_server.authenticate(_credUser, _credPassword)) {
return _server.requestAuthentication();
}
processGpioArgs();
String response = "";
buildConfirmHtml(response, "");
_server.send(200, "text/html", response);
Log->println(F("Restarting"));
waitAndProcess(true, 1000);
restartEsp(RestartReason::GpioConfigurationUpdated);
});
_server.on("/ota", [&]() {
if (_hasCredentials && !_server.authenticate(_credUser, _credPassword)) {
return _server.requestAuthentication();
@@ -523,6 +549,32 @@ bool WebCfgServer::processArgs(String& message)
return configChanged;
}
void WebCfgServer::processGpioArgs()
{
int count = _server.args();
std::vector<PinEntry> pinConfiguration;
for(int index = 0; index < count; index++)
{
String key = _server.argName(index);
String value = _server.arg(index);
PinRole role = (PinRole)value.toInt();
if(role != PinRole::Disabled)
{
PinEntry entry;
entry.pin = key.toInt();
entry.role = role;
pinConfiguration.push_back(entry);
}
}
_gpio->savePinConfiguration(pinConfiguration);
}
void WebCfgServer::update()
{
if(_otaStartTs > 0 && (millis() - _otaStartTs) > 120000)
@@ -575,6 +627,9 @@ void WebCfgServer::buildHtml(String& response)
response.concat("<BR><BR><h3>Credentials</h3>");
buildNavigationButton(response, "Edit", "/cred", _pinsConfigured ? "" : "<font color=\"#f07000\"><em>(!) Please configure PIN</em></font>");
response.concat("<BR><BR><h3>GPIO Configuration</h3>");
buildNavigationButton(response, "Edit", "/gpiocfg");
response.concat("<BR><BR><h3>Firmware update</h3>");
buildNavigationButton(response, "Open", "/ota");
@@ -780,6 +835,29 @@ void WebCfgServer::buildNukiConfigHtml(String &response)
response.concat("</BODY></HTML>");
}
void WebCfgServer::buildGpioConfigHtml(String &response)
{
buildHtmlHeader(response);
response.concat("<FORM ACTION=savegpiocfg method='POST'>");
response.concat("<h3>GPIO Configuration</h3>");
response.concat("<table>");
const auto& availablePins = _gpio->availablePins();
for(const auto& pin : availablePins)
{
String pinStr = String(pin);
String pinDesc = "Gpio " + pinStr;
printDropDown(response, pinStr.c_str(), pinDesc.c_str(), getPreselectionForGpio(pin), getGpioOptions());
}
response.concat("</table>");
response.concat("<br><INPUT TYPE=SUBMIT NAME=\"submit\" VALUE=\"Save\">");
response.concat("</FORM>");
response.concat("</BODY></HTML>");
}
void WebCfgServer::buildConfirmHtml(String &response, const String &message, uint32_t redirectDelay)
{
String delay(redirectDelay);
@@ -1226,3 +1304,32 @@ const std::vector<std::pair<String, String>> WebCfgServer::getNetworkGpioOptions
return options;
}
const std::vector<std::pair<String, String>> WebCfgServer::getGpioOptions() const
{
std::vector<std::pair<String, String>> options;
const auto& roles = _gpio->getAllRoles();
for(const auto& role : roles)
{
options.push_back( std::make_pair(String((int)role), _gpio->getRoleDescription(role)));
}
return options;
}
String WebCfgServer::getPreselectionForGpio(const uint8_t &pin)
{
const std::vector<PinEntry>& pinConfiguration = _gpio->pinConfiguration();
for(const auto& entry : pinConfiguration)
{
if(pin == entry.pin)
{
return String((int8_t)entry.role);
}
}
return String((int8_t)PinRole::Disabled);
}