-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
188 lines (157 loc) · 5.99 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
var net = require("net");
var Service, Characteristic;
/* Register the plugin with homebridge */
module.exports = function(homebridge) {
Service = homebridge.hap.Service;
Characteristic = homebridge.hap.Characteristic;
homebridge.registerPlatform("homebridge-globalcache-gc100", "GC100Platform", GC100Platform);
}
function GC100Platform(log, config) {
this.log = log;
this.name = config["name"];
this.ir_devices = config["ir_devices"];
this.rs232_devices = config["rs232_devices"];
// Device connection settings
this.host = config["host"];
this.ir_port = (config["ir_port"] ? config["ir_port"] : 4998);
// RS232 ports are device specific so they are part of the device config
}
GC100Platform.prototype.accessories = function(callback){
var results = [];
if(Array.isArray(this.ir_devices)){
/* IR Devices from the config file */
for(var i = 0; i < this.ir_devices.length; i++){
results.push(new GC100Accessory('ir', this.ir_devices[i], this, this.ir_port));
}
}
if(Array.isArray(this.rs232_devices)){
/* RS232 Devices from the config file */
for(var i = 0; i < this.rs232_devices.length; i++){
results.push(new GC100Accessory('rs232', this.rs232_devices[i], this, this.rs232_devices[i].port));
}
}
if(results.length == 0){
this.log("WARNING: No Accessories were loaded.");
}
callback(results);
}
/* Global Cache Accessories
IR
RS232
DC Trigger (not implemented yet)
*/
function GC100Accessory(type, dconfig, platform, port){
this.name = dconfig.name;
this.platform = platform;
this.port = port;
if(type == 'ir'){
this.executeCommand = this.executeIRCommand;
}else if(type == 'rs232'){
this.executeCommand = this.executeRS232Command;
}
this.services = []; // will hold the services this acessory supports
this.commands = dconfig.commands || {};
this.success_messages = dconfig.success_messages || {}; // When a command is sent to the GC100, it will respond with something. If that something matches what's in the corresponding success_messages object, then the command was sucessful
// If the commands & success_messages are base64 encoded, decode them
if(dconfig.base64_encoded) this.base64Decode();
// If both commands for on & off are defined in the device config, implement the switch service
if(this.commands.on && this.commands.off){
// Make accessories act like a switch that supports On/Off
var switchService = new Service.Switch(this.name);
// Wire the get & set
switchService
.getCharacteristic(Characteristic.On)
// .on('get', this.getState.bind(this));
switchService
.getCharacteristic(Characteristic.On)
.on('set', this.setState.bind(this))
this.services.push(switchService);
}
if(this.services.length == 0){
this.platform.log("WARNING: no services determined for accessory `"+this.name+"`. Make sure valid commands are defined in ~/homebridge/config.json");
}
}
GC100Accessory.prototype.getServices = function() {
return this.services;
}
GC100Accessory.prototype.setState = function(state, callback) {
if(state){
this.platform.log("Set `"+this.name+"` state to `on`");
this.executeCommand(this.commands.on, this.success_messages.on, callback);
}else{
this.platform.log("Set `"+this.name+"` state to `off`");
this.executeCommand(this.commands.off, this.success_messages.off, callback);
}
}
/* Not really possible to determine state with IR devices
Returning null, Unknown causes siri to respond that it couldn't contact the device
Removing the getState all together causes siri to respond with whatever the last
command was (which could be out of sync if someone controls the IR device with a
real IR remote)
*/
/*GC100Accessory.prototype.getState = function(callback){
callback(null, "Unknown");
this.platform.log("IR Device state is `Unknown`");
}*/
GC100Accessory.prototype.executeIRCommand = function(command, success_message, callback){
sock = new net.Socket(); // A socket to communicate to the GC100 with
sock.log = this.platform.log; // Make it possible to use the platform log from the net.Socket instance
sock.connect(this.port, this.platform.host, function(){
this.log('CONNECTED TO: ' + this.localAddress + ':' + this.localPort);
// Send the IR command to the GC100
this.write(command+"\r");
}).on('data', function(data) {
// log the response from the GC100
this.log('DATA: ' + data);
if(data.toString().trim() == success_message){
this.log("IR Command Accepted");
callback(null);// success
}else{
this.log("IR Command Failed: "+data);
callback(new Error("Error setting state."));
}
// Close the connection
this.destroy();
});
}
/* RS232 Command Execution */
GC100Accessory.prototype.executeRS232Command = function(command, success_message, callback){
sock = new net.Socket(); // A socket to communicate to the GC100 with
sock.log = this.platform.log; // Make it possible to use the platform log from the net.Socket instance
command = new Buffer(command,'binary');
sock.connect(this.port, this.platform.host, function(){
this.log('CONNECTED TO: ' + this.localAddress + ':' + this.localPort);
this.write(command);
}).on('data', function(data) {
// log the response from the GC100
this.log('DATA: ' + data);
if(data.toString().trim() == success_message){
this.log("RS232 Command Accepted");
callback(null);// success
}else{
this.log("RS232 Command Failed: "+data);
callback(new Error("Error setting state."));
}
// Close the connection
this.destroy();
});
}
/*
This method is a workaround for:
https://github.com/nfarina/homebridge/issues/441
Any devices specified in config.json that have base64_encoded = true
will be decoded with this method.
Use base64_encoder.js to create encoded commands
*/
GC100Accessory.prototype.base64Decode = function(){
var decoded = {};
for(var i in this.commands){
decoded[i] = new Buffer(this.commands[i], 'base64').toString('ascii');
}
this.commands = decoded;
var decoded = {};
for(var i in this.success_messages){
decoded[i] = new Buffer(this.success_messages[i], 'base64').toString('ascii');
}
this.success_messages = decoded;
}