|
| 1 | +#include "config.h" |
| 2 | + |
| 3 | +const char Configuration::protocolValues[][STRING_LEN] = {"tcp", "udp"}; |
| 4 | +const char Configuration::baudrateValues[][STRING_LEN] = {"9600", "19200", "38400", "57600", "115200"}; |
| 5 | + |
| 6 | +SelectTParameter<STRING_LEN> Configuration::protocolParam = |
| 7 | + Builder<SelectTParameter<STRING_LEN>>("Protocoll"). |
| 8 | + label("Protocoll"). |
| 9 | + optionValues((const char*)protocolValues). |
| 10 | + optionNames((const char*)protocolValues). |
| 11 | + optionCount(sizeof(protocolValues) / STRING_LEN). |
| 12 | + nameLength(STRING_LEN). |
| 13 | + defaultValue("TCP"). |
| 14 | + build(); |
| 15 | + |
| 16 | +IntTParameter<uint16_t> Configuration::portParam = |
| 17 | + Builder<IntTParameter<uint16_t>>("Port"). |
| 18 | + label("Port"). |
| 19 | + defaultValue(5761). |
| 20 | + min(1). |
| 21 | + max(65535). |
| 22 | + step(1). |
| 23 | + placeholder("5761"). |
| 24 | + build(); |
| 25 | + |
| 26 | +SelectTParameter<STRING_LEN> Configuration::baudrateParam = |
| 27 | + Builder<SelectTParameter<STRING_LEN>>("Baudrate"). |
| 28 | + label("Baudarate"). |
| 29 | + optionValues((const char*)baudrateValues). |
| 30 | + optionNames((const char*)baudrateValues). |
| 31 | + optionCount(sizeof(baudrateValues) / STRING_LEN). |
| 32 | + nameLength(STRING_LEN). |
| 33 | + defaultValue("115200"). |
| 34 | + build(); |
| 35 | + |
| 36 | +ParameterGroup Configuration::protocollGroup = ParameterGroup("protocoll", "Protocoll"); |
| 37 | +ParameterGroup Configuration::serialGroup = ParameterGroup("serial", "Serial"); |
| 38 | + |
| 39 | +Configuration::Configuration() { |
| 40 | + dnsServer = new DNSServer(); |
| 41 | + webServer = new WebServer(80); |
| 42 | + iotWebConf = new IotWebConf(NAME, dnsServer, webServer, INITAL_WIFI_PASSWORD, CONFIG_VERSION); |
| 43 | + |
| 44 | + #if defined(ESP8266) |
| 45 | + httpUpdater = new ESP8266HTTPUpdateServer(); |
| 46 | + #elif defined(ESP32) |
| 47 | + httpUpdater = new HTTPUpdateServer(); |
| 48 | + #endif |
| 49 | +} |
| 50 | + |
| 51 | +void Configuration::setupWebConfig(void) { |
| 52 | + protocollGroup.addItem(&protocolParam); |
| 53 | + protocollGroup.addItem(&portParam); |
| 54 | + serialGroup.addItem(&baudrateParam); |
| 55 | + |
| 56 | + iotWebConf->setStatusPin(STATUS_PIN); |
| 57 | + iotWebConf->setConfigPin(CONFIG_PIN); |
| 58 | + iotWebConf->setupUpdateServer( |
| 59 | + [this](const char* updatePath) { httpUpdater->setup(webServer, updatePath); }, |
| 60 | + [this](const char* userName, char* password) { httpUpdater->updateCredentials(userName, password); } |
| 61 | + ); |
| 62 | + iotWebConf->addParameterGroup(&protocollGroup); |
| 63 | + iotWebConf->addParameterGroup(&serialGroup); |
| 64 | + iotWebConf->setConfigSavedCallback([this]{saved();}); |
| 65 | + iotWebConf->getApTimeoutParameter()->visible = true; |
| 66 | + iotWebConf->init(); |
| 67 | + delay(2000); |
| 68 | + |
| 69 | + webServer->on("/", [this]{handleRoot();}); |
| 70 | + webServer->on("/config", [this]{iotWebConf->handleConfig();}); |
| 71 | + webServer->onNotFound([this]{iotWebConf->handleNotFound();}); |
| 72 | +} |
| 73 | + |
| 74 | +void Configuration::doLoop() |
| 75 | +{ |
| 76 | + iotWebConf->doLoop(); |
| 77 | +} |
| 78 | + |
| 79 | +IPAddress Configuration::getIP() { |
| 80 | + IPAddress ip; |
| 81 | + if (iotWebConf->getState() == NetworkState::ApMode || iotWebConf->getState() == NetworkState::NotConfigured) { |
| 82 | + ip = WiFi.softAPIP(); |
| 83 | + } else { |
| 84 | + ip = WiFi.localIP(); |
| 85 | + } |
| 86 | + return ip; |
| 87 | +} |
| 88 | + |
| 89 | +void Configuration::handleRoot() |
| 90 | +{ |
| 91 | + if (iotWebConf->handleCaptivePortal()) { |
| 92 | + return; |
| 93 | + } |
| 94 | + String s = "<!DOCTYPE html><html lang=\"en\"><head><meta name=\"viewport\" content=\"width=device-width, initial-scale=1, user-scalable=no\"/>"; |
| 95 | + s += "<title>MSPWifiBridge</title></head><body>"; |
| 96 | + s += "<h2>Welcome to MSPWifiBridge</h2>"; |
| 97 | + s += "You can reach me under "; |
| 98 | + s += protocolParam.value(); |
| 99 | + s += "://"; |
| 100 | + s += getIP().toString(); |
| 101 | + s += ":"; |
| 102 | + s += portParam.value(); |
| 103 | + s += "<br>Go to <a href='config'>configure page</a> to change values."; |
| 104 | + s += "</body></html>\n"; |
| 105 | + |
| 106 | + webServer->send(200, "text/html", s); |
| 107 | +} |
| 108 | + |
| 109 | +void Configuration::saved() |
| 110 | +{ |
| 111 | + Serial.println("Configuration was updated.</br>"); |
| 112 | +} |
0 commit comments