229 lines
6.5 KiB
JavaScript
Raw Normal View History

2016-03-24 17:19:32 +01:00
/* Magic Mirror
* Module: Compliments
*
2020-04-28 23:05:28 +02:00
* By Michael Teeuw https://michaelteeuw.nl
2016-03-24 17:19:32 +01:00
* MIT Licensed.
*/
2017-02-08 19:29:52 +02:00
Module.register("compliments", {
2016-03-24 17:19:32 +01:00
// Module config defaults.
defaults: {
compliments: {
anytime: ["Hey there sexy!"],
morning: ["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!"],
"....-01-01": ["Happy new year!"]
2016-03-24 17:19:32 +01:00
},
updateInterval: 30000,
remoteFile: null,
2018-02-05 18:15:02 +03:00
fadeSpeed: 4000,
morningStartTime: 3,
morningEndTime: 12,
afternoonStartTime: 12,
afternoonEndTime: 17,
2020-04-01 10:57:50 +02:00
random: true,
2020-05-14 14:56:38 +02:00
mockDate: null,
2020-05-14 15:00:09 +02:00
advice: false
2016-03-24 17:19:32 +01:00
},
lastIndexUsed: -1,
// 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-03-24 17:19:32 +01:00
this.lastComplimentIndex = -1;
var self = this;
2019-06-05 09:32:10 +02:00
if (this.config.remoteFile !== null) {
this.complimentFile(function (response) {
2018-04-07 17:26:50 -05:00
self.config.compliments = JSON.parse(response);
self.updateDom();
});
2020-05-14 14:56:38 +02:00
} else if (this.config.advice) {
2020-05-14 15:00:09 +02:00
var xobj = new XMLHttpRequest();
2020-05-14 14:56:38 +02:00
xobj.overrideMimeType("application/json");
2020-05-14 15:00:09 +02:00
xobj.open("GET", "https://api.adviceslip.com/advice", true);
xobj.onreadystatechange = function () {
2020-05-14 15:00:09 +02:00
if (xobj.readyState === 4 && xobj.status === 200) {
const adviceResp = JSON.parse(xobj.responseText);
self.config.compliments = adviceResp.slip.advice;
2020-05-14 15:00:09 +02:00
self.updateDom();
}
};
xobj.send(null);
}
2016-03-24 17:19:32 +01:00
// Schedule update timer.
setInterval(function () {
2016-03-24 17:19:32 +01:00
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) {
2016-03-24 17:19:32 +01:00
if (compliments.length === 1) {
return 0;
}
var generate = function () {
2016-03-24 17:19:32 +01:00
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 () {
2016-03-24 17:19:32 +01:00
var hour = moment().hour();
2020-04-01 10:57:50 +02:00
var date = this.config.mockDate ? this.config.mockDate : moment().format("YYYY-MM-DD");
2017-04-22 12:25:51 +02:00
var compliments;
2016-03-24 17:19:32 +01:00
2018-02-05 18:15:02 +03:00
if (hour >= this.config.morningStartTime && hour < this.config.morningEndTime && this.config.compliments.hasOwnProperty("morning")) {
2017-04-22 11:35:42 +02:00
compliments = this.config.compliments.morning.slice(0);
2018-02-05 18:15:02 +03:00
} else if (hour >= this.config.afternoonStartTime && hour < this.config.afternoonEndTime && this.config.compliments.hasOwnProperty("afternoon")) {
2017-04-22 11:35:42 +02:00
compliments = this.config.compliments.afternoon.slice(0);
} else if (this.config.compliments.hasOwnProperty("evening")) {
2017-04-22 11:35:42 +02:00
compliments = this.config.compliments.evening.slice(0);
}
2017-02-08 19:29:52 +02:00
if (typeof compliments === "undefined") {
compliments = new Array();
}
2017-02-08 19:29:52 +02:00
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);
2020-05-02 10:39:09 +02:00
for (var entry in this.config.compliments) {
2020-03-28 09:24:30 +01:00
if (new RegExp(entry).test(date)) {
compliments.push.apply(compliments, this.config.compliments[entry]);
}
2020-03-10 10:45:09 +01:00
}
return compliments;
2016-03-24 17:19:32 +01:00
},
/* complimentFile(callback)
* Retrieve a file from the local filesystem
*/
complimentFile: function (callback) {
var xobj = new XMLHttpRequest(),
isRemote = this.config.remoteFile.indexOf("http://") === 0 || this.config.remoteFile.indexOf("https://") === 0,
path = isRemote ? this.config.remoteFile : this.file(this.config.remoteFile);
xobj.overrideMimeType("application/json");
xobj.open("GET", path, true);
xobj.onreadystatechange = function () {
2019-06-05 09:32:10 +02:00
if (xobj.readyState === 4 && xobj.status === 200) {
callback(xobj.responseText);
}
};
xobj.send(null);
},
2016-03-24 17:19:32 +01:00
/* complimentArray()
* Retrieve a random compliment.
*
* return compliment string - A compliment.
*/
randomCompliment: function () {
// get the current time of day compliments list
2016-03-24 17:19:32 +01:00
var compliments = this.complimentArray();
2019-12-08 10:07:55 -06:00
// variable for index to next message to display
2020-03-15 09:49:32 +01:00
let index = 0;
// are we randomizing
if (this.config.random) {
// yes
index = this.randomIndex(compliments);
} else {
2020-03-15 09:49:32 +01:00
// no, sequential
// if doing sequential, don't fall off the end
index = this.lastIndexUsed >= compliments.length - 1 ? 0 : ++this.lastIndexUsed;
}
2016-03-24 17:19:32 +01:00
2020-03-15 09:49:32 +01:00
return compliments[index] || "";
},
2016-03-24 17:19:32 +01:00
// Override dom generator.
getDom: function () {
2016-03-24 17:19:32 +01:00
var wrapper = document.createElement("div");
wrapper.className = this.config.classes ? this.config.classes : "thin xlarge bright pre-line";
// get the compliment text
var complimentText = this.randomCompliment();
// split it into parts on newline text
2020-03-15 09:49:32 +01:00
var parts = complimentText.split("\n");
// create a span to hold it all
2020-03-15 09:49:32 +01:00
var compliment = document.createElement("span");
// process all the parts of the compliment text
for (var part of parts) {
// create a text element for each part
compliment.appendChild(document.createTextNode(part));
// add a break `
compliment.appendChild(document.createElement("BR"));
}
// remove the last break
compliment.lastElementChild.remove();
wrapper.appendChild(compliment);
2016-03-24 17:19:32 +01:00
return wrapper;
},
// 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) {
2019-06-05 09:32:10 +02:00
if (notification === "CURRENTWEATHER_DATA") {
this.setCurrentWeatherType(payload.data);
}
}
2018-04-07 17:26:50 -05:00
});