-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlock.ts
61 lines (48 loc) · 1.49 KB
/
lock.ts
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
import type { Builder, Command, Describe } from 'landlubber'
import {
isSeamActionAttemptFailedError,
isSeamActionAttemptTimeoutError,
type LocksLockDoorResponse,
} from 'seam'
import type { Handler } from './index.js'
interface Options {
deviceId: string
}
export const command: Command = 'lock deviceId'
export const describe: Describe = 'Lock a door'
export const builder: Builder = {
deviceId: {
type: 'string',
describe: 'Device id of lock to lock',
},
}
export const handler: Handler<Options> = async ({ deviceId, seam, logger }) => {
const device = await seam.devices.get({ device_id: deviceId })
if (device.can_remotely_lock == null) {
throw new Error('This device does not support remote locking')
}
if (!device.can_remotely_lock) {
throw new Error('This device cannot be locked at this time')
}
try {
const actionAttempt = await seam.locks.lockDoor(
{
device_id: deviceId,
},
{ waitForActionAttempt: true },
)
logger.info({ actionAttempt }, 'locked')
} catch (err: unknown) {
if (isSeamActionAttemptFailedError<LockDoorActionAttempt>(err)) {
logger.info({ err }, 'Could not unlock the door')
return
}
if (isSeamActionAttemptTimeoutError<LockDoorActionAttempt>(err)) {
logger.info({ err }, 'Door took too long to unlock')
return
}
throw err
}
}
// TODO: import type { LockDoorActionAttempt } from 'seam'
type LockDoorActionAttempt = LocksLockDoorResponse['action_attempt']