homebridge-lockstate 1.0.1

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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2019 Tom Rodrigues
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,56 @@
1
+
2
+ {
3
+ "pluginAlias": "lockstate",
4
+ "pluginType": "accessory",
5
+ "schema": {
6
+ "type": "object",
7
+ "properties": {
8
+ "name": {
9
+ "title": "plugin-name",
10
+ "type": "string",
11
+
12
+ "required": true,
13
+ "placeholder": "lockstate"
14
+
15
+
16
+ },
17
+ "ip": {
18
+ "title": "ip-address with server",
19
+ "type": "string",
20
+ "required": true,
21
+ "placeholder": "10.0.0.30"
22
+
23
+ },
24
+ "port": {
25
+ "title": "the port",
26
+ "type":"integer",
27
+ "required": true,
28
+ "placeholder": 8898
29
+
30
+ },
31
+ "pollFrequency": {
32
+ "title": "the pollFrequency",
33
+ "type":"integer",
34
+ "required": true,
35
+ "placeholder":1000
36
+
37
+ },
38
+ "timeout": {
39
+ "title": "the timeout",
40
+ "type":"integer",
41
+ "required": true,
42
+ "placeholder":8000
43
+
44
+ },
45
+ "unit": {
46
+ "title": "the unit",
47
+ "type":"integer",
48
+ "required": true,
49
+ "placeholder":1
50
+
51
+ }
52
+
53
+
54
+ }
55
+ }
56
+ }
package/index.js ADDED
@@ -0,0 +1,334 @@
1
+ var Service, Characteristic, Homebridge
2
+ const request = require('request')
3
+ const packageJson = require('./package.json')
4
+ const Net = require('net')
5
+ const Modbus = require('jsmodbus')
6
+ const STATE_UNSECURED = 0;
7
+ const STATE_SECURED = 1;
8
+ const STATE_JAMMED = 2;
9
+ const STATE_UNKNOWN = 3;
10
+
11
+ module.exports = function (homebridge) {
12
+ Homebridge = homebridge
13
+ Service = homebridge.hap.Service
14
+ Characteristic = homebridge.hap.Characteristic
15
+ homebridge.registerAccessory('homebridge-lockstate', 'lockstate',lockstate)
16
+ }
17
+
18
+ function lockstate(log, config) {
19
+ this.status = []
20
+ this.status_update = []
21
+ this.config = config
22
+ this.log = log
23
+ this.name = config.name
24
+ this.manufacturer = config.manufacturer || packageJson.author.name
25
+ this.serial = config.serial || packageJson.version
26
+ this.model = config.model || packageJson.name
27
+ this.firmware = config.firmware || packageJson.version
28
+
29
+ this.username = config.username || null
30
+ this.password = config.password || null
31
+ this.timeout = (config.httptimeout * 1000) || 5000
32
+ this.http_method = config.http_method || 'GET'
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
40
+
41
+ this.autoLock = config.autoLock || false
42
+ this.autoLockDelay = config.autoLockDelay || 5
43
+
44
+ this.resetLock = config.resetLock || false
45
+ this.resetLockTime = config.resetLockTime || 5
46
+ // this.interval = setInterval(this.update.bind(this), this.config.pollFrequency || 1000);
47
+ this.currentState = STATE_SECURED;
48
+ this.targetState = this.currentState;
49
+ if (this.username != null && this.password != null) {
50
+ this.auth = {
51
+ user: this.username,
52
+ pass: this.password
53
+ }
54
+ }
55
+
56
+ this.log(this.name)
57
+ 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
+ }
115
+
116
+ lockstate.prototype = {
117
+
118
+ identify: function (callback) {
119
+ this.log('Identify requested!')
120
+ 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
+
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
+ },
196
+ getLockTargetState: function (callback) {
197
+ this.log("Lock target state: %s", this.targetState);
198
+
199
+ // this.modbus.readCoils(9, 1).then((resp) => { //����״̬������Ϊ�������
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
+ // });
220
+ },
221
+ getLockCurrentState: function (callback) { //�򿪼�ͥ������һ��
222
+ this.log("Lock Current state: %s", this.currentState);
223
+
224
+ // this.modbus.readCoils(9, 1).then((resp) => { //����״̬������Ϊ�������
225
+ // this.status[0] = resp.response._body.valuesAsArray;
226
+
227
+ // let val = parseInt(this.status[0]);
228
+ // if (val == 0){
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
+ // });
246
+ },
247
+
248
+ setLockTargetState: function (value, callback) {
249
+
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
+ if (value == 1) {
276
+
277
+ this.service.getCharacteristic(Characteristic.LockTargetState).updateValue(STATE_SECURED);
278
+ this.service.getCharacteristic(Characteristic.LockCurrentState).updateValue(STATE_SECURED);
279
+ this.targetState = STATE_SECURED
280
+ this.currentState = STATE_SECURED
281
+ } else {
282
+
283
+ this.service.getCharacteristic(Characteristic.LockTargetState).updateValue(STATE_UNSECURED);
284
+ this.service.getCharacteristic(Characteristic.LockCurrentState).updateValue(STATE_UNSECURED);
285
+ this.targetState = STATE_UNSECURED
286
+ this.currentState = STATE_UNSECURED
287
+ }
288
+ 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
+ },
308
+
309
+ getServices: function () {
310
+ this.service.setCharacteristic(Characteristic.LockCurrentState, Characteristic.LockCurrentState.SECURED)
311
+ this.service.setCharacteristic(Characteristic.LockTargetState, Characteristic.LockTargetState.SECURED)
312
+
313
+ this.informationService = new Service.AccessoryInformation()
314
+ this.informationService
315
+ .setCharacteristic(Characteristic.Manufacturer, this.manufacturer)
316
+ .setCharacteristic(Characteristic.Model, this.model)
317
+ .setCharacteristic(Characteristic.SerialNumber, this.serial)
318
+ .setCharacteristic(Characteristic.FirmwareRevision, this.firmware)
319
+ this.service
320
+ .getCharacteristic(Characteristic.LockCurrentState)
321
+ .on("get", this.getLockCurrentState.bind(this))
322
+
323
+ this.service
324
+ .getCharacteristic(Characteristic.LockTargetState)
325
+ .on('get', this.getLockTargetState.bind(this))
326
+ //.on('set', this.getLockTargetState.bind(this))
327
+ .on('set', this.setLockTargetState.bind(this))
328
+
329
+ return [this.informationService, this.service]
330
+ }
331
+ }
332
+
333
+
334
+
package/package.json ADDED
@@ -0,0 +1,72 @@
1
+ {
2
+ "name": "homebridge-lockstate",
3
+ "version": "1.0.1",
4
+ "description": "lock",
5
+ "main": "index.js",
6
+ "keywords": [
7
+ "homebridge-plugin",
8
+ "lock"
9
+ ],
10
+ "author": "masm32",
11
+ "license": "MIT",
12
+ "engines": {
13
+ "homebridge": ">=0.2.0"
14
+ },
15
+ "scripts": {
16
+ "test": "echo \"Error: no test specified\" && exit 1"
17
+ },
18
+ "dependencies": {
19
+ "ajv": "^6.12.6",
20
+ "asn1": "^0.2.6",
21
+ "assert-plus": "^1.0.0",
22
+ "asynckit": "^0.4.0",
23
+ "aws-sign2": "^0.7.0",
24
+ "aws4": "^1.13.2",
25
+ "bcrypt-pbkdf": "^1.0.2",
26
+ "caseless": "^0.12.0",
27
+ "combined-stream": "^1.0.8",
28
+ "core-util-is": "^1.0.2",
29
+ "crc": "^3.4.0",
30
+ "dashdash": "^1.14.1",
31
+ "debug": "^3.2.7",
32
+ "delayed-stream": "^1.0.0",
33
+ "ecc-jsbn": "^0.1.2",
34
+ "extend": "^3.0.2",
35
+ "extsprintf": "^1.3.0",
36
+ "fast-deep-equal": "^3.1.3",
37
+ "fast-json-stable-stringify": "^2.1.0",
38
+ "forever-agent": "^0.6.1",
39
+ "form-data": "^2.3.3",
40
+ "getpass": "^0.1.7",
41
+ "har-schema": "^2.0.0",
42
+ "har-validator": "^5.1.5",
43
+ "http-signature": "^1.2.0",
44
+ "is-typedarray": "^1.0.0",
45
+ "isstream": "^0.1.2",
46
+ "jsbn": "^0.1.1",
47
+ "jsmodbus": "^4.0.10",
48
+ "json-schema": "^0.4.0",
49
+ "json-schema-traverse": "^0.4.1",
50
+ "json-stringify-safe": "^5.0.1",
51
+ "jsprim": "^1.4.2",
52
+ "mime-db": "^1.52.0",
53
+ "mime-types": "^2.1.35",
54
+ "ms": "^2.1.3",
55
+ "oauth-sign": "^0.9.0",
56
+ "performance-now": "^2.1.0",
57
+ "psl": "^1.15.0",
58
+ "punycode": "^2.3.1",
59
+ "qs": "^6.5.3",
60
+ "request": "^2.88.2",
61
+ "safe-buffer": "^5.2.1",
62
+ "safer-buffer": "^2.1.2",
63
+ "sshpk": "^1.18.0",
64
+ "tough-cookie": "^2.5.0",
65
+ "tunnel-agent": "^0.6.0",
66
+ "tweetnacl": "^0.14.5",
67
+ "uri-js": "^4.4.1",
68
+ "uuid": "^3.4.0",
69
+ "verror": "^1.10.0"
70
+ },
71
+ "devDependencies": {}
72
+ }