forked from tgymnich/homebridge-vsx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
115 lines (97 loc) · 2.87 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
var Service, Characteristic;
var net = require('net');
module.exports = function (homebridge) {
Service = homebridge.hap.Service;
Characteristic = homebridge.hap.Characteristic;
homebridge.registerAccessory("homebridge-vsx", "VSX", VSX);
};
function VSX(log, config) {
this.log = log;
this.name = config.name;
this.HOST = config.ip;
this.PORT = config.port;
this.INPUT = config.input;
}
VSX.prototype.getServices = function () {
this.informationService = new Service.AccessoryInformation();
this.informationService.setCharacteristic(
Characteristic.Manufacturer, "Pioneer");
this.switchService = new Service.Switch(this.name);
this.switchService.getCharacteristic(Characteristic.On)
.on('set', this.setOn.bind(this))
.on('get', this.getOn.bind(this));
return [this.switchService, this.informationService];
};
VSX.prototype.getOn = function (callback) {
const me = this;
me.log('Query Power Status on '
+ me.HOST + ':' + me.PORT + " input " + me.INPUT);
var client = new net.Socket();
client.on('error', function (ex) {
me.log("Received an error while communicating" + ex);
callback(ex)
});
client.connect(me.PORT, me.HOST, function () {
client.write('?P\r\n');
});
client.on('data', function (data) {
me.log('Received data: ' + data);
var str = data.toString();
if (str.includes("PWR1")) {
me.log("Power is Off");
client.destroy();
callback(null, false);
} else if (str.includes("PWR0")) {
me.log("Power is On");
if (me.INPUT) {
client.write('?F\r\n'); // Request input
} else {
callback(null, true);
client.destroy();
}
} else if (str.includes("FN")) {
me.log("Current input is " + str);
client.destroy();
if (str.includes(me.INPUT)) {
me.log("Current input matches target input of " + me.INPUT);
callback(null, true);
} else {
me.log("Receiver has different input selected");
callback(null, false);
}
} else {
me.log("waiting");
}
});
};
VSX.prototype.setOn = function (on, callback) {
const me = this;
var client = new net.Socket();
client.on('error', function (ex) {
me.log("Received an error while communicating" + ex);
callback(ex)
});
if (on) {
client.connect(me.PORT, me.HOST, function () {
me.log('Set Power On on '
+ me.HOST + ':' + me.PORT + " input " + me.INPUT);
client.write('PO\r\n');
if (me.INPUT == null) {
client.destroy();
}
});
client.on('data', function (data) {
me.log("Change input to " + me.INPUT);
client.write(me.INPUT + 'FN\r\n');
client.destroy();
});
}
if (!on) {
client.connect(me.PORT, me.HOST, function () {
me.log('Set Power Off on ' + me.HOST + ':' + me.PORT);
client.write('PF\r\n');
client.destroy();
});
}
callback();
};