-
Notifications
You must be signed in to change notification settings - Fork 0
/
idasen.js
executable file
·75 lines (62 loc) · 2.14 KB
/
idasen.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
#!/usr/bin/env node
import { Command } from 'commander';
import { connect } from './connect.js';
import { setTimeout } from 'node:timers/promises';
import { MIN_HEIGHT, MAX_HEIGHT } from './Desk.js';
import { readFileSync } from 'fs';
import { dirname } from 'path';
import { fileURLToPath } from 'url';
import { scan } from "./scan.js";
const program = new Command();
const __dirname = dirname(fileURLToPath(import.meta.url));
const { version } = JSON.parse(readFileSync(`${__dirname}/package.json`, 'utf-8'));
program
.name('idasen')
.description('CLI to control your Idasen Ikea standing desk')
.version(version);
program.command('scan')
.description('scan bluetooth devices. Use this command to search for your desk uuid')
.action(() => {
scan();
});
const deskUuid = process.env.IDASEN_DESK_UUID;
program.command('height')
.description('Return current height expressed in cm')
.action(async () => {
if (!deskUuid) {
console.error('Unable to find IDASEN_DESK_UUID variable');
process.exit(-1);
}
const [desk, peripheral] = await connect(deskUuid);
const heightCm = await desk.getCurrentHeightCm();
console.log('current height', heightCm);
await peripheral.disconnectAsync();
process.exit();
});
program.command('move-to')
.description('Move desk to target height in cm')
.argument('<target-cm>', 'target height in cm')
.action(async (targetCm) => {
if (!deskUuid) {
console.error('Unable to find IDASEN_DESK_UUID variable');
process.exit(-1);
}
if (targetCm < MIN_HEIGHT) {
console.error(`Cannot move under ${MIN_HEIGHT} cm`);
process.exit(-1);
}
if (targetCm > MAX_HEIGHT) {
console.error(`Cannot move over ${MAX_HEIGHT} cm`);
process.exit(-1);
}
const [desk, peripheral] = await connect(deskUuid);
let heightCm = await desk.getCurrentHeightCm();
console.log(`initial height ${heightCm} cm`);
await desk.moveTo(targetCm);
await setTimeout(500);
heightCm = await desk.getCurrentHeightCm();
console.log(`final height ${heightCm} cm`);
await peripheral.disconnectAsync();
process.exit();
});
program.parse();