mirror of
https://github.com/jwetzell/showbridge-go.git
synced 2026-04-27 05:15:47 +00:00
add timer and interval modules
This commit is contained in:
70
interval.go
Normal file
70
interval.go
Normal 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
70
timer.go
Normal 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")
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user