From 5333a403ee212e96db8cdffd31bf308454ae4481 Mon Sep 17 00:00:00 2001 From: Gisle Aune Date: Thu, 29 Dec 2022 18:19:25 +0100 Subject: [PATCH] add log event + small bugfixes. --- bus.go | 2 +- events/log.go | 14 ++++++++++++++ services/hue/bridge.go | 14 ++++++-------- services/hue/service.go | 21 +++++++++++++++++++-- 4 files changed, 40 insertions(+), 11 deletions(-) create mode 100644 events/log.go diff --git a/bus.go b/bus.go index 7a8e050..0244a2d 100644 --- a/bus.go +++ b/bus.go @@ -65,7 +65,7 @@ func (b *EventBus) JoinPrivileged(service ActiveService) { } func (b *EventBus) RunCommand(command Command) { - if cd := command.CommandDescription(); !strings.HasPrefix(cd, "SetStates(") { + if cd := command.CommandDescription(); !strings.HasPrefix(cd, "SetStateBatch(") { if setStates := atomic.LoadInt32(&b.setStates); setStates > 0 { fmt.Println("[INFO]", setStates, "SetStates commands hidden.") atomic.AddInt32(&b.setStates, -setStates) diff --git a/events/log.go b/events/log.go new file mode 100644 index 0000000..081f34a --- /dev/null +++ b/events/log.go @@ -0,0 +1,14 @@ +package events + +import "fmt" + +type Log struct { + ID string `json:"id"` + Level string `json:"level"` + Code string `json:"code"` + Message string `json:"message"` +} + +func (l Log) EventDescription() string { + return fmt.Sprintf("LOG(id:%s level:%s code:%s :: %s)", l.ID, l.Level, l.Code, l.Message) +} diff --git a/services/hue/bridge.go b/services/hue/bridge.go index 3df3c80..18941df 100644 --- a/services/hue/bridge.go +++ b/services/hue/bridge.go @@ -347,15 +347,13 @@ func (b *Bridge) makeCongruentLoop(ctx context.Context) { } if dc.XY != nil { - if ac.XY != nil { - dist := math.Abs(ac.XY.X-dc.XY.X) + math.Abs(ac.XY.Y-dc.XY.Y) - if dist > 0.0002 { - update.ColorXY = gentools.Ptr(*dc.XY) - updated = true - } - } else { - newActiveState.Color = &color.Color{XY: gentools.Ptr(*dc.XY)} + if ac.K != nil { + ac.K = gentools.Ptr(1000000 / (1000000 / *ac.K)) + } + acXY, _ := ac.ToXY() + dist := dc.XY.DistanceTo(*acXY.XY) + if dist > 0.0002 { update.ColorXY = gentools.Ptr(*dc.XY) updated = true } diff --git a/services/hue/service.go b/services/hue/service.go index cdcef92..3329975 100644 --- a/services/hue/service.go +++ b/services/hue/service.go @@ -7,7 +7,6 @@ import ( "git.aiterp.net/lucifer3/server/commands" "git.aiterp.net/lucifer3/server/events" "git.aiterp.net/lucifer3/server/internal/gentools" - "log" "sync" "time" ) @@ -70,7 +69,12 @@ func (s *service) HandleCommand(bus *lucifer3.EventBus, command lucifer3.Command go func() { err := s.bridges[sub].SearchDevices(time.Second * 30) if err != nil { - log.Println("Search failed:", err) + bus.RunEvent(events.Log{ + ID: command.ID, + Level: "error", + Code: "search_failed", + Message: "Could not search: " + err.Error(), + }) } }() } @@ -92,6 +96,13 @@ func (s *service) HandleCommand(bus *lucifer3.EventBus, command lucifer3.Command s.bridges[sub] = bridge go func() { + bus.RunEvent(events.Log{ + ID: command.ID, + Level: "info", + Code: "connecting_device", + Message: "Connecting to device...", + }) + for bridge.ctx.Err() == nil { ctx2, cancel2 := context.WithCancel(ctx) @@ -102,6 +113,12 @@ func (s *service) HandleCommand(bus *lucifer3.EventBus, command lucifer3.Command ID: command.ID, Error: fmt.Sprintf("Run failed: %s", err), }) + bus.RunEvent(events.Log{ + ID: command.ID, + Level: "error", + Code: "connect_failed", + Message: fmt.Sprintf("Could not connect device: %s", err), + }) } select {