@@ -175,16 +175,18 @@ const createDevice = async (
175
175
publicKeyFile
176
176
) => {
177
177
// [START iot_create_device]
178
- // Client retrieved in callback
179
- // getClient(serviceAccountJson, function(client) {...});
180
178
// const cloudRegion = 'us-central1';
181
179
// const deviceId = 'my-device';
182
180
// const projectId = 'adjective-noun-123';
183
181
// const registryId = 'my-registry';
182
+ const iot = require ( '@google-cloud/iot' ) ;
184
183
185
- const parentName = `projects/${ projectId } /locations/${ cloudRegion } ` ;
186
- const registryName = `${ parentName } /registries/${ registryId } ` ;
187
- const body = {
184
+ const iotClient = new iot . v1 . DeviceManagerClient ( {
185
+ // optional auth parameters.
186
+ } ) ;
187
+
188
+ const regPath = iotClient . registryPath ( projectId , cloudRegion , registryId ) ;
189
+ const device = {
188
190
id : deviceId ,
189
191
credentials : [
190
192
{
@@ -197,17 +199,13 @@ const createDevice = async (
197
199
} ;
198
200
199
201
const request = {
200
- parent : registryName ,
201
- resource : body ,
202
+ parent : regPath ,
203
+ device ,
202
204
} ;
203
-
204
205
try {
205
- const { data} = await client . projects . locations . registries . devices . create (
206
- request
207
- ) ;
208
-
209
- console . log ( 'Created device' ) ;
210
- console . log ( data ) ;
206
+ const responses = await iotClient . createDevice ( request ) ;
207
+ const response = responses [ 0 ] ;
208
+ console . log ( 'Created device' , response ) ;
211
209
} catch ( err ) {
212
210
console . error ( 'Could not create device' , err ) ;
213
211
}
@@ -224,28 +222,27 @@ const createUnauthDevice = async (
224
222
cloudRegion
225
223
) => {
226
224
// [START iot_create_unauth_device]
227
- // Client retrieved in callback
228
- // getClient(serviceAccountJson, function(client) {...});
229
225
// const cloudRegion = 'us-central1';
230
226
// const deviceId = 'my-unauth-device';
231
227
// const projectId = 'adjective-noun-123';
232
228
// const registryId = 'my-registry';
233
- console . log ( 'Creating device:' , deviceId ) ;
234
- const parentName = `projects/${ projectId } /locations/${ cloudRegion } ` ;
235
- const registryName = `${ parentName } /registries/${ registryId } ` ;
229
+ const iot = require ( '@google-cloud/iot' ) ;
236
230
231
+ const iotClient = new iot . v1 . DeviceManagerClient ( {
232
+ // optional auth parameters.
233
+ } ) ;
234
+
235
+ const regPath = iotClient . registryPath ( projectId , cloudRegion , registryId ) ;
236
+ const device = { id : deviceId } ;
237
237
const request = {
238
- parent : registryName ,
239
- resource : { id : deviceId } ,
238
+ parent : regPath ,
239
+ device ,
240
240
} ;
241
241
242
242
try {
243
- const { data} = await client . projects . locations . registries . devices . create (
244
- request
245
- ) ;
246
-
247
- console . log ( 'Created device' ) ;
248
- console . log ( data ) ;
243
+ const responses = await iotClient . createDevice ( request ) ;
244
+ const response = responses [ 0 ] ;
245
+ console . log ( 'Created device' , response ) ;
249
246
} catch ( err ) {
250
247
console . error ( 'Could not create device' , err ) ;
251
248
}
@@ -262,15 +259,18 @@ const createRsaDevice = async (
262
259
rsaCertificateFile
263
260
) => {
264
261
// [START iot_create_rsa_device]
265
- // Client retrieved in callback
266
- // getClient(serviceAccountJson, function(client) {...});
267
262
// const cloudRegion = 'us-central1';
268
263
// const deviceId = 'my-rsa-device';
269
264
// const projectId = 'adjective-noun-123';
270
265
// const registryId = 'my-registry';
271
- const parentName = `projects/${ projectId } /locations/${ cloudRegion } ` ;
272
- const registryName = `${ parentName } /registries/${ registryId } ` ;
273
- const body = {
266
+ const iot = require ( '@google-cloud/iot' ) ;
267
+
268
+ const iotClient = new iot . v1 . DeviceManagerClient ( {
269
+ // optional auth parameters.
270
+ } ) ;
271
+
272
+ const regPath = iotClient . registryPath ( projectId , cloudRegion , registryId ) ;
273
+ const device = {
274
274
id : deviceId ,
275
275
credentials : [
276
276
{
@@ -283,19 +283,14 @@ const createRsaDevice = async (
283
283
} ;
284
284
285
285
const request = {
286
- parent : registryName ,
287
- resource : body ,
286
+ parent : regPath ,
287
+ device ,
288
288
} ;
289
289
290
- console . log ( JSON . stringify ( request ) ) ;
291
-
292
290
try {
293
- const { data} = await client . projects . locations . registries . devices . create (
294
- request
295
- ) ;
296
-
297
- console . log ( 'Created device' ) ;
298
- console . log ( data ) ;
291
+ const responses = await iotClient . createDevice ( request ) ;
292
+ const response = responses [ 0 ] ;
293
+ console . log ( 'Created device' , response ) ;
299
294
} catch ( err ) {
300
295
console . error ( 'Could not create device' , err ) ;
301
296
}
@@ -312,15 +307,18 @@ const createEsDevice = async (
312
307
esCertificateFile
313
308
) => {
314
309
// [START iot_create_es_device]
315
- // Client retrieved in callback
316
- // getClient(serviceAccountJson, function(client) {...});
317
310
// const cloudRegion = 'us-central1';
318
311
// const deviceId = 'my-es-device';
319
312
// const projectId = 'adjective-noun-123';
320
313
// const registryId = 'my-registry';
321
- const parentName = `projects/${ projectId } /locations/${ cloudRegion } ` ;
322
- const registryName = `${ parentName } /registries/${ registryId } ` ;
323
- const body = {
314
+ const iot = require ( '@google-cloud/iot' ) ;
315
+
316
+ const iotClient = new iot . v1 . DeviceManagerClient ( {
317
+ // optional auth parameters.
318
+ } ) ;
319
+
320
+ const regPath = iotClient . registryPath ( projectId , cloudRegion , registryId ) ;
321
+ const device = {
324
322
id : deviceId ,
325
323
credentials : [
326
324
{
@@ -331,19 +329,15 @@ const createEsDevice = async (
331
329
} ,
332
330
] ,
333
331
} ;
334
-
335
332
const request = {
336
- parent : registryName ,
337
- resource : body ,
333
+ parent : regPath ,
334
+ device ,
338
335
} ;
339
336
340
337
try {
341
- const { data} = await client . projects . locations . registries . devices . create (
342
- request
343
- ) ;
344
-
345
- console . log ( 'Created device' ) ;
346
- console . log ( data ) ;
338
+ const responses = await iotClient . createDevice ( request ) ;
339
+ const response = responses [ 0 ] ;
340
+ console . log ( 'Created device' , response ) ;
347
341
} catch ( err ) {
348
342
console . error ( 'Could not create device' , err ) ;
349
343
}
@@ -503,26 +497,26 @@ const deleteDevice = async (
503
497
cloudRegion
504
498
) => {
505
499
// [START iot_delete_device]
506
- // Client retrieved in callback
507
- // getClient(serviceAccountJson, function(client) {...});
508
500
// const cloudRegion = 'us-central1';
509
501
// const projectId = 'adjective-noun-123';
510
502
// const registryId = 'my-registry';
511
- const parentName = `projects/${ projectId } /locations/${ cloudRegion } ` ;
512
- const registryName = `${ parentName } /registries/${ registryId } ` ;
513
- const request = {
514
- name : `${ registryName } /devices/${ deviceId } ` ,
515
- } ;
503
+ const iot = require ( '@google-cloud/iot' ) ;
516
504
517
- try {
518
- const { data} = await client . projects . locations . registries . devices . delete (
519
- request
520
- ) ;
505
+ const iotClient = new iot . v1 . DeviceManagerClient ( {
506
+ // optional auth parameters.
507
+ } ) ;
521
508
522
- console . log ( 'Successfully deleted device:' , deviceId ) ;
523
- console . log ( data ) ;
509
+ const devPath = iotClient . devicePath (
510
+ projectId ,
511
+ cloudRegion ,
512
+ registryId ,
513
+ deviceId
514
+ ) ;
515
+ try {
516
+ const responses = await iotClient . deleteDevice ( { name : devPath } ) ;
517
+ console . log ( 'Successfully deleted device' , responses ) ;
524
518
} catch ( err ) {
525
- console . error ( 'Could not delete device:' , deviceId , err ) ;
519
+ console . error ( 'Could not delete device' , err ) ;
526
520
}
527
521
// [END iot_delete_device]
528
522
} ;
0 commit comments