-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
80 lines (64 loc) · 2.17 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
var request = require("request");
var Service, Characteristic;
module.exports = function (homebridge) {
Service = homebridge.hap.Service;
Characteristic = homebridge.hap.Characteristic;
homebridge.registerAccessory("homebridge-mythtv", "MythTV", MythtTV);
}
function MythtTV(log, config) {
this.log = log;
this.name = config["name"] || 'MythtTV';
this.host = config["frontend"] || 'localhost';
this.port = config["port"] || '6547';
this.pollingInterval = config["polling_interval"] || 3;
this.debug = config["debug"] || false;
this.service = new Service.OccupancySensor(this.name);
this.playing = false;
this.service
.getCharacteristic(Characteristic.OccupancyDetected)
.on('get', this.getState.bind(this));
var self = this;
var callback = function (err, value) {
setTimeout(function () {
self.getState(callback);
}, self.pollingInterval * 1000);
if (err !== null)
return;
self.service
.getCharacteristic(Characteristic.OccupancyDetected)
.updateValue(value);
};
self.getState(callback);
}
MythtTV.prototype.getState = function (callback) {
var self = this;
request.get({
url: "http://" + self.host + ":" + self.port + "/Frontend/GetStatus",
headers: {
Accept: 'text/javascript'
}
}, function (err, response, body) {
if (err || response.statusCode !== 200) {
var statusCode = response ? response.statusCode : 1;
self.log("Error getting state (status code %s): %s", statusCode, err);
callback(err);
return;
}
var data = JSON.parse(body);
data = data.FrontendStatus.State;
var playing = false;
if (data.state === "idle") {
if (self.debug)
self.log('MythTV Frontend is idle.');
playing = false;
} else {
if (self.debug)
self.log('MythTV Frontend is %s.', data.state);
playing = true;
};
callback(null, playing);
});
}
MythtTV.prototype.getServices = function () {
return [this.service];
}