-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMMM-SleepWake.js
executable file
·130 lines (126 loc) · 3.63 KB
/
MMM-SleepWake.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
9
Module.register("MMM-SleepWake",{
previously_hidden: [],
defaults: {
delay: 15,
source: 'external',
mode: "hide",
ndetectionDir: "/motion",
ndetectionFile: "detected",
pi_off: "/opt/vc/bin/tvservice -o",
pi_on: "/opt/vc/bin/tvservice -p && sudo chvt 6 && sudo chvt 7",
dpms_off: "xset dpms force off",
dpms_on: "xset dpms force on",
cec_on:"echo on 0 | cec-client -s",
cec_off:"echo standby 0 | cec-client -s",
debug: true
},
socketNotificationReceived: function(notification, payload)
{
let self = this;
Log.log("sleep/wake in socketNotificationReceived");
Log.log("notification='"+notification+"'");
switch(notification)
{
case "SLEEP_HIDE":
MM.getModules().enumerate((module) => {
// if the module is already hidden
if(module.hidden==true)
// save it for wake up
{self.previously_hidden.push(module.identifier);}
else
// hide this module
{module.hide(1000);}
});
break;
case "SLEEP_WAKE":
MM.getModules().enumerate((module) => {
// if this module was NOT in the previously hidden list
//Log.log("looking for module ="+module.name+" in previous list");
if(self.previously_hidden.indexOf(module.identifier)==-1)
{
// show it
module.show(1000);
}
});
// clear the list, if any
self.previously_hidden = [];
break;
}
},
PIR_Loaded: function(){
let rc=false;
MM.getModules().enumerate((module) => {
if(module.name.startsWith("MMM-PIR-Sensor")){
rc=true;
}
});
return rc;
},
notificationReceived: function(notification, payload, sender){
let self = this;
//Log.log("sleep-wake in notificationReceived");
//Log.log("notification='"+notification+"'");
//Log.log("sender="+sender);
//Log.log("payload="+JSON.stringify(payload));
switch(notification){
case "ALL_MODULES_STARTED":
//self=this;
if(self.config.mode.toUpperCase()!=="PIR" || !this.PIR_Loaded())
{
//Log.log("SleepWake config="+JSON.stringify(self.config));
self.sendSocketNotification("config", self.config);
}
else
{Log.log("MMM-PIR-Sensor loaded, defering");}
break;
case "STAND_BY":
Log.log("received notification about sleep from "+ sender.name);
if(sender.name=="MMM-voice" || sender.name=="MMM-PIR-Sensor" || sender.name=="MMM-PIR-Sensor-Lite" )
{self.sendSocketNotification(notification,payload);}
Log.log("config="+self.config.mode.toUpperCase());
if(self.config.mode.toUpperCase()==="HIDE" && payload.status === true){
if( sender.name == "MMM-AssistantMk2" && payload.triggerHide === true){
//
Log.log("MMM-AssistantMk2 says go to sleep");
this.sendSocketNotification("START_SLEEP", null);
}
else {
Log.log("previously hidden module identifiers="+payload.modules);
// loop thru the modules
MM.getModules().enumerate((module) => {
// if this module should be in the previously hidden list
if(payload.modules.indexOf(module.identifier) !== -1)
{
// save it
self.previously_hidden.push(module.identifier);
}
});
}
}
break;
case 'USER_PRESENCE':
if(sender.name == 'MMM-PIR-Sensor' || sender.name=="MMM-PIR-Sensor-Lite"){
if(payload == true){
Log.log("received notice user around")
self.sendSocketNotification("END_SLEEP");
}
else{
Log.log("received notice user no longer around")
self.sendSocketNotification("START_SLEEP");
}
}
break;
case 'MONITOR_ACTION':
if(sender.name =='MMM-AlexaControl'){
if(payload == 'SLEEP_WAKE'){
self.sendSocketNotification("END_SLEEP");
}
else{
self.sendSocketNotification("START_SLEEP");
}
}
break;
}
},
});