7 Commits
Author SHA1 Message Date
gisle 73e6cba770 add very partial hue2 driver wip.
continuous-integration/drone/push Build is passing
2022-02-14 22:03:41 +01:00
gisle 44bd327a71 add index for name in scene table to force uniqueness. 2022-02-14 22:03:28 +01:00
gisle cedd0d1d28 add support for invoking and assigning scenes by name.
continuous-integration/drone/push Build is passing
2022-02-12 11:36:23 +01:00
gisle 7471102413 add scene list subcommand.
continuous-integration/drone/push Build is passing
2022-02-10 20:46:05 +01:00
gisle bf4d000455 add info mode to lucy list command. 2022-02-10 20:38:10 +01:00
gisle 6947fc3461 add delete light endpoint and forget for Hue.
continuous-integration/drone/push Build is passing
2022-01-31 23:09:00 +01:00
gisle 600c90a614 undo group nonsense in hue bridge.
continuous-integration/drone/push Build is passing
2022-01-28 23:01:44 +01:00
20 changed files with 1268 additions and 130 deletions
+62 -7
View File
@@ -7,6 +7,7 @@ import (
"git.aiterp.net/lucifer/new-server/models"
"github.com/gin-gonic/gin"
"log"
"strconv"
"time"
)
@@ -217,14 +218,28 @@ func Devices(r gin.IRoutes) {
return []models.Device{}, nil
}
_, err = config.SceneRepository().Find(ctxOf(c), body.SceneID)
if err != nil {
return nil, err
var scene *models.Scene
if body.SceneName != "" {
scene, err = config.SceneRepository().FindName(ctxOf(c), body.SceneName)
if err != nil {
return nil, err
}
if body.DurationMS < 0 {
body.DurationMS = 0
}
body.StartTime = time.Now()
body.SceneID = scene.ID
} else {
scene, err = config.SceneRepository().Find(ctxOf(c), body.SceneID)
if err != nil {
return nil, err
}
if body.DurationMS < 0 {
body.DurationMS = 0
}
body.StartTime = time.Now()
body.SceneName = scene.Name
}
if body.DurationMS < 0 {
body.DurationMS = 0
}
body.StartTime = time.Now()
pushMode := c.Query("push") == "true"
for i := range devices {
@@ -265,6 +280,46 @@ func Devices(r gin.IRoutes) {
return withSceneState(devices), nil
}))
r.DELETE("/:fetch", handler(func(c *gin.Context) (interface{}, error) {
id, err := strconv.Atoi(c.Param("fetch"))
if err != nil {
return nil, err
}
device, err := config.DeviceRepository().Find(ctxOf(c), id)
if err != nil {
return nil, err
}
if c.Query("forgetmenot") != "true" {
bridge, err := config.BridgeRepository().Find(ctxOf(c), device.BridgeID)
if err != nil {
return nil, err
}
driver, err := config.DriverProvider().Provide(bridge.Driver)
if err != nil {
return nil, err
}
forgettableDriver, ok := driver.(models.ForgettableDriver)
if !ok {
return nil, models.ErrCannotForget
}
err = forgettableDriver.ForgetDevice(ctxOf(c), bridge, *device)
if err != nil {
return nil, err
}
}
err = config.DeviceRepository().Delete(ctxOf(c), device)
if err != nil {
return nil, err
}
return device, nil
}))
}
func withSceneState(devices []models.Device) []models.Device {
+14 -2
View File
@@ -5,15 +5,27 @@ import (
"git.aiterp.net/lucifer/new-server/app/services/publisher"
"git.aiterp.net/lucifer/new-server/models"
"github.com/gin-gonic/gin"
"regexp"
)
func Scenes(r gin.IRoutes) {
nonNumericRegex := regexp.MustCompile("[a-zA-Z ]")
findScene := func(c *gin.Context) (*models.Scene, error) {
param := c.Param("id")
if nonNumericRegex.MatchString(param) {
return config.SceneRepository().FindName(ctxOf(c), param)
} else {
return config.SceneRepository().Find(ctxOf(c), intParam(c, "id"))
}
}
r.GET("", handler(func(c *gin.Context) (interface{}, error) {
return config.SceneRepository().FetchAll(ctxOf(c))
}))
r.GET("/:id", handler(func(c *gin.Context) (interface{}, error) {
return config.SceneRepository().Find(ctxOf(c), intParam(c, "id"))
return findScene(c)
}))
r.POST("", handler(func(c *gin.Context) (interface{}, error) {
@@ -45,7 +57,7 @@ func Scenes(r gin.IRoutes) {
return nil, err
}
scene, err := config.SceneRepository().Find(ctxOf(c), intParam(c, "id"))
scene, err := findScene(c)
if err != nil {
return nil, err
}
+157
View File
@@ -0,0 +1,157 @@
package main
import (
"encoding/json"
"fmt"
"git.aiterp.net/lucifer/new-server/models"
"io"
"sort"
"strings"
"time"
)
func WriteDeviceInfo(w io.Writer, devices []models.Device, fields []string) {
if len(fields) == 1 && fields[0] == "all" {
fields = allFields
} else {
fields = append(fields[:0:0], fields...)
for i, field := range fields {
if aliased, ok := fieldAliases[field]; ok {
fields[i] = aliased
}
}
}
length := 0
for _, field := range fields {
if mlFields[field] {
continue
}
if len(field) > length {
length = len(field)
}
}
for i, device := range devices {
if i > 0 {
fmt.Println()
}
for _, field := range fields {
if formatter, ok := fieldFormatters[field]; ok {
_, _ = fmt.Fprintf(w, "%-*s %s\n", length+1, field+":", formatter(&device))
} else {
_, _ = fmt.Fprintf(w, "%-*s (unknown field)\n", length+1, field+":")
}
}
}
}
var allFields = []string{
"id", "name", "icon", "bridgeId", "tags", "buttonNames",
"power", "color", "intensity", "temperature",
"internalId", "sceneAssignment", "userProperties", "driverProperties",
}
var mlFields = map[string]bool{
"sceneAssignment": true,
"userProperties": true,
"driverProperties": true,
}
var fieldFormatters = map[string]func(d *models.Device) string{
"id": func(d *models.Device) string { return fmt.Sprint(d.ID) },
"internalId": func(d *models.Device) string { return d.InternalID },
"name": func(d *models.Device) string { return d.Name },
"icon": func(d *models.Device) string { return d.Icon },
"bridgeId": func(d *models.Device) string { return fmt.Sprint(d.BridgeID) },
"tags": func(d *models.Device) string { return strings.Join(d.Tags, ", ") },
"buttonNames": func(d *models.Device) string { return strings.Join(d.ButtonNames, ", ") },
"power": func(d *models.Device) string { return fmt.Sprint(d.State.Power) },
"color": func(d *models.Device) string { return d.State.Color.String() },
"intensity": func(d *models.Device) string { return fmt.Sprint(d.State.Intensity) },
"temperature": func(d *models.Device) string { return fmt.Sprint(d.State.Temperature) },
"sceneAssignment": func(d *models.Device) string {
str := &strings.Builder{}
str.Grow(64)
for _, scene := range d.SceneAssignments {
_, _ = fmt.Fprintf(str, "\n - id: %d\n", scene.SceneID)
_, _ = fmt.Fprintf(str, " duration: %d\n", scene.DurationMS)
_, _ = fmt.Fprintf(str, " group: %s\n", scene.Group)
_, _ = fmt.Fprintf(str, " startTime: %s", scene.StartTime.Format(time.RFC3339))
}
if len(d.SceneAssignments) == 0 {
str.Reset()
str.WriteString("(none)")
}
return str.String()
},
"userProperties": func(d *models.Device) string {
str := &strings.Builder{}
str.Grow(64)
str.WriteString("\n")
keys := make([]string, 0, len(d.UserProperties))
for key := range d.UserProperties {
keys = append(keys, key)
}
sort.Strings(keys)
for _, key := range keys {
_, _ = fmt.Fprintf(str, " %s: %s\n", key, d.UserProperties[key])
}
if len(d.UserProperties) == 0 {
str.Reset()
str.WriteString("(none)")
}
return str.String()
},
"driverProperties": func(d *models.Device) string {
str := &strings.Builder{}
str.Grow(256)
str.WriteString("\n")
keys := make([]string, 0, len(d.DriverProperties))
for key := range d.DriverProperties {
keys = append(keys, key)
}
sort.Strings(keys)
for _, key := range keys {
j, _ := json.Marshal(d.DriverProperties[key])
_, _ = fmt.Fprintf(str, " %s: %s\n", key, string(j))
}
if len(d.DriverProperties) == 0 {
str.Reset()
str.WriteString("(none)")
}
return str.String()
},
"capabilities": func(d *models.Device) string {
capStr := make([]string, len(d.Capabilities))
for i := range d.Capabilities {
capStr[i] = string(d.Capabilities[i])
}
return strings.Join(capStr, ", ")
},
}
var fieldAliases = map[string]string{
"bridge": "bridgeId",
"buttons": "buttonNames",
"internal": "internalId",
"caps": "capabilities",
"cap": "capabilities",
"userprops": "userProperties",
"uprops": "userProperties",
"driverprops": "driverProperties",
"dprops": "driverProperties",
"internalid": "internalId",
"bridgeid": "bridgeId",
"buttonnames": "buttonNames",
"scene": "sceneAssignment",
}
+2 -2
View File
@@ -37,8 +37,8 @@ func main() {
log.Fatalln(err)
}
if cmd.Params.Get("info").StringOr("false") == "true" {
WriteDeviceInfoTable(os.Stdout, devices)
if info := cmd.Params.Get("info").String(); info != nil {
WriteDeviceInfo(os.Stdout, devices, strings.Split(*info, ","))
} else {
WriteDeviceStateTable(os.Stdout, devices)
}
+22 -5
View File
@@ -19,6 +19,16 @@ func sceneCmd(
cmd := parseCommand(os.Args[2:])
switch cmd.Name {
case "list":
{
scenes, err := c.GetScenes(ctx)
if err != nil {
log.Fatalln("Failed to load scenes:", err)
}
WriteSceneTable(os.Stdout, scenes)
}
case "create", "update":
{
fileName := cmd.Params.Get(0).String()
@@ -83,15 +93,22 @@ func sceneCmd(
{
fetch := cmd.Params.Get(0).String()
id := cmd.Params.Get(1).Int()
if fetch == nil || id == nil {
log.Println("Usage: lucy scene assign <fetch> <id> <group=S> <duration=I>")
name := cmd.Params.Get(1).String()
if fetch == nil || (id == nil && name == nil) {
log.Println("Usage: lucy scene assign <fetch> <id|name> <group=S> <duration=I>")
}
devices, err := c.AssignDevice(ctx, *fetch, cmd.Name == "push", models.DeviceSceneAssignment{
SceneID: *id,
assignment := models.DeviceSceneAssignment{
Group: cmd.Params.Get("group").StringOr(*fetch),
DurationMS: int64(cmd.Params.Get("duration").IntOr(0)),
})
}
if id != nil {
assignment.SceneID = *id
} else {
assignment.SceneName = *name
}
devices, err := c.AssignDevice(ctx, *fetch, cmd.Name == "push", assignment)
if err != nil {
log.Println("Could not assign devices:", err)
return
+18
View File
@@ -94,6 +94,24 @@ func WriteDeviceInfoTable(w io.Writer, devices []models.Device) {
table.Render()
}
func WriteSceneTable(w io.Writer, scenes []models.Scene) {
table := tablewriter.NewWriter(w)
table.SetHeader([]string{"ID", "NAME", "ROLES", "INTERVAL"})
table.SetReflowDuringAutoWrap(false)
for _, v := range scenes {
table.Append([]string{
fmt.Sprint(v.ID),
v.Name,
fmt.Sprint(len(v.Roles)),
fmt.Sprint(v.IntervalMS),
})
}
table.Render()
}
func WriteHandlerInfoTable(w io.Writer, handlers []models.EventHandler) {
table := tablewriter.NewWriter(w)
table.SetHeader([]string{"ID", "EVENT NAME", "PRIORITY", "CONDITIONS", "TARGET", "ACTION"})
+39
View File
@@ -0,0 +1,39 @@
package main
import (
"context"
"encoding/json"
"fmt"
"git.aiterp.net/lucifer/new-server/internal/drivers/hue2"
"git.aiterp.net/lucifer/new-server/models"
"log"
)
func main() {
client := hue2.NewClient("10.80.8.8", "o2XKGgmVUGNBghYFdLUCVuinOTMxFH4pHV9PuTbU")
bridge := hue2.NewBridge(client)
err := bridge.RefreshAll(context.Background())
if err != nil {
log.Fatalln(err)
}
j, _ := json.Marshal(bridge.GenerateDevices())
fmt.Println(string(j))
for _, device := range bridge.GenerateDevices() {
switch device.InternalID {
case "6d5a45b0-ec69-4417-8588-717358b05086":
c, _ := models.ParseColorValue("xy:0.22,0.18")
device.State.Color = c
device.State.Intensity = 1
bridge.Update(device)
case "a71128f4-5295-4ae4-9fbc-5541abc8739b":
c, _ := models.ParseColorValue("k:2000")
device.State.Color = c
device.State.Intensity = 0.2
bridge.Update(device)
}
}
fmt.Println(bridge.MakeCongruent(context.Background()))
}
+61 -73
View File
@@ -6,8 +6,8 @@ import (
"encoding/json"
"fmt"
"git.aiterp.net/lucifer/new-server/models"
"golang.org/x/sync/errgroup"
"io"
"log"
"net"
"net/http"
"strconv"
@@ -23,6 +23,7 @@ type Bridge struct {
externalID int
lightStates []*hueLightState
sensorStates []*hueSensorState
quarantine map[string]time.Time
syncingPublish uint32
}
@@ -34,6 +35,10 @@ func (b *Bridge) Refresh(ctx context.Context) error {
b.mu.Lock()
for index, light := range lightMap {
if time.Now().Before(b.quarantine[light.Uniqueid]) {
continue
}
var state *hueLightState
for _, existingState := range b.lightStates {
if existingState.index == index {
@@ -68,6 +73,10 @@ func (b *Bridge) Refresh(ctx context.Context) error {
b.mu.Lock()
for index, sensor := range sensorMap {
if time.Now().Before(b.quarantine[sensor.UniqueID]) {
continue
}
var state *hueSensorState
for _, existingState := range b.sensorStates {
if existingState.index == index {
@@ -98,17 +107,17 @@ func (b *Bridge) Refresh(ctx context.Context) error {
func (b *Bridge) SyncStale(ctx context.Context) error {
indices := make([]int, 0, 4)
arrayIndices := make([]int, 0, 4)
inputs := make([]LightStateInput, 0, 4)
eg, ctx := errgroup.WithContext(ctx)
b.mu.Lock()
for i, state := range b.lightStates {
for _, state := range b.lightStates {
if !state.stale {
continue
}
indices = append(indices, state.index)
arrayIndices = append(arrayIndices, i)
inputs = append(inputs, state.input)
}
b.mu.Unlock()
@@ -117,83 +126,60 @@ func (b *Bridge) SyncStale(ctx context.Context) error {
return nil
}
groups := make([]*syncGroup, 0, 4)
for i := range inputs {
input := inputs[i]
for i, input := range inputs {
iCopy := i
index := indices[i]
inputCopy := input
found := false
for _, group := range groups {
if group.State.Equal(input) {
group.Indexes = append(group.Indexes, indices[i])
group.ArrayIndexes = append(group.ArrayIndexes, arrayIndices[i])
found = true
}
}
if !found {
groups = append(groups, &syncGroup{
GroupIndex: -1,
State: input,
Indexes: []int{indices[i]},
ArrayIndexes: []int{arrayIndices[i]},
})
}
}
groupMap, err := b.getGroups(ctx)
if err != nil {
return err
}
for id, data := range groupMap {
for _, group := range groups {
if group.Matches(&data) {
group.GroupIndex = id
break
}
}
}
for _, group := range groups {
if group.GroupIndex == -1 {
data := GroupData{
Name: "lucifer_auto_group",
Lights: []string{},
}
for _, idx := range group.Indexes {
data.Lights = append(data.Lights, strconv.Itoa(idx))
}
id, err := b.postGroup(ctx, data)
eg.Go(func() error {
err := b.putLightState(ctx, index, inputCopy)
if err != nil {
return err
}
group.GroupIndex = id
}
b.lightStates[iCopy].stale = false
return nil
})
}
log.Println("Updating", len(inputs), "lights on Hue bridge", b.externalID, "in", len(groups), "groups")
return eg.Wait()
}
for _, group := range groups {
err := b.putGroupLightState(ctx, group.GroupIndex, group.State)
if err != nil {
return err
}
b.mu.Lock()
for _, arrayIndex := range group.ArrayIndexes {
b.lightStates[arrayIndex].stale = false
}
b.mu.Unlock()
if groupMap[group.GroupIndex].Name == "lucifer_auto_group" {
err := b.deleteGroup(ctx, group.GroupIndex)
if err != nil {
log.Println("Could not delete temporary group", err)
}
func (b *Bridge) ForgetDevice(ctx context.Context, device models.Device) error {
// Find index
b.mu.Lock()
found := false
index := -1
for i, ls := range b.lightStates {
if ls.uniqueID == device.InternalID {
found = true
index = i
}
}
b.mu.Unlock()
if !found {
return models.ErrNotFound
}
// Delete light from bridge
err := b.deleteLight(ctx, index)
if err != nil {
return err
}
// Remove light state from local list. I don't know if the quarantine is necessary, but let's have it anyway.
b.mu.Lock()
for i, ls := range b.lightStates {
if ls.uniqueID == device.InternalID {
b.lightStates = append(b.lightStates[:i], b.lightStates[i+1:]...)
}
}
if b.quarantine == nil {
b.quarantine = make(map[string]time.Time, 1)
}
b.quarantine[device.InternalID] = time.Now().Add(time.Second * 30)
b.mu.Unlock()
return nil
}
@@ -236,6 +222,10 @@ func (b *Bridge) putGroupLightState(ctx context.Context, index int, input LightS
return b.put(ctx, fmt.Sprintf("groups/%d/action", index), input, nil)
}
func (b *Bridge) deleteLight(ctx context.Context, index int) error {
return b.delete(ctx, fmt.Sprintf("lights/%d", index), nil)
}
func (b *Bridge) getToken(ctx context.Context) (string, error) {
result := make([]CreateUserResponse, 0, 1)
err := b.post(ctx, "", CreateUserInput{DeviceType: "git.aiterp.net/lucifer"}, &result)
@@ -296,7 +286,6 @@ func (b *Bridge) postGroup(ctx context.Context, input GroupData) (int, error) {
return id, err
}
func (b *Bridge) deleteGroup(ctx context.Context, index int) error {
return b.delete(ctx, "groups/"+strconv.Itoa(index), nil)
}
@@ -343,7 +332,6 @@ func (b *Bridge) delete(ctx context.Context, resource string, target interface{}
return json.NewDecoder(res.Body).Decode(target)
}
func (b *Bridge) post(ctx context.Context, resource string, body interface{}, target interface{}) error {
rb, err := reqBody(body)
if err != nil {
+11 -2
View File
@@ -322,8 +322,8 @@ func (d *Driver) Run(ctx context.Context, bridge models.Bridge, ch chan<- models
return err
}
fastTicker := time.NewTicker(time.Second / 10)
slowTicker := time.NewTicker(time.Second / 3)
fastTicker := time.NewTicker(time.Second / 5)
slowTicker := time.NewTicker(time.Second / 2)
selectedTicker := fastTicker
ticksUntilRefresh := 0
ticksSinceChange := 0
@@ -372,6 +372,15 @@ func (d *Driver) Run(ctx context.Context, bridge models.Bridge, ch chan<- models
}
}
func (d *Driver) ForgetDevice(ctx context.Context, bridge models.Bridge, device models.Device) error {
b, err := d.ensureBridge(ctx, bridge)
if err != nil {
return err
}
return b.ForgetDevice(ctx, device)
}
func (d *Driver) ensureBridge(ctx context.Context, info models.Bridge) (*Bridge, error) {
d.mu.Lock()
for _, bridge := range d.bridges {
+257
View File
@@ -0,0 +1,257 @@
package hue2
import (
"context"
"fmt"
"git.aiterp.net/lucifer/new-server/models"
"golang.org/x/sync/errgroup"
"math"
"strings"
"sync"
"time"
)
type Bridge struct {
mu sync.Mutex
client *Client
devices map[string]models.Device
resources map[string]*ResourceData
}
func NewBridge(client *Client) *Bridge {
return &Bridge{
client: client,
devices: make(map[string]models.Device, 64),
resources: make(map[string]*ResourceData, 256),
}
}
func (b *Bridge) Update(devices ...models.Device) {
b.mu.Lock()
for _, device := range devices {
b.devices[device.InternalID] = device
}
b.mu.Unlock()
}
func (b *Bridge) MakeCongruent(ctx context.Context) (int, error) {
b.mu.Lock()
dur := time.Millisecond * 200
updates := make(map[string]ResourceUpdate)
for _, device := range b.devices {
resource := b.resources[device.InternalID]
if lightID := resource.ServiceID("light"); lightID != nil {
light := b.resources[*lightID]
update := ResourceUpdate{TransitionDuration: &dur}
changed := false
if light.ColorTemperature != nil && device.State.Color.IsKelvin() {
mirek := 1000000 / device.State.Color.Kelvin
if mirek < light.ColorTemperature.MirekSchema.MirekMinimum {
mirek = light.ColorTemperature.MirekSchema.MirekMinimum
}
if mirek > light.ColorTemperature.MirekSchema.MirekMaximum {
mirek = light.ColorTemperature.MirekSchema.MirekMaximum
}
if light.ColorTemperature.Mirek == nil || mirek != *light.ColorTemperature.Mirek {
update.Mirek = &mirek
changed = true
}
} else if xy, ok := device.State.Color.ToXY(); ok && light.Color != nil {
xy = light.Color.Gamut.Conform(xy).Round()
if xy.DistanceTo(light.Color.XY) > 0.00015 || (light.ColorTemperature != nil && light.ColorTemperature.Mirek != nil) {
update.ColorXY = &xy
changed = true
}
}
if light.Power != nil && light.Power.On != device.State.Power {
update.Power = &device.State.Power
changed = true
}
if light.Dimming != nil && math.Abs(light.Dimming.Brightness/100-device.State.Intensity) > 0.005 {
brightness := math.Abs(math.Min(device.State.Intensity*100, 100))
update.Brightness = &brightness
changed = true
}
if changed {
updates["light/"+light.ID] = update
}
}
}
b.mu.Unlock()
if len(updates) == 0 {
return 0, nil
}
eg, ctx := errgroup.WithContext(ctx)
for key := range updates {
update := updates[key]
split := strings.SplitN(key, "/", 2)
link := ResourceLink{Kind: split[0], ID: split[1]}
eg.Go(func() error {
return b.client.UpdateResource(ctx, link, update)
})
}
return len(updates), eg.Wait()
}
func (b *Bridge) GenerateDevices() []models.Device {
b.mu.Lock()
resources := b.resources
b.mu.Unlock()
devices := make([]models.Device, 0, 16)
for _, resource := range resources {
if resource.Type != "device" || strings.HasPrefix(resource.Metadata.Archetype, "bridge") {
continue
}
device := models.Device{
InternalID: resource.ID,
Name: resource.Metadata.Name,
DriverProperties: map[string]interface{}{
"archetype": resource.Metadata.Archetype,
"name": resource.ProductData.ProductName,
"product": resource.ProductData,
"legacyId": resource.LegacyID,
},
}
// Set icon
if resource.ProductData.ProductName == "Hue dimmer switch" {
device.Icon = "switch"
} else if resource.ProductData.ProductName == "Hue motion sensor" {
device.Icon = "sensor"
} else {
device.Icon = "lightbulb"
}
buttonCount := 0
for _, ptr := range resource.Services {
switch ptr.Kind {
case "device_power":
{
device.DriverProperties["battery"] = resources[ptr.ID].PowerState
}
case "button":
{
buttonCount += 1
}
case "zigbee_connectivity":
{
device.DriverProperties["zigbee"] = resources[ptr.ID].Status
}
case "motion":
{
device.Capabilities = append(device.Capabilities, models.DCPresence)
}
case "temperature":
{
device.Capabilities = append(device.Capabilities, models.DCTemperatureSensor)
}
case "light":
{
light := resources[ptr.ID]
if light.Power != nil {
device.State.Power = light.Power.On
device.Capabilities = append(device.Capabilities, models.DCPower)
}
if light.Dimming != nil {
device.State.Intensity = light.Dimming.Brightness / 100
device.Capabilities = append(device.Capabilities, models.DCIntensity)
}
if light.ColorTemperature != nil {
if light.ColorTemperature.Mirek != nil {
device.State.Color = models.ColorValue{
Kelvin: int(1000000 / *light.ColorTemperature.Mirek),
}
}
device.Capabilities = append(device.Capabilities, models.DCColorKelvin)
device.DriverProperties["maxTemperature"] = 1000000 / light.ColorTemperature.MirekSchema.MirekMinimum
device.DriverProperties["minTemperature"] = 1000000 / light.ColorTemperature.MirekSchema.MirekMaximum
}
if light.Color != nil {
if device.State.Color.IsEmpty() {
device.State.Color = models.ColorValue{
XY: &light.Color.XY,
}
}
device.DriverProperties["colorGamut"] = light.Color.Gamut
device.DriverProperties["colorGamutType"] = light.Color.GamutType
device.Capabilities = append(device.Capabilities, models.DCColorHS, models.DCColorXY)
}
}
}
}
if buttonCount == 4 {
device.ButtonNames = []string{"On", "DimUp", "DimDown", "Off"}
} else if buttonCount == 1 {
device.ButtonNames = []string{"Button"}
} else {
for n := 1; n <= buttonCount; n++ {
device.ButtonNames = append(device.ButtonNames, fmt.Sprint("Button", n))
}
}
devices = append(devices, device)
}
return devices
}
func (b *Bridge) Refresh(ctx context.Context, kind string) error {
if kind == "device" {
// Device refresh requires the full deal as services are taken for granted.
return b.RefreshAll(ctx)
}
resources, err := b.client.Resources(ctx, kind)
if err != nil {
return err
}
b.mu.Lock()
oldResources := b.resources
b.mu.Unlock()
newResources := make(map[string]*ResourceData, len(b.resources))
for key, value := range oldResources {
if value.Type != kind {
newResources[key] = value
}
}
for i := range resources {
resource := resources[i]
newResources[resource.ID] = &resource
}
b.mu.Lock()
b.resources = newResources
b.mu.Unlock()
return nil
}
func (b *Bridge) RefreshAll(ctx context.Context) error {
allResources, err := b.client.AllResources(ctx)
if err != nil {
return err
}
resources := make(map[string]*ResourceData, len(allResources))
for i := range allResources {
resource := allResources[i]
resources[resource.ID] = &resource
}
b.mu.Lock()
b.resources = resources
b.mu.Unlock()
return nil
}
+167
View File
@@ -0,0 +1,167 @@
package hue2
import (
"bytes"
"context"
"crypto/tls"
"encoding/json"
"fmt"
"io"
"net"
"net/http"
"strings"
"time"
)
func NewClient(host, token string) *Client {
ch := make(chan struct{}, 5)
for i := 0; i < 3; i++ {
ch <- struct{}{}
}
return &Client{
host: host,
token: token,
ch: ch,
}
}
type Client struct {
host string
token string
ch chan struct{}
}
func (c *Client) AllResources(ctx context.Context) ([]ResourceData, error) {
res := struct {
Error interface{}
Data []ResourceData
}{}
err := c.get(ctx, "clip/v2/resource", &res)
if err != nil {
return nil, err
}
return res.Data, nil
}
func (c *Client) Resources(ctx context.Context, kind string) ([]ResourceData, error) {
res := struct {
Error interface{}
Data []ResourceData
}{}
err := c.get(ctx, "clip/v2/resource/"+kind, &res)
if err != nil {
return nil, err
}
return res.Data, nil
}
func (c *Client) UpdateResource(ctx context.Context, link ResourceLink, update ResourceUpdate) error {
return c.put(ctx, link.Path(), update, nil)
}
func (c *Client) get(ctx context.Context, path string, target interface{}) error {
select {
case <-ctx.Done():
return ctx.Err()
case <-c.ch:
defer func() {
c.ch <- struct{}{}
}()
}
req, err := http.NewRequest("GET", fmt.Sprintf("https://%s/%s", c.host, path), nil)
if err != nil {
return err
}
req.Header.Set("hue-application-key", c.token)
res, err := httpClient.Do(req.WithContext(ctx))
if err != nil {
return err
}
defer res.Body.Close()
if target == nil {
return nil
}
return json.NewDecoder(res.Body).Decode(&target)
}
func (c *Client) put(ctx context.Context, path string, body interface{}, target interface{}) error {
select {
case <-ctx.Done():
return ctx.Err()
case <-c.ch:
defer func() {
c.ch <- struct{}{}
}()
}
rb, err := reqBody(body)
if err != nil {
return err
}
req, err := http.NewRequest("PUT", fmt.Sprintf("https://%s/%s", c.host, path), rb)
if err != nil {
return err
}
req.Header.Set("hue-application-key", c.token)
res, err := httpClient.Do(req.WithContext(ctx))
if err != nil {
return err
}
defer res.Body.Close()
if target == nil {
return nil
}
return json.NewDecoder(res.Body).Decode(&target)
}
func reqBody(body interface{}) (io.Reader, error) {
if body == nil {
return nil, nil
}
switch v := body.(type) {
case []byte:
return bytes.NewReader(v), nil
case string:
return strings.NewReader(v), nil
case io.Reader:
return v, nil
default:
jsonData, err := json.Marshal(v)
if err != nil {
return nil, err
}
return bytes.NewReader(jsonData), nil
}
}
var httpClient = &http.Client{
Transport: &http.Transport{
Proxy: http.ProxyFromEnvironment,
DialContext: (&net.Dialer{
Timeout: 30 * time.Second,
KeepAlive: 30 * time.Second,
}).DialContext,
MaxIdleConns: 256,
MaxIdleConnsPerHost: 16,
IdleConnTimeout: 10 * time.Minute,
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
},
Timeout: time.Minute,
}
+144
View File
@@ -0,0 +1,144 @@
package hue2
import (
"fmt"
"git.aiterp.net/lucifer/new-server/models"
"strings"
"time"
)
type DeviceData struct {
ID string `json:"id"`
LegacyID string `json:"id_v1"`
Metadata DeviceMetadata `json:"metadata"`
Type string `json:"type"`
ProductData DeviceProductData `json:"product_data"`
Services []ResourceLink `json:"services"`
}
type DeviceMetadata struct {
Archetype string `json:"archetype"`
Name string `json:"name"`
}
type DeviceProductData struct {
Certified bool `json:"certified"`
ManufacturerName string `json:"manufacturer_name"`
ModelID string `json:"model_id"`
ProductArchetype string `json:"product_archetype"`
ProductName string `json:"product_name"`
SoftwareVersion string `json:"software_version"`
}
type ResourceData struct {
ID string `json:"id"`
LegacyID string `json:"id_v1"`
Metadata DeviceMetadata `json:"metadata"`
Type string `json:"type"`
Mode *string `json:"mode"`
Owner *ResourceLink `json:"owner"`
ProductData *DeviceProductData `json:"product_data"`
Services []ResourceLink `json:"services"`
Power *LightPower `json:"on"`
Color *LightColor `json:"color"`
ColorTemperature *LightCT `json:"color_temperature"`
Dimming *LightDimming `json:"dimming"`
Dynamics *LightDynamics `json:"dynamics"`
Alert *LightAlert `json:"alert"`
PowerState *PowerState `json:"power_state"`
Status *string `json:"status"`
}
func (res *ResourceData) ServiceID(kind string) *string {
for _, ptr := range res.Services {
if ptr.Kind == kind {
return &ptr.ID
}
}
return nil
}
type PowerState struct {
BatteryState string `json:"battery_state"`
BatteryLevel float64 `json:"battery_level"`
}
type LightPower struct {
On bool `json:"on"`
}
type LightDimming struct {
Brightness float64 `json:"brightness"`
}
type LightColor struct {
Gamut models.ColorGamut `json:"gamut"`
GamutType string `json:"gamut_type"`
XY models.ColorXY `json:"xy"`
}
type LightCT struct {
Mirek *int `json:"mirek"`
MirekSchema LightCTMirekSchema `json:"mirek_schema"`
MirekValid bool `json:"mirek_valid"`
}
type LightCTMirekSchema struct {
MirekMaximum int `json:"mirek_maximum"`
MirekMinimum int `json:"mirek_minimum"`
}
type LightDynamics struct {
Speed float64 `json:"speed"`
SpeedValid bool `json:"speed_valid"`
Status string `json:"status"`
StatusValues []string `json:"status_values"`
}
type LightAlert struct {
ActionValues []string `json:"action_values"`
}
type ResourceUpdate struct {
Power *bool
ColorXY *models.ColorXY
Brightness *float64
Mirek *int
TransitionDuration *time.Duration
}
func (r ResourceUpdate) MarshalJSON() ([]byte, error) {
chunks := make([]string, 0, 4)
if r.Power != nil {
chunks = append(chunks, fmt.Sprintf(`"on":{"on":%v}`, *r.Power))
}
if r.ColorXY != nil {
chunks = append(chunks, fmt.Sprintf(`"color":{"xy":{"x":%f,"y":%f}}`, r.ColorXY.X, r.ColorXY.Y))
}
if r.Brightness != nil {
chunks = append(chunks, fmt.Sprintf(`"dimming":{"brightness":%f}`, *r.Brightness))
}
if r.Mirek != nil {
chunks = append(chunks, fmt.Sprintf(`"color_temperature":{"mirek":%d}`, *r.Mirek))
}
if r.TransitionDuration != nil {
chunks = append(chunks, fmt.Sprintf(`"dynamics":{"duration":%d}`, r.TransitionDuration.Truncate(time.Millisecond*100).Milliseconds()))
}
fmt.Println(fmt.Sprintf("{%s}", strings.Join(chunks, ",")))
return []byte(fmt.Sprintf("{%s}", strings.Join(chunks, ","))), nil
}
type ResourceLink struct {
ID string `json:"rid"`
Kind string `json:"rtype"`
}
func (rl *ResourceLink) Path() string {
return fmt.Sprintf("/clip/v2/resource/%s/%s", rl.Kind, rl.ID)
}
+10
View File
@@ -29,6 +29,16 @@ func (r *SceneRepo) Find(ctx context.Context, id int) (*models.Scene, error) {
return r.populateOne(&scene)
}
func (r *SceneRepo) FindName(ctx context.Context, name string) (*models.Scene, error) {
var scene sceneRecord
err := r.DBX.GetContext(ctx, &scene, "SELECT * FROM scene WHERE name = ?", name)
if err != nil {
return nil, dbErr(err)
}
return r.populateOne(&scene)
}
func (r *SceneRepo) FetchAll(ctx context.Context) ([]models.Scene, error) {
scenes := make([]sceneRecord, 0, 8)
err := r.DBX.SelectContext(ctx, &scenes, "SELECT * FROM scene")
+83 -39
View File
@@ -8,9 +8,14 @@ import (
)
type ColorValue struct {
Hue float64 `json:"h,omitempty"` // 0..360
Saturation float64 `json:"s,omitempty"` // 0..=1
Kelvin int `json:"kelvin,omitempty"`
Hue float64 `json:"h,omitempty"` // 0..360
Saturation float64 `json:"s,omitempty"` // 0..=1
Kelvin int `json:"kelvin,omitempty"`
XY *ColorXY `json:"xy,omitempty"`
}
func (c *ColorValue) IsEmpty() bool {
return c.XY == nil && c.Kelvin == 0 && c.Saturation == 0 && c.Hue == 0
}
func (c *ColorValue) IsHueSat() bool {
@@ -18,7 +23,27 @@ func (c *ColorValue) IsHueSat() bool {
}
func (c *ColorValue) IsKelvin() bool {
return c.Kelvin > 0
return !c.IsXY() && c.Kelvin > 0
}
func (c *ColorValue) IsXY() bool {
return c.XY != nil
}
// ToXY converts the color to XY if possible. If the color already is XY, it returns
// a copy of its held value. There are no guarantees of conforming to a gamut, however.
func (c *ColorValue) ToXY() (xy ColorXY, ok bool) {
if c.XY != nil {
xy = *c.XY
ok = true
} else if c.Kelvin > 0 && c.Hue < 0.001 && c.Saturation <= 0.001 {
ok = false
} else {
xy = hsToXY(c.Hue, c.Saturation)
ok = true
}
return
}
func (c *ColorValue) String() string {
@@ -29,56 +54,75 @@ func (c *ColorValue) String() string {
return fmt.Sprintf("hs:%.4g,%.3g", c.Hue, c.Saturation)
}
func ParseColorValue(raw string) (ColorValue, error) {
tokens := strings.SplitN(raw, ":", 2)
if len(tokens) != 2 {
return ColorValue{}, ErrBadInput
}
if tokens[0] == "kelvin" || tokens[0] == "k" {
parsedPart, err := strconv.Atoi(tokens[1])
if err != nil {
return ColorValue{}, ErrBadInput
switch tokens[0] {
case "kelvin", "k":
{
parsedPart, err := strconv.Atoi(tokens[1])
if err != nil {
return ColorValue{}, ErrBadInput
}
return ColorValue{Kelvin: parsedPart}, nil
}
return ColorValue{Kelvin: parsedPart}, nil
}
case "xy":
{
parts := strings.Split(tokens[1], ",")
if len(parts) < 2 {
return ColorValue{}, ErrUnknownColorFormat
}
if tokens[0] == "hs" {
parts := strings.Split(tokens[1], ",")
if len(parts) < 2 {
return ColorValue{}, ErrUnknownColorFormat
x, err1 := strconv.ParseFloat(parts[0], 64)
y, err2 := strconv.ParseFloat(parts[1], 64)
if err1 != nil || err2 != nil {
return ColorValue{}, ErrBadInput
}
return ColorValue{XY: &ColorXY{X: x, Y: y}}, nil
}
part1, err1 := strconv.ParseFloat(parts[0], 64)
part2, err2 := strconv.ParseFloat(parts[1], 64)
if err1 != nil || err2 != nil {
return ColorValue{}, ErrBadInput
case "hs":
{
parts := strings.Split(tokens[1], ",")
if len(parts) < 2 {
return ColorValue{}, ErrUnknownColorFormat
}
part1, err1 := strconv.ParseFloat(parts[0], 64)
part2, err2 := strconv.ParseFloat(parts[1], 64)
if err1 != nil || err2 != nil {
return ColorValue{}, ErrBadInput
}
return ColorValue{Hue: math.Mod(part1, 360), Saturation: math.Min(math.Max(part2, 0), 1)}, nil
}
return ColorValue{Hue: math.Mod(part1, 360), Saturation: math.Min(math.Max(part2, 0), 1)}, nil
}
case "hsk":
{
parts := strings.Split(tokens[1], ",")
if len(parts) < 3 {
return ColorValue{}, ErrUnknownColorFormat
}
if tokens[0] == "hsk" {
parts := strings.Split(tokens[1], ",")
if len(parts) < 3 {
return ColorValue{}, ErrUnknownColorFormat
part1, err1 := strconv.ParseFloat(parts[0], 64)
part2, err2 := strconv.ParseFloat(parts[1], 64)
part3, err3 := strconv.Atoi(parts[2])
if err1 != nil || err2 != nil || err3 != nil {
return ColorValue{}, ErrBadInput
}
return ColorValue{
Hue: math.Mod(part1, 360),
Saturation: math.Min(math.Max(part2, 0), 1),
Kelvin: part3,
}, nil
}
part1, err1 := strconv.ParseFloat(parts[0], 64)
part2, err2 := strconv.ParseFloat(parts[1], 64)
part3, err3 := strconv.Atoi(parts[2])
if err1 != nil || err2 != nil || err3 != nil {
return ColorValue{}, ErrBadInput
}
return ColorValue{
Hue: math.Mod(part1, 360),
Saturation: math.Min(math.Max(part2, 0), 1),
Kelvin: part3,
}, nil
}
return ColorValue{}, ErrUnknownColorFormat
+204
View File
@@ -0,0 +1,204 @@
package models
import (
"github.com/lucasb-eyer/go-colorful"
"math"
)
const eps = 0.0001
const epsSquare = eps * eps
type ColorGamut struct {
Red ColorXY `json:"red"`
Green ColorXY `json:"green"`
Blue ColorXY `json:"blue"`
}
func (cg *ColorGamut) side(x1, y1, x2, y2, x, y float64) float64 {
return (y2-y1)*(x-x1) + (-x2+x1)*(y-y1)
}
func (cg *ColorGamut) naiveContains(color ColorXY) bool {
x, y := color.X, color.Y
x1, y1 := cg.Red.X, cg.Red.Y
x2, y2 := cg.Green.X, cg.Green.Y
x3, y3 := cg.Blue.X, cg.Blue.Y
checkSide1 := cg.side(x1, y1, x2, y2, x, y) < 0
checkSide2 := cg.side(x2, y2, x3, y3, x, y) < 0
checkSide3 := cg.side(x3, y3, x1, y1, x, y) < 0
return checkSide1 && checkSide2 && checkSide3
}
func (cg *ColorGamut) getBounds() (xMin, xMax, yMin, yMax float64) {
x1, y1 := cg.Red.X, cg.Red.Y
x2, y2 := cg.Green.X, cg.Green.Y
x3, y3 := cg.Blue.X, cg.Blue.Y
xMin = math.Min(x1, math.Min(x2, x3)) - eps
xMax = math.Max(x1, math.Max(x2, x3)) + eps
yMin = math.Min(y1, math.Min(y2, y3)) - eps
yMax = math.Max(y1, math.Max(y2, y3)) + eps
return
}
func (cg *ColorGamut) isInBounds(color ColorXY) bool {
x, y := color.X, color.Y
xMin, xMax, yMin, yMax := cg.getBounds()
return !(x < xMin || xMax < x || y < yMin || yMax < y)
}
func (cg *ColorGamut) distanceSquarePointToSegment(x1, y1, x2, y2, x, y float64) float64 {
sqLength1 := (x2-x1)*(x2-x1) + (y2-y1)*(y2-y1)
dotProduct := ((x-x1)*(x2-x1) + (y-y1)*(y2-y1)) / sqLength1
if dotProduct < 0 {
return (x-x1)*(x-x1) + (y-y1)*(y-y1)
} else if dotProduct <= 1 {
sqLength2 := (x1-x)*(x1-x) + (y1-y)*(y1-y)
return sqLength2 - dotProduct*dotProduct*sqLength1
} else {
return (x-x2)*(x-x2) + (y-y2)*(y-y2)
}
}
func (cg *ColorGamut) atTheEdge(color ColorXY) bool {
x, y := color.X, color.Y
x1, y1 := cg.Red.X, cg.Red.Y
x2, y2 := cg.Green.X, cg.Green.Y
x3, y3 := cg.Blue.X, cg.Blue.Y
if cg.distanceSquarePointToSegment(x1, y1, x2, y2, x, y) <= epsSquare {
return true
}
if cg.distanceSquarePointToSegment(x2, y2, x3, y3, x, y) <= epsSquare {
return true
}
if cg.distanceSquarePointToSegment(x3, y3, x1, y1, x, y) <= epsSquare {
return true
}
return false
}
func (cg *ColorGamut) Contains(color ColorXY) bool {
if cg == nil {
return true
}
return cg.isInBounds(color) && (cg.naiveContains(color) || cg.atTheEdge(color))
}
func (cg *ColorGamut) Conform(color ColorXY) ColorXY {
if cg.Contains(color) {
return color
}
var best *ColorXY
xMin, xMax, yMin, yMax := cg.getBounds()
for x := xMin; x < xMax; x += 0.001 {
for y := yMin; y < yMax; y += 0.001 {
color2 := ColorXY{X: x, Y: y}
if cg.Contains(color2) {
if best == nil || color.DistanceTo(color2) < color.DistanceTo(*best) {
best = &color2
}
}
}
}
if best == nil {
centerX := (cg.Red.X + cg.Green.X + cg.Blue.X) / 3
centerY := (cg.Red.Y + cg.Green.Y + cg.Blue.Y) / 3
stepX := (centerX - color.X) / 5000
stepY := (centerY - color.Y) / 5000
for !cg.Contains(color) {
color.X += stepX
color.Y += stepY
}
return color
}
for x := best.X - 0.001; x < best.X+0.001; x += 0.0001 {
for y := best.Y - 0.001; y < best.Y+0.001; y += 0.0001 {
color2 := ColorXY{X: x, Y: y}
if cg.atTheEdge(color2) {
if best == nil || color.DistanceTo(color2) < color.DistanceTo(*best) {
best = &color2
}
}
}
}
for x := best.X - 0.0001; x < best.X+0.0001; x += 0.00001 {
for y := best.Y - 0.0001; y < best.Y+0.0001; y += 0.00001 {
color2 := ColorXY{X: x, Y: y}
if cg.atTheEdge(color2) {
if best == nil || color.DistanceTo(color2) < color.DistanceTo(*best) {
best = &color2
}
}
}
}
return *best
}
type ColorXY struct {
X float64 `json:"x"`
Y float64 `json:"y"`
}
func (xy ColorXY) DistanceTo(other ColorXY) float64 {
return math.Sqrt(math.Pow(xy.X-other.X, 2) + math.Pow(xy.Y-other.Y, 2))
}
func (xy ColorXY) Round() ColorXY {
return ColorXY{
X: math.Round(xy.X*10000) / 10000,
Y: math.Round(xy.Y*10000) / 10000,
}
}
func hsToXY(hue, sat float64) ColorXY {
c := colorful.Hsv(hue, sat, 1)
red255, green255, blue255 := c.RGB255()
red := float64(red255) / 255.0
green := float64(green255) / 255.0
blue := float64(blue255) / 255.0
return screenRGBToXY(red, green, blue)
}
func rgbToXY(red float64, green float64, blue float64) ColorXY {
x := red*0.649926 + green*0.103455 + blue*0.197109
y := red*0.234327 + green*0.743075 + blue*0.022598
z := green*0.053077 + blue*1.035763
return ColorXY{
X: x / (x + y + z),
Y: y / (x + y + z),
}
}
func screenRGBToXY(red, green, blue float64) ColorXY {
for _, component := range []*float64{&red, &green, &blue} {
if *component > 0.04045 {
*component = math.Pow((*component+0.055)/(1.055), 2.4)
} else {
*component /= 12.92
}
}
return rgbToXY(red, green, blue)
}
+1
View File
@@ -86,6 +86,7 @@ var (
DCColorHS DeviceCapability = "ColorHS"
DCColorHSK DeviceCapability = "ColorHSK"
DCColorKelvin DeviceCapability = "ColorKelvin"
DCColorXY DeviceCapability = "ColorXY"
DCButtons DeviceCapability = "Buttons"
DCPresence DeviceCapability = "Presence"
DCIntensity DeviceCapability = "Intensity"
+4
View File
@@ -16,3 +16,7 @@ type Driver interface {
Publish(ctx context.Context, bridge Bridge, devices []Device) error
Run(ctx context.Context, bridge Bridge, ch chan<- Event) error
}
type ForgettableDriver interface {
ForgetDevice(ctx context.Context, bridge Bridge, device Device) error
}
+1
View File
@@ -16,6 +16,7 @@ var ErrUnexpectedResponse = errors.New("driver api returned unexpected response
var ErrBridgeSearchFailed = errors.New("bridge search failed")
var ErrAddressOnlyDryRunnable = errors.New("this address may only be used for a dry run")
var ErrCannotForwardRequest = errors.New("driver is not able to forward requests")
var ErrCannotForget = errors.New("forget not supported on this bridge type")
var ErrInvalidAddress = errors.New("invalid mac address")
var ErrPayloadTooShort = errors.New("payload too short")
+2
View File
@@ -299,6 +299,7 @@ const (
// instance is created for the group.
type DeviceSceneAssignment struct {
SceneID int `json:"sceneId"`
SceneName string `json:"sceneName"`
Group string `json:"group"`
StartTime time.Time `json:"start"`
DurationMS int64 `json:"durationMs"`
@@ -306,6 +307,7 @@ type DeviceSceneAssignment struct {
type SceneRepository interface {
Find(ctx context.Context, id int) (*Scene, error)
FindName(ctx context.Context, name string) (*Scene, error)
FetchAll(ctx context.Context) ([]Scene, error)
Save(ctx context.Context, bridge *Scene) error
Delete(ctx context.Context, bridge *Scene) error
@@ -0,0 +1,9 @@
-- +goose Up
-- +goose StatementBegin
CREATE UNIQUE INDEX scene_name ON scene (name);
-- +goose StatementEnd
-- +goose Down
-- +goose StatementBegin
DROP INDEX IF EXISTS scene_name;
-- +goose StatementEnd