-
Notifications
You must be signed in to change notification settings - Fork 0
/
terminal.js
67 lines (56 loc) · 1.85 KB
/
terminal.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
import TriggerBox13 from './lib/boxes/TriggerBox13.js'
import readline from 'readline'
import { listPossibleTriggerBoxes } from './lib/other/triggerBoxFactory.js'
// Check command line arguments and print usage if incorrect
if (process.argv.length < 3) {
console.log(`Usage: node ${process.argv[1]} <serial port name>`)
// Get list of trigger boxes
const devices = await listPossibleTriggerBoxes()
if (devices.length < 1) {
console.error('No trigger boxes detected')
} else {
console.log('\nPossible trigger boxes:')
devices.forEach(device => console.log(` - "${device}"`))
}
// Do not continue
process.exit(1)
}
async function main (PORT_PATH) {
// Initialize readline input interface
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
})
rl.setPrompt(`${PORT_PATH} > `)
// Initialize trigger box object
console.log(`** Connecting to ${PORT_PATH} **`)
const triggerBox = new TriggerBox13(PORT_PATH)
triggerBox.on('ready', () => {
// Print connection string
console.log('Connected to trigger box ' + triggerBox.boxId + ' in ' + triggerBox.mode + ' mode on port "' + PORT_PATH + '"')
// Show responses and then re-prompt
// triggerBox.on('data', (response) => {
// console.log(' ->' + response + '\n')
// rl.prompt()
// })
// Exit after close
triggerBox.on('close', () => {
console.log('Bye')
process.exit(0)
})
// Show initial prompt
rl.prompt()
})
// Handle input from the user
rl.on('line', async (input) => {
// Special close command
if (input.toLowerCase() === 'close') {
triggerBox.close()
} else {
const response = await triggerBox.sendCommand(input, (input !== 'S' ? 5000 : 0))
console.log(`[${response || '<no response expected>'}]`)
rl.prompt()
}
})
}
main(process.argv[2])