From fdfe5fc548698981a087c529b0eb53955460c9e8 Mon Sep 17 00:00:00 2001 From: Robert Stein Date: Sun, 13 Dec 2020 18:38:44 +0100 Subject: [PATCH] Weather Working --- src/Global/GlobalDataController.cpp | 121 ++++++++++++++++++--------- src/Global/GlobalDataController.h | 12 +++ src/Network/OpenWeatherMapClient.cpp | 46 ++++++++-- src/Network/OpenWeatherMapClient.h | 7 +- src/Network/TimeClient.cpp | 8 +- src/Network/TimeClient.h | 3 +- src/Network/WebServer.cpp | 72 ++++++++-------- src/main.cpp | 5 +- 8 files changed, 187 insertions(+), 87 deletions(-) diff --git a/src/Global/GlobalDataController.cpp b/src/Global/GlobalDataController.cpp index 1c17498..917020a 100644 --- a/src/Global/GlobalDataController.cpp +++ b/src/Global/GlobalDataController.cpp @@ -144,49 +144,48 @@ void GlobalDataController::readSettings() { } fr.close(); this->getPrinterClient()->updatePrintClient(); - //weatherClient.updateWeatherApiKey(WeatherApiKey); - //weatherClient.updateLanguage(WeatherLanguage); - //weatherClient.setMetric(IS_METRIC); - //weatherClient.updateCityIdList(CityIDs, 1); + this->weatherClient->updateWeatherApiKey(this->WeatherApiKey); + this->weatherClient->updateLanguage(this->WeatherLang); + this->weatherClient->setMetric(this->WeatherIsMetric); + this->weatherClient->updateCityId(this->WeatherCityId); this->timeClient->setUtcOffset(this->getClockUtcOffset()); } void GlobalDataController::writeSettings() { - // Save decoded message to SPIFFS file for playback on power up. - File f = LittleFS.open(CONFIG, "w"); - if (!f) { - this->debugController->printLn("File open failed!"); - } else { - this->debugController->printLn("Saving settings now..."); - f.println("printerApiKey=" + this->PrinterApiKey); - f.println("printerHostName=" + this->PrinterHostName); - f.println("printerServer=" + this->PrinterServer); - f.println("printerPort=" + String(this->PrinterPort)); - f.println("printerName=" + this->getPrinterClient()->getPrinterName()); - f.println("printerAuthUser=" + this->PrinterAuthUser); - f.println("printerAuthPass=" + this->PrinterAuthPass); - f.println("printerHasPsu=" + String(this->PrinterHasPsu)); -#ifndef USE_NEXTION_DISPLAY - f.println("displayInvertDisplay=" + String(this->DisplayInvertDisplay)); -#endif - f.println("webserverTheme=" + this->WebserverTheme); - f.println("webserverIsBasicAuth=" + String(this->WebserverIsBasicAuth)); - f.println("webserverUsername=" + String(this->WebserverUsername)); - f.println("webserverPassword=" + String(this->WebserverPassword)); - f.println("clockUtcOffset=" + String(this->ClockUtcOffset)); - f.println("clockResyncMinutes=" + String(this->ClockResyncMinutes)); - f.println("displayClock=" + String(this->DisplayClock)); - f.println("clockIs24h=" + String(this->ClockIs24h)); - f.println("weatherShow=" + String(this->WeatherShow)); - f.println("weatherApiKey=" + this->WeatherApiKey); - f.println("weatherCityId=" + String(this->WeatherCityId)); - f.println("weatherIsMetric=" + String(this->WeatherIsMetric)); - f.println("weatherLang=" + this->WeatherLang); - f.println("useLedFlash=" + String(this->UseLedFlash)); - } - f.close(); - readSettings(); - this->getTimeClient()->setUtcOffset(this->ClockUtcOffset); + // Save decoded message to SPIFFS file for playback on power up. + File f = LittleFS.open(CONFIG, "w"); + if (!f) { + this->debugController->printLn("File open failed!"); + } else { + this->debugController->printLn("Saving settings now..."); + f.println("printerApiKey=" + this->PrinterApiKey); + f.println("printerHostName=" + this->PrinterHostName); + f.println("printerServer=" + this->PrinterServer); + f.println("printerPort=" + String(this->PrinterPort)); + f.println("printerName=" + this->getPrinterClient()->getPrinterName()); + f.println("printerAuthUser=" + this->PrinterAuthUser); + f.println("printerAuthPass=" + this->PrinterAuthPass); + f.println("printerHasPsu=" + String(this->PrinterHasPsu)); + #ifndef USE_NEXTION_DISPLAY + f.println("displayInvertDisplay=" + String(this->DisplayInvertDisplay)); + #endif + f.println("webserverTheme=" + this->WebserverTheme); + f.println("webserverIsBasicAuth=" + String(this->WebserverIsBasicAuth)); + f.println("webserverUsername=" + String(this->WebserverUsername)); + f.println("webserverPassword=" + String(this->WebserverPassword)); + f.println("clockUtcOffset=" + String(this->ClockUtcOffset)); + f.println("clockResyncMinutes=" + String(this->ClockResyncMinutes)); + f.println("displayClock=" + String(this->DisplayClock)); + f.println("clockIs24h=" + String(this->ClockIs24h)); + f.println("weatherShow=" + String(this->WeatherShow)); + f.println("weatherApiKey=" + this->WeatherApiKey); + f.println("weatherCityId=" + String(this->WeatherCityId)); + f.println("weatherIsMetric=" + String(this->WeatherIsMetric)); + f.println("weatherLang=" + this->WeatherLang); + f.println("useLedFlash=" + String(this->UseLedFlash)); + } + f.close(); + readSettings(); } String GlobalDataController::getVersion() { @@ -201,6 +200,10 @@ TimeClient *GlobalDataController::getTimeClient() { return this->timeClient; } +OpenWeatherMapClient *GlobalDataController::getWeatherClient() { + return this->weatherClient; +} + void GlobalDataController::setPrinterClient(BasePrinterClient *basePrinterClient) { this->basePrinterClient = basePrinterClient; } @@ -353,6 +356,46 @@ void GlobalDataController::setUseLedFlash(bool useLedFlash) { this->UseLedFlash = useLedFlash; } +bool GlobalDataController::getWeatherShow() { + return this->WeatherShow; +} + +void GlobalDataController::setWeatherShow(bool weatherShow) { + this->WeatherShow = weatherShow; +} + +String GlobalDataController::getWeatherApiKey() { + return this->WeatherApiKey; +} + +void GlobalDataController::setWeatherApiKey(String weatherApiKey) { + this->WeatherApiKey = weatherApiKey; +} + +int GlobalDataController::getWeatherCityId() { + return this->WeatherCityId; +} + +void GlobalDataController::setWeatherCityId(int weatherCityId) { + this->WeatherCityId = weatherCityId; +} + +bool GlobalDataController::getWeatherIsMetric() { + return this->WeatherIsMetric; +} + +void GlobalDataController::setWeatherIsMetric(bool weatherIsMetric) { + this->WeatherIsMetric = weatherIsMetric; +} + +String GlobalDataController::getWeatherLang() { + return this->WeatherLang; +} + +void GlobalDataController::setWeatherLang(String weatherLang) { + this->WeatherLang = weatherLang; +} + /** * Notify LED */ diff --git a/src/Global/GlobalDataController.h b/src/Global/GlobalDataController.h index 55ccf6f..b31d8cd 100644 --- a/src/Global/GlobalDataController.h +++ b/src/Global/GlobalDataController.h @@ -62,6 +62,7 @@ public: void setPrinterClient(BasePrinterClient *basePrinterClient); TimeClient *getTimeClient(); + OpenWeatherMapClient *getWeatherClient(); BasePrinterClient *getPrinterClient(); String getLastReportStatus(); String getVersion(); @@ -106,6 +107,17 @@ public: bool useLedFlash(); void setUseLedFlash(bool useLedFlash); + bool getWeatherShow(); + void setWeatherShow(bool weatherShow); + String getWeatherApiKey(); + void setWeatherApiKey(String weatherApiKey); + int getWeatherCityId(); + void setWeatherCityId(int weatherCityId); + bool getWeatherIsMetric(); + void setWeatherIsMetric(bool weatherIsMetric); + String getWeatherLang(); + void setWeatherLang(String weatherLang); + void ledOnOff(boolean value); void flashLED(int number, int delayTime); bool resetConfig(); diff --git a/src/Network/OpenWeatherMapClient.cpp b/src/Network/OpenWeatherMapClient.cpp index 36c4fc8..ad8e661 100644 --- a/src/Network/OpenWeatherMapClient.cpp +++ b/src/Network/OpenWeatherMapClient.cpp @@ -2,11 +2,10 @@ OpenWeatherMapClient::OpenWeatherMapClient(String ApiKey, int CityID, int cityCount, boolean isMetric, String language, DebugController *debugController) { this->debugController = debugController; - int CityIDs[1]; - CityIDs[0] = CityID; - updateCityIdList(CityIDs, cityCount); + updateCityId(CityID); updateLanguage(language); myApiKey = ApiKey; + this->isMetric = false; setMetric(isMetric); } @@ -106,7 +105,6 @@ void OpenWeatherMapClient::updateWeather() { this->debugController->printLn("description: " + weathers[inx].description); this->debugController->printLn("icon: " + weathers[inx].icon); this->debugController->printLn(""); - } } @@ -116,14 +114,20 @@ String OpenWeatherMapClient::roundValue(String value) { return String(rounded); } +void OpenWeatherMapClient::updateCityId(int CityID) { + int CityIDs[1]; + CityIDs[0] = CityID; + this->updateCityIdList(CityIDs, 1); +} + void OpenWeatherMapClient::updateCityIdList(int CityIDs[], int cityCount) { myCityIDs = ""; for (int inx = 0; inx < cityCount; inx++) { if (CityIDs[inx] > 0) { - if (myCityIDs != "") { - myCityIDs = myCityIDs + ","; - } - myCityIDs = myCityIDs + String(CityIDs[inx]); + if (myCityIDs != "") { + myCityIDs = myCityIDs + ","; + } + myCityIDs = myCityIDs + String(CityIDs[inx]); } } } @@ -134,6 +138,7 @@ void OpenWeatherMapClient::setMetric(boolean isMetric) { } else { units = "imperial"; } + this->isMetric = isMetric; } String OpenWeatherMapClient::getWeatherResults() { @@ -282,3 +287,28 @@ String OpenWeatherMapClient::getWeatherIcon(int index) } return W; } + +String OpenWeatherMapClient::getTempSymbol() { + return this->getTempSymbol(false); +} + +String OpenWeatherMapClient::getTempSymbol(boolean forHTML) { + String rtnValue = "F"; + if (this->isMetric) { + rtnValue = "C"; + } + if (forHTML) { + rtnValue = "°" + rtnValue; + } else { + rtnValue = "°" + rtnValue; + } + return rtnValue; +} + +String OpenWeatherMapClient::getSpeedSymbol() { + String rtnValue = "mph"; + if (this->isMetric) { + rtnValue = "kph"; + } + return rtnValue; +} \ No newline at end of file diff --git a/src/Network/OpenWeatherMapClient.h b/src/Network/OpenWeatherMapClient.h index aef1f96..11dad88 100644 --- a/src/Network/OpenWeatherMapClient.h +++ b/src/Network/OpenWeatherMapClient.h @@ -10,7 +10,8 @@ private: String myApiKey = ""; String units = ""; String lang = ""; - + bool isMetric; + const char* servername = "api.openweathermap.org"; // remote server we will connect to String result; @@ -40,6 +41,7 @@ public: OpenWeatherMapClient(String ApiKey, int CityID, int cityCount, boolean isMetric, String language, DebugController *debugController); void updateWeather(); void updateWeatherApiKey(String ApiKey); + void updateCityId(int CityID); void updateCityIdList(int CityIDs[], int cityCount); void updateLanguage(String language); void setMetric(boolean isMetric); @@ -65,4 +67,7 @@ public: String getMyCityIDs(); String getWeatherIcon(int index); String getError(); + String getTempSymbol(); + String getTempSymbol(boolean forHTML); + String getSpeedSymbol(); }; \ No newline at end of file diff --git a/src/Network/TimeClient.cpp b/src/Network/TimeClient.cpp index d8d18e4..ce0f34c 100644 --- a/src/Network/TimeClient.cpp +++ b/src/Network/TimeClient.cpp @@ -5,14 +5,16 @@ TimeClient::TimeClient(float utcOffset, DebugController * debugController) { this->debugController = debugController; } -void TimeClient::handleSync(int snycDelayMinutes) { +bool TimeClient::handleSync(int snycDelayMinutes) { //Get Time Update if((this->getMinutesFromLastRefresh() >= snycDelayMinutes) || this->lastEpoch == 0) { this->debugController->printLn("Updating Time..."); this->updateTime(); this->lastEpoch = this->getCurrentEpoch(); this->debugController->printLn("Local time: " + this->getAmPmFormattedTime()); + return true; } + return false; } int TimeClient::getMinutesFromLastRefresh() { @@ -20,6 +22,10 @@ int TimeClient::getMinutesFromLastRefresh() { return minutes; } +void TimeClient::resetLastEpoch() { + this->lastEpoch = 0; +} + void TimeClient::updateTime() { WiFiClient client; diff --git a/src/Network/TimeClient.h b/src/Network/TimeClient.h index b2967f0..d026e26 100644 --- a/src/Network/TimeClient.h +++ b/src/Network/TimeClient.h @@ -20,8 +20,9 @@ private: public: TimeClient(float utcOffset, DebugController * debugController); void updateTime(); - void handleSync(int snycDelayMinutes); + bool handleSync(int snycDelayMinutes); int getMinutesFromLastRefresh(); + void resetLastEpoch(); void setUtcOffset(float utcOffset); String getHours(); diff --git a/src/Network/WebServer.cpp b/src/Network/WebServer.cpp index 4b8a309..7ffc847 100644 --- a/src/Network/WebServer.cpp +++ b/src/Network/WebServer.cpp @@ -233,30 +233,30 @@ void WebServer::displayPrinterStatus() { this->server->sendContent(html); // spit out what we got html = ""; - /* - if (DISPLAYWEATHER) { - if (weatherClient.getCity(0) == "") { - html += "

Please Configure Weather API

"; - if (weatherClient.getError() != "") { - html += "

Weather Error: " + weatherClient.getError() + "

"; - } + if (this->globalDataController->getWeatherShow()) { + OpenWeatherMapClient *weatherClient = this->globalDataController->getWeatherClient(); + if (weatherClient->getCity(0) == "") { + html += "

Please Configure Weather API

"; + if (weatherClient->getError() != "") { + html += "

Weather Error: " + weatherClient->getError() + "

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

" + weatherClient.getCity(0) + ", " + weatherClient.getCountry(0) + "

"; - html += "
"; - html += "" + weatherClient.getDescription(0) + "
"; - html += weatherClient.getHumidity(0) + "% Humidity
"; - html += weatherClient.getWind(0) + " " + getSpeedSymbol() + " Wind
"; - html += "
"; - html += "

"; - html += weatherClient.getCondition(0) + " (" + weatherClient.getDescription(0) + ")
"; - html += weatherClient.getTempRounded(0) + getTempSymbol(true) + "
"; - html += " Map It!
"; - html += "

"; + html += "

" + weatherClient->getCity(0) + ", " + weatherClient->getCountry(0) + "

"; + html += "
"; + html += "" + weatherClient->getDescription(0) + "
"; + html += weatherClient->getHumidity(0) + "% Humidity
"; + html += weatherClient->getWind(0) + " " + weatherClient->getSpeedSymbol() + " Wind
"; + html += "
"; + html += "

"; + html += weatherClient->getCondition(0) + " (" + weatherClient->getDescription(0) + ")
"; + html += weatherClient->getTempRounded(0) + weatherClient->getTempSymbol(true) + "
"; + html += " Map It!
"; + html += "

"; } - server.sendContent(html); // spit out what we got + this->server->sendContent(html); // spit out what we got html = ""; // fresh start - }*/ + } this->server->sendContent(String(getFooter())); this->server->sendContent(""); @@ -327,8 +327,8 @@ void WebServer::handleUpdateConfig() { display.flipScreenVertically(); ui.update(); } - checkDisplay(); - lastEpoch = 0; */ + checkDisplay();*/ + this->globalDataController->getTimeClient()->resetLastEpoch(); this->redirectHome(); } @@ -336,16 +336,16 @@ void WebServer::handleUpdateWeather() { if (!this->authentication()) { return this->server->requestAuthentication(); } - //DISPLAYWEATHER = server.hasArg("isWeatherEnabled"); - //WeatherApiKey = server.arg("openWeatherMapApiKey"); - //CityIDs[0] = server.arg("city1").toInt(); - //IS_METRIC = server.hasArg("metric"); - //WeatherLanguage = server.arg("language"); - + this->globalDataController->setWeatherShow(this->server->hasArg("isWeatherEnabled")); + this->globalDataController->setWeatherApiKey(this->server->arg("openWeatherMapApiKey")); + this->globalDataController->setWeatherCityId(this->server->arg("city1").toInt()); + this->globalDataController->setWeatherIsMetric(this->server->hasArg("metric")); + this->globalDataController->setWeatherLang(this->server->arg("language")); this->globalDataController->writeSettings(); + //isClockOn = false; // this will force a check for the display //checkDisplay(); - //lastEpoch = 0; + this->globalDataController->getTimeClient()->resetLastEpoch(); this->redirectHome(); } @@ -495,19 +495,19 @@ void WebServer::handleWeatherConfigure() { if (DISPLAYWEATHER) { isWeatherChecked = "checked='checked'"; } - /*form.replace("%IS_WEATHER_CHECKED%", isWeatherChecked); - form.replace("%WEATHERKEY%", this->globalDataController()); - form.replace("%CITYNAME1%", weatherClient.getCity(0)); - form.replace("%CITY1%", String(CityIDs[0])); + form.replace("%IS_WEATHER_CHECKED%", this->globalDataController->getWeatherShow() ? "1" : "0"); + form.replace("%WEATHERKEY%", this->globalDataController->getWeatherApiKey()); + form.replace("%CITYNAME1%", this->globalDataController->getWeatherClient()->getCity(0)); + form.replace("%CITY1%", String(this->globalDataController->getWeatherCityId())); String checked = ""; - if (IS_METRIC) { + if (this->globalDataController->getWeatherIsMetric()) { checked = "checked='checked'"; } form.replace("%METRIC%", checked); String options = FPSTR(LANG_OPTIONS); - options.replace(">"+String(WeatherLanguage)+"<", " selected>"+String(WeatherLanguage)+"<"); + options.replace(">"+String(this->globalDataController->getWeatherLang())+"<", " selected>"+String(this->globalDataController->getWeatherLang())+"<"); form.replace("%LANGUAGEOPTIONS%", options); - this->server->sendContent(form);*/ + this->server->sendContent(form); html = this->getFooter(); this->server->sendContent(html); diff --git a/src/main.cpp b/src/main.cpp index b440d53..26086ae 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -79,7 +79,10 @@ void setup() { void loop() { // Handle update of time - timeClient.handleSync(globalDataController.getClockResyncMinutes()); + if(timeClient.handleSync(globalDataController.getClockResyncMinutes()) && globalDataController.getWeatherShow()) { + // We sync time? Ok, sync weather also! + weatherClient.updateWeather(); + }