@@ -69,7 +69,7 @@ def get_client(service_account_json, api_key):
69
69
provided API key and creating a service object using the service account
70
70
credentials JSON."""
71
71
api_scopes = ['https://www.googleapis.com/auth/cloud-platform' ]
72
- api_version = 'v1beta1 '
72
+ api_version = 'v1 '
73
73
discovery_api = 'https://cloudiot.googleapis.com/$discovery/rest'
74
74
service_name = 'cloudiotcore'
75
75
@@ -173,7 +173,7 @@ def delete_device(
173
173
174
174
175
175
def 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 ):
177
177
"""Deletes the specified registry."""
178
178
print ('Delete registry' )
179
179
client = get_client (service_account_json , api_key )
@@ -216,6 +216,23 @@ def get_device(
216
216
return device
217
217
218
218
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
+
219
236
def list_devices (
220
237
service_account_json , api_key , project_id , cloud_region , registry_id ):
221
238
"""List all devices in the registry."""
@@ -261,9 +278,9 @@ def create_registry(
261
278
project_id ,
262
279
cloud_region )
263
280
body = {
264
- 'eventNotificationConfig ' : {
281
+ 'eventNotificationConfigs ' : [ {
265
282
'pubsubTopicName' : pubsub_topic
266
- },
283
+ }] ,
267
284
'id' : registry_id
268
285
}
269
286
request = client .projects ().locations ().registries ().create (
@@ -274,6 +291,7 @@ def create_registry(
274
291
print ('Created registry' )
275
292
return response
276
293
except HttpError :
294
+ print ('Error, registry not created' )
277
295
return ""
278
296
279
297
@@ -425,7 +443,8 @@ def parse_command_line_args():
425
443
command .add_parser ('delete-device' , help = delete_device .__doc__ )
426
444
command .add_parser ('delete-registry' , help = delete_registry .__doc__ )
427
445
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__ )
429
448
command .add_parser ('list' , help = list_devices .__doc__ )
430
449
command .add_parser ('list-registries' , help = list_registries .__doc__ )
431
450
command .add_parser ('patch-es256' , help = patch_es256_auth .__doc__ )
@@ -436,6 +455,10 @@ def parse_command_line_args():
436
455
437
456
def run_command (args ):
438
457
"""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
+
439
462
if args .command == 'create-rsa256' :
440
463
create_rs256_device (
441
464
args .service_account_json , args .api_key , args .project_id ,
@@ -476,6 +499,11 @@ def run_command(args):
476
499
args .service_account_json , args .api_key , args .project_id ,
477
500
args .cloud_region , args .registry_id , args .device_id )
478
501
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
+
479
507
elif args .command == 'list' :
480
508
list_devices (
481
509
args .service_account_json , args .api_key , args .project_id ,
0 commit comments