forked from oyooyo/keyble
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsend_command.js
125 lines (115 loc) · 4.09 KB
/
send_command.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
#!/usr/bin/env node
/**
* Use "strict" mode.
*/
'use strict';
/**
* The "send_command" submodule.
* Command line tool for controlling (lock/unlock/open) eQ-3 eqiva Bluetooth smart locks.
* @module send_command
*/
/**
* Import/require the "keyble" submodule.
*/
const keyble = require('./keyble');
/**
* Import required functions from the "cli" submodule.
*/
const {ArgumentParser, generate_input_strings} = require('./cli.js');
/**
* The default auto-disconnect time, in seconds.
*/
const DEFAULT_AUTO_DISCONNECT_TIME = 15.0;
/**
* The default status update time, in seconds.
*/
const DEFAULT_STATUS_UPDATE_TIME = 900.0;
/**
* The default timeout time, in seconds.
*/
const DEFAULT_TIMEOUT_TIME = 30.0;
const send_commands_then_exit = async ({address, user_id, user_key, auto_disconnect_time, status_update_time, command, timeout, output_status_updates=true}) => {
try {
const key_ble = new keyble.Key_Ble({
address: address,
user_id: user_id,
user_key: user_key,
auto_disconnect_time: auto_disconnect_time,
status_update_time: status_update_time,
});
key_ble.on((output_status_updates ? 'status_update' : 'status_change'), (lock_state) => {
console.log(JSON.stringify(lock_state));
});
for await (let input_command of generate_input_strings([command], process.stdin)) {
const action = {
lock: key_ble.lock,
unlock: key_ble.unlock,
open: key_ble.open,
status: key_ble.request_status,
}[input_command.toLowerCase()];
if (! action) {
throw new Error(`Unknown command "${command}"`);
}
await keyble.utils.time_limit(action.call(key_ble), (timeout * 1000));
}
// "noble", the Bluetooth library being used, does not properly shut down. An explicit process.exit() is required when finished.
process.exit(0);
} catch (error) {
console.error `Error: ${error}`;
process.exit(1);
}
}
/**
* MAIN
*/
// Only execute the following code when run from the command line
if (require.main == module) {
// Set up the command line arguments parser.
const argument_parser = new ArgumentParser({
description: 'Control (lock/unlock/open) an eQ-3 eqiva Bluetooth smart lock.',
});
argument_parser.add_argument('--address', '-a', {
required: true,
type: String,
help: 'The smart lock\'s MAC address',
});
argument_parser.add_argument('--user_id', '-u', {
required: true,
type: 'int',
help: 'The user ID',
});
argument_parser.add_argument('--user_key', '-k', {
required: true,
type: String,
help: 'The user key',
});
argument_parser.add_argument('--auto_disconnect_time', '-adt', {
type: 'float',
default: DEFAULT_AUTO_DISCONNECT_TIME,
help: `The auto-disconnect time. If connected to the lock, the connection will be automatically disconnected after this many seconds of inactivity, in order to save battery. A value of 0 will deactivate auto-disconnect (default: ${DEFAULT_AUTO_DISCONNECT_TIME})`,
});
argument_parser.add_argument('--status_update_time', '-sut', {
type: 'float',
default: DEFAULT_STATUS_UPDATE_TIME,
help: `The status update time. If no status information has been received for this many seconds, automatically connect to the lock and query the status. A value of 0 will deactivate status updates (default: ${DEFAULT_STATUS_UPDATE_TIME})`,
});
argument_parser.add_argument('--timeout', '-t', {
type: 'float',
default: DEFAULT_TIMEOUT_TIME,
help: `The timeout time. Commands must finish within this many seconds, otherwise there is an error. A value of 0 will deactivate timeouts (default: ${DEFAULT_TIMEOUT_TIME})`,
});
argument_parser.add_argument('--command', '-c', {
choices: ['lock', 'open', 'unlock', 'status'],
required: false,
type: String,
help: 'The command to perform. If not provided on the command line, the command(s) will be read as input lines from STDIN instead',
});
argument_parser.add_argument('--output_status_updates', '-osu', {
required: false,
default: true,
type: Boolean,
help: 'Output status updates as well, not just status changes',
});
// Parse the command line arguments and pass them to the send_commands_then_exit function.
send_commands_then_exit(argument_parser.parse_args());
}