Beispiel #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();
        }
Beispiel #2
0
        public async Task RunAsync(string connectionString, ILogger logger, CancellationToken quitSignal)
        {
            this.quitSignal = quitSignal;
            this.logger     = logger;

            var deviceClient = await Rido.DeviceClientFactory.CreateDeviceClientAsync(connectionString, logger, modelId);

            pnpClient = PnPClient.CreateFromDeviceClient(new PnPDeviceClient(deviceClient));

            await pnpClient.SetCommandHandlerAsync("reboot", root_RebootCommandHadler, this);

            pnpClient.SetDesiredPropertyUpdateCommandHandler("thermostat1", thermostat1_OnDesiredPropertiesReceived);
            await pnpClient.SetComponentCommandHandlerAsync("thermostat1", "getMaxMinReport", thermostat1_GetMinMaxReportCommandHadler, this);

            pnpClient.SetDesiredPropertyUpdateCommandHandler("thermostat2", thermostat2_OnDesiredPropertiesReceived);
            await pnpClient.SetComponentCommandHandlerAsync("thermostat2", "getMaxMinReport", thermostat2_GetMinMaxReportCommandHadler, this);

            await InitDeviceAsync();
            await EnterDeviceLoopAsync();
        }
 public void Dispose()
 {
     mockClient = null;
     pnpClient  = null;
 }
 public PnPClientTest()
 {
     mockClient = new MockDeviceClient();
     pnpClient  = PnPClient.CreateFromDeviceClient(mockClient);
 }