-
Notifications
You must be signed in to change notification settings - Fork 41
/
Copy pathindex.js
221 lines (189 loc) · 6.5 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
var Service;
var Characteristic;
var request = require("request");
var pollingtoevent = require('polling-to-event');
var util = require('util');
module.exports = function(homebridge)
{
Service = homebridge.hap.Service;
Characteristic = homebridge.hap.Characteristic;
homebridge.registerAccessory("homebridge-onkyo-avr", "OnkyoAVR", HttpStatusAccessory);
}
/*
exports.init = function(log, config)
{
return new HttpStatusAccessory(log, config);
}*/
function HttpStatusAccessory(log, config)
{
this.log = log;
var that = this;
this.eiscp = require('eiscp');
this.setAttempt = 0;
// config
this.ip_address = config["ip_address"];
this.name = config["name"];
this.model = config["model"];
this.poll_status_interval = config["poll_status_interval"] || "0";
this.state = false;
this.interval = parseInt( this.poll_status_interval);
this.avrManufacturer = "Onkyo";
this.avrSerial = "unknown";
this.switchHandling = "check";
if (this.interval > 10 && this.interval < 100000) {
this.switchHandling = "poll";
}
this.eiscp.on('debug', this.eventDebug.bind(this));
this.eiscp.on('error', this.eventError.bind(this));
this.eiscp.on('connect', this.eventConnect.bind(this));
this.eiscp.on('connect', this.eventConnect.bind(this));
this.eiscp.on('system-power', this.eventSystemPower.bind(this));
this.eiscp.on('volume', this.eventVolume.bind(this));
this.eiscp.on('close', this.eventClose.bind(this));
this.eiscp.connect(
{host: this.ip_address, reconnect: true, model: this.model}
);
//that.log("hello - "+config["ip_address"]);
// Status Polling
if (this.switchHandling == "poll") {
var powerurl = this.status_url;
that.log("start long poller..");
var statusemitter = pollingtoevent(function(done) {
that.log("start polling..");
that.getPowerState( function( error, response) {
//pass also the setAttempt, to force a homekit update if needed
done(error, response, that.setAttempt);
}, "statuspoll");
}, {longpolling:true,interval:that.interval * 1000,longpollEventName:"statuspoll"});
statusemitter.on("statuspoll", function(data) {
that.state = data;
that.log("event - status poller - new state: ", that.state);
if (that.switchService ) {
that.switchService.getCharacteristic(Characteristic.On).setValue(that.state, null, "statuspoll");
}
});
}
}
HttpStatusAccessory.prototype = {
eventDebug: function( response)
{
//this.log( "eventDebug: %s", response);
},
eventError: function( response)
{
this.log( "eventError: %s", response);
},
eventConnect: function( response)
{
this.log( "eventConnect: %s", response);
},
eventSystemPower: function( response)
{
//this.log( "eventSystemPower: %s", response);
this.state = (response == "on");
this.log("eventSystemPower - message: %s, new state %s", response, this.state);
//Communicate status
if (this.switchService ) {
this.switchService.getCharacteristic(Characteristic.On).setValue(this.state, null, "statuspoll");
}
},
eventVolume: function( response)
{
//this.log( "eventVolume: %s", response);
},
eventClose: function( response)
{
this.log( "eventClose: %s", response);
},
setPowerState: function(powerOn, callback, context) {
var that = this;
//if context is statuspoll, then we need to ensure that we do not set the actual value
if (context && context == "statuspoll") {
this.log( "setPowerState - polling mode, ignore, state: %s", this.state);
callback(null, this.state);
return;
}
if (!this.ip_address) {
this.log.warn("Ignoring request; No ip_address defined.");
callback(new Error("No ip_address defined."));
return;
}
this.setAttempt = this.setAttempt+1;
//do the callback immediately, to free homekit
//have the event later on execute changes
that.state = powerOn;
callback( null, that.state);
if (powerOn) {
this.log("setPowerState - actual mode, power state: %s, switching to ON", that.state);
this.eiscp.command("system-power=on", function(error, response) {
//that.log( "PWR ON: %s - %s -- current state: %s", error, response, that.state);
if (error) {
that.state = false;
that.log( "setPowerState - PWR ON: ERROR - current state: %s", that.state);
if (that.switchService ) {
that.switchService.getCharacteristic(Characteristic.On).setValue(powerOn, null, "statuspoll");
}
}
}.bind(this) );
} else {
this.log("setPowerState - actual mode, power state: %s, switching to OFF", that.state);
this.eiscp.command("system-power=standby", function(error, response) {
//that.log( "PWR OFF: %s - %s -- current state: %s", error, response, that.state);
if (error) {
that.state = false;
that.log( "setPowerState - PWR OFF: ERROR - current state: %s", that.state);
if (that.switchService ) {
that.switchService.getCharacteristic(Characteristic.On).setValue(powerOn, null, "statuspoll");
}
}
}.bind(this) );
}
},
getPowerState: function(callback, context) {
var that = this;
//if context is statuspoll, then we need to request the actual value
if (!context || context != "statuspoll") {
if (this.switchHandling == "poll") {
this.log("getPowerState - polling mode, return state: ", this.state);
callback(null, this.state);
return;
}
}
if (!this.ip_address) {
this.log.warn("Ignoring request; No ip_address defined.");
callback(new Error("No ip_address defined."));
return;
}
//do the callback immediately, to free homekit
//have the event later on execute changes
callback(null, this.state);
this.log("getPowerState - actual mode, return state: ", this.state);
this.eiscp.command("system-power=query", function( error, data) {
if (error) {
that.state = false;
that.log( "getPowerState - PWR QRY: ERROR - current state: %s", that.state);
if (that.switchService ) {
that.switchService.getCharacteristic(Characteristic.On).setValue(powerOn, null, "statuspoll");
}
}
}.bind(this) );
},
identify: function(callback) {
this.log("Identify requested!");
callback(); // success
},
getServices: function() {
var that = this;
var informationService = new Service.AccessoryInformation();
informationService
.setCharacteristic(Characteristic.Manufacturer, this.avrManufacturer)
.setCharacteristic(Characteristic.Model, this.model)
.setCharacteristic(Characteristic.SerialNumber, this.avrSerial);
this.switchService = new Service.Switch(this.name);
this.switchService
.getCharacteristic(Characteristic.On)
.on('get', this.getPowerState.bind(this))
.on('set', this.setPowerState.bind(this));
return [informationService, this.switchService];
}
};