2929"""
3030
3131import argparse
32- import base64
3332import io
3433import os
3534import sys
@@ -464,20 +463,13 @@ def set_config(
464463 version , config ):
465464 # [START iot_set_device_config]
466465 print ('Set device configuration' )
467- client = get_client (service_account_json )
468- device_path = 'projects/{}/locations/{}/registries/{}/devices/{}' .format (
469- project_id , cloud_region , registry_id , device_id )
470-
471- config_body = {
472- 'versionToUpdate' : version ,
473- 'binaryData' : base64 .urlsafe_b64encode (
474- config .encode ('utf-8' )).decode ('ascii' )
475- }
466+ client = iot_v1 .DeviceManagerClient ()
467+ device_path = client .device_path (
468+ project_id , cloud_region , registry_id , device_id )
476469
477- return client .projects (
478- ).locations ().registries (
479- ).devices ().modifyCloudToDeviceConfig (
480- name = device_path , body = config_body ).execute ()
470+ data = config .encode ('utf-8' )
471+
472+ return client .modify_cloud_to_device_config (device_path , data , version )
481473 # [END iot_set_device_config]
482474
483475
@@ -486,21 +478,17 @@ def get_config_versions(
486478 device_id ):
487479 """Lists versions of a device config in descending order (newest first)."""
488480 # [START iot_get_device_configs]
489- client = get_client (service_account_json )
490- registry_name = 'projects/{}/locations/{}/registries/{}' .format (
491- project_id , cloud_region , registry_id )
492-
493- device_name = '{}/devices/{}' .format (registry_name , device_id )
494- devices = client .projects ().locations ().registries ().devices ()
495- configs = devices .configVersions ().list (
496- name = device_name ).execute ().get (
497- 'deviceConfigs' , [])
498-
499- for config in configs :
500- print ('version: {}\n \t cloudUpdateTime: {}\n \t binaryData: {}' .format (
501- config .get ('version' ),
502- config .get ('cloudUpdateTime' ),
503- config .get ('binaryData' )))
481+ client = iot_v1 .DeviceManagerClient ()
482+ device_path = client .device_path (
483+ project_id , cloud_region , registry_id , device_id )
484+
485+ configs = client .list_device_config_versions (device_path )
486+
487+ for config in configs .device_configs :
488+ print ('version: {}\n \t cloudUpdateTime: {}\n \t data: {}' .format (
489+ config .version ,
490+ config .cloud_update_time ,
491+ config .binary_data ))
504492
505493 return configs
506494 # [END iot_get_device_configs]
@@ -546,19 +534,13 @@ def send_command(
546534 """Send a command to a device."""
547535 # [START iot_send_command]
548536 print ('Sending command to device' )
549- client = get_client ( service_account_json )
550- device_path = 'projects/{}/locations/{}/registries/{}/devices/{}' . format (
551- project_id , cloud_region , registry_id , device_id )
537+ client = iot_v1 . DeviceManagerClient ( )
538+ device_path = client . device_path (
539+ project_id , cloud_region , registry_id , device_id )
552540
553- config_body = {
554- 'binaryData' : base64 .urlsafe_b64encode (
555- command .encode ('utf-8' )).decode ('ascii' )
556- }
541+ data = command .encode ('utf-8' )
557542
558- return client .projects (
559- ).locations ().registries (
560- ).devices ().sendCommandToDevice (
561- name = device_path , body = config_body ).execute ()
543+ return client .send_command_to_device (device_path , data )
562544 # [END iot_send_command]
563545
564546
@@ -578,10 +560,10 @@ def create_gateway(
578560 if device .id == gateway_id :
579561 exists = True
580562 print ('Device: {} : {} : {} : {}' .format (
581- device .get ( 'id' ) ,
582- device .get ( 'numId' ) ,
583- device .get ( ' config' ) ,
584- device .get ( 'gatewayConfig' )
563+ device .id ,
564+ device .num_id ,
565+ device .config ,
566+ device .gateway_config
585567 ))
586568
587569 with io .open (certificate_file ) as f :
@@ -759,7 +741,8 @@ def parse_command_line_args():
759741 help = 'Path to service account json file.' )
760742 parser .add_argument (
761743 '--version' ,
762- default = None ,
744+ default = 0 ,
745+ type = int ,
763746 help = 'Version number for setting device configuration.' )
764747
765748 # Command subparser
@@ -841,7 +824,7 @@ def run_get(args):
841824 args .cloud_region , args .registry_id , args .device_id )
842825
843826 elif args .command == 'get-config-versions' :
844- get_device (
827+ get_config_versions (
845828 args .service_account_json , args .project_id ,
846829 args .cloud_region , args .registry_id , args .device_id )
847830
0 commit comments