@@ -573,6 +573,40 @@ function getDeviceState (client, deviceId, registryId, projectId,
573573 // [END iot_get_device_state]
574574}
575575
576+ // Retrieve the given device's state from the registry.
577+ function setDeviceConfig ( client , deviceId , registryId , projectId ,
578+ cloudRegion , data , version ) {
579+ // [START iot_get_device_state]
580+ // Client retrieved in callback
581+ // getClient(serviceAccountJson, function(client) {...});
582+ // const cloudRegion = 'us-central1';
583+ // const deviceId = 'my-device';
584+ // const projectId = 'adjective-noun-123';
585+ // const registryId = 'my-registry';
586+ // const data = 'test-data';
587+ // const version = 0;
588+ const parentName = `projects/${ projectId } /locations/${ cloudRegion } ` ;
589+ const registryName = `${ parentName } /registries/${ registryId } ` ;
590+
591+ const binaryData = Buffer . from ( data ) . toString ( 'base64' ) ;
592+ const request = {
593+ name : `${ registryName } /devices/${ deviceId } ` ,
594+ versionToUpdate : version ,
595+ binaryData : binaryData
596+ } ;
597+
598+ client . projects . locations . registries . devices . modifyCloudToDeviceConfig ( request ,
599+ ( err , data ) => {
600+ if ( err ) {
601+ console . log ( 'Could not update config:' , deviceId ) ;
602+ console . log ( 'Message: ' , err ) ;
603+ } else {
604+ console . log ( 'Success :' , data ) ;
605+ }
606+ } ) ;
607+ // [END iot_get_device_state]
608+ }
609+
576610// Retrieve the given device from the registry.
577611function getRegistry ( client , registryId , projectId , cloudRegion ) {
578612 // [START iot_get_registry]
@@ -823,6 +857,19 @@ require(`yargs`) // eslint-disable-line
823857 getClient ( opts . serviceAccount , cb ) ;
824858 }
825859 )
860+ . command (
861+ `setConfig <deviceId> <registryId> <configuration> <version>` ,
862+ `Sets a devices configuration to the specified data.` ,
863+ { } ,
864+ ( opts ) => {
865+ const cb = function ( client ) {
866+ setDeviceConfig ( client , opts . deviceId , opts . registryId ,
867+ opts . projectId , opts . cloudRegion , opts . configuration ,
868+ opts . version || 0 ) ;
869+ } ;
870+ getClient ( opts . serviceAccount , cb ) ;
871+ }
872+ )
826873 . example ( `node $0 createEs256Device my-es-device my-registry ../ec_public.pem` )
827874 . example ( `node $0 createRegistry my-registry my-iot-topic --serviceAccount=$HOME/creds_iot.json --project_id=my-project-id` )
828875 . example ( `node $0 createRsa256Device my-rsa-device my-registry ../rsa_cert.pem` )
@@ -836,6 +883,7 @@ require(`yargs`) // eslint-disable-line
836883 . example ( `node $0 listRegistries` )
837884 . example ( `node $0 patchRsa256 my-device my-registry ../rsa_cert.pem` )
838885 . example ( `node $0 patchEs256 my-device my-registry ../ec_public.pem` )
886+ . example ( `node $0 setConfig my-device my-registry "test" 0` )
839887 . example ( `node $0 setupTopic my-iot-topic --serviceAccount=$HOME/creds_iot.json --projectId=my-project-id` )
840888 . wrap ( 120 )
841889 . recommendCommands ( )
0 commit comments