-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathcmd.d.ts
339 lines (324 loc) · 10.9 KB
/
cmd.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
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
/**
* @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 DataFlow namespace that provides interface to send commands to
* platform using DeviceManager send API.
*/
declare namespace smarthome {
/**
* Request and response interfaces for communicating with local devices
* over BLE, TCP, UDP, and HTTP.
* @preferred
*/
namespace DataFlow {
interface BleOptions {
characteristicUuid: string;
serviceUuid: string;
operation: Constants.BleOperation;
waitForNotification?: boolean;
mtu?: number;
}
interface BleRequestData extends Command, BleOptions {}
/** For sending BLE commands using DeviceManager.send API. */
class BleRequestData implements BleRequestData {}
interface BleResponse {
value: string;
characteristicUuid: string;
serviceUuid: string;
}
/** Describes the response for a BLE Request. */
interface BleResponseData extends CommandSuccess {
bleResponse: BleResponse;
}
interface HttpOptions {
/** HTTP Content-Type header */
dataType: string;
/**
* List of HTTP headers for the request.
* @deprecated use [[HttpRequestData.additionalHeaders]]
*/
headers: string;
/**
* Additional HTTP headers for the request, as an object containing
* key/value pairs.
* ```
* {
* 'Authorization': 'Bearer ...',
* 'Accept': 'application/json',
* }
* ```
*/
additionalHeaders: {[key: string]: string};
/** @hidden @deprecated True to send using HTTPS, false for HTTP */
isSecure?: boolean;
/** HTTP method to perform */
method: Constants.HttpOperation;
/** URI path on the target device */
path: string;
/** Port number on the target device. Default is port 80. */
port?: number;
}
/** Content of an HTTP response */
interface HttpResponse {
/** HTTP response code */
statusCode: number;
/** HTTP response body */
body: unknown;
}
/** @hidden */
interface Template {}
/** @hidden Supported Cipher Suites */
type CipherSuites = 'EC-JPAKE';
interface TcpOptions {
/** @hidden True to enable TLS for this request */
isSecure?: boolean;
/** Port number on the target device */
port: number;
/** TCP operation to perform */
operation: Constants.TcpOperation;
/** Hostname on the target device */
hostname?: string;
/** For read requests, number of expected bytes */
bytesToRead?: number;
/** @hidden Cipher suite to be used for TLS */
cipher?: CipherSuites;
/** @hidden Short code needed with EC-JPAKE */
shortCode?: string;
}
/** Content of a TCP response */
interface TcpResponse {
/** Hex-encoded payload received from the device. */
data: string;
}
interface UdpOptions {
/** Port number on the target device */
port: number;
/**
* Expected number of UDP response packets. Actual number of packets
* received in the [[UdpResponseData]] may not match expected value if
* timeout of 1 second is exceeded.
*/
expectedResponsePackets?: number;
}
/** Content of a UDP response */
interface UdpResponse {
/** Array of hex-encoded packets received from the device. */
responsePackets?: string[];
}
interface CommandBase {
/** Request ID from the associated `EXECUTE` intent. */
requestId: string;
/** Device ID of the target device. */
deviceId: string;
/** Protocol to use when sending this command */
protocol: Constants.Protocol;
}
interface Command extends CommandBase {
/** Payload sent to the target device */
data: string;
/** @hidden Experimental feature, to apply template parameters to data */
template?: Template;
}
interface HttpRequestData extends Command, HttpOptions {}
interface TcpRequestData extends Command, TcpOptions {}
interface UdpRequestData extends Command, UdpOptions {}
/**
* Request to send a local device command over HTTP.
* Commands are sent to the device using [[DeviceManager.send]].
*
* Example [[GET]] request:
* ```typescript
* const command = new DataFlow.HttpRequestData();
* command.requestId = request.requestId;
* command.deviceId = 'device-id';
* command.method = Constants.HttpOperation.GET;
* command.port = 80;
* command.path = '/endpoint/control?state=on';
* ```
*
* Example [[POST]] request:
* ```typescript
* const postData = {
* on: true,
* };
*
* const command = new DataFlow.HttpRequestData();
* command.requestId = request.requestId;
* command.deviceId = 'device-id';
* command.method = Constants.HttpOperation.POST;
* command.port = 80;
* command.path = '/endpoint/control';
* command.dataType = 'application/json';
* command.data = JSON.stringify(postData);
* ```
*
* ## Handle the response
*
* If the command succeeds, [[DeviceManager.send]] returns an
* [[HttpResponseData]] result, which contains the [[HttpResponse]].
*
* ```typescript
* const command = new DataFlow.HttpRequestData();
* ...
*
* localHomeApp.getDeviceManager()
* .send(command)
* .then((result: DataFlow.CommandSuccess) => {
* const httpResult = result as DataFlow.HttpResponseData;
* const responseBody = httpResult.httpResponse.body;
* })
* .catch((err: IntentFlow.HandlerError) => {
* // Handle command error
* });
* ```
*/
class HttpRequestData implements HttpRequestData {}
/**
* Request to send a local device command over TCP sockets.
* Commands are sent to the device using [[DeviceManager.send]].
*
* Example TCP command:
* ```typescript
* const payload = new Uint8Array([0x00, 0xFF, 0x00, 0xFF]);
*
* const command = new DataFlow.TcpRequestData();
* command.requestId = request.requestId;
* command.deviceId = 'device-id';
* command.port = 5555;
* command.operation = Constants.TcpOperation.WRITE;
* command.data = Buffer.from(payload).toString('hex');
* ```
*
* ## Handle the response
*
* If the command succeeds, [[DeviceManager.send]] returns a
* [[TcpResponseData]] result. For [[TcpOperation.READ]] commands, the
* result contains a [[TcpResponse]].
*
* ```typescript
* const command = new DataFlow.TcpRequestData();
* command.operation = Constants.TcpOperation.READ;
* ...
*
* localHomeApp.getDeviceManager()
* .send(command)
* .then((result: DataFlow.CommandSuccess) => {
* const tcpResult = result as DataFlow.TcpResponseData;
* const response = tcpResult.tcpResponse.data;
* })
* .catch((err: IntentFlow.HandlerError) => {
* // Handle command error
* });
* ```
*/
class TcpRequestData implements TcpRequestData {}
/**
* Request to send a local device command over UDP datagram.
* Commands are sent to the device using [[DeviceManager.send]].
*
* Example UDP command:
* ```typescript
* const payload = new Uint8Array([0x00, 0xFF, 0x00, 0xFF]);
*
* const command = new DataFlow.UdpRequestData();
* command.requestId = request.requestId;
* command.deviceId = 'device-id';
* command.port = 5555;
* command.data = Buffer.from(payload).toString('hex');
* command.expectedResponsePackets = 1;
* ```
*
* ## Handle the response
*
* If the command succeeds, [[DeviceManager.send]] returns a
* [[UdpResponseData]] result. For commands with
* [[UdpRequestData.expectedResponsePackets]] set, the result contains a
* [[UdpResponse]].
*
* ```typescript
* const command = new DataFlow.UdpRequestData();
* command.expectedResponsePackets = 1;
* ...
*
* localHomeApp.getDeviceManager()
* .send(command)
* .then((result: DataFlow.CommandSuccess) => {
* const udpResult = result as DataFlow.UdpResponseData;
* const packets = udpResult.udpResponse.responsePackets;
* })
* .catch((err: IntentFlow.HandlerError) => {
* // Handle command error
* });
* ```
*/
class UdpRequestData implements UdpRequestData {}
/**
* Successful response to a [[DeviceManager]] command.
*/
interface CommandSuccess extends CommandBase {}
/** Command result containing an [[HttpResponse]]. */
interface HttpResponseData extends CommandSuccess {
/** Response to an HTTP request */
httpResponse: HttpResponse;
}
/** Command result containing a [[TcpResponse]]. */
interface TcpResponseData extends CommandSuccess {
/** Response to a TCP request */
tcpResponse: TcpResponse;
}
/** Command result containing a [[UdpResponse]]. */
interface UdpResponseData extends CommandSuccess {
/** Response to a UDP request. */
udpResponse: UdpResponse;
}
/**
* Response to a [[DeviceManager]] command that resulted
* in an error.
*
* @deprecated See [[HandlerError]] for handling command failures.
*/
interface CommandFailure extends CommandBase {
/** The cause for this error */
errorCode: string;
/** Human readable description of this error */
debugString?: string;
}
}
/**
* Options for [[DeviceManager.send]] API.
* Use `send(command, {commandTimeout: 1000});` to wait for the platform to
* respond with success or timeout after 1000ms.
* Use `send(command, {retries: 2, delayInMilliseconds: 20});` to retry a
* command 2 times with 20ms delay between each retry.
*/
interface SendOptions {
/**
* Waits for command response for upto `commandTimeout` ms but no less than
* 1000ms. Usage outside execute handler is not recommended. In addition, it
* should be used only when platform provided timeouts are not enough.
*/
commandTimeout?: number;
/**
* Retries the command upto `retries` times upto 3 times. `commandTimeout`
* applies to each command. Each retry will also get the timeout.
*/
retries?: number;
/** Delay in ms between each retry. Default is no-delay between commands. */
delayInMilliseconds?: number;
}
}