-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
38 lines (33 loc) · 1.07 KB
/
index.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
/* Copyright 2017 Paul Brewer, Economic and Financial Technology Consulting LLC <drpaulbrewer@eaftc.com>
* This file is open source software. The MIT License applies to this software.
*/
/* jshint esnext:true,eqeqeq:true,undef:true,lastsemic:true,strict:true,unused:true,node:true */
const acpiSocketFile = '/var/run/acpid.socket';
const net = require('net');
const EventEmitter = require('events');
const powerEventEmitter = new EventEmitter();
let acpiSocket;
let connected = 0;
function cleanup(){
"use strict";
powerEventEmitter.removeAllListeners();
acpiSocket.destroy();
connected = 0;
}
function connect(){
"use strict";
connected = 1;
acpiSocket = net.connect(acpiSocketFile);
acpiSocket.on('data', function(buf){
const data = buf.toString().toLowerCase();
if (data.includes("power") && data.includes("button")){
powerEventEmitter.emit('powerbutton');
cleanup();
}
});
}
module.exports = function beforePowerOff(func){
"use strict";
powerEventEmitter.once('powerbutton', func);
if (!connected) connect();
};