How can I enroll a device to a group enrollment in

2019-08-25 12:00发布

问题:

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!