-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathindex.js
executable file
·157 lines (138 loc) · 5.29 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
var WebSocket = require('ws');
var wol = require('wake_on_lan');
var inherits = require('util').inherits;
var Service, Characteristic;
var request = require('request');
module.exports = function(homebridge) {
Service = homebridge.hap.Service;
Characteristic = homebridge.hap.Characteristic;
homebridge.registerAccessory("homebridge-samsungtv", "SamsungTV2016", SamsungTv2016Accessory);
};
//
// SamsungTV2016 Accessory
//
function SamsungTv2016Accessory(log, config) {
var accessory = this;
this.log = log;
this.config = config;
this.name = config["name"];
this.mac_address = config["mac_address"];
this.ip_address = config["ip_address"];
this.api_timeout = config["api_timeout"] || 2000;
if (!this.ip_address) throw new Error("You must provide a config value for 'ip_address'.");
if (!this.mac_address) throw new Error("You must provide a config value for 'mac_address'.");
this.app_name_base64 = (new Buffer(config["app_name"] || "homebridge")).toString('base64');
this.is_powering_off = false;
this.wake = function(done) {
wol.wake(this.mac_address, function(error) {
if (error) { done(1); }
else { done(0); }
});
};
this.sendKey = function(key, done) {
var ws = new WebSocket('http://' + this.ip_address + ':8001/api/v2/channels/samsung.remote.control?name=' + this.app_name_base64, function(error) {
done(new Error(error));
});
ws.on('error', function (e) {
accessory.log('Error in sendKey WebSocket communication');
done(e);
});
ws.on('message', function(data, flags) {
var cmd = {"method":"ms.remote.control","params":{"Cmd":"Click","DataOfCmd":key,"Option":"false","TypeOfRemote":"SendRemoteKey"}};
data = JSON.parse(data);
if(data.event == "ms.channel.connect") {
accessory.log('websocket connect');
ws.send(JSON.stringify(cmd));
setTimeout(function() {ws.close(); accessory.log('websocket closed');}, 1000);
done(0);
}
});
};
this.service = new Service.Switch(this.name);
this.is_api_active = function(done) {
request.get({ url: 'http://' + this.ip_address + ':8001/api/v2/', timeout: this.api_timeout}, function(err, res, body) {
if(!err && res.statusCode === 200) {
accessory.log('TV API is active');
done(true);
} else {
accessory.log('No response from TV');
done(false);
}
});
};
this.service
.getCharacteristic(Characteristic.On)
.on('get', this._getOn.bind(this))
.on('set', this._setOn.bind(this));
}
SamsungTv2016Accessory.prototype.getInformationService = function() {
var informationService = new Service.AccessoryInformation();
informationService
.setCharacteristic(Characteristic.Name, this.name)
.setCharacteristic(Characteristic.Manufacturer, 'Samsung TV')
.setCharacteristic(Characteristic.Model, '1.0.0')
.setCharacteristic(Characteristic.SerialNumber, this.ip_address);
return informationService;
};
SamsungTv2016Accessory.prototype.getServices = function() {
return [this.service, this.getInformationService()];
};
SamsungTv2016Accessory.prototype._getOn = function(callback) {
var accessory = this;
if(accessory.is_powering_off) {
accessory.log('power off in progress, reporting status as off.');
callback(null, false);
} else {
// if we can access the info API, then assume the TV is on
// there is a short period of time after the TV is turned off where the API is still active
// so this isn't bulletproof.
this.is_api_active(function(active) {
callback(null, active);
});
}
};
SamsungTv2016Accessory.prototype._setOn = function(on, callback) {
var accessory = this;
accessory.log('received on command: ' + on);
if (on) {
accessory.is_api_active(function(alive) {
if(alive) {
accessory.log('sending power key');
accessory.sendKey('KEY_POWER', function(err) {
if (err) {
callback(new Error(err));
} else {
// command has been successfully transmitted to your tv
accessory.log('successfully powered on tv');
accessory.is_powering_off = false;
callback(null);
}
});
} else {
accessory.log('attempting wake');
accessory.wake(function(err) {
if (err) {
callback(new Error(err));
} else {
// command has been successfully transmitted to your tv
accessory.log('wake request sent successfully');
callback(null);
}
});
}
});
} else {
accessory.log('sending power key');
accessory.sendKey('KEY_POWER', function(err) {
if (err) {
callback(new Error(err));
} else {
// command has been successfully transmitted to your tv
accessory.log('successfully powered off tv');
accessory.is_powering_off = true;
setTimeout(function() { accessory.is_powering_off = false;}, 15000)
callback(null);
}
});
}
};