29
29
"""
30
30
31
31
import argparse
32
- import base64
33
32
import io
34
33
import os
35
34
import sys
@@ -464,20 +463,13 @@ def set_config(
464
463
version , config ):
465
464
# [START iot_set_device_config]
466
465
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 )
476
469
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 )
481
473
# [END iot_set_device_config]
482
474
483
475
@@ -486,21 +478,17 @@ def get_config_versions(
486
478
device_id ):
487
479
"""Lists versions of a device config in descending order (newest first)."""
488
480
# [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 ))
504
492
505
493
return configs
506
494
# [END iot_get_device_configs]
@@ -546,19 +534,13 @@ def send_command(
546
534
"""Send a command to a device."""
547
535
# [START iot_send_command]
548
536
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 )
552
540
553
- config_body = {
554
- 'binaryData' : base64 .urlsafe_b64encode (
555
- command .encode ('utf-8' )).decode ('ascii' )
556
- }
541
+ data = command .encode ('utf-8' )
557
542
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 )
562
544
# [END iot_send_command]
563
545
564
546
@@ -578,10 +560,10 @@ def create_gateway(
578
560
if device .id == gateway_id :
579
561
exists = True
580
562
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
585
567
))
586
568
587
569
with io .open (certificate_file ) as f :
@@ -759,7 +741,8 @@ def parse_command_line_args():
759
741
help = 'Path to service account json file.' )
760
742
parser .add_argument (
761
743
'--version' ,
762
- default = None ,
744
+ default = 0 ,
745
+ type = int ,
763
746
help = 'Version number for setting device configuration.' )
764
747
765
748
# Command subparser
@@ -841,7 +824,7 @@ def run_get(args):
841
824
args .cloud_region , args .registry_id , args .device_id )
842
825
843
826
elif args .command == 'get-config-versions' :
844
- get_device (
827
+ get_config_versions (
845
828
args .service_account_json , args .project_id ,
846
829
args .cloud_region , args .registry_id , args .device_id )
847
830
0 commit comments