|
| 1 | +import type { Builder, Command, Describe } from 'landlubber' |
| 2 | + |
| 3 | +import { |
| 4 | + isSeamActionAttemptFailedError, |
| 5 | + isSeamActionAttemptTimeoutError, |
| 6 | + type LocksUnlockDoorResponse, |
| 7 | +} from 'seam' |
| 8 | + |
| 9 | +import type { Handler } from './index.js' |
| 10 | + |
| 11 | +interface Options { |
| 12 | + deviceId: string |
| 13 | +} |
| 14 | + |
| 15 | +export const command: Command = 'unlock deviceId' |
| 16 | + |
| 17 | +export const describe: Describe = 'Unlock a door' |
| 18 | + |
| 19 | +export const builder: Builder = { |
| 20 | + deviceId: { |
| 21 | + type: 'string', |
| 22 | + describe: 'Device id of lock to unlock', |
| 23 | + }, |
| 24 | +} |
| 25 | + |
| 26 | +export const handler: Handler<Options> = async ({ deviceId, seam, logger }) => { |
| 27 | + const device = await seam.devices.get({ device_id: deviceId }) |
| 28 | + |
| 29 | + if (device.can_remotely_unlock == null) { |
| 30 | + throw new Error('This device does not support remote unlocking') |
| 31 | + } |
| 32 | + |
| 33 | + if (!device.can_remotely_unlock) { |
| 34 | + throw new Error('This device cannot be unlocked at this time') |
| 35 | + } |
| 36 | + |
| 37 | + try { |
| 38 | + const actionAttempt = await seam.locks.unlockDoor( |
| 39 | + { |
| 40 | + device_id: deviceId, |
| 41 | + }, |
| 42 | + { waitForActionAttempt: true }, |
| 43 | + ) |
| 44 | + logger.info({ actionAttempt }, 'unlocked') |
| 45 | + } catch (err: unknown) { |
| 46 | + if (isSeamActionAttemptFailedError<UnlockDoorActionAttempt>(err)) { |
| 47 | + logger.info({ err }, 'Could not unlock the door') |
| 48 | + return |
| 49 | + } |
| 50 | + |
| 51 | + if (isSeamActionAttemptTimeoutError<UnlockDoorActionAttempt>(err)) { |
| 52 | + logger.info({ err }, 'Door took too long to unlock') |
| 53 | + return |
| 54 | + } |
| 55 | + |
| 56 | + throw err |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | +// TODO: import type { UnlockDoorActionAttempt } from 'seam' |
| 61 | +type UnlockDoorActionAttempt = LocksUnlockDoorResponse['action_attempt'] |
0 commit comments