mirror of
https://github.com/MichMich/MagicMirror.git
synced 2025-08-22 13:09:26 +00:00
Standardize: TO JSCS!
This commit is contained in:
@@ -7,16 +7,16 @@
|
||||
* MIT Licensed.
|
||||
*/
|
||||
|
||||
Module.register('weatherforecast',{
|
||||
Module.register("weatherforecast",{
|
||||
|
||||
// Default module config.
|
||||
defaults: {
|
||||
location: '',
|
||||
appid: '',
|
||||
units: 'metric',
|
||||
updateInterval: 10 * 60 * 1000, // every 10 minutes
|
||||
animationSpeed: 1000,
|
||||
timeFormat: config.timeFormat,
|
||||
location: "",
|
||||
appid: "",
|
||||
units: "metric",
|
||||
updateInterval: 10 * 60 * 1000, // every 10 minutes
|
||||
animationSpeed: 1000,
|
||||
timeFormat: config.timeFormat,
|
||||
lang: config.language,
|
||||
fade: true,
|
||||
fadePoint: 0.25, // Start on 1/4th of the list.
|
||||
@@ -24,46 +24,45 @@ Module.register('weatherforecast',{
|
||||
initialLoadDelay: 2500, // 2.5 seconds delay. This delay is used to keep the OpenWeather API happy.
|
||||
retryDelay: 2500,
|
||||
|
||||
apiVersion: '2.5',
|
||||
apiBase: 'http://api.openweathermap.org/data/',
|
||||
forecastEndpoint: 'forecast/daily',
|
||||
apiVersion: "2.5",
|
||||
apiBase: "http://api.openweathermap.org/data/",
|
||||
forecastEndpoint: "forecast/daily",
|
||||
|
||||
iconTable: {
|
||||
'01d':'wi-day-sunny',
|
||||
'02d':'wi-day-cloudy',
|
||||
'03d':'wi-cloudy',
|
||||
'04d':'wi-cloudy-windy',
|
||||
'09d':'wi-showers',
|
||||
'10d':'wi-rain',
|
||||
'11d':'wi-thunderstorm',
|
||||
'13d':'wi-snow',
|
||||
'50d':'wi-fog',
|
||||
'01n':'wi-night-clear',
|
||||
'02n':'wi-night-cloudy',
|
||||
'03n':'wi-night-cloudy',
|
||||
'04n':'wi-night-cloudy',
|
||||
'09n':'wi-night-showers',
|
||||
'10n':'wi-night-rain',
|
||||
'11n':'wi-night-thunderstorm',
|
||||
'13n':'wi-night-snow',
|
||||
'50n':'wi-night-alt-cloudy-windy'
|
||||
"01d": "wi-day-sunny",
|
||||
"02d": "wi-day-cloudy",
|
||||
"03d": "wi-cloudy",
|
||||
"04d": "wi-cloudy-windy",
|
||||
"09d": "wi-showers",
|
||||
"10d": "wi-rain",
|
||||
"11d": "wi-thunderstorm",
|
||||
"13d": "wi-snow",
|
||||
"50d": "wi-fog",
|
||||
"01n": "wi-night-clear",
|
||||
"02n": "wi-night-cloudy",
|
||||
"03n": "wi-night-cloudy",
|
||||
"04n": "wi-night-cloudy",
|
||||
"09n": "wi-night-showers",
|
||||
"10n": "wi-night-rain",
|
||||
"11n": "wi-night-thunderstorm",
|
||||
"13n": "wi-night-snow",
|
||||
"50n": "wi-night-alt-cloudy-windy"
|
||||
},
|
||||
},
|
||||
|
||||
// Define required scripts.
|
||||
getScripts: function() {
|
||||
return ['moment.js'];
|
||||
return ["moment.js"];
|
||||
},
|
||||
|
||||
// Define required scripts.
|
||||
getStyles: function() {
|
||||
return ['weather-icons.css', 'weatherforecast.css'];
|
||||
return ["weather-icons.css", "weatherforecast.css"];
|
||||
},
|
||||
|
||||
|
||||
// Define start sequence.
|
||||
start: function() {
|
||||
Log.info('Starting module: ' + this.name);
|
||||
Log.info("Starting module: " + this.name);
|
||||
|
||||
// Set locale.
|
||||
moment.locale(config.language);
|
||||
@@ -80,13 +79,13 @@ Module.register('weatherforecast',{
|
||||
getDom: function() {
|
||||
var wrapper = document.createElement("div");
|
||||
|
||||
if (this.config.appid === '') {
|
||||
if (this.config.appid === "") {
|
||||
wrapper.innerHTML = "Please set the correct openweather <i>appid</i> in the config for module: " + this.name + ".";
|
||||
wrapper.className = "dimmed light small";
|
||||
return wrapper;
|
||||
}
|
||||
|
||||
if (this.config.location === '') {
|
||||
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;
|
||||
@@ -98,7 +97,6 @@ Module.register('weatherforecast',{
|
||||
return wrapper;
|
||||
}
|
||||
|
||||
|
||||
var table = document.createElement("table");
|
||||
table.className = "small";
|
||||
|
||||
@@ -109,7 +107,7 @@ Module.register('weatherforecast',{
|
||||
table.appendChild(row);
|
||||
|
||||
var dayCell = document.createElement("td");
|
||||
dayCell.className = 'day';
|
||||
dayCell.className = "day";
|
||||
dayCell.innerHTML = forecast.day;
|
||||
row.appendChild(dayCell);
|
||||
|
||||
@@ -123,15 +121,14 @@ Module.register('weatherforecast',{
|
||||
|
||||
var maxTempCell = document.createElement("td");
|
||||
maxTempCell.innerHTML = forecast.maxTemp;
|
||||
maxTempCell.className = 'align-right bright max-temp';
|
||||
maxTempCell.className = "align-right bright max-temp";
|
||||
row.appendChild(maxTempCell);
|
||||
|
||||
var minTempCell = document.createElement("td");
|
||||
minTempCell.innerHTML = forecast.minTemp;
|
||||
minTempCell.className = 'align-right min-temp';
|
||||
minTempCell.className = "align-right min-temp";
|
||||
row.appendChild(minTempCell);
|
||||
|
||||
|
||||
if (this.config.fade && this.config.fadePoint < 1) {
|
||||
if (this.config.fadePoint < 0) {
|
||||
this.config.fadePoint = 0;
|
||||
@@ -144,45 +141,40 @@ Module.register('weatherforecast',{
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
return table;
|
||||
},
|
||||
|
||||
|
||||
/* updateWeather(compliments)
|
||||
* Requests new data from openweather.org.
|
||||
* Calls processWeather on succesfull response.
|
||||
*/
|
||||
updateWeather: function() {
|
||||
var url = this.config.apiBase + this.config.apiVersion + '/' + this.config.forecastEndpoint + this.getParams();
|
||||
var url = this.config.apiBase + this.config.apiVersion + "/" + this.config.forecastEndpoint + this.getParams();
|
||||
var self = this;
|
||||
var retry = true;
|
||||
|
||||
var weatherRequest = new XMLHttpRequest();
|
||||
weatherRequest.open("GET", url, true);
|
||||
weatherRequest.onreadystatechange = function() {
|
||||
if(this.readyState === 4) {
|
||||
if(this.status === 200) {
|
||||
self.processWeather(JSON.parse(this.response));
|
||||
} else if (this.status === 401) {
|
||||
self.config.appid = '';
|
||||
self.updateDom(self.config.animationSpeed);
|
||||
if (this.readyState === 4) {
|
||||
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;
|
||||
} else {
|
||||
Log.error(self.name + ": Could not load weather.");
|
||||
}
|
||||
Log.error(self.name + ": Incorrect APPID.");
|
||||
retry = false;
|
||||
} else {
|
||||
Log.error(self.name + ": Could not load weather.");
|
||||
}
|
||||
|
||||
if (retry) {
|
||||
self.scheduleUpdate((self.loaded) ? -1 : self.config.retryDelay);
|
||||
if (retry) {
|
||||
self.scheduleUpdate((self.loaded) ? -1 : self.config.retryDelay);
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
weatherRequest.send();
|
||||
},
|
||||
@@ -194,10 +186,10 @@ Module.register('weatherforecast',{
|
||||
*/
|
||||
getParams: function() {
|
||||
var params = "?";
|
||||
params += 'q=' + this.config.location;
|
||||
params += '&units=' + this.config.units;
|
||||
params += '&lang=' + this.config.lang;
|
||||
params += '&APPID=' + this.config.appid;
|
||||
params += "q=" + this.config.location;
|
||||
params += "&units=" + this.config.units;
|
||||
params += "&lang=" + this.config.lang;
|
||||
params += "&APPID=" + this.config.appid;
|
||||
|
||||
return params;
|
||||
},
|
||||
@@ -215,7 +207,7 @@ Module.register('weatherforecast',{
|
||||
var forecast = data.list[i];
|
||||
this.forecast.push({
|
||||
|
||||
day: moment(forecast.dt, 'X').format('ddd.'),
|
||||
day: moment(forecast.dt, "X").format("ddd."),
|
||||
icon: this.config.iconTable[forecast.weather[0].icon],
|
||||
maxTemp: this.roundValue(forecast.temp.max),
|
||||
minTemp: this.roundValue(forecast.temp.min)
|
||||
@@ -236,7 +228,7 @@ Module.register('weatherforecast',{
|
||||
*/
|
||||
scheduleUpdate: function(delay) {
|
||||
var nextLoad = this.config.updateInterval;
|
||||
if (typeof delay !== 'undefined' && delay >= 0) {
|
||||
if (typeof delay !== "undefined" && delay >= 0) {
|
||||
nextLoad = delay;
|
||||
}
|
||||
|
||||
@@ -273,7 +265,7 @@ Module.register('weatherforecast',{
|
||||
*
|
||||
* return number - Rounded Temperature.
|
||||
*/
|
||||
roundValue: function (temperature) {
|
||||
roundValue: function(temperature) {
|
||||
return parseFloat(temperature).toFixed(1);
|
||||
}
|
||||
});
|
||||
|
Reference in New Issue
Block a user