Skip to end of metadata
Go to start of metadata

You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 3 Next »

This documentation is still in development - use at your own risk

This page explains how to create, configure, and manage gateways.

Creating or editing a gateway

You can create a gateway or edit an existing one using console, or gcloud. After you create a gateway, you can't change it to a non-gateway device. Make sure you've created a registry and a device key pair before completing the steps in this section.

ConsolegcloudAPI

Use the following methods to create or edit a gateway:

  • Device create method to add gateways to registries

  • Device patch method to edit existing gateways

C#GoJavaNode.jsPythonRuby

View on GitHub Feedback

public static object CreateGateway(string projectId, string cloudRegion,
    string registryName, string gatewayId, string publicKeyFilePath,
    string algorithm){
    var cloudIot = CreateAuthorizedClient();
    var registryPath = $"projects/{projectId}/locations/{cloudRegion}"
        + $"/registries/{registryName}";
    Console.WriteLine("Creating gateway with id: {0}", gatewayId);

    Device body = new Device()
    {
        Id = gatewayId,
        GatewayConfig = new GatewayConfig()
        {
            GatewayType = "GATEWAY",
            GatewayAuthMethod = "ASSOCIATION_ONLY"
        },
        Credentials =
        new List<DeviceCredential>()
        {
            new DeviceCredential()
            {
                PublicKey = new PublicKeyCredential()
                {
                    Key = File.ReadAllText(publicKeyFilePath),
                    Format = (algorithm == "ES256" ?
                        "ES256_PEM" : "RSA_X509_PEM")
                },
            }
        }
    };

    Device createdDevice = cloudIot.Projects.Locations.Registries
        .Devices.Create(body, registryPath).Execute();
    Console.WriteLine("Created gateway: {0}", createdDevice.ToString());
    return 0;}

To learn how to create the devices you'll use with the gateway, see Creating or editing a device.

Configuring the gateway and getting state

With Cloud IoT Core, you can control a gateway by modifying its configuration, just as you would with any other device. See Configuring Devices to learn how to configure a gateway over the MQTT or HTTP bridge.

After a configuration has been applied to a gateway, the gateway can report its state to Cloud IoT Core. You can compare the gateway's state and its most recent configuration to make sure the gateway is doing what it's supposed to be doing.

Binding or unbinding a device

You can authenticate non-gateway devices to Cloud IoT Core by binding them to the gateway. Binding creates an association between the devices and the gateway that Cloud IoT Core checks to authenticate the devices.

Note: Binding is required when using the HTTP bridge.

ConsolegcloudAPI

Use the following methods to bind a device to or unbind a device from a gateway:

  • Registries BindDeviceToGateway method to bind devices to gateways

  • Registries UnbindDeviceFromGateway method to unbind devices from gateways

C#GoJavaNode.jsPythonRuby

View on GitHub Feedback

public static object BindDeviceToGateway(string projectId, string cloudRegion,
    string registryName, string deviceId, string gatewayId){
    CreateDevice(projectId, cloudRegion, registryName, deviceId);
    var cloudIot = CreateAuthorizedClient();
    var registryPath = $"projects/{projectId}" +
        $"/locations/{cloudRegion}" +
        $"/registries/{registryName}";
    BindDeviceToGatewayRequest req = new BindDeviceToGatewayRequest
    {
        DeviceId = deviceId,
        GatewayId = gatewayId
    };

    var res = cloudIot
        .Projects
        .Locations
        .Registries
        .BindDeviceToGateway(req, registryPath)
        .Execute();
    Console.WriteLine("Device bound: {0}", res.ToString());

    return 0;}

Listing all devices bound to a gateway

ConsolegcloudAPI

Use the Devices list method and specify a gateway ID to list all devices bound to the gateway.

C#GoJavaNode.jsPythonRuby

View on GitHub Feedback

public static object ListDevicesForGateways(string projectId,
 string cloudRegion, string registryName, string gatewayId){
    var cloudIot = CreateAuthorizedClient();
    var gatewayPath = $"projects/{projectId}/locations/{cloudRegion}" +
        $"/registries/{registryName}/devices/{gatewayId}";
    var registryPath = $"projects/{projectId}/locations/{cloudRegion}" +
        $"/registries/{registryName}";
    var req = cloudIot.Projects.Locations.Registries.Devices.List(registryPath);
    req.GatewayListOptionsAssociationsGatewayId = gatewayId;
    var devices = req.Execute().Devices;

    if (devices != null)
    {
        Console.WriteLine("Found {0} devices", devices.Count);
        foreach (var device in devices)
        {
            Console.WriteLine("ID: {0}", device.Id);
        }
    }
    else
    {
        Console.WriteLine("Gateway has no bound devices.");
    }

    return 0;}

Listing all gateways in a registry

ConsolegcloudAPI

Use the Device list method to list all gateways in a registry.

C#GoJavaNode.jsPythonRuby

View on GitHub Feedback

public static object ListGateways(string projectId, string cloudRegion, string registryName){
    var cloudIot = CreateAuthorizedClient();
    var registryPath = $"projects/{projectId}/locations/{cloudRegion}/registries/{registryName}";

    var req = cloudIot
        .Projects
        .Locations
        .Registries
        .Devices
        .List(registryPath);
    req.FieldMask = "config,gatewayConfig";

    var devices = req.Execute().Devices;

    if (devices != null)
    {
        Console.WriteLine("Found {0} devices", devices.Count);
        devices.ToList().ForEach(device =>
           {
               if (device.GatewayConfig != null
               && device.GatewayConfig.GatewayType != null
                  && device.GatewayConfig.GatewayType.Equals("GATEWAY"))
               {
                   Console.WriteLine("Id :{0}", device.Id);
                   if (device.Config != null)
                   {
                       Console.WriteLine("Config: {0}", device.Config.ToString());
                   }
               }
           }
        );
    }
    else
    {
        Console.WriteLine("Registry has no gateway devices");
    }

    return 0;}

Deleting devices bound to a gateway

To delete a device bound to a gateway, you first unbind the device from all gateways it's bound to, then delete the device from the registry.

ConsolegcloudAPI

After unbinding the device from all gateways it's bound to, use the Device delete method to delete the device.

C#GoJavaNode.jsPythonRuby

View on GitHub Feedback

public static object DeleteDevice(string projectId, string cloudRegion, string registryId, string deviceId){
    var cloudIot = CreateAuthorizedClient();
    // The resource name of the location associated with the key rings.
    var name = $"projects/{projectId}/locations/{cloudRegion}/registries/{registryId}/devices/{deviceId}";

    try
    {
        var res = cloudIot.Projects.Locations.Registries.Devices.Delete(name).Execute();
        Console.WriteLine($"Removed device: {deviceId}");
    }
    catch (Google.GoogleApiException e)
    {
        Console.WriteLine(e.Message);
        if (e.Error != null) return e.Error.Code;
        return -1;
    }
    return 0;}

Deleting a gateway

To delete a gateway, you first unbind its devices then delete the gateway from the registry.

ConsolegcloudAPI

After unbinding all devices from the gateway, use the Device delete method to delete the gateway, specifying the ID of the gateway you want to delete.

C#GoJavaNode.jsPythonRuby

View on GitHub Feedback

public static object DeleteDevice(string projectId, string cloudRegion, string registryId, string deviceId){
    var cloudIot = CreateAuthorizedClient();
    // The resource name of the location associated with the key rings.
    var name = $"projects/{projectId}/locations/{cloudRegion}/registries/{registryId}/devices/{deviceId}";

    try
    {
        var res = cloudIot.Projects.Locations.Registries.Devices.Delete(name).Execute();
        Console.WriteLine($"Removed device: {deviceId}");
    }
    catch (Google.GoogleApiException e)
    {
        Console.WriteLine(e.Message);
        if (e.Error != null) return e.Error.Code;
        return -1;
    }
    return 0;}

What's next

  • No labels