- Azure IoT Development Cookbook
- Yatish Patil
- 294字
- 2021-07-02 20:50:36
How to do it...
In this section, we will start with registering the new device, followed by some more device management operations such as: retrieve, delete, list, export, and import.
- Create a device identity by initializing the Azure IoT Hub registry connection:
string deviceId = "myFirstDevice";
registryManager = RegistryManager.CreateFromConnectionString(connectionString);
Device device = new Device();
try
{
device = await registryManager.AddDeviceAsync(new Device(deviceId));
success = true;
}
catch (DeviceAlreadyExistsException)
{
success = false;
}
- Retrieve the device identity by deviceId:
string deviceId = "myFirstDevice";
Device device = new Device();
try
{
device = await registryManager.GetDeviceAsync(deviceId);
}
catch (DeviceAlreadyExistsException)
{
return device;
}
- Delete the device identity:
string deviceId = "myFirstDevice";
Device device = new Device();
try
{
device = GetDevice(deviceId);
await registryManager.RemoveDeviceAsync(device);
success = true;
}
catch (Exception ex)
{
success = false;
}
- Disable the IoT device from the device registry. This can be handled through an IoT solution in certain scenarios, such as if the device is tampered with or these are some critical issues with the device:
string deviceId = "myFirstDevice";
Device device = new Device();
try
{
device = GetDevice(deviceId);
device.Status = DeviceStatus.Disabled;
// Update the device registry
await registryManager.UpdateDeviceAsync(device);
success = true;
}
catch (Exception ex)
{
success = false;
}
- List up to 1000 identities:
try
{
var devicelist = registryManager.GetDevicesAsync(1000);
return devicelist.Result;
}
catch (Exception ex)
{
//
- Export all the identities to Azure Blob storage:
var blobClient = storageAccount.CreateCloudBlobClient();
string Containername = "iothubdevices";
//Get a reference to a container
var container = blobClient.GetContainerReference(Containername);
container.CreateIfNotExists();
//Generate a SAS token
var storageUri = GetContainerSasUri(container);
await registryManager.ExportDevicesAsync(storageUri, "devices1.txt", false);
}
- Import all the identities to Azure Blob storage:
await registryManager.ImportDevicesAsync(storageUri, OutputStorageUri);
The output of ImportDevice methods creates a job which can be used to perform bulk operation like create and delete.
推薦閱讀
- Debian 7:System Administration Best Practices
- Android開發(fā)精要
- Interactive Data Visualization with Python
- Mastering phpMyAdmin 3.4 for Effective MySQL Management
- Web交互界面設(shè)計(jì)與制作(微課版)
- Python零基礎(chǔ)快樂學(xué)習(xí)之旅(K12實(shí)戰(zhàn)訓(xùn)練)
- Access 2016數(shù)據(jù)庫管
- Node.js區(qū)塊鏈開發(fā)
- Practical GIS
- 愛上C語言:C KISS
- Java EE Web應(yīng)用開發(fā)基礎(chǔ)
- Spring Boot從入門到實(shí)戰(zhàn)
- 3D Printing Designs:Octopus Pencil Holder
- 一覽眾山小:ASP.NET Web開發(fā)修行實(shí)錄
- ASP.NET jQuery Cookbook(Second Edition)