public BulbViewModel(LifxBulb b, LifxController controller) { Bulb = b; _controller = controller; Bulb.PropertyChanged += Bulb_PropertyChanged; }
private void InitObservableProperties() { _networkManager.UdpListener() .ObserveOn(NewThreadScheduler.Default) .Do(p => Log.Debug(TAG, "Received: {0}", p)) .Where(packet => packet.Sequence != 0) .Do(packet => { var discoveryPacket = packet as DiscoveryPacket; if (discoveryPacket != null) { var duplicate = Bulbs.Any(b => b.IP.Equals(discoveryPacket.IP)); if (!duplicate) { var bulb = new LifxBulb() { Mac = discoveryPacket.Mac, IP = discoveryPacket.IP, SiteAddress = discoveryPacket.Site }; Bulbs.Add(bulb); _discoverySubject.OnNext(bulb); _labelSubject.OnNext(bulb); _groupSubject.OnNext(bulb); } } else { var sentPacket = _sentPackets.Where(p => p.Key == packet.Sequence).FirstOrDefault(); packet.ProcessBulb(sentPacket.Value); _sentPackets.Remove(sentPacket); } }) .Subscribe(_ct); _labelSubject.Subscribe(bulb => { Thread.Sleep(750); // don't want to spam packets, bulbs might ignore some var data = new byte[PacketSize.GET_LABEL]; var seq = SequenceGenerator.Next; NativeMethods.GetLabelPacket(bulb.SiteAddress, seq, data); _sentPackets.Add(new KeyValuePair<byte, LifxBulb>(seq, bulb)); _networkManager.SendTargetedPacket(data, seq, bulb.IP); }, _ct); _groupSubject.Subscribe(bulb => { Thread.Sleep(750); var data = new Byte[PacketSize.GET_GROUP]; var seq = SequenceGenerator.Next; NativeMethods.GetGroupPacket(bulb.SiteAddress, seq, data); _sentPackets.Add(new KeyValuePair<byte, LifxBulb>(seq, bulb)); _networkManager.SendTargetedPacket(data, seq, bulb.IP); }, _ct); }