Ejemplo n.º 1
0
        private async Task ConnectIGrill(DeviceInformation device)
        {
            igrill = IGrill.Core.IGrillFactory.FromDeviceInformation(device);

            foreach (int i in Enumerable.Range(0, igrill.ProbeCount))
            {
                ViewModel.Probes.Add(new ProbeViewModel(i));
            }

            igrill.TemperatureChanged += async(sender, args) =>
            {
                Debug.WriteLine(String.Format("{0}: Probe {1} = {2}°C", DateTime.Now, args.ProbeIndex, args.Temperature));

                await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
                {
                    ViewModel.Probes[args.ProbeIndex].Value = args.Temperature;
                });
            };
            igrill.BatteryLevelChanges += async(sender, batteryLevel) =>
            {
                await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
                {
                    ViewModel.BatteryLevel = batteryLevel;
                });
            };

            // MQTT
            igrill.TemperatureChanged += async(object sender, TemperatureChangedEventArg args) =>
            {
                await mqttService.SendProbeTemperatureAsync(args.ProbeIndex, args.Temperature);
            };
            igrill.BatteryLevelChanges += async(sender, batteryLevel) =>
            {
                await mqttService.SendBatteryLevelAsync(batteryLevel);
            };

            await igrill.ConnectAsync();

            ViewModel.Name            = igrill.DeviceName;
            ViewModel.FirmwareVersion = igrill.FirmwareVersion;
        }