Possibility to use the the calendar feed as the source for the weather.

This commit is contained in:
Michael Teeuw
2016-10-14 17:42:07 +02:00
parent 5858e862d9
commit 84dc1fa151
7 changed files with 162 additions and 40 deletions

View File

@@ -11,8 +11,8 @@ Module.register("weatherforecast",{
// Default module config.
defaults: {
location: "",
locationID: "",
location: false,
locationID: false,
appid: "",
units: config.units,
maxNumberOfDays: 7,
@@ -30,6 +30,9 @@ Module.register("weatherforecast",{
apiBase: "http://api.openweathermap.org/data/",
forecastEndpoint: "forecast/daily",
appendLocationNameToHeader: true,
calendarClass: "calendar",
iconTable: {
"01d": "wi-day-sunny",
"02d": "wi-day-cloudy",
@@ -52,6 +55,12 @@ Module.register("weatherforecast",{
},
},
// create a variable for the first upcoming calendaar event. Used if no location is specified.
firstEvent: false,
// create a variable to hold the location name based on the API result.
fetchedLocatioName: "",
// Define required scripts.
getScripts: function() {
return ["moment.js"];
@@ -95,12 +104,6 @@ Module.register("weatherforecast",{
return wrapper;
}
if (this.config.location === "") {
wrapper.innerHTML = "Please set the openweather <i>location</i> in the config for module: " + this.name + ".";
wrapper.className = "dimmed light small";
return wrapper;
}
if (!this.loaded) {
wrapper.innerHTML = this.translate("LOADING");
wrapper.className = "dimmed light small";
@@ -156,11 +159,50 @@ Module.register("weatherforecast",{
return table;
},
// Override getHeader method.
getHeader: function() {
if (this.config.appendLocationNameToHeader) {
return this.data.header + " " + this.fetchedLocatioName;
}
return this.data.header;
},
// Override notification handler.
notificationReceived: function(notification, payload, sender) {
if (notification === "DOM_OBJECTS_CREATED") {
if (this.config.appendLocationNameToHeader) {
this.hide(0, {lockString: this.identifier});
}
}
if (notification === "CALENDAR_EVENTS") {
var senderClasses = sender.data.classes.toLowerCase().split(" ");
if (senderClasses.indexOf(this.config.calendarClass.toLowerCase()) !== -1) {
var lastEvent = this.firstEvent;
this.firstEvent = false;
for (e in payload) {
var event = payload[e];
if (event.location || event.geo) {
this.firstEvent = event;
//Log.log("First upcoming event with location: ", event);
break;
}
}
}
}
},
/* updateWeather(compliments)
* Requests new data from openweather.org.
* Calls processWeather on succesfull response.
*/
updateWeather: function() {
if (this.config.appid === "") {
Log.error("WeatherForecast: APPID not set!");
return;
}
var url = this.config.apiBase + this.config.apiVersion + "/" + this.config.forecastEndpoint + this.getParams();
var self = this;
var retry = true;
@@ -172,11 +214,10 @@ Module.register("weatherforecast",{
if (this.status === 200) {
self.processWeather(JSON.parse(this.response));
} else if (this.status === 401) {
self.config.appid = "";
self.updateDom(self.config.animationSpeed);
Log.error(self.name + ": Incorrect APPID.");
retry = false;
retry = true;
} else {
Log.error(self.name + ": Could not load weather.");
}
@@ -196,11 +237,19 @@ Module.register("weatherforecast",{
*/
getParams: function() {
var params = "?";
if(this.config.locationID !== "") {
if(this.config.locationID !== false) {
params += "id=" + this.config.locationID;
} else {
} else if(this.config.location !== false) {
params += "q=" + this.config.location;
} else if (this.firstEvent && this.firstEvent.geo) {
params += "lat=" + this.firstEvent.geo.lat + "&lon=" + this.firstEvent.geo.lon
} else if (this.firstEvent && this.firstEvent.location) {
params += "q=" + this.firstEvent.location;
} else {
this.hide(this.config.animationSpeed, {lockString:this.identifier});
return;
}
params += "&units=" + this.config.units;
params += "&lang=" + this.config.lang;
/*
@@ -220,6 +269,7 @@ Module.register("weatherforecast",{
* argument data object - Weather information received form openweather.org.
*/
processWeather: function(data) {
this.fetchedLocatioName = data.city.name + ", " + data.city.country;
this.forecast = [];
for (var i = 0, count = data.list.length; i < count; i++) {
@@ -236,7 +286,7 @@ Module.register("weatherforecast",{
}
//Log.log(this.forecast);
this.show(this.config.animationSpeed, {lockString:this.identifier});
this.loaded = true;
this.updateDom(this.config.animationSpeed);
},