add timer and interval modules

This commit is contained in:
Joel Wetzell
2025-11-20 22:16:19 -06:00
parent 7792444c3e
commit d1d00237b0
2 changed files with 140 additions and 0 deletions

70
interval.go Normal file
View File

@@ -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")
}

70
timer.go Normal file
View File

@@ -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")
}