From d1d00237b0e31875f87de7d54f04185135d8741e Mon Sep 17 00:00:00 2001 From: Joel Wetzell Date: Thu, 20 Nov 2025 22:16:19 -0600 Subject: [PATCH] add timer and interval modules --- interval.go | 70 +++++++++++++++++++++++++++++++++++++++++++++++++++++ timer.go | 70 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 140 insertions(+) create mode 100644 interval.go create mode 100644 timer.go diff --git a/interval.go b/interval.go new file mode 100644 index 0000000..ba06d70 --- /dev/null +++ b/interval.go @@ -0,0 +1,70 @@ +package showbridge + +import ( + "context" + "fmt" + "log/slog" + "time" +) + +type Interval struct { + config ModuleConfig + Duration uint32 + router *Router +} + +func init() { + RegisterModule(ModuleRegistration{ + Type: "gen.interval", + New: func(config ModuleConfig) (Module, error) { + params := config.Params + + duration, ok := params["duration"] + if !ok { + return nil, fmt.Errorf("interval requires a duration parameter") + } + + durationNum, ok := duration.(float64) + + if !ok { + return nil, fmt.Errorf("interval duration must be number") + } + + return &Interval{Duration: uint32(durationNum), config: config}, nil + }, + }) +} + +func (i *Interval) Id() string { + return i.config.Id +} + +func (i *Interval) Type() string { + return i.config.Type +} + +func (i *Interval) RegisterRouter(router *Router) { + slog.Debug("registering router", "id", i.config.Id) + i.router = router +} + +func (i *Interval) Run(ctx context.Context) error { + ticker := time.NewTicker(time.Millisecond * time.Duration(i.Duration)) + defer ticker.Stop() + for { + select { + case <-ctx.Done(): + ticker.Stop() + return nil + case t := <-ticker.C: + if i.router != nil { + i.router.HandleInput(i.config.Id, t) + } + } + } + +} + +func (i *Interval) Output(payload any) error { + return fmt.Errorf("interval output is not implemented") +} diff --git a/timer.go b/timer.go new file mode 100644 index 0000000..b59b9d8 --- /dev/null +++ b/timer.go @@ -0,0 +1,70 @@ +package showbridge + +import ( + "context" + "fmt" + "log/slog" + "time" +) + +type Timer struct { + config ModuleConfig + Duration uint32 + router *Router + timer *time.Timer +} + +func init() { + RegisterModule(ModuleRegistration{ + Type: "gen.timer", + New: func(config ModuleConfig) (Module, error) { + params := config.Params + + duration, ok := params["duration"] + if !ok { + return nil, fmt.Errorf("timer requires a duration parameter") + } + + durationNum, ok := duration.(float64) + + if !ok { + return nil, fmt.Errorf("timer duration must be number") + } + + return &Interval{Duration: uint32(durationNum), config: config}, nil + }, + }) +} + +func (t *Timer) Id() string { + return t.config.Id +} + +func (t *Timer) Type() string { + return t.config.Type +} + +func (t *Timer) RegisterRouter(router *Router) { + slog.Debug("registering router", "id", t.config.Id) + t.router = router +} + +func (t *Timer) Run(ctx context.Context) error { + t.timer = time.NewTimer(time.Millisecond * time.Duration(t.Duration)) + defer t.timer.Stop() + for { + select { + case <-ctx.Done(): + t.timer.Stop() + return nil + case time := <-t.timer.C: + if t.router != nil { + t.router.HandleInput(t.config.Id, time) + } + } + } +} + +func (t *Timer) Output(payload any) error { + return fmt.Errorf("timer output is not implemented") +}