-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.js
150 lines (139 loc) · 3.82 KB
/
main.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
'use strict';
var adapter, initialize, key_ble, keyble, on_command_state, terminate, utils;
// Get common adapter utils
utils = require('@iobroker/adapter-core');
// Create Adapter instance
adapter = new utils.Adapter('keyble');
// Get keyble module
keyble = require('keyble');
// Reference to the device
key_ble = null;
// Called when the instance should be initialized
adapter.on('ready', function() {
adapter.log.info(`Initializing instance using configuration: ${JSON.stringify(adapter.config)}`);
initialize();
});
// Called if a subscribed state changes
adapter.on('stateChange', function(long_id, state) {
var id;
// Warning, state can be null if it was deleted
adapter.log.info(`stateChange ${id}: ${JSON.stringify(state)}`);
// you can use the ack flag to detect if it is status (true) or command (false)
if (state && (!state.ack)) {
id = long_id.slice(adapter.namespace.length + 1);
on_command_state(id, state, state.val, long_id);
}
});
// Called when the instance shall be terminated
// Callback has to be called under any circumstances!
adapter.on('unload', function(callback) {
adapter.log.info("Terminating instance, cleaning up...");
Promise.resolve(terminate()).then(function() {
adapter.log.info("Successfully cleaned up.");
callback();
}).catch(function(error) {
adapter.log.info("Error cleaning up!");
callback();
});
});
initialize = function() {
key_ble = new keyble.Key_Ble({
address: adapter.config.mac_address,
user_id: adapter.config.user_id,
user_key: adapter.config.user_key,
auto_disconnect_time: adapter.config.auto_disconnect_time,
status_update_time: adapter.config.status_update_time
});
key_ble.on('status_change', function(status_id, status_string) {
adapter.setState('active', {
val: status_id === 1,
ack: true
});
adapter.setState('lock_state', {
val: status_id,
ack: true
});
if (status_id !== 1) {
// Lock is not active
adapter.setState('opened', {
val: status_id === 4,
ack: true
});
return adapter.setState('unlocked', {
val: (status_id === 2) || (status_id === 4),
ack: true
});
}
});
// For every state in the system there has to be also an object of type state
// Here a simple keyble for a boolean variable named "testVariable"
// Because every adapter instance uses its own unique namespace variable names can't collide with other adapters variables
adapter.setObjectNotExists('unlocked', {
type: 'state',
common: {
name: 'unlocked',
type: 'boolean',
role: 'switch.lock.door',
write: true
},
native: {}
});
adapter.setObjectNotExists('active', {
type: 'state',
common: {
name: 'active',
type: 'boolean',
role: 'indicator.working',
write: false
},
native: {}
});
adapter.setObjectNotExists('opened', {
type: 'state',
common: {
name: 'opened',
type: 'boolean',
role: 'switch.lock.door',
write: true
},
native: {}
});
adapter.setObjectNotExists('lock_state', {
type: 'state',
common: {
name: 'lock_state',
type: 'number',
role: 'value.lock',
write: false,
states: {
0: 'LOCKED',
1: 'ACTIVE',
2: 'UNLOCKED',
4: 'OPEN'
}
},
native: {}
});
// in this keyble all states changes inside the adapters namespace are subscribed
adapter.subscribeStates('*');
};
on_command_state = function(id, state, value, long_id) {
switch (id) {
case 'unlocked':
if (value) {
key_ble.unlock();
} else {
key_ble.lock();
}
break;
case 'opened':
if (value) {
key_ble.open();
}
}
};
terminate = function() {
key_ble.disconnect();
return key_ble = null;
};
//# sourceMappingURL=main.js.map