- 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.
推薦閱讀
- JavaScript 從入門到項目實踐(超值版)
- Debian 7:System Administration Best Practices
- Effective C#:改善C#代碼的50個有效方法(原書第3版)
- Vue.js快速入門與深入實戰
- Software Testing using Visual Studio 2012
- STM32F0實戰:基于HAL庫開發
- PHP+MySQL+Dreamweaver動態網站開發實例教程
- 基于Swift語言的iOS App 商業實戰教程
- 精通Python自然語言處理
- Building an RPG with Unity 2018
- C語言開發基礎教程(Dev-C++)(第2版)
- 打開Go語言之門:入門、實戰與進階
- UI設計全書(全彩)
- Photoshop CC移動UI設計案例教程(全彩慕課版·第2版)
- 從零開始學UI:概念解析、實戰提高、突破規則