186 lines
5.3 KiB
JavaScript
Raw Normal View History

/* MagicMirror²
2016-03-24 17:19:32 +01:00
* 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,
random: true
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: async function () {
Log.info(`Starting module: ${this.name}`);
2016-03-24 17:19:32 +01:00
this.lastComplimentIndex = -1;
2019-06-05 09:32:10 +02:00
if (this.config.remoteFile !== null) {
const response = await this.loadComplimentFile();
this.config.compliments = JSON.parse(response);
this.updateDom();
2020-05-14 15:00:09 +02:00
}
2016-03-24 17:19:32 +01:00
// Schedule update timer.
setInterval(() => {
this.updateDom(this.config.fadeSpeed);
2016-03-24 17:19:32 +01:00
}, this.config.updateInterval);
},
/**
2016-03-24 17:19:32 +01:00
* Generate a random index for a list of compliments.
*
2021-05-15 14:49:04 +02:00
* @param {string[]} compliments Array with compliments.
* @returns {number} a random index of given array
2016-03-24 17:19:32 +01:00
*/
randomIndex: function (compliments) {
2016-03-24 17:19:32 +01:00
if (compliments.length === 1) {
return 0;
}
const generate = function () {
2016-03-24 17:19:32 +01:00
return Math.floor(Math.random() * compliments.length);
};
let complimentIndex = generate();
2016-03-24 17:19:32 +01:00
while (complimentIndex === this.lastComplimentIndex) {
complimentIndex = generate();
}
this.lastComplimentIndex = complimentIndex;
return complimentIndex;
},
/**
2016-03-24 17:19:32 +01:00
* Retrieve an array of compliments for the time of the day.
*
* @returns {string[]} array with compliments for the time of the day.
2016-03-24 17:19:32 +01:00
*/
complimentArray: function () {
const hour = moment().hour();
const date = moment().format("YYYY-MM-DD");
let compliments = [];
2016-03-24 17:19:32 +01:00
// Add time of day compliments
2018-02-05 18:15:02 +03:00
if (hour >= this.config.morningStartTime && hour < this.config.morningEndTime && this.config.compliments.hasOwnProperty("morning")) {
compliments = [...this.config.compliments.morning];
2018-02-05 18:15:02 +03:00
} else if (hour >= this.config.afternoonStartTime && hour < this.config.afternoonEndTime && this.config.compliments.hasOwnProperty("afternoon")) {
compliments = [...this.config.compliments.afternoon];
} else if (this.config.compliments.hasOwnProperty("evening")) {
compliments = [...this.config.compliments.evening];
2017-02-08 19:29:52 +02:00
}
// Add compliments based on weather
2017-02-08 19:29:52 +02:00
if (this.currentWeatherType in this.config.compliments) {
Array.prototype.push.apply(compliments, this.config.compliments[this.currentWeatherType]);
2016-03-24 17:19:32 +01:00
}
2017-02-05 19:28:42 -06:00
// Add compliments for anytime
Array.prototype.push.apply(compliments, this.config.compliments.anytime);
2017-02-05 19:28:42 -06:00
// Add compliments for special days
for (let entry in this.config.compliments) {
2020-03-28 09:24:30 +01:00
if (new RegExp(entry).test(date)) {
Array.prototype.push.apply(compliments, this.config.compliments[entry]);
2020-03-28 09:24:30 +01:00
}
2020-03-10 10:45:09 +01:00
}
return compliments;
2016-03-24 17:19:32 +01:00
},
/**
* Retrieve a file from the local filesystem
*
* @returns {Promise} Resolved when the file is loaded
*/
loadComplimentFile: async function () {
const isRemote = this.config.remoteFile.indexOf("http://") === 0 || this.config.remoteFile.indexOf("https://") === 0,
url = isRemote ? this.config.remoteFile : this.file(this.config.remoteFile);
const response = await fetch(url);
return await response.text();
},
/**
2016-03-24 17:19:32 +01:00
* Retrieve a random compliment.
*
* @returns {string} a compliment
2016-03-24 17:19:32 +01:00
*/
getRandomCompliment: function () {
// get the current time of day compliments list
const compliments = this.complimentArray();
2019-12-08 10:07:55 -06:00
// variable for index to next message to display
let index;
// 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 () {
const wrapper = document.createElement("div");
wrapper.className = this.config.classes ? this.config.classes : "thin xlarge bright pre-line";
// get the compliment text
const complimentText = this.getRandomCompliment();
// split it into parts on newline text
const parts = complimentText.split("\n");
// create a span to hold the compliment
const compliment = document.createElement("span");
// process all the parts of the compliment text
for (const part of parts) {
if (part !== "") {
// create a text element for each part
compliment.appendChild(document.createTextNode(part));
// add a break
compliment.appendChild(document.createElement("BR"));
}
}
// only add compliment to wrapper if there is actual text in there
if (compliment.children.length > 0) {
// remove the last break
compliment.lastElementChild.remove();
wrapper.appendChild(compliment);
}
return wrapper;
},
// Override notification handler.
notificationReceived: function (notification, payload, sender) {
if (notification === "CURRENTWEATHER_TYPE") {
this.currentWeatherType = payload.type;
}
}
2018-04-07 17:26:50 -05:00
});