Exemplo n.º 1
0
        /// <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(LightBulb bulb,
                                        ushort hue,
                                        ushort saturation,
                                        ushort brightness,
                                        ushort kelvin,
                                        TimeSpan transitionDuration)
        {
            if (bulb == null)
            {
                throw new ArgumentNullException(nameof(bulb));
            }
            if (transitionDuration.TotalMilliseconds > uint.MaxValue ||
                transitionDuration.Ticks < 0)
            {
                throw new ArgumentOutOfRangeException("transitionDuration");
            }
            if (kelvin < 2500 || kelvin > 9000)
            {
                throw new ArgumentOutOfRangeException("kelvin", "Kelvin must be between 2500 and 9000");
            }

            Debug.WriteLine("Setting color to {0}", bulb.HostName);
            FrameHeader header   = new FrameHeader(GetNextIdentifier(), true);
            var         duration = (uint)transitionDuration.TotalMilliseconds;

            await BroadcastMessageAsync <AcknowledgementResponse>(bulb.HostName, header,
                                                                  MessageType.LightSetColor, (byte)0x00, //reserved
                                                                  hue, saturation, brightness, kelvin,   //HSBK
                                                                  duration
                                                                  );
        }
Exemplo n.º 2
0
        /// <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(LightBulb bulb,
                                        UInt16 hue,
                                        UInt16 saturation,
                                        UInt16 brightness,
                                        UInt16 kelvin,
                                        TimeSpan transitionDuration)
        {
            if (transitionDuration.TotalMilliseconds > UInt32.MaxValue ||
                transitionDuration.Ticks < 0)
            {
                throw new ArgumentOutOfRangeException("transitionDuration");
            }

            if (kelvin < 2500 || kelvin > 9000)
            {
                throw new ArgumentOutOfRangeException("kelvin", "Kelvin must be between 2500 and 9000");
            }

            System.Diagnostics.Debug.WriteLine("Setting color to {0}", bulb.Endpoint);

            UInt32 duration = (UInt32)transitionDuration.TotalMilliseconds;

            var payload  = new LightSetColorRequest(hue, saturation, brightness, kelvin, duration);
            var message  = LifxMessage.CreateTargeted(payload, (uint)randomizer.Next(), false, true, 0, bulb.MacAddress, bulb.SendClient);
            var response = await _client.SendMessage(message, bulb.Endpoint);
        }
Exemplo n.º 3
0
		private void ProcessDeviceDiscoveryMessage(HostName remoteAddress, string remotePort, LifxResponse msg)
		{
			if (DiscoveredBulbs.ContainsKey(remoteAddress.ToString()))  //already discovered
            {
				DiscoveredBulbs[remoteAddress.ToString()].LastSeen = DateTime.UtcNow; //Update datestamp
				return;
			}
			if (msg.Source != discoverSourceID || //did we request the discovery?
				_DiscoverCancellationSource == null ||
				_DiscoverCancellationSource.IsCancellationRequested) //did we cancel discovery?
				return;

			var device = new LightBulb()
			{
				HostName = remoteAddress,
				Service = msg.Payload[0],
				Port = BitConverter.ToUInt32(msg.Payload, 1),
				LastSeen = DateTime.UtcNow
			};
			DiscoveredBulbs[remoteAddress.ToString()] = device;
			devices.Add(device);
			if (DeviceDiscovered != null)
			{
				DeviceDiscovered(this, new DeviceDiscoveryEventArgs() { Device = device });
			}
		}
Exemplo n.º 4
0
        /// <summary>
        /// Turns a bulb on or off using the provided transition time
        /// </summary>
        /// <param name="bulb"></param>
        /// <param name="transitionDuration"></param>
        /// <param name="isOn">True to turn on, false to turn off</param>
        /// <returns></returns>
        /// <seealso cref="TurnBulbOffAsync(LightBulb, TimeSpan)"/>
        /// <seealso cref="TurnBulbOnAsync(LightBulb, TimeSpan)"/>
        /// <seealso cref="TurnDeviceOnAsync(Device)"/>
        /// <seealso cref="TurnDeviceOffAsync(Device)"/>
        /// <seealso cref="SetDevicePowerStateAsync(Device, bool)"/>
        /// <seealso cref="GetLightPowerAsync(LightBulb)"/>
        public async Task SetLightPowerAsync(LightBulb bulb, TimeSpan transitionDuration, bool isOn)
        {
            if (bulb == null)
            {
                throw new ArgumentNullException("bulb");
            }
            if (transitionDuration.TotalMilliseconds > UInt32.MaxValue ||
                transitionDuration.Ticks < 0)
            {
                throw new ArgumentOutOfRangeException("transitionDuration");
            }

            FrameHeader header = new FrameHeader()
            {
                Identifier          = GetNextIdentifier(),
                AcknowledgeRequired = true
            };

            var b = BitConverter.GetBytes((UInt16)transitionDuration.TotalMilliseconds);

            System.Diagnostics.Debug.WriteLine($"Sending LightSetPower(on={isOn},duration={transitionDuration.TotalMilliseconds}ms) to {bulb.HostName}");

            await BroadcastMessageAsync <AcknowledgementResponse>(bulb.HostName, header, MessageType.LightSetPower,
                                                                  (UInt16)(isOn ? 65535 : 0), b
                                                                  ).ConfigureAwait(false);
        }
Exemplo n.º 5
0
        private void ProcessDeviceDiscoveryMessage(HostName remoteAddress, string remotePort, LifxResponse msg)
        {
            if (DiscoveredBulbs.ContainsKey(remoteAddress.ToString()))                //already discovered
            {
                DiscoveredBulbs[remoteAddress.ToString()].LastSeen = DateTime.UtcNow; //Update datestamp
                return;
            }
            if (msg.Source != discoverSourceID ||                    //did we request the discovery?
                _DiscoverCancellationSource == null ||
                _DiscoverCancellationSource.IsCancellationRequested) //did we cancel discovery?
            {
                return;
            }

            var device = new LightBulb()
            {
                HostName = remoteAddress,
                Service  = msg.Payload[0],
                Port     = BitConverter.ToUInt32(msg.Payload, 1),
                LastSeen = DateTime.UtcNow
            };

            DiscoveredBulbs[remoteAddress.ToString()] = device;
            devices.Add(device);
            if (DeviceDiscovered != null)
            {
                DeviceDiscovered(this, new DeviceDiscoveryEventArgs()
                {
                    Device = device
                });
            }
        }
Exemplo n.º 6
0
        /// <summary>
        /// Gets the current power state for a light bulb
        /// </summary>
        /// <param name="bulb"></param>
        /// <returns></returns>
        public async Task <bool> GetLightPowerAsync(LightBulb bulb)
        {
            var payload = new LightGetPowerRequest();
            var message = LifxMessage.CreateTargeted(payload, (uint)randomizer.Next(), true, false, 0, bulb.MacAddress, bulb.SendClient);

            var response = await _client.SendMessage(message, bulb.Endpoint);

            return((response.Message.Payload is LightPowerResponse power) ? power.IsOn : throw new InvalidOperationException("wrong response"));
        }
		/// <summary>
		/// Gets the current power state for a light bulb
		/// </summary>
		/// <param name="bulb"></param>
		/// <returns></returns>
		public async Task<bool> GetLightPowerAsync(LightBulb bulb)
		{
			FrameHeader header = new FrameHeader()
			{
				Identifier = (uint)randomizer.Next(),
				AcknowledgeRequired = true
			};
			return (await BroadcastMessageAsync<LightPowerResponse>(
				bulb.HostName, header, MessageType.LightGetPower).ConfigureAwait(false)).IsOn;
		}
Exemplo n.º 8
0
        /// <summary>
        /// Sets color and temperature for a bulb and uses a transition time to the provided state
        /// </summary>
        /// <param name="bulb"></param>
        /// <param name="lifxColor"></param>
        /// <param name="kelvin"></param>
        /// <param name="transitionDuration"></param>
        /// <returns></returns>
        public Task SetColorAsync(LightBulb bulb, LifxColor lifxColor, ushort kelvin, TimeSpan transitionDuration)
        {
            if (bulb == null)
            {
                throw new ArgumentNullException(nameof(bulb));
            }
            var hsl = Utilities.RgbToHsl(lifxColor);

            return(SetColorAsync(bulb, hsl[0], hsl[1], hsl[2], kelvin, transitionDuration));
        }
Exemplo n.º 9
0
        public static LightBulb Create(IPEndPoint iPEndPoint, PhysicalAddress macAddress, byte service)
        {
            var result = new LightBulb();

            result.Endpoint = iPEndPoint;
            // mac addresses take up 8 bytes in the packet
            result.MacAddress = macAddress.GetAddressBytes().Concat(new byte[] { 0, 0 }).ToArray();
            result.Service    = service;
            return(result);
        }
Exemplo n.º 10
0
        /*
         * public async Task SetBrightnessAsync(LightBulb bulb,
         *      UInt16 brightness,
         *      TimeSpan transitionDuration)
         * {
         *      if (transitionDuration.TotalMilliseconds > UInt32.MaxValue ||
         *              transitionDuration.Ticks < 0)
         *              throw new ArgumentOutOfRangeException("transitionDuration");
         *
         *      FrameHeader header = new FrameHeader()
         *      {
         *              Identifier = (uint)randomizer.Next(),
         *              AcknowledgeRequired = true
         *      };
         *      UInt32 duration = (UInt32)transitionDuration.TotalMilliseconds;
         *      var durationBytes = BitConverter.GetBytes(duration);
         *      var b = BitConverter.GetBytes(brightness);
         *
         *      await BroadcastMessageAsync<AcknowledgementResponse>(bulb.HostName, header,
         *              MessageType.SetLightBrightness, brightness, duration
         *      );
         * }*/

        /// <summary>
        /// Gets the current state of the bulb
        /// </summary>
        /// <param name="bulb"></param>
        /// <returns></returns>
        public Task <LightStateResponse> GetLightStateAsync(LightBulb bulb)
        {
            FrameHeader header = new FrameHeader()
            {
                Identifier          = (uint)randomizer.Next(),
                AcknowledgeRequired = false
            };

            return(BroadcastMessageAsync <LightStateResponse>(
                       bulb.HostName, header, MessageType.LightGet));
        }
Exemplo n.º 11
0
        /// <summary>
        /// Gets the current state of the bulb
        /// </summary>
        /// <param name="bulb"></param>
        /// <returns></returns>
        public async Task <LightStateResponse> GetLightStateAsync(LightBulb bulb)
        {
            if (bulb == null)
            {
                throw new ArgumentNullException(nameof(bulb));
            }
            FrameHeader header = new FrameHeader(GetNextIdentifier());

            return(await BroadcastMessageAsync <LightStateResponse>(
                       bulb.HostName, header, MessageType.LightGet));
        }
Exemplo n.º 12
0
        /// <summary>
        /// Gets the current power state for a light bulb
        /// </summary>
        /// <param name="bulb"></param>
        /// <returns></returns>
        public async Task <bool> GetLightPowerAsync(LightBulb bulb)
        {
            FrameHeader header = new FrameHeader()
            {
                Identifier          = (uint)randomizer.Next(),
                AcknowledgeRequired = true
            };

            return((await BroadcastMessageAsync <LightPowerResponse>(
                        bulb.HostName, header, MessageType.LightGetPower).ConfigureAwait(false)).IsOn);
        }
Exemplo n.º 13
0
        /// <summary>
        /// Gets the current maximum power level of the Infrared channel
        /// </summary>
        /// <param name="bulb"></param>
        /// <returns></returns>
        public async Task <ushort> GetInfraredAsync(LightBulb bulb)
        {
            if (bulb == null)
            {
                throw new ArgumentNullException(nameof(bulb));
            }

            FrameHeader header = new FrameHeader(GetNextIdentifier());

            return((await BroadcastMessageAsync <InfraredStateResponse>(
                        bulb.HostName, header, MessageType.InfraredGet).ConfigureAwait(false)).Brightness);
        }
Exemplo n.º 14
0
        /// <summary>
        /// Gets the current power state for a light bulb
        /// </summary>
        /// <param name="bulb"></param>
        /// <returns></returns>
        public async Task <bool> GetLightPowerAsync(LightBulb bulb)
        {
            if (bulb == null)
            {
                throw new ArgumentNullException(nameof(bulb));
            }

            FrameHeader header = new FrameHeader(GetNextIdentifier(), true);

            return((await BroadcastMessageAsync <LightPowerResponse>(
                        bulb.HostName, header, MessageType.LightGetPower).ConfigureAwait(false)).IsOn);
        }
Exemplo n.º 15
0
        /*
         * public async Task SetBrightnessAsync(LightBulb bulb,
         *      UInt16 brightness,
         *      TimeSpan transitionDuration)
         * {
         *      if (transitionDuration.TotalMilliseconds > UInt32.MaxValue ||
         *              transitionDuration.Ticks < 0)
         *              throw new ArgumentOutOfRangeException("transitionDuration");
         *
         *      FrameHeader header = new FrameHeader()
         *      {
         *              Identifier = (uint)randomizer.Next(),
         *              AcknowledgeRequired = true
         *      };
         *      UInt32 duration = (UInt32)transitionDuration.TotalMilliseconds;
         *      var durationBytes = BitConverter.GetBytes(duration);
         *      var b = BitConverter.GetBytes(brightness);
         *
         *      await BroadcastMessageAsync<AcknowledgementResponse>(bulb.HostName, header,
         *              MessageType.SetLightBrightness, brightness, duration
         *      );
         * }*/

        /// <summary>
        /// Gets the current state of the bulb
        /// </summary>
        /// <param name="bulb"></param>
        /// <returns></returns>
        public Task <LightStateResponse> GetLightStateAsync(LightBulb bulb)
        {
            if (bulb == null)
            {
                throw new ArgumentNullException(nameof(bulb));
            }
            FrameHeader header = new FrameHeader()
            {
                Identifier          = GetNextIdentifier(),
                AcknowledgeRequired = false
            };

            return(BroadcastMessageAsync <LightStateResponse>(
                       bulb.HostName, header, MessageType.LightGet));
        }
Exemplo n.º 16
0
        public async Task SetBrightnessAsync(LightBulb bulb,
                                             ushort brightness,
                                             TimeSpan transitionDuration)
        {
            if (transitionDuration.TotalMilliseconds > UInt32.MaxValue ||
                transitionDuration.Ticks < 0)
            {
                throw new ArgumentOutOfRangeException(nameof(transitionDuration));
            }

            FrameHeader header   = new FrameHeader(GetNextIdentifier(), true);
            var         duration = (uint)transitionDuration.TotalMilliseconds;

            await BroadcastMessageAsync <AcknowledgementResponse>(bulb.HostName, header,
                                                                  MessageType.SetLightBrightness, brightness, duration
                                                                  );
        }
Exemplo n.º 17
0
        private async Task SetLightPowerAsync(LightBulb bulb, TimeSpan transitionDuration, bool isOn)
        {
            if (bulb == null)
            {
                throw new ArgumentNullException("bulb");
            }
            if (transitionDuration.TotalMilliseconds > UInt32.MaxValue ||
                transitionDuration.Ticks < 0)
            {
                throw new ArgumentOutOfRangeException("transitionDuration");
            }

            var payload = new LightSetPowerRequest(isOn, (UInt32)transitionDuration.TotalMilliseconds);
            var message = LifxMessage.CreateTargeted(payload, (uint)randomizer.Next(), false, true, 0, bulb.MacAddress, bulb.SendClient);

            var response = await _client.SendMessage(message, bulb.Endpoint);
        }
		private async Task SetLightPowerAsync(LightBulb bulb, TimeSpan transitionDuration, bool isOn)
		{
			if (bulb == null)
				throw new ArgumentNullException("bulb");
			if (transitionDuration.TotalMilliseconds > UInt32.MaxValue ||
				transitionDuration.Ticks < 0)
				throw new ArgumentOutOfRangeException("transitionDuration");

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

			var b = BitConverter.GetBytes((UInt16)transitionDuration.TotalMilliseconds);

			await BroadcastMessageAsync<AcknowledgementResponse>(bulb.HostName, header, MessageType.LightSetPower,
				(UInt16)(isOn ? 65535 : 0), b
			).ConfigureAwait(false);
		}
Exemplo n.º 19
0
        /// <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(LightBulb bulb,
                                        UInt16 hue,
                                        UInt16 saturation,
                                        UInt16 brightness,
                                        UInt16 kelvin,
                                        TimeSpan transitionDuration)
        {
            if (bulb == null)
            {
                throw new ArgumentNullException(nameof(bulb));
            }
            if (transitionDuration.TotalMilliseconds > UInt32.MaxValue ||
                transitionDuration.Ticks < 0)
            {
                throw new ArgumentOutOfRangeException("transitionDuration");
            }
            if (kelvin < 2500 || kelvin > 9000)
            {
                throw new ArgumentOutOfRangeException("kelvin", "Kelvin must be between 2500 and 9000");
            }

            System.Diagnostics.Debug.WriteLine("Setting color to {0}", bulb.HostName);
            FrameHeader header = new FrameHeader()
            {
                Identifier          = GetNextIdentifier(),
                AcknowledgeRequired = true
            };
            UInt32 duration      = (UInt32)transitionDuration.TotalMilliseconds;
            var    durationBytes = BitConverter.GetBytes(duration);
            var    h             = BitConverter.GetBytes(hue);
            var    s             = BitConverter.GetBytes(saturation);
            var    b             = BitConverter.GetBytes(brightness);
            var    k             = BitConverter.GetBytes(kelvin);

            await BroadcastMessageAsync <AcknowledgementResponse>(bulb.HostName, header,
                                                                  MessageType.LightSetColor, (byte)0x00, //reserved
                                                                  hue, saturation, brightness, kelvin,   //HSBK
                                                                  duration
                                                                  );
        }
Exemplo n.º 20
0
        private void ProcessDeviceDiscoveryMessage(IPEndPoint remoteEndpoint, LifxMessage msg)
        {
            string id = msg.Header.FrameAddress.TargetMacAddressName; //remoteAddress.ToString()

            if (DiscoveredBulbs.ContainsKey(id))                      //already discovered
            {
                DiscoveredBulbs[id].LastSeen = DateTime.UtcNow;       //Update datestamp
                DiscoveredBulbs[id].Endpoint = remoteEndpoint;

                return;
            }
            if (msg.Header.Frame.SourceIdentifier != discoverSourceID ||             //did we request the discovery?
                _DiscoverCancellationSource == null ||
                _DiscoverCancellationSource.IsCancellationRequested)                 //did we cancel discovery?
            {
                return;
            }

            if (!(msg.Payload is StateServiceResponse stateServiceResponse))
            {
                return; // not the message we were expecting
            }

            var device = new LightBulb()
            {
                Endpoint   = remoteEndpoint,
                Service    = stateServiceResponse.Service,
                LastSeen   = DateTime.UtcNow,
                MacAddress = msg.Header.FrameAddress.TargetMacAddress,
                SendClient = msg.RespondClient,
            };

            DiscoveredBulbs[id] = device;
            devices.Add(device);

            DeviceDiscovered?.Invoke(this, new DeviceDiscoveryEventArgs()
            {
                Device = device
            });
        }
Exemplo n.º 21
0
        /// <summary>
        /// Turns a bulb on or off using the provided transition time
        /// </summary>
        /// <param name="bulb"></param>
        /// <param name="transitionDuration"></param>
        /// <param name="isOn">True to turn on, false to turn off</param>
        /// <returns></returns>
        /// <seealso cref="TurnBulbOffAsync(LightBulb, TimeSpan)"/>
        /// <seealso cref="TurnBulbOnAsync(LightBulb, TimeSpan)"/>
        /// <seealso cref="TurnDeviceOnAsync(Device)"/>
        /// <seealso cref="TurnDeviceOffAsync(Device)"/>
        /// <seealso cref="SetDevicePowerStateAsync(Device, bool)"/>
        /// <seealso cref="GetLightPowerAsync(LightBulb)"/>
        public async Task SetLightPowerAsync(LightBulb bulb, TimeSpan transitionDuration, bool isOn)
        {
            if (bulb == null)
            {
                throw new ArgumentNullException(nameof(bulb));
            }
            if (transitionDuration.TotalMilliseconds > uint.MaxValue ||
                transitionDuration.Ticks < 0)
            {
                throw new ArgumentOutOfRangeException(nameof(transitionDuration));
            }

            FrameHeader header = new FrameHeader(GetNextIdentifier(), true);

            var b = BitConverter.GetBytes((ushort)transitionDuration.TotalMilliseconds);

            Debug.WriteLine(
                $"Sending LightSetPower(on={isOn},duration={transitionDuration.TotalMilliseconds}ms) to {bulb.HostName}");

            await BroadcastMessageAsync <AcknowledgementResponse>(bulb.HostName, header, MessageType.LightSetPower,
                                                                  (ushort)(isOn ? 65535 : 0), b
                                                                  ).ConfigureAwait(false);
        }
Exemplo n.º 22
0
        private void ProcessDeviceDiscoveryMessage(System.Net.IPAddress remoteAddress, int remotePort, LifxResponse msg)
        {
            string id = msg.Header.TargetMacAddressName;                 //remoteAddress.ToString()

            if (DiscoveredBulbs.ContainsKey(id))                         //already discovered
            {
                DiscoveredBulbs[id].LastSeen = DateTime.UtcNow;          //Update datestamp
                DiscoveredBulbs[id].HostName = remoteAddress.ToString(); //Update hostname in case IP changed

                return;
            }
            if (msg.Source != discoverSourceID ||                    //did we request the discovery?
                _DiscoverCancellationSource == null ||
                _DiscoverCancellationSource.IsCancellationRequested) //did we cancel discovery?
            {
                return;
            }

            var device = new LightBulb()
            {
                HostName   = remoteAddress.ToString(),
                Service    = msg.Payload[0],
                Port       = BitConverter.ToUInt32(msg.Payload, 1),
                LastSeen   = DateTime.UtcNow,
                MacAddress = msg.Header.TargetMacAddress
            };

            DiscoveredBulbs[id] = device;
            devices.Add(device);
            if (DeviceDiscovered != null)
            {
                DeviceDiscovered(this, new DeviceDiscoveryEventArgs()
                {
                    Device = device
                });
            }
        }
Exemplo n.º 23
0
        private async Task SetLightPowerAsync(LightBulb bulb, TimeSpan transitionDuration, bool isOn)
        {
            if (bulb == null)
            {
                throw new ArgumentNullException("bulb");
            }
            if (transitionDuration.TotalMilliseconds > UInt32.MaxValue ||
                transitionDuration.Ticks < 0)
            {
                throw new ArgumentOutOfRangeException("transitionDuration");
            }

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

            var b = BitConverter.GetBytes((UInt16)transitionDuration.TotalMilliseconds);

            await BroadcastMessageAsync <AcknowledgementResponse>(bulb.HostName, header, MessageType.LightSetPower,
                                                                  (UInt16)(isOn ? 65535 : 0), b
                                                                  ).ConfigureAwait(false);
        }
		/*
		public async Task SetBrightnessAsync(LightBulb bulb,
			UInt16 brightness,
			TimeSpan transitionDuration)
		{
			if (transitionDuration.TotalMilliseconds > UInt32.MaxValue ||
				transitionDuration.Ticks < 0)
				throw new ArgumentOutOfRangeException("transitionDuration");

			FrameHeader header = new FrameHeader()
			{
				Identifier = (uint)randomizer.Next(),
				AcknowledgeRequired = true
			};
			UInt32 duration = (UInt32)transitionDuration.TotalMilliseconds;
			var durationBytes = BitConverter.GetBytes(duration);
			var b = BitConverter.GetBytes(brightness);

			await BroadcastMessageAsync<AcknowledgementResponse>(bulb.HostName, header,
				MessageType.SetLightBrightness, brightness, duration
			);
		}*/

			/// <summary>
			/// Gets the current state of the bulb
			/// </summary>
			/// <param name="bulb"></param>
			/// <returns></returns>
		public Task<LightStateResponse> GetLightStateAsync(LightBulb bulb)
		{
			FrameHeader header = new FrameHeader()
			{
				Identifier = (uint)randomizer.Next(),
				AcknowledgeRequired = false
			};
			return BroadcastMessageAsync<LightStateResponse>(
				bulb.HostName, header, MessageType.LightGet);
		}
Exemplo n.º 25
0
        /// <summary>
        /// Sets color and temperature for a bulb and uses a transition time to the provided state
        /// </summary>
        /// <param name="bulb"></param>
        /// <param name="color"></param>
        /// <param name="kelvin"></param>
        /// <param name="transitionDuration"></param>
        /// <returns></returns>
        public Task SetColorAsync(LightBulb bulb, Color color, UInt16 kelvin, TimeSpan transitionDuration)
        {
            var hsl = Utilities.RgbToHsl(color);

            return(SetColorAsync(bulb, hsl[0], hsl[1], hsl[2], kelvin, transitionDuration));
        }
Exemplo n.º 26
0
 /// <summary>
 /// Sets color and temperature for a bulb
 /// </summary>
 /// <param name="bulb"></param>
 /// <param name="color"></param>
 /// <param name="kelvin"></param>
 /// <returns></returns>
 public Task SetColorAsync(LightBulb bulb, Color color, UInt16 kelvin)
 {
     return(SetColorAsync(bulb, color, kelvin, TimeSpan.Zero));
 }
Exemplo n.º 27
0
 /// <summary>
 /// Sets color and temperature for a bulb
 /// </summary>
 /// <param name="bulb"></param>
 /// <param name="lifxColor"></param>
 /// <param name="kelvin"></param>
 /// <returns></returns>
 public Task SetColorAsync(LightBulb bulb, LifxColor lifxColor, ushort kelvin) =>
 SetColorAsync(bulb, lifxColor, kelvin, TimeSpan.Zero);
Exemplo n.º 28
0
 /// <summary>
 /// Turns a bulb off using the provided transition time
 /// </summary>
 /// <seealso cref="TurnBulbOnAsync(LightBulb, TimeSpan)"/>
 /// <seealso cref="TurnDeviceOnAsync(Device)"/>
 /// <seealso cref="TurnDeviceOffAsync(Device)"/>
 /// <seealso cref="SetLightPowerAsync(LightBulb, TimeSpan, bool)"/>
 /// <seealso cref="SetDevicePowerStateAsync(Device, bool)"/>
 /// <seealso cref="GetLightPowerAsync(LightBulb)"/>
 public Task TurnBulbOffAsync(LightBulb bulb, TimeSpan transitionDuration) =>
 SetLightPowerAsync(bulb, transitionDuration, false);
Exemplo n.º 29
0
 /// <summary>
 /// Turns a bulb off using the provided transition time
 /// </summary>
 public Task TurnBulbOffAsync(LightBulb bulb, TimeSpan transitionDuration)
 {
     System.Diagnostics.Debug.WriteLine("Sending TurnBulbOff to {0}", bulb.HostName);
     return(SetLightPowerAsync(bulb, transitionDuration, false));
 }
		/// <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(LightBulb bulb,
			UInt16 hue,
			UInt16 saturation,
			UInt16 brightness,
			UInt16 kelvin,
			TimeSpan transitionDuration)
		{
			if (transitionDuration.TotalMilliseconds > UInt32.MaxValue ||
				transitionDuration.Ticks < 0)
				throw new ArgumentOutOfRangeException("transitionDuration");
			if (kelvin < 2500 || kelvin > 9000)
			{
				throw new ArgumentOutOfRangeException("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
			};
			UInt32 duration = (UInt32)transitionDuration.TotalMilliseconds;
			var durationBytes = BitConverter.GetBytes(duration);
			var h = BitConverter.GetBytes(hue);
			var s = BitConverter.GetBytes(saturation);
			var b = BitConverter.GetBytes(brightness);
			var k = BitConverter.GetBytes(kelvin);

			await BroadcastMessageAsync<AcknowledgementResponse>(bulb.HostName, header,
				MessageType.LightSetColor, (byte)0x00, //reserved
					hue, saturation, brightness, kelvin, //HSBK
					duration
			);
		}
Exemplo n.º 31
0
 /// <summary>
 /// Sets color and temperature of bulb
 /// </summary>
 /// <param name="bulb">The bulb to set</param>
 /// <param name="color">The LifxColor to set the bulb to</param>
 /// <param name="duration">An optional transition duration, in milliseconds.</param>
 /// <returns></returns>
 public Task SetColorAsync(LightBulb bulb, LifxColor color, int duration = 0)
 {
     return(SetColorAsync(bulb, (ushort)color.LifxHue, (ushort)color.LifxSaturation, (ushort)color.LifxBrightness, (ushort)color.K,
                          TimeSpan.FromMilliseconds(duration)));
 }
		/// <summary>
		/// Turns a bulb off using the provided transition time
		/// </summary>
		public Task TurnBulbOffAsync(LightBulb bulb, TimeSpan transitionDuration)
		{
			System.Diagnostics.Debug.WriteLine("Sending TurnBulbOff to {0}", bulb.HostName);
			return SetLightPowerAsync(bulb, transitionDuration, false);
		}
Exemplo n.º 33
0
 /// <summary>
 /// Turns a bulb off using the provided transition time
 /// </summary>
 public Task TurnBulbOffAsync(LightBulb bulb, TimeSpan transitionDuration)
 {
     Console.WriteLine("Sending TurnBulbOff to {0}", bulb.Endpoint);
     return(SetLightPowerAsync(bulb, transitionDuration, false));
 }
Exemplo n.º 34
0
 /// <summary>
 /// Turns a bulb on using the provided transition time
 /// </summary>
 /// <param name="bulb"></param>
 /// <param name="transitionDuration"></param>
 /// <returns></returns>
 public Task TurnBulbOnAsync(LightBulb bulb, TimeSpan transitionDuration)
 {
     System.Diagnostics.Debug.WriteLine("Sending TurnBulbOn to {0}", bulb.Endpoint);
     return(SetLightPowerAsync(bulb, transitionDuration, true));
 }
		/// <summary>
		/// Sets color and temperature for a bulb and uses a transition time to the provided state
		/// </summary>
		/// <param name="bulb"></param>
		/// <param name="color"></param>
		/// <param name="kelvin"></param>
		/// <param name="transitionDuration"></param>
		/// <returns></returns>
		public Task SetColorAsync(LightBulb bulb, Windows.UI.Color color, UInt16 kelvin, TimeSpan transitionDuration)
		{
			var hsl = Utilities.RgbToHsl(color);
			return SetColorAsync(bulb, hsl[0], hsl[1], hsl[2], kelvin, transitionDuration);
		}
		/// <summary>
		/// Sets color and temperature for a bulb
		/// </summary>
		/// <param name="bulb"></param>
		/// <param name="color"></param>
		/// <param name="kelvin"></param>
		/// <returns></returns>
		public Task SetColorAsync(LightBulb bulb, Windows.UI.Color color, UInt16 kelvin)
		{
			return SetColorAsync(bulb, color, kelvin, TimeSpan.Zero);
		}