2016-03-24 17:19:32 +01:00
|
|
|
/* global Log, Module, moment */
|
|
|
|
|
|
|
|
/* Magic Mirror
|
|
|
|
* Module: Compliments
|
|
|
|
*
|
|
|
|
* By Michael Teeuw http://michaelteeuw.nl
|
|
|
|
* MIT Licensed.
|
|
|
|
*/
|
|
|
|
|
2016-04-05 14:35:11 -04:00
|
|
|
Module.register("compliments",{
|
2016-03-24 17:19:32 +01:00
|
|
|
|
|
|
|
// Module config defaults.
|
|
|
|
defaults: {
|
|
|
|
compliments: {
|
2017-02-05 19:28:42 -06:00
|
|
|
anytime: [
|
|
|
|
"Hey there sexy!"
|
|
|
|
],
|
2016-03-24 17:19:32 +01:00
|
|
|
morning: [
|
2016-04-05 14:35:11 -04:00
|
|
|
"Good morning, handsome!",
|
|
|
|
"Enjoy your day!",
|
|
|
|
"How was your sleep?"
|
|
|
|
],
|
|
|
|
afternoon: [
|
|
|
|
"Hello, beauty!",
|
|
|
|
"You look sexy!",
|
|
|
|
"Looking good today!"
|
|
|
|
],
|
|
|
|
evening: [
|
|
|
|
"Wow, you look hot!",
|
|
|
|
"You look nice!",
|
|
|
|
"Hi, sexy!"
|
|
|
|
]
|
2016-03-24 17:19:32 +01:00
|
|
|
},
|
|
|
|
updateInterval: 30000,
|
2016-12-01 19:48:53 -03:00
|
|
|
remoteFile: null,
|
2016-03-24 17:19:32 +01:00
|
|
|
fadeSpeed: 4000
|
|
|
|
},
|
|
|
|
|
2016-11-07 20:08:56 -03:00
|
|
|
// Set currentweather from module
|
|
|
|
currentWeatherType: "",
|
|
|
|
|
2016-03-24 17:19:32 +01:00
|
|
|
// Define required scripts.
|
|
|
|
getScripts: function() {
|
2016-04-05 14:35:11 -04:00
|
|
|
return ["moment.js"];
|
2016-03-24 17:19:32 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
// Define start sequence.
|
|
|
|
start: function() {
|
2016-04-05 14:35:11 -04:00
|
|
|
Log.info("Starting module: " + this.name);
|
2016-04-03 19:52:13 +02:00
|
|
|
|
2016-03-24 17:19:32 +01:00
|
|
|
this.lastComplimentIndex = -1;
|
|
|
|
|
2016-12-01 19:48:53 -03:00
|
|
|
if (this.config.remoteFile != null) {
|
|
|
|
this.complimentFile((response) => {
|
|
|
|
this.config.compliments = JSON.parse(response);
|
|
|
|
});
|
|
|
|
}
|
2016-11-30 10:30:49 -05:00
|
|
|
|
2016-03-24 17:19:32 +01:00
|
|
|
// Schedule update timer.
|
|
|
|
var self = this;
|
|
|
|
setInterval(function() {
|
|
|
|
self.updateDom(self.config.fadeSpeed);
|
|
|
|
}, this.config.updateInterval);
|
|
|
|
},
|
|
|
|
|
|
|
|
/* randomIndex(compliments)
|
|
|
|
* Generate a random index for a list of compliments.
|
|
|
|
*
|
|
|
|
* argument compliments Array<String> - Array with compliments.
|
|
|
|
*
|
|
|
|
* return Number - Random index.
|
|
|
|
*/
|
|
|
|
randomIndex: function(compliments) {
|
|
|
|
if (compliments.length === 1) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
var generate = function() {
|
|
|
|
return Math.floor(Math.random() * compliments.length);
|
|
|
|
};
|
|
|
|
|
|
|
|
var complimentIndex = generate();
|
|
|
|
|
|
|
|
while (complimentIndex === this.lastComplimentIndex) {
|
|
|
|
complimentIndex = generate();
|
|
|
|
}
|
|
|
|
|
|
|
|
this.lastComplimentIndex = complimentIndex;
|
|
|
|
|
|
|
|
return complimentIndex;
|
|
|
|
},
|
|
|
|
|
|
|
|
/* complimentArray()
|
|
|
|
* Retrieve an array of compliments for the time of the day.
|
|
|
|
*
|
|
|
|
* return compliments Array<String> - Array with compliments for the time of the day.
|
|
|
|
*/
|
|
|
|
complimentArray: function() {
|
|
|
|
var hour = moment().hour();
|
2016-11-07 20:08:56 -03:00
|
|
|
var compliments = null;
|
2016-03-24 17:19:32 +01:00
|
|
|
|
|
|
|
if (hour >= 3 && hour < 12) {
|
2016-11-07 20:08:56 -03:00
|
|
|
compliments = this.config.compliments.morning;
|
2016-03-24 17:19:32 +01:00
|
|
|
} else if (hour >= 12 && hour < 17) {
|
2016-11-07 20:08:56 -03:00
|
|
|
compliments = this.config.compliments.afternoon;
|
2016-03-24 17:19:32 +01:00
|
|
|
} else {
|
2016-11-07 20:08:56 -03:00
|
|
|
compliments = this.config.compliments.evening;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( this.currentWeatherType in this.config.compliments) {
|
|
|
|
compliments.push.apply(compliments, this.config.compliments[this.currentWeatherType]);
|
2016-03-24 17:19:32 +01:00
|
|
|
}
|
2017-02-05 19:28:42 -06:00
|
|
|
|
|
|
|
compliments.push.apply(compliments, this.config.compliments.anytime);
|
|
|
|
|
2016-11-07 20:08:56 -03:00
|
|
|
return compliments;
|
|
|
|
|
2016-03-24 17:19:32 +01:00
|
|
|
},
|
|
|
|
|
2016-11-30 10:30:49 -05:00
|
|
|
/* complimentFile(callback)
|
|
|
|
* Retrieve a file from the local filesystem
|
|
|
|
*/
|
2016-12-01 19:48:53 -03:00
|
|
|
complimentFile: function(callback) {
|
|
|
|
var xobj = new XMLHttpRequest();
|
|
|
|
xobj.overrideMimeType("application/json");
|
|
|
|
xobj.open("GET", this.file(this.config.remoteFile), true);
|
|
|
|
xobj.onreadystatechange = function () {
|
|
|
|
if (xobj.readyState == 4 && xobj.status == "200") {
|
|
|
|
callback(xobj.responseText);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
xobj.send(null);
|
|
|
|
},
|
2016-11-30 10:30:49 -05:00
|
|
|
|
2016-03-24 17:19:32 +01:00
|
|
|
/* complimentArray()
|
|
|
|
* Retrieve a random compliment.
|
|
|
|
*
|
|
|
|
* return compliment string - A compliment.
|
|
|
|
*/
|
|
|
|
randomCompliment: function() {
|
|
|
|
var compliments = this.complimentArray();
|
|
|
|
var index = this.randomIndex(compliments);
|
|
|
|
|
|
|
|
return compliments[index];
|
2016-04-03 19:52:13 +02:00
|
|
|
},
|
2016-03-24 17:19:32 +01:00
|
|
|
|
|
|
|
// Override dom generator.
|
|
|
|
getDom: function() {
|
|
|
|
var complimentText = this.randomCompliment();
|
|
|
|
|
2016-04-03 19:52:13 +02:00
|
|
|
var compliment = document.createTextNode(complimentText);
|
2016-03-24 17:19:32 +01:00
|
|
|
var wrapper = document.createElement("div");
|
2016-12-07 22:00:12 -03:00
|
|
|
wrapper.className = this.config.classes ? this.config.classes : "thin xlarge bright";
|
2016-03-24 17:19:32 +01:00
|
|
|
wrapper.appendChild(compliment);
|
|
|
|
|
2016-04-03 19:52:13 +02:00
|
|
|
return wrapper;
|
2016-11-07 20:08:56 -03:00
|
|
|
},
|
|
|
|
|
|
|
|
|
|
|
|
// From data currentweather set weather type
|
|
|
|
setCurrentWeatherType: function(data) {
|
|
|
|
var weatherIconTable = {
|
|
|
|
"01d": "day_sunny",
|
|
|
|
"02d": "day_cloudy",
|
|
|
|
"03d": "cloudy",
|
|
|
|
"04d": "cloudy_windy",
|
|
|
|
"09d": "showers",
|
|
|
|
"10d": "rain",
|
|
|
|
"11d": "thunderstorm",
|
|
|
|
"13d": "snow",
|
|
|
|
"50d": "fog",
|
|
|
|
"01n": "night_clear",
|
|
|
|
"02n": "night_cloudy",
|
|
|
|
"03n": "night_cloudy",
|
|
|
|
"04n": "night_cloudy",
|
|
|
|
"09n": "night_showers",
|
|
|
|
"10n": "night_rain",
|
|
|
|
"11n": "night_thunderstorm",
|
|
|
|
"13n": "night_snow",
|
|
|
|
"50n": "night_alt_cloudy_windy"
|
|
|
|
};
|
|
|
|
this.currentWeatherType = weatherIconTable[data.weather[0].icon];
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
|
|
// Override notification handler.
|
|
|
|
notificationReceived: function(notification, payload, sender) {
|
|
|
|
if (notification == "CURRENTWEATHER_DATA") {
|
|
|
|
this.setCurrentWeatherType(payload.data);
|
|
|
|
}
|
|
|
|
},
|
2016-03-24 17:19:32 +01:00
|
|
|
|
2016-04-03 19:52:13 +02:00
|
|
|
});
|