-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathindex.js
302 lines (266 loc) · 8.44 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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
var Service;
var Characteristic;
var request = require("request");
var pollingtoevent = require('polling-to-event');
var wol = require('wake_on_lan');
module.exports = function(homebridge)
{
Service = homebridge.hap.Service;
Characteristic = homebridge.hap.Characteristic;
homebridge.registerAccessory("homebridge-winpc", "WinPC", HttpStatusAccessory);
}
function HttpStatusAccessory(log, config)
{
this.log = log;
var that = this;
this.setAttempt = 0;
// url info
this.on_url = config["on_url"];
this.on_body = config["on_body"];
this.off_url = config["off_url"];
this.off_body = config["off_body"];
this.status_url = config["status_url"];
this.http_method = config["http_method"] || "GET";;
this.username = config["username"] || "";
this.password = config["password"] || "";
this.sendimmediately = config["sendimmediately"] || "";
this.name = config["name"];
this.poll_status_interval = config["poll_status_interval"];
this.interval = parseInt( this.poll_status_interval);
this.powerstateOnError = config["powerstateOnError"];
this.powerstateOnConnect = config["powerstateOnConnect"];
this.info = {
serialnumber : "Unknown",
model: "Windows PC",
manufacterer : "Microsoft",
name : "Windows PC",
softwareversion : "Unknown"
};
this.switchHandling = "check";
if (this.status_url && this.interval > 10 && this.interval < 100000) {
this.switchHandling = "poll";
}
this.state = false;
// Status Polling
if (this.switchHandling == "poll") {
var powerurl = this.status_url;
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");
}
});
}
}
function parse( url) {
var address = {};
var s = url.replace(/^WOL[:]?[\/]?[\/]?(.*)[\?]ip=(.*)|^WOL[:]?[\/]?[\/]?(.*)/ig, function( str, p1, p2, p3) {
if (p1) {
address.mac = p1;
address.ip = p2;
}
if (p3) {
address.mac = p3;
}
});
return address;
}
HttpStatusAccessory.prototype = {
httpRequest: function(url, body, method, username, password, sendimmediately, callback) {
if (url.substring( 0, 3).toUpperCase() == "WOL") {
//Wake on lan request
var address = parse( url);
var opts={};
var macAddress = address.mac;
if (address.ip) {
opts.address = address.ip;
}
this.log("Excuting WakeOnLan request to "+macAddress+" options: "+JSON.stringify( opts));
wol.wake(macAddress, opts, function(error) {
if (error) {
callback( error);
} else {
callback( null, 200, "OK");
}
});
} else {
request({
url: url,
body: body,
method: method,
rejectUnauthorized: false,
timeout: 3000,
auth: {
user: username,
pass: password,
sendImmediately: sendimmediately
}
},
function(error, response, body) {
callback(error, response, body)
});
}
},
setPowerState: function(powerState, callback, context) {
var url;
var body;
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, powerState);
return;
}
if (!this.on_url || !this.off_url) {
this.log.warn("Ignoring request; No power url defined.");
callback(new Error("No power url defined."));
return;
}
this.setAttempt = this.setAttempt+1;
if (powerState) {
url = this.on_url;
body = this.on_body;
this.log("setPowerState - setting power state to on");
} else {
url = this.off_url;
body = this.off_body;
this.log("setPowerState - setting power state to off");
}
this.httpRequest(url, body, this.http_method, this.username, this.password, this.sendimmediately, function(error, response, responseBody) {
if (error) {
that.log('setPowerState - actual mode - failed: %s', error.message);
powerState = false;
that.state = powerState;
that.log("setPowerState - actual mode - current state: %s", that.state);
if (that.switchService ) {
that.switchService.getCharacteristic(Characteristic.On).setValue(powerState, null, "statuspoll");
}
callback(null, powerState);
} else {
that.state = powerState;
that.log("setPowerState - actual mode - current state: %s", that.state);
callback(null, powerState);
}
}.bind(this));
},
getPowerState: function(callback, context) {
//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.status_url) {
this.log.warn("Ignoring request; No status url defined.");
callback(new Error("No status url defined."));
return;
}
var url = this.status_url;
this.log("getPowerState - actual mode");
var that = this;
this.httpRequest(url, "", "GET", this.username, this.password, this.sendimmediately, function(error, response, responseBody) {
var tResp = responseBody;
var tError = error;
if (tError) {
if (that.powerstateOnError) {
tResp = that.powerstateOnError;
tError = null;
}
} else {
if (that.powerstateOnConnect) {
tResp = that.powerstateOnConnect;
tError = null;
}
}
if (tError) {
that.log('getPowerState - actual mode - failed: %s', error.message);
var powerState = false;
that.log("getPowerState - actual mode - current state: %s", powerState);
that.state = powerState;
callback(null, powerState);
} else {
var binaryState = parseInt(tResp);
var powerState = binaryState > 0;
that.log("getPowerState - actual mode - current state: %s", powerState);
that.state = powerState;
callback(null, powerState);
}
}.bind(this));
},
identify: function(callback) {
this.log("Identify requested!");
callback(); // success
},
processInformation: function( info, informationService, firstTime)
{
if (!info)
return;
var equal = true;
var deviceManufacturer = info.manufacturer || "Microsoft";
if (deviceManufacturer != this.info.manufacturer) {
equal = false;
this.info.manufacturer = deviceManufacturer;
}
var deviceModel = info.model || "Not provided";
if (deviceModel == "Not provided" && info.model_encrypted) {
deviceModel = "encrypted";
}
if (deviceModel != this.info.model) {
equal = false;
this.info.model = deviceModel;
}
var deviceSerialnumber = info.serialnumber || "Not provided";
if (deviceSerialnumber == "Not provided" && info.serialnumber_encrypted) {
deviceSerialnumber = "encrypted";
}
if (deviceSerialnumber != this.info.serialnumber) {
equal = false;
this.info.serialnumber = deviceSerialnumber;
}
var deviceName = info.name || "Not provided";
if (deviceName != this.info.name) {
equal = false;
this.info.name = deviceName;
}
var deviceSoftwareversion = info.softwareversion || "Not provided";
if (deviceSoftwareversion == "Not provided" && info.softwareversion_encrypted) {
deviceSoftwareversion = "encrypted";
}
if (deviceSoftwareversion != this.info.softwareversion) {
equal = false;
this.info.softwareversion = deviceSoftwareversion;
}
if( !equal || firstTime) {
if (informationService) {
this.log('Setting info: '+ JSON.stringify( this.info));
informationService
.setCharacteristic(Characteristic.Manufacturer, deviceManufacturer)
.setCharacteristic(Characteristic.Model, deviceModel)
.setCharacteristic(Characteristic.SerialNumber, deviceSerialnumber)
.setCharacteristic(Characteristic.Name, deviceName)
.setCharacteristic(Characteristic.SoftwareRevision, deviceSoftwareversion );
}
}
},
getServices: function() {
var that = this;
var informationService = new Service.AccessoryInformation();
this.processInformation( this.info, informationService, true);
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];
}
};