From 4ab9714c487afa2b9a220fe3f4d7cda094376ebf Mon Sep 17 00:00:00 2001 From: Dan Jenkins Date: Sun, 8 Jan 2023 20:34:05 +0000 Subject: [PATCH] add events --- client.go | 15 ++++++++++++++- go.mod | 2 +- 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/client.go b/client.go index 812012a..fd8bde8 100644 --- a/client.go +++ b/client.go @@ -35,6 +35,8 @@ type Client struct { stateMutex sync.RWMutex timeRequests chan chan models.Timecode + + events map[string]func(SwitcherState) } func NewClient(log logrus.FieldLogger, host string) *Client { @@ -53,6 +55,10 @@ func NewClientWithPort(log logrus.FieldLogger, host string, port int) *Client { } } +func (c *Client) SetEvents(events map[string]func(SwitcherState)) { + c.events = events +} + func (c *Client) State() SwitcherState { c.stateMutex.RLock() state := c.state @@ -227,7 +233,7 @@ func (c *Client) processCommands(log logrus.FieldLogger, msg packet.Message) (in defer c.stateMutex.Unlock() for _, cmd := range msg.Commands { log.WithField("slug", cmd.Slug()).Debug("Starting to process command") - + funcName := "" switch t := cmd.(type) { case *cmds.UnknownCommand: log.WithField("slug", t.Slug()).Debug("Got unknown command") @@ -253,6 +259,7 @@ func (c *Client) processCommands(log logrus.FieldLogger, msg packet.Message) (in c.state.Config.SuperSources = int(*t) case *cmds.TlcCmd: c.state.Config.TallyChannels = int(*t) + funcName = "onTallyChannels" case *cmds.MacCmd: c.state.Config.MacroBanks = int(*t) case *cmds.PowrCmd: @@ -284,8 +291,10 @@ func (c *Client) processCommands(log logrus.FieldLogger, msg packet.Message) (in } case cmds.TlinCmd: c.state.TallyByIndex = t + funcName = "onTallyByIndex" case cmds.TlsrCmd: c.state.TallyBySource = t + funcName = "onTallyBySource" case *cmds.TimeCmd: tc := models.Timecode(*t) done := false @@ -306,6 +315,10 @@ func (c *Client) processCommands(log logrus.FieldLogger, msg packet.Message) (in default: log.Debug("Unhandled packet type") } + callback, ok := c.events[funcName] + if ok { + callback(c.state) + } } return } diff --git a/go.mod b/go.mod index dfa6e50..478388b 100644 --- a/go.mod +++ b/go.mod @@ -1,4 +1,4 @@ -module github.com/mraerino/atem-go +module github.com/everycastlabs/atem-go go 1.13