From c6da62d0c1d1b646f7d4627f0e8e358199a789e3 Mon Sep 17 00:00:00 2001 From: Gisle Aune Date: Thu, 18 Aug 2022 12:58:47 +0200 Subject: [PATCH] add trigger event idea. --- bus.go | 8 ++++++++ cmd/bustest/main.go | 11 +++++++++++ events/external.go | 21 +++++++++++++++++++++ 3 files changed, 40 insertions(+) create mode 100644 events/external.go diff --git a/bus.go b/bus.go index 5003a92..b7ee0de 100644 --- a/bus.go +++ b/bus.go @@ -11,6 +11,14 @@ type Event interface { EventDescription() string } +// A TriggerEvent is a catch-all for events that may trigger a command from the event handler. +type TriggerEvent interface { + Event + + TriggerKind() string + TriggerValue(key string) (string, bool) +} + type Command interface { CommandDescription() string } diff --git a/cmd/bustest/main.go b/cmd/bustest/main.go index ebf96ef..36ecb63 100644 --- a/cmd/bustest/main.go +++ b/cmd/bustest/main.go @@ -61,6 +61,17 @@ func main() { SceneID: 7, }) + bus.RunEvent(events.ExternalEvent{ + Kind: "weather", + Values: map[string]string{ + "city": "Ørland", + "region": "Trøndelag", + "country": "Norway", + "temperature_celsius": "21.00", + "precipitation_mm": "3.21", + }, + }) + time.Sleep(time.Second / 8) log.Println("Search \"**:Hexagon {1,5,6}\"") diff --git a/events/external.go b/events/external.go new file mode 100644 index 0000000..0fbb0c8 --- /dev/null +++ b/events/external.go @@ -0,0 +1,21 @@ +package events + +import "fmt" + +type ExternalEvent struct { + Kind string + Values map[string]string +} + +func (e ExternalEvent) EventDescription() string { + return fmt.Sprintf("External(kind:%s, values:%v)", e.Kind, e.Values) +} + +func (e ExternalEvent) TriggerKind() string { + return fmt.Sprintf("external.%s", e.Kind) +} + +func (e ExternalEvent) TriggerValue(key string) (string, bool) { + v, ok := e.Values[key] + return v, ok +}