-
Notifications
You must be signed in to change notification settings - Fork 0
/
MMM-Smile.js
106 lines (89 loc) · 2.51 KB
/
MMM-Smile.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
/* global Module */
/* Magic Mirror
* Module: MMM-Smile
*
*
* MIT Licensed.
*/
Module.register('MMM-Smile', {
defaults: {
// recognition interval in ms, default to 8 hours
interval: 8 * 60 * 60 * 1000,
// test running time in seconds
testRunTime: 60,
// smile time in seconds
smileLength: 5,
// use pi camera by default
usePiCam: true
},
start: function() {
Log.info('Starting module: ' + this.name);
var self = this
this.show(1000, function() {
Log.log(this.name + ' is shown.');
})
this.message = 'Starting smile test...'
this.gifUrl = ''
this.progressBarWidth = 0
this.getSmileTestResult()
setTimeout(function() {
self.start()
}, this.config.interval);
},
getStyles: function() {
return ["MMM-Smile.css"]
},
getSmileTestResult: function() {
Log.info("Start smile test.");
this.pythonStarted = false
this.sendSocketNotification('START_TEST', this.config);
this.message = "Time to smile~";
},
// Override socket notification handler.
socketNotificationReceived: function(notification, payload) {
var self = this
if (notification === "GIF") {
this.gifUrl = payload
this.updateDom()
} else if (notification === "RESULT") {
if (payload === -1) {
this.message = "Let's try again"
setTimeout(function() {
self.start()
}, 1000);
} else {
if (payload >= 0 && payload < this.config.smileLength) {
this.message = "Keep smiling~"
} else {
this.sendSocketNotification('PASSED_TEST');
this.message = "Great job!"
setTimeout(function() {
self.hide(1000, function() {
Log.log(self.name + ' is hidden.');
})
}, 5000);
}
this.progressBarWidth = Math.round(100 * payload / this.config.smileLength).toString() + "%";
}
this.updateDom()
}
},
getDom: function() {
wrapper = document.createElement("div");
wrapper.className = 'thin large bright';
var h = document.createElement("p")
var t = document.createTextNode(this.message);
h.appendChild(t)
wrapper.appendChild(h)
if (this.gitUrl != '') {
var img = document.createElement("img");
img.src = this.gifUrl
wrapper.appendChild(img);
}
progressBar = document.createElement("div")
progressBar.id = "progress-bar"
progressBar.style.width = this.progressBarWidth
wrapper.appendChild(progressBar)
return wrapper;
}
});