Пример #1
0
        static async Task Main(string[] args)
        {
            var dc = DeviceClient.CreateFromConnectionString(CS, TransportType.Mqtt, new ClientOptions {
                ModelId = mid
            });
            var client = new PnPClient(dc);

            await client.ReportComponentPropertyAsync("deviceInfo", "manufacturer", "rido");

            var manufacturer = await client.ReadReportedComponentPropertyAsync <string>("deviceInfo", "manufacturer");

            await client.SendComponentTelemetryValueAsync("themorstat1", JsonConvert.SerializeObject(new { temperature = 11 }));

            await client.SendComponentTelemetryValueAsync("themorstat2", JsonConvert.SerializeObject(new { temperature = 22 }));

            client.SetDesiredPropertyUpdateCommandHandler("thermostat1", async(twin) => {
                Console.WriteLine("T1 " + twin.ToJson());
                var targetTemp1 = twin.GetPropertyValue <int>("thermostat1", "targetTemperature");
                await client.AckDesiredPropertyReadAsync("thermostat1", "targetTemperature", targetTemp1, StatusCodes.Completed, "tt1 received", twin.Version);
                Console.WriteLine(targetTemp1);
            });

            client.SetDesiredPropertyUpdateCommandHandler("thermostat2", async(twin) => {
                Console.WriteLine("T2 " + twin.ToJson());
                var targetTemp2 = twin.GetPropertyValue <int>("thermostat2", "targetTemperature");
                await client.AckDesiredPropertyReadAsync("thermostat2", "targetTemperature", targetTemp2, StatusCodes.Completed, "tt2 received", twin.Version);
                Console.WriteLine(targetTemp2);
            });

            await client.SetCommandHandlerAsync("reboot", async (req, ctx) => {
                Console.WriteLine(req.Name);
                Console.WriteLine(req.DataAsJson);
                await Task.Delay(1);
                return(new MethodResponse(UTF8Encoding.UTF8.GetBytes("{}"), 200));
            }, null);

            await client.SetComponentCommandHandlerAsync("thermostat1", "getMaxMinReport", async (req, ctx) => {
                Console.WriteLine(req.Name);
                Console.WriteLine(req.DataAsJson);
                await Task.Delay(1);
                return(new MethodResponse(UTF8Encoding.UTF8.GetBytes("{}"), 200));
            }, null);

            await client.SetComponentCommandHandlerAsync("thermostat2", "getMaxMinReport", async (req, ctx) => {
                Console.WriteLine(req.Name);
                Console.WriteLine(req.DataAsJson);
                await Task.Delay(1);
                return(new MethodResponse(UTF8Encoding.UTF8.GetBytes("{}"), 200));
            }, null);

            Console.ReadLine();
        }
Пример #2
0
        public async Task ComponentSendTelemetry()
        {
            await pnpClient.SendComponentTelemetryValueAsync("c1", "{t1:2}");

            Assert.Equal("application/json", mockClient.MessageSent.ContentType);
            Assert.Equal("utf-8", mockClient.MessageSent.ContentEncoding);
            Assert.Equal("c1", mockClient.MessageSent.Properties["$.sub"]);
            Assert.Equal("{t1:2}", Encoding.UTF8.GetString(mockClient.MessageSent.GetBytes()));
        }
Пример #3
0
        private async Task EnterDeviceLoopAsync()
        {
            await Task.Run(async() =>
            {
                this.logger.LogWarning("Entering Device Loop");
                while (!this.quitSignal.IsCancellationRequested)
                {
                    temperatureSeries1.Add(DateTime.Now, CurrentTemperature1);
                    temperatureSeries2.Add(DateTime.Now, CurrentTemperature2);

                    await pnpClient.SendTelemetryValueAsync(
                        JsonConvert.SerializeObject(new { workingSet = Environment.WorkingSet }));

                    await pnpClient.SendComponentTelemetryValueAsync("thermostat1",
                                                                     JsonConvert.SerializeObject(new { temperature = CurrentTemperature1 }));

                    await pnpClient.SendComponentTelemetryValueAsync("thermostat2",
                                                                     JsonConvert.SerializeObject(new { temperature = CurrentTemperature2 }));

                    logger.LogInformation($"Telemetry sent. t1 {CurrentTemperature1}, t2 {CurrentTemperature2}, ws:{Environment.WorkingSet}");
                    await Task.Delay(1000);
                }
            });
        }