Exemple #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,
                                        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);
        }
        /// <summary>
        /// Sets the device power state
        /// </summary>
        public async Task SetDevicePowerStateAsync(Device device, bool isOn)
        {
            System.Diagnostics.Debug.WriteLine("Sending TurnDeviceOff to {0}", device.Endpoint);

            var payload = new DeviceSetPowerRequest(isOn);
            var message = LifxMessage.CreateTargeted(payload, (uint)randomizer.Next(), false, true, 0, device.MacAddress, device.SendClient);
            await _client.SendMessage(message, device.Endpoint);
        }
Exemple #3
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>
        /// Sets the label on the device
        /// </summary>
        public async Task SetDeviceLabelAsync(Device device, string label)
        {
            System.Diagnostics.Debug.WriteLine("Sending SetDeviceLabelAsync to {0}", device.Endpoint);

            var payload = new DeviceSetLabelRequest(label);

            var message = LifxMessage.CreateTargeted(payload, (uint)randomizer.Next(), false, true, 0, device.MacAddress, device.SendClient);
            await _client.SendMessage(message, device.Endpoint);
        }
        /// <summary>
        /// Gets the device's host firmware
        /// </summary>
        public async Task <FirmwareVersion> GetDeviceHostFirmwareAsync(Device device)
        {
            System.Diagnostics.Debug.WriteLine("Sending GetDeviceHostFirmware to {0}", device.Endpoint);

            var payload = new DeviceGetHostFirmware();
            var message = LifxMessage.CreateTargeted(payload, (uint)randomizer.Next(), true, false, 0, device.MacAddress, device.SendClient);

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

            return((response.Message.Payload is StateHostFirmwareResponse versionResponse) ? new FirmwareVersion(versionResponse) : throw new InvalidOperationException("wrong response"));
        }
        /// <summary>
        /// Gets the label for the device
        /// </summary>
        public async Task <string> GetDeviceLabelAsync(Device device)
        {
            System.Diagnostics.Debug.WriteLine("Sending GetDeviceLabel to {0}", device.Endpoint);

            var payload = new DeviceGetLabelRequest();
            var message = LifxMessage.CreateTargeted(payload, (uint)randomizer.Next(), true, false, 0, device.MacAddress, device.SendClient);

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

            return((response.Message.Payload is StateLabelResponse label) ? label.Label : throw new InvalidOperationException("wrong response"));
        }
Exemple #7
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);
        }