-
-
Notifications
You must be signed in to change notification settings - Fork 13
/
MMM-ModuleScheduler.js
executable file
·139 lines (130 loc) · 4.82 KB
/
MMM-ModuleScheduler.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
/* Magic Mirror
* Module: MMM-ModuleScheduler
*
* By Ian Perrin http://ianperrin.com
* MIT Licensed.
*/
Module.register("MMM-ModuleScheduler", {
// Set the minimum MagicMirror module version for this module.
requiresVersion: "2.0.0",
// Module config defaults.
defaults: {
schedulerClass: "scheduler",
animationSpeed: 1000,
notification_schedule: false,
global_schedule: false,
debug: true,
uselock: true
},
// Define start sequence.
start: function () {
Log.info("Starting module: " + this.name);
this.sendSocketNotification("INITIALISE_SCHEDULER", this.config);
},
notificationReceived: function (notification, payload, sender) {
var self = this;
if (sender === undefined && notification === "ALL_MODULES_STARTED") {
// Create notification schedules
if (this.config.notification_schedule) {
this.sendSocketNotification("CREATE_NOTIFICATION_SCHEDULE", this.config.notification_schedule);
}
return;
}
if (sender === undefined && notification === "DOM_OBJECTS_CREATED") {
// Create global schedules
if (typeof this.config.global_schedule === "object") {
this.sendSocketNotification("CREATE_GLOBAL_SCHEDULE", this.config.global_schedule);
}
// Create module schedules
MM.getModules()
.exceptModule(this)
.withClass(this.config.schedulerClass)
.enumerate(function (module) {
Log.log(self.name + " wants to schedule the display of " + module.name);
if (typeof module.config.module_schedule === "object") {
self.sendSocketNotification("CREATE_MODULE_SCHEDULE", { name: module.name, id: module.identifier, schedule: module.config.module_schedule });
} else {
Log.error(module.name + " is configured to be scheduled, but the module_schedule option is undefined");
}
});
return;
}
},
socketNotificationReceived: function (notification, payload) {
var self = this;
if (notification === "SHOW_MODULE" || notification === "HIDE_MODULE" || notification === "DIM_MODULE") {
Log.log(this.name + " received a " + notification + " notification for " + payload.target);
MM.getModules()
.exceptModule(this)
.withClass(this.config.schedulerClass)
.enumerate(function (module) {
if (payload.target === module.identifier) {
self.setModuleDisplay(module, notification, payload.dimLevel ? payload.dimLevel : "25");
return;
}
});
}
if (notification === "SHOW_MODULES" || notification === "HIDE_MODULES" || notification === "DIM_MODULES") {
Log.log(this.name + " received a " + notification + " notification for " + (payload.target ? payload.target : "all") + " modules");
// Get all modules except this one
var modules = MM.getModules().exceptModule(this);
// Restrict to group of modules with specified class
if (payload.target) {
modules = modules.withClass(payload.target);
}
// Ignore specified modules
if (payload.ignoreModules) {
modules = modules.filter(function (module) {
if (payload.ignoreModules.indexOf(module.name) === -1) {
return true;
}
Log.log(self.name + " is ignoring " + module.name + " from the " + notification + " notification for " + (payload.target ? payload.target : "all") + " modules");
return false;
});
}
// Process the notification request
var action = notification.replace("_MODULES", "_MODULE");
var brightness = payload.dimLevel ? payload.dimLevel : "25";
for (var i = 0; i < modules.length; i++) {
this.setModuleDisplay(modules[i], action, brightness);
}
return;
}
if (notification === "SEND_NOTIFICATION") {
Log.log(this.name + " received a request to send a " + payload.target + " notification");
this.sendNotification(payload.target, payload.payload);
return;
}
},
setModuleDisplay: function (module, action, brightness) {
const options = this.config.uselock ? { lockString: this.identifier } : "";
Log.log(this.name + " is processing the " + action + (action === "DIM_MODULE" ? " (" + brightness + "%)" : "") + " request for " + module.identifier);
if (action === "SHOW_MODULE") {
module["show"](
this.config.animationSpeed,
() => {
Log.log(this.name + " has shown " + module.identifier);
this.setModuleBrightness(module.identifier, 100);
},
options
);
return true;
}
if (action === "HIDE_MODULE") {
module.hide(this.config.animationSpeed, Log.log(this.name + " has hidden " + module.identifier), options);
return true;
}
if (action === "DIM_MODULE") {
this.setModuleBrightness(module.identifier, brightness);
return true;
}
return false;
},
setModuleBrightness(moduleIdentifier, brightness = 100) {
const moduleDiv = document.getElementById(moduleIdentifier);
if (moduleDiv) {
moduleDiv.style.filter = "brightness(" + brightness + "%)";
Log.log(this.name + " has set the brightness of " + moduleIdentifier + " to " + brightness + "%");
}
}
});