This repository has been archived by the owner on Aug 26, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
MMM-PIR.js
133 lines (113 loc) · 3.87 KB
/
MMM-PIR.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
Module.register("MMM-PIR", {
defaults: {
sensorPin: 4,
delay: 10000,
turnOffDisplay: true,
showCountdown: true,
callbackScripts: []
},
start: function () {
Log.log(this.name + ' is started!');
this.sendSocketNotification("CONFIG", this.config);
moment.locale(config.language);
},
getDom: function () {
if (this.config.showCountdown) {
var self = this;
var html = document.createElement("div");
html.className = "wrapper";
if (typeof self.counter !== "undefined") {
var headline = document.createElement("div");
headline.className = "head";
headline.innerText = self.translate("HEADER");
html.appendChild(headline);
var time = document.createElement("div");
time.className = "time";
time.innerText = formatMillis(this.counter);
html.appendChild(time);
if (typeof self.presence === "object") {
var last = document.createElement("div");
last.className = "last";
last.innerText = self.translate("LAST_USER_PRESENCE") + " " + self.presence.format("dddd, LL HH:mm:ss");
html.appendChild(last);
}
}
return html;
}
},
getStyles: function () {
return ["mmm-pir-style.css"];
},
getScripts: function () {
return ["moment.js"];
},
getCommands: function (commander) {
commander.add({
command: 'resetPir',
callback: 'resetCountdown',
description: 'Resets the countdown to configured settings.',
});
commander.add({
command: 'resetPirDefaults',
callback: 'resetDefaults',
description: 'Reset the countdown to default settings.',
});
commander.add({
command: 'setCustomPirCountdown',
callback: 'setCustomCountdown',
description: 'Configure a custom countdown. (in seconds)'
});
},
getTranslations: function () {
return {
en: "translations/en.json",
de: "translations/de.json"
}
},
socketNotificationReceived: function (notification, payload) {
if (notification === "USER_PRESENCE") {
this.presence_temp = moment();
this.resetCountdown();
}
},
notificationReceived: function (notification, payload) {
if (notification === 'DOM_OBJECTS_CREATED') {
//DOM creation complete, let's start the module
this.resetCountdown();
}
},
resetDefaults: function () {
this.counter = this.config.delay;
},
resetCountdown: function () {
var self = this;
clearInterval(self.interval);
if (self.customCounter != null) {
self.counter = this.customCounter;
} else {
self.resetDefaults();
}
self.updateDom();
self.interval = setInterval(function () {
self.counter -= 1000;
if (self.counter <= 0) {
self.presence = self.presence_temp;
self.sendSocketNotification('TIMER_EXPIRED');
clearInterval(self.interval);
}
self.updateDom();
}, 1000);
},
setCustomCountdown: function (commander, handler) {
var ccd = parseInt(handler.args);
if (isNaN(ccd) || ccd < 1) {
handler.reply("TEXT", this.translate("TELEGRAM_COMMAND_ERROR", {"command": "setCustomPirCountdown"}));
} else {
this.customCounter = ccd * 1000;
this.resetCountdown();
}
}
});
function formatMillis(millis) {
return new Date(millis).toUTCString().match(/\d{2}:\d{2}:\d{2}/)[0];
}