-
Notifications
You must be signed in to change notification settings - Fork 104
/
history.ts
61 lines (52 loc) · 1.62 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
import { DeviceActivity } from '@smartthings/core-sdk'
import {
APICommand,
buildOutputFormatter,
calculateOutputFormat,
formatAndWriteItem,
IOFormat,
writeOutput,
} from '@smartthings/cli-lib'
import {
calculateRequestLimit,
getHistory,
historyFlags,
maxItemsPerRequest,
toEpochTime,
writeDeviceEventsTable,
} from '../../lib/commands/history-util'
import { chooseLocation } from '../locations'
export default class LocationDeviceHistoryCommand extends APICommand<typeof LocationDeviceHistoryCommand.flags> {
static description = 'get device history by location'
static flags = {
...APICommand.flags,
...formatAndWriteItem.flags,
...historyFlags,
}
static args = [{
name: 'id',
description: 'the location id',
}]
async run(): Promise<void> {
const limit = this.flags.limit
const perRequestLimit = calculateRequestLimit(limit)
const id = await chooseLocation(this, this.args.id, true, true)
const params = {
locationId: id,
limit: this.flags.limit,
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, { includeName: true, 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)
}
}
}