Weather Working

pull/125/head
Robert Stein 2020-12-13 18:38:44 +01:00
parent 4ef1eb2d83
commit fdfe5fc548
8 changed files with 187 additions and 87 deletions

View File

@ -144,10 +144,10 @@ void GlobalDataController::readSettings() {
} }
fr.close(); fr.close();
this->getPrinterClient()->updatePrintClient(); this->getPrinterClient()->updatePrintClient();
//weatherClient.updateWeatherApiKey(WeatherApiKey); this->weatherClient->updateWeatherApiKey(this->WeatherApiKey);
//weatherClient.updateLanguage(WeatherLanguage); this->weatherClient->updateLanguage(this->WeatherLang);
//weatherClient.setMetric(IS_METRIC); this->weatherClient->setMetric(this->WeatherIsMetric);
//weatherClient.updateCityIdList(CityIDs, 1); this->weatherClient->updateCityId(this->WeatherCityId);
this->timeClient->setUtcOffset(this->getClockUtcOffset()); this->timeClient->setUtcOffset(this->getClockUtcOffset());
} }
@ -166,9 +166,9 @@ void GlobalDataController::writeSettings() {
f.println("printerAuthUser=" + this->PrinterAuthUser); f.println("printerAuthUser=" + this->PrinterAuthUser);
f.println("printerAuthPass=" + this->PrinterAuthPass); f.println("printerAuthPass=" + this->PrinterAuthPass);
f.println("printerHasPsu=" + String(this->PrinterHasPsu)); f.println("printerHasPsu=" + String(this->PrinterHasPsu));
#ifndef USE_NEXTION_DISPLAY #ifndef USE_NEXTION_DISPLAY
f.println("displayInvertDisplay=" + String(this->DisplayInvertDisplay)); f.println("displayInvertDisplay=" + String(this->DisplayInvertDisplay));
#endif #endif
f.println("webserverTheme=" + this->WebserverTheme); f.println("webserverTheme=" + this->WebserverTheme);
f.println("webserverIsBasicAuth=" + String(this->WebserverIsBasicAuth)); f.println("webserverIsBasicAuth=" + String(this->WebserverIsBasicAuth));
f.println("webserverUsername=" + String(this->WebserverUsername)); f.println("webserverUsername=" + String(this->WebserverUsername));
@ -186,7 +186,6 @@ void GlobalDataController::writeSettings() {
} }
f.close(); f.close();
readSettings(); readSettings();
this->getTimeClient()->setUtcOffset(this->ClockUtcOffset);
} }
String GlobalDataController::getVersion() { String GlobalDataController::getVersion() {
@ -201,6 +200,10 @@ TimeClient *GlobalDataController::getTimeClient() {
return this->timeClient; return this->timeClient;
} }
OpenWeatherMapClient *GlobalDataController::getWeatherClient() {
return this->weatherClient;
}
void GlobalDataController::setPrinterClient(BasePrinterClient *basePrinterClient) { void GlobalDataController::setPrinterClient(BasePrinterClient *basePrinterClient) {
this->basePrinterClient = basePrinterClient; this->basePrinterClient = basePrinterClient;
} }
@ -353,6 +356,46 @@ void GlobalDataController::setUseLedFlash(bool useLedFlash) {
this->UseLedFlash = 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 * Notify LED
*/ */

View File

@ -62,6 +62,7 @@ public:
void setPrinterClient(BasePrinterClient *basePrinterClient); void setPrinterClient(BasePrinterClient *basePrinterClient);
TimeClient *getTimeClient(); TimeClient *getTimeClient();
OpenWeatherMapClient *getWeatherClient();
BasePrinterClient *getPrinterClient(); BasePrinterClient *getPrinterClient();
String getLastReportStatus(); String getLastReportStatus();
String getVersion(); String getVersion();
@ -106,6 +107,17 @@ public:
bool useLedFlash(); bool useLedFlash();
void setUseLedFlash(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 ledOnOff(boolean value);
void flashLED(int number, int delayTime); void flashLED(int number, int delayTime);
bool resetConfig(); bool resetConfig();

View File

@ -2,11 +2,10 @@
OpenWeatherMapClient::OpenWeatherMapClient(String ApiKey, int CityID, int cityCount, boolean isMetric, String language, DebugController *debugController) { OpenWeatherMapClient::OpenWeatherMapClient(String ApiKey, int CityID, int cityCount, boolean isMetric, String language, DebugController *debugController) {
this->debugController = debugController; this->debugController = debugController;
int CityIDs[1]; updateCityId(CityID);
CityIDs[0] = CityID;
updateCityIdList(CityIDs, cityCount);
updateLanguage(language); updateLanguage(language);
myApiKey = ApiKey; myApiKey = ApiKey;
this->isMetric = false;
setMetric(isMetric); setMetric(isMetric);
} }
@ -106,7 +105,6 @@ void OpenWeatherMapClient::updateWeather() {
this->debugController->printLn("description: " + weathers[inx].description); this->debugController->printLn("description: " + weathers[inx].description);
this->debugController->printLn("icon: " + weathers[inx].icon); this->debugController->printLn("icon: " + weathers[inx].icon);
this->debugController->printLn(""); this->debugController->printLn("");
} }
} }
@ -116,6 +114,12 @@ String OpenWeatherMapClient::roundValue(String value) {
return String(rounded); 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) { void OpenWeatherMapClient::updateCityIdList(int CityIDs[], int cityCount) {
myCityIDs = ""; myCityIDs = "";
for (int inx = 0; inx < cityCount; inx++) { for (int inx = 0; inx < cityCount; inx++) {
@ -134,6 +138,7 @@ void OpenWeatherMapClient::setMetric(boolean isMetric) {
} else { } else {
units = "imperial"; units = "imperial";
} }
this->isMetric = isMetric;
} }
String OpenWeatherMapClient::getWeatherResults() { String OpenWeatherMapClient::getWeatherResults() {
@ -282,3 +287,28 @@ String OpenWeatherMapClient::getWeatherIcon(int index)
} }
return W; 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 = "&#176;" + rtnValue;
} else {
rtnValue = "°" + rtnValue;
}
return rtnValue;
}
String OpenWeatherMapClient::getSpeedSymbol() {
String rtnValue = "mph";
if (this->isMetric) {
rtnValue = "kph";
}
return rtnValue;
}

View File

@ -10,6 +10,7 @@ private:
String myApiKey = ""; String myApiKey = "";
String units = ""; String units = "";
String lang = ""; String lang = "";
bool isMetric;
const char* servername = "api.openweathermap.org"; // remote server we will connect to const char* servername = "api.openweathermap.org"; // remote server we will connect to
String result; String result;
@ -40,6 +41,7 @@ public:
OpenWeatherMapClient(String ApiKey, int CityID, int cityCount, boolean isMetric, String language, DebugController *debugController); OpenWeatherMapClient(String ApiKey, int CityID, int cityCount, boolean isMetric, String language, DebugController *debugController);
void updateWeather(); void updateWeather();
void updateWeatherApiKey(String ApiKey); void updateWeatherApiKey(String ApiKey);
void updateCityId(int CityID);
void updateCityIdList(int CityIDs[], int cityCount); void updateCityIdList(int CityIDs[], int cityCount);
void updateLanguage(String language); void updateLanguage(String language);
void setMetric(boolean isMetric); void setMetric(boolean isMetric);
@ -65,4 +67,7 @@ public:
String getMyCityIDs(); String getMyCityIDs();
String getWeatherIcon(int index); String getWeatherIcon(int index);
String getError(); String getError();
String getTempSymbol();
String getTempSymbol(boolean forHTML);
String getSpeedSymbol();
}; };

View File

@ -5,14 +5,16 @@ TimeClient::TimeClient(float utcOffset, DebugController * debugController) {
this->debugController = debugController; this->debugController = debugController;
} }
void TimeClient::handleSync(int snycDelayMinutes) { bool TimeClient::handleSync(int snycDelayMinutes) {
//Get Time Update //Get Time Update
if((this->getMinutesFromLastRefresh() >= snycDelayMinutes) || this->lastEpoch == 0) { if((this->getMinutesFromLastRefresh() >= snycDelayMinutes) || this->lastEpoch == 0) {
this->debugController->printLn("Updating Time..."); this->debugController->printLn("Updating Time...");
this->updateTime(); this->updateTime();
this->lastEpoch = this->getCurrentEpoch(); this->lastEpoch = this->getCurrentEpoch();
this->debugController->printLn("Local time: " + this->getAmPmFormattedTime()); this->debugController->printLn("Local time: " + this->getAmPmFormattedTime());
return true;
} }
return false;
} }
int TimeClient::getMinutesFromLastRefresh() { int TimeClient::getMinutesFromLastRefresh() {
@ -20,6 +22,10 @@ int TimeClient::getMinutesFromLastRefresh() {
return minutes; return minutes;
} }
void TimeClient::resetLastEpoch() {
this->lastEpoch = 0;
}
void TimeClient::updateTime() { void TimeClient::updateTime() {
WiFiClient client; WiFiClient client;

View File

@ -20,8 +20,9 @@ private:
public: public:
TimeClient(float utcOffset, DebugController * debugController); TimeClient(float utcOffset, DebugController * debugController);
void updateTime(); void updateTime();
void handleSync(int snycDelayMinutes); bool handleSync(int snycDelayMinutes);
int getMinutesFromLastRefresh(); int getMinutesFromLastRefresh();
void resetLastEpoch();
void setUtcOffset(float utcOffset); void setUtcOffset(float utcOffset);
String getHours(); String getHours();

View File

@ -233,30 +233,30 @@ void WebServer::displayPrinterStatus() {
this->server->sendContent(html); // spit out what we got this->server->sendContent(html); // spit out what we got
html = ""; html = "";
/* if (this->globalDataController->getWeatherShow()) {
if (DISPLAYWEATHER) { OpenWeatherMapClient *weatherClient = this->globalDataController->getWeatherClient();
if (weatherClient.getCity(0) == "") { if (weatherClient->getCity(0) == "") {
html += "<p>Please <a href='/configureweather'>Configure Weather</a> API</p>"; html += "<p>Please <a href='/configureweather'>Configure Weather</a> API</p>";
if (weatherClient.getError() != "") { if (weatherClient->getError() != "") {
html += "<p>Weather Error: <strong>" + weatherClient.getError() + "</strong></p>"; html += "<p>Weather Error: <strong>" + weatherClient->getError() + "</strong></p>";
} }
} else { } else {
html += "<div class='w3-cell-row' style='width:100%'><h2>" + weatherClient.getCity(0) + ", " + weatherClient.getCountry(0) + "</h2></div><div class='w3-cell-row'>"; html += "<div class='w3-cell-row' style='width:100%'><h2>" + weatherClient->getCity(0) + ", " + weatherClient->getCountry(0) + "</h2></div><div class='w3-cell-row'>";
html += "<div class='w3-cell w3-left w3-medium' style='width:120px'>"; html += "<div class='w3-cell w3-left w3-medium' style='width:120px'>";
html += "<img src='http://openweathermap.org/img/w/" + weatherClient.getIcon(0) + ".png' alt='" + weatherClient.getDescription(0) + "'><br>"; html += "<img src='http://openweathermap.org/img/w/" + weatherClient->getIcon(0) + ".png' alt='" + weatherClient->getDescription(0) + "'><br>";
html += weatherClient.getHumidity(0) + "% Humidity<br>"; html += weatherClient->getHumidity(0) + "% Humidity<br>";
html += weatherClient.getWind(0) + " <span class='w3-tiny'>" + getSpeedSymbol() + "</span> Wind<br>"; html += weatherClient->getWind(0) + " <span class='w3-tiny'>" + weatherClient->getSpeedSymbol() + "</span> Wind<br>";
html += "</div>"; html += "</div>";
html += "<div class='w3-cell w3-container' style='width:100%'><p>"; html += "<div class='w3-cell w3-container' style='width:100%'><p>";
html += weatherClient.getCondition(0) + " (" + weatherClient.getDescription(0) + ")<br>"; html += weatherClient->getCondition(0) + " (" + weatherClient->getDescription(0) + ")<br>";
html += weatherClient.getTempRounded(0) + getTempSymbol(true) + "<br>"; html += weatherClient->getTempRounded(0) + weatherClient->getTempSymbol(true) + "<br>";
html += "<a href='https://www.google.com/maps/@" + weatherClient.getLat(0) + "," + weatherClient.getLon(0) + ",10000m/data=!3m1!1e3' target='_BLANK'><i class='fa fa-map-marker' style='color:red'></i> Map It!</a><br>"; html += "<a href='https://www.google.com/maps/@" + weatherClient->getLat(0) + "," + weatherClient->getLon(0) + ",10000m/data=!3m1!1e3' target='_BLANK'><i class='fa fa-map-marker' style='color:red'></i> Map It!</a><br>";
html += "</p></div></div>"; html += "</p></div></div>";
} }
server.sendContent(html); // spit out what we got this->server->sendContent(html); // spit out what we got
html = ""; // fresh start html = ""; // fresh start
}*/ }
this->server->sendContent(String(getFooter())); this->server->sendContent(String(getFooter()));
this->server->sendContent(""); this->server->sendContent("");
@ -327,8 +327,8 @@ void WebServer::handleUpdateConfig() {
display.flipScreenVertically(); display.flipScreenVertically();
ui.update(); ui.update();
} }
checkDisplay(); checkDisplay();*/
lastEpoch = 0; */ this->globalDataController->getTimeClient()->resetLastEpoch();
this->redirectHome(); this->redirectHome();
} }
@ -336,16 +336,16 @@ void WebServer::handleUpdateWeather() {
if (!this->authentication()) { if (!this->authentication()) {
return this->server->requestAuthentication(); return this->server->requestAuthentication();
} }
//DISPLAYWEATHER = server.hasArg("isWeatherEnabled"); this->globalDataController->setWeatherShow(this->server->hasArg("isWeatherEnabled"));
//WeatherApiKey = server.arg("openWeatherMapApiKey"); this->globalDataController->setWeatherApiKey(this->server->arg("openWeatherMapApiKey"));
//CityIDs[0] = server.arg("city1").toInt(); this->globalDataController->setWeatherCityId(this->server->arg("city1").toInt());
//IS_METRIC = server.hasArg("metric"); this->globalDataController->setWeatherIsMetric(this->server->hasArg("metric"));
//WeatherLanguage = server.arg("language"); this->globalDataController->setWeatherLang(this->server->arg("language"));
this->globalDataController->writeSettings(); this->globalDataController->writeSettings();
//isClockOn = false; // this will force a check for the display //isClockOn = false; // this will force a check for the display
//checkDisplay(); //checkDisplay();
//lastEpoch = 0; this->globalDataController->getTimeClient()->resetLastEpoch();
this->redirectHome(); this->redirectHome();
} }
@ -495,19 +495,19 @@ void WebServer::handleWeatherConfigure() {
if (DISPLAYWEATHER) { if (DISPLAYWEATHER) {
isWeatherChecked = "checked='checked'"; isWeatherChecked = "checked='checked'";
} }
/*form.replace("%IS_WEATHER_CHECKED%", isWeatherChecked); form.replace("%IS_WEATHER_CHECKED%", this->globalDataController->getWeatherShow() ? "1" : "0");
form.replace("%WEATHERKEY%", this->globalDataController()); form.replace("%WEATHERKEY%", this->globalDataController->getWeatherApiKey());
form.replace("%CITYNAME1%", weatherClient.getCity(0)); form.replace("%CITYNAME1%", this->globalDataController->getWeatherClient()->getCity(0));
form.replace("%CITY1%", String(CityIDs[0])); form.replace("%CITY1%", String(this->globalDataController->getWeatherCityId()));
String checked = ""; String checked = "";
if (IS_METRIC) { if (this->globalDataController->getWeatherIsMetric()) {
checked = "checked='checked'"; checked = "checked='checked'";
} }
form.replace("%METRIC%", checked); form.replace("%METRIC%", checked);
String options = FPSTR(LANG_OPTIONS); 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); form.replace("%LANGUAGEOPTIONS%", options);
this->server->sendContent(form);*/ this->server->sendContent(form);
html = this->getFooter(); html = this->getFooter();
this->server->sendContent(html); this->server->sendContent(html);

View File

@ -79,7 +79,10 @@ void setup() {
void loop() { void loop() {
// Handle update of time // Handle update of time
timeClient.handleSync(globalDataController.getClockResyncMinutes()); if(timeClient.handleSync(globalDataController.getClockResyncMinutes()) && globalDataController.getWeatherShow()) {
// We sync time? Ok, sync weather also!
weatherClient.updateWeather();
}