homebridge-lockstate 1.0.1 → 1.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/index.js +15 -236
- package/package.json +1 -1
package/index.js
CHANGED
|
@@ -2,11 +2,11 @@ var Service, Characteristic, Homebridge
|
|
|
2
2
|
const request = require('request')
|
|
3
3
|
const packageJson = require('./package.json')
|
|
4
4
|
const Net = require('net')
|
|
5
|
-
|
|
6
|
-
const STATE_UNSECURED = 0
|
|
7
|
-
const STATE_SECURED = 1
|
|
8
|
-
const STATE_JAMMED = 2
|
|
9
|
-
const STATE_UNKNOWN = 3
|
|
5
|
+
|
|
6
|
+
const STATE_UNSECURED = 0;//���������
|
|
7
|
+
const STATE_SECURED = 1;//���ߴ�������״̬
|
|
8
|
+
const STATE_JAMMED = 2;//���߷������ϣ�������
|
|
9
|
+
const STATE_UNKNOWN = 3;//����״̬δ֪
|
|
10
10
|
|
|
11
11
|
module.exports = function (homebridge) {
|
|
12
12
|
Homebridge = homebridge
|
|
@@ -16,8 +16,8 @@ module.exports = function (homebridge) {
|
|
|
16
16
|
}
|
|
17
17
|
|
|
18
18
|
function lockstate(log, config) {
|
|
19
|
-
|
|
20
|
-
|
|
19
|
+
this.status = []
|
|
20
|
+
this.status_update = []
|
|
21
21
|
this.config = config
|
|
22
22
|
this.log = log
|
|
23
23
|
this.name = config.name
|
|
@@ -29,21 +29,14 @@ function lockstate(log, config) {
|
|
|
29
29
|
this.username = config.username || null
|
|
30
30
|
this.password = config.password || null
|
|
31
31
|
this.timeout = (config.httptimeout * 1000) || 5000
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
this.openURL = config.openURL
|
|
35
|
-
this.openBody = config.openBody || ''
|
|
36
|
-
this.openHeader = config.openHeader || null
|
|
37
|
-
this.closeURL = config.closeURL
|
|
38
|
-
this.closeBody = config.closeBody || ''
|
|
39
|
-
this.closeHeader = config.closeHeader || null
|
|
32
|
+
|
|
40
33
|
|
|
41
34
|
this.autoLock = config.autoLock || false
|
|
42
35
|
this.autoLockDelay = config.autoLockDelay || 5
|
|
43
36
|
|
|
44
37
|
this.resetLock = config.resetLock || false
|
|
45
38
|
this.resetLockTime = config.resetLockTime || 5
|
|
46
|
-
|
|
39
|
+
|
|
47
40
|
this.currentState = STATE_SECURED;
|
|
48
41
|
this.targetState = this.currentState;
|
|
49
42
|
if (this.username != null && this.password != null) {
|
|
@@ -55,62 +48,6 @@ function lockstate(log, config) {
|
|
|
55
48
|
|
|
56
49
|
this.log(this.name)
|
|
57
50
|
this.service = new Service.LockMechanism(this.name)
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
this.ip = {
|
|
62
|
-
'host': config.ip || '127.0.0.1',
|
|
63
|
-
'port': config.port || 502
|
|
64
|
-
}
|
|
65
|
-
this.socket = new Net.Socket();
|
|
66
|
-
this.modbus = new Modbus.client.TCP(this.socket, this.config.unit, this.config.timeout);
|
|
67
|
-
this.log("electromagnetic", config.unit, config.timeout)
|
|
68
|
-
this.flagscontect = false;
|
|
69
|
-
Homebridge.on("didFinishLaunching", () => {
|
|
70
|
-
this.socket.connect(this.ip);
|
|
71
|
-
this.log("Connecting to", this.ip.host);
|
|
72
|
-
|
|
73
|
-
});
|
|
74
|
-
|
|
75
|
-
this.socket.on('connect', () => {
|
|
76
|
-
this.log("Socket connected");
|
|
77
|
-
this.flagscontect = true;
|
|
78
|
-
flags_connect = true;
|
|
79
|
-
if (this.interval) {
|
|
80
|
-
clearInterval(this.interval);
|
|
81
|
-
|
|
82
|
-
}
|
|
83
|
-
// else {
|
|
84
|
-
// this.update();
|
|
85
|
-
// }
|
|
86
|
-
this.interval = setInterval(this.update.bind(this), this.config.pollFrequency || 1000);
|
|
87
|
-
});
|
|
88
|
-
|
|
89
|
-
this.socket.on('error', (error) => {
|
|
90
|
-
if (error) {
|
|
91
|
-
this.log(error);
|
|
92
|
-
}
|
|
93
|
-
this.log("error-Connection error");
|
|
94
|
-
// this.flagscontect = false;
|
|
95
|
-
|
|
96
|
-
setTimeout(() => {
|
|
97
|
-
//this.socket.connect(this.ip);
|
|
98
|
-
this.log("error-Reconnecting to", this.ip.host);
|
|
99
|
-
}, 10000);
|
|
100
|
-
});
|
|
101
|
-
|
|
102
|
-
this.socket.on('close', (error) => {
|
|
103
|
-
if (error) {
|
|
104
|
-
this.log(error);
|
|
105
|
-
}
|
|
106
|
-
this.log("Connection lost");
|
|
107
|
-
this.flagscontect = false;
|
|
108
|
-
|
|
109
|
-
setTimeout(() => {
|
|
110
|
-
this.socket.connect(this.ip);
|
|
111
|
-
this.log("Reconnecting to", this.ip.host);
|
|
112
|
-
}, 9000);
|
|
113
|
-
});
|
|
114
51
|
}
|
|
115
52
|
|
|
116
53
|
lockstate.prototype = {
|
|
@@ -118,160 +55,25 @@ lockstate.prototype = {
|
|
|
118
55
|
identify: function (callback) {
|
|
119
56
|
this.log('Identify requested!')
|
|
120
57
|
callback()
|
|
121
|
-
},
|
|
122
|
-
_httpRequest: function (url, headers, body, method, callback) {
|
|
123
|
-
request({
|
|
124
|
-
url: url,
|
|
125
|
-
headers: headers,
|
|
126
|
-
body: body,
|
|
127
|
-
method: this.http_method,
|
|
128
|
-
timeout: this.timeout,
|
|
129
|
-
rejectUnauthorized: false,
|
|
130
|
-
auth: this.auth
|
|
131
|
-
},
|
|
132
|
-
function (error, response, body) {
|
|
133
|
-
callback(error, response, body)
|
|
134
|
-
})
|
|
135
|
-
},
|
|
136
|
-
update: function () {
|
|
137
|
-
|
|
138
|
-
if (this.interval) {
|
|
139
|
-
this.modbus.readCoils(9, 1).then((resp) => { //����״̬������Ϊ�������
|
|
140
|
-
this.status_update[0] = resp.response._body.valuesAsArray;
|
|
141
|
-
|
|
142
|
-
let val_update = parseInt(this.status_update[0]);
|
|
143
|
-
// if (parseInt(resp.response._body.valuesAsArray)) {
|
|
144
|
-
// if (val_update != this.targetState) {
|
|
145
|
-
|
|
146
|
-
if (val_update == 0) {
|
|
147
|
-
|
|
148
|
-
//this.log('update-target-securd', val);
|
|
149
|
-
this.service.getCharacteristic(Characteristic.LockTargetState).updateValue(STATE_SECURED);
|
|
150
|
-
this.service.getCharacteristic(Characteristic.LockCurrentState).updateValue(STATE_SECURED);
|
|
151
|
-
|
|
152
|
-
this.targetState = STATE_SECURED
|
|
153
|
-
this.currentState = STATE_SECURED
|
|
154
|
-
// this.service.setCharacteristic(Characteristic.LockCurrentState, Characteristic.LockCurrentState.SECURED)
|
|
155
|
-
// this.service.setCharacteristic(Characteristic.LockkTargetState, Characteristic.LockTargetState.SECURED)
|
|
156
|
-
}
|
|
157
|
-
else {
|
|
158
|
-
|
|
159
|
-
// this.log('update-target-unsecurd', val);
|
|
160
|
-
this.service.getCharacteristic(Characteristic.LockTargetState).updateValue(STATE_UNSECURED);
|
|
161
|
-
this.service.getCharacteristic(Characteristic.LockCurrentState).updateValue(STATE_UNSECURED);
|
|
162
|
-
this.targetState = STATE_UNSECURED
|
|
163
|
-
this.currentState = STATE_UNSECURED
|
|
164
58
|
|
|
165
|
-
// this.service.setCharacteristic(Characteristic.LockCurrentState, Characteristic.LockCurrentState.UNSECURED)
|
|
166
|
-
// this.service.setCharacteristic(Characteristic.LockkTargetState, Characteristic.LockTargetState.UNSECURED)
|
|
167
|
-
}
|
|
168
|
-
// }
|
|
169
|
-
}).catch((error) => {
|
|
170
|
-
this.log("error-update");
|
|
171
|
-
this.reset();
|
|
172
|
-
});
|
|
173
|
-
// this.log("writeing-every-time");
|
|
174
|
-
}
|
|
175
|
-
|
|
176
|
-
},
|
|
177
|
-
reset: function () {
|
|
178
|
-
if (this.interval) {
|
|
179
|
-
clearInterval(this.interval);
|
|
180
|
-
}
|
|
181
|
-
|
|
182
|
-
this.flagscontect = false;
|
|
183
|
-
this.commands = [];
|
|
184
|
-
this.command = null;
|
|
185
|
-
if (flags_connect) {
|
|
186
|
-
this.log("error-rest-rest-rest");
|
|
187
|
-
flags_connect = false;
|
|
188
|
-
this.socket.end();
|
|
189
|
-
}
|
|
190
|
-
else {
|
|
191
|
-
this.log("error-norest-norest-norest");
|
|
192
|
-
//flags_connect = true;
|
|
193
|
-
//this.socket.end();
|
|
194
|
-
}
|
|
195
59
|
},
|
|
60
|
+
|
|
196
61
|
getLockTargetState: function (callback) {
|
|
197
62
|
this.log("Lock target state: %s", this.targetState);
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
// this.status_get_Target[0] = resp.response._body.valuesAsArray;
|
|
201
|
-
// let val_get_Target = parseInt(this.status[0]);
|
|
202
|
-
// if (val_get_Target == 0) {
|
|
203
|
-
// // if (parseInt(resp.response._body.valuesAsArray)) {
|
|
204
|
-
// this.targetState = STATE_SECURED
|
|
205
|
-
// this.currentState = STATE_SECURED
|
|
206
|
-
// // this.log('target-securd', val_get);
|
|
207
|
-
// }
|
|
208
|
-
// else {
|
|
209
|
-
// this.targetState = STATE_UNSECURED
|
|
210
|
-
// this.currentState = STATE_UNSECURED
|
|
211
|
-
// // this.log('target-unsecurd', val_get);
|
|
212
|
-
// }
|
|
213
|
-
// // this.log('target-state', val_get);
|
|
214
|
-
|
|
215
|
-
callback(null, this.currentState);
|
|
216
|
-
// }).catch((error) => {
|
|
217
|
-
// this.log("error-getLockTargetStates-readCoils9-1");
|
|
218
|
-
// this.reset();
|
|
219
|
-
// });
|
|
63
|
+
callback(null, this.currentState);
|
|
64
|
+
|
|
220
65
|
},
|
|
66
|
+
|
|
221
67
|
getLockCurrentState: function (callback) { //��ͥ������һ��
|
|
222
68
|
this.log("Lock Current state: %s", this.currentState);
|
|
223
69
|
|
|
224
|
-
// this.modbus.readCoils(9, 1).then((resp) => { //����״̬������Ϊ�������
|
|
225
|
-
// this.status[0] = resp.response._body.valuesAsArray;
|
|
226
70
|
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
// if (parseInt(resp.response._body.valuesAsArray)) {
|
|
230
|
-
// this.targetState = STATE_SECURED
|
|
231
|
-
// this.currentState = STATE_SECURED
|
|
232
|
-
// // this.log('current-securd', val);
|
|
233
|
-
// }
|
|
234
|
-
//
|
|
235
|
-
// else {
|
|
236
|
-
// this.targetState = STATE_UNSECURED
|
|
237
|
-
// this.currentState = STATE_UNSECURED
|
|
238
|
-
// // this.log('current-unsecurd',val);
|
|
239
|
-
// }
|
|
240
|
-
// // this.log('current-state', val);
|
|
241
|
-
callback(null, this.currentState);
|
|
242
|
-
// }).catch((error) => {
|
|
243
|
-
// this.log("error-getLockCurrentStates-readCoils-9-1");
|
|
244
|
-
// this.reset();
|
|
245
|
-
// });
|
|
71
|
+
callback(null, this.currentState);
|
|
72
|
+
|
|
246
73
|
},
|
|
247
74
|
|
|
248
75
|
setLockTargetState: function (value, callback) {
|
|
249
76
|
|
|
250
|
-
var url
|
|
251
|
-
var body
|
|
252
|
-
var headers
|
|
253
|
-
this.log('[+] Setting LockTargetState to %s', value)
|
|
254
|
-
if (value == 1) {
|
|
255
|
-
url = this.closeURL
|
|
256
|
-
body = this.closeBody
|
|
257
|
-
headers = this.closeHeader
|
|
258
|
-
}else {
|
|
259
|
-
url = this.openURL
|
|
260
|
-
body = this.openBody
|
|
261
|
-
headers = this.openHeader
|
|
262
|
-
}
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
this._httpRequest(url, headers, body, this.http_method, function (error, response, responseBody) {
|
|
266
|
-
}.bind(this))
|
|
267
|
-
this.modbus.readCoils(9, 1).then((resp) => { //����״̬������Ϊ�������
|
|
268
|
-
this.status_update[0] = resp.response._body.valuesAsArray;
|
|
269
|
-
|
|
270
|
-
});
|
|
271
|
-
let val_update = parseInt(this.status_update[0]);
|
|
272
|
-
|
|
273
|
-
value = val_update;
|
|
274
|
-
|
|
275
77
|
if (value == 1) {
|
|
276
78
|
|
|
277
79
|
this.service.getCharacteristic(Characteristic.LockTargetState).updateValue(STATE_SECURED);
|
|
@@ -286,30 +88,11 @@ lockstate.prototype = {
|
|
|
286
88
|
this.currentState = STATE_UNSECURED
|
|
287
89
|
}
|
|
288
90
|
callback()
|
|
289
|
-
|
|
290
|
-
},
|
|
291
|
-
|
|
292
|
-
autoLockFunction: function () {
|
|
293
|
-
this.log('[+] Waiting %s seconds for autolock', this.autoLockDelay)
|
|
294
|
-
setTimeout(() => {
|
|
295
|
-
this.service.setCharacteristic(Characteristic.LockTargetState, Characteristic.LockTargetState.SECURED)
|
|
296
|
-
this.log('[*] Autolocking')
|
|
297
|
-
}, this.autoLockDelay * 1000)
|
|
298
|
-
},
|
|
299
|
-
|
|
300
|
-
resetLockFunction: function () {
|
|
301
|
-
this.log('[+] Waiting %s seconds for resetting lock state to locked', this.resetLockTime)
|
|
302
|
-
setTimeout(() => {
|
|
303
|
-
this.service.getCharacteristic(Characteristic.LockCurrentState).updateValue(1)
|
|
304
|
-
this.service.getCharacteristic(Characteristic.LockTargetState).updateValue(1)
|
|
305
|
-
this.log('[*] Lock State resetted')
|
|
306
|
-
}, this.resetLockTime * 1000)
|
|
307
91
|
},
|
|
308
92
|
|
|
309
93
|
getServices: function () {
|
|
310
94
|
this.service.setCharacteristic(Characteristic.LockCurrentState, Characteristic.LockCurrentState.SECURED)
|
|
311
95
|
this.service.setCharacteristic(Characteristic.LockTargetState, Characteristic.LockTargetState.SECURED)
|
|
312
|
-
|
|
313
96
|
this.informationService = new Service.AccessoryInformation()
|
|
314
97
|
this.informationService
|
|
315
98
|
.setCharacteristic(Characteristic.Manufacturer, this.manufacturer)
|
|
@@ -319,16 +102,12 @@ lockstate.prototype = {
|
|
|
319
102
|
this.service
|
|
320
103
|
.getCharacteristic(Characteristic.LockCurrentState)
|
|
321
104
|
.on("get", this.getLockCurrentState.bind(this))
|
|
322
|
-
|
|
323
105
|
this.service
|
|
324
106
|
.getCharacteristic(Characteristic.LockTargetState)
|
|
325
107
|
.on('get', this.getLockTargetState.bind(this))
|
|
326
|
-
//.on('set', this.getLockTargetState.bind(this))
|
|
327
108
|
.on('set', this.setLockTargetState.bind(this))
|
|
328
109
|
|
|
329
110
|
return [this.informationService, this.service]
|
|
330
111
|
}
|
|
331
112
|
}
|
|
332
113
|
|
|
333
|
-
|
|
334
|
-
|