-
Notifications
You must be signed in to change notification settings - Fork 5
/
nodecec.js
119 lines (96 loc) · 2.96 KB
/
nodecec.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
var EventEmitter = require('events').EventEmitter;
var spawn = require('child_process').spawn;
var byline = require('byline');
// NodeCEC object
function NodeCEC(args) {
if(false === (this instanceof NodeCEC)) {
return new NodeCEC(args);
}
this.args = args;
EventEmitter.call(this);
}
// inherit from EventEmitter
NodeCEC.prototype.__proto__ = EventEmitter.prototype;
// ========================
// PUBLIC API
// ========================
// start NodeCEC server
NodeCEC.prototype.start = function() {
this.client = spawn('cec-client');
this.stdout = byline(this.client.stdout);
this.stderr = byline(this.client.stderr);
this.stdin = this.client.stdin;
var that = this;
this.stdout.on('data', function(data) {
that.handleData(data);
});
this.stderr.on('data', function(data) {
that.handleError(data);
});
this.client.on('close', function(code) {
that.emit('close', code);
that.ready = false;
that.client = null;
});
this.client.on('error', function(err) {
that.handleError(err);
});
}
// stop NodeCEC server
NodeCEC.prototype.stop = function(callback) {
// if client is null nothing to do
if (!this.client) {
callback(null);
}
// if we are ready to send then send quit command
if (this.ready) {
this.send('q');
callback(null);
} else {
callback('cec-client not ready. Could not terminate');
}
}
// send command
NodeCEC.prototype.send = function(command) {
this.stdin.write(command);
}
// ========================
// PUBLIC API END
// ========================
// handle data from stdout
NodeCEC.prototype.handleData = function(data) {
var line = data.toString();
//console.log(line);
if (line == 'waiting for input') {
// means cec-client is up and running
// set ready flag
this.ready = true;
this.emit('ready', this);
} else if (line.indexOf('power status changed') > -1) {
// power status changed
var id = line.match(/\((\w+)\)/)[1];
// save id for potential use later
this.id = id;
var statuses = line.match(/'(\w+)'/g);
var from = statuses[0];
var to = statuses[1];
// save status for later use
this.status = to;
// emit status update event
this.emit('status', { id: id, from: from, to: to });
} else if (line.indexOf('key released') > -1) {
// generic key press
var key = line.match(/key released:([\w\s]+)/)[1].trim();
var code = line.match(/\((\w+)\)/)[1];
this.emit('key', { code: code, name: key });
} else if (line.indexOf('play') > -1) {
this.emit('key', { code: 41, name: 'play' });
} else if (line.indexOf('deck control') > -1) {
this.emit('key', { code: 42, name: 'stop' });
}
}
// handles stderr data
NodeCEC.prototype.handleError = function(data) {
this.emit('error', data);
};
module.exports = NodeCEC;