@@ -69,7 +69,7 @@ def get_client(service_account_json, api_key):
6969 provided API key and creating a service object using the service account
7070 credentials JSON."""
7171 api_scopes = ['https://www.googleapis.com/auth/cloud-platform' ]
72- api_version = 'v1beta1 '
72+ api_version = 'v1 '
7373 discovery_api = 'https://cloudiot.googleapis.com/$discovery/rest'
7474 service_name = 'cloudiotcore'
7575
@@ -173,7 +173,7 @@ def delete_device(
173173
174174
175175def delete_registry (
176- service_account_json , api_key , project_id , cloud_region , registry_id ):
176+ service_account_json , api_key , project_id , cloud_region , registry_id ):
177177 """Deletes the specified registry."""
178178 print ('Delete registry' )
179179 client = get_client (service_account_json , api_key )
@@ -216,6 +216,23 @@ def get_device(
216216 return device
217217
218218
219+ def get_state (
220+ service_account_json , api_key , project_id , cloud_region , registry_id ,
221+ device_id ):
222+ """Retrieve a device's state blobs."""
223+ client = get_client (service_account_json , api_key )
224+ registry_name = 'projects/{}/locations/{}/registries/{}' .format (
225+ project_id , cloud_region , registry_id )
226+
227+ device_name = '{}/devices/{}' .format (registry_name , device_id )
228+ devices = client .projects ().locations ().registries ().devices ()
229+ state = devices .states ().list (name = device_name , numStates = 5 ).execute ()
230+
231+ print ('State: {}\n ' .format (state ))
232+
233+ return state
234+
235+
219236def list_devices (
220237 service_account_json , api_key , project_id , cloud_region , registry_id ):
221238 """List all devices in the registry."""
@@ -261,9 +278,9 @@ def create_registry(
261278 project_id ,
262279 cloud_region )
263280 body = {
264- 'eventNotificationConfig ' : {
281+ 'eventNotificationConfigs ' : [ {
265282 'pubsubTopicName' : pubsub_topic
266- },
283+ }] ,
267284 'id' : registry_id
268285 }
269286 request = client .projects ().locations ().registries ().create (
@@ -274,6 +291,7 @@ def create_registry(
274291 print ('Created registry' )
275292 return response
276293 except HttpError :
294+ print ('Error, registry not created' )
277295 return ""
278296
279297
@@ -425,7 +443,8 @@ def parse_command_line_args():
425443 command .add_parser ('delete-device' , help = delete_device .__doc__ )
426444 command .add_parser ('delete-registry' , help = delete_registry .__doc__ )
427445 command .add_parser ('get' , help = get_device .__doc__ )
428- command .add_parser ('get-registry' , help = get_device .__doc__ )
446+ command .add_parser ('get-registry' , help = get_registry .__doc__ )
447+ command .add_parser ('get-state' , help = get_state .__doc__ )
429448 command .add_parser ('list' , help = list_devices .__doc__ )
430449 command .add_parser ('list-registries' , help = list_registries .__doc__ )
431450 command .add_parser ('patch-es256' , help = patch_es256_auth .__doc__ )
@@ -436,6 +455,10 @@ def parse_command_line_args():
436455
437456def run_command (args ):
438457 """Calls the program using the specified command."""
458+ if args .project_id is None :
459+ print ('You must specify a project ID or set the environment variable.' )
460+ return
461+
439462 if args .command == 'create-rsa256' :
440463 create_rs256_device (
441464 args .service_account_json , args .api_key , args .project_id ,
@@ -476,6 +499,11 @@ def run_command(args):
476499 args .service_account_json , args .api_key , args .project_id ,
477500 args .cloud_region , args .registry_id , args .device_id )
478501
502+ elif args .command == 'get-state' :
503+ get_state (
504+ args .service_account_json , args .api_key , args .project_id ,
505+ args .cloud_region , args .registry_id , args .device_id )
506+
479507 elif args .command == 'list' :
480508 list_devices (
481509 args .service_account_json , args .api_key , args .project_id ,
0 commit comments