This repository has been archived by the owner on Sep 13, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MMM-AI-Face-Login.js
executable file
·175 lines (148 loc) · 4.36 KB
/
MMM-AI-Face-Login.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
/* global Module */
/* Magic Mirror
* Module: MMM-AI-Face-Login
*
* By James Macdonald
* MIT Licensed.
*/
const MORNING = -1;
const AFTERNOON = 0;
const EVENING = 1;
Module.register("MMM-AI-Face-Login", {
defaults: {
updateInterval: 10000,
retryDelay: 5000,
width: "200px",
position: "lower_third",
useMMMFaceRecoDNN: false,
interval: 2000,
morningStartTime: 3,
morningEndTime: 12,
afternoonStartTime: 12,
afternoonEndTime: 17,
},
requiresVersion: "2.1.0", // Required version of MagicMirror
start: function() {
var self = this;
var dataRequest = null;
var dataNotification = null;
this.userName = "Guest";
this.userImage = "guest.gif";
this.timer = null;
// Are we logged in or not
this.loggedIn = false;
//Flag for check if module is loaded
this.loaded = false;
},
// This will be called every update and update the image based on:
// .userId
// .userName
getDom: function() {
var self = this;
// create element wrapper for show into the module
var wrapper = document.createElement("div");
wrapper.innerHTML = this.translate('TITLE');
wrapper.className = "face-image";
// Figure out what time of day message we want
var message = this.translate('WELCOME');
if ( this.userName !== "Guest")
{
if ( this.timeOfDay() == MORNING ) {
message = this.translate('GOOD_MORNING');
} else if (this.timeOfDay() == AFTERNOON) {
message = this.translate('GOOD_AFERNOON');
} else {
message = this.translate('GOOD_EVENING');
}
}
// Ceate the image element and show gif by default.
var imgHolderElement = document.createElement("p");
imgHolderElement.innerHTML = message + " " + this.capitalizeWords(this.userName);
imgHolderElement.classList.add(this.config.position);
imgHolderElement.style.width = this.config.width;
// Asychronoulsy load either GIF or face (as name).
var img = document.createElement("img");
var newImg = new Image;
newImg.src = "modules/MMM-AI-Face-Login/public/" + this.userImage;
newImg.onload = function()
{
img.src = this.src;
}
imgHolderElement.appendChild(img);
wrapper.appendChild(imgHolderElement);
return wrapper;
},
getScripts: function() {
return [];
},
getStyles: function () {
return [
"MMM-AI-Face-Login.css",
];
},
// Load translations files
getTranslations: function() {
//FIXME: This can be load a one file javascript definition
return {
en: "translations/en.json",
es: "translations/es.json",
ko: "translations/ko.json"
};
},
notificationReceived: function(notification, payload, sender) {
var self = this;
// Log.log("Got Notificaiton: " + notification + " Payload: " + payload);
switch (notification)
{
case "USERS_LOGIN":
{
// Face Rec sends multiple notifications even if user is already logged in and logout timer still active.
if (this.config.useMMMFaceRecoDNN === true && this.loggedIn == false )
{
Log.log("Notificaiton: " + notification + " from Mirror. Logging in " + payload);
// Fetch the users image.
this.loggedIn = true;
this.userName = payload;
this.userImage = payload + ".jpg"; //Assume for now.
self.updateDom(100);
// Clear existing timer and reset.
// This is only required if MMM-Face-Reco-DNN module not updated with new Notification for lougout.
// if ( this.timer != null ) clearInterval(this.timer);
// this.timer = setTimeout(()=>{
// Log.log("Logging out. Reset image.");
//
// this.loggedIn = false;
// this.userName = "Mr. Nobody";
// this.userImage = "guest.gif";
// self.updateDom(100);
// }, 25000);
}
break;
}
case "USERS_LOGOUT_MODULES":
{
Log.log("Notificaiton: " + notification + " from Mirror. Logging out " + payload);
this.loggedIn = false;
this.userName = "Mr. Nobody";
this.userImage = "guest.gif";
self.updateDom(100);
break;
}
}
},
// Local helper functions
capitalizeWords: function(str)
{
return str.toString().toLowerCase().split(' ').map(word => word.charAt(0).toUpperCase() + word.substring(1)).join(' ');
},
timeOfDay: function()
{
var hour = moment().hour();
if (hour >= this.config.morningStartTime && hour < this.config.morningEndTime) {
return MORNING;
} else if (hour >= this.config.afternoonStartTime && hour < this.config.afternoonEndTime ) {
return AFTERNOON;
}
return EVENING;
}
});