Exemple #1
0
        // Provision a device via DPS, by sending the PnP model Id as DPS payload.
        private static async Task <DeviceRegistrationResult> ProvisionDeviceAsync(Parameters parameters, CancellationToken cancellationToken)
        {
            SecurityProvider             symmetricKeyProvider = new SecurityProviderSymmetricKey(parameters.DeviceId, parameters.DeviceSymmetricKey, null);
            ProvisioningTransportHandler mqttTransportHandler = new ProvisioningTransportHandlerMqtt();
            var pdc = ProvisioningDeviceClient.Create(parameters.DpsEndpoint, parameters.DpsIdScope, symmetricKeyProvider, mqttTransportHandler);

            var pnpPayload = new ProvisioningRegistrationAdditionalData
            {
                JsonData = $"{{ \"modelId\": \"{ModelId}\" }}",
            };

            return(await pdc.RegisterAsync(pnpPayload, cancellationToken));
        }
        static async Task Main(string[] args)
        {
            using (var security = new SecurityProviderSymmetricKey(registrationId, primaryKey, secondaryKey))
                using (var transport = new ProvisioningTransportHandlerMqtt())
                {
                    ProvisioningDeviceClient provClient = ProvisioningDeviceClient.Create(GlobalDeviceEndpoint, idScope, security, transport);
                    var pnpPayload = new ProvisioningRegistrationAdditionalData
                    {
                        JsonData = $"{{ \"modelId\": \"{ModelId}\" }}",
                    };

                    DeviceRegistrationResult result = await provClient.RegisterAsync(pnpPayload);

                    IAuthenticationMethod auth = new DeviceAuthenticationWithRegistrySymmetricKey(result.DeviceId, (security as SecurityProviderSymmetricKey).GetPrimaryKey());

                    using (iotClient = DeviceClient.Create(result.AssignedHub, auth, TransportType.Mqtt))
                    {
                        await iotClient.SetDesiredPropertyUpdateCallbackAsync(OnDesiredPropertyChangedAsync, null).ConfigureAwait(false); // callback for Device Twin updates
                        await DeviceTwinGetInitialState(iotClient);                                                                       // Get current cloud state of the device twin

                        while (true)
                        {
                            if (_temperature.IsAvailable)
                            {
                                try
                                {
                                    temperature = Math.Round(_temperature.Temperature.DegreesCelsius, 2);

                                    Console.WriteLine($"The CPU temperature is {temperature}");

                                    await SendMsgIotHub(iotClient, temperature);

                                    roomState = (int)temperature > targetTemperature ? RoomAction.Cooling : (int)temperature < targetTemperature ? RoomAction.Heating : RoomAction.Green;
                                    await UpdateRoomAction(roomState);

                                    if (temperature > maxTemperature)
                                    {
                                        maxTemperature = temperature;
                                        await UpdateDeviceTwin("maxTempSinceLastReboot", maxTemperature);
                                    }
                                }
                                catch (Exception ex)
                                {
                                    Console.WriteLine("exception msg: " + ex.Message);
                                }
                            }
                            Thread.Sleep(2000); // sleep for 2 seconds
                        }
                    }
                }
        }