mirror of
https://github.com/jwetzell/showbridge-go.git
synced 2026-04-26 21:05:30 +00:00
move timer and interval to time namespace
This commit is contained in:
@@ -10,7 +10,7 @@ import (
|
|||||||
"github.com/jwetzell/showbridge-go/internal/route"
|
"github.com/jwetzell/showbridge-go/internal/route"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Interval struct {
|
type TimeInterval struct {
|
||||||
config config.ModuleConfig
|
config config.ModuleConfig
|
||||||
Duration uint32
|
Duration uint32
|
||||||
ctx context.Context
|
ctx context.Context
|
||||||
@@ -21,35 +21,35 @@ type Interval struct {
|
|||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
RegisterModule(ModuleRegistration{
|
RegisterModule(ModuleRegistration{
|
||||||
Type: "gen.interval",
|
Type: "time.interval",
|
||||||
New: func(ctx context.Context, config config.ModuleConfig, router route.RouteIO) (Module, error) {
|
New: func(ctx context.Context, config config.ModuleConfig, router route.RouteIO) (Module, error) {
|
||||||
params := config.Params
|
params := config.Params
|
||||||
|
|
||||||
duration, ok := params["duration"]
|
duration, ok := params["duration"]
|
||||||
if !ok {
|
if !ok {
|
||||||
return nil, errors.New("gen.interval requires a duration parameter")
|
return nil, errors.New("time.interval requires a duration parameter")
|
||||||
}
|
}
|
||||||
|
|
||||||
durationNum, ok := duration.(float64)
|
durationNum, ok := duration.(float64)
|
||||||
|
|
||||||
if !ok {
|
if !ok {
|
||||||
return nil, errors.New("gen.interval duration must be number")
|
return nil, errors.New("time.interval duration must be number")
|
||||||
}
|
}
|
||||||
|
|
||||||
return &Interval{Duration: uint32(durationNum), config: config, ctx: ctx, router: router, logger: CreateLogger(config)}, nil
|
return &TimeInterval{Duration: uint32(durationNum), config: config, ctx: ctx, router: router, logger: CreateLogger(config)}, nil
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func (i *Interval) Id() string {
|
func (i *TimeInterval) Id() string {
|
||||||
return i.config.Id
|
return i.config.Id
|
||||||
}
|
}
|
||||||
|
|
||||||
func (i *Interval) Type() string {
|
func (i *TimeInterval) Type() string {
|
||||||
return i.config.Type
|
return i.config.Type
|
||||||
}
|
}
|
||||||
|
|
||||||
func (i *Interval) Run() error {
|
func (i *TimeInterval) Run() error {
|
||||||
ticker := time.NewTicker(time.Millisecond * time.Duration(i.Duration))
|
ticker := time.NewTicker(time.Millisecond * time.Duration(i.Duration))
|
||||||
i.ticker = ticker
|
i.ticker = ticker
|
||||||
defer ticker.Stop()
|
defer ticker.Stop()
|
||||||
@@ -68,7 +68,7 @@ func (i *Interval) Run() error {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (i *Interval) Output(payload any) error {
|
func (i *TimeInterval) Output(payload any) error {
|
||||||
i.ticker.Reset(time.Millisecond * time.Duration(i.Duration))
|
i.ticker.Reset(time.Millisecond * time.Duration(i.Duration))
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
@@ -10,7 +10,7 @@ import (
|
|||||||
"github.com/jwetzell/showbridge-go/internal/route"
|
"github.com/jwetzell/showbridge-go/internal/route"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Timer struct {
|
type TimeTimer struct {
|
||||||
config config.ModuleConfig
|
config config.ModuleConfig
|
||||||
Duration uint32
|
Duration uint32
|
||||||
ctx context.Context
|
ctx context.Context
|
||||||
@@ -21,35 +21,35 @@ type Timer struct {
|
|||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
RegisterModule(ModuleRegistration{
|
RegisterModule(ModuleRegistration{
|
||||||
Type: "gen.timer",
|
Type: "time.timer",
|
||||||
New: func(ctx context.Context, config config.ModuleConfig, router route.RouteIO) (Module, error) {
|
New: func(ctx context.Context, config config.ModuleConfig, router route.RouteIO) (Module, error) {
|
||||||
params := config.Params
|
params := config.Params
|
||||||
|
|
||||||
duration, ok := params["duration"]
|
duration, ok := params["duration"]
|
||||||
if !ok {
|
if !ok {
|
||||||
return nil, errors.New("gen.timer requires a duration parameter")
|
return nil, errors.New("time.timer requires a duration parameter")
|
||||||
}
|
}
|
||||||
|
|
||||||
durationNum, ok := duration.(float64)
|
durationNum, ok := duration.(float64)
|
||||||
|
|
||||||
if !ok {
|
if !ok {
|
||||||
return nil, errors.New("gen.timer duration must be a number")
|
return nil, errors.New("time.timer duration must be a number")
|
||||||
}
|
}
|
||||||
|
|
||||||
return &Timer{Duration: uint32(durationNum), config: config, ctx: ctx, router: router, logger: CreateLogger(config)}, nil
|
return &TimeTimer{Duration: uint32(durationNum), config: config, ctx: ctx, router: router, logger: CreateLogger(config)}, nil
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *Timer) Id() string {
|
func (t *TimeTimer) Id() string {
|
||||||
return t.config.Id
|
return t.config.Id
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *Timer) Type() string {
|
func (t *TimeTimer) Type() string {
|
||||||
return t.config.Type
|
return t.config.Type
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *Timer) Run() error {
|
func (t *TimeTimer) Run() error {
|
||||||
t.timer = time.NewTimer(time.Millisecond * time.Duration(t.Duration))
|
t.timer = time.NewTimer(time.Millisecond * time.Duration(t.Duration))
|
||||||
defer t.timer.Stop()
|
defer t.timer.Stop()
|
||||||
for {
|
for {
|
||||||
@@ -66,7 +66,7 @@ func (t *Timer) Run() error {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *Timer) Output(payload any) error {
|
func (t *TimeTimer) Output(payload any) error {
|
||||||
t.timer.Reset(time.Millisecond * time.Duration(t.Duration))
|
t.timer.Reset(time.Millisecond * time.Duration(t.Duration))
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user