From 1ab8d23320f99acc692bfaec8e16bc9184a8d9ac Mon Sep 17 00:00:00 2001 From: Robert Stein Date: Mon, 14 Dec 2020 06:39:33 +0100 Subject: [PATCH] Enable MDNS and display snyc --- src/Display/OledDisplay.cpp | 4 ++ src/Display/OledDisplay.h | 1 + src/Includes.h | 4 +- src/Network/WebServer.cpp | 123 ++++++++++++++++++------------------ src/Network/WebServer.h | 2 + src/main.cpp | 69 +++++++++----------- 6 files changed, 102 insertions(+), 101 deletions(-) diff --git a/src/Display/OledDisplay.cpp b/src/Display/OledDisplay.cpp index d677d58..111cf36 100644 --- a/src/Display/OledDisplay.cpp +++ b/src/Display/OledDisplay.cpp @@ -42,6 +42,10 @@ void OledDisplay::postSetup() { } } +void OledDisplay::handleUpdate() { + this->ui->update(); +} + void OledDisplay::showBootScreen() { this->oledDisplay->setTextAlignment(TEXT_ALIGN_CENTER); this->oledDisplay->setContrast(255); // default is 255 diff --git a/src/Display/OledDisplay.h b/src/Display/OledDisplay.h index e7c0315..fad4f92 100644 --- a/src/Display/OledDisplay.h +++ b/src/Display/OledDisplay.h @@ -22,6 +22,7 @@ public: OledDisplay(OLEDDisplay *oledDisplay, GlobalDataController *globalDataController, DebugController *debugController); void preSetup(); void postSetup(); + void handleUpdate(); void showBootScreen(); void showApAccessScreen(String apSsid, String apIp); void showWebserverSplashScreen(bool isEnabled); diff --git a/src/Includes.h b/src/Includes.h index e29b764..608ab4a 100644 --- a/src/Includes.h +++ b/src/Includes.h @@ -30,9 +30,7 @@ DebugController debugController; TimeClient timeClient(TIME_UTCOFFSET, &debugController); OpenWeatherMapClient weatherClient(WEATHER_APIKEY, WEATHER_CITYID, 1, WEATHER_METRIC, WEATHER_LANGUAGE, &debugController); GlobalDataController globalDataController(&timeClient, &weatherClient, &debugController); -#if WEBSERVER_ENABLED - WebServer webServer(&globalDataController, &debugController); -#endif +WebServer webServer(&globalDataController, &debugController); // Construct the correct printer client #if (PRINTERCLIENT == REPETIER_CLIENT) diff --git a/src/Network/WebServer.cpp b/src/Network/WebServer.cpp index 78f5fc4..0ddbf5f 100644 --- a/src/Network/WebServer.cpp +++ b/src/Network/WebServer.cpp @@ -319,6 +319,7 @@ void WebServer::handleUpdateConfig() { this->globalDataController->setUseLedFlash(this->server->hasArg("useFlash")); this->globalDataController->writeSettings(); + this->findMDNS(); this->globalDataController->getPrinterClient()->getPrinterJobResults(); this->globalDataController->getPrinterClient()->getPrinterPsuState(); @@ -332,35 +333,37 @@ void WebServer::handleUpdateConfig() { this->globalDataController->getTimeClient()->resetLastEpoch(); this->redirectHome(); } -// findMDNS(); - -// void findMDNS() { -// if (PrinterServer == "" || ENABLE_OTA == false) { -// return; // nothing to do here -// } -// // We now query our network for 'web servers' service -// // over tcp, and get the number of available devices -// int n = MDNS.queryService("http", "tcp"); -// if (n == 0) { -// Serial.println("no services found - make sure Printer server is turned on"); -// return; -// } -// Serial.println("*** Looking for " + PrinterHostName + " over mDNS"); -// for (int i = 0; i < n; ++i) { -// // Going through every available service, -// // we're searching for the one whose hostname -// // matches what we want, and then get its IP -// Serial.println("Found: " + MDNS.hostname(i)); -// if (MDNS.hostname(i) == PrinterHostName) { -// IPAddress serverIp = MDNS.IP(i); -// PrinterServer = serverIp.toString(); -// PrinterPort = MDNS.port(i); // save the port -// Serial.println("*** Found Printer Server " + PrinterHostName + " http://" + PrinterServer + ":" + PrinterPort); -// writeSettings(); // update the settings -// } -// } -// } +void WebServer::findMDNS() { + if (this->globalDataController->getPrinterHostName() == "" || ENABLE_OTA == false) { + return; // nothing to do here + } + // We now query our network for 'web servers' service + // over tcp, and get the number of available devices + int n = MDNS.queryService("http", "tcp"); + if (n == 0) { + this->debugController->printLn("no services found - make sure Printer server is turned on"); + return; + } + this->debugController->printLn("*** Looking for " + this->globalDataController->getPrinterHostName() + " over mDNS"); + for (int i = 0; i < n; ++i) { + // Going through every available service, + // we're searching for the one whose hostname + // matches what we want, and then get its IP + Serial.println("Found: " + MDNS.hostname(i)); + if (MDNS.hostname(i) == this->globalDataController->getPrinterHostName()) { + IPAddress serverIp = MDNS.IP(i); + this->globalDataController->setPrinterServer(serverIp.toString()); + this->globalDataController->setPrinterPort(MDNS.port(i)); + this->debugController->printLn( + "*** Found Printer Server " + this->globalDataController->getPrinterHostName() + + " http://" + this->globalDataController->getPrinterServer() + + ":" + this->globalDataController->getPrinterPort() + ); + this->globalDataController->writeSettings(); // update the settings + } + } +} void WebServer::handleUpdateWeather() { if (!this->authentication()) { @@ -396,32 +399,7 @@ void WebServer::handleConfigure() { html = this->getHeader(); this->server->sendContent(html); - CHANGE_FORM = "

Station Config:

" - "

" - "

"; - if ((printerClient->getPrinterType() == "OctoPrint") || (printerClient->getPrinterType() == "Klipper")) { - CHANGE_FORM += "

"; - } - CHANGE_FORM += "

" - "

" - "

" - "

"; - if (printerClient->getPrinterType() == "Repetier") { - CHANGE_FORM += "" - "

" - ""; - } - else if (printerClient->getPrinterType() == "Klipper") { - CHANGE_FORM += "" - "

" - ""; - } - else { - CHANGE_FORM += "

"; - } - CHANGE_FORM += "

" - "

"; - + // send javascript functions if (printerClient->getPrinterType() == "Repetier") { html = ""; this->server->sendContent(html); } - - String form = CHANGE_FORM; - + + + String form = "

Station Config:

" + "

" + "

"; + if ((printerClient->getPrinterType() == "OctoPrint") || (printerClient->getPrinterType() == "Klipper")) { + form += "

"; + } + form += "

" + "

" + "

" + "

"; + if (printerClient->getPrinterType() == "Repetier") { + form += "" + "

" + ""; + } + else if (printerClient->getPrinterType() == "Klipper") { + form += "" + "

" + ""; + } + else { + form += "

"; + } + form += "

" + "

"; + form.replace("%OCTOKEY%", this->globalDataController->getPrinterApiKey()); form.replace("%OCTOHOST%", this->globalDataController->getPrinterHostName()); form.replace("%OCTOADDRESS%", this->globalDataController->getPrinterServer()); form.replace("%OCTOPORT%", String(this->globalDataController->getPrinterPort())); form.replace("%OCTOUSER%", this->globalDataController->getPrinterAuthUser()); form.replace("%OCTOPASS%", this->globalDataController->getPrinterAuthPass()); - this->server->sendContent(form); form = FPSTR(CLOCK_FORM); diff --git a/src/Network/WebServer.h b/src/Network/WebServer.h index 98cddc3..4a35e88 100644 --- a/src/Network/WebServer.h +++ b/src/Network/WebServer.h @@ -4,6 +4,7 @@ #include #include #include +#include #include "../Global/GlobalDataController.h" class WebServer { @@ -16,6 +17,7 @@ private: public: WebServer(GlobalDataController *globalDataController, DebugController *debugController); void setup(); + void findMDNS(); void handleClient(); boolean authentication(); diff --git a/src/main.cpp b/src/main.cpp index 26086ae..a9229ea 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,13 +1,10 @@ #include "Includes.h" #include - - - +String lastMinute = "xx"; +String lastSecond = "xx"; void configModeCallback(WiFiManager *myWiFiManager); - - void setup() { LittleFS.begin(); debugController.setup(); @@ -73,6 +70,7 @@ void setup() { #endif globalDataController.flashLED(5, 100); + webServer.findMDNS(); debugController.printLn("*** Leaving setup()"); } @@ -81,20 +79,29 @@ void loop() { // Handle update of time if(timeClient.handleSync(globalDataController.getClockResyncMinutes()) && globalDataController.getWeatherShow()) { // We sync time? Ok, sync weather also! + debugController.printLn("Updating weather..."); weatherClient.updateWeather(); } - - - // put your main code here, to run repeatedly: - - - - - - - + if (lastMinute != timeClient.getMinutes() && !printerClient.isPrinting()) { + // Check status every 60 seconds + globalDataController.ledOnOff(true); + lastMinute = timeClient.getMinutes(); // reset the check value + printerClient.getPrinterJobResults(); + printerClient.getPrinterPsuState(); + globalDataController.ledOnOff(false); + } else if (printerClient.isPrinting()) { + if (lastSecond != timeClient.getSeconds() && timeClient.getSeconds().endsWith("0")) { + lastSecond = timeClient.getSeconds(); + // every 10 seconds while printing get an update + globalDataController.ledOnOff(true); + printerClient.getPrinterJobResults(); + printerClient.getPrinterPsuState(); + globalDataController.ledOnOff(false); + } + } + displayClient.handleUpdate(); if (WEBSERVER_ENABLED) { webServer.handleClient(); @@ -104,27 +111,13 @@ void loop() { } } - - - - - - - - - - - - - - void configModeCallback(WiFiManager *myWiFiManager) { - debugController.printLn("Entered config mode"); - debugController.printLn(WiFi.softAPIP().toString()); - displayClient.showApAccessScreen(myWiFiManager->getConfigPortalSSID(), WiFi.softAPIP().toString()); - debugController.printLn("Wifi Manager"); - debugController.printLn("Please connect to AP"); - debugController.printLn(myWiFiManager->getConfigPortalSSID()); - debugController.printLn("To setup Wifi Configuration"); - globalDataController.flashLED(20, 50); -} \ No newline at end of file + debugController.printLn("Entered config mode"); + debugController.printLn(WiFi.softAPIP().toString()); + displayClient.showApAccessScreen(myWiFiManager->getConfigPortalSSID(), WiFi.softAPIP().toString()); + debugController.printLn("Wifi Manager"); + debugController.printLn("Please connect to AP"); + debugController.printLn(myWiFiManager->getConfigPortalSSID()); + debugController.printLn("To setup Wifi Configuration"); + globalDataController.flashLED(20, 50); +}