-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
executable file
·257 lines (214 loc) · 8.69 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
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
// Accessory for controlling Marantz AVR via HomeKit
var inherits = require('util').inherits;
var nodecec = require("node-cec");
var NodeCec = nodecec.NodeCec;
//var cec = new NodeCec( 'node-cec-monitor' );
var CEC = nodecec.CEC;
var Service, Characteristic;
module.exports = function(homebridge) {
Service = homebridge.hap.Service;
Characteristic = homebridge.hap.Characteristic;
homebridge.registerAccessory("homebridge-cec", "CEC", LibCEC);
function LibCEC(log, config) {
// configuration
this.name = config['name'];
this.timeout = config.timeout || 1000;
this.queue = [];
this.ready = true;
this.hdmiPort = 1;
this.log = log;
this.isOn = true; // TV is turned on at start
}
LibCEC.prototype = {
send: function(cmd, callback) {
this.sendCommand(cmd, callback);
//if (callback) callback();
},
exec: function() {
// Check if the queue has a reasonable size
if(this.queue.length > 5) this.queue.clear();
this.queue.push(arguments);
this.process();
},
sendCommand: function(command, callback) {
var that = this;
if(that.client) {
if(command == "power") {
if(that.isOn) {
that.client.sendCommand( 0xf0, CEC.Opcode.IMAGE_VIEW_ON );
}
else {
that.client.sendCommand( 0xf0, CEC.Opcode.STANDBY );
}
if(callback) callback();
}
else if (command == "hdmi") {
that.client.sendCommand( 0x1f, CEC.Opcode.ACTIVE_SOURCE, this.hdmiPort*16, 0x00);
if(callback) callback();
}
}
else {
if(callback) callback("no client");
}
},
process: function() {
if (this.queue.length === 0) return;
if (!this.ready) return;
var self = this;
this.ready = false;
this.send.apply(this, this.queue.shift());
setTimeout(function () {
self.ready = true;
self.process();
}, this.timeout);
},
getPowerState: function(callback) {
this.log("Power state is: ", this.isOn);
if(callback) callback(null, this.isOn);
},
setPowerState: function(powerOn, callback) {
var cmd = "power";
this.isOn = powerOn;
this.log('setPowerState call: ' + powerOn);
this.exec(cmd, function(error) {
if (error) {
this.log('setPowerState: ' + error);
if(callback) callback(error);
}
else {
this.log('setPowerState succeeded!');
if(callback) callback();
}
}.bind(this));
},
getHDMIPort: function(callback) {
// Not implemented yet
callback(null,this.hdmiPort);
},
setHDMIPort: function(port, callback) {
if(!this.isOn) { // Turn on if it was off
var cmd = "power";
this.isOn = true;
this.exec(cmd, function(response,error) {
if (error) {
this.log('setHDMIPort: ' + error);
}
else {
this.switchService.getCharacteristic(Characteristic.On).setValue(this.isOn);
this.log('setPowerState succeeded!');
}
}.bind(this));
}
var cmd = "hdmi";
this.hdmiPort = port;
this.exec(cmd, function(error) {
if (error) {
this.log('setHDMIPort failed: ' + error);
if(callback) callback(error);
}
else {
this.log('setHDMIPort succeeded!');
if(callback) callback();
}
}.bind(this));
},
getServices: function() {
var that = this;
this.cec = new NodeCec( 'node-cec-monitor' );
var informationService = new Service.AccessoryInformation();
informationService
.setCharacteristic(Characteristic.Name, this.name)
.setCharacteristic(Characteristic.Manufacturer, "Toshiba")
.setCharacteristic(Characteristic.Model, "homebridge")
.setCharacteristic(Characteristic.SerialNumber, "1244567890");
this.informationService = informationService; // Store to allow changing at runtime
this.switchService = new Service.Switch("Power State");
this.switchService
.getCharacteristic(Characteristic.On)
.on('get', this.getPowerState.bind(this))
.on('set', this.setPowerState.bind(this));
makeHDMICharacteristic();
this.switchService
.addCharacteristic(HDMICharacteristic)
.on('get', this.getHDMIPort.bind(this))
.on('set', this.setHDMIPort.bind(this));
// Get Vendor ID
this.cec.once( 'ready', function(client) {
this.log( ' -- GIVE_DEVICE_VENDOR_ID -- ' );
this.client = client; // Store the client for later usage
client.sendCommand( 0xf0, CEC.Opcode.GIVE_DEVICE_VENDOR_ID, 0x0F, 0x8C );
client.sendCommand( 0xf0, CEC.Opcode.IMAGE_VIEW_ON ); // This works (on)
this.cec.once( 'DEVICE_VENDOR_ID', function (packet, id, vendor) {
this.log('DEVICE_VENDOR_ID:' + packet + ", " + id + ", " + vendor);
this.informationService.getCharacteristic(Characteristic.Manufacturer).setValue(vendor);
}.bind(this));
}.bind(this));
this.cec.on( 'ROUTING_CHANGE', function(packet, fromSource, toSource) {
this.log( 'Routing changed from ' + fromSource + ' to ' + toSource + '.' );
if(toSource == 0) {
this.isOn = false;
this.hdmiPort = 0;
this.switchService.getCharacteristic(Characteristic.On).setValue(this.isOn);
this.switchService.getCharacteristic(HDMICharacteristic).setValue(this.hdmiPort);
}
else if( fromSource == 0 ){
this.isOn = true;
this.hdmiPort = toSource/4096;
this.switchService.getCharacteristic(Characteristic.On).setValue(this.isOn);
this.switchService.getCharacteristic(HDMICharacteristic).setValue(this.hdmiPort);
}
else{
this.hdmiPort = toSource/4096;
this.log("Switched to: " + this.hdmiPort + " port");
this.switchService.getCharacteristic(HDMICharacteristic).setValue(this.hdmiPort);
}
}.bind(this));
this.cec.on( 'STANDBY', function() {
this.log( 'STANDBY' );
if(this.isOn) this.switchService.getCharacteristic(Characteristic.On).setValue(false);
this.isOn = false;
}.bind(this));
// -------------------------------------------------------------------------- //
//- KILL CEC-CLIENT PROCESS ON EXIT
process.on( 'SIGINT', function() {
console.log("Killing CEC");
if ( this.cec != null ) {
this.cec.stop();
}
process.exit();
});
// -------------------------------------------------------------------------- //
//- KILL CEC-CLIENT PROCESS ON SEGFAULT
process.on( 'SIGSEGV', function() {
console.log("Killing CEC");
if ( this.cec != null ) {
this.cec.stop();
}
process.exit();
});
// -------------------------------------------------------------------------- //
//- START CEC CLIENT
// -m = start in monitor-mode
// -d8 = set log level to 8 (=TRAFFIC) (-d 8)
// -br = logical address set to `recording device`
this.log("Starting cec-client");
this.cec.start( 'cec-client', '-m', '-d', '8', '-b', 'r' );
return [informationService, this.switchService];
}
};
};
function makeHDMICharacteristic() {
HDMICharacteristic = function () {
Characteristic.call(this, 'HDMI', '212131F4-2E14-4FF4-AE13-C97C3232499D');
this.setProps({
format: Characteristic.Formats.INT,
unit: Characteristic.Units.NONE,
maxValue: 3,
minValue: 1,
minStep: 1,
perms: [Characteristic.Perms.READ, Characteristic.Perms.WRITE, Characteristic.Perms.NOTIFY]
});
this.value = this.getDefaultValue();
};
inherits(HDMICharacteristic, Characteristic);
}