-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathcloud.d.ts
218 lines (211 loc) · 7.17 KB
/
cloud.d.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
/**
* @license
* Copyright 2020 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
// Minimum TypeScript Version: 3.0
/** Declares the request and response JSON for cloud intents. */
declare namespace smarthome {
/**
* `smarthome.IntentFlow` is a namespace that encapsulates all
* intent request and response objects.
*/
namespace IntentFlow {
interface CloudRequest<P> {
requestId: string;
inputs: Array<{intent: Intents; payload: P;}>;
/** @deprecated use [[DeviceManager.getRegisteredDevices]] */
devices?: RegisteredDevice[];
}
interface DeviceMetadata {
/** Device ID provided in the `SYNC` response */
id: string;
/** Custom data provided in the `SYNC` response */
customData?: unknown;
}
interface QueryRequestPayload {
/** List of devices to query */
devices: DeviceMetadata[];
/** @hidden */
structureData: unknown;
}
/**
* Request passed to the application's `QUERY` intent handler,
* containing a list of device IDs to report state.
*
* See [[QueryHandler]] for more details.
*/
type QueryRequest = CloudRequest<QueryRequestPayload>;
interface ExecuteRequestExecution {
/** Device trait to be updated */
command: string;
/** New state values for the trait attributes */
params: unknown;
}
interface ExecuteRequestCommands {
/** List of devices to update */
devices: DeviceMetadata[];
/** List of traits to update */
execution: ExecuteRequestExecution[];
}
interface ExecuteRequestPayload {
/** List of commands to execute */
commands: ExecuteRequestCommands[];
/** @hidden */
structureData: unknown;
}
/**
* Request passed to the application's `EXECUTE` intent handler,
* containing a list of commands and target device IDs to be updated.
*
* See [[ExecuteHandler]] for more details.
*/
type ExecuteRequest = CloudRequest<ExecuteRequestPayload>;
/**
* Response status codes for `EXECUTE` intent requests.
*/
type ExecuteStatus = 'SUCCESS'|'PENDING'|'OFFLINE'|'ERROR'|'EXCEPTIONS';
/**
* For a list of the supported `EXECUTE` error codes, see
* [Errors and exceptions](/assistant/smarthome/reference/errors-exceptions)
*/
type ExecuteErrors = string;
interface CloudResponse<P> {
requestId: string;
payload: P;
}
/**
* Content of the [[QueryResponse]] returned by the application's
* `QUERY` intent handler.
*/
interface QueryPayload {
devices: unknown;
}
/**
* Response returned by the application's `QUERY` intent handler.
*
* See [[QueryHandler]] for more details.
*/
type QueryResponse = CloudResponse<QueryPayload>;
interface ExecuteResponseCommands {
/** List of affected device ids. */
ids: string[];
/** Response status code of the commands sent to these devices. */
status: ExecuteStatus;
/**
* If status is set to `ERROR` or `EXCEPTIONS`,
* the cause of the error.
*/
errorCode?: ExecuteErrors;
/**
* If status is set to `ERROR` or `EXCEPTIONS`,
* a human readable description of the error.
*/
debugString?: string;
/** Per-trait state values after execution is complete. */
states?: unknown;
}
/**
* Content of the [[ExecuteResponse]] returned by the application's
* `EXECUTE` intent handler.
*/
interface ExecutePayload {
/** List of command response details */
commands: ExecuteResponseCommands[];
/** Error code for the entire command transaction */
errorCode?: ExecuteErrors;
/** Human readable description of any errors present */
debugString?: string;
}
/**
* Container for the status of the commands that the local app received
* in an `EXECUTE` intent.
*
* Use [[Response.Builder]] to create an [[ExecuteResponse]] instance and
* set the corresponding status for each target device ID present in
* the [[ExecuteRequest]].
*
* ```
* const response = new Execute.Response.Builder()
* .setRequestId(request.requestId);
*
* const result = localHomeApp.getDeviceManager()
* .send(deviceCommand)
* .then((result) => {
* // Handle command success
* response.setSuccessState(device.id, state);
* })
* .catch((err: IntentFlow.HandlerError) => {
* // Handle command error
* response.setErrorState(device.id, err.errorCode);
* });
*
* return result.then(() => response.build());
* ```
*
* See [[ExecuteHandler]] for more details.
*/
type ExecuteResponse = CloudResponse<ExecutePayload>;
/**
* Callback registered with the [[App]] via [[App.onQuery]] to process
* requests for current device state.
*/
interface QueryHandler extends IntentHandler<QueryRequest, QueryResponse> {}
/**
* Callback registered with the [[App]] via [[App.onExecute]] to process
* requests to update device state.
*
* To support local fulfillment, the local home platform must first
* establish a local fulfillment path. For more details, see the
* [developer guide](/assistant/smarthome/develop/local).
*
* ```typescript
* const executeHandler = (request: IntentFlow.ExecuteRequest):
* Promise<IntentFlow.ExecuteResponse> => {
*
* // Extract command(s) and device target(s) from request
* const command = request.inputs[0].payload.commands[0];
* const execution = command.execution[0];
*
* const response = new Execute.Response.Builder()
* .setRequestId(request.requestId);
*
* const result = command.devices.map((device) => {
* // Construct a local device command
* const deviceCommand = new DataFlow.TcpRequestData();
* // ...
*
* // Send command to the local device
* return localHomeApp.getDeviceManager()
* .send(deviceCommand)
* .then((result) => {
* response.setSuccessState(result.deviceId, state);
* })
* .catch((err: IntentFlow.HandlerError) => {
* err.errorCode = err.errorCode || "invalid_request";
* response.setErrorState(device.id, err.errorCode);
* });
* });
*
* // Respond once all commands complete
* return Promise.all(result)
* .then(() => response.build());
* };
* ```
*
*/
interface ExecuteHandler extends
IntentHandler<ExecuteRequest, ExecuteResponse> {}
}
}