How to disable Enable connection to IoT Hub?

烂漫一生 提交于 2021-02-11 12:23:09

问题


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

Enable connection to IoT Hub

to disable for every registered device.

Is there any way to do this from the code.


回答1:


You can use the REST API that allows to create or update enrollments (either group enrollments or single enrollments), in particular the provisioningStatus flag. (there is a similar API for individual enrollments). Next time your device tries to use DPS to get its provisioning info, it will be denied access. If you're caching the IoT Hub credentials, you will need to use the IoT Hub REST API to disable the device (see status flag) that DPS provisioned in the registry.




回答2:


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

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);
        }
    }
}


来源:https://stackoverflow.com/questions/63255145/how-to-disable-enable-connection-to-iot-hub

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!