示例#1
0
        public async Task NoComponentReportPropertyCollection()
        {
            var props = new Dictionary <string, object>
            {
                { "prop1", "val1" },
                { "prop2", "val2" }
            };
            await nocomp.ReportPropertyCollectionAsync(props);

            var compTwinValue1 = mockClient.ReportedCollection.GetPropertyValue <string>("prop1");

            Assert.Equal("val1", compTwinValue1);
            var compTwinValue2 = mockClient.ReportedCollection.GetPropertyValue <string>("prop2");

            Assert.Equal("val2", compTwinValue2);
        }
        public async Task RunAsync(string connectionString, ILogger logger, CancellationToken quitSignal)
        {
            this.logger = logger;

            deviceClient = DeviceClient.CreateFromConnectionString(connectionString,
                                                                   TransportType.Mqtt, new ClientOptions {
                ModelId = modelId
            });

            tempSensor = new PnPComponent(deviceClient, "tempSensor1", logger);
            diag       = new PnPComponent(deviceClient, "diag");
            deviceInfo = new PnPComponent(deviceClient, "deviceInfo");
            sdkInfo    = new PnPComponent(deviceClient, "sdkInfo");

            await deviceInfo.ReportPropertyCollectionAsync(DeviceInfo.ThisDeviceInfo.ToDictionary());

            await sdkInfo.ReportPropertyCollectionAsync(SdkInformation.ThisSdkInfo);

            await diag.SetPnPCommandHandlerAsync("reboot", Diag_RebootCommandHadler, this);

            await tempSensor.SetPnPDesiredPropertyHandlerAsync <double>("targetTemperature", tempSensor_tergetTemperature_UpdateHandler, this);

            var targetTemperature = await tempSensor.ReadDesiredPropertyAsync <double>("targetTemperature");

            await this.ProcessTempUpdateAsync(targetTemperature);

            await Task.Run(async() =>
            {
                logger.LogWarning("Entering Device Loop");
                while (!quitSignal.IsCancellationRequested)
                {
                    await tempSensor.SendTelemetryValueAsync(JsonConvert.SerializeObject(new { temperature = CurrentTemperature }));
                    await diag.SendTelemetryValueAsync(JsonConvert.SerializeObject(new { workingSet = Environment.WorkingSet }));

                    logger.LogInformation("Sending workingset and temp " + CurrentTemperature);
                    await Task.Delay(5000);
                }
            });
        }