How do I map IoTHub in Device Provisioning Service

2019-08-11 01:04发布

问题:

I have a Azure device provisioning service setup entitled "myDPS" and below IoT hubs are linked.

  1. IoTHub-Dev-Asia
  2. IoTHub-Prod-Europe

Currently there are no enrollment list. The below c# code I am using to enroll the device

private const string RegistrationId = "TestRegID";
private const string OptionalDeviceId = "Device1";
private const ProvisioningStatus OptionalProvisioningStatus = ProvisioningStatus.Enabled;
private const string SampleTpmEndorsementKey = "***"// Key generated using TPM Simulator

static async Task SetRegistrationDataAsync()
{
   Console.WriteLine("Starting SetRegistrationData");
   Attestation attestation = new TpmAttestation(SampleTpmEndorsementKey);
   IndividualEnrollment individualEnrollment = new 
   IndividualEnrollment(RegistrationId, attestation);
   individualEnrollment.DeviceId = OptionalDeviceId;
   individualEnrollment.ProvisioningStatus = OptionalProvisioningStatus;
   Console.WriteLine("\nAdding new individualEnrollment...");
   var serviceClient = ProvisioningServiceClient.CreateFromConnectionString(ServiceConnectionString);
   IndividualEnrollment individualEnrollmentResult =
    await  serviceClient.CreateOrUpdateIndividualEnrollmentAsync(individualEnrollment).ConfigureAwait(false);
    Console.WriteLine("\nIndividualEnrollment created with success.");
    Console.WriteLine(individualEnrollmentResult);
}

The above code successfully enrolls the device to the DPS but status shows as unassigned

Issue#1 - Enrollment status unassigned, sometimes shows FAILED Status: failed Error code: 404201

Issue#2 Once above issue resolved, then I would like to have some configuration where I can specify which device should map to which IoT, so that device can automatically decide it's target IoT hubs.

Example:

Device1->IoTHub-Dev-Asia

Device2->IoTHub-Dev-Asia

Device3->IoTHub-Dev-Europe

I assume Static configuration via the enrollment list can help but not sure how to use it?

回答1:

The three supported allocation policies determine how devices are assigned to an IoT hub:

  1. Lowest latency: Devices are provisioned to an IoT hub based on the hub with the lowest latency to the device.
  2. Evenly weighted distribution (default): Linked IoT hubs are equally likely to have devices provisioned to them. This is the default setting. If you are provisioning devices to only one IoT hub, you can keep this setting.
  3. Static configuration via the enrollment list: Specification of the desired IoT hub in the enrollment list takes priority over the DPS-level allocation policy.

If you need to assign the device ,you should use a single call to the ProvisioningDeviceClient.RegisterAsync() API. You can refer to the sample.In the sample, you need to replace the RegistrationId with that your created before.