public async Task <bool> SelectDeviceAsync(string label)
        {
            LifxDevice device = await GetDeviceByLabelAsync(label);

            if (device == null)
            {
                Debug.WriteLine($"No device was found with label {label}.");
                return(false);
            }

            _selected = device;
            return(true);
        }
        void _client_DeviceLost(object sender, LifxClient.DeviceDiscoveryEventArgs e)
        {
            LightBulb bulb = e.Device as LightBulb;

            if (_bulbs.Contains(bulb))
            {
                _devices.RemoveAll(x => x.MacAddressName == bulb.MacAddressName);

                if (bulb.MacAddressName == _selected.MacAddressName)
                {
                    _selected = null;
                }

                _bulbs.Remove(bulb);
            }
        }
Ejemplo n.º 3
0
        public async Task <IGetResponse> GetSelected()
        {
            using (ILifetimeScope scope = DI.Container.BeginLifetimeScope())
            {
                ILifxManager lifxMan = scope.Resolve <ILifxManager>();

                LifxDevice device = await lifxMan.GetSelectedDeviceAsync();

                if (device == null)
                {
                    return(new GetResponse(GetResponse.ResponseStatus.OK, "No Lifx device has been selected."));
                }
                else
                {
                    return(new GetResponse(GetResponse.ResponseStatus.OK, device));
                }
            }
        }
        public async Task SetDeviceRgbColorAsync(string label, Windows.UI.Color color)
        {
            LifxDevice device = await GetDeviceByLabelAsync(label);

            if (device == null)
            {
                Debug.WriteLine($"Device with label {label} was not found.");
            }

            JObject jsonReq = new JObject();

            jsonReq.Add("power", "on");
            jsonReq.Add("color", $"rgb:{color.R},{color.G},{color.B}");
            jsonReq.Add("brightness", 1);
            jsonReq.Add("duration", 1);

            string jsonData = jsonReq.ToString();

            HttpContent         content  = new StringContent(jsonReq.ToString(), Encoding.UTF8, "application/json");
            HttpResponseMessage response = await _httpClient.PutAsync(ENDPOINT + $"/id:{device.Id}/state", content);
        }
        async void _client_DeviceDiscovered(object sender, LifxClient.DeviceDiscoveryEventArgs e)
        {
            LightBulb bulb = e.Device as LightBulb;

            if (!_bulbs.Contains(bulb))
            {
                _bulbs.Add(bulb);

                LifxDevice device = new LifxDevice
                {
                    MacAddressName = bulb.MacAddressName
                };

                device.Label = await _client.GetDeviceLabelAsync(bulb);

                if (device.Label == _initLabel && _selected == null)
                {
                    _selected = device;
                }
                _devices.Add(device);
            }
        }
        public async Task SetDeviceRgbColorAsync(string label, Windows.UI.Color color)
        {
            LifxDevice lifx = await GetDeviceByLabelAsync(label);

            if (lifx == null)
            {
                Debug.WriteLine($"Device with label {label} was not found.");
                return;
            }

            LightBulb bulb = GetBulb(lifx);

            LifxNet.Color lifxColor = new LifxNet.Color
            {
                R = color.R,
                G = color.G,
                B = color.B
            };

            // Allowed values for kelvin parameter are between 2500 and 9000.
            Task task = _client.SetColorAsync(bulb, lifxColor, 5000, TimeSpan.FromSeconds(1));

            await Task.WhenAny(task, Task.Delay(5000));
        }
 LightBulb GetBulb(LifxDevice device)
 {
     return(_bulbs.Where(x => x.MacAddressName == device.MacAddressName).FirstOrDefault());
 }
        public async void StartAsync()
        {
            /* Values gathered from previous reading cycle. */
            double?prevHumidity        = null;
            double?previousTemperature = null;
            int?   previousSensorId    = null;

            using (ILifetimeScope scope = DI.Container.BeginLifetimeScope())
            {
                ILifxManager           lifx         = scope.Resolve <ILifxManager>();
                IDewPointCalculator    dewPointCalc = scope.Resolve <IDewPointCalculator>();
                IColorCalculator       colorCalc    = scope.Resolve <IColorCalculator>();
                IGatewayManager        gateway      = scope.Resolve <IGatewayManager>();
                IConfigurationProvider config       = scope.Resolve <IConfigurationProvider>();
                IHttpServer            http         = scope.Resolve <IHttpServer>();

                await http.ListenAsync(8800, "api");

                gateway.SetDefaultBoard(config.DefaultSensorBoardId);

                try
                {
                    Dictionary <string, string> parameters = new Dictionary <string, string>
                    {
                        { "OAUTH2_TOKEN", config.LifxHttpApiOAuth2Token },
                        { "SELECTED_DEVICE", config.DefaultLifxDeviceLabel }
                    };

                    await lifx.InitAsync(parameters);

                    await gateway.InitAsync();

                    while (true)
                    {
                        try
                        {
                            IEnumerable <BoardReading> readings = await gateway.GetBoardReadingsAsync();

                            DataTextBlock.Text = readings.Count().ToString();

                            SensorsTextBlock.Text = string.Join(", ", gateway.GetBoards());

                            int?boardId = gateway.GetSelectedBoard();

                            BoardReading reading = readings
                                                   .Where(x => x.Id == boardId)
                                                   .FirstOrDefault();

                            // IF: There is a reading for the selected sensor.
                            if (reading != null)
                            {
                                double dewPoint = dewPointCalc.Calculate(reading.Humidity.Value, reading.Temperature.Value);

                                HumidityTextBlock.Text    = reading.Humidity.ToString();
                                TemperatureTextBlock.Text = reading.Temperature.ToString();
                                DewPointTextBlock.Text    = dewPoint.ToString();
                                SensorIdTextBlock.Text    = boardId.ToString();

                                /* IF: Values have been changed or sensor has been changed. */
                                if (reading.Humidity != prevHumidity || reading.Temperature != previousTemperature || previousSensorId != boardId)
                                {
                                    Color color = colorCalc.CalculateDewPointColor(dewPoint);

                                    await lifx.SetSelectedDeviceRgbColorAsync(color);

                                    IEnumerable <LifxDevice> lifxDevices = await lifx.GetAllDevicesAsync(false);

                                    if (lifxDevices != null)
                                    {
                                        BulbsTextBlock.Text = string.Join(", ", lifxDevices.Select(x => x.Label));
                                    }

                                    LifxDevice selected = await lifx.GetSelectedDeviceAsync();

                                    if (selected != null)
                                    {
                                        SelectedTextBlock.Text = selected.Label;
                                    }

                                    RedTextBlock.Text   = color.R.ToString();
                                    GreenTextBlock.Text = color.G.ToString();
                                    BlueTextBlock.Text  = color.B.ToString();

                                    HumidityRectangle.Fill = new SolidColorBrush(color);

                                    prevHumidity        = reading.Humidity;
                                    previousTemperature = reading.Temperature;
                                    previousSensorId    = boardId;
                                }
                            }
                        }
                        catch (Exception e)
                        {
                            ErrorTextBlock.Text = e.Message;
                        }
                    }
                }
                catch (Exception e)
                {
                    ErrorTextBlock.Text = e.Message;
                }
            }
        }