use Google\Cloud\Iot\V1\DeviceManagerClient;
/**
* Binds a device to a gateway.
*
* @param string $registryId IOT Device Registry ID
* @param string $deviceId the device ID to bind
* @param string $gatewayId the ID for the gateway to bind to
* @param string $projectId Google Cloud project ID
* @param string $location Google Cloud region
*/
function bind_device_to_gateway(
$registryId,
$gatewayId,
$deviceId,
$projectId,
$location = 'us-central1'
) {
print('Binding Device to Gateway' . PHP_EOL);
// Instantiate a client.
$deviceManager = new DeviceManagerClient();
$registryName = $deviceManager->registryName($projectId, $location, $registryId);
$result = $deviceManager->bindDeviceToGateway($registryName, $gatewayId, $deviceId);
print('Device bound');
}
# project_id = "Your Google Cloud project ID"
# location_id = "The Cloud region the registry is located in"
# registry_id = "The registry to create a device in"
# gateway_id = "The Gateway to bind to"
# device_id = "The identifier of the device to bind"
require "google/apis/cloudiot_v1"
# Initialize the client and authenticate with the specified scope
Cloudiot = Google::Apis::CloudiotV1
iot_client = Cloudiot::CloudIotService.new
iot_client.authorization = Google::Auth.get_application_default(
"https://www.googleapis.com/auth/cloud-platform"
)
# The resource name of the location associated with the project
parent = "projects/#{project_id}/locations/#{location_id}/registries/#{registry_id}"
bind_req = Google::Apis::CloudiotV1::BindDeviceToGatewayRequest.new
bind_req.gateway_id = gateway_id
bind_req.device_id = device_id
res = iot_client.bind_registry_device_to_gateway parent, bind_req
puts "Device bound"