32
32
"""
33
33
34
34
import argparse
35
+ import base64
35
36
import io
36
37
import os
37
38
import sys
@@ -69,7 +70,7 @@ def get_client(service_account_json, api_key):
69
70
provided API key and creating a service object using the service account
70
71
credentials JSON."""
71
72
api_scopes = ['https://www.googleapis.com/auth/cloud-platform' ]
72
- api_version = 'v1beta1 '
73
+ api_version = 'v1 '
73
74
discovery_api = 'https://cloudiot.googleapis.com/$discovery/rest'
74
75
service_name = 'cloudiotcore'
75
76
@@ -216,6 +217,23 @@ def get_device(
216
217
return device
217
218
218
219
220
+ def get_state (
221
+ service_account_json , api_key , project_id , cloud_region , registry_id ,
222
+ device_id ):
223
+ """Retrieve a device's state blobs."""
224
+ client = get_client (service_account_json , api_key )
225
+ registry_name = 'projects/{}/locations/{}/registries/{}' .format (
226
+ project_id , cloud_region , registry_id )
227
+
228
+ device_name = '{}/devices/{}' .format (registry_name , device_id )
229
+ devices = client .projects ().locations ().registries ().devices ()
230
+ state = devices .states ().list (name = device_name , numStates = 5 ).execute ()
231
+
232
+ print ('State: {}\n ' .format (state ))
233
+
234
+ return state
235
+
236
+
219
237
def list_devices (
220
238
service_account_json , api_key , project_id , cloud_region , registry_id ):
221
239
"""List all devices in the registry."""
@@ -274,6 +292,8 @@ def create_registry(
274
292
print ('Created registry' )
275
293
return response
276
294
except HttpError :
295
+ print (dir (HttpError ))
296
+ print ('Error {}' .format (HttpError ))
277
297
return ""
278
298
279
299
@@ -425,7 +445,8 @@ def parse_command_line_args():
425
445
command .add_parser ('delete-device' , help = delete_device .__doc__ )
426
446
command .add_parser ('delete-registry' , help = delete_registry .__doc__ )
427
447
command .add_parser ('get' , help = get_device .__doc__ )
428
- command .add_parser ('get-registry' , help = get_device .__doc__ )
448
+ command .add_parser ('get-state' , help = get_state .__doc__ )
449
+ command .add_parser ('get-registry' , help = get_registry .__doc__ )
429
450
command .add_parser ('list' , help = list_devices .__doc__ )
430
451
command .add_parser ('list-registries' , help = list_registries .__doc__ )
431
452
command .add_parser ('patch-es256' , help = patch_es256_auth .__doc__ )
@@ -436,6 +457,10 @@ def parse_command_line_args():
436
457
437
458
def run_command (args ):
438
459
"""Calls the program using the specified command."""
460
+ if args .project_id is None :
461
+ print ("You must specify a project ID or set the environment variable." )
462
+ return
463
+
439
464
if args .command == 'create-rsa256' :
440
465
create_rs256_device (
441
466
args .service_account_json , args .api_key , args .project_id ,
@@ -476,6 +501,11 @@ def run_command(args):
476
501
args .service_account_json , args .api_key , args .project_id ,
477
502
args .cloud_region , args .registry_id , args .device_id )
478
503
504
+ elif args .command == 'get-state' :
505
+ get_state (
506
+ args .service_account_json , args .api_key , args .project_id ,
507
+ args .cloud_region , args .registry_id , args .device_id )
508
+
479
509
elif args .command == 'list' :
480
510
list_devices (
481
511
args .service_account_json , args .api_key , args .project_id ,
0 commit comments