/
index.js
232 lines (194 loc) · 6.45 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
var inherits = require('util').inherits;
var PanasonicViera = require('panasonic-viera-control/panasonicviera.js');
var http = require('http');
var Service, Characteristic, VolumeCharacteristic, ChannelCharacteristic;
module.exports = function(homebridge) {
Service = homebridge.hap.Service;
Characteristic = homebridge.hap.Characteristic;
// we can only do this after we receive the homebridge API object
makeVolumeCharacteristic();
makeChannelCharacteristic();
homebridge.registerAccessory("homebridge-panasonictv", "TV", PanasonicTV);
}
function PanasonicTV(log, config) {
this.log = log;
this.name = config.name;
this.HOST = config.ip;
this.maxVolume = config.maxVolume || 12;
this.service = new Service.Switch(this.name);
this.service
.getCharacteristic(Characteristic.On)
.on("set", this.setOn.bind(this))
.on("get", this.getOn.bind(this));
this.service
.addCharacteristic(VolumeCharacteristic)
.on('get', this.getVolume.bind(this))
.on('set', this.setVolume.bind(this));
this.service
.addCharacteristic(ChannelCharacteristic)
.on('get', this.getChannel.bind(this))
.on('set', this.setChannel.bind(this));
// Init the panasonic controller
this.tv = new PanasonicViera(this.HOST);
}
PanasonicTV.prototype.getServices = function() {
return [this.service];
}
PanasonicTV.prototype.getOn = function(callback) {
var self = this;
self.getOnCallback = callback;
this.getPowerState(this.HOST, function(state) {
self.getOnCallback(null,state == 1);
});
}
PanasonicTV.prototype.setOn = function(on, callback) {
var self = this;
self.setOnCallback = callback;
this.getPowerState(this.HOST, function(state) {
if (state == -1 && on) {
self.tv.send(PanasonicViera.POWER_TOGGLE);
self.setOnCallback(null, true);
}
else if (state == 0 && on) {
self.setOnCallback(new Error("The TV is *really* off and cannot be woken up."));
}
else if (state == 1 && !on) {
self.tv.send(PanasonicViera.POWER_TOGGLE);
self.setOnCallback(null, false);
}
else {
self.setOnCallback(new Error("Cannot fullfill " + (on ? "ON" : "OFF") + " request. Powerstate == " + state));
}
})
}
PanasonicTV.prototype.getVolume = function(callback) {
var self = this;
self.volumeCallback = callback;
this.getPowerState(this.HOST, function(state) {
if (state == 1) {
self.tv.getVolume(function (data) {
var translatedVolume = (data / self.maxVolume) * 100;
self.volumeCallback(null, translatedVolume);
});
}
else {
self.volumeCallback(null, 0);
}
});
}
PanasonicTV.prototype.setVolume = function(volume, callback) {
// Here we don't care about the TV's powerstate. If it's off, then all calls time out or error..
var translatedVolume = (volume / 100) * this.maxVolume;
this.tv.setVolume(translatedVolume);
callback();
}
PanasonicTV.prototype.getChannel = function(callback) {
callback(null, 0);
}
PanasonicTV.prototype.setChannel = function(channel, callback) {
this.tv.send("D" + channel);
callback();
}
// Returns:
// -1 when the TV is in standby-mode (a 400-Bad Request is returned by the TV)
// 0 when the TV is off, or it's a TV that does not support the standby wake-up request(the request errors)
// 1 when the TV is on (a normal 200 response is returned)
PanasonicTV.prototype.getPowerState = function(ipAddress, stateCallback) {
var path = "/dmr/control_0";
var body = '<?xml version="1.0" encoding="utf-8"?>\n' +
'<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/" s:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">\n' +
' <s:Body>\n' +
' <u:getVolume xmlns:u="urn:schemas-upnp-org:service:RenderingControl:1">\n' +
' <InstanceID>0</InstanceID><Channel>Master</Channel>\n' +
' </u:getVolume>\n' +
' </s:Body>\n' +
'</s:Envelope>\n';
var post_options = {
host: ipAddress,
port: '55000',
path: path,
method: 'POST',
timeout: 2000,
headers: {
'Content-Length': body.length,
'Content-Type': 'text/xml; charset="utf-8"',
'User-Agent': 'net.thlabs.nodecontrol',
'SOAPACTION': '"urn:schemas-upnp-org:service:RenderingControl:1#getVolume"'
}
}
// The request intermittently TIMES OUT, ERRORS, OR BOTH(!) when the TV is not
// available. Therefore we're maintaining state whether the callback is called
// since you're only allowed to call the Homekit-callback once.
var calledBack = false;
var req = http.request(post_options, function(res) {
res.setEncoding('utf8');
res.on('data', function(data) {
// do nothing here, but without attaching a 'data' event, the 'end' event is not called
});
res.on('end', function() {
if(res.statusCode == 200) {
if (!calledBack) {
stateCallback(1);
}
}
else {
if (!calledBack) {
stateCallback(-1);
}
}
});
});
req.on('error', function(e) {
console.log('errored');
console.log(e);
if (!calledBack) {
stateCallback(0);
calledBack = true;
}
else {
console.log ("already called callback");
}
});
req.on('timeout', function() {
console.log('timed out');
if (!calledBack) {
stateCallback(0);
calledBack = true;
}
else {
console.log ("already called callback");
}
});
req.write(body);
req.end();
}
function makeVolumeCharacteristic() {
VolumeCharacteristic = function() {
Characteristic.call(this, 'Volume', '91288267-5678-49B2-8D22-F57BE995AA93');
this.setProps({
format: Characteristic.Formats.INT,
unit: Characteristic.Units.PERCENTAGE,
maxValue: 100,
minValue: 0,
minStep: 1,
perms: [Characteristic.Perms.READ, Characteristic.Perms.WRITE, Characteristic.Perms.NOTIFY]
});
this.value = this.getDefaultValue();
};
inherits(VolumeCharacteristic, Characteristic);
}
function makeChannelCharacteristic() {
ChannelCharacteristic = function () {
Characteristic.call(this, 'Channel', '212131F4-2E14-4FF4-AE13-C97C3232499D');
this.setProps({
format: Characteristic.Formats.INT,
unit: Characteristic.Units.NONE,
maxValue: 100,
minValue: 0,
minStep: 1,
perms: [Characteristic.Perms.READ, Characteristic.Perms.WRITE, Characteristic.Perms.NOTIFY]
});
this.value = this.getDefaultValue();
};
inherits(ChannelCharacteristic, Characteristic);
}