// getColor returns the average color for lights in the group, or error if any // light returns an error. func (g *Group) getColor(cached bool) (common.Color, error) { var err error g.RLock() lastColor := g.color g.RUnlock() lights := g.Lights() if len(lights) == 0 { return lastColor, nil } colors := make([]common.Color, len(lights)) for i, light := range lights { var c common.Color if cached { c = light.CachedColor() } else { c, err = light.GetColor() if err != nil { return lastColor, err } } colors[i] = c } g.Lock() g.color = common.AverageColor(colors...) g.Unlock() g.RLock() defer g.RUnlock() if !common.ColorEqual(lastColor, g.color) { err = g.publish(common.EventUpdateColor{Color: g.color}) if err != nil { return g.color, err } } return g.color, nil }
func (b *boblightSync) sync(l common.Light, boblightIDs []uint16, rateLimit time.Duration, c closer) { ticker := time.NewTicker(time.Second / 20) for { select { case <-c: ticker.Stop() return case <-ticker.C: colors := make([]common.Color, len(boblightIDs)) for i, id := range boblightIDs { bColor, err := boblight.Lights.Get(id) if err != nil { continue } colors[i] = bColor.ToLifx() } if err := l.SetColor(common.AverageColor(colors...), rateLimit); err != nil { continue } } } }