forked from codetwice/homebridge-http-securitysystem
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
150 lines (133 loc) · 3.73 KB
/
index.js
File metadata and controls
150 lines (133 loc) · 3.73 KB
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
var Service, Characteristic;
var request = require("request");
module.exports = function(homebridge){
Service = homebridge.hap.Service;
Characteristic = homebridge.hap.Characteristic;
homebridge.registerAccessory("homebridge-http-securitysystem", "Http-SecuritySystem", HttpSecuritySystemAccessory);
}
function HttpSecuritySystemAccessory(log, config) {
this.log = log;
// url info
this.urls = {
stay: {
url: config.urls.stay.url,
body: config.urls.stay.body
},
away: {
url: config.urls.away.url,
body: config.urls.away.body
},
night: {
url: config.urls.night.url,
body: config.urls.night.body
},
disarm: {
url: config.urls.disarm.url,
body: config.urls.disarm.body
},
readCurrentState: {
url: config.urls.readCurrentState.url,
body: config.urls.readCurrentState.body
},
readTargetState: {
url: config.urls.readTargetState.url,
body: config.urls.readTargetState.body
}
};
this.httpMethod = config["http_method"] || "GET";
this.auth = {
username: config.auth.username,
password: config.auth.password,
immediately: config.auth.immediately
},
this.name = config["name"];
}
HttpSecuritySystemAccessory.prototype = {
httpRequest: function(url, body, callback) {
request({
url: url,
body: body,
method: this.httpMethod,
auth: {
user: this.auth.username,
pass: this.auth.password,
sendImmediately: this.auth.immediately
}
},
function(error, response, body) {
callback(error, response, body)
})
},
setTargetState: function(state, callback) {
this.log("Setting state to %s", state);
var cfg = null;
switch (state) {
case Characteristic.SecuritySystemTargetState.STAY_ARM:
cfg = this.urls.stay;
break;
case Characteristic.SecuritySystemTargetState.AWAY_ARM :
cfg = this.urls.away;
break;
case Characteristic.SecuritySystemTargetState.NIGHT_ARM:
cfg = this.urls.night;
break;
case Characteristic.SecuritySystemTargetState.DISARM:
cfg = this.urls.disarm;
break;
}
var url = cfg.url;
var body = cfg.body;
if (url) {
this.httpRequest(url, body, function(error, response, responseBody) {
if (error) {
this.log('SetState function failed: %s', error.message);
callback(error);
} else {
this.log('SetState function succeeded!');
this.securityService.setCharacteristic(Characteristic.SecuritySystemCurrentState, state);
callback(error, response, state);
}
}.bind(this));
} else {
callback(null);
}
},
getState: function(url, body, callback) {
if (!url) {
callback(null);
}
this.httpRequest(url, body, function(error, response, responseBody) {
if (error) {
this.log('GetState function failed: %s', error.message);
callback(error);
} else {
var state = parseInt(responseBody);
this.log("State is currently %s", state);
callback(null, state);
}
}.bind(this));
},
getCurrentState: function(callback) {
this.log("Getting current state");
this.getState(this.urls.readCurrentState.url, this.urls.readCurrentState.body, callback);
},
getTargetState: function(callback) {
this.log("Getting target state");
this.getState(this.urls.readTargetState.url, this.urls.readTargetState.body, callback);
},
identify: function(callback) {
this.log("Identify requested!");
callback(); // success
},
getServices: function() {
this.securityService = new Service.SecuritySystem(this.name);
this.securityService
.getCharacteristic(Characteristic.SecuritySystemCurrentState)
.on('get', this.getCurrentState.bind(this));
this.securityService
.getCharacteristic(Characteristic.SecuritySystemTargetState)
.on('get', this.getTargetState.bind(this))
.on('set', this.setTargetState.bind(this));
return [this.securityService];
}
};