Skip to content

Commit 77dc51d

Browse files
authored
Add lock and unlock examples (#69)
* Add lock and unlock examples * Fix type
1 parent e4aa806 commit 77dc51d

File tree

3 files changed

+125
-1
lines changed

3 files changed

+125
-1
lines changed

examples/index.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ import landlubber, {
1313
import { Seam } from 'seam'
1414

1515
import * as devices from './devices.js'
16+
import * as lock from './lock.js'
17+
import * as unlock from './unlock.js'
1618

1719
export type Handler<Options = EmptyOptions> = DefaultHandler<Options, Context>
1820

@@ -22,7 +24,7 @@ interface ClientContext {
2224
seam: Seam
2325
}
2426

25-
const commands = [devices]
27+
const commands = [devices, lock, unlock]
2628

2729
const createAppContext: MiddlewareFunction = async (argv) => {
2830
const apiKey = argv['api-key']

examples/lock.ts

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
import type { Builder, Command, Describe } from 'landlubber'
2+
3+
import {
4+
isSeamActionAttemptFailedError,
5+
isSeamActionAttemptTimeoutError,
6+
type LocksLockDoorResponse,
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 = 'lock deviceId'
16+
17+
export const describe: Describe = 'Lock a door'
18+
19+
export const builder: Builder = {
20+
deviceId: {
21+
type: 'string',
22+
describe: 'Device id of lock to lock',
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_lock == null) {
30+
throw new Error('This device does not support remote locking')
31+
}
32+
33+
if (!device.can_remotely_lock) {
34+
throw new Error('This device cannot be locked at this time')
35+
}
36+
37+
try {
38+
const actionAttempt = await seam.locks.lockDoor(
39+
{
40+
device_id: deviceId,
41+
},
42+
{ waitForActionAttempt: true },
43+
)
44+
logger.info({ actionAttempt }, 'locked')
45+
} catch (err: unknown) {
46+
if (isSeamActionAttemptFailedError<LockDoorActionAttempt>(err)) {
47+
logger.info({ err }, 'Could not unlock the door')
48+
return
49+
}
50+
51+
if (isSeamActionAttemptTimeoutError<LockDoorActionAttempt>(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 { LockDoorActionAttempt } from 'seam'
61+
type LockDoorActionAttempt = LocksLockDoorResponse['action_attempt']

examples/unlock.ts

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
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

Comments
 (0)