3535import sys
3636import time
3737
38+ from google .cloud import iot_v1
3839from google .cloud import pubsub
3940from google .oauth2 import service_account
4041from googleapiclient import discovery
@@ -236,29 +237,38 @@ def get_device(
236237 """Retrieve the device with the given id."""
237238 # [START iot_get_device]
238239 print ('Getting device' )
239- client = get_client ( service_account_json )
240- registry_name = 'projects/{}/locations/{}/registries/{}' . format (
241- project_id , cloud_region , registry_id )
240+ client = iot_v1 . DeviceManagerClient ( )
241+ device_path = client . device_path (
242+ project_id , cloud_region , registry_id , device_id )
242243
243- device_name = '{}/devices/{}' .format (registry_name , device_id )
244- devices = client .projects ().locations ().registries ().devices ()
245- device = devices .get (name = device_name ).execute ()
244+ device = client .get_device (device_path )
246245
247- print ('Id : {}' .format (device .get ( 'id' ) ))
248- print ('Name : {}' .format (device .get ( ' name' ) ))
246+ print ('Id : {}' .format (device .id ))
247+ print ('Name : {}' .format (device .name ))
249248 print ('Credentials:' )
250- if device .get ('credentials' ) is not None :
251- for credential in device .get ('credentials' ):
252- keyinfo = credential .get ('publicKey' )
253- print ('\t certificate: \n {}' .format (keyinfo .get ('key' )))
254- print ('\t format : {}' .format (keyinfo .get ('format' )))
255- print ('\t expiration: {}' .format (credential .get ('expirationTime' )))
249+
250+ if device .credentials is not None :
251+ for credential in device .credentials :
252+ keyinfo = credential .public_key
253+ print ('\t certificate: \n {}' .format (keyinfo .key ))
254+
255+ if keyinfo .format == 4 :
256+ keyformat = 'ES256_X509_PEM'
257+ elif keyinfo .format == 3 :
258+ keyformat = 'RSA_PEM'
259+ elif keyinfo .format == 2 :
260+ keyformat = 'ES256_PEM'
261+ elif keyinfo .format == 1 :
262+ keyformat = 'RSA_X509_PEM'
263+ else :
264+ keyformat = 'UNSPECIFIED_PUBLIC_KEY_FORMAT'
265+ print ('\t format : {}' .format (keyformat ))
266+ print ('\t expiration: {}' .format (credential .expiration_time ))
256267
257268 print ('Config:' )
258- print ('\t data: {}' .format (device .get ('config' ).get ('data' )))
259- print ('\t version: {}' .format (device .get ('config' ).get ('version' )))
260- print ('\t cloudUpdateTime: {}' .format (device .get ('config' ).get (
261- 'cloudUpdateTime' )))
269+ print ('\t data: {}' .format (device .config .binary_data ))
270+ print ('\t version: {}' .format (device .config .version ))
271+ print ('\t cloudUpdateTime: {}' .format (device .config .cloud_update_time ))
262272
263273 return device
264274 # [END iot_get_device]
@@ -269,17 +279,19 @@ def get_state(
269279 device_id ):
270280 """Retrieve a device's state blobs."""
271281 # [START iot_get_device_state]
272- client = get_client ( service_account_json )
273- registry_name = 'projects/{}/locations/{}/registries/{}' . format (
274- project_id , cloud_region , registry_id )
282+ client = iot_v1 . DeviceManagerClient ( )
283+ device_path = client . device_path (
284+ project_id , cloud_region , registry_id , device_id )
275285
276- device_name = '{}/devices/{}' .format (registry_name , device_id )
277- devices = client .projects ().locations ().registries ().devices ()
278- state = devices .states ().list (name = device_name , numStates = 5 ).execute ()
286+ device = client .get_device (device_path )
287+ print ('Last state: {}' .format (device .state ))
279288
280- print ('State: {}\n ' .format (state ))
289+ print ('State history' )
290+ states = client .list_device_states (device_path ).device_states
291+ for state in states :
292+ print ('State: {}' .format (state ))
281293
282- return state
294+ return states
283295 # [END iot_get_device_state]
284296
285297
@@ -288,16 +300,13 @@ def list_devices(
288300 """List all devices in the registry."""
289301 # [START iot_list_devices]
290302 print ('Listing devices' )
291- registry_path = 'projects/{}/locations/{}/registries/{}' .format (
292- project_id , cloud_region , registry_id )
293- client = get_client (service_account_json )
294- devices = client .projects ().locations ().registries ().devices (
295- ).list (parent = registry_path ).execute ().get ('devices' , [])
296303
304+ client = iot_v1 .DeviceManagerClient ()
305+ registry_path = client .registry_path (project_id , cloud_region , registry_id )
306+
307+ devices = list (client .list_devices (parent = registry_path ))
297308 for device in devices :
298- print ('Device: {} : {}' .format (
299- device .get ('numId' ),
300- device .get ('id' )))
309+ print ('Device: {} : {}' .format (device .num_id , device .id ))
301310
302311 return devices
303312 # [END iot_list_devices]
@@ -307,16 +316,14 @@ def list_registries(service_account_json, project_id, cloud_region):
307316 """List all registries in the project."""
308317 # [START iot_list_registries]
309318 print ('Listing Registries' )
310- registry_path = 'projects/{}/locations/{}' .format (
311- project_id , cloud_region )
312- client = get_client (service_account_json )
313- registries = client .projects ().locations ().registries ().list (
314- parent = registry_path ).execute ().get ('deviceRegistries' , [])
319+ client = iot_v1 .DeviceManagerClient ()
320+ parent = client .location_path (project_id , cloud_region )
315321
322+ registries = list (client .list_device_registries (parent ))
316323 for registry in registries :
317324 print ('id: {}\n \t name: {}' .format (
318- registry .get ( 'id' ) ,
319- registry .get ( ' name' ) ))
325+ registry .id ,
326+ registry .name ))
320327
321328 return registries
322329 # [END iot_list_registries]
0 commit comments