.NET 从 Microsoft.Azure.Devices.Client.ModuleClient 获取 DeviceId

.NET get DeviceId from Microsoft.Azure.Devices.Client.ModuleClient

我想从 Microsoft.Azure.Devices.Client.ModuleClient 获取从 IoT-Hub 分配到我的模块的分配设备 ID。

在运行时检索设备 ID 的最佳方法是什么?

所以我们必须使用后端解决方案来读取模块孪生属性,例如 deviceId。 这是设计使然,您必须使用以下文档中所述的后端应用程序:Module twins,在模块应用程序内部,只有 Desired 和 Reported 可用。

请参考Get started with IoT Hub module identity and module twin (.NET)

示例控制台 C# 应用程序: 利用 'registryManager' 在 RegistryManager class 中使用 query language. The query functionality is exposed by the C# service SDK 查询所有设备。

var query = registryManager.CreateQuery("SELECT * FROM devices", 100);

完整的示例控制台 C# 代码片段如下所示。 [使用 Microsoft.Azure.Devices;]

using Microsoft.Azure.Devices;
using Microsoft.Azure.Devices.Common.Exceptions;
using Newtonsoft.Json;
using System;
using System.Threading.Tasks;

namespace CreateIds
{
    class Program
    {
        const string connectionString ="<>";     
        static void Main(string[] args)
        {
            GetDeviceIdAsync().Wait();
        }
        private static async Task GetDeviceIdAsync()
        {
            RegistryManager registryManager =
              RegistryManager.CreateFromConnectionString(connectionString);
            try
            {
                
                var query = registryManager.CreateQuery("SELECT * FROM devices", 100);
                while (query.HasMoreResults)
                {
                    var page = await query.GetNextAsTwinAsync();
                    foreach (var twin in page)
                    {
                        Console.WriteLine(JsonConvert.SerializeObject(twin.DeviceId));
                    }
                }
            }
            catch (DeviceAlreadyExistsException dvcEx)
            {
                Console.WriteLine("Error : {0}", dvcEx);
            }

        }
    }
}

更新 1: 使用 'Where' 子句获取 deviceId 来自 devices.modules 的示例 json twin 如下所示。现在我们必须使用 'Where' 子句进行查询,例如 moduleId='sampleModule'

Sample:{"deviceId":"devkitdps","moduleId":"sampleModule","etag":"AAAAAAAAAAE=","version":4,"status":"enabled","statusUpdateTime":"0001-01-01T00:00:00Z","connectionState":"Disconnected","lastActivityTime":"0001-01-01T00:00:00Z","cloudToDeviceMessageCount":0,"authenticationType":"sas","x509Thumbprint":{"primaryThumbprint":null,"secondaryThumbprint":null},"properties":{"desired":{"$metadata":{"$lastUpdated":"0001-01-01T00:00:00Z"},"$version":1},"reported":{"DateTimeLastAppLaunch":"2020-06-23T17:22:19.4938255-07:00","$metadata":{"$lastUpdated":"2020-06-24T00:22:19.6523448Z","DateTimeLastAppLaunch":{"$lastUpdated":"2020-06-24T00:22:19.6523448Z"}},"$version":3}}}

  var query = registryManager.CreateQuery("SELECT * FROM devices.modules Where moduleId='sampleModule'", 100);
                while (query.HasMoreResults)
                {
                    var page = await query.GetNextAsTwinAsync();
                    foreach (var twin in page)
                    {
                        Console.WriteLine(JsonConvert.SerializeObject(twin.DeviceId));
                    }
                }

如果您需要进一步的帮助,请告诉我们!