- 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.
推薦閱讀
- Spring Cloud Alibaba核心技術與實戰案例
- Android應用程序開發與典型案例
- vSphere High Performance Cookbook
- 跟“龍哥”學C語言編程
- JavaScript 網頁編程從入門到精通 (清華社"視頻大講堂"大系·網絡開發視頻大講堂)
- Python Game Programming By Example
- 信息安全技術
- SQL Server 2012數據庫管理與開發項目教程
- 零基礎學Python數據分析(升級版)
- HDInsight Essentials(Second Edition)
- Webpack實戰:入門、進階與調優
- RealSenseTM互動開發實戰
- Spring Boot+Vue全棧開發實戰
- Visual Basic程序設計習題與上機實踐
- GameMaker Essentials