Gisle Aune
3 years ago
16 changed files with 467 additions and 178 deletions
-
48cmd/xy/main.go
-
8internal/drivers/hue/state.go
-
13internal/drivers/hue2/bridge.go
-
8internal/drivers/lifx/bridge.go
-
25internal/drivers/lifx/state.go
-
30internal/drivers/nanoleaf/bridge.go
-
20internal/mysql/devicerepo.go
-
17internal/mysql/presetrepo.go
-
17models/colorhs.go
-
29models/colorrgb.go
-
325models/colorvalue.go
-
44models/colorxy.go
-
16models/device.go
-
9scripts/20220220121712_color_preset_wipe.sql
-
9scripts/20220220121724_color_preset_add_column_value.sql
-
9scripts/20220220124631_device_state_color.sql
@ -1,49 +1,21 @@ |
|||
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)) |
|||
|
|||
ch := make(chan models.Event) |
|||
go func() { |
|||
for event := range ch { |
|||
log.Println("EVENT", event.Name, event.Payload) |
|||
} |
|||
}() |
|||
|
|||
for i, dev := range bridge.GenerateDevices() { |
|||
device := dev |
|||
switch device.InternalID { |
|||
case "6d5a45b0-ec69-4417-8588-717358b05086": |
|||
c, _ := models.ParseColorValue("xy:0.22,0.18") |
|||
device.State.Color = c |
|||
device.State.Intensity = 0.3 |
|||
case "a71128f4-5295-4ae4-9fbc-5541abc8739b": |
|||
c, _ := models.ParseColorValue("k:6500") |
|||
device.State.Color = c |
|||
device.State.Intensity = 0.2 |
|||
} |
|||
|
|||
device.ID = i + 1 |
|||
bridge.Update(device) |
|||
} |
|||
cv, _ := models.ParseColorValue("rgb:#fff") |
|||
hs, _ := cv.ToHS() |
|||
log.Println(cv.String()) |
|||
log.Println(hs.String()) |
|||
xy, _ := hs.ToXY() |
|||
log.Println(xy.String()) |
|||
} |
|||
|
|||
err = bridge.Run(context.Background(), ch) |
|||
log.Println(err) |
|||
func toJSON(v interface{}) string { |
|||
j, _ := json.Marshal(v) |
|||
return string(j) |
|||
} |
@ -0,0 +1,17 @@ |
|||
package models |
|||
|
|||
import "github.com/lucasb-eyer/go-colorful" |
|||
|
|||
type ColorHS struct { |
|||
Hue float64 `json:"hue"` |
|||
Sat float64 `json:"sat"` |
|||
} |
|||
|
|||
func (hs ColorHS) ToXY() ColorXY { |
|||
return hs.ToRGB().ToXY() |
|||
} |
|||
|
|||
func (hs ColorHS) ToRGB() ColorRGB { |
|||
c := colorful.Hsv(hs.Hue, hs.Sat, 1) |
|||
return ColorRGB{Red: c.R, Green: c.G, Blue: c.B} |
|||
} |
@ -0,0 +1,29 @@ |
|||
package models |
|||
|
|||
import "github.com/lucasb-eyer/go-colorful" |
|||
|
|||
type ColorRGB struct { |
|||
Red float64 `json:"red"` |
|||
Green float64 `json:"green"` |
|||
Blue float64 `json:"blue"` |
|||
} |
|||
|
|||
func (rgb ColorRGB) AtIntensity(intensity float64) ColorRGB { |
|||
hue, sat, _ := colorful.Color{R: rgb.Red, G: rgb.Green, B: rgb.Blue}.Hsv() |
|||
hsv2 := colorful.Hsv(hue, sat, intensity) |
|||
return ColorRGB{Red: hsv2.R, Green: hsv2.G, Blue: hsv2.B} |
|||
} |
|||
|
|||
func (rgb ColorRGB) ToHS() ColorHS { |
|||
hue, sat, _ := colorful.Color{R: rgb.Red, G: rgb.Green, B: rgb.Blue}.Hsv() |
|||
return ColorHS{Hue: hue, Sat: sat} |
|||
} |
|||
|
|||
func (rgb ColorRGB) ToXY() ColorXY { |
|||
x, y, z := (colorful.Color{R: rgb.Red, G: rgb.Green, B: rgb.Blue}).Xyz() |
|||
|
|||
return ColorXY{ |
|||
X: x / (x + y + z), |
|||
Y: y / (x + y + z), |
|||
} |
|||
} |
@ -0,0 +1,9 @@ |
|||
-- +goose Up |
|||
-- +goose StatementBegin |
|||
TRUNCATE color_preset; |
|||
-- +goose StatementEnd |
|||
|
|||
-- +goose Down |
|||
-- +goose StatementBegin |
|||
SELECT 'down SQL query'; |
|||
-- +goose StatementEnd |
@ -0,0 +1,9 @@ |
|||
-- +goose Up |
|||
-- +goose StatementBegin |
|||
ALTER TABLE color_preset ADD COLUMN value VARCHAR(255) NOT NULL DEFAULT 'hs:0,0'; |
|||
-- +goose StatementEnd |
|||
|
|||
-- +goose Down |
|||
-- +goose StatementBegin |
|||
ALTER TABLE color_preset DROP COLUMN value; |
|||
-- +goose StatementEnd |
@ -0,0 +1,9 @@ |
|||
-- +goose Up |
|||
-- +goose StatementBegin |
|||
ALTER TABLE device_state ADD COLUMN color VARCHAR(255) NOT NULL DEFAULT 'hs:0,0'; |
|||
-- +goose StatementEnd |
|||
|
|||
-- +goose Down |
|||
-- +goose StatementBegin |
|||
ALTER TABLE device_state DROP COLUMN IF EXISTS color; |
|||
-- +goose StatementEnd |
Write
Preview
Loading…
Cancel
Save
Reference in new issue