-
Notifications
You must be signed in to change notification settings - Fork 103
/
events.ts
176 lines (155 loc) · 5.32 KB
/
events.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
import {
DeviceEvent,
DeviceIntegrationType,
VirtualDeviceEventsResponse,
} from '@smartthings/core-sdk'
import {
APICommand,
chooseDevice,
inputAndOutputItem,
inputProcessor,
stringFromUnknown,
TableGenerator,
} from '@smartthings/cli-lib'
import {
chooseAttribute,
chooseCapability,
chooseComponent,
chooseUnit,
chooseValue,
} from '../../lib/commands/virtualdevices-util'
function buildTableOutput(tableGenerator: TableGenerator, data: EventInputOutput): string {
const { input, output } = data
const table = tableGenerator.newOutputTable({ head: ['Component', 'Capability', 'Attribute', 'Value', 'State Change?'] })
const { stateChanges } = output
for (const index in input) {
const event = input[index]
const isStateChange = parseInt(index) < stateChanges.length ? stateChanges[index] : 'undefined'
const value = stringFromUnknown(event.value)
table.push([event.component, event.capability, event.attribute, value, isStateChange])
}
return table.toString()
}
type EventInputOutput = {
input: DeviceEvent[]
output: VirtualDeviceEventsResponse
}
export default class VirtualDeviceEventsCommand extends APICommand<typeof VirtualDeviceEventsCommand.flags> {
static description = 'create events for a virtual device\n' +
'The command can be run interactively, in question & answer mode, with command line parameters, ' +
'or with input from a file or standard in.'
static examples = [
'$ smartthings virtualdevices:events # interactive mode',
'$ smartthings virtualdevices:events <id> -i data.yml # from a YAML or JSON file',
'$ smartthings virtualdevices:events <id> switch:switch on # command line input',
'$ smartthings virtualdevices:events <id> temperatureMeasurement:temperature 22.5 C # command line input',
]
static flags = {
...APICommand.flags,
...inputAndOutputItem.flags,
}
static args = [
{
name: 'id',
description: 'the device id',
},
{
name: 'name',
description: 'the fully qualified attribute name [<component>]:<capability>:<attribute>',
},
{
name: 'value',
description: 'the attribute value',
},
{
name: 'unit',
description: 'optional unit of measure',
},
]
async run(): Promise<void> {
const deviceId = await chooseDevice(this, this.args.id, { deviceListOptions: { type: DeviceIntegrationType.VIRTUAL } })
const createEvents = async (_: void, input: DeviceEvent[]): Promise<EventInputOutput> => {
const output = await this.client.virtualDevices.createEvents(deviceId, input)
return {
input,
output,
}
}
await inputAndOutputItem<DeviceEvent[], EventInputOutput>(this, {
buildTableOutput: (data: EventInputOutput) => buildTableOutput(this.tableGenerator, data),
}, createEvents, inputProcessor(() => true, () => this.getInputFromUser(deviceId)))
}
async getInputFromUser(deviceId: string): Promise<DeviceEvent[]> {
const attributeName = this.args.name
const attributeValue = this.args.value
let events: DeviceEvent[] = []
if (attributeName) {
if (attributeValue) {
const event = await this.parseDeviceEvent(attributeName, attributeValue, this.args.unit)
events = [event]
} else {
this.error('Attribute name specified without attribute value')
}
} else {
const device = await this.client.devices.get(deviceId)
const component = await chooseComponent(this, device)
const capability = await chooseCapability(this, component)
const { attributeName, attribute } = await chooseAttribute(this, capability)
const value = await chooseValue(this, attribute, attributeName)
const unit = await chooseUnit(this, attribute)
events = [
{
component: component.id,
capability: capability.id,
attribute: attributeName,
value: this.convertAttributeValue(attribute.schema.properties.value.type, value),
unit,
},
]
}
return events
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
convertAttributeValue(attributeType: string | undefined, attributeValue: string): any {
switch (attributeType) {
case 'integer':
return parseInt(attributeValue)
case 'number':
return parseFloat(attributeValue)
case 'array':
case 'object':
return JSON.parse(attributeValue)
default:
return attributeValue
}
}
async parseDeviceEvent(attributeName: string, attributeValue: string, unitOfMeasure: string): Promise<DeviceEvent> {
const segments = attributeName.split(':')
if (segments.length < 2 || segments.length > 3) {
this.error('Invalid attribute name')
}
const event: DeviceEvent = segments.length === 3 ? {
component: segments[0],
capability: segments[1],
attribute: segments[2],
value: attributeValue,
unit: unitOfMeasure,
} : {
component: 'main',
capability: segments[0],
attribute: segments[1],
value: attributeValue,
unit: unitOfMeasure,
}
const capability = await this.client.capabilities.get(event.capability, 1)
if (!capability || !capability.attributes) {
this.error(`Capability ${event.capability} not valid`)
}
const attribute = capability.attributes[event.attribute]
if (!attribute) {
this.error(`Attribute ${event.attribute} not found in capability ${capability.id}`)
}
event.value = this.convertAttributeValue(attribute.schema.properties.value.type, attributeValue)
return event
}
}