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.
Use the following methods to create or edit a gateway:
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 ClearBlade 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 ClearBlade 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 ClearBlade IoT Core by binding them to the gateway. Binding creates an association between the devices and the gateway that ClearBlade IoT Core checks to authenticate the devices.
Note: Binding is required when using the HTTP bridge.
Use the following methods to bind a device to or unbind a device from a gateway:
Registries
BindDeviceToGateway
method to bind devices to gatewaysRegistries
UnbindDeviceFromGateway
method to unbind devices from gateways
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
Use the Devices list
method and specify a gateway ID to list all devices bound to the gateway.
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
Use the Device list
method to list all gateways in a registry.
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.
After unbinding the device from all gateways it's bound to, use the Device delete
method to delete the device.
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.
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.
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;}