-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathkeyble-mqtt.js
More file actions
290 lines (261 loc) · 8.71 KB
/
Copy pathkeyble-mqtt.js
File metadata and controls
290 lines (261 loc) · 8.71 KB
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
#!/usr/bin/env node
'use strict';
// Required imports
const {ArgumentParser:Argument_Parser, ArgumentDefaultsHelpFormatter:Argument_Defaults_Help_Formatter} = require('argparse');
const {Key_Ble, utils:{canonicalize_hex_string, canonicalize_mac_address}} = require('keyble');
const {connect:connect_to_mqtt_broker} = require('mqtt');
// Default values
const DEFAULT_HOST = '127.0.0.1';
const DEFAULT_PORT = 1883;
const DEFAULT_USERNAME = undefined;
const DEFAULT_PASSWORD = undefined;
const DEFAULT_CLIENT_ID = 'keyble-{canonical_address}';
const DEFAULT_ROOT_TOPIC = 'keyble/{canonical_address}';
const DEFAULT_DEVICE_ROOT_TOPIC = '{root_topic}';
const DEFAULT_DEVICE_COMMAND_TOPIC = '{device_root_topic}/command';
const DEFAULT_DEVICE_STATUS_TOPIC = '{device_root_topic}/status';
const DEFAULT_CLIENT_ROOT_TOPIC = '{root_topic}/client';
const DEFAULT_CLIENT_STATUS_TOPIC = '{client_root_topic}/status';
const DEFAULT_ONLINE_MESSAGE = 'online';
const DEFAULT_OFFLINE_MESSAGE = 'offline';
const DEFAULT_QOS = 0;
const DEFAULT_KEEPALIVE = 60;
// Functions
const format_string = (string, values) =>
((typeof(string) === 'string') ? string.replace(/{(?<value_id>[A-Za-z0-9 _\-]+)}/g, ((match, value_id) => values[value_id])) : string)
const connect_to_mqtt_broker_async = (options) => new Promise((resolve, reject) => {
const mqtt_client = connect_to_mqtt_broker(options);
mqtt_client.once('connect', (connack) => {
resolve(mqtt_client);
});
mqtt_client.once('error', (error) => {
reject(error);
});
})
const publish_mqtt_message_async = (mqtt_client, topic, message, options) => new Promise((resolve, reject) => {
mqtt_client.publish(topic, message, options, (error) => {
if (error) {
reject(error);
} else {
resolve();
}
});
})
const subscribe_to_mqtt_topics_async = (mqtt_client, topic_array, options) => new Promise((resolve, reject) => {
mqtt_client.subscribe(topic_array, options, (error, granted) => {
if (error) {
reject(error);
} else {
resolve(granted);
}
});
})
const run_mqtt_client = async (address, user_id, user_key, {
host=DEFAULT_HOST,
port=DEFAULT_PORT,
online_message=DEFAULT_ONLINE_MESSAGE,
offline_message=DEFAULT_OFFLINE_MESSAGE,
qos=DEFAULT_QOS,
keepalive=DEFAULT_KEEPALIVE,
username:username_pattern=DEFAULT_USERNAME,
password:password_pattern=DEFAULT_PASSWORD,
client_id:client_id_pattern=DEFAULT_CLIENT_ID,
root_topic:root_topic_pattern=DEFAULT_ROOT_TOPIC,
device_root_topic:device_root_topic_pattern=DEFAULT_LOCK_ROOT_TOPIC,
device_command_topic:device_command_topic_pattern=DEFAULT_LOCK_COMMAND_TOPIC,
device_status_topic:device_status_topic_pattern=DEFAULT_LOCK_STATUS_TOPIC,
client_root_topic:client_root_topic_pattern=DEFAULT_CLIENT_ROOT_TOPIC,
client_status_topic:client_status_topic_pattern=DEFAULT_CLIENT_STATUS_TOPIC,
}) => {
const canonical_address = canonicalize_mac_address(address);
const short_address = canonicalize_hex_string(canonical_address);
const values_1 = {
host: host,
port: port,
online_message: online_message,
offline_message: offline_message,
qos: qos,
keepalive: keepalive,
address: address,
canonical_address: canonical_address,
short_address: short_address,
};
const username = format_string(username_pattern, values_1);
const password = format_string(password_pattern, values_1);
const client_id = format_string(client_id_pattern, values_1);
const root_topic = format_string(root_topic_pattern, values_1);
const values_2 = {...values_1,
username: username,
password: password,
client_id: client_id,
root_topic: root_topic,
};
const device_root_topic = format_string(device_root_topic_pattern, values_2);
const client_root_topic = format_string(client_root_topic_pattern, values_2);
const values_3 = {...values_2,
device_root_topic: device_root_topic,
client_root_topic: client_root_topic,
};
const device_command_topic = format_string(device_command_topic_pattern, values_3);
const device_status_topic = format_string(device_status_topic_pattern, values_3);
const client_status_topic = format_string(client_status_topic_pattern, values_3);
const mqtt_client = await connect_to_mqtt_broker_async({
servers: [{host:host, port:port}],
keepalive: keepalive,
clientId: client_id,
username: username,
password: password,
will: {
topic: client_status_topic,
payload: offline_message,
qos: qos,
retain: true,
},
});
const keyble_device = new Key_Ble({
address: canonical_address,
user_id: user_id,
user_key: user_key,
});
keyble_device.on('status_update', async (lock_state) => {
await publish_mqtt_message_async(mqtt_client, device_status_topic, JSON.stringify(lock_state), {
qos: qos,
retain: false,
});
});
mqtt_client.on('message', async (topic, message_buffer) => {
const message = message_buffer.toString().trim();
switch(topic) {
case device_command_topic:
switch(message.toLowerCase()) {
case 'lock':
await keyble_device.lock();
break;
case 'unlock':
await keyble_device.unlock();
break;
case 'open':
await keyble_device.open();
break;
case 'toggle':
await keyble_device.toggle();
break;
default:
// TODO handle or ignore invalid/unknown command?
break;
}
break;
default:
// TODO handle or ignore invalid/unknown topic?
break;
}
});
subscribe_to_mqtt_topics_async(mqtt_client, [device_command_topic], {
qos: qos,
});
await publish_mqtt_message_async(mqtt_client, client_status_topic, online_message, {
qos: qos,
retain: true,
});
}
// Main
if (require.main == module) {
const argument_parser = new Argument_Parser({
description: 'MQTT client for controlling eQ-3 eqiva bluetooth smart locks',
formatter_class: Argument_Defaults_Help_Formatter,
});
argument_parser.add_argument('address', {
help: 'The smart lock\'s MAC address (a string with exactly 12 hexadecimal characters)',
type: 'str',
});
argument_parser.add_argument('user_id', {
help: 'The user ID (an integer number)',
type: 'int',
});
argument_parser.add_argument('user_key', {
help: 'The user key (a string with exactly 32 hexadecimal characters)',
type: 'str',
});
argument_parser.add_argument('--host', {
help: 'The IP address of the broker',
type: 'str',
default: DEFAULT_HOST,
});
argument_parser.add_argument('--port', {
help: 'The port number of the broker',
type: 'int',
default: DEFAULT_PORT,
});
argument_parser.add_argument('--username', {
help: 'The username to be used for authenticating with the broker',
type: 'str',
default: DEFAULT_USERNAME,
});
argument_parser.add_argument('--password', {
help: 'The password to be used for authenticating with the broker',
type: 'str',
default: DEFAULT_PASSWORD,
});
argument_parser.add_argument('--client_id', {
help: 'The client ID to be used for authenticating with the broker',
type: 'str',
default: DEFAULT_CLIENT_ID,
});
argument_parser.add_argument('--root_topic', {
help: 'The "root" topic',
type: 'str',
default: DEFAULT_ROOT_TOPIC,
});
argument_parser.add_argument('--device_root_topic', {
help: 'The "device root" topic',
type: 'str',
default: DEFAULT_DEVICE_ROOT_TOPIC,
});
argument_parser.add_argument('--device_command_topic', {
help: 'The "device command" topic',
type: 'str',
default: DEFAULT_DEVICE_COMMAND_TOPIC,
});
argument_parser.add_argument('--device_status_topic', {
help: 'The "device status" topic',
type: 'str',
default: DEFAULT_DEVICE_STATUS_TOPIC,
});
argument_parser.add_argument('--client_root_topic', {
help: 'The "client root" topic',
type: 'str',
default: DEFAULT_CLIENT_ROOT_TOPIC,
});
argument_parser.add_argument('--client_status_topic', {
help: 'The "client status" topic',
type: 'str',
default: DEFAULT_CLIENT_STATUS_TOPIC,
});
argument_parser.add_argument('--online_message', {
help: 'The (retained) message that will be published to the status topic when the client is online',
type: 'str',
default: DEFAULT_ONLINE_MESSAGE,
});
argument_parser.add_argument('--offline_message', {
help: 'The (retained) message that will be published to the status topic when the client is offline',
type: 'str',
default: DEFAULT_OFFLINE_MESSAGE,
});
argument_parser.add_argument('--qos', {
help: 'The quality of service (QOS) level to use',
type: 'int',
choices: [0, 1, 2],
default: DEFAULT_QOS,
});
argument_parser.add_argument('--keepalive', {
help: 'The number of seconds between sending PING commands to the broker for the purposes of informing it we are still connected and functioning (0=disabled)',
type: 'int',
default: DEFAULT_KEEPALIVE,
});
const {address, user_id, user_key, ...options} = argument_parser.parse_args();
run_mqtt_client(address, user_id, user_key, options)
.catch((error) => {
console.error(`Error: ${error.message}`);
process.exit(1);
});
}