-
Notifications
You must be signed in to change notification settings - Fork 112
/
Copy pathhistory.ts
63 lines (54 loc) · 1.65 KB
/
history.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
62
63
import { DeviceActivity } from '@smartthings/core-sdk'
import {
APICommand,
buildOutputFormatter,
calculateOutputFormat,
chooseDevice,
formatAndWriteItem,
IOFormat,
writeOutput,
} from '@smartthings/cli-lib'
import {
calculateRequestLimit,
getHistory,
historyFlags,
maxItemsPerRequest,
toEpochTime,
writeDeviceEventsTable,
} from '../../lib/commands/history-util'
export default class DeviceHistoryCommand extends APICommand<typeof DeviceHistoryCommand.flags> {
static description = 'get device history by device'
static flags = {
...APICommand.flags,
...formatAndWriteItem.flags,
...historyFlags,
}
static args = [{
name: 'id',
description: 'the device id',
}]
async run(): Promise<void> {
const limit = this.flags.limit
const perRequestLimit = calculateRequestLimit(limit)
const deviceId = await chooseDevice(this, this.args.id, { allowIndex: true })
const device = await this.client.devices.get(deviceId)
const params = {
deviceId,
locationId: device.locationId,
limit: perRequestLimit,
before: toEpochTime(this.flags.before),
after: toEpochTime(this.flags.after),
}
if (calculateOutputFormat(this) === IOFormat.COMMON) {
if (limit > perRequestLimit) {
this.log(`History is limited to ${maxItemsPerRequest} items per request.`)
}
const history = await this.client.history.devices(params)
await writeDeviceEventsTable(this, history, { utcTimeFormat: this.flags.utc })
} else {
const items = await getHistory(this.client, limit, perRequestLimit, params)
const outputFormatter = buildOutputFormatter<DeviceActivity[]>(this)
await writeOutput(outputFormatter(items), this.flags.output)
}
}
}