Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion iot/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand All @@ -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"
Expand Down
17 changes: 17 additions & 0 deletions iot/iot.php
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down
50 changes: 50 additions & 0 deletions iot/src/send_command_to_device.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

/**
* Copyright 2018 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
namespace Google\Cloud\Samples\Iot;

# [START iot_send_command_to_device]
use Google\Cloud\Iot\V1\DeviceManagerClient;

/**
* Sends a command to a device.
*
* @param string $registryId IOT Device Registry ID
* @param string $deviceId IOT Device ID
* @param string $command The command sent to a device
* @param string $projectId Google Cloud project ID
* @param string $location (Optional) Google Cloud region
*/
function send_command_to_device(
$registryId,
$deviceId,
$command,
$projectId,
$location = 'us-central1'
) {
print('Sending command to device' . PHP_EOL);

// Instantiate a client.
$deviceManager = new DeviceManagerClient();
$deviceName = $deviceManager->deviceName($projectId, $location, $registryId, $deviceId);

// Response empty on success
$deviceManager->sendCommandToDevice($deviceName, $command);

printf('Command sent' . PHP_EOL);
}
# [END iot_send_command_to_device]
12 changes: 12 additions & 0 deletions iot/test/iotTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test does not appear to test a success case, as a success case would contain Command sent.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In order to test the success case, we would need a PHP device sample :-/

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There may be a way for us to use HTTP to "connect" a device to the commands topic but I have only done it in MQTT device samples.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would require a PHP device sample to confirm the command data on the device side, but it shouldn't require a PHP device to just send the data...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The device needs to be subscribed to the commands topic over MQTT in order to not receive the error message.

}

/** @depends testSetDeviceConfig */
public function testGetDeviceConfigs()
{
Expand Down