-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMMM-Fortune.js
136 lines (104 loc) · 4.13 KB
/
MMM-Fortune.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
/* Magic Mirror
* Module: MMM-Fortune
*
* By Mykle1
*
*/
Module.register("MMM-Fortune", {
// Module config defaults.
defaults: {
updateInterval: 60 * 60 * 1000, // every hour
fadeSpeed: 3000,
initialLoadDelay: 1250, // ms seconds delay
retryDelay: 2500,
header: "Opening your fortune cookie!",
maxWidth: "100%",
},
// Define required scripts.
getScripts: function() {
return ["moment.js"];
},
getStyles: function() {
return ["MMM-Fortune.css", "font-awesome.css"];
},
// Define start sequence.
start: function() {
Log.info("Starting module: " + this.name);
// Set locale.
moment.locale(config.language);
this.today = "";
this.fortune = [];
this.url = "http://fortunecookieapi.herokuapp.com/v1/cookie?fortuneId=&lottoId=&lessonId=&limit=";
this.scheduleUpdate();
},
getDom: function() {
var fortune = this.fortune;
var lesson = this.lesson;
var lotto = this.lotto;
var wrapper = document.createElement("div");
wrapper.className = "wrapper";
wrapper.style.maxWidth = this.config.maxWidth;
if (!this.loaded) {
wrapper.innerHTML = "Telling your fortune...";
wrapper.className = "bright light small";
return wrapper;
}
if (this.config.header != "") {
var header = document.createElement("header");
header.className = "header";
header.innerHTML = this.config.header;
wrapper.appendChild(header);
}
var top = document.createElement("div");
top.classList.add("content");
var title = document.createElement("h3");
title.classList.add("small");
title.innerHTML = this.fortune.message;
top.appendChild(title);
var des = document.createElement("p");
des.classList.add("xsmall", "bright");
des.innerHTML = 'English phrase: ' + ' ' + lesson.english + ' ' + ' ' + 'In Chinese: ' + ' ' + lesson.chinese + ' ' + ' ' + ' Pronounced: ' + lesson.pronunciation; // <- Objects goes in there
top.appendChild(des);
var des2 = document.createElement("p");
des2.classList.add("xsmall", "bright");
var numbers = lotto.numbers.sort(function(a, b) {
return a - b
});
var lotNumbers = numbers;
var showNumbers = lotNumbers.join(', ');
des2.innerHTML = "Lucky Numbers ~ " + showNumbers;
top.appendChild(des2);
wrapper.appendChild(top);
return wrapper;
},
///// Add this function to the modules you want to control with voice //////
notificationReceived: function(notification, payload) {
if (notification === 'HIDE_FORTUNE') {
this.hide(1000);
} else if (notification === 'SHOW_FORTUNE') {
this.show(1000);
}
},
processFortune: function(data) {
this.fortune = data.fortune;
this.lesson = data.lesson;
this.lotto = data.lotto;
this.loaded = true;
},
scheduleUpdate: function() {
setInterval(() => {
this.getFortune();
}, this.config.updateInterval);
this.getFortune(this.config.initialLoadDelay);
},
getFortune: function() {
this.sendSocketNotification('GET_FORTUNE', this.url);
},
socketNotificationReceived: function(notification, payload) {
if (notification === "FORTUNE_RESULT") {
this.processFortune(payload);
this.updateDom(this.config.fadeSpeed);
}
this.updateDom(this.config.initialLoadDelay);
},
});