homebridge-lockstate 1.0.1 → 1.0.4

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.
Files changed (2) hide show
  1. package/index.js +107 -234
  2. package/package.json +2 -56
package/index.js CHANGED
@@ -1,12 +1,13 @@
1
- var Service, Characteristic, Homebridge
1
+ var Service,Characteristic,Homebridge,recv_Value,Log
2
+ recv_Value=0;
2
3
  const request = require('request')
3
4
  const packageJson = require('./package.json')
4
5
  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;
6
+
7
+ const STATE_UNSECURED = 0;//���������
8
+ const STATE_SECURED = 1;//���ߴ�������״̬
9
+ const STATE_JAMMED = 2;//���߷������ϣ�������
10
+ const STATE_UNKNOWN = 3;//����״̬δ֪
10
11
 
11
12
  module.exports = function (homebridge) {
12
13
  Homebridge = homebridge
@@ -16,8 +17,8 @@ module.exports = function (homebridge) {
16
17
  }
17
18
 
18
19
  function lockstate(log, config) {
19
- this.status = []
20
- this.status_update = []
20
+ this.status = []
21
+ this.status_update = []
21
22
  this.config = config
22
23
  this.log = log
23
24
  this.name = config.name
@@ -29,21 +30,14 @@ function lockstate(log, config) {
29
30
  this.username = config.username || null
30
31
  this.password = config.password || null
31
32
  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
33
+
40
34
 
41
35
  this.autoLock = config.autoLock || false
42
36
  this.autoLockDelay = config.autoLockDelay || 5
43
37
 
44
38
  this.resetLock = config.resetLock || false
45
39
  this.resetLockTime = config.resetLockTime || 5
46
- // this.interval = setInterval(this.update.bind(this), this.config.pollFrequency || 1000);
40
+
47
41
  this.currentState = STATE_SECURED;
48
42
  this.targetState = this.currentState;
49
43
  if (this.username != null && this.password != null) {
@@ -56,61 +50,98 @@ function lockstate(log, config) {
56
50
  this.log(this.name)
57
51
  this.service = new Service.LockMechanism(this.name)
58
52
 
53
+ this.ip = {
54
+ 'host': config.ip || '127.0.0.1',
55
+ 'port': config.port || 502
56
+ }
57
+ this.socket = new Net.Socket();
58
+ // Connect
59
+
60
+ //this.socket.connect(this.ip);
61
+ //this.socket = Net.createConnection({
62
+ // "port": config.port,
63
+ //"host": config.ip,
64
+ //"noDelay": true,
65
+ // "keepAlive": true
66
+ //});
67
+
68
+ Homebridge.on("didFinishLaunching", () => {
69
+ this.socket.connect(this.ip);
70
+ this.log("Connecting to", this.ip.host);
71
+ });
72
+ this.socket.on('connect', () => {
73
+ log("Socket connected");
74
+ });
75
+
76
+ this.socket.on('error', (error) => {
77
+ if (error) {
78
+ log(error);
79
+ }
80
+ log("error-Connection error");
81
+
82
+ setTimeout(() => {
83
+ //this.socket = Net.createConnection({
84
+ // "port": config.port,
85
+ // "host": config.ip,
86
+ // "noDelay": true,
87
+ // "keepAlive": true
88
+ //});
89
+ //this.socket.connect(this.ip);���dz���
90
+ log("error-Reconnecting to",);
91
+ }, 10000);
92
+ });
93
+
94
+ this.socket.on('data', (data) => {
95
+
96
+ var temp_value = data[0]
97
+ if (temp_value & 0x80 ) {
98
+
99
+ temp_value =temp_value &0x7f;
100
+ if (temp_value <= 100 && temp_value >= 0){
101
+
102
+ recv_Value =temp_value;
103
+
104
+ this.log("socket-data:%s", recv_Value);
105
+
106
+ if (recv_Value ==0)
107
+ {
108
+ this.currentState = STATE_UNSECURED
109
+ this.targetState = STATE_UNSECURED
110
+ this.service.getCharacteristic(Characteristic.LockTargetState).updateValue(STATE_UNSECURED);
111
+ this.service.getCharacteristic(Characteristic.LockCurrentState).updateValue(STATE_UNSECURED);
112
+ }
113
+ else{
114
+ this.currentState = STATE_SECURED
115
+ this.targetState = STATE_SECURED
116
+
117
+ this.service.getCharacteristic(Characteristic.LockTargetState).updateValue(STATE_SECURED);
118
+ this.service.getCharacteristic(Characteristic.LockCurrentState).updateValue(STATE_SECURED);
119
+ }
120
+ }
121
+
122
+ }
123
+ });
124
+
125
+ this.socket.on('close', (error) => {
126
+ if (error) {
127
+ this.log(error);
128
+ }
129
+ this.log("Connection lost");
130
+ this.flagscontect = false;
131
+
132
+ setTimeout(() => {
133
+ //this.socket = Net.createConnection({
134
+ // "port": config.port,
135
+ // "host": config.ip,
136
+ // "noDelay": true,
137
+ // "keepAlive": true
138
+ //});
139
+ this.socket.connect(this.ip);
140
+ this.log("Reconnecting to", this.ip.host);
141
+ }, 9000);
142
+ });
59
143
 
60
144
 
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
145
  }
115
146
 
116
147
  lockstate.prototype = {
@@ -118,160 +149,25 @@ lockstate.prototype = {
118
149
  identify: function (callback) {
119
150
  this.log('Identify requested!')
120
151
  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
152
 
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
153
  },
154
+
196
155
  getLockTargetState: function (callback) {
197
156
  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
- // });
157
+ callback(null, this.currentState);
158
+
220
159
  },
160
+
221
161
  getLockCurrentState: function (callback) { //�򿪼�ͥ������һ��
222
162
  this.log("Lock Current state: %s", this.currentState);
223
163
 
224
- // this.modbus.readCoils(9, 1).then((resp) => { //����״̬������Ϊ�������
225
- // this.status[0] = resp.response._body.valuesAsArray;
226
164
 
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
- // });
165
+ callback(null, this.currentState);
166
+
246
167
  },
247
168
 
248
169
  setLockTargetState: function (value, callback) {
249
170
 
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
171
  if (value == 1) {
276
172
 
277
173
  this.service.getCharacteristic(Characteristic.LockTargetState).updateValue(STATE_SECURED);
@@ -286,30 +182,11 @@ lockstate.prototype = {
286
182
  this.currentState = STATE_UNSECURED
287
183
  }
288
184
  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
185
  },
308
186
 
309
187
  getServices: function () {
310
188
  this.service.setCharacteristic(Characteristic.LockCurrentState, Characteristic.LockCurrentState.SECURED)
311
189
  this.service.setCharacteristic(Characteristic.LockTargetState, Characteristic.LockTargetState.SECURED)
312
-
313
190
  this.informationService = new Service.AccessoryInformation()
314
191
  this.informationService
315
192
  .setCharacteristic(Characteristic.Manufacturer, this.manufacturer)
@@ -319,16 +196,12 @@ lockstate.prototype = {
319
196
  this.service
320
197
  .getCharacteristic(Characteristic.LockCurrentState)
321
198
  .on("get", this.getLockCurrentState.bind(this))
322
-
323
199
  this.service
324
200
  .getCharacteristic(Characteristic.LockTargetState)
325
201
  .on('get', this.getLockTargetState.bind(this))
326
- //.on('set', this.getLockTargetState.bind(this))
327
202
  .on('set', this.setLockTargetState.bind(this))
328
203
 
329
204
  return [this.informationService, this.service]
330
205
  }
331
206
  }
332
207
 
333
-
334
-
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "homebridge-lockstate",
3
- "version": "1.0.1",
3
+ "version": "1.0.4",
4
4
  "description": "lock",
5
5
  "main": "index.js",
6
6
  "keywords": [
@@ -14,59 +14,5 @@
14
14
  },
15
15
  "scripts": {
16
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": {}
17
+ }
72
18
  }