如何禁用启用到IoT中心的连接? [英] How to disable Enable connection to IoT Hub?

查看:72
本文介绍了如何禁用启用到IoT中心的连接?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在通过设备预配置服务成功将Azure IoT中心中的设备重新记录,但是我想禁用该属性

I am rehistering succesfully a device in a Azure IoT hub via device provisioning service, but i want to disable the property

启用与IoT中心的连接

Enable connection to IoT Hub

禁用每个已注册的设备.

to disable for every registered device.

有什么办法可以从代码中做到这一点.

Is there any way to do this from the code.

推荐答案

是的,有很多可用的不同语言的库.

Yes, there are plenty of libraries available in different different languages.

我正在使用C#的RegistryManager类.这是链接!.让我分享我正在使用的C#代码,

I am using RegistryManager class of C#. Here's a link!. Let me share C# code which I am using for same,

using System;
using System.IO;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
using Microsoft.Azure.Devices;

public static class EnableDevice
{
    static RegistryManager registryManager;
    static string iotHubConnectionString = Environment.GetEnvironmentVariable("iotHubConnectionString");

    [FunctionName("EnableDevice")]
    public static async Task<IActionResult> Run(
        [HttpTrigger(AuthorizationLevel.Function, "get", Route = null)] HttpRequest req,
        ILogger log)
    {
        JObject response = new JObject();
        try
        {
            string deviceId = req.Query["device_id"];
            if (string.IsNullOrEmpty(deviceId))
            {
                response.Add("message", "Please provide valid device_id in request params or in the request body");
                response.Add("code", 400);
                return new BadRequestObjectResult(response);
            }

            registryManager = RegistryManager.CreateFromConnectionString(iotHubConnectionString);
            Device device = await registryManager.GetDeviceAsync(deviceId);
            if (device == null)
            {
                response.Add("message", $"Error while enabling device: Device with {deviceId} not found.");
                response.Add("code", 400);
                return new BadRequestObjectResult(response);
            }
            device.Status = DeviceStatus.Enabled;  // DeviceStatus.Disabled to Disable device
            await registryManager.UpdateDeviceAsync(device);

            response.Add("message", "Device enabled successfully");
            response.Add("code", 200);
            return new OkObjectResult(response);
        }
        catch (Exception e)
        {
            response.Add("message", e.Message);
            response.Add("stacktrace", e.StackTrace);
            response.Add("code", 500);
            return new BadRequestObjectResult(response);
        }
    }
}

这篇关于如何禁用启用到IoT中心的连接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆