forked from Jopyth/MMM-Buttons
-
Notifications
You must be signed in to change notification settings - Fork 1
/
MMM-Buttons.js
151 lines (132 loc) · 3.96 KB
/
MMM-Buttons.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
/* global Module */
/* MagicMirror²
* Module: Buttons
*
* By Joseph Bethge
* MIT Licensed.
*/
Module.register("MMM-Buttons", {
// Default module config.
defaults: {
buttons: [
{
pin: 24,
activeLow: false,
name: "Button",
shortPress: [
{
title: "",
message: "",
imageFA: "",
notification: "",
payload: ""
}
],
longPress: [
{
title: "",
message: "",
imageFA: "",
notification: "",
payload: ""
}
],
}
],
minShortPressTime: 0,
maxShortPressTime: 500,
minLongPressTime: 3000,
bounceTimeout: 300
},
// Define start sequence.
start () {
Log.info("Starting module: " + this.name);
this.sendConfig();
this.intervals = [];
this.alerts = [];
for (var i = 0; i < this.config.buttons.length; i++)
{
this.intervals.push(undefined);
this.alerts.push(false);
}
},
// Override dom generator.
getDom () {
var wrapper = document.createElement("div");
return wrapper;
},
/* sendConfig()
* intialize backend
*/
sendConfig () {
this.sendSocketNotification("BUTTON_CONFIG", {
config: this.config
});
},
buttonUp (index, duration) {
if (this.alerts[index]) {
// alert already shown, clear interval to update it and hide it
if (this.intervals[index] !== undefined) {
clearInterval(this.intervals[index]);
}
this.alerts[index] = false;
this.sendNotification("HIDE_ALERT");
} else {
// no alert shown, clear time out for showing it
if (this.intervals[index] !== undefined) {
clearTimeout(this.intervals[index]);
}
}
this.intervals[index] = undefined;
var min = this.config.minShortPressTime;
var max = this.config.maxShortPressTime;
var shortPress = this.config.buttons[index].shortPress
var longPress = this.config.buttons[index].longPress
if (shortPress && min <= duration && duration <= max)
{
this.sendAction(shortPress);
}
min = this.config.minLongPressTime;
if (longPress && min <= duration)
{
this.sendAction(longPress);
}
},
sendAction (description) {
for (var i = 0; i < description.length; i++) {
this.sendNotification(description[i].notification, description[i].payload);
}
},
buttonDown (index) {
var self = this;
if (self.config.buttons[index].longPress && self.config.buttons[index].longPress.title)
{
this.intervals[index] = setTimeout(function () {
self.startAlert(index);
}, this.config.maxShortPressTime);
}
},
showAlert (index) {
// display the message
this.sendNotification("SHOW_ALERT", {
title: this.config.buttons[index].longPress.title,
message: this.config.buttons[index].longPress.message,
imageFA: this.config.buttons[index].longPress.imageFA
});
},
startAlert (index) {
this.alerts[index] = true;
this.showAlert(index);
},
// Override socket notification handler.
socketNotificationReceived (notification, payload) {
if (notification === "BUTTON_UP")
{
this.buttonUp(payload.index, payload.duration);
}
if (notification === "BUTTON_DOWN")
{
this.buttonDown(payload.index);
}
},
});