-
Notifications
You must be signed in to change notification settings - Fork 3
/
LockMechanism.js
176 lines (137 loc) · 4.71 KB
/
LockMechanism.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
// Description: Acceccory Shim to use with homebridge https://github.com/nfarina/homebridge
// Copy this file into the folder: homebridge/accessories
/* config.json Example:
{
"bridge": {
"name": "Homebridge",
"username": "CC:22:3D:E3:CE:27",
"port": 51826,
"pin": "031-45-154"
},
"platforms": [],
"accessories": [
{
"accessory": "LockMechanism",
"name": "LockMechanism"
}
]
}
*/
var Service = require("HAP-NodeJS").Service;
var Characteristic = require("HAP-NodeJS").Characteristic;
var request = require("request");
module.exports = {
accessory: LockMechanism
}
function LockMechanism(log, config) {
this.log = log;
this.name = config["name"];
this.Characteristic = {};
this.currentValue = {};
}
LockMechanism.prototype = {
/**
* Hardware Lock Simulation
*/
setLockCurrentState: function(value) {
if(this.timeoutObj) {
clearTimeout(this.timeoutObj);
}
this.timeoutObj = setTimeout(function() {
// Simulation: after 1.5 seconds this.currentValue.LockCurrentState is set to this.currentValue.LockTargetState
switch (value) {
case 'unlock': this.currentValue.LockCurrentState = Characteristic.LockTargetState.UNSECURED; break;
case 'lock': this.currentValue.LockCurrentState = Characteristic.LockTargetState.SECURED; break;
default:
}
this.Characteristic.LockCurrentState.setValue(this.currentValue.LockCurrentState);
}.bind(this), 1500);
},
/**
* Characteristic.LockCurrentState
*/
getLockCurrentState: function(callback) {
this.log('getLockCurrentState');
// comment callback and uncomment your code
callback(null, this.currentValue.LockCurrentState);
/* your code
request('http://192.168.0.100/jsonapi.asp?action=getdevice&id=Z2', function (error, resp, body) {
if (!error) {
var response = JSON.parse(body);
var value = response['value'];
callback(null,value);
}
else {
callback(error);
}
});
*/ // your code end
},
/**
* Characteristic.LockTargetState
*/
getLockTargetState: function(callback) {
this.log('getLockTargetState');
// comment callback and uncomment your code
callback(null, this.currentValue.LockTargetState);
/* your code
request('http://192.168.0.100/jsonapi.asp?action=getdevice&id=L70', function (error, resp, body) {
if (!error) {
var response = JSON.parse(body);
var value = response['value'];
callback(null,value);
}
else {
callback(error);
}
});
*/ // your code end
},
setLockTargetState: function(value, callback) {
this.log('setLockTargetState: ' + value);
this.currentValue.LockTargetState = value;
switch (value) {
case Characteristic.LockTargetState.UNSECURED: target = 'unlock'; break;
case Characteristic.LockTargetState.SECURED: target = 'lock'; break
default:
}
// comment setLockCurrentState and callback and uncomment your code
this.setLockCurrentState(target);
callback();
/* your code
this.log( "Rear door set to: " + target );
request("http://192.168.0.100/tenHsServer/tenHsServer.aspx?t=ab&f=RunEvent&d="+target);
// todo error handling
callback();
*/ // your code end
},
/**
* Accessory Information Identify
*/
identify: function(callback) {
this.log("Identify requested!");
callback();
},
/**
* Services and Characteristics
*/
getServices: function() {
var informationService = new Service.AccessoryInformation();
informationService
.setCharacteristic(Characteristic.Manufacturer, "LM Manufacturer")
.setCharacteristic(Characteristic.Model, "LM Model")
.setCharacteristic(Characteristic.SerialNumber, "LM Serial Number")
.setCharacteristic(Characteristic.Name, this.name);
var LockMechanismService = new Service.LockMechanism();
this.Characteristic.LockCurrentState = LockMechanismService
.getCharacteristic(Characteristic.LockCurrentState)
.on('get', this.getLockCurrentState.bind(this));
this.currentValue.LockCurrentState = Characteristic.LockCurrentState.UNSECURED;
this.Characteristic.LockTargetState = LockMechanismService
.getCharacteristic(Characteristic.LockTargetState)
.on('get', this.getLockTargetState.bind(this))
.on('set', this.setLockTargetState.bind(this));
this.currentValue.LockTargetState = Characteristic.LockTargetState.UNSECURED;
return [informationService, LockMechanismService];
}
};