private void ProcessVariables(LifxResponse msg, LifxLocalLight light)
        {
            var stateLabelResponse = msg as StateLabelResponse;
            var lightStateResponse = msg as LightStateResponse;
            var lightPowerResponse = msg as LightPowerResponse;

            if (stateLabelResponse != null)
            {
                light.Name = stateLabelResponse.Label;
                VariableChanged?.Invoke(light, Tuple.Create(light, "Name", (object)stateLabelResponse.Label));
            }
            else if (lightStateResponse != null)
            {
                var brightness = lightStateResponse.Brightness/65535d;
                var saturation = lightStateResponse.Saturation/65535d;
                var kelvin = lightStateResponse.Kelvin;
                double hue = lightStateResponse.Hue;

                var color = new HsbkColor
                {
                    Hue = hue,
                    Saturation = saturation,
                    Brightness = brightness,
                    Kelvin = kelvin
                };
                
                var hexColor = color.ToRgb().ToString();

                light.Name = lightStateResponse.Label;
                light.Color = color;
                VariableChanged?.Invoke(light, Tuple.Create(light, "Color", (object)hexColor));
                VariableChanged?.Invoke(light, Tuple.Create(light, "Brightness", (object)Math.Round(brightness, 2)));
                VariableChanged?.Invoke(light, Tuple.Create(light, "Name", (object)lightStateResponse.Label));
                VariableChanged?.Invoke(light, Tuple.Create(light, "IsOn", (object)lightStateResponse.IsOn));
            }
            else if (lightPowerResponse != null)
            {
                VariableChanged?.Invoke(light, Tuple.Create(light, "IsOn", (object)lightPowerResponse.IsOn));
            }
        }
        public override async Task StartAsync(CancellationToken cancellationToken)
        {
            await Task.Delay(TimeSpan.FromSeconds(1), cancellationToken).ContinueWith(_ => { });

            FindLocalBulbs(cancellationToken);
            FindCloudBulbsAsync(cancellationToken).ConfigureAwait(false);

            await Task.Delay(TimeSpan.FromSeconds(1), cancellationToken).ContinueWith(_ => { });

            var backupDto = _deviceConfigurationBackupService.Get <LocalLifxLightConfigurationBackupDto>(Name);

            if (backupDto != null)
            {
                foreach (var ipAddress in backupDto.LocalIpAddresses)
                {
                    var temporaryDevice = new LifxLocalLight
                    {
                        Endpoint = new IPEndPoint(IPAddress.Parse(ipAddress), 56700)
                    };

                    await _localClient.GetLightStateAsync(temporaryDevice);
                }
            }
        }
Example #3
0
 internal LifxDevice(LifxLocalLight light)
 {
     Id     = light.Id;
     Source = LifxSource.Lan;
 }
 public async Task SetPowerAsync(LifxLocalLight bulb, bool isOn)
 {
     //await SendAsync(device, new LifxMessageSetPower(isOn));
     await SendAsync(bulb, new LifxMessageSetPower(isOn, TimeSpan.FromMilliseconds(200)));
 }
        public async Task SendAsync(LifxLocalLight bulb, LifxMessage message)
        {
            bulb.Mac.CopyTo(message.Address.Target, 0);

            await SendAsync(bulb.Endpoint, message);
        }
 public async Task GetLightStateAsync(LifxLocalLight bulb)
 {
     await SendAsync(bulb, new LifxMessageGetColor());
 }
        public async Task SetColorAsync(LifxLocalLight bulb, HsbkColor color, TimeSpan transitionDuration)
        {
            var duration = (uint)Math.Max(0, Math.Min(uint.MaxValue, transitionDuration.TotalMilliseconds));

            await SendAsync(bulb, new LifxMessageSetColor(color, duration));
        }
 public async Task SetPowerAsync(LifxLocalLight bulb, TimeSpan transitionDuration, bool isOn)
 {
     await SendAsync(bulb, new LifxMessageSetPower(isOn, transitionDuration));
 }
 internal LifxDevice(LifxLocalLight light)
 {
     Id = light.Id;
     Source = LifxSource.Lan;
 }
 /// <summary>
 /// Gets the current state of the bulb
 /// </summary>
 /// <param name="bulb"></param>
 /// <returns></returns>
 public async Task GetLightStateAsync(LifxLocalLight bulb)
 {
     var header = new FrameHeader
     {
         Identifier = (uint)_randomizer.Next(),
         AcknowledgeRequired = false
     };
     await BroadcastMessageAsync(bulb.HostName, header, MessageType.LightGet);
 }
        /// <summary>
        /// Sets color and temperature for a bulb and uses a transition time to the provided state
        /// </summary>
        /// <param name="bulb">Light bulb</param>
        /// <param name="hue">0..65535</param>
        /// <param name="saturation">0..65535</param>
        /// <param name="brightness">0..65535</param>
        /// <param name="kelvin">2700..9000</param>
        /// <param name="transitionDuration"></param>
        /// <returns></returns>
        public async Task SetColorAsync(LifxLocalLight bulb,
            ushort hue,
            ushort saturation,
            ushort brightness,
            ushort kelvin,
            TimeSpan transitionDuration)
        {
            if (transitionDuration.TotalMilliseconds > uint.MaxValue ||
                transitionDuration.Ticks < 0)
                throw new ArgumentOutOfRangeException(nameof(transitionDuration));
            if (kelvin < 2500 || kelvin > 9000)
            {
                throw new ArgumentOutOfRangeException(nameof(kelvin), "Kelvin must be between 2500 and 9000");
            }

            System.Diagnostics.Debug.WriteLine("Setting color to {0}", bulb.HostName);
            FrameHeader header = new FrameHeader()
            {
                Identifier = (uint)_randomizer.Next(),
                AcknowledgeRequired = true
            };
            uint duration = (uint)transitionDuration.TotalMilliseconds;

            await BroadcastMessageAsync(bulb.HostName, header,
                MessageType.LightSetColor, (byte)0x00, //reserved
                    hue, saturation, brightness, kelvin, //HSBK
                    duration
            );
        }
        public async Task SetLightPowerAsync(LifxLocalLight bulb, TimeSpan transitionDuration, bool isOn)
        {
            if (transitionDuration.TotalMilliseconds > uint.MaxValue || transitionDuration.Ticks < 0)
            {
                throw new ArgumentOutOfRangeException("transitionDuration");
            }

            var header = new FrameHeader()
            {
                Identifier = (uint)_randomizer.Next(),
                AcknowledgeRequired = true
            };

            await BroadcastMessageAsync(
                bulb.HostName,
                header,
                MessageType.LightSetPower,
                (ushort)(isOn ? 65535 : 0),
                (ushort)transitionDuration.TotalMilliseconds
            ).ConfigureAwait(false);
        }
        /// <summary>
        /// Sets the device power state
        /// </summary>
        /// <param name="device"></param>
        /// <param name="isOn"></param>
        /// <returns></returns>
        public async Task SetDevicePowerStateAsync(LifxLocalLight device, bool isOn)
        {
            FrameHeader header = new FrameHeader
            {
                Identifier = (uint)_randomizer.Next(),
                AcknowledgeRequired = true
            };

            await BroadcastMessageAsync(device.HostName, header, MessageType.DeviceSetPower, (ushort)(isOn ? 65535 : 0));
        }
        private LifxLocalLight ProcessDeviceDiscoveryMessage(IPEndPoint remoteAddress, LifxResponse msg)
        {
            LifxLocalLight light;
            if (_discoveredBulbs.TryGetValue(remoteAddress.ToString(), out light))
            {
                light.LastSeen = DateTime.UtcNow;
                return light;
            }

            var device = new LifxLocalLight
            {
                HostName = remoteAddress,
                LastSeen = DateTime.UtcNow,
                Id = string.Join("", msg.Header.TargetMacAddress.Select(b => b.ToString("x2")))
            };
            
            _discoveredBulbs[remoteAddress.ToString()] = device;

            DeviceDiscovered?.Invoke(this, device);
            return device;
        }