forked from rudders/homebridge-http
-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
154 lines (128 loc) · 4.75 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
var Service, Characteristic;
var request = require("request");
module.exports = function(homebridge){
Service = homebridge.hap.Service;
Characteristic = homebridge.hap.Characteristic;
homebridge.registerAccessory("homebridge-readablehttp", "Http", HttpAccessory);
}
function HttpAccessory(log, config) {
this.log = log;
// 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.read_url = config["read_url"];
this.brightness_url = config["brightness_url"];
this.http_method = config["http_method"];
this.http_brightness_method = config["http_brightness_method"] || this.http_method;
this.username = config["username"];
this.password = config["password"];
this.sendimmediately = config["sendimmediately"];
this.service = config["service"] || "Switch";
this.name = config["name"];
this.brightnessHandling = config["brightnessHandling"] || "no";
}
HttpAccessory.prototype = {
httpRequest: function(url, body, method, username, password, sendimmediately, callback) {
request({
url: url,
body: body,
method: method,
auth: {
user: username,
pass: password,
sendImmediately: sendimmediately
}
},
function(error, response, body) {
callback(error, response, body)
})
},
setPowerState: function(powerOn, callback) {
var url;
var body;
if (powerOn) {
url = this.on_url;
body = this.on_body;
this.log("Setting power state to on");
} else {
url = this.off_url;
body = this.off_body;
this.log("Setting power state to off");
}
this.httpRequest(url, body, this.http_method, this.username, this.password, this.sendimmediately, function(error, response, responseBody) {
if (error) {
this.log('HTTP set power function failed: %s', error.message);
callback(error);
} else {
this.log('HTTP set power function succeeded!');
// this.log(response);
// this.log(responseBody);
callback();
}
}.bind(this));
},
getPowerState: function(callback) {
if (!this.read_url) { callback(null); }
var url = this.read_url;
this.log("Getting power state");
this.httpRequest(url, '', 'GET', this.username, this.password, this.sendimmediately, function(error, response, responseBody) {
if (error) {
this.log('HTTP get power function failed: %s', error.message);
callback(error);
} else {
var binaryState = parseInt(responseBody);
var powerOn = binaryState > 0;
this.log("Power state is currently %s", binaryState);
callback(null, powerOn);
}
}.bind(this));
},
setBrightness: function(level, callback) {
var url = this.brightness_url.replace("%b", level)
this.log("Setting brightness to %s", level);
this.httpRequest(url, "", this.http_brightness_method, this.username, this.password, this.sendimmediately, function(error, response, body) {
if (error) {
this.log('HTTP brightness function failed: %s', error);
callback(error);
} else {
this.log('HTTP brightness function succeeded!');
callback();
}
}.bind(this));
},
identify: function(callback) {
this.log("Identify requested!");
callback(); // success
},
getServices: function() {
// you can OPTIONALLY create an information service if you wish to override
// the default values for things like serial number, model, etc.
var informationService = new Service.AccessoryInformation();
informationService
.setCharacteristic(Characteristic.Manufacturer, "HTTP Manufacturer")
.setCharacteristic(Characteristic.Model, "HTTP Model")
.setCharacteristic(Characteristic.SerialNumber, "HTTP Serial Number");
if (this.service == "Switch") {
var switchService = new Service.Switch(this.name);
switchService
.getCharacteristic(Characteristic.On)
.on('get', this.getPowerState.bind(this))
.on('set', this.setPowerState.bind(this));
return [switchService];
} else if (this.service == "Light") {
var lightbulbService = new Service.Lightbulb(this.name);
lightbulbService
.getCharacteristic(Characteristic.On)
.on('get', this.getPowerState.bind(this))
.on('set', this.setPowerState.bind(this));
if (this.brightnessHandling == "yes") {
lightbulbService
.addCharacteristic(new Characteristic.Brightness())
.on('set', this.setBrightness.bind(this));
}
return [informationService, lightbulbService];
}
}
};