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();
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());
}
@ -166,9 +166,9 @@ void GlobalDataController::writeSettings() {
f.println("printerAuthUser=" + this->PrinterAuthUser);
f.println("printerAuthPass=" + this->PrinterAuthPass);
f.println("printerHasPsu=" + String(this->PrinterHasPsu));
#ifndef USE_NEXTION_DISPLAY
#ifndef USE_NEXTION_DISPLAY
f.println("displayInvertDisplay=" + String(this->DisplayInvertDisplay));
#endif
#endif
f.println("webserverTheme=" + this->WebserverTheme);
f.println("webserverIsBasicAuth=" + String(this->WebserverIsBasicAuth));
f.println("webserverUsername=" + String(this->WebserverUsername));
@ -186,7 +186,6 @@ void GlobalDataController::writeSettings() {
}
f.close();
readSettings();
this->getTimeClient()->setUtcOffset(this->ClockUtcOffset);
}
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
*/

View File

@ -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();

View File

@ -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,6 +114,12 @@ 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++) {
@ -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 = "&#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 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();
};

View File

@ -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;

View File

@ -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();

View File

@ -233,30 +233,30 @@ void WebServer::displayPrinterStatus() {
this->server->sendContent(html); // spit out what we got
html = "";
/*
if (DISPLAYWEATHER) {
if (weatherClient.getCity(0) == "") {
if (this->globalDataController->getWeatherShow()) {
OpenWeatherMapClient *weatherClient = this->globalDataController->getWeatherClient();
if (weatherClient->getCity(0) == "") {
html += "<p>Please <a href='/configureweather'>Configure Weather</a> API</p>";
if (weatherClient.getError() != "") {
html += "<p>Weather Error: <strong>" + weatherClient.getError() + "</strong></p>";
if (weatherClient->getError() != "") {
html += "<p>Weather Error: <strong>" + weatherClient->getError() + "</strong></p>";
}
} 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 += "<img src='http://openweathermap.org/img/w/" + weatherClient.getIcon(0) + ".png' alt='" + weatherClient.getDescription(0) + "'><br>";
html += weatherClient.getHumidity(0) + "% Humidity<br>";
html += weatherClient.getWind(0) + " <span class='w3-tiny'>" + getSpeedSymbol() + "</span> Wind<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->getWind(0) + " <span class='w3-tiny'>" + weatherClient->getSpeedSymbol() + "</span> Wind<br>";
html += "</div>";
html += "<div class='w3-cell w3-container' style='width:100%'><p>";
html += weatherClient.getCondition(0) + " (" + weatherClient.getDescription(0) + ")<br>";
html += weatherClient.getTempRounded(0) + 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 += weatherClient->getCondition(0) + " (" + weatherClient->getDescription(0) + ")<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 += "</p></div></div>";
}
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);

View File

@ -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();
}