diff --git a/iot/composer.json b/iot/composer.json index 5153cf1b14..466b1a1cae 100644 --- a/iot/composer.json +++ b/iot/composer.json @@ -2,7 +2,7 @@ "name": "google/iot-sample", "type": "project", "require": { - "google/cloud-iot": "^0.1", + "google/cloud-iot": "^0.5.0", "symfony/console": "^3.3" }, "autoload": { @@ -22,6 +22,7 @@ "src/list_registries.php", "src/patch_es.php", "src/patch_rsa.php", + "src/send_command_to_device.php", "src/set_device_config.php", "src/set_device_state.php", "src/set_iam_policy.php" diff --git a/iot/iot.php b/iot/iot.php index f21896e18d..e8ded1f5de 100644 --- a/iot/iot.php +++ b/iot/iot.php @@ -283,6 +283,23 @@ ); }); +$application->add(new Command('send-command-to-device')) + ->addArgument('registry', InputArgument::REQUIRED, 'the registry ID') + ->addArgument('device', InputArgument::REQUIRED, 'the device ID') + ->addArgument('command-data', InputArgument::REQUIRED, 'the binary data to send as the command') + ->addOption('project', '', InputOption::VALUE_REQUIRED, 'The Google Cloud project ID', getenv('GCLOUD_PROJECT')) + ->addOption('location', '', InputOption::VALUE_REQUIRED, 'The location of your device registries', 'us-central1') + ->setDescription('Sends a command to a device.') + ->setCode(function ($input, $output) { + send_command_to_device( + $input->getArgument('registry'), + $input->getArgument('device'), + $input->getArgument('command-data'), + $input->getOption('project'), + $input->getOption('location') + ); + }); + $application->add(new Command('set-device-state')) ->addArgument('registry', InputArgument::REQUIRED, 'the registry ID') ->addArgument('device', InputArgument::REQUIRED, 'the device ID') diff --git a/iot/src/send_command_to_device.php b/iot/src/send_command_to_device.php new file mode 100644 index 0000000000..5cc5c5faa7 --- /dev/null +++ b/iot/src/send_command_to_device.php @@ -0,0 +1,50 @@ +deviceName($projectId, $location, $registryId, $deviceId); + + // Response empty on success + $deviceManager->sendCommandToDevice($deviceName, $command); + + printf('Command sent' . PHP_EOL); +} +# [END iot_send_command_to_device] diff --git a/iot/test/iotTest.php b/iot/test/iotTest.php index 2cec53062d..b6831cd192 100644 --- a/iot/test/iotTest.php +++ b/iot/test/iotTest.php @@ -180,6 +180,18 @@ public function testSetDeviceConfig() $this->assertContains('Data: ' . $config, $output); } + /** @depends testCreateRsaDevice */ + public function testSendCommandToDevice() + { + $command = '{"data":"example of command data"}'; + $output = $this->runCommand('send-command-to-device', [ + 'registry' => self::$registryId, + 'device' => self::$devices[0], + 'command-data' => $command, + ]); + $this->assertContains('not subscribed to the commands topic', $output); + } + /** @depends testSetDeviceConfig */ public function testGetDeviceConfigs() {