Skip to content

Commit 7cf5946

Browse files
authored
Adds example for sending a command to a device (GoogleCloudPlatform#763)
* Adds example for sending a command to a device
1 parent 01c04b3 commit 7cf5946

File tree

4 files changed

+81
-1
lines changed

4 files changed

+81
-1
lines changed

iot/composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "google/iot-sample",
33
"type": "project",
44
"require": {
5-
"google/cloud-iot": "^0.1",
5+
"google/cloud-iot": "^0.5.0",
66
"symfony/console": "^3.3"
77
},
88
"autoload": {
@@ -22,6 +22,7 @@
2222
"src/list_registries.php",
2323
"src/patch_es.php",
2424
"src/patch_rsa.php",
25+
"src/send_command_to_device.php",
2526
"src/set_device_config.php",
2627
"src/set_device_state.php",
2728
"src/set_iam_policy.php"

iot/iot.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,23 @@
283283
);
284284
});
285285

286+
$application->add(new Command('send-command-to-device'))
287+
->addArgument('registry', InputArgument::REQUIRED, 'the registry ID')
288+
->addArgument('device', InputArgument::REQUIRED, 'the device ID')
289+
->addArgument('command-data', InputArgument::REQUIRED, 'the binary data to send as the command')
290+
->addOption('project', '', InputOption::VALUE_REQUIRED, 'The Google Cloud project ID', getenv('GCLOUD_PROJECT'))
291+
->addOption('location', '', InputOption::VALUE_REQUIRED, 'The location of your device registries', 'us-central1')
292+
->setDescription('Sends a command to a device.')
293+
->setCode(function ($input, $output) {
294+
send_command_to_device(
295+
$input->getArgument('registry'),
296+
$input->getArgument('device'),
297+
$input->getArgument('command-data'),
298+
$input->getOption('project'),
299+
$input->getOption('location')
300+
);
301+
});
302+
286303
$application->add(new Command('set-device-state'))
287304
->addArgument('registry', InputArgument::REQUIRED, 'the registry ID')
288305
->addArgument('device', InputArgument::REQUIRED, 'the device ID')

iot/src/send_command_to_device.php

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?php
2+
3+
/**
4+
* Copyright 2018 Google Inc.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
namespace Google\Cloud\Samples\Iot;
19+
20+
# [START iot_send_command_to_device]
21+
use Google\Cloud\Iot\V1\DeviceManagerClient;
22+
23+
/**
24+
* Sends a command to a device.
25+
*
26+
* @param string $registryId IOT Device Registry ID
27+
* @param string $deviceId IOT Device ID
28+
* @param string $command The command sent to a device
29+
* @param string $projectId Google Cloud project ID
30+
* @param string $location (Optional) Google Cloud region
31+
*/
32+
function send_command_to_device(
33+
$registryId,
34+
$deviceId,
35+
$command,
36+
$projectId,
37+
$location = 'us-central1'
38+
) {
39+
print('Sending command to device' . PHP_EOL);
40+
41+
// Instantiate a client.
42+
$deviceManager = new DeviceManagerClient();
43+
$deviceName = $deviceManager->deviceName($projectId, $location, $registryId, $deviceId);
44+
45+
// Response empty on success
46+
$deviceManager->sendCommandToDevice($deviceName, $command);
47+
48+
printf('Command sent' . PHP_EOL);
49+
}
50+
# [END iot_send_command_to_device]

iot/test/iotTest.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,18 @@ public function testSetDeviceConfig()
180180
$this->assertContains('Data: ' . $config, $output);
181181
}
182182

183+
/** @depends testCreateRsaDevice */
184+
public function testSendCommandToDevice()
185+
{
186+
$command = '{"data":"example of command data"}';
187+
$output = $this->runCommand('send-command-to-device', [
188+
'registry' => self::$registryId,
189+
'device' => self::$devices[0],
190+
'command-data' => $command,
191+
]);
192+
$this->assertContains('not subscribed to the commands topic', $output);
193+
}
194+
183195
/** @depends testSetDeviceConfig */
184196
public function testGetDeviceConfigs()
185197
{

0 commit comments

Comments
 (0)