154 lines
3.1 KiB
Go
154 lines
3.1 KiB
Go
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"math/rand"
|
|
"strconv"
|
|
"time"
|
|
|
|
"git.barakmich.com/barak/hue/discovery"
|
|
"github.com/amimof/huego"
|
|
"github.com/lucasb-eyer/go-colorful"
|
|
)
|
|
|
|
func setLightToHex(l *huego.Light, hex string, tTenths int) error {
|
|
return setLightToHexBright(l, hex, -1, tTenths)
|
|
}
|
|
|
|
func setLightToHexBright(l *huego.Light, hex string, bright int, tTenths int) error {
|
|
c, _ := colorful.Hex(hex)
|
|
xc, yc, yz := c.Xyy()
|
|
var bout uint8
|
|
if bright < 0 {
|
|
bout = uint8(255 * yz)
|
|
} else {
|
|
bout = uint8(bright)
|
|
}
|
|
return l.SetState(huego.State{
|
|
On: true,
|
|
TransitionTime: uint16(tTenths),
|
|
ColorMode: "xy",
|
|
Xy: []float32{float32(xc), float32(yc)},
|
|
Bri: bout,
|
|
})
|
|
}
|
|
|
|
func runTransition(l *huego.Light, ret chan *huego.Light) {
|
|
setLightToHex(l, "#aaaaaa", 7)
|
|
time.Sleep(800 * time.Millisecond)
|
|
setLightToHex(l, "#5555ff", 7)
|
|
time.Sleep(700 * time.Millisecond)
|
|
ret <- l
|
|
}
|
|
|
|
func runSnowAmbiance(lights []*huego.Light) {
|
|
for _, x := range lights {
|
|
setLightToHex(x, "#5555ff", 0)
|
|
}
|
|
var ready []*huego.Light
|
|
ret := make(chan *huego.Light, 100)
|
|
ready = lights
|
|
for {
|
|
select {
|
|
case l := <-ret:
|
|
ready = append(ready, l)
|
|
case <-time.Tick(100 * time.Millisecond):
|
|
if rand.Float32() < 0.03 {
|
|
if len(ready) == 0 {
|
|
continue
|
|
}
|
|
i := rand.Intn(len(ready))
|
|
l := ready[i]
|
|
ready = append(ready[:i], ready[i+1:]...)
|
|
go runTransition(l, ret)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
func runRedTransition(l *huego.Light, c chan *huego.Light) {
|
|
setLightToHex(l, "#ff0000", 0)
|
|
c <- l
|
|
}
|
|
|
|
func runGreenTransition(l *huego.Light, c chan *huego.Light) {
|
|
setLightToHex(l, "#009900", 0)
|
|
c <- l
|
|
}
|
|
|
|
func runChristmas(lights []*huego.Light) {
|
|
var red []*huego.Light
|
|
var green []*huego.Light
|
|
redc := make(chan *huego.Light, 100)
|
|
greenc := make(chan *huego.Light, 100)
|
|
for _, x := range lights {
|
|
if rand.Float32() < 0.5 {
|
|
setLightToHex(x, "#ff0000", 0)
|
|
red = append(red, x)
|
|
} else {
|
|
setLightToHex(x, "#009900", 0)
|
|
green = append(green, x)
|
|
}
|
|
}
|
|
for {
|
|
select {
|
|
case x := <-redc:
|
|
red = append(red, x)
|
|
case x := <-greenc:
|
|
green = append(green, x)
|
|
case <-time.Tick(1000 * time.Millisecond):
|
|
if len(red)+len(green) == 0 {
|
|
continue
|
|
}
|
|
var selector = red
|
|
toRed := false
|
|
i := rand.Intn(len(red) + len(green))
|
|
if i >= len(red) {
|
|
i = i - len(red)
|
|
selector = green
|
|
toRed = true
|
|
}
|
|
l := selector[i]
|
|
selector = append(selector[:i], selector[i+1:]...)
|
|
if toRed {
|
|
go runRedTransition(l, redc)
|
|
} else {
|
|
go runGreenTransition(l, greenc)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
func main() {
|
|
bridge, err := discovery.DiscoverAndConnectFromConfig("barak-hue-test")
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
groups, err := bridge.GetGroups()
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
var all huego.Group
|
|
found := false
|
|
for _, x := range groups {
|
|
if x.Name == "Dining Room" {
|
|
found = true
|
|
all = x
|
|
break
|
|
}
|
|
}
|
|
fmt.Println(all)
|
|
|
|
if !found {
|
|
panic("couldn't find it")
|
|
}
|
|
lightaddr := make([]*huego.Light, len(all.Lights))
|
|
for i, x := range all.Lights {
|
|
n, _ := strconv.Atoi(x)
|
|
lightaddr[i], err = bridge.GetLight(n)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
}
|
|
runSnowAmbiance(lightaddr)
|
|
}
|