forked from merdok/homebridge-webos-tv
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
183 lines (159 loc) · 4.8 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
var lgtv, Service, Characteristic;
var wol = require('wake_on_lan');
module.exports = function(homebridge) {
Service = homebridge.hap.Service;
Characteristic = homebridge.hap.Characteristic;
homebridge.registerAccessory('homebridge-webos3', 'webos3', webos3Accessory);
}
function webos3Accessory(log, config, api) {
this.log = log;
this.ip = config['ip'];
this.name = config['name'];
this.mac = config['mac'];
this.url = 'ws://' + this.ip + ':3000';
this.connected = false;
this.checkCount = 0;
lgtv = require('lgtv2')({
url: this.url,
timeout: 5000,
reconnect: 3000,
keyFile: '/tmp/webos_key_'
});
var self = this;
lgtv.on('connect', function() {
self.log('webOS3 connected to TV');
self.connected = true;
});
lgtv.on('close', function() {
self.log('webOS3 disconnected from TV');
self.connected = false;
});
lgtv.on('error', function(error) {
self.log('webOS3 error %s', error);
self.connected = false;
//setTimeout(lgtv.connect(this.url), 5000);
});
lgtv.on('prompt', function() {
self.log('webOS3 prompt for confirmation');
self.connected = false;
});
lgtv.on('connecting', function() {
self.log('webOS3 connecting to TV');
self.connected = false;
});
this.service = new Service.Switch(this.name, "powerService");
this.volumeService = new Service.Lightbulb(this.name, "volumeService");
this.service
.getCharacteristic(Characteristic.On)
.on('get', this.getState.bind(this))
.on('set', this.setState.bind(this));
this.volumeService
.getCharacteristic(Characteristic.On)
.on('get', this.getMuteState.bind(this))
.on('set', this.setMuteState.bind(this));
this.volumeService
.addCharacteristic(new Characteristic.Brightness())
.on('get', this.getVolume.bind(this))
.on('set', this.setVolume.bind(this));
}
webos3Accessory.prototype.getState = function(callback) {
this.log('webOS3 TV state: %s', this.connected ? "On" : "Off");
return callback(null, this.connected);
}
webos3Accessory.prototype.checkWakeOnLan = function(callback) {
if (this.connected) {
this.checkCount = 0;
return callback(null, true);
} else {
if (this.checkCount < 3) {
this.checkCount++;
lgtv.connect(this.url);
setTimeout(this.checkWakeOnLan.bind(this, callback), 5000);
} else {
return callback(new Error('webOS3 wake timeout'));
this.checkCount = 0;
}
}
}
webos3Accessory.prototype.setState = function(state, callback) {
if (state) {
if (!this.connected) {
var self = this;
wol.wake(this.mac, function(error) {
if (error) return callback(new Error('webOS3 wake on lan error'));
this.checkCount = 0;
setTimeout(self.checkWakeOnLan.bind(self, callback), 5000);
})
} else {
return callback(null, true);
}
} else {
if (this.connected) {
var self = this;
lgtv.request('ssap://system/turnOff', function(err, res) {
if (err) return callback(null, false);
lgtv.disconnect();
self.connected = false ;
return callback(null, true);
})
} else {
return callback(new Error('webOS3 is not connected'))
}
}
}
webos3Accessory.prototype.getMuteState = function(callback) {
var self = this;
if (self.connected) {
lgtv.request('ssap://audio/getStatus', function (err, res) {
if (!res || err){
self.connected = false ;
lgtv.disconnect();
return callback(null, false);
}
self.log('webOS3 TV muted: %s', res.mute ? "Yes" : "No");
callback(null, !res.mute);
});
}else{
callback(null, false);
}
}
webos3Accessory.prototype.setMuteState = function(state, callback) {
var self = this;
if (self.connected) {
lgtv.request('ssap://audio/setMute', {mute: !state});
return callback(null, true);
}else {
return callback(new Error('webOS3 is not connected'))
}
}
webos3Accessory.prototype.getVolume = function(callback) {
var self = this;
if (self.connected) {
lgtv.request('ssap://audio/getVolume', function (err, res) {
if (!res || err){
self.connected = false ;
lgtv.disconnect();
return callback(null, false);
}
self.log('webOS3 TV volume: ' + res.volume);
callback(null, parseInt(res.volume));
});
}else{
callback(null, false);
}
}
webos3Accessory.prototype.setVolume = function(level, callback) {
var self = this;
if (self.connected) {
lgtv.request('ssap://audio/setVolume', {volume: level});
return callback(null, level);
}else {
return callback(new Error('webOS3 is not connected'))
}
}
webos3Accessory.prototype.getServices = function() {
return [
this.service,
this.volumeService
]
}