Ejemplo n.º 1
0
        private async Task <Bot> ActUponNextBulb(IBulb nextBulb)
        {
            var context     = new BulbContext(this, nextBulb);
            var nextContext = await nextBulb.Act(context);

            return(((BulbContext)nextContext).Bot);
        }
        private static void UpdateGroupName(LifxNetwork network, BulbGroup group, IBulb bulb)
        {
            var command = (SetTagLabels)PacketFactory.GetCommand(CommandType.SetTagLabels);

            command.Init(group.Bitmask, Encoding.UTF8.GetBytes(group.Name));
            network.SendCommand(bulb, command);
        }
Ejemplo n.º 3
0
 public bool Contains(IBulb bulb)
 {
     lock (_bulbCollectionLock)
     {
         return(_bulbs.ContainsKey(bulb.Mac));
     }
 }
Ejemplo n.º 4
0
 private async Task <Brain> WithBulbEvent(IBulb bulb)
 {
     return(
         OnBulbChanged != null
             ? await OnBulbChanged(this, bulb)
             : this);
 }
Ejemplo n.º 5
0
        private async Task <Brain> WithBulb(IBulb bulb)
        {
            var withBulb  = new Brain(Memory, Bulbs.Add(bulb), OnBulbChanged);
            var withEvent = await withBulb.WithBulbEvent(bulb);

            return(withEvent);
        }
Ejemplo n.º 6
0
        public Tuple <IGateway, IBulb, AnswerType> ReceivedPacket(string ipAddress, byte[] data)
        {
            var      answerType = AnswerType.None;
            IGateway gateway    = null;
            IBulb    bulb       = null;

            try
            {
                var packet = PacketFactory.GetAnswer(data);

                if (packet == null)
                {
                    return(Tuple.Create((IGateway)null, (IBulb)null, AnswerType.None));
                }

                answerType = packet.Type;
                gateway    = HandleGatewayPacket(packet.GatewayMac, ipAddress);
                bulb       = HandleBulbPacket(gateway, packet);
                HandleWifiStatePacket(bulb, packet);
                HandleBulbGroupsPacket(bulb, packet);
                HandleGroupNamePacket(packet);
                HandleAccessPoint(packet);
            }
            catch (Exception e)
            {
                Debug.WriteLine("ReceivedPacket: {0}", e.Message);
                Debug.WriteLine(e.StackTrace);
            }

            return(Tuple.Create(gateway, bulb, answerType));
        }
        private static void SetGroups(LifxNetwork network, IBulb bulb, IEnumerable <BulbGroup> groups)
        {
            var command = (SetTags)PacketFactory.GetCommand(CommandType.SetTags);

            command.Init(GetBitmask(groups));

            network.SendCommand(bulb, command);
        }
Ejemplo n.º 8
0
 public override void ProcessBulb(IBulb bulb)
 {
     Log.Debug(TAG, "Processing bulb label");
     var b = bulb as LifxBulb;
     if (b != null)
     {
         b.Label = Label;
     }
 }
Ejemplo n.º 9
0
 public override void ProcessBulb(IBulb bulb)
 {
     Log.Debug(TAG, "Processing bulb power state");
     var b = bulb as LifxBulb;
     if (b != null)
     {
         b.Power = Power;
     }
 }
Ejemplo n.º 10
0
 public override void ProcessBulb(IBulb bulb)
 {
     Log.Debug(TAG, "Processing bulb group");
     var b = bulb as LifxBulb;
     if (b != null)
     {
         b.Group = Group;
     }
 }
Ejemplo n.º 11
0
 public RevanClock()
 {
     // for now we will set the mod to 5
     this._bulb     = new YOBulb();
     this.encoder   = new StandardEncoder(5);
     this.parser    = new StandardParser();
     this.formatter = new StandardFormatter();
     this.accent    = new QuarterAccent(new ROBulb());
 }
Ejemplo n.º 12
0
 public static void ReadBulbInfo(this ILifxNetwork network, IBulb bulb)
 {
     ((LifxNetwork)network).SendCommand(bulb, PacketFactory.GetCommand(CommandType.GetInfo));
     ((LifxNetwork)network).SendCommand(bulb, PacketFactory.GetCommand(CommandType.GetMeshFirmware));
     ((LifxNetwork)network).SendCommand(bulb, PacketFactory.GetCommand(CommandType.GetMeshInfo));
     ((LifxNetwork)network).SendCommand(bulb, PacketFactory.GetCommand(CommandType.GetTime));
     ((LifxNetwork)network).SendCommand(bulb, PacketFactory.GetCommand(CommandType.GetVersion));
     ((LifxNetwork)network).SendCommand(bulb, PacketFactory.GetCommand(CommandType.GetWifiFirmwareState));
     ((LifxNetwork)network).SendCommand(bulb, PacketFactory.GetCommand(CommandType.GetWifiInfo));
 }
Ejemplo n.º 13
0
        public override void ProcessBulb(IBulb bulb)
        {
            Log.Debug(TAG, "Processing bulb label");
            var b = bulb as LifxBulb;

            if (b != null)
            {
                b.Label = Label;
            }
        }
Ejemplo n.º 14
0
        public static void RenameBulb(this ILifxNetwork network, IBulb bulb, string name)
        {
            var command = (SetBulbLabel)PacketFactory.GetCommand(CommandType.SetBulbLabel);

            command.Init(name);

            ((LifxNetwork)network).SendCommand(bulb, command);

            ((Bulb)bulb).Name = name;
        }
Ejemplo n.º 15
0
        public override void ProcessBulb(IBulb bulb)
        {
            Log.Debug(TAG, "Processing bulb group");
            var b = bulb as LifxBulb;

            if (b != null)
            {
                b.Group = Group;
            }
        }
Ejemplo n.º 16
0
        public override void ProcessBulb(IBulb bulb)
        {
            Log.Debug(TAG, "Processing bulb power state");
            var b = bulb as LifxBulb;

            if (b != null)
            {
                b.Power = Power;
            }
        }
Ejemplo n.º 17
0
        private static void SetPowerState(ILifxNetwork network, IBulb bulb, bool isPowerOn)
        {
            var command = (SetPowerState)PacketFactory.GetCommand(CommandType.SetPowerState);

            command.Init(isPowerOn);

            ((LifxNetwork)network).SendCommand(bulb, command);

            ((Bulb)bulb).IsPowerOn = isPowerOn;
        }
        private static void Waveform(this ILifxNetwork network, IBulb bulb, IColor color, double cycleDurationInSeconds, float cycles, WaveformType waveform)
        {
            var command = (SetWaveForm)PacketFactory.GetCommand(CommandType.SetWaveform);
            var c       = color.ToHsv();
            var period  = (uint)cycleDurationInSeconds * 1000;

            command.Init((ushort)c.Hue, (ushort)(c.Saturation * 100), (ushort)(c.Brightness * 255), (ushort)c.Kelvin, period, cycles, 0, waveform);

            ((LifxNetwork)network).SendCommand(bulb, command);
        }
Ejemplo n.º 19
0
        public static void ChangeColor(this ILifxNetwork network, IBulb bulb, IColor color, double fadeTimeInSeconds)
        {
            var command = (SetLightColor)PacketFactory.GetCommand(CommandType.SetLightColor);
            var c       = color.ToHsv();

            command.Init((ushort)c.Hue, (ushort)(c.Saturation * 100), (ushort)(c.Brightness * 255), (ushort)color.Kelvin, (uint)(fadeTimeInSeconds * 1000));

            ((LifxNetwork)network).SendCommand(bulb, command);

            ((Bulb)bulb).Color = c;
        }
Ejemplo n.º 20
0
        public bool Add(IBulb bulb)
        {
            lock (_bulbCollectionLock)
            {
                if (_bulbs.ContainsKey(bulb.Mac))
                {
                    return(false);
                }

                _bulbs.Add(bulb.Mac, bulb);
                return(true);
            }
        }
Ejemplo n.º 21
0
        private void HandleWifiStatePacket(IBulb bulb, AnswerPacketBase packet)
        {
            var wifiState = packet as WifiState;

            if (wifiState == null)
            {
                return;
            }

            if (wifiState.InterfaceType == Interface.Station && wifiState.WifiStatus == WifiStatus.Connected)
            {
                IsBulbSuccessfullyConnectedToWifi = true;
            }
        }
Ejemplo n.º 22
0
        public async Task <Brain> Light(IBulb bulb, int?intensity = null, int?autoDimBy = null)
        {
            if (Bulbs.Contains(bulb))
            {
                throw new ArgumentException(
                          $"Bulb '{bulb}' is already lighted.", nameof(bulb));
            }

            var adjustedBulb = await bulb.Adjust(intensity, autoDimBy);

            var nextBrain = await WithBulb(adjustedBulb);

            return(nextBrain);
        }
Ejemplo n.º 23
0
        /// <summary>
        /// Set the light state fo the bulb
        /// </summary>
        /// <param name="target">Target bulb</param>
        /// <param name="h">Hue, range from 0 to 360</param>
        /// <param name="s">Saturation, 0 to 100</param>
        /// <param name="b">Brightness, 0 to 100</param>
        /// <param name="k">Kelvin, 2500 to 9000</param>
        /// <param name="d">Dim, color transition in milliseconds</param>
        public void SetLightState(IBulb target, ushort h, ushort s, ushort b, ushort k, uint d)
        {
            var bulb = target as LifxBulb;

            if (bulb != null)
            {
                var data = new byte[PacketSize.SET_LIGHT_STATE];
                var seq  = SequenceGenerator.Next;
                NativeMethods.SetLightColorPacket(bulb.SiteAddress, bulb.Mac, seq, h, s, b, k, d, data);

                _sentPackets.Add(new KeyValuePair <byte, LifxBulb>(seq, bulb));
                _networkManager.SendTargetedPacket(data, seq, bulb.IP);
            }
        }
Ejemplo n.º 24
0
        /// <summary>
        /// Request the light state for the target bulb
        /// </summary>
        /// <param name="target">Target bulb</param>
        public void GetLightState(IBulb target)
        {
            var b = target as LifxBulb;

            if (b != null)
            {
                var data = new byte[PacketSize.GET_LIGHT_STATE];
                var seq  = SequenceGenerator.Next;
                NativeMethods.GetLightStatePacket(b.SiteAddress, seq, data);

                _sentPackets.Add(new KeyValuePair <byte, LifxBulb>(seq, b));
                _networkManager.SendTargetedPacket(data, seq, b.IP);
            }
        }
Ejemplo n.º 25
0
 public override void ProcessBulb(IBulb bulb)
 {
     Log.Debug(TAG, "Processing bulb light state");
     var b = bulb as LifxBulb;
     if (b != null)
     {
         b.Hue = Hue * 360 / 65535;
         b.Saturation = Saturation / 65535 * 100;
         b.Brightness = Brightness / 65535 * 100;
         b.Kelvin = Kelvin;
         b.Dim = Dim;
         b.Power = Power;
     }
 }
Ejemplo n.º 26
0
        public override void ProcessBulb(IBulb bulb)
        {
            Log.Debug(TAG, "Processing bulb light state");
            var b = bulb as LifxBulb;

            if (b != null)
            {
                b.Hue        = Hue * 360 / 65535;
                b.Saturation = Saturation / 65535 * 100;
                b.Brightness = Brightness / 65535 * 100;
                b.Kelvin     = Kelvin;
                b.Dim        = Dim;
                b.Power      = Power;
            }
        }
Ejemplo n.º 27
0
        /// <summary>
        /// Set the bulb power
        /// </summary>
        /// <param name="target">The target bulb</param>
        /// <param name="power">Power, 0 off, 0xFFFF on</param>
        public void SetPower(IBulb target, ushort power)
        {
            var data = new byte[PacketSize.SET_POWER];
            var seq  = SequenceGenerator.Next;

            var b = target as LifxBulb;

            if (b != null)
            {
                NativeMethods.SetPowerPacket(b.SiteAddress, b.Mac, seq, power, data);

                _sentPackets.Add(new KeyValuePair <byte, LifxBulb>(seq, b));
                _networkManager.SendTargetedPacket(data, seq, b.IP);
            }
        }
Ejemplo n.º 28
0
        public async Task <Brain> Adjust(IBulb bulb, int?intensity = null, int?autoDimBy = null)
        {
            if (!Bulbs.Contains(bulb))
            {
                throw new ArgumentException(
                          $"Bulb '{bulb}' is not lighted.", nameof(bulb));
            }

            var adjustedBulb = await bulb.Adjust(intensity, autoDimBy);

            var shouldRemove = (adjustedBulb.Intensity <= 0);
            var nextBrain    =
                shouldRemove
                    ? await WithoutBulb(adjustedBulb)
                    : await WithBulbEvent(adjustedBulb);

            return(nextBrain);
        }
Ejemplo n.º 29
0
        /// <summary>
        /// Set the label for the target bulb
        /// </summary>
        /// <param name="target">Target bulb</param>
        /// <param name="label">label string, will be truncated to 32 bytes</param>
        public void SetLabel(IBulb target, string label)
        {
            var b = target as LifxBulb;

            if (b != null)
            {
                var labelBytes = Encoding.UTF8.GetBytes(label);

                var data = new byte[PacketSize.SET_LABEL];
                var seq  = SequenceGenerator.Next;
                NativeMethods.SetLabelPacket(
                    b.SiteAddress,
                    b.Mac,
                    seq,
                    labelBytes,
                    Math.Min((uint)labelBytes.Length, 32),
                    data);

                _sentPackets.Add(new KeyValuePair <byte, LifxBulb>(seq, b));
                _networkManager.SendTargetedPacket(data, seq, b.IP);
            }
        }
Ejemplo n.º 30
0
        private void HandleBulbGroupsPacket(IBulb bulb, AnswerPacketBase packet)
        {
            var lightStatus = packet as LightStatus;
            var tags        = packet as Tags;

            if (tags != null || lightStatus != null)
            {
                var bitmask = lightStatus != null ? lightStatus.Bitmask : tags.Bitmask;

                var groupBitmasks = bitmask.GetGroupTags().ToList();

                foreach (var groupBitmask in groupBitmasks)
                {
                    BulbGroup group;
                    if (!_groups.TryGetValue(groupBitmask, out group))
                    {
                        lock (_groupCollectionLock)
                        {
                            if (!_groups.TryGetValue(groupBitmask, out group))
                            {
                                group = CreateGroup(groupBitmask);
                            }
                        }
                    }

                    group.Add(bulb);

                    if (string.IsNullOrEmpty(group.Name))
                    {
                        var getTagLabels = (GetTagLabels)PacketFactory.GetCommand(CommandType.GetTagLabels);
                        getTagLabels.Init(groupBitmask);
                        SendCommand(bulb, getTagLabels);
                    }
                }
            }
        }
Ejemplo n.º 31
0
        /// <summary>
        /// Request the light state for the target bulb
        /// </summary>
        /// <param name="target">Target bulb</param>
        public void GetLightState(IBulb target)
        {
            var b = target as LifxBulb;
            if (b != null)
            {
                var data = new byte[PacketSize.GET_LIGHT_STATE];
                var seq = SequenceGenerator.Next;
                NativeMethods.GetLightStatePacket(b.SiteAddress, seq, data);

                _sentPackets.Add(new KeyValuePair<byte, LifxBulb>(seq, b));
                _networkManager.SendTargetedPacket(data, seq, b.IP);
            }
        }
        public static void RemoveBulbFromGroup(this ILifxNetwork network, IBulbGroup bulbGroup, IBulb bulb)
        {
            var n     = (LifxNetwork)network;
            var group = (BulbGroup)bulbGroup;

            group.Remove(bulb);

            var groups = n.Groups.Where(g => g.Contains(bulb)).ToList();

            SetGroups(n, bulb, groups);
        }
        public static void AddBulbToGroup(this ILifxNetwork network, IBulbGroup bulbGroup, IBulb bulb)
        {
            var n     = (LifxNetwork)network;
            var group = (BulbGroup)bulbGroup;

            if (group.Contains(bulb))
            {
                return;
            }

            group.Add(bulb);

            var groups = n.Groups.Where(g => g.Contains(bulb)).ToList();

            SetGroups(n, bulb, groups);

            UpdateGroupName(n, group, bulb);
        }
Ejemplo n.º 34
0
        /// <summary>
        /// Set the label for the target bulb
        /// </summary>
        /// <param name="target">Target bulb</param>
        /// <param name="label">label string, will be truncated to 32 bytes</param>
        public void SetLabel(IBulb target, string label)
        {
            var b = target as LifxBulb;

            if (b != null)
            {
                var labelBytes = Encoding.UTF8.GetBytes(label);

                var data = new byte[PacketSize.SET_LABEL];
                var seq = SequenceGenerator.Next;
                NativeMethods.SetLabelPacket(
                    b.SiteAddress,
                    b.Mac,
                    seq,
                    labelBytes,
                    Math.Min((uint)labelBytes.Length, 32),
                    data);

                _sentPackets.Add(new KeyValuePair<byte, LifxBulb>(seq, b));
                _networkManager.SendTargetedPacket(data, seq, b.IP);
            }
        }
Ejemplo n.º 35
0
 public virtual void ProcessBulb(IBulb bulb)
 {
 }
 public static void Pulse(this ILifxNetwork network, IBulb bulb, IColor color, double cycleDurationInSeconds, float cycles)
 {
     network.Waveform(bulb, color, cycleDurationInSeconds, cycles, WaveformType.Pulse);
 }
Ejemplo n.º 37
0
        /// <summary>
        /// Set the light state fo the bulb
        /// </summary>
        /// <param name="target">Target bulb</param>
        /// <param name="h">Hue, range from 0 to 360</param>
        /// <param name="s">Saturation, 0 to 100</param>
        /// <param name="b">Brightness, 0 to 100</param>
        /// <param name="k">Kelvin, 2500 to 9000</param>
        /// <param name="d">Dim, color transition in milliseconds</param>
        public void SetLightState(IBulb target, ushort h, ushort s, ushort b, ushort k, uint d)
        {
            var bulb = target as LifxBulb;
            if (bulb != null)
            {
                var data = new byte[PacketSize.SET_LIGHT_STATE];
                var seq = SequenceGenerator.Next;
                NativeMethods.SetLightColorPacket(bulb.SiteAddress, bulb.Mac, seq, h, s, b, k, d, data);

                _sentPackets.Add(new KeyValuePair<byte, LifxBulb>(seq, bulb));
                _networkManager.SendTargetedPacket(data, seq, bulb.IP);
            }
        }
Ejemplo n.º 38
0
        /// <summary>
        /// Set the bulb power
        /// </summary>
        /// <param name="target">The target bulb</param>
        /// <param name="power">Power, 0 off, 0xFFFF on</param>
        public void SetPower(IBulb target, ushort power)
        {
            var data = new byte[PacketSize.SET_POWER];
            var seq = SequenceGenerator.Next;

            var b = target as LifxBulb;
            if (b != null)
            {
                NativeMethods.SetPowerPacket(b.SiteAddress, b.Mac, seq, power, data);

                _sentPackets.Add(new KeyValuePair<byte, LifxBulb>(seq, b));
                _networkManager.SendTargetedPacket(data, seq, b.IP);
            }
        }
Ejemplo n.º 39
0
 public void Remove(IBulb bulb)
 {
     _bulbs.Remove(bulb.Mac);
 }