...
This Node.js code sample illustrates how to create or edit a gateway:
Code Block |
---|
// const cloudRegion = 'us-central1';
// const deviceId = 'my-unauth-device';
// const gatewayId = 'my-gateway';
// const projectId = 'adjective-noun-123';
// const registryId = 'my-registry';
// const gatewayAuthMethod = 'ASSOCIATION_ONLY';
import { DeviceManagerClient } from '@clearblade/iot';
const iotClient = new DeviceManagerClient({
// optional auth parameters.
});
async function createDevice() {
// Construct request
const regPath = iotClient.registryPath(projectId, cloudRegion, registryId);
console.log('Creating gateway:', gatewayId);
let credentials = [];
// if public key format and path are specified, use those
if (publicKeyFormat && publicKeyFile) {
credentials = [
{
publicKey: {
format: publicKeyFormat,
key: readFileSync(publicKeyFile).toString()
}
}
];
}
const device = {
id: gatewayId,
credentials: credentials,
gatewayConfig: {
gatewayType: 'GATEWAY',
gatewayAuthMethod: gatewayAuthMethod
}
};
const request = {
parent: regPath,
device
};
const [response] = await iotClient.createDevice(request);
console.log('Created device:', response);
}
createDevice(); |
See Creating or editing a device to learn how to create the devices you'll use with the gateway.
...
Registries BindDeviceToGateway method to bind devices to gateways
Registries UnbindDeviceFromGateway method to unbind devices from gateways
This Node.js code sample illustrates how to bind a device to or unbind a device from a gateway:
Code Block |
---|
// const cloudRegion = 'us-central1';
// const deviceId = 'my-unauth-device';
// const gatewayId = 'my-gateway';
// const projectId = 'adjective-noun-123';
// const registryId = 'my-registry';
import { DeviceManagerClient } from '@clearblade/iot';
const iotClient = new DeviceManagerClient({
// optional auth parameters.
});
async function bindDeviceToGateway() {
// Construct request
const regPath = iotClient.registryPath(projectId, cloudRegion, registryId);
const bindRequest = {
parent: regPath,
deviceId: deviceId,
gatewayId: gatewayId
};
console.log(`Binding device: ${deviceId}`);
await iotClient.bindDeviceToGateway(bindRequest);
console.log(`Bound ${deviceId} to`, gatewayId);
}
bindDeviceToGateway(); |
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.
This Node.js code sample illustrates how to list all devices bound to the gateway:
Code Block |
---|
// const cloudRegion = 'us-central1'; // const gatewayId = 'my-gateway'; // const projectId = 'adjective-noun-123'; // const registryId = 'my-registry'; import { DeviceManagerClient } from '@clearblade/iot'; const iotClient = new DeviceManagerClient({ // optional auth parameters. }); async function listDevices() { // Construct request const parentName = iotClient.registryPath(projectId, cloudRegion, registryId); const [response] = await iotClient.listDevices({ parent: parentName, gatewayListOptions: { associationsGatewayId: gatewayId } }); const devices = response; if (devices.length > 0) { console.log('Current devices bound to gateway: ', gatewayId); } else { console.log('No devices bound to this gateway.'); } for (let i = 0; i < devices.length; i++) { const device = devices[i]; console.log(`\tDevice: ${device.numId}: ${device.id}`); } } listDevices(); |
Listing all gateways in a registry
...
Use the Device list method to list all gateways in a registry.
This Node.js code sample illustrates how to list all gateways in a registry:
Code Block |
---|
// const cloudRegion = 'us-central1';
// const projectId = 'adjective-noun-123';
// const registryId = 'my-registry';
import { DeviceManagerClient } from '@clearblade/iot';
const iotClient = new DeviceManagerClient({
// optional auth parameters.
});
async function listDevices() {
// Construct request
const registryPath = iotClient.registryPath(projectId, cloudRegion, registryId);
console.log('Current gateways in registry:');
const [response] = await iotClient.listDevices({
parent: registryPath,
fieldMask: { paths: ['config', 'gateway_config'] },
gatewayListOptions: {
gatewayType: 'GATEWAY'
}
});
const devices = response;
devices.forEach((device) => {
console.log('----\n', device);
});
}
listDevices(); |
Deleting devices bound to a gateway
...
After unbinding the device from all gateways it's bound to, use the Device delete method to delete the device.
This Node.js code samples illustrates how to delete a device bound to a gateway:
Code Block |
---|
// const cloudRegion = 'us-central1';
// const projectId = 'adjective-noun-123';
// const registryId = 'my-registry';
import { DeviceManagerClient } from '@clearblade/iot';
const iotClient = new DeviceManagerClient({
// optional auth parameters.
});
async function deleteDevice() {
// Construct request
const devPath = iotClient.devicePath(projectId, cloudRegion, registryId, deviceId);
const [responses] = await iotClient.deleteDevice({ name: devPath });
console.log('Successfully deleted device', responses);
}
deleteDevice(); |
Deleting a gateway
To delete a gateway, you first unbind its devices and 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.
This Node.js code sample illustrates how to delete a gateway:
Code Block |
---|
// const cloudRegion = 'us-central1';
// const projectId = 'adjective-noun-123';
// const registryId = 'my-registry';
import { DeviceManagerClient } from '@clearblade/iot';
const iotClient = new DeviceManagerClient({
// optional auth parameters.
});
async function deleteDevice() {
// Construct request
const devPath = iotClient.devicePath(projectId, cloudRegion, registryId, deviceId);
const [responses] = await iotClient.deleteDevice({ name: devPath });
console.log('Successfully deleted device', responses);
}
deleteDevice(); |