-
Notifications
You must be signed in to change notification settings - Fork 53
/
rules.ts
417 lines (352 loc) · 10.3 KB
/
rules.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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
import { Endpoint } from '../endpoint'
import { EndpointClient, EndpointClientConfig } from '../endpoint-client'
export type ConditionAggregationMode = 'Any' | 'All'
export interface SimpleCondition {
left: RuleOperand
right: RuleOperand
aggregation?: ConditionAggregationMode
}
export interface BetweenCondition {
value: RuleOperand
start: RuleOperand
end: RuleOperand
aggregation?: ConditionAggregationMode
}
/**
* A condition that returns true when its evaluation resolves to true and the previous evaluation
* resolved to false.
*/
export interface ChangesCondition extends BasicCondition {
id: string
operand?: RuleOperand
}
/**
* A condition that returns true if its evaluation would return true within the specified duration.
*/
export interface WasCondition extends BasicCondition {
id: string
operand?: RuleOperand
duration: RuleInterval
}
/**
* A condition that returns true if its evaluation is true after the specified duration.
*/
export interface RemainsCondition extends BasicCondition {
id: string
duration: RuleInterval
operand?: RuleOperand
}
export interface BasicCondition {
and?: RuleCondition[]
or?: RuleCondition[]
not?: RuleCondition
equals?: SimpleCondition
greaterThan?: SimpleCondition
greaterThanOrEquals?: SimpleCondition
lessThan?: SimpleCondition
lessThanOrEquals?: SimpleCondition
between?: BetweenCondition
}
export interface RuleCondition {
and?: RuleCondition[]
or?: RuleCondition[]
not?: RuleCondition
equals?: SimpleCondition
greaterThan?: SimpleCondition
greaterThanOrEquals?: SimpleCondition
lessThan?: SimpleCondition
lessThanOrEquals?: SimpleCondition
between?: BetweenCondition
changes?: ChangesCondition
remains?: RemainsCondition
was?: WasCondition
}
export type TimeReference = 'Now' | 'Midnight' | 'Sunrise' | 'Noon' | 'Sunset'
export type DateReference = 'Today'
export type OperandAggregationMode = 'None'
export type TriggerMode = 'Auto' | 'Always' | 'Never'
export interface ArrayOperand {
operands: RuleOperand[]
aggregation?: OperandAggregationMode
}
export interface MapOperand {
[name: string]: RuleOperand | undefined
}
export interface DeviceOperand {
devices: string[]
component: string
capability: string
attribute: string
path?: string
aggregation?: OperandAggregationMode
trigger?: TriggerMode
}
export type LocationAttribute = 'FineDust' | 'FineDustIndex' | 'Humidity' | 'Mode' | 'Security' | 'Temperature' | 'TemperatureC' | 'TemperatureF' | 'UltraFineDust' | 'UltraFineDustIndex' | 'Weather' | 'WeatherAlertSeverity'
export type DayOfWeek = 'Sun' | 'Mon' | 'Tue' | 'Wed' | 'Thu' | 'Fri' | 'Sat'
export interface LocationOperand {
/**
* Required for User level rule, optional for Location level in request. Will always be present
* in response for both.
*/
locationId?: string
attribute: LocationAttribute
trigger?: TriggerMode
}
export interface DateOperand {
/**
* A java time zone ID reference.
*/
timeZoneId?: string
daysOfWeek?: DayOfWeek[]
year?: number
month?: number
day?: number
reference?: DateReference
}
export interface TimeOperand {
/**
* A java time zone ID reference.
*/
timeZoneId?: string
daysOfWeek?: DayOfWeek[]
/**
* default: Midnight
*/
reference: TimeReference
offset?: RuleInterval
}
export interface DateTimeOperand {
/**
* A java time zone ID reference.
*/
timeZoneId?: string
/**
* Location ID for location actions.
*/
locationId?: string
daysOfWeek?: DayOfWeek[]
year?: number
month?: number
day?: number
/**
* default: Midnight
*/
reference: TimeReference
offset?: RuleInterval
}
export interface RuleOperand {
'boolean'?: boolean
decimal?: number
integer?: number
string?: string
array?: ArrayOperand
map?: MapOperand
device?: DeviceOperand
location?: LocationOperand
date?: DateOperand
time?: TimeOperand
datetime?: DateTimeOperand
}
export type IntervalUnit = 'Second' | 'Minute' | 'Hour' | 'Day' | 'Week' | 'Month' | 'Year'
export interface RuleInterval {
value: RuleOperand
unit: IntervalUnit
}
export interface DeviceCommand {
/**
* The name of the component on this device, default is 'main'. The
* component must be valid for the device.
*/
component: string
/**
* Capability that this command relates to. This must be a capability of the
* component.
*/
capability: string
/**
* Name of the command, this must be valid for the capability.
*/
command: string
/**
* Arguments of the command. All the required arguments defined in the
* capability's command argument definition must be provided. The type of
* the arguments are dependent on the type of the capability's command
* argument. Please refer to the capabilities definition at
* https://smartthings.developer.samsung.com/develop/api-ref/capabilities.html
*/
arguments?: RuleOperand[]
}
export interface IfActionSequence{
then?: RuleSequence
'else'?: RuleSequence
}
export interface IfAction extends RuleCondition {
then?: RuleAction[]
'else'?: RuleAction[]
/**
* The sequence in which the actions are to be executed.
*/
sequence?: IfActionSequence
}
export interface SleepAction {
duration: RuleInterval
}
export interface CommandSequence {
commands?: RuleSequence
devices?: RuleSequence
}
export interface CommandAction {
devices: string[]
commands: DeviceCommand[]
sequence?: CommandSequence
}
export interface EveryAction {
interval?: RuleInterval
specific?: DateTimeOperand
actions: RuleAction[]
sequence?: RuleActionSequence
}
export interface LocationAction {
/**
* locationId is required for "User level rule". (It's optional for "Location level rule".)
*
* <^(?:([0-9a-fA-F]{32})|([0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}))$>
*/
locationId: string
mode?: string
}
export type RuleAction = { 'if': IfAction } | { sleep: SleepAction } | { command: CommandAction } | { every: EveryAction } | { location: LocationAction }
export type RuleSequence = 'Serial' | 'Parallel'
export interface RuleActionSequence {
actions?: RuleSequence
}
export interface RuleRequest {
/**
* The name for the Rule. Limit 100 characters.
*/
name: string
actions: RuleAction[]
/**
* The sequence in which the actions are to be executed (i.e. Serial (default) or Parallel).
*/
sequence?: RuleActionSequence
/**
* Time zone ID for this rule. This overrides the location time zone ID,
* but is overridden by time zone ID provided by each operand individually.
*/
timeZoneId?: string
}
export type RuleOwnerType = 'Location' | 'User'
export type RuleStatus = 'Enabled' | 'Disabled'
export type RuleExecutionLocation = 'Cloud' | 'Local'
export type RuleCreator = 'SMARTTHINGS' | 'ARB' | 'RECIPE' | 'UNDEFINED'
export interface Rule extends RuleRequest {
/**
* Unique id for the rule.
*/
id: string
ownerType: RuleOwnerType
ownerId: string
dateCreated: string
dateUpdated: string
status?: RuleStatus
executionLocation?: RuleExecutionLocation
creator?: RuleCreator
}
export type ExecutionResult = 'Success' | 'Failure' | 'Ignored'
export type IfExecutionResult = 'True' | 'False'
export interface IfActionExecutionResult {
result: IfExecutionResult
}
/**
* The result of a location action execution.
*/
export interface LocationActionExecutionResult {
result: ExecutionResult
locationId: string
}
export type CommandExecutionResult = 'Success' | 'Failure' | 'Offline'
export interface CommandActionExecutionResult {
result: CommandExecutionResult
deviceId: string
}
export interface SleepActionExecutionResult {
result: ExecutionResult
}
export interface ActionExecutionResult {
actionId: string
if?: IfActionExecutionResult
location?: LocationActionExecutionResult
command?: CommandActionExecutionResult[]
sleep?: SleepActionExecutionResult
}
/**
* The result of a Rule execution.
*/
export interface RuleExecutionResponse {
executionId: string
id: string
result: ExecutionResult
actions?: ActionExecutionResult[]
}
export class RulesEndpoint extends Endpoint {
constructor(config: EndpointClientConfig) {
super(new EndpointClient('rules', config))
}
/**
* List the rules for a location and the access token principal. The principal is the user in the case of a
* PAT (personal access) token or the installed app in the case of a SmartApp token. The rules belonging to one
* principal cannot see the rules belonging to another principal.
* @param locationId UUID of the location, If the client is configured with a location ID this parameter
* can be omitted
*/
public list(locationId?: string): Promise<Rule[]> {
return this.client.getPagedItems<Rule>(undefined, { locationId: this.locationId(locationId) })
}
/**
* Get a specific rule
* @param id UUID of the rule
* @param locationId UUID of the location, If the client is configured with a location ID this parameter
* can be omitted
*/
public get(id: string, locationId?: string): Promise<Rule> {
return this.client.get<Rule>(id, { locationId: this.locationId(locationId) })
}
/**
* Delete a specific rule
* @param id UUID of the rule
* @param locationId UUID of the location, If the client is configured with a location ID this parameter
* can be omitted
*/
public async delete(id: string, locationId?: string): Promise<Rule> {
return this.client.delete(id, { locationId: this.locationId(locationId) })
}
/**
* Create a rule
* @param data the rule definition
* @param locationId UUID of the location, If the client is configured with a location ID this parameter
* can be omitted
*/
public create(data: RuleRequest, locationId?: string): Promise<Rule> {
return this.client.post(undefined, data, { locationId: this.locationId(locationId) })
}
/**
* Update a rule
* @param id UUID of the rule
* @param data the new rule definition
* @param locationId UUID of the location, If the client is configured with a location ID this parameter
* can be omitted
*/
public update(id: string, data: RuleRequest, locationId?: string): Promise<Rule> {
return this.client.put(id, data, { locationId: this.locationId(locationId) })
}
/**
* Execute a rule's actions
* @param id UUID of the rule
* @param locationId UUID of the location, If the client is configured with a location ID this parameter
* can be omitted
*/
public async execute(id: string, locationId?: string): Promise<RuleExecutionResponse> {
return this.client.post(`execute/${id}`, undefined, { locationId: this.locationId(locationId) })
}
}