forked from x2c/homebridge-hdmi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
executable file
·124 lines (102 loc) · 3.57 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
// Accessory for HDMI CEC connected devices
var Service, Characteristic;
var NodeCEC = require('nodecec');
var deviceModule = require('./lib/devices');
var cec = new NodeCEC();
var devices = deviceModule.devices;
CECInit();
module.exports = function(homebridge) {
Service = homebridge.hap.Service;
Characteristic = homebridge.hap.Characteristic;
homebridge.registerAccessory("homebridge-hdmi", "TV", TvAccessory);
homebridge.registerAccessory("homebridge-hdmi", "AMP", AmpAccessory);
};
function TvAccessory(log, config) {
this.service = new Service.Switch(config["name"]);
this.service
.getCharacteristic(Characteristic.On)
.on('get', this.getOn.bind(this))
.on('set', this.setOn.bind(this));
}
TvAccessory.prototype.getOn = function(callback) {
var state = (devices[0].status == 1 ? true : false);
callback(null, state);
}
TvAccessory.prototype.setOn = function(on, callback) {
if (devices[0].status == 0 && on) {
console.log('Turning TV on');
cec.send('on 0');
devices[0].status = 1;
}
if (devices[0].status == 1 && !on) {
console.log('Turning TV off');
cec.send('standby 0');
devices[0].status = 0;
}
callback(null);
}
TvAccessory.prototype.getInformationService = function() {
var informationService = new Service.AccessoryInformation();
informationService
.setCharacteristic(Characteristic.Name, 'HDMI TV')
.setCharacteristic(Characteristic.Manufacturer, 'Unknown')
.setCharacteristic(Characteristic.Model, '1.0.0')
.setCharacteristic(Characteristic.SerialNumber, '123456789');
return informationService;
};
TvAccessory.prototype.getServices = function() {
return [this.service, this.getInformationService()];
};
function AmpAccessory(log, config) {
this.service = new Service.Switch(config["name"]);
this.service
.getCharacteristic(Characteristic.On)
.on('get', this.getOn.bind(this))
.on('set', this.setOn.bind(this));
}
AmpAccessory.prototype.getOn = function(callback) {
var state = (devices[5].status == 1 ? true : false);
callback(null, state);
}
AmpAccessory.prototype.setOn = function(on, callback) {
if (devices[5].status == 0 && on) {
console.log('Turning AMP on');
cec.send('on 5');
devices[5].status = 1;
}
if (devices[5].status == 1 && !on) {
console.log('Turning AMP off');
cec.send('standby 5');
devices[5].status = 0;
}
callback(null);
}
AmpAccessory.prototype.getInformationService = function() {
var informationService = new Service.AccessoryInformation();
informationService
.setCharacteristic(Characteristic.Name, 'HDMI AMP')
.setCharacteristic(Characteristic.Manufacturer, 'Unknown')
.setCharacteristic(Characteristic.Model, '1.0.0')
.setCharacteristic(Characteristic.SerialNumber, '123456789');
return informationService;
};
AmpAccessory.prototype.getServices = function() {
return [this.service, this.getInformationService()];
};
function CECInit() {
cec.start();
cec.on('ready', function(data) {
console.log("HDMI ready...");
cec.send('scan');
});
cec.on('status', function(data) {
devices[data.id].status = ((data.to == '\'on\'' || typeof data.to == "undefined") ? 1 : 0);
console.log("Device [" + devices[data.id].name + "] changed from " + data.from + " to " + data.to);
});
cec.on('key', function(data) {
console.log(data.name);
});
cec.on('close', function(code) {
process.exit(0);
});
}