c# - 如何通过 C# 代码检查 Azure IoT 中心门户中 IoT 设备的状态(启用或禁用)?

标签 c# azure iot azure-iot-hub

我正在编写 C# 代码,需要检查 Azure 中心是否禁用了 IoT 设备。请帮助我使用 C# 代码片段,如果设备已启用和禁用,我如何在代码中检查它。目前我正在使用 10 台设备进行测试。请帮忙。

最佳答案

我引用了这篇文章(How to disable Enable connection to IoT Hub?),我想我们可以使用下面的代码来测试它。

官方文档:

1. Device Class

2. DeviceStatus Enum

相关帖子

1. How to disable Enable connection to IoT Hub?

    Dictionary<string,int> dic = new Dictionary<string, int>();
    // key-value    deviceid--status
    dic.Add("deviceid1",-1);
    ...
    dic.Add("deviceid10", -1);
    // init RegistryManager 
    var registryManager = RegistryManager.CreateFromConnectionString(iotHubConnectionString);
    Device device = null;
    foreach (var item in dic)
    {
        device= registryManager.GetDeviceAsync(item.Key);
        if (device != null)
        {
            dic[item.Key] = device.Status.DeviceStatus;
        }
        if (dic[item.Key] == -1)
        {
            Console.WriteLine("deviceid=" + item.Key + "  ,  device not found");
        }
        else {
            Console.WriteLine("deviceid=" + item.Key + "  ,  status=" + (dic[item.Key] == 1 ? "Disabled" : "Enabled"));
        }
    }

enter image description here

关于c# - 如何通过 C# 代码检查 Azure IoT 中心门户中 IoT 设备的状态(启用或禁用)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65426390/

相关文章:

c# - WPF 在控件中添加自定义属性

c# - 创建 Thickness() 时如何使用合格的 Double?

c# - Winform字符间距

azure - Windows Azure 实例到底是什么?

cloud - 具有主/从设置的网状网络

.net - ESP8266 - .Net 可行吗?

c# - : Azure service client and device client 之间的区别

c# - for循环中事件处理程序的匿名方法

Azure函数预编译timerTrigger错误

python - 如何在 Python 中将 Azure Blob 文件 CSV 转换为 Excel