How can I enroll a device to a group enrollment in Azure IOT hub?

拥有回忆 提交于 2019-12-11 15:38:14

问题


I have seen code for individual enrollment but I can't find any for group enrollment. I need to bulk enroll a thousand devices to Azure IOT Hub and was thinking of group enrollment.Any sample code will be appreciated.


回答1:


It should be possible both with group enrollment and bulk individual enrollments. From the samples related to How to manage device enrollments with Azure Device Provisioning Service SDKs:

Bulk Individual Enrollments

public async Task<List<IndividualEnrollment>> CreateBulkIndividualEnrollmentsAsync()
{
    Console.WriteLine("\nCreating a new set of individualEnrollments...");

    List<IndividualEnrollment> individualEnrollments = new List<IndividualEnrollment>();

    foreach (var item in _registrationIds)
    {
        Attestation attestation = new TpmAttestation(item.Value);

        individualEnrollments.Add(new IndividualEnrollment(item.Key, attestation));
    }

    Console.WriteLine("\nRunning the bulk operation to create the individualEnrollments...");

    BulkEnrollmentOperationResult bulkEnrollmentOperationResult =

    await _provisioningServiceClient.RunBulkEnrollmentOperationAsync(BulkOperationMode.Create, individualEnrollments).ConfigureAwait(false);

    Console.WriteLine("\nResult of the Create bulk enrollment.");

    Console.WriteLine(bulkEnrollmentOperationResult);

    return individualEnrollments;
}

Create Enrollment Group

public async Task CreateEnrollmentGroupAsync()
{
    Console.WriteLine("\nCreating a new enrollmentGroup...");

    Attestation attestation = X509Attestation.CreateFromRootCertificates(_groupIssuerCertificate);

    EnrollmentGroup enrollmentGroup =
        new EnrollmentGroup(
            EnrollmentGroupId,
            attestation);

    Console.WriteLine(enrollmentGroup);

    Console.WriteLine("\nAdding new enrollmentGroup...");

    EnrollmentGroup enrollmentGroupResult =
        await _provisioningServiceClient.CreateOrUpdateEnrollmentGroupAsync(enrollmentGroup).ConfigureAwait(false);

    Console.WriteLine("\nEnrollmentGroup created with success.");

    Console.WriteLine(enrollmentGroupResult);
}

Update

Take a look at the device samples. You don't need to specify the enrollment type (individual/group) when registering the device. the correlation to the defined enrollment in the portal is done using the certificate the device uses when it registers.

Update 2

See Quickstart: Control a device connected to an IoT hub (.NET) to see how to communicate with a device that was already enrolled to IoT Hub

Hope it helps!



来源:https://stackoverflow.com/questions/53845505/how-can-i-enroll-a-device-to-a-group-enrollment-in-azure-iot-hub

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