mirror of
https://github.com/jwetzell/showbridge-go.git
synced 2026-04-30 14:55:28 +00:00
Compare commits
34 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
55f5767d47 | ||
|
|
66435fc2ba | ||
|
|
a85b0e64e0 | ||
|
|
202d1aea1f | ||
|
|
3a7b484c76 | ||
|
|
ffb39b1034 | ||
|
|
bb78b7b08b | ||
|
|
48ac2640d0 | ||
|
|
25324a4dcd | ||
|
|
bebcd56d70 | ||
|
|
ce8e18dd4f | ||
|
|
f4021d858b | ||
|
|
008d7665bd | ||
|
|
917f728f14 | ||
|
|
e3b6783a98 | ||
|
|
5e7ebd1bf1 | ||
|
|
0bd43ca0c3 | ||
|
|
2e7feede28 | ||
|
|
a5deea6447 | ||
|
|
3f6914282b | ||
|
|
7a01ce6691 | ||
|
|
5be40a5abf | ||
|
|
388851f8bd | ||
|
|
9a06949b5b | ||
|
|
8d8347fc4d | ||
|
|
07108918f1 | ||
|
|
2c6502b622 | ||
|
|
4c0f7c1723 | ||
|
|
04103cc6ca | ||
|
|
66097f7297 | ||
|
|
3eb3ccca0c | ||
|
|
55452d9dd0 | ||
|
|
ed4f7dc1f7 | ||
|
|
190bdd8b44 |
20
.github/labeler.yml
vendored
20
.github/labeler.yml
vendored
@@ -1,10 +1,22 @@
|
|||||||
framing:
|
config:
|
||||||
- changed-files:
|
- changed-files:
|
||||||
- any-glob-to-any-file: 'internal/framing/**'
|
- any-glob-to-any-file: 'internal/config/**'
|
||||||
|
|
||||||
processing:
|
framer:
|
||||||
- changed-files:
|
- changed-files:
|
||||||
- any-glob-to-any-file: 'internal/processing/**'
|
- any-glob-to-any-file: 'internal/framer/**'
|
||||||
|
|
||||||
|
module:
|
||||||
|
- changed-files:
|
||||||
|
- any-glob-to-any-file: 'internal/module/**'
|
||||||
|
|
||||||
|
processor:
|
||||||
|
- changed-files:
|
||||||
|
- any-glob-to-any-file: 'internal/processor/**'
|
||||||
|
|
||||||
|
route:
|
||||||
|
- changed-files:
|
||||||
|
- any-glob-to-any-file: 'internal/route/**'
|
||||||
|
|
||||||
cli:
|
cli:
|
||||||
- changed-files:
|
- changed-files:
|
||||||
|
|||||||
17
.github/release.yml
vendored
17
.github/release.yml
vendored
@@ -5,12 +5,21 @@ changelog:
|
|||||||
labels:
|
labels:
|
||||||
- dependencies
|
- dependencies
|
||||||
categories:
|
categories:
|
||||||
- title: Framing 🖼️
|
- title: Config ⚙️
|
||||||
labels:
|
labels:
|
||||||
- framing
|
- config
|
||||||
- title: Processing 🏭
|
- title: Framer 🖼️
|
||||||
labels:
|
labels:
|
||||||
- processing
|
- framer
|
||||||
|
- title: Module 📦
|
||||||
|
labels:
|
||||||
|
- module
|
||||||
|
- title: Processor 🏭
|
||||||
|
labels:
|
||||||
|
- processor
|
||||||
|
- title: Route 🛣️
|
||||||
|
labels:
|
||||||
|
- route
|
||||||
- title: CLI ⌨️
|
- title: CLI ⌨️
|
||||||
labels:
|
labels:
|
||||||
- cmd
|
- cmd
|
||||||
|
|||||||
@@ -8,6 +8,7 @@ import (
|
|||||||
"os/signal"
|
"os/signal"
|
||||||
|
|
||||||
"github.com/jwetzell/showbridge-go"
|
"github.com/jwetzell/showbridge-go"
|
||||||
|
"github.com/jwetzell/showbridge-go/internal/config"
|
||||||
"github.com/urfave/cli/v3"
|
"github.com/urfave/cli/v3"
|
||||||
"sigs.k8s.io/yaml"
|
"sigs.k8s.io/yaml"
|
||||||
)
|
)
|
||||||
@@ -54,19 +55,19 @@ func main() {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func readConfig(configPath string) (showbridge.Config, error) {
|
func readConfig(configPath string) (config.Config, error) {
|
||||||
|
cfg := config.Config{}
|
||||||
|
|
||||||
configBytes, err := os.ReadFile(configPath)
|
configBytes, err := os.ReadFile(configPath)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return showbridge.Config{}, err
|
return config.Config{}, err
|
||||||
}
|
}
|
||||||
|
|
||||||
config := showbridge.Config{}
|
err = yaml.Unmarshal(configBytes, &cfg)
|
||||||
|
|
||||||
err = yaml.Unmarshal(configBytes, &config)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return showbridge.Config{}, err
|
return config.Config{}, err
|
||||||
}
|
}
|
||||||
|
|
||||||
return config, nil
|
return cfg, nil
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +0,0 @@
|
|||||||
package showbridge
|
|
||||||
|
|
||||||
type Config struct {
|
|
||||||
Modules []ModuleConfig `json:"modules"`
|
|
||||||
Routes []RouteConfig `json:"routes"`
|
|
||||||
}
|
|
||||||
2
go.mod
2
go.mod
@@ -11,11 +11,13 @@ require (
|
|||||||
github.com/nats-io/nats.go v1.47.0
|
github.com/nats-io/nats.go v1.47.0
|
||||||
github.com/urfave/cli/v3 v3.6.1
|
github.com/urfave/cli/v3 v3.6.1
|
||||||
gitlab.com/gomidi/midi/v2 v2.3.16
|
gitlab.com/gomidi/midi/v2 v2.3.16
|
||||||
|
go.bug.st/serial v1.6.4
|
||||||
modernc.org/quickjs v0.17.0
|
modernc.org/quickjs v0.17.0
|
||||||
sigs.k8s.io/yaml v1.6.0
|
sigs.k8s.io/yaml v1.6.0
|
||||||
)
|
)
|
||||||
|
|
||||||
require (
|
require (
|
||||||
|
github.com/creack/goselect v0.1.2 // indirect
|
||||||
github.com/dustin/go-humanize v1.0.1 // indirect
|
github.com/dustin/go-humanize v1.0.1 // indirect
|
||||||
github.com/google/uuid v1.6.0 // indirect
|
github.com/google/uuid v1.6.0 // indirect
|
||||||
github.com/gorilla/websocket v1.5.3 // indirect
|
github.com/gorilla/websocket v1.5.3 // indirect
|
||||||
|
|||||||
4
go.sum
4
go.sum
@@ -1,3 +1,5 @@
|
|||||||
|
github.com/creack/goselect v0.1.2 h1:2DNy14+JPjRBgPzAd1thbQp4BSIihxcBf0IXhQXDRa0=
|
||||||
|
github.com/creack/goselect v0.1.2/go.mod h1:a/NhLweNvqIYMuxcMOuWY516Cimucms3DglDzQP3hKY=
|
||||||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
||||||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||||
github.com/dustin/go-humanize v1.0.1 h1:GzkhY7T5VNhEkwH0PVJgjz+fX1rhBrR7pRT3mDkpeCY=
|
github.com/dustin/go-humanize v1.0.1 h1:GzkhY7T5VNhEkwH0PVJgjz+fX1rhBrR7pRT3mDkpeCY=
|
||||||
@@ -42,6 +44,8 @@ github.com/urfave/cli/v3 v3.6.1 h1:j8Qq8NyUawj/7rTYdBGrxcH7A/j7/G8Q5LhWEW4G3Mo=
|
|||||||
github.com/urfave/cli/v3 v3.6.1/go.mod h1:ysVLtOEmg2tOy6PknnYVhDoouyC/6N42TMeoMzskhso=
|
github.com/urfave/cli/v3 v3.6.1/go.mod h1:ysVLtOEmg2tOy6PknnYVhDoouyC/6N42TMeoMzskhso=
|
||||||
gitlab.com/gomidi/midi/v2 v2.3.16 h1:yufWSENyjnJ4LFQa9BerzUm4E4aLfTyzw5nmnCteO0c=
|
gitlab.com/gomidi/midi/v2 v2.3.16 h1:yufWSENyjnJ4LFQa9BerzUm4E4aLfTyzw5nmnCteO0c=
|
||||||
gitlab.com/gomidi/midi/v2 v2.3.16/go.mod h1:jDpP4O4skYi+7iVwt6Zyp18bd2M4hkjtMuw2cmgKgfw=
|
gitlab.com/gomidi/midi/v2 v2.3.16/go.mod h1:jDpP4O4skYi+7iVwt6Zyp18bd2M4hkjtMuw2cmgKgfw=
|
||||||
|
go.bug.st/serial v1.6.4 h1:7FmqNPgVp3pu2Jz5PoPtbZ9jJO5gnEnZIvnI1lzve8A=
|
||||||
|
go.bug.st/serial v1.6.4/go.mod h1:nofMJxTeNVny/m6+KaafC6vJGj3miwQZ6vW4BZUGJPI=
|
||||||
go.yaml.in/yaml/v2 v2.4.2 h1:DzmwEr2rDGHl7lsFgAHxmNz/1NlQ7xLIrlN2h5d1eGI=
|
go.yaml.in/yaml/v2 v2.4.2 h1:DzmwEr2rDGHl7lsFgAHxmNz/1NlQ7xLIrlN2h5d1eGI=
|
||||||
go.yaml.in/yaml/v2 v2.4.2/go.mod h1:081UH+NErpNdqlCXm3TtEran0rJZGxAYx9hb/ELlsPU=
|
go.yaml.in/yaml/v2 v2.4.2/go.mod h1:081UH+NErpNdqlCXm3TtEran0rJZGxAYx9hb/ELlsPU=
|
||||||
go.yaml.in/yaml/v3 v3.0.3 h1:bXOww4E/J3f66rav3pX3m8w6jDE4knZjGOw8b5Y6iNE=
|
go.yaml.in/yaml/v3 v3.0.3 h1:bXOww4E/J3f66rav3pX3m8w6jDE4knZjGOw8b5Y6iNE=
|
||||||
|
|||||||
23
internal/config/config.go
Normal file
23
internal/config/config.go
Normal file
@@ -0,0 +1,23 @@
|
|||||||
|
package config
|
||||||
|
|
||||||
|
type Config struct {
|
||||||
|
Modules []ModuleConfig `json:"modules"`
|
||||||
|
Routes []RouteConfig `json:"routes"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type ModuleConfig struct {
|
||||||
|
Id string `json:"id"`
|
||||||
|
Type string `json:"type"`
|
||||||
|
Params map[string]any `json:"params"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type RouteConfig struct {
|
||||||
|
Input string `json:"input"`
|
||||||
|
Processors []ProcessorConfig `json:"processors"`
|
||||||
|
Output string `json:"output"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type ProcessorConfig struct {
|
||||||
|
Type string `json:"type"`
|
||||||
|
Params map[string]any `json:"params"`
|
||||||
|
}
|
||||||
29
internal/framer/framer.go
Normal file
29
internal/framer/framer.go
Normal file
@@ -0,0 +1,29 @@
|
|||||||
|
package framer
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Framer interface {
|
||||||
|
Decode([]byte) [][]byte
|
||||||
|
Encode([]byte) []byte
|
||||||
|
Clear()
|
||||||
|
Buffer() []byte
|
||||||
|
}
|
||||||
|
|
||||||
|
func GetFramer(framingType string) (Framer, error) {
|
||||||
|
switch framingType {
|
||||||
|
case "CR":
|
||||||
|
return NewByteSeparatorFramer([]byte{'\r'}), nil
|
||||||
|
case "LF":
|
||||||
|
return NewByteSeparatorFramer([]byte{'\n'}), nil
|
||||||
|
case "CRLF":
|
||||||
|
return NewByteSeparatorFramer([]byte{'\r', '\n'}), nil
|
||||||
|
case "SLIP":
|
||||||
|
return NewSlipFramer(), nil
|
||||||
|
case "RAW":
|
||||||
|
return NewRawFramer(), nil
|
||||||
|
default:
|
||||||
|
return nil, fmt.Errorf("unknown framing method: %s", framingType)
|
||||||
|
}
|
||||||
|
}
|
||||||
23
internal/framer/raw.go
Normal file
23
internal/framer/raw.go
Normal file
@@ -0,0 +1,23 @@
|
|||||||
|
package framer
|
||||||
|
|
||||||
|
type RawFramer struct{}
|
||||||
|
|
||||||
|
func NewRawFramer() *RawFramer {
|
||||||
|
return &RawFramer{}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (rf *RawFramer) Decode(data []byte) [][]byte {
|
||||||
|
return [][]byte{data}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (rf *RawFramer) Encode(data []byte) []byte {
|
||||||
|
return data
|
||||||
|
}
|
||||||
|
|
||||||
|
func (rf *RawFramer) Clear() {
|
||||||
|
// NOTE(jwetzell): no internal state to clear
|
||||||
|
}
|
||||||
|
|
||||||
|
func (rf *RawFramer) Buffer() []byte {
|
||||||
|
return []byte{}
|
||||||
|
}
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
package framing
|
package framer
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
@@ -35,3 +35,7 @@ func (bsf *ByteSeparatorFramer) Encode(data []byte) []byte {
|
|||||||
func (bsf *ByteSeparatorFramer) Clear() {
|
func (bsf *ByteSeparatorFramer) Clear() {
|
||||||
bsf.buffer = []byte{}
|
bsf.buffer = []byte{}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (bsf *ByteSeparatorFramer) Buffer() []byte {
|
||||||
|
return bsf.buffer
|
||||||
|
}
|
||||||
69
internal/framer/separator_test.go
Normal file
69
internal/framer/separator_test.go
Normal file
@@ -0,0 +1,69 @@
|
|||||||
|
package framer_test
|
||||||
|
|
||||||
|
import (
|
||||||
|
"slices"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/jwetzell/showbridge-go/internal/framer"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestGoodSeparatorFramer(t *testing.T) {
|
||||||
|
tests := []struct {
|
||||||
|
name string
|
||||||
|
framer framer.Framer
|
||||||
|
input []byte
|
||||||
|
expected [][]byte
|
||||||
|
buffer []byte
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
name: "new line separator",
|
||||||
|
framer: framer.NewByteSeparatorFramer([]byte{0x0a}),
|
||||||
|
input: []byte("Hello\nWorld\nThis is a test\n"),
|
||||||
|
expected: [][]byte{
|
||||||
|
[]byte("Hello"),
|
||||||
|
[]byte("World"),
|
||||||
|
[]byte("This is a test"),
|
||||||
|
},
|
||||||
|
buffer: []byte{},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "CRLF separator",
|
||||||
|
framer: framer.NewByteSeparatorFramer([]byte{0x0d, 0x0a}),
|
||||||
|
input: []byte("Hello\r\nWorld\r\nThis is a test\r\n"),
|
||||||
|
expected: [][]byte{
|
||||||
|
[]byte("Hello"),
|
||||||
|
[]byte("World"),
|
||||||
|
[]byte("This is a test"),
|
||||||
|
},
|
||||||
|
buffer: []byte{},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "extra data after separator",
|
||||||
|
framer: framer.NewByteSeparatorFramer([]byte{0x0d, 0x0a}),
|
||||||
|
input: []byte("Hello\r\nWorld\r\nThis is a test\r\nextra"),
|
||||||
|
expected: [][]byte{
|
||||||
|
[]byte("Hello"),
|
||||||
|
[]byte("World"),
|
||||||
|
[]byte("This is a test"),
|
||||||
|
},
|
||||||
|
buffer: []byte("extra"),
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, test := range tests {
|
||||||
|
t.Run(test.name, func(t *testing.T) {
|
||||||
|
frames := test.framer.Decode(test.input)
|
||||||
|
if len(frames) != len(test.expected) {
|
||||||
|
t.Errorf("separator framer got %d frames, expected %d", len(frames), len(test.expected))
|
||||||
|
}
|
||||||
|
for i, frame := range frames {
|
||||||
|
if !slices.Equal(frame, test.expected[i]) {
|
||||||
|
t.Errorf("separator framer frame %d got %s, expected %s", i, frame, test.expected[i])
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if !slices.Equal(test.framer.Buffer(), test.buffer) {
|
||||||
|
t.Errorf("separator framer buffer got %s, expected %s", test.framer.Buffer(), test.buffer)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
package framing
|
package framer
|
||||||
|
|
||||||
type SlipFramer struct {
|
type SlipFramer struct {
|
||||||
buffer []byte
|
buffer []byte
|
||||||
@@ -74,3 +74,7 @@ func (sf *SlipFramer) Encode(data []byte) []byte {
|
|||||||
func (sf *SlipFramer) Clear() {
|
func (sf *SlipFramer) Clear() {
|
||||||
sf.buffer = []byte{}
|
sf.buffer = []byte{}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (sf *SlipFramer) Buffer() []byte {
|
||||||
|
return sf.buffer
|
||||||
|
}
|
||||||
45
internal/framer/slip_test.go
Normal file
45
internal/framer/slip_test.go
Normal file
@@ -0,0 +1,45 @@
|
|||||||
|
package framer_test
|
||||||
|
|
||||||
|
import (
|
||||||
|
"slices"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/jwetzell/showbridge-go/internal/framer"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestGoodSLIPFramer(t *testing.T) {
|
||||||
|
tests := []struct {
|
||||||
|
name string
|
||||||
|
framer framer.Framer
|
||||||
|
input []byte
|
||||||
|
expected [][]byte
|
||||||
|
buffer []byte
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
name: "OSC SLIP messages",
|
||||||
|
framer: framer.NewSlipFramer(),
|
||||||
|
input: []byte{0xc0, 0x2f, 0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x00, 0x00, 0x2c, 0x00, 0x00, 0x00, 0xc0},
|
||||||
|
expected: [][]byte{
|
||||||
|
{0x2f, 0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x00, 0x00, 0x2c, 0x00, 0x00, 0x00},
|
||||||
|
},
|
||||||
|
buffer: []byte{},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, test := range tests {
|
||||||
|
t.Run(test.name, func(t *testing.T) {
|
||||||
|
frames := test.framer.Decode(test.input)
|
||||||
|
if len(frames) != len(test.expected) {
|
||||||
|
t.Errorf("SLIP framer got %d frames, expected %d", len(frames), len(test.expected))
|
||||||
|
}
|
||||||
|
for i, frame := range frames {
|
||||||
|
if !slices.Equal(frame, test.expected[i]) {
|
||||||
|
t.Errorf("SLIP framer frame %d got %s, expected %s", i, frame, test.expected[i])
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if !slices.Equal(test.framer.Buffer(), test.buffer) {
|
||||||
|
t.Errorf("SLIP framer buffer got %s, expected %s", test.framer.Buffer(), test.buffer)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,7 +0,0 @@
|
|||||||
package framing
|
|
||||||
|
|
||||||
type Framer interface {
|
|
||||||
Decode([]byte) [][]byte
|
|
||||||
Encode([]byte) []byte
|
|
||||||
Clear()
|
|
||||||
}
|
|
||||||
@@ -1,24 +1,29 @@
|
|||||||
package showbridge
|
package module
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
"log/slog"
|
"log/slog"
|
||||||
"net/http"
|
"net/http"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/jwetzell/showbridge-go/internal/config"
|
||||||
|
"github.com/jwetzell/showbridge-go/internal/route"
|
||||||
)
|
)
|
||||||
|
|
||||||
type HTTPClient struct {
|
type HTTPClient struct {
|
||||||
config ModuleConfig
|
config config.ModuleConfig
|
||||||
router *Router
|
ctx context.Context
|
||||||
client *http.Client
|
client *http.Client
|
||||||
|
router route.RouteIO
|
||||||
}
|
}
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
RegisterModule(ModuleRegistration{
|
RegisterModule(ModuleRegistration{
|
||||||
Type: "net.http.client",
|
Type: "net.http.client",
|
||||||
New: func(config ModuleConfig) (Module, error) {
|
New: func(ctx context.Context, config config.ModuleConfig, router route.RouteIO) (Module, error) {
|
||||||
|
|
||||||
return &HTTPClient{config: config}, nil
|
return &HTTPClient{config: config, ctx: ctx, router: router}, nil
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
@@ -31,17 +36,13 @@ func (hc *HTTPClient) Type() string {
|
|||||||
return hc.config.Type
|
return hc.config.Type
|
||||||
}
|
}
|
||||||
|
|
||||||
func (hc *HTTPClient) RegisterRouter(router *Router) {
|
|
||||||
hc.router = router
|
|
||||||
}
|
|
||||||
|
|
||||||
func (hc *HTTPClient) Run() error {
|
func (hc *HTTPClient) Run() error {
|
||||||
|
|
||||||
hc.client = &http.Client{
|
hc.client = &http.Client{
|
||||||
Timeout: 10 * time.Second,
|
Timeout: 10 * time.Second,
|
||||||
}
|
}
|
||||||
|
|
||||||
<-hc.router.Context.Done()
|
<-hc.ctx.Done()
|
||||||
slog.Debug("router context done in module", "id", hc.config.Id)
|
slog.Debug("router context done in module", "id", hc.config.Id)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
@@ -1,16 +1,21 @@
|
|||||||
package showbridge
|
package module
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"log/slog"
|
"log/slog"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
|
"github.com/jwetzell/showbridge-go/internal/config"
|
||||||
|
"github.com/jwetzell/showbridge-go/internal/route"
|
||||||
)
|
)
|
||||||
|
|
||||||
type HTTPServer struct {
|
type HTTPServer struct {
|
||||||
config ModuleConfig
|
config config.ModuleConfig
|
||||||
Port uint16
|
Port uint16
|
||||||
router *Router
|
ctx context.Context
|
||||||
|
router route.RouteIO
|
||||||
}
|
}
|
||||||
|
|
||||||
type ResponseData struct {
|
type ResponseData struct {
|
||||||
@@ -21,7 +26,7 @@ type ResponseData struct {
|
|||||||
func init() {
|
func init() {
|
||||||
RegisterModule(ModuleRegistration{
|
RegisterModule(ModuleRegistration{
|
||||||
Type: "net.http.server",
|
Type: "net.http.server",
|
||||||
New: func(config ModuleConfig) (Module, error) {
|
New: func(ctx context.Context, config config.ModuleConfig, router route.RouteIO) (Module, error) {
|
||||||
params := config.Params
|
params := config.Params
|
||||||
port, ok := params["port"]
|
port, ok := params["port"]
|
||||||
if !ok {
|
if !ok {
|
||||||
@@ -34,7 +39,7 @@ func init() {
|
|||||||
return nil, fmt.Errorf("net.http.server port must be uint16")
|
return nil, fmt.Errorf("net.http.server port must be uint16")
|
||||||
}
|
}
|
||||||
|
|
||||||
return &HTTPServer{Port: uint16(portNum), config: config}, nil
|
return &HTTPServer{Port: uint16(portNum), config: config, ctx: ctx, router: router}, nil
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
@@ -47,10 +52,6 @@ func (hs *HTTPServer) Type() string {
|
|||||||
return hs.config.Type
|
return hs.config.Type
|
||||||
}
|
}
|
||||||
|
|
||||||
func (hs *HTTPServer) RegisterRouter(router *Router) {
|
|
||||||
hs.router = router
|
|
||||||
}
|
|
||||||
|
|
||||||
func (hs *HTTPServer) HandleDefault(w http.ResponseWriter, r *http.Request) {
|
func (hs *HTTPServer) HandleDefault(w http.ResponseWriter, r *http.Request) {
|
||||||
w.Header().Set("Content-Type", "application/json")
|
w.Header().Set("Content-Type", "application/json")
|
||||||
|
|
||||||
@@ -87,18 +88,19 @@ func (hs *HTTPServer) Run() error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
go func() {
|
go func() {
|
||||||
<-hs.router.Context.Done()
|
<-hs.ctx.Done()
|
||||||
slog.Debug("router context done in module", "id", hs.config.Id)
|
slog.Debug("router context done in module", "id", hs.config.Id)
|
||||||
httpServer.Close()
|
httpServer.Close()
|
||||||
}()
|
}()
|
||||||
|
|
||||||
err := httpServer.ListenAndServe()
|
err := httpServer.ListenAndServe()
|
||||||
slog.Debug("net.http.server closed", "id", hs.config.Id)
|
slog.Debug("net.http.server closed", "id", hs.config.Id)
|
||||||
|
// TODO(jwetzell): handle server closed error differently
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
<-hs.router.Context.Done()
|
<-hs.ctx.Done()
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1,22 +1,27 @@
|
|||||||
package showbridge
|
package module
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
"log/slog"
|
"log/slog"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/jwetzell/showbridge-go/internal/config"
|
||||||
|
"github.com/jwetzell/showbridge-go/internal/route"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Interval struct {
|
type Interval struct {
|
||||||
config ModuleConfig
|
config config.ModuleConfig
|
||||||
Duration uint32
|
Duration uint32
|
||||||
router *Router
|
ctx context.Context
|
||||||
|
router route.RouteIO
|
||||||
ticker *time.Ticker
|
ticker *time.Ticker
|
||||||
}
|
}
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
RegisterModule(ModuleRegistration{
|
RegisterModule(ModuleRegistration{
|
||||||
Type: "gen.interval",
|
Type: "gen.interval",
|
||||||
New: func(config ModuleConfig) (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"]
|
||||||
@@ -30,7 +35,7 @@ func init() {
|
|||||||
return nil, fmt.Errorf("gen.interval duration must be number")
|
return nil, fmt.Errorf("gen.interval duration must be number")
|
||||||
}
|
}
|
||||||
|
|
||||||
return &Interval{Duration: uint32(durationNum), config: config}, nil
|
return &Interval{Duration: uint32(durationNum), config: config, ctx: ctx, router: router}, nil
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
@@ -43,10 +48,6 @@ func (i *Interval) Type() string {
|
|||||||
return i.config.Type
|
return i.config.Type
|
||||||
}
|
}
|
||||||
|
|
||||||
func (i *Interval) RegisterRouter(router *Router) {
|
|
||||||
i.router = router
|
|
||||||
}
|
|
||||||
|
|
||||||
func (i *Interval) Run() error {
|
func (i *Interval) 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
|
||||||
@@ -54,7 +55,7 @@ func (i *Interval) Run() error {
|
|||||||
|
|
||||||
for {
|
for {
|
||||||
select {
|
select {
|
||||||
case <-i.router.Context.Done():
|
case <-i.ctx.Done():
|
||||||
slog.Debug("router context done in module", "id", i.config.Id)
|
slog.Debug("router context done in module", "id", i.config.Id)
|
||||||
return nil
|
return nil
|
||||||
case <-ticker.C:
|
case <-ticker.C:
|
||||||
@@ -1,18 +1,22 @@
|
|||||||
//go:build cgo
|
//go:build cgo
|
||||||
|
|
||||||
package showbridge
|
package module
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
"log/slog"
|
"log/slog"
|
||||||
|
|
||||||
|
"github.com/jwetzell/showbridge-go/internal/config"
|
||||||
|
"github.com/jwetzell/showbridge-go/internal/route"
|
||||||
"gitlab.com/gomidi/midi/v2"
|
"gitlab.com/gomidi/midi/v2"
|
||||||
_ "gitlab.com/gomidi/midi/v2/drivers/rtmididrv"
|
_ "gitlab.com/gomidi/midi/v2/drivers/rtmididrv"
|
||||||
)
|
)
|
||||||
|
|
||||||
type MIDIClient struct {
|
type MIDIClient struct {
|
||||||
config ModuleConfig
|
config config.ModuleConfig
|
||||||
router *Router
|
ctx context.Context
|
||||||
|
router route.RouteIO
|
||||||
InputPort string
|
InputPort string
|
||||||
OutputPort string
|
OutputPort string
|
||||||
SendFunc func(midi.Message) error
|
SendFunc func(midi.Message) error
|
||||||
@@ -22,7 +26,7 @@ func init() {
|
|||||||
RegisterModule(ModuleRegistration{
|
RegisterModule(ModuleRegistration{
|
||||||
//TODO(jwetzell): find a better namespace than "misc"
|
//TODO(jwetzell): find a better namespace than "misc"
|
||||||
Type: "misc.midi.client",
|
Type: "misc.midi.client",
|
||||||
New: func(config ModuleConfig) (Module, error) {
|
New: func(ctx context.Context, config config.ModuleConfig, router route.RouteIO) (Module, error) {
|
||||||
params := config.Params
|
params := config.Params
|
||||||
input, ok := params["input"]
|
input, ok := params["input"]
|
||||||
|
|
||||||
@@ -48,7 +52,7 @@ func init() {
|
|||||||
return nil, fmt.Errorf("misc.midi.client output must be a string")
|
return nil, fmt.Errorf("misc.midi.client output must be a string")
|
||||||
}
|
}
|
||||||
|
|
||||||
return &MIDIClient{config: config, InputPort: inputString, OutputPort: outputString}, nil
|
return &MIDIClient{config: config, InputPort: inputString, OutputPort: outputString, ctx: ctx, router: router}, nil
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
@@ -61,10 +65,6 @@ func (mc *MIDIClient) Type() string {
|
|||||||
return mc.config.Type
|
return mc.config.Type
|
||||||
}
|
}
|
||||||
|
|
||||||
func (mc *MIDIClient) RegisterRouter(router *Router) {
|
|
||||||
mc.router = router
|
|
||||||
}
|
|
||||||
|
|
||||||
func (mc *MIDIClient) Run() error {
|
func (mc *MIDIClient) Run() error {
|
||||||
defer midi.CloseDriver()
|
defer midi.CloseDriver()
|
||||||
|
|
||||||
@@ -98,7 +98,7 @@ func (mc *MIDIClient) Run() error {
|
|||||||
|
|
||||||
mc.SendFunc = send
|
mc.SendFunc = send
|
||||||
|
|
||||||
<-mc.router.Context.Done()
|
<-mc.ctx.Done()
|
||||||
slog.Debug("router context done in module", "id", mc.config.Id)
|
slog.Debug("router context done in module", "id", mc.config.Id)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
@@ -1,33 +1,30 @@
|
|||||||
package showbridge
|
package module
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
"sync"
|
"sync"
|
||||||
|
|
||||||
|
"github.com/jwetzell/showbridge-go/internal/config"
|
||||||
|
"github.com/jwetzell/showbridge-go/internal/route"
|
||||||
)
|
)
|
||||||
|
|
||||||
type ModuleError struct {
|
type ModuleError struct {
|
||||||
Index int
|
Index int
|
||||||
Config ModuleConfig
|
Config config.ModuleConfig
|
||||||
Error error
|
Error error
|
||||||
}
|
}
|
||||||
|
|
||||||
type Module interface {
|
type Module interface {
|
||||||
Id() string
|
Id() string
|
||||||
Type() string
|
Type() string
|
||||||
RegisterRouter(*Router)
|
|
||||||
Run() error
|
Run() error
|
||||||
Output(any) error
|
Output(any) error
|
||||||
}
|
}
|
||||||
|
|
||||||
type ModuleConfig struct {
|
|
||||||
Id string `json:"id"`
|
|
||||||
Type string `json:"type"`
|
|
||||||
Params map[string]any `json:"params"`
|
|
||||||
}
|
|
||||||
|
|
||||||
type ModuleRegistration struct {
|
type ModuleRegistration struct {
|
||||||
Type string `json:"type"`
|
Type string `json:"type"`
|
||||||
New func(ModuleConfig) (Module, error)
|
New func(context.Context, config.ModuleConfig, route.RouteIO) (Module, error)
|
||||||
}
|
}
|
||||||
|
|
||||||
func RegisterModule(mod ModuleRegistration) {
|
func RegisterModule(mod ModuleRegistration) {
|
||||||
@@ -42,13 +39,13 @@ func RegisterModule(mod ModuleRegistration) {
|
|||||||
moduleRegistryMu.Lock()
|
moduleRegistryMu.Lock()
|
||||||
defer moduleRegistryMu.Unlock()
|
defer moduleRegistryMu.Unlock()
|
||||||
|
|
||||||
if _, ok := moduleRegistry[string(mod.Type)]; ok {
|
if _, ok := ModuleRegistry[string(mod.Type)]; ok {
|
||||||
panic(fmt.Sprintf("module already registered: %s", mod.Type))
|
panic(fmt.Sprintf("module already registered: %s", mod.Type))
|
||||||
}
|
}
|
||||||
moduleRegistry[string(mod.Type)] = mod
|
ModuleRegistry[string(mod.Type)] = mod
|
||||||
}
|
}
|
||||||
|
|
||||||
var (
|
var (
|
||||||
moduleRegistryMu sync.RWMutex
|
moduleRegistryMu sync.RWMutex
|
||||||
moduleRegistry = make(map[string]ModuleRegistration)
|
ModuleRegistry = make(map[string]ModuleRegistration)
|
||||||
)
|
)
|
||||||
@@ -1,16 +1,20 @@
|
|||||||
package showbridge
|
package module
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
"log/slog"
|
"log/slog"
|
||||||
|
|
||||||
mqtt "github.com/eclipse/paho.mqtt.golang"
|
mqtt "github.com/eclipse/paho.mqtt.golang"
|
||||||
"github.com/jwetzell/showbridge-go/internal/processing"
|
"github.com/jwetzell/showbridge-go/internal/config"
|
||||||
|
"github.com/jwetzell/showbridge-go/internal/processor"
|
||||||
|
"github.com/jwetzell/showbridge-go/internal/route"
|
||||||
)
|
)
|
||||||
|
|
||||||
type MQTTClient struct {
|
type MQTTClient struct {
|
||||||
config ModuleConfig
|
config config.ModuleConfig
|
||||||
router *Router
|
ctx context.Context
|
||||||
|
router route.RouteIO
|
||||||
Broker string
|
Broker string
|
||||||
ClientID string
|
ClientID string
|
||||||
Topic string
|
Topic string
|
||||||
@@ -20,7 +24,7 @@ type MQTTClient struct {
|
|||||||
func init() {
|
func init() {
|
||||||
RegisterModule(ModuleRegistration{
|
RegisterModule(ModuleRegistration{
|
||||||
Type: "net.mqtt.client",
|
Type: "net.mqtt.client",
|
||||||
New: func(config ModuleConfig) (Module, error) {
|
New: func(ctx context.Context, config config.ModuleConfig, router route.RouteIO) (Module, error) {
|
||||||
params := config.Params
|
params := config.Params
|
||||||
broker, ok := params["broker"]
|
broker, ok := params["broker"]
|
||||||
|
|
||||||
@@ -58,7 +62,7 @@ func init() {
|
|||||||
return nil, fmt.Errorf("net.mqtt.client clientId must be string")
|
return nil, fmt.Errorf("net.mqtt.client clientId must be string")
|
||||||
}
|
}
|
||||||
|
|
||||||
return &MQTTClient{config: config, Broker: brokerString, Topic: topicString, ClientID: clientIdString}, nil
|
return &MQTTClient{config: config, Broker: brokerString, Topic: topicString, ClientID: clientIdString, ctx: ctx, router: router}, nil
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
@@ -71,10 +75,6 @@ func (mc *MQTTClient) Type() string {
|
|||||||
return mc.config.Type
|
return mc.config.Type
|
||||||
}
|
}
|
||||||
|
|
||||||
func (mc *MQTTClient) RegisterRouter(router *Router) {
|
|
||||||
mc.router = router
|
|
||||||
}
|
|
||||||
|
|
||||||
func (mc *MQTTClient) Run() error {
|
func (mc *MQTTClient) Run() error {
|
||||||
opts := mqtt.NewClientOptions()
|
opts := mqtt.NewClientOptions()
|
||||||
opts.AddBroker(mc.Broker)
|
opts.AddBroker(mc.Broker)
|
||||||
@@ -99,13 +99,13 @@ func (mc *MQTTClient) Run() error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
<-mc.router.Context.Done()
|
<-mc.ctx.Done()
|
||||||
slog.Debug("router context done in module", "id", mc.config.Id)
|
slog.Debug("router context done in module", "id", mc.config.Id)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (mc *MQTTClient) Output(payload any) error {
|
func (mc *MQTTClient) Output(payload any) error {
|
||||||
payloadMessage, ok := payload.(processing.MQTTMessage)
|
payloadMessage, ok := payload.(processor.MQTTMessage)
|
||||||
|
|
||||||
if !ok {
|
if !ok {
|
||||||
return fmt.Errorf("net.mqtt.client is only able to output a MQTTMessage")
|
return fmt.Errorf("net.mqtt.client is only able to output a MQTTMessage")
|
||||||
@@ -1,16 +1,20 @@
|
|||||||
package showbridge
|
package module
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
"log/slog"
|
"log/slog"
|
||||||
|
|
||||||
"github.com/jwetzell/showbridge-go/internal/processing"
|
"github.com/jwetzell/showbridge-go/internal/config"
|
||||||
|
"github.com/jwetzell/showbridge-go/internal/processor"
|
||||||
|
"github.com/jwetzell/showbridge-go/internal/route"
|
||||||
"github.com/nats-io/nats.go"
|
"github.com/nats-io/nats.go"
|
||||||
)
|
)
|
||||||
|
|
||||||
type NATSClient struct {
|
type NATSClient struct {
|
||||||
config ModuleConfig
|
config config.ModuleConfig
|
||||||
router *Router
|
ctx context.Context
|
||||||
|
router route.RouteIO
|
||||||
URL string
|
URL string
|
||||||
Subject string
|
Subject string
|
||||||
client *nats.Conn
|
client *nats.Conn
|
||||||
@@ -19,7 +23,7 @@ type NATSClient struct {
|
|||||||
func init() {
|
func init() {
|
||||||
RegisterModule(ModuleRegistration{
|
RegisterModule(ModuleRegistration{
|
||||||
Type: "net.nats.client",
|
Type: "net.nats.client",
|
||||||
New: func(config ModuleConfig) (Module, error) {
|
New: func(ctx context.Context, config config.ModuleConfig, router route.RouteIO) (Module, error) {
|
||||||
params := config.Params
|
params := config.Params
|
||||||
url, ok := params["url"]
|
url, ok := params["url"]
|
||||||
|
|
||||||
@@ -45,7 +49,7 @@ func init() {
|
|||||||
return nil, fmt.Errorf("net.nats.client subject must be string")
|
return nil, fmt.Errorf("net.nats.client subject must be string")
|
||||||
}
|
}
|
||||||
|
|
||||||
return &NATSClient{config: config, URL: urlString, Subject: subjectString}, nil
|
return &NATSClient{config: config, URL: urlString, Subject: subjectString, ctx: ctx, router: router}, nil
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
@@ -58,10 +62,6 @@ func (nc *NATSClient) Type() string {
|
|||||||
return nc.config.Type
|
return nc.config.Type
|
||||||
}
|
}
|
||||||
|
|
||||||
func (nc *NATSClient) RegisterRouter(router *Router) {
|
|
||||||
nc.router = router
|
|
||||||
}
|
|
||||||
|
|
||||||
func (nc *NATSClient) Run() error {
|
func (nc *NATSClient) Run() error {
|
||||||
client, err := nats.Connect(nc.URL, nats.RetryOnFailedConnect(true))
|
client, err := nats.Connect(nc.URL, nats.RetryOnFailedConnect(true))
|
||||||
|
|
||||||
@@ -86,14 +86,14 @@ func (nc *NATSClient) Run() error {
|
|||||||
|
|
||||||
defer sub.Unsubscribe()
|
defer sub.Unsubscribe()
|
||||||
|
|
||||||
<-nc.router.Context.Done()
|
<-nc.ctx.Done()
|
||||||
slog.Debug("router context done in module", "id", nc.config.Id)
|
slog.Debug("router context done in module", "id", nc.config.Id)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (nc *NATSClient) Output(payload any) error {
|
func (nc *NATSClient) Output(payload any) error {
|
||||||
|
|
||||||
payloadMessage, ok := payload.(processing.NATSMessage)
|
payloadMessage, ok := payload.(processor.NATSMessage)
|
||||||
|
|
||||||
if !ok {
|
if !ok {
|
||||||
return fmt.Errorf("net.nats.client is only able to output NATSMessage")
|
return fmt.Errorf("net.nats.client is only able to output NATSMessage")
|
||||||
@@ -1,27 +1,31 @@
|
|||||||
package showbridge
|
package module
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
"log/slog"
|
"log/slog"
|
||||||
"net"
|
"net"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/jwetzell/psn-go"
|
"github.com/jwetzell/psn-go"
|
||||||
|
"github.com/jwetzell/showbridge-go/internal/config"
|
||||||
|
"github.com/jwetzell/showbridge-go/internal/route"
|
||||||
)
|
)
|
||||||
|
|
||||||
type PSNClient struct {
|
type PSNClient struct {
|
||||||
config ModuleConfig
|
config config.ModuleConfig
|
||||||
conn *net.UDPConn
|
conn *net.UDPConn
|
||||||
router *Router
|
ctx context.Context
|
||||||
|
router route.RouteIO
|
||||||
decoder *psn.Decoder
|
decoder *psn.Decoder
|
||||||
}
|
}
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
RegisterModule(ModuleRegistration{
|
RegisterModule(ModuleRegistration{
|
||||||
Type: "net.psn.client",
|
Type: "net.psn.client",
|
||||||
New: func(config ModuleConfig) (Module, error) {
|
New: func(ctx context.Context, config config.ModuleConfig, router route.RouteIO) (Module, error) {
|
||||||
|
|
||||||
return &PSNClient{config: config, decoder: psn.NewDecoder()}, nil
|
return &PSNClient{config: config, decoder: psn.NewDecoder(), ctx: ctx, router: router}, nil
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
@@ -34,10 +38,6 @@ func (pc *PSNClient) Type() string {
|
|||||||
return pc.config.Type
|
return pc.config.Type
|
||||||
}
|
}
|
||||||
|
|
||||||
func (pc *PSNClient) RegisterRouter(router *Router) {
|
|
||||||
pc.router = router
|
|
||||||
}
|
|
||||||
|
|
||||||
func (pc *PSNClient) Run() error {
|
func (pc *PSNClient) Run() error {
|
||||||
|
|
||||||
addr, err := net.ResolveUDPAddr("udp", "236.10.10.10:56565")
|
addr, err := net.ResolveUDPAddr("udp", "236.10.10.10:56565")
|
||||||
@@ -56,7 +56,7 @@ func (pc *PSNClient) Run() error {
|
|||||||
buffer := make([]byte, 2048)
|
buffer := make([]byte, 2048)
|
||||||
for {
|
for {
|
||||||
select {
|
select {
|
||||||
case <-pc.router.Context.Done():
|
case <-pc.ctx.Done():
|
||||||
// TODO(jwetzell): cleanup?
|
// TODO(jwetzell): cleanup?
|
||||||
slog.Debug("router context done in module", "id", pc.config.Id)
|
slog.Debug("router context done in module", "id", pc.config.Id)
|
||||||
return nil
|
return nil
|
||||||
174
internal/module/serial-client.go
Normal file
174
internal/module/serial-client.go
Normal file
@@ -0,0 +1,174 @@
|
|||||||
|
//go:build cgo
|
||||||
|
|
||||||
|
package module
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"fmt"
|
||||||
|
"log/slog"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/jwetzell/showbridge-go/internal/config"
|
||||||
|
"github.com/jwetzell/showbridge-go/internal/framer"
|
||||||
|
"github.com/jwetzell/showbridge-go/internal/route"
|
||||||
|
"go.bug.st/serial"
|
||||||
|
)
|
||||||
|
|
||||||
|
type SerialClient struct {
|
||||||
|
config config.ModuleConfig
|
||||||
|
ctx context.Context
|
||||||
|
router route.RouteIO
|
||||||
|
Port string
|
||||||
|
Framer framer.Framer
|
||||||
|
Mode *serial.Mode
|
||||||
|
port serial.Port
|
||||||
|
}
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
RegisterModule(ModuleRegistration{
|
||||||
|
//TODO(jwetzell): find a better namespace than "misc"
|
||||||
|
Type: "misc.serial.client",
|
||||||
|
New: func(ctx context.Context, config config.ModuleConfig, router route.RouteIO) (Module, error) {
|
||||||
|
params := config.Params
|
||||||
|
port, ok := params["port"]
|
||||||
|
|
||||||
|
if !ok {
|
||||||
|
return nil, fmt.Errorf("misc.serial.client requires a port parameter")
|
||||||
|
}
|
||||||
|
|
||||||
|
portString, ok := port.(string)
|
||||||
|
|
||||||
|
if !ok {
|
||||||
|
return nil, fmt.Errorf("misc.serial.client port must be a string")
|
||||||
|
}
|
||||||
|
|
||||||
|
framingMethod := "RAW"
|
||||||
|
|
||||||
|
framingMethodRaw, ok := params["framing"]
|
||||||
|
|
||||||
|
if ok {
|
||||||
|
framingMethodString, ok := framingMethodRaw.(string)
|
||||||
|
|
||||||
|
if !ok {
|
||||||
|
return nil, fmt.Errorf("misc.serial.client framing method must be a string")
|
||||||
|
}
|
||||||
|
framingMethod = framingMethodString
|
||||||
|
}
|
||||||
|
|
||||||
|
framer, err := framer.GetFramer(framingMethod)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
buadRate, ok := params["baudRate"]
|
||||||
|
if !ok {
|
||||||
|
return nil, fmt.Errorf("misc.serial.client requires a baudRate parameter")
|
||||||
|
}
|
||||||
|
|
||||||
|
baudRateNum, ok := buadRate.(float64)
|
||||||
|
if !ok {
|
||||||
|
return nil, fmt.Errorf("misc.serial.client baudRate must be a number")
|
||||||
|
}
|
||||||
|
|
||||||
|
mode := serial.Mode{
|
||||||
|
BaudRate: int(baudRateNum),
|
||||||
|
}
|
||||||
|
|
||||||
|
return &SerialClient{config: config, Port: portString, Framer: framer, Mode: &mode, ctx: ctx, router: router}, nil
|
||||||
|
},
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func (mc *SerialClient) Id() string {
|
||||||
|
return mc.config.Id
|
||||||
|
}
|
||||||
|
|
||||||
|
func (mc *SerialClient) Type() string {
|
||||||
|
return mc.config.Type
|
||||||
|
}
|
||||||
|
|
||||||
|
func (mc *SerialClient) SetupPort() error {
|
||||||
|
|
||||||
|
port, err := serial.Open(mc.Port, mc.Mode)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("misc.serial.client can't open input port: %s", mc.Port)
|
||||||
|
}
|
||||||
|
|
||||||
|
mc.port = port
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (mc *SerialClient) Run() error {
|
||||||
|
|
||||||
|
// TODO(jwetzell): shutdown with router.Context properly
|
||||||
|
go func() {
|
||||||
|
<-mc.ctx.Done()
|
||||||
|
slog.Debug("router context done in module", "id", mc.config.Id)
|
||||||
|
if mc.port != nil {
|
||||||
|
mc.port.Close()
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
|
for {
|
||||||
|
err := mc.SetupPort()
|
||||||
|
if err != nil {
|
||||||
|
if mc.ctx.Err() != nil {
|
||||||
|
slog.Debug("router context done in module", "id", mc.config.Id)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
slog.Error("misc.serial.client", "id", mc.config.Id, "error", err.Error())
|
||||||
|
time.Sleep(time.Second * 2)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
buffer := make([]byte, 1024)
|
||||||
|
select {
|
||||||
|
case <-mc.ctx.Done():
|
||||||
|
slog.Debug("router context done in module", "id", mc.config.Id)
|
||||||
|
return nil
|
||||||
|
default:
|
||||||
|
READ:
|
||||||
|
for {
|
||||||
|
select {
|
||||||
|
case <-mc.ctx.Done():
|
||||||
|
slog.Debug("router context done in module", "id", mc.config.Id)
|
||||||
|
return nil
|
||||||
|
default:
|
||||||
|
byteCount, err := mc.port.Read(buffer)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
mc.Framer.Clear()
|
||||||
|
break READ
|
||||||
|
}
|
||||||
|
|
||||||
|
if mc.Framer != nil {
|
||||||
|
if byteCount > 0 {
|
||||||
|
messages := mc.Framer.Decode(buffer[0:byteCount])
|
||||||
|
for _, message := range messages {
|
||||||
|
if mc.router != nil {
|
||||||
|
mc.router.HandleInput(mc.config.Id, message)
|
||||||
|
} else {
|
||||||
|
slog.Error("misc.serial.client has no router", "id", mc.config.Id)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (mc *SerialClient) Output(payload any) error {
|
||||||
|
|
||||||
|
payloadBytes, ok := payload.([]byte)
|
||||||
|
|
||||||
|
if !ok {
|
||||||
|
return fmt.Errorf("misc.serial.client can only ouptut bytes")
|
||||||
|
}
|
||||||
|
|
||||||
|
_, err := mc.port.Write(mc.Framer.Encode(payloadBytes))
|
||||||
|
return err
|
||||||
|
}
|
||||||
@@ -1,26 +1,30 @@
|
|||||||
package showbridge
|
package module
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
"log/slog"
|
"log/slog"
|
||||||
"net"
|
"net"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/jwetzell/showbridge-go/internal/framing"
|
"github.com/jwetzell/showbridge-go/internal/config"
|
||||||
|
"github.com/jwetzell/showbridge-go/internal/framer"
|
||||||
|
"github.com/jwetzell/showbridge-go/internal/route"
|
||||||
)
|
)
|
||||||
|
|
||||||
type TCPClient struct {
|
type TCPClient struct {
|
||||||
config ModuleConfig
|
config config.ModuleConfig
|
||||||
framer framing.Framer
|
framer framer.Framer
|
||||||
conn *net.TCPConn
|
conn *net.TCPConn
|
||||||
router *Router
|
ctx context.Context
|
||||||
|
router route.RouteIO
|
||||||
Addr *net.TCPAddr
|
Addr *net.TCPAddr
|
||||||
}
|
}
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
RegisterModule(ModuleRegistration{
|
RegisterModule(ModuleRegistration{
|
||||||
Type: "net.tcp.client",
|
Type: "net.tcp.client",
|
||||||
New: func(config ModuleConfig) (Module, error) {
|
New: func(ctx context.Context, config config.ModuleConfig, router route.RouteIO) (Module, error) {
|
||||||
params := config.Params
|
params := config.Params
|
||||||
host, ok := params["host"]
|
host, ok := params["host"]
|
||||||
|
|
||||||
@@ -50,33 +54,26 @@ func init() {
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
framingMethod, ok := params["framing"]
|
framingMethod := "RAW"
|
||||||
if !ok {
|
|
||||||
return nil, fmt.Errorf("net.tcp.client requires a framing method")
|
framingMethodRaw, ok := params["framing"]
|
||||||
|
|
||||||
|
if ok {
|
||||||
|
framingMethodString, ok := framingMethodRaw.(string)
|
||||||
|
|
||||||
|
if !ok {
|
||||||
|
return nil, fmt.Errorf("misc.serial.client framing method must be a string")
|
||||||
|
}
|
||||||
|
framingMethod = framingMethodString
|
||||||
}
|
}
|
||||||
|
|
||||||
framingMethodString, ok := framingMethod.(string)
|
framer, err := framer.GetFramer(framingMethod)
|
||||||
|
|
||||||
if !ok {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("net.tcp.client framing method must be a string")
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
var framer framing.Framer
|
return &TCPClient{framer: framer, Addr: addr, config: config, ctx: ctx, router: router}, nil
|
||||||
|
|
||||||
switch framingMethodString {
|
|
||||||
case "CR":
|
|
||||||
framer = framing.NewByteSeparatorFramer([]byte{'\r'})
|
|
||||||
case "LF":
|
|
||||||
framer = framing.NewByteSeparatorFramer([]byte{'\n'})
|
|
||||||
case "CRLF":
|
|
||||||
framer = framing.NewByteSeparatorFramer([]byte{'\r', '\n'})
|
|
||||||
case "SLIP":
|
|
||||||
framer = framing.NewSlipFramer()
|
|
||||||
default:
|
|
||||||
return nil, fmt.Errorf("unknown framing method: %s", framingMethodString)
|
|
||||||
}
|
|
||||||
|
|
||||||
return &TCPClient{framer: framer, Addr: addr, config: config}, nil
|
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
@@ -89,15 +86,11 @@ func (tc *TCPClient) Type() string {
|
|||||||
return tc.config.Type
|
return tc.config.Type
|
||||||
}
|
}
|
||||||
|
|
||||||
func (tc *TCPClient) RegisterRouter(router *Router) {
|
|
||||||
tc.router = router
|
|
||||||
}
|
|
||||||
|
|
||||||
func (tc *TCPClient) Run() error {
|
func (tc *TCPClient) Run() error {
|
||||||
|
|
||||||
// TODO(jwetzell): shutdown with router.Context properly
|
// TODO(jwetzell): shutdown with router.Context properly
|
||||||
go func() {
|
go func() {
|
||||||
<-tc.router.Context.Done()
|
<-tc.ctx.Done()
|
||||||
slog.Debug("router context done in module", "id", tc.config.Id)
|
slog.Debug("router context done in module", "id", tc.config.Id)
|
||||||
if tc.conn != nil {
|
if tc.conn != nil {
|
||||||
tc.conn.Close()
|
tc.conn.Close()
|
||||||
@@ -107,7 +100,7 @@ func (tc *TCPClient) Run() error {
|
|||||||
for {
|
for {
|
||||||
err := tc.SetupConn()
|
err := tc.SetupConn()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if tc.router.Context.Err() != nil {
|
if tc.ctx.Err() != nil {
|
||||||
slog.Debug("router context done in module", "id", tc.config.Id)
|
slog.Debug("router context done in module", "id", tc.config.Id)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
@@ -118,14 +111,14 @@ func (tc *TCPClient) Run() error {
|
|||||||
|
|
||||||
buffer := make([]byte, 1024)
|
buffer := make([]byte, 1024)
|
||||||
select {
|
select {
|
||||||
case <-tc.router.Context.Done():
|
case <-tc.ctx.Done():
|
||||||
slog.Debug("router context done in module", "id", tc.config.Id)
|
slog.Debug("router context done in module", "id", tc.config.Id)
|
||||||
return nil
|
return nil
|
||||||
default:
|
default:
|
||||||
READ:
|
READ:
|
||||||
for {
|
for {
|
||||||
select {
|
select {
|
||||||
case <-tc.router.Context.Done():
|
case <-tc.ctx.Done():
|
||||||
slog.Debug("router context done in module", "id", tc.config.Id)
|
slog.Debug("router context done in module", "id", tc.config.Id)
|
||||||
return nil
|
return nil
|
||||||
default:
|
default:
|
||||||
@@ -1,6 +1,7 @@
|
|||||||
package showbridge
|
package module
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"log/slog"
|
"log/slog"
|
||||||
@@ -10,25 +11,27 @@ import (
|
|||||||
"syscall"
|
"syscall"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/jwetzell/showbridge-go/internal/framing"
|
"github.com/jwetzell/showbridge-go/internal/config"
|
||||||
|
"github.com/jwetzell/showbridge-go/internal/framer"
|
||||||
|
"github.com/jwetzell/showbridge-go/internal/route"
|
||||||
)
|
)
|
||||||
|
|
||||||
type TCPServer struct {
|
type TCPServer struct {
|
||||||
config ModuleConfig
|
config config.ModuleConfig
|
||||||
Ip string
|
Addr *net.TCPAddr
|
||||||
Port uint16
|
Framer framer.Framer
|
||||||
framingMethod string
|
ctx context.Context
|
||||||
router *Router
|
router route.RouteIO
|
||||||
quit chan interface{}
|
quit chan interface{}
|
||||||
wg sync.WaitGroup
|
wg sync.WaitGroup
|
||||||
connections []net.Conn
|
connections []*net.TCPConn
|
||||||
connectionsMu sync.RWMutex
|
connectionsMu sync.RWMutex
|
||||||
}
|
}
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
RegisterModule(ModuleRegistration{
|
RegisterModule(ModuleRegistration{
|
||||||
Type: "net.tcp.server",
|
Type: "net.tcp.server",
|
||||||
New: func(config ModuleConfig) (Module, error) {
|
New: func(ctx context.Context, config config.ModuleConfig, router route.RouteIO) (Module, error) {
|
||||||
params := config.Params
|
params := config.Params
|
||||||
port, ok := params["port"]
|
port, ok := params["port"]
|
||||||
if !ok {
|
if !ok {
|
||||||
@@ -41,15 +44,23 @@ func init() {
|
|||||||
return nil, fmt.Errorf("net.tcp.server port must be a number")
|
return nil, fmt.Errorf("net.tcp.server port must be a number")
|
||||||
}
|
}
|
||||||
|
|
||||||
framingMethod, ok := params["framing"]
|
framingMethod := "RAW"
|
||||||
if !ok {
|
|
||||||
return nil, fmt.Errorf("net.tcp.server requires a framing method")
|
framingMethodRaw, ok := params["framing"]
|
||||||
|
|
||||||
|
if ok {
|
||||||
|
framingMethodString, ok := framingMethodRaw.(string)
|
||||||
|
|
||||||
|
if !ok {
|
||||||
|
return nil, fmt.Errorf("misc.serial.client framing method must be a string")
|
||||||
|
}
|
||||||
|
framingMethod = framingMethodString
|
||||||
}
|
}
|
||||||
|
|
||||||
framingMethodString, ok := framingMethod.(string)
|
framer, err := framer.GetFramer(framingMethod)
|
||||||
|
|
||||||
if !ok {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("net.tcp.server framing method must be a string")
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
ipString := "0.0.0.0"
|
ipString := "0.0.0.0"
|
||||||
@@ -65,7 +76,12 @@ func init() {
|
|||||||
ipString = specificIpString
|
ipString = specificIpString
|
||||||
}
|
}
|
||||||
|
|
||||||
return &TCPServer{framingMethod: framingMethodString, Port: uint16(portNum), Ip: ipString, config: config, quit: make(chan interface{})}, nil
|
addr, err := net.ResolveTCPAddr("tcp", fmt.Sprintf("%s:%d", ipString, uint16(portNum)))
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return &TCPServer{Framer: framer, Addr: addr, config: config, quit: make(chan interface{}), ctx: ctx, router: router}, nil
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
@@ -78,28 +94,12 @@ func (ts *TCPServer) Type() string {
|
|||||||
return ts.config.Type
|
return ts.config.Type
|
||||||
}
|
}
|
||||||
|
|
||||||
func (ts *TCPServer) RegisterRouter(router *Router) {
|
func (ts *TCPServer) handleClient(client *net.TCPConn) {
|
||||||
ts.router = router
|
|
||||||
}
|
|
||||||
|
|
||||||
func (ts *TCPServer) handleClient(client net.Conn) {
|
|
||||||
ts.connectionsMu.Lock()
|
ts.connectionsMu.Lock()
|
||||||
ts.connections = append(ts.connections, client)
|
ts.connections = append(ts.connections, client)
|
||||||
ts.connectionsMu.Unlock()
|
ts.connectionsMu.Unlock()
|
||||||
slog.Debug("net.tcp.server connection accepted", "id", ts.config.Id, "remoteAddr", client.RemoteAddr().String())
|
slog.Debug("net.tcp.server connection accepted", "id", ts.config.Id, "remoteAddr", client.RemoteAddr().String())
|
||||||
defer client.Close()
|
defer client.Close()
|
||||||
var framer framing.Framer
|
|
||||||
|
|
||||||
switch ts.framingMethod {
|
|
||||||
case "LF":
|
|
||||||
framer = framing.NewByteSeparatorFramer([]byte{'\n'})
|
|
||||||
case "CR":
|
|
||||||
framer = framing.NewByteSeparatorFramer([]byte{'\r'})
|
|
||||||
case "CRLF":
|
|
||||||
framer = framing.NewByteSeparatorFramer([]byte{'\r', '\n'})
|
|
||||||
case "SLIP":
|
|
||||||
framer = framing.NewSlipFramer()
|
|
||||||
}
|
|
||||||
|
|
||||||
buffer := make([]byte, 1024)
|
buffer := make([]byte, 1024)
|
||||||
ClientRead:
|
ClientRead:
|
||||||
@@ -143,9 +143,9 @@ ClientRead:
|
|||||||
}
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if framer != nil {
|
if ts.Framer != nil {
|
||||||
if byteCount > 0 {
|
if byteCount > 0 {
|
||||||
messages := framer.Decode(buffer[0:byteCount])
|
messages := ts.Framer.Decode(buffer[0:byteCount])
|
||||||
for _, message := range messages {
|
for _, message := range messages {
|
||||||
if ts.router != nil {
|
if ts.router != nil {
|
||||||
ts.router.HandleInput(ts.config.Id, message)
|
ts.router.HandleInput(ts.config.Id, message)
|
||||||
@@ -160,15 +160,14 @@ ClientRead:
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (ts *TCPServer) Run() error {
|
func (ts *TCPServer) Run() error {
|
||||||
// TODO(jwetzell): switch to net.ListenTCP and move addr resolution to init
|
listener, err := net.ListenTCP("tcp", ts.Addr)
|
||||||
listener, err := net.Listen("tcp", fmt.Sprintf("%s:%d", ts.Ip, ts.Port))
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
ts.wg.Add(1)
|
ts.wg.Add(1)
|
||||||
|
|
||||||
go func() {
|
go func() {
|
||||||
<-ts.router.Context.Done()
|
<-ts.ctx.Done()
|
||||||
close(ts.quit)
|
close(ts.quit)
|
||||||
listener.Close()
|
listener.Close()
|
||||||
slog.Debug("router context done in module", "id", ts.config.Id)
|
slog.Debug("router context done in module", "id", ts.config.Id)
|
||||||
@@ -176,7 +175,7 @@ func (ts *TCPServer) Run() error {
|
|||||||
|
|
||||||
AcceptLoop:
|
AcceptLoop:
|
||||||
for {
|
for {
|
||||||
conn, err := listener.Accept()
|
conn, err := listener.AcceptTCP()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
select {
|
select {
|
||||||
case <-ts.quit:
|
case <-ts.quit:
|
||||||
@@ -1,22 +1,27 @@
|
|||||||
package showbridge
|
package module
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
"log/slog"
|
"log/slog"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/jwetzell/showbridge-go/internal/config"
|
||||||
|
"github.com/jwetzell/showbridge-go/internal/route"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Timer struct {
|
type Timer struct {
|
||||||
config ModuleConfig
|
config config.ModuleConfig
|
||||||
Duration uint32
|
Duration uint32
|
||||||
router *Router
|
ctx context.Context
|
||||||
|
router route.RouteIO
|
||||||
timer *time.Timer
|
timer *time.Timer
|
||||||
}
|
}
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
RegisterModule(ModuleRegistration{
|
RegisterModule(ModuleRegistration{
|
||||||
Type: "gen.timer",
|
Type: "gen.timer",
|
||||||
New: func(config ModuleConfig) (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"]
|
||||||
@@ -30,7 +35,7 @@ func init() {
|
|||||||
return nil, fmt.Errorf("gen.timer duration must be a number")
|
return nil, fmt.Errorf("gen.timer duration must be a number")
|
||||||
}
|
}
|
||||||
|
|
||||||
return &Timer{Duration: uint32(durationNum), config: config}, nil
|
return &Timer{Duration: uint32(durationNum), config: config, ctx: ctx, router: router}, nil
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
@@ -43,16 +48,12 @@ func (t *Timer) Type() string {
|
|||||||
return t.config.Type
|
return t.config.Type
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *Timer) RegisterRouter(router *Router) {
|
|
||||||
t.router = router
|
|
||||||
}
|
|
||||||
|
|
||||||
func (t *Timer) Run() error {
|
func (t *Timer) 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 {
|
||||||
select {
|
select {
|
||||||
case <-t.router.Context.Done():
|
case <-t.ctx.Done():
|
||||||
t.timer.Stop()
|
t.timer.Stop()
|
||||||
slog.Debug("router context done in module", "id", t.config.Id)
|
slog.Debug("router context done in module", "id", t.config.Id)
|
||||||
return nil
|
return nil
|
||||||
@@ -1,24 +1,28 @@
|
|||||||
package showbridge
|
package module
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
"log/slog"
|
"log/slog"
|
||||||
"net"
|
"net"
|
||||||
|
|
||||||
|
"github.com/jwetzell/showbridge-go/internal/config"
|
||||||
|
"github.com/jwetzell/showbridge-go/internal/route"
|
||||||
)
|
)
|
||||||
|
|
||||||
type UDPClient struct {
|
type UDPClient struct {
|
||||||
config ModuleConfig
|
config config.ModuleConfig
|
||||||
Host string
|
Addr *net.UDPAddr
|
||||||
Port uint16
|
Port uint16
|
||||||
conn *net.UDPConn
|
conn *net.UDPConn
|
||||||
router *Router
|
ctx context.Context
|
||||||
addr *net.UDPAddr
|
router route.RouteIO
|
||||||
}
|
}
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
RegisterModule(ModuleRegistration{
|
RegisterModule(ModuleRegistration{
|
||||||
Type: "net.udp.client",
|
Type: "net.udp.client",
|
||||||
New: func(config ModuleConfig) (Module, error) {
|
New: func(ctx context.Context, config config.ModuleConfig, router route.RouteIO) (Module, error) {
|
||||||
params := config.Params
|
params := config.Params
|
||||||
host, ok := params["host"]
|
host, ok := params["host"]
|
||||||
|
|
||||||
@@ -43,7 +47,12 @@ func init() {
|
|||||||
return nil, fmt.Errorf("net.udp.client port must be a number")
|
return nil, fmt.Errorf("net.udp.client port must be a number")
|
||||||
}
|
}
|
||||||
|
|
||||||
return &UDPClient{Host: hostString, Port: uint16(portNum), config: config}, nil
|
addr, err := net.ResolveUDPAddr("udp", fmt.Sprintf("%s:%d", hostString, uint16(portNum)))
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return &UDPClient{Addr: addr, config: config, ctx: ctx, router: router}, nil
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
@@ -56,20 +65,20 @@ func (uc *UDPClient) Type() string {
|
|||||||
return uc.config.Type
|
return uc.config.Type
|
||||||
}
|
}
|
||||||
|
|
||||||
func (uc *UDPClient) RegisterRouter(router *Router) {
|
func (uc *UDPClient) SetupConn() error {
|
||||||
uc.router = router
|
client, err := net.DialUDP("udp", nil, uc.Addr)
|
||||||
|
uc.conn = client
|
||||||
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
func (uc *UDPClient) Run() error {
|
func (uc *UDPClient) Run() error {
|
||||||
|
|
||||||
addr, err := net.ResolveUDPAddr("udp", fmt.Sprintf("%s:%d", uc.Host, uc.Port))
|
err := uc.SetupConn()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
uc.addr = addr
|
<-uc.ctx.Done()
|
||||||
|
|
||||||
<-uc.router.Context.Done()
|
|
||||||
slog.Debug("router context done in module", "id", uc.config.Id)
|
slog.Debug("router context done in module", "id", uc.config.Id)
|
||||||
if uc.conn != nil {
|
if uc.conn != nil {
|
||||||
uc.conn.Close()
|
uc.conn.Close()
|
||||||
@@ -83,19 +92,14 @@ func (uc *UDPClient) Output(payload any) error {
|
|||||||
if !ok {
|
if !ok {
|
||||||
return fmt.Errorf("net.udp.client is only able to output bytes")
|
return fmt.Errorf("net.udp.client is only able to output bytes")
|
||||||
}
|
}
|
||||||
|
if uc.conn != nil {
|
||||||
|
_, err := uc.conn.Write(payloadBytes)
|
||||||
|
|
||||||
// TODO(jwetzell): reuse connection or setup new one when necessary
|
if err != nil {
|
||||||
client, err := net.DialUDP("udp", nil, uc.addr)
|
return err
|
||||||
if err != nil {
|
}
|
||||||
return err
|
} else {
|
||||||
}
|
return fmt.Errorf("net.udp.client client is not setup")
|
||||||
|
|
||||||
uc.conn = client
|
|
||||||
|
|
||||||
_, err = uc.conn.Write(payloadBytes)
|
|
||||||
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
@@ -1,23 +1,28 @@
|
|||||||
package showbridge
|
package module
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
"log/slog"
|
"log/slog"
|
||||||
"net"
|
"net"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/jwetzell/showbridge-go/internal/config"
|
||||||
|
"github.com/jwetzell/showbridge-go/internal/route"
|
||||||
)
|
)
|
||||||
|
|
||||||
type UDPMulticast struct {
|
type UDPMulticast struct {
|
||||||
config ModuleConfig
|
config config.ModuleConfig
|
||||||
conn *net.UDPConn
|
conn *net.UDPConn
|
||||||
router *Router
|
ctx context.Context
|
||||||
|
router route.RouteIO
|
||||||
Addr *net.UDPAddr
|
Addr *net.UDPAddr
|
||||||
}
|
}
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
RegisterModule(ModuleRegistration{
|
RegisterModule(ModuleRegistration{
|
||||||
Type: "net.udp.multicast",
|
Type: "net.udp.multicast",
|
||||||
New: func(config ModuleConfig) (Module, error) {
|
New: func(ctx context.Context, config config.ModuleConfig, router route.RouteIO) (Module, error) {
|
||||||
params := config.Params
|
params := config.Params
|
||||||
ip, ok := params["ip"]
|
ip, ok := params["ip"]
|
||||||
|
|
||||||
@@ -46,7 +51,7 @@ func init() {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
return &UDPMulticast{config: config, Addr: addr}, nil
|
return &UDPMulticast{config: config, Addr: addr, ctx: ctx, router: router}, nil
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
@@ -59,10 +64,6 @@ func (um *UDPMulticast) Type() string {
|
|||||||
return um.config.Type
|
return um.config.Type
|
||||||
}
|
}
|
||||||
|
|
||||||
func (um *UDPMulticast) RegisterRouter(router *Router) {
|
|
||||||
um.router = router
|
|
||||||
}
|
|
||||||
|
|
||||||
func (um *UDPMulticast) Run() error {
|
func (um *UDPMulticast) Run() error {
|
||||||
|
|
||||||
client, err := net.ListenMulticastUDP("udp", nil, um.Addr)
|
client, err := net.ListenMulticastUDP("udp", nil, um.Addr)
|
||||||
@@ -76,7 +77,7 @@ func (um *UDPMulticast) Run() error {
|
|||||||
buffer := make([]byte, 2048)
|
buffer := make([]byte, 2048)
|
||||||
for {
|
for {
|
||||||
select {
|
select {
|
||||||
case <-um.router.Context.Done():
|
case <-um.ctx.Done():
|
||||||
// TODO(jwetzell): cleanup?
|
// TODO(jwetzell): cleanup?
|
||||||
slog.Debug("router context done in module", "id", um.config.Id)
|
slog.Debug("router context done in module", "id", um.config.Id)
|
||||||
return nil
|
return nil
|
||||||
@@ -94,9 +95,6 @@ func (um *UDPMulticast) Run() error {
|
|||||||
|
|
||||||
if numBytes > 0 {
|
if numBytes > 0 {
|
||||||
message := buffer[:numBytes]
|
message := buffer[:numBytes]
|
||||||
if err != nil {
|
|
||||||
slog.Error("net.udp.multicast problem decoding psn traffic", "id", um.config.Id, "error", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
if um.router != nil {
|
if um.router != nil {
|
||||||
um.router.HandleInput(um.config.Id, message)
|
um.router.HandleInput(um.config.Id, message)
|
||||||
@@ -109,5 +107,16 @@ func (um *UDPMulticast) Run() error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (um *UDPMulticast) Output(payload any) error {
|
func (um *UDPMulticast) Output(payload any) error {
|
||||||
return fmt.Errorf("net.udp.multicast output is not implemented")
|
|
||||||
|
payloadBytes, ok := payload.([]byte)
|
||||||
|
if !ok {
|
||||||
|
return fmt.Errorf("net.udp.multicast can only output bytes")
|
||||||
|
}
|
||||||
|
|
||||||
|
if um.conn == nil {
|
||||||
|
return fmt.Errorf("net.udp.multicast connection is not setup")
|
||||||
|
}
|
||||||
|
|
||||||
|
_, err := um.conn.Write(payloadBytes)
|
||||||
|
return err
|
||||||
}
|
}
|
||||||
@@ -1,24 +1,28 @@
|
|||||||
package showbridge
|
package module
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
"log/slog"
|
"log/slog"
|
||||||
"net"
|
"net"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/jwetzell/showbridge-go/internal/config"
|
||||||
|
"github.com/jwetzell/showbridge-go/internal/route"
|
||||||
)
|
)
|
||||||
|
|
||||||
type UDPServer struct {
|
type UDPServer struct {
|
||||||
Ip string
|
Addr *net.UDPAddr
|
||||||
Port uint16
|
config config.ModuleConfig
|
||||||
config ModuleConfig
|
ctx context.Context
|
||||||
router *Router
|
router route.RouteIO
|
||||||
}
|
}
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
RegisterModule(ModuleRegistration{
|
RegisterModule(ModuleRegistration{
|
||||||
Type: "net.udp.server",
|
Type: "net.udp.server",
|
||||||
New: func(config ModuleConfig) (Module, error) {
|
New: func(ctx context.Context, config config.ModuleConfig, router route.RouteIO) (Module, error) {
|
||||||
params := config.Params
|
params := config.Params
|
||||||
port, ok := params["port"]
|
port, ok := params["port"]
|
||||||
if !ok {
|
if !ok {
|
||||||
@@ -44,7 +48,12 @@ func init() {
|
|||||||
ipString = specificIpString
|
ipString = specificIpString
|
||||||
}
|
}
|
||||||
|
|
||||||
return &UDPServer{Ip: ipString, Port: uint16(portNum), config: config}, nil
|
addr, err := net.ResolveUDPAddr("udp", fmt.Sprintf("%s:%d", ipString, uint16(portNum)))
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalf("error resolving UDP address: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return &UDPServer{Addr: addr, config: config, ctx: ctx, router: router}, nil
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
@@ -57,29 +66,20 @@ func (us *UDPServer) Type() string {
|
|||||||
return us.config.Id
|
return us.config.Id
|
||||||
}
|
}
|
||||||
|
|
||||||
func (us *UDPServer) RegisterRouter(router *Router) {
|
|
||||||
us.router = router
|
|
||||||
}
|
|
||||||
|
|
||||||
func (us *UDPServer) Run() error {
|
func (us *UDPServer) Run() error {
|
||||||
|
|
||||||
// TODO(jwetzell): move this to init
|
listener, err := net.ListenUDP("udp", us.Addr)
|
||||||
addr, err := net.ResolveUDPAddr("udp", fmt.Sprintf("%s:%d", us.Ip, us.Port))
|
|
||||||
if err != nil {
|
|
||||||
log.Fatalf("error resolving UDP address: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
listener, err := net.ListenUDP("udp", addr)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
defer listener.Close()
|
defer listener.Close()
|
||||||
|
|
||||||
buffer := make([]byte, 1024)
|
// TODO(jwetzell): make buffer size configurable
|
||||||
|
buffer := make([]byte, 65535)
|
||||||
for {
|
for {
|
||||||
select {
|
select {
|
||||||
case <-us.router.Context.Done():
|
case <-us.ctx.Done():
|
||||||
// TODO(jwetzell): cleanup?
|
// TODO(jwetzell): cleanup?
|
||||||
slog.Debug("router context done in module", "id", us.config.Id)
|
slog.Debug("router context done in module", "id", us.config.Id)
|
||||||
return nil
|
return nil
|
||||||
@@ -1,41 +0,0 @@
|
|||||||
package processing_test
|
|
||||||
|
|
||||||
import (
|
|
||||||
"testing"
|
|
||||||
|
|
||||||
"github.com/jwetzell/showbridge-go/internal/processing"
|
|
||||||
)
|
|
||||||
|
|
||||||
func TestGoodStringDecode(t *testing.T) {
|
|
||||||
stringDecoder := processing.StringDecode{}
|
|
||||||
tests := []struct {
|
|
||||||
processor processing.Processor
|
|
||||||
name string
|
|
||||||
payload any
|
|
||||||
expected string
|
|
||||||
}{
|
|
||||||
{
|
|
||||||
processor: &stringDecoder,
|
|
||||||
name: "hello",
|
|
||||||
payload: []byte{0x68, 0x65, 0x6c, 0x6c, 0x6f},
|
|
||||||
expected: "hello",
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, test := range tests {
|
|
||||||
t.Run(test.name, func(t *testing.T) {
|
|
||||||
got, err := test.processor.Process(t.Context(), test.payload)
|
|
||||||
|
|
||||||
gotString, ok := got.(string)
|
|
||||||
if !ok {
|
|
||||||
t.Errorf("string.decode returned a %T payload: %s", got, got)
|
|
||||||
}
|
|
||||||
if err != nil {
|
|
||||||
t.Errorf("string.decode failed: %s", err)
|
|
||||||
}
|
|
||||||
if gotString != test.expected {
|
|
||||||
t.Errorf("string.decode got %s, expected %s", got, test.expected)
|
|
||||||
}
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,42 +0,0 @@
|
|||||||
package processing_test
|
|
||||||
|
|
||||||
import (
|
|
||||||
"slices"
|
|
||||||
"testing"
|
|
||||||
|
|
||||||
"github.com/jwetzell/showbridge-go/internal/processing"
|
|
||||||
)
|
|
||||||
|
|
||||||
func TestGoodStringEncode(t *testing.T) {
|
|
||||||
stringEncoder := processing.StringEncode{}
|
|
||||||
tests := []struct {
|
|
||||||
processor processing.Processor
|
|
||||||
name string
|
|
||||||
payload any
|
|
||||||
expected []byte
|
|
||||||
}{
|
|
||||||
{
|
|
||||||
processor: &stringEncoder,
|
|
||||||
name: "hello",
|
|
||||||
payload: "hello",
|
|
||||||
expected: []byte{0x68, 0x65, 0x6c, 0x6c, 0x6f},
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, test := range tests {
|
|
||||||
t.Run(test.name, func(t *testing.T) {
|
|
||||||
got, err := test.processor.Process(t.Context(), test.payload)
|
|
||||||
|
|
||||||
gotBytes, ok := got.([]byte)
|
|
||||||
if !ok {
|
|
||||||
t.Errorf("string.encode returned a %T payload: %s", got, got)
|
|
||||||
}
|
|
||||||
if err != nil {
|
|
||||||
t.Errorf("string.encode failed: %s", err)
|
|
||||||
}
|
|
||||||
if !slices.Equal(gotBytes, test.expected) {
|
|
||||||
t.Errorf("string.encode got %s, expected %s", got, test.expected)
|
|
||||||
}
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,13 +1,15 @@
|
|||||||
package processing
|
package processor
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
"log/slog"
|
"log/slog"
|
||||||
|
|
||||||
|
"github.com/jwetzell/showbridge-go/internal/config"
|
||||||
)
|
)
|
||||||
|
|
||||||
type DebugLog struct {
|
type DebugLog struct {
|
||||||
config ProcessorConfig
|
config config.ProcessorConfig
|
||||||
}
|
}
|
||||||
|
|
||||||
func (dl *DebugLog) Process(ctx context.Context, payload any) (any, error) {
|
func (dl *DebugLog) Process(ctx context.Context, payload any) (any, error) {
|
||||||
@@ -22,7 +24,7 @@ func (dl *DebugLog) Type() string {
|
|||||||
func init() {
|
func init() {
|
||||||
RegisterProcessor(ProcessorRegistration{
|
RegisterProcessor(ProcessorRegistration{
|
||||||
Type: "debug.log",
|
Type: "debug.log",
|
||||||
New: func(config ProcessorConfig) (Processor, error) {
|
New: func(config config.ProcessorConfig) (Processor, error) {
|
||||||
return &DebugLog{config: config}, nil
|
return &DebugLog{config: config}, nil
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
@@ -1,13 +1,15 @@
|
|||||||
package processing
|
package processor
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
|
||||||
|
"github.com/jwetzell/showbridge-go/internal/config"
|
||||||
)
|
)
|
||||||
|
|
||||||
type FloatParse struct {
|
type FloatParse struct {
|
||||||
config ProcessorConfig
|
config config.ProcessorConfig
|
||||||
}
|
}
|
||||||
|
|
||||||
func (fp *FloatParse) Process(ctx context.Context, payload any) (any, error) {
|
func (fp *FloatParse) Process(ctx context.Context, payload any) (any, error) {
|
||||||
@@ -32,7 +34,7 @@ func (fp *FloatParse) Type() string {
|
|||||||
func init() {
|
func init() {
|
||||||
RegisterProcessor(ProcessorRegistration{
|
RegisterProcessor(ProcessorRegistration{
|
||||||
Type: "float.parse",
|
Type: "float.parse",
|
||||||
New: func(config ProcessorConfig) (Processor, error) {
|
New: func(config config.ProcessorConfig) (Processor, error) {
|
||||||
return &FloatParse{config: config}, nil
|
return &FloatParse{config: config}, nil
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
85
internal/processor/float-parse_test.go
Normal file
85
internal/processor/float-parse_test.go
Normal file
@@ -0,0 +1,85 @@
|
|||||||
|
package processor_test
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/jwetzell/showbridge-go/internal/processor"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestGoodFloatParse(t *testing.T) {
|
||||||
|
floatParser := processor.FloatParse{}
|
||||||
|
tests := []struct {
|
||||||
|
processor processor.Processor
|
||||||
|
name string
|
||||||
|
payload any
|
||||||
|
expected float64
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
name: "positive number",
|
||||||
|
payload: "12345.67",
|
||||||
|
expected: 12345.67,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "negative number",
|
||||||
|
payload: "-12345.67",
|
||||||
|
expected: -12345.67,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "zero",
|
||||||
|
payload: "0",
|
||||||
|
expected: 0,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, test := range tests {
|
||||||
|
t.Run(test.name, func(t *testing.T) {
|
||||||
|
got, err := floatParser.Process(t.Context(), test.payload)
|
||||||
|
|
||||||
|
gotFloat, ok := got.(float64)
|
||||||
|
if !ok {
|
||||||
|
t.Errorf("float.parse returned a %T payload: %s", got, got)
|
||||||
|
}
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("float.parse failed: %s", err)
|
||||||
|
}
|
||||||
|
if gotFloat != test.expected {
|
||||||
|
t.Errorf("float.parse got %f, expected %f", gotFloat, test.expected)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestBadFloatParse(t *testing.T) {
|
||||||
|
floatParser := processor.FloatParse{}
|
||||||
|
tests := []struct {
|
||||||
|
processor processor.Processor
|
||||||
|
name string
|
||||||
|
payload any
|
||||||
|
errorString string
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
name: "non-string input",
|
||||||
|
payload: []byte{0x01},
|
||||||
|
errorString: "float.parse processor only accepts a string",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "not float string",
|
||||||
|
payload: "abcd",
|
||||||
|
errorString: "strconv.ParseFloat: parsing \"abcd\": invalid syntax",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, test := range tests {
|
||||||
|
t.Run(test.name, func(t *testing.T) {
|
||||||
|
got, err := floatParser.Process(t.Context(), test.payload)
|
||||||
|
|
||||||
|
if err == nil {
|
||||||
|
t.Errorf("float.parse expected to fail but succeeded, got: %v", got)
|
||||||
|
|
||||||
|
}
|
||||||
|
if err.Error() != test.errorString {
|
||||||
|
t.Errorf("float.parse got error '%s', expected '%s'", err.Error(), test.errorString)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
package processing
|
package processor
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
@@ -8,10 +8,11 @@ import (
|
|||||||
"text/template"
|
"text/template"
|
||||||
|
|
||||||
freeD "github.com/jwetzell/free-d-go"
|
freeD "github.com/jwetzell/free-d-go"
|
||||||
|
"github.com/jwetzell/showbridge-go/internal/config"
|
||||||
)
|
)
|
||||||
|
|
||||||
type FreeDCreate struct {
|
type FreeDCreate struct {
|
||||||
config ProcessorConfig
|
config config.ProcessorConfig
|
||||||
Id *template.Template
|
Id *template.Template
|
||||||
Pan *template.Template
|
Pan *template.Template
|
||||||
Tilt *template.Template
|
Tilt *template.Template
|
||||||
@@ -51,6 +52,10 @@ func (fc *FreeDCreate) Process(ctx context.Context, payload any) (any, error) {
|
|||||||
|
|
||||||
panNum, err := strconv.ParseFloat(panString, 32)
|
panNum, err := strconv.ParseFloat(panString, 32)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
var tiltBuffer bytes.Buffer
|
var tiltBuffer bytes.Buffer
|
||||||
err = fc.Tilt.Execute(&tiltBuffer, payload)
|
err = fc.Tilt.Execute(&tiltBuffer, payload)
|
||||||
|
|
||||||
@@ -62,6 +67,10 @@ func (fc *FreeDCreate) Process(ctx context.Context, payload any) (any, error) {
|
|||||||
|
|
||||||
tiltNum, err := strconv.ParseFloat(tiltString, 32)
|
tiltNum, err := strconv.ParseFloat(tiltString, 32)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
var rollBuffer bytes.Buffer
|
var rollBuffer bytes.Buffer
|
||||||
err = fc.Tilt.Execute(&rollBuffer, payload)
|
err = fc.Tilt.Execute(&rollBuffer, payload)
|
||||||
|
|
||||||
@@ -77,10 +86,6 @@ func (fc *FreeDCreate) Process(ctx context.Context, payload any) (any, error) {
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
var posXBuffer bytes.Buffer
|
var posXBuffer bytes.Buffer
|
||||||
err = fc.PosX.Execute(&posXBuffer, payload)
|
err = fc.PosX.Execute(&posXBuffer, payload)
|
||||||
|
|
||||||
@@ -178,7 +183,7 @@ func (fc *FreeDCreate) Type() string {
|
|||||||
func init() {
|
func init() {
|
||||||
RegisterProcessor(ProcessorRegistration{
|
RegisterProcessor(ProcessorRegistration{
|
||||||
Type: "freed.create",
|
Type: "freed.create",
|
||||||
New: func(config ProcessorConfig) (Processor, error) {
|
New: func(config config.ProcessorConfig) (Processor, error) {
|
||||||
|
|
||||||
// TODO(jwetzell): make some params optional
|
// TODO(jwetzell): make some params optional
|
||||||
params := config.Params
|
params := config.Params
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
package processing
|
package processor
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
@@ -6,10 +6,11 @@ import (
|
|||||||
"log/slog"
|
"log/slog"
|
||||||
|
|
||||||
freeD "github.com/jwetzell/free-d-go"
|
freeD "github.com/jwetzell/free-d-go"
|
||||||
|
"github.com/jwetzell/showbridge-go/internal/config"
|
||||||
)
|
)
|
||||||
|
|
||||||
type FreeDDecode struct {
|
type FreeDDecode struct {
|
||||||
config ProcessorConfig
|
config config.ProcessorConfig
|
||||||
}
|
}
|
||||||
|
|
||||||
func (fdd *FreeDDecode) Process(ctx context.Context, payload any) (any, error) {
|
func (fdd *FreeDDecode) Process(ctx context.Context, payload any) (any, error) {
|
||||||
@@ -33,7 +34,7 @@ func (fdd *FreeDDecode) Type() string {
|
|||||||
func init() {
|
func init() {
|
||||||
RegisterProcessor(ProcessorRegistration{
|
RegisterProcessor(ProcessorRegistration{
|
||||||
Type: "freed.decode",
|
Type: "freed.decode",
|
||||||
New: func(config ProcessorConfig) (Processor, error) {
|
New: func(config config.ProcessorConfig) (Processor, error) {
|
||||||
return &FreeDDecode{config: config}, nil
|
return &FreeDDecode{config: config}, nil
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
@@ -1,14 +1,15 @@
|
|||||||
package processing
|
package processor
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
freeD "github.com/jwetzell/free-d-go"
|
freeD "github.com/jwetzell/free-d-go"
|
||||||
|
"github.com/jwetzell/showbridge-go/internal/config"
|
||||||
)
|
)
|
||||||
|
|
||||||
type FreeDEncode struct {
|
type FreeDEncode struct {
|
||||||
config ProcessorConfig
|
config config.ProcessorConfig
|
||||||
}
|
}
|
||||||
|
|
||||||
func (fde *FreeDEncode) Process(ctx context.Context, payload any) (any, error) {
|
func (fde *FreeDEncode) Process(ctx context.Context, payload any) (any, error) {
|
||||||
@@ -29,7 +30,7 @@ func (fde *FreeDEncode) Type() string {
|
|||||||
func init() {
|
func init() {
|
||||||
RegisterProcessor(ProcessorRegistration{
|
RegisterProcessor(ProcessorRegistration{
|
||||||
Type: "freed.encode",
|
Type: "freed.encode",
|
||||||
New: func(config ProcessorConfig) (Processor, error) {
|
New: func(config config.ProcessorConfig) (Processor, error) {
|
||||||
return &FreeDEncode{config: config}, nil
|
return &FreeDEncode{config: config}, nil
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
package processing
|
package processor
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
@@ -6,10 +6,12 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
"text/template"
|
"text/template"
|
||||||
|
|
||||||
|
"github.com/jwetzell/showbridge-go/internal/config"
|
||||||
)
|
)
|
||||||
|
|
||||||
type HTTPRequestCreate struct {
|
type HTTPRequestCreate struct {
|
||||||
config ProcessorConfig
|
config config.ProcessorConfig
|
||||||
Method string
|
Method string
|
||||||
URL *template.Template
|
URL *template.Template
|
||||||
}
|
}
|
||||||
@@ -42,7 +44,7 @@ func (hre *HTTPRequestCreate) Type() string {
|
|||||||
func init() {
|
func init() {
|
||||||
RegisterProcessor(ProcessorRegistration{
|
RegisterProcessor(ProcessorRegistration{
|
||||||
Type: "http.request.create",
|
Type: "http.request.create",
|
||||||
New: func(config ProcessorConfig) (Processor, error) {
|
New: func(config config.ProcessorConfig) (Processor, error) {
|
||||||
params := config.Params
|
params := config.Params
|
||||||
|
|
||||||
method, ok := params["method"]
|
method, ok := params["method"]
|
||||||
@@ -1,14 +1,16 @@
|
|||||||
package processing
|
package processor
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
|
"github.com/jwetzell/showbridge-go/internal/config"
|
||||||
)
|
)
|
||||||
|
|
||||||
type HTTPRequestEncode struct {
|
type HTTPRequestEncode struct {
|
||||||
config ProcessorConfig
|
config config.ProcessorConfig
|
||||||
}
|
}
|
||||||
|
|
||||||
func (hre *HTTPRequestEncode) Process(ctx context.Context, payload any) (any, error) {
|
func (hre *HTTPRequestEncode) Process(ctx context.Context, payload any) (any, error) {
|
||||||
@@ -33,7 +35,7 @@ func (hre *HTTPRequestEncode) Type() string {
|
|||||||
func init() {
|
func init() {
|
||||||
RegisterProcessor(ProcessorRegistration{
|
RegisterProcessor(ProcessorRegistration{
|
||||||
Type: "http.request.encode",
|
Type: "http.request.encode",
|
||||||
New: func(config ProcessorConfig) (Processor, error) {
|
New: func(config config.ProcessorConfig) (Processor, error) {
|
||||||
return &HTTPRequestEncode{config: config}, nil
|
return &HTTPRequestEncode{config: config}, nil
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
@@ -1,14 +1,16 @@
|
|||||||
package processing
|
package processor
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
"regexp"
|
"regexp"
|
||||||
|
|
||||||
|
"github.com/jwetzell/showbridge-go/internal/config"
|
||||||
)
|
)
|
||||||
|
|
||||||
type HTTPRequestFilter struct {
|
type HTTPRequestFilter struct {
|
||||||
config ProcessorConfig
|
config config.ProcessorConfig
|
||||||
Path *regexp.Regexp
|
Path *regexp.Regexp
|
||||||
Method string
|
Method string
|
||||||
}
|
}
|
||||||
@@ -41,7 +43,7 @@ func (hrf *HTTPRequestFilter) Type() string {
|
|||||||
func init() {
|
func init() {
|
||||||
RegisterProcessor(ProcessorRegistration{
|
RegisterProcessor(ProcessorRegistration{
|
||||||
Type: "http.request.filter",
|
Type: "http.request.filter",
|
||||||
New: func(config ProcessorConfig) (Processor, error) {
|
New: func(config config.ProcessorConfig) (Processor, error) {
|
||||||
params := config.Params
|
params := config.Params
|
||||||
path, ok := params["path"]
|
path, ok := params["path"]
|
||||||
|
|
||||||
@@ -1,14 +1,16 @@
|
|||||||
package processing
|
package processor
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
|
"github.com/jwetzell/showbridge-go/internal/config"
|
||||||
)
|
)
|
||||||
|
|
||||||
type HTTPResponseEncode struct {
|
type HTTPResponseEncode struct {
|
||||||
config ProcessorConfig
|
config config.ProcessorConfig
|
||||||
}
|
}
|
||||||
|
|
||||||
func (hre *HTTPResponseEncode) Process(ctx context.Context, payload any) (any, error) {
|
func (hre *HTTPResponseEncode) Process(ctx context.Context, payload any) (any, error) {
|
||||||
@@ -34,7 +36,7 @@ func (hre *HTTPResponseEncode) Type() string {
|
|||||||
func init() {
|
func init() {
|
||||||
RegisterProcessor(ProcessorRegistration{
|
RegisterProcessor(ProcessorRegistration{
|
||||||
Type: "http.response.encode",
|
Type: "http.response.encode",
|
||||||
New: func(config ProcessorConfig) (Processor, error) {
|
New: func(config config.ProcessorConfig) (Processor, error) {
|
||||||
return &HTTPResponseEncode{config: config}, nil
|
return &HTTPResponseEncode{config: config}, nil
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
@@ -1,13 +1,15 @@
|
|||||||
package processing
|
package processor
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
|
||||||
|
"github.com/jwetzell/showbridge-go/internal/config"
|
||||||
)
|
)
|
||||||
|
|
||||||
type IntParse struct {
|
type IntParse struct {
|
||||||
config ProcessorConfig
|
config config.ProcessorConfig
|
||||||
}
|
}
|
||||||
|
|
||||||
func (ip *IntParse) Process(ctx context.Context, payload any) (any, error) {
|
func (ip *IntParse) Process(ctx context.Context, payload any) (any, error) {
|
||||||
@@ -32,7 +34,7 @@ func (ip *IntParse) Type() string {
|
|||||||
func init() {
|
func init() {
|
||||||
RegisterProcessor(ProcessorRegistration{
|
RegisterProcessor(ProcessorRegistration{
|
||||||
Type: "int.parse",
|
Type: "int.parse",
|
||||||
New: func(config ProcessorConfig) (Processor, error) {
|
New: func(config config.ProcessorConfig) (Processor, error) {
|
||||||
return &IntParse{config: config}, nil
|
return &IntParse{config: config}, nil
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
85
internal/processor/int-parse_test.go
Normal file
85
internal/processor/int-parse_test.go
Normal file
@@ -0,0 +1,85 @@
|
|||||||
|
package processor_test
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/jwetzell/showbridge-go/internal/processor"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestGoodIntParse(t *testing.T) {
|
||||||
|
intParser := processor.IntParse{}
|
||||||
|
tests := []struct {
|
||||||
|
processor processor.Processor
|
||||||
|
name string
|
||||||
|
payload any
|
||||||
|
expected int64
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
name: "positive number",
|
||||||
|
payload: "12345",
|
||||||
|
expected: 12345,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "negative number",
|
||||||
|
payload: "-12345",
|
||||||
|
expected: -12345,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "zero",
|
||||||
|
payload: "0",
|
||||||
|
expected: 0,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, test := range tests {
|
||||||
|
t.Run(test.name, func(t *testing.T) {
|
||||||
|
got, err := intParser.Process(t.Context(), test.payload)
|
||||||
|
|
||||||
|
gotInt, ok := got.(int64)
|
||||||
|
if !ok {
|
||||||
|
t.Errorf("int.parse returned a %T payload: %s", got, got)
|
||||||
|
}
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("int.parse failed: %s", err)
|
||||||
|
}
|
||||||
|
if gotInt != test.expected {
|
||||||
|
t.Errorf("int.parse got %d, expected %d", gotInt, test.expected)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestBadIntParse(t *testing.T) {
|
||||||
|
intParser := processor.IntParse{}
|
||||||
|
tests := []struct {
|
||||||
|
processor processor.Processor
|
||||||
|
name string
|
||||||
|
payload any
|
||||||
|
errorString string
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
name: "non-string input",
|
||||||
|
payload: []byte{0x01},
|
||||||
|
errorString: "int.parse processor only accepts a string",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "not int string",
|
||||||
|
payload: "123.46",
|
||||||
|
errorString: "strconv.ParseInt: parsing \"123.46\": invalid syntax",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, test := range tests {
|
||||||
|
t.Run(test.name, func(t *testing.T) {
|
||||||
|
got, err := intParser.Process(t.Context(), test.payload)
|
||||||
|
|
||||||
|
if err == nil {
|
||||||
|
t.Errorf("int.parse expected to fail but succeeded, got: %v", got)
|
||||||
|
|
||||||
|
}
|
||||||
|
if err.Error() != test.errorString {
|
||||||
|
t.Errorf("int.parse got error '%s', expected '%s'", err.Error(), test.errorString)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,16 +1,17 @@
|
|||||||
//go:build cgo
|
//go:build cgo
|
||||||
|
|
||||||
package processing
|
package processor
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
|
"github.com/jwetzell/showbridge-go/internal/config"
|
||||||
"gitlab.com/gomidi/midi/v2"
|
"gitlab.com/gomidi/midi/v2"
|
||||||
)
|
)
|
||||||
|
|
||||||
type MIDIMessageDecode struct {
|
type MIDIMessageDecode struct {
|
||||||
config ProcessorConfig
|
config config.ProcessorConfig
|
||||||
}
|
}
|
||||||
|
|
||||||
func (mmd *MIDIMessageDecode) Process(ctx context.Context, payload any) (any, error) {
|
func (mmd *MIDIMessageDecode) Process(ctx context.Context, payload any) (any, error) {
|
||||||
@@ -32,7 +33,7 @@ func (mmd *MIDIMessageDecode) Type() string {
|
|||||||
func init() {
|
func init() {
|
||||||
RegisterProcessor(ProcessorRegistration{
|
RegisterProcessor(ProcessorRegistration{
|
||||||
Type: "midi.message.decode",
|
Type: "midi.message.decode",
|
||||||
New: func(config ProcessorConfig) (Processor, error) {
|
New: func(config config.ProcessorConfig) (Processor, error) {
|
||||||
return &MIDIMessageDecode{config: config}, nil
|
return &MIDIMessageDecode{config: config}, nil
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
@@ -1,16 +1,17 @@
|
|||||||
//go:build cgo
|
//go:build cgo
|
||||||
|
|
||||||
package processing
|
package processor
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
|
"github.com/jwetzell/showbridge-go/internal/config"
|
||||||
"gitlab.com/gomidi/midi/v2"
|
"gitlab.com/gomidi/midi/v2"
|
||||||
)
|
)
|
||||||
|
|
||||||
type MIDIMessageEncode struct {
|
type MIDIMessageEncode struct {
|
||||||
config ProcessorConfig
|
config config.ProcessorConfig
|
||||||
}
|
}
|
||||||
|
|
||||||
func (mme *MIDIMessageEncode) Process(ctx context.Context, payload any) (any, error) {
|
func (mme *MIDIMessageEncode) Process(ctx context.Context, payload any) (any, error) {
|
||||||
@@ -30,7 +31,7 @@ func (mme *MIDIMessageEncode) Type() string {
|
|||||||
func init() {
|
func init() {
|
||||||
RegisterProcessor(ProcessorRegistration{
|
RegisterProcessor(ProcessorRegistration{
|
||||||
Type: "midi.message.encode",
|
Type: "midi.message.encode",
|
||||||
New: func(config ProcessorConfig) (Processor, error) {
|
New: func(config config.ProcessorConfig) (Processor, error) {
|
||||||
return &MIDIMessageEncode{config: config}, nil
|
return &MIDIMessageEncode{config: config}, nil
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
@@ -1,8 +1,10 @@
|
|||||||
package processing
|
package processor
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
|
"github.com/jwetzell/showbridge-go/internal/config"
|
||||||
)
|
)
|
||||||
|
|
||||||
type MQTTMessage struct {
|
type MQTTMessage struct {
|
||||||
@@ -13,7 +15,7 @@ type MQTTMessage struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type MQTTMessageCreate struct {
|
type MQTTMessageCreate struct {
|
||||||
config ProcessorConfig
|
config config.ProcessorConfig
|
||||||
Topic string
|
Topic string
|
||||||
QoS byte
|
QoS byte
|
||||||
Retained bool
|
Retained bool
|
||||||
@@ -39,7 +41,7 @@ func (mmc *MQTTMessageCreate) Type() string {
|
|||||||
func init() {
|
func init() {
|
||||||
RegisterProcessor(ProcessorRegistration{
|
RegisterProcessor(ProcessorRegistration{
|
||||||
Type: "mqtt.message.create",
|
Type: "mqtt.message.create",
|
||||||
New: func(config ProcessorConfig) (Processor, error) {
|
New: func(config config.ProcessorConfig) (Processor, error) {
|
||||||
params := config.Params
|
params := config.Params
|
||||||
topic, ok := params["topic"]
|
topic, ok := params["topic"]
|
||||||
|
|
||||||
@@ -1,14 +1,15 @@
|
|||||||
package processing
|
package processor
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
mqtt "github.com/eclipse/paho.mqtt.golang"
|
mqtt "github.com/eclipse/paho.mqtt.golang"
|
||||||
|
"github.com/jwetzell/showbridge-go/internal/config"
|
||||||
)
|
)
|
||||||
|
|
||||||
type MQTTMessageEncode struct {
|
type MQTTMessageEncode struct {
|
||||||
config ProcessorConfig
|
config config.ProcessorConfig
|
||||||
}
|
}
|
||||||
|
|
||||||
func (mme *MQTTMessageEncode) Process(ctx context.Context, payload any) (any, error) {
|
func (mme *MQTTMessageEncode) Process(ctx context.Context, payload any) (any, error) {
|
||||||
@@ -28,7 +29,7 @@ func (mme *MQTTMessageEncode) Type() string {
|
|||||||
func init() {
|
func init() {
|
||||||
RegisterProcessor(ProcessorRegistration{
|
RegisterProcessor(ProcessorRegistration{
|
||||||
Type: "mqtt.message.encode",
|
Type: "mqtt.message.encode",
|
||||||
New: func(config ProcessorConfig) (Processor, error) {
|
New: func(config config.ProcessorConfig) (Processor, error) {
|
||||||
return &MQTTMessageEncode{config: config}, nil
|
return &MQTTMessageEncode{config: config}, nil
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
@@ -1,10 +1,12 @@
|
|||||||
package processing
|
package processor
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
"text/template"
|
"text/template"
|
||||||
|
|
||||||
|
"github.com/jwetzell/showbridge-go/internal/config"
|
||||||
)
|
)
|
||||||
|
|
||||||
type NATSMessage struct {
|
type NATSMessage struct {
|
||||||
@@ -13,7 +15,7 @@ type NATSMessage struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type NATSMessageCreate struct {
|
type NATSMessageCreate struct {
|
||||||
config ProcessorConfig
|
config config.ProcessorConfig
|
||||||
Subject string
|
Subject string
|
||||||
Payload *template.Template
|
Payload *template.Template
|
||||||
}
|
}
|
||||||
@@ -44,7 +46,7 @@ func (nmc *NATSMessageCreate) Type() string {
|
|||||||
func init() {
|
func init() {
|
||||||
RegisterProcessor(ProcessorRegistration{
|
RegisterProcessor(ProcessorRegistration{
|
||||||
Type: "nats.message.create",
|
Type: "nats.message.create",
|
||||||
New: func(config ProcessorConfig) (Processor, error) {
|
New: func(config config.ProcessorConfig) (Processor, error) {
|
||||||
params := config.Params
|
params := config.Params
|
||||||
// TODO(jwetzell): support template for subject
|
// TODO(jwetzell): support template for subject
|
||||||
subject, ok := params["subject"]
|
subject, ok := params["subject"]
|
||||||
@@ -1,14 +1,15 @@
|
|||||||
package processing
|
package processor
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
|
"github.com/jwetzell/showbridge-go/internal/config"
|
||||||
"github.com/nats-io/nats.go"
|
"github.com/nats-io/nats.go"
|
||||||
)
|
)
|
||||||
|
|
||||||
type NATSMessageEncode struct {
|
type NATSMessageEncode struct {
|
||||||
config ProcessorConfig
|
config config.ProcessorConfig
|
||||||
}
|
}
|
||||||
|
|
||||||
func (nme *NATSMessageEncode) Process(ctx context.Context, payload any) (any, error) {
|
func (nme *NATSMessageEncode) Process(ctx context.Context, payload any) (any, error) {
|
||||||
@@ -28,7 +29,7 @@ func (nme *NATSMessageEncode) Type() string {
|
|||||||
func init() {
|
func init() {
|
||||||
RegisterProcessor(ProcessorRegistration{
|
RegisterProcessor(ProcessorRegistration{
|
||||||
Type: "nats.message.encode",
|
Type: "nats.message.encode",
|
||||||
New: func(config ProcessorConfig) (Processor, error) {
|
New: func(config config.ProcessorConfig) (Processor, error) {
|
||||||
return &NATSMessageEncode{config: config}, nil
|
return &NATSMessageEncode{config: config}, nil
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
package processing
|
package processor
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
@@ -9,10 +9,11 @@ import (
|
|||||||
"text/template"
|
"text/template"
|
||||||
|
|
||||||
"github.com/jwetzell/osc-go"
|
"github.com/jwetzell/osc-go"
|
||||||
|
"github.com/jwetzell/showbridge-go/internal/config"
|
||||||
)
|
)
|
||||||
|
|
||||||
type OSCMessageCreate struct {
|
type OSCMessageCreate struct {
|
||||||
config ProcessorConfig
|
config config.ProcessorConfig
|
||||||
Address *template.Template
|
Address *template.Template
|
||||||
Args []*template.Template
|
Args []*template.Template
|
||||||
Types string
|
Types string
|
||||||
@@ -76,7 +77,7 @@ func (o *OSCMessageCreate) Type() string {
|
|||||||
func init() {
|
func init() {
|
||||||
RegisterProcessor(ProcessorRegistration{
|
RegisterProcessor(ProcessorRegistration{
|
||||||
Type: "osc.message.create",
|
Type: "osc.message.create",
|
||||||
New: func(config ProcessorConfig) (Processor, error) {
|
New: func(config config.ProcessorConfig) (Processor, error) {
|
||||||
params := config.Params
|
params := config.Params
|
||||||
address, ok := params["address"]
|
address, ok := params["address"]
|
||||||
|
|
||||||
@@ -1,14 +1,15 @@
|
|||||||
package processing
|
package processor
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
osc "github.com/jwetzell/osc-go"
|
osc "github.com/jwetzell/osc-go"
|
||||||
|
"github.com/jwetzell/showbridge-go/internal/config"
|
||||||
)
|
)
|
||||||
|
|
||||||
type OSCMessageDecode struct {
|
type OSCMessageDecode struct {
|
||||||
config ProcessorConfig
|
config config.ProcessorConfig
|
||||||
}
|
}
|
||||||
|
|
||||||
func (o *OSCMessageDecode) Process(ctx context.Context, payload any) (any, error) {
|
func (o *OSCMessageDecode) Process(ctx context.Context, payload any) (any, error) {
|
||||||
@@ -40,7 +41,7 @@ func (o *OSCMessageDecode) Type() string {
|
|||||||
func init() {
|
func init() {
|
||||||
RegisterProcessor(ProcessorRegistration{
|
RegisterProcessor(ProcessorRegistration{
|
||||||
Type: "osc.message.decode",
|
Type: "osc.message.decode",
|
||||||
New: func(config ProcessorConfig) (Processor, error) {
|
New: func(config config.ProcessorConfig) (Processor, error) {
|
||||||
return &OSCMessageDecode{config: config}, nil
|
return &OSCMessageDecode{config: config}, nil
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
@@ -1,14 +1,15 @@
|
|||||||
package processing
|
package processor
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
osc "github.com/jwetzell/osc-go"
|
osc "github.com/jwetzell/osc-go"
|
||||||
|
"github.com/jwetzell/showbridge-go/internal/config"
|
||||||
)
|
)
|
||||||
|
|
||||||
type OSCMessageEncode struct {
|
type OSCMessageEncode struct {
|
||||||
config ProcessorConfig
|
config config.ProcessorConfig
|
||||||
}
|
}
|
||||||
|
|
||||||
func (o *OSCMessageEncode) Process(ctx context.Context, payload any) (any, error) {
|
func (o *OSCMessageEncode) Process(ctx context.Context, payload any) (any, error) {
|
||||||
@@ -29,7 +30,7 @@ func (o *OSCMessageEncode) Type() string {
|
|||||||
func init() {
|
func init() {
|
||||||
RegisterProcessor(ProcessorRegistration{
|
RegisterProcessor(ProcessorRegistration{
|
||||||
Type: "osc.message.encode",
|
Type: "osc.message.encode",
|
||||||
New: func(config ProcessorConfig) (Processor, error) {
|
New: func(config config.ProcessorConfig) (Processor, error) {
|
||||||
return &OSCMessageEncode{config: config}, nil
|
return &OSCMessageEncode{config: config}, nil
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
package processing
|
package processor
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
@@ -7,10 +7,11 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/jwetzell/osc-go"
|
"github.com/jwetzell/osc-go"
|
||||||
|
"github.com/jwetzell/showbridge-go/internal/config"
|
||||||
)
|
)
|
||||||
|
|
||||||
type OSCMessageFilter struct {
|
type OSCMessageFilter struct {
|
||||||
config ProcessorConfig
|
config config.ProcessorConfig
|
||||||
Address *regexp.Regexp
|
Address *regexp.Regexp
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -36,7 +37,7 @@ func (o *OSCMessageFilter) Type() string {
|
|||||||
func init() {
|
func init() {
|
||||||
RegisterProcessor(ProcessorRegistration{
|
RegisterProcessor(ProcessorRegistration{
|
||||||
Type: "osc.message.filter",
|
Type: "osc.message.filter",
|
||||||
New: func(config ProcessorConfig) (Processor, error) {
|
New: func(config config.ProcessorConfig) (Processor, error) {
|
||||||
params := config.Params
|
params := config.Params
|
||||||
address, ok := params["address"]
|
address, ok := params["address"]
|
||||||
|
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
package processing
|
package processor
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
@@ -7,10 +7,11 @@ import (
|
|||||||
"text/template"
|
"text/template"
|
||||||
|
|
||||||
osc "github.com/jwetzell/osc-go"
|
osc "github.com/jwetzell/osc-go"
|
||||||
|
"github.com/jwetzell/showbridge-go/internal/config"
|
||||||
)
|
)
|
||||||
|
|
||||||
type OSCMessageTransform struct {
|
type OSCMessageTransform struct {
|
||||||
config ProcessorConfig
|
config config.ProcessorConfig
|
||||||
Address *template.Template
|
Address *template.Template
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -51,7 +52,7 @@ func (o *OSCMessageTransform) Type() string {
|
|||||||
func init() {
|
func init() {
|
||||||
RegisterProcessor(ProcessorRegistration{
|
RegisterProcessor(ProcessorRegistration{
|
||||||
Type: "osc.message.transform",
|
Type: "osc.message.transform",
|
||||||
New: func(config ProcessorConfig) (Processor, error) {
|
New: func(config config.ProcessorConfig) (Processor, error) {
|
||||||
params := config.Params
|
params := config.Params
|
||||||
address, ok := params["address"]
|
address, ok := params["address"]
|
||||||
|
|
||||||
@@ -1,9 +1,11 @@
|
|||||||
package processing
|
package processor
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
"sync"
|
"sync"
|
||||||
|
|
||||||
|
"github.com/jwetzell/showbridge-go/internal/config"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Processor interface {
|
type Processor interface {
|
||||||
@@ -11,14 +13,9 @@ type Processor interface {
|
|||||||
Process(context.Context, any) (any, error)
|
Process(context.Context, any) (any, error)
|
||||||
}
|
}
|
||||||
|
|
||||||
type ProcessorConfig struct {
|
|
||||||
Type string `json:"type"`
|
|
||||||
Params map[string]any `json:"params"`
|
|
||||||
}
|
|
||||||
|
|
||||||
type ProcessorRegistration struct {
|
type ProcessorRegistration struct {
|
||||||
Type string `json:"type"`
|
Type string `json:"type"`
|
||||||
New func(ProcessorConfig) (Processor, error)
|
New func(config.ProcessorConfig) (Processor, error)
|
||||||
}
|
}
|
||||||
|
|
||||||
func RegisterProcessor(processor ProcessorRegistration) {
|
func RegisterProcessor(processor ProcessorRegistration) {
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
package processing
|
package processor
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
@@ -6,11 +6,12 @@ import (
|
|||||||
|
|
||||||
"github.com/expr-lang/expr"
|
"github.com/expr-lang/expr"
|
||||||
"github.com/expr-lang/expr/vm"
|
"github.com/expr-lang/expr/vm"
|
||||||
|
"github.com/jwetzell/showbridge-go/internal/config"
|
||||||
)
|
)
|
||||||
|
|
||||||
// NOTE(jwetzell): see language definition https://expr-lang.org/docs/language-definition
|
// NOTE(jwetzell): see language definition https://expr-lang.org/docs/language-definition
|
||||||
type ScriptExpr struct {
|
type ScriptExpr struct {
|
||||||
config ProcessorConfig
|
config config.ProcessorConfig
|
||||||
Program *vm.Program
|
Program *vm.Program
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -31,7 +32,7 @@ func (se *ScriptExpr) Type() string {
|
|||||||
func init() {
|
func init() {
|
||||||
RegisterProcessor(ProcessorRegistration{
|
RegisterProcessor(ProcessorRegistration{
|
||||||
Type: "script.expr",
|
Type: "script.expr",
|
||||||
New: func(config ProcessorConfig) (Processor, error) {
|
New: func(config config.ProcessorConfig) (Processor, error) {
|
||||||
params := config.Params
|
params := config.Params
|
||||||
|
|
||||||
expression, ok := params["expression"]
|
expression, ok := params["expression"]
|
||||||
@@ -1,15 +1,16 @@
|
|||||||
package processing
|
package processor
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
|
"github.com/jwetzell/showbridge-go/internal/config"
|
||||||
"modernc.org/quickjs"
|
"modernc.org/quickjs"
|
||||||
)
|
)
|
||||||
|
|
||||||
type ScriptJS struct {
|
type ScriptJS struct {
|
||||||
config ProcessorConfig
|
config config.ProcessorConfig
|
||||||
Program string
|
Program string
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -67,7 +68,7 @@ func (sj *ScriptJS) Type() string {
|
|||||||
func init() {
|
func init() {
|
||||||
RegisterProcessor(ProcessorRegistration{
|
RegisterProcessor(ProcessorRegistration{
|
||||||
Type: "script.js",
|
Type: "script.js",
|
||||||
New: func(config ProcessorConfig) (Processor, error) {
|
New: func(config config.ProcessorConfig) (Processor, error) {
|
||||||
params := config.Params
|
params := config.Params
|
||||||
|
|
||||||
program, ok := params["program"]
|
program, ok := params["program"]
|
||||||
@@ -1,14 +1,16 @@
|
|||||||
package processing
|
package processor
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
"text/template"
|
"text/template"
|
||||||
|
|
||||||
|
"github.com/jwetzell/showbridge-go/internal/config"
|
||||||
)
|
)
|
||||||
|
|
||||||
type StringCreate struct {
|
type StringCreate struct {
|
||||||
config ProcessorConfig
|
config config.ProcessorConfig
|
||||||
Template *template.Template
|
Template *template.Template
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -32,7 +34,7 @@ func (sc *StringCreate) Type() string {
|
|||||||
func init() {
|
func init() {
|
||||||
RegisterProcessor(ProcessorRegistration{
|
RegisterProcessor(ProcessorRegistration{
|
||||||
Type: "string.create",
|
Type: "string.create",
|
||||||
New: func(config ProcessorConfig) (Processor, error) {
|
New: func(config config.ProcessorConfig) (Processor, error) {
|
||||||
params := config.Params
|
params := config.Params
|
||||||
tmpl, ok := params["template"]
|
tmpl, ok := params["template"]
|
||||||
|
|
||||||
@@ -1,12 +1,14 @@
|
|||||||
package processing
|
package processor
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
|
"github.com/jwetzell/showbridge-go/internal/config"
|
||||||
)
|
)
|
||||||
|
|
||||||
type StringDecode struct {
|
type StringDecode struct {
|
||||||
config ProcessorConfig
|
config config.ProcessorConfig
|
||||||
}
|
}
|
||||||
|
|
||||||
func (sd *StringDecode) Process(ctx context.Context, payload any) (any, error) {
|
func (sd *StringDecode) Process(ctx context.Context, payload any) (any, error) {
|
||||||
@@ -28,7 +30,7 @@ func (sd *StringDecode) Type() string {
|
|||||||
func init() {
|
func init() {
|
||||||
RegisterProcessor(ProcessorRegistration{
|
RegisterProcessor(ProcessorRegistration{
|
||||||
Type: "string.decode",
|
Type: "string.decode",
|
||||||
New: func(config ProcessorConfig) (Processor, error) {
|
New: func(config config.ProcessorConfig) (Processor, error) {
|
||||||
return &StringDecode{config: config}, nil
|
return &StringDecode{config: config}, nil
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
71
internal/processor/string-decode_test.go
Normal file
71
internal/processor/string-decode_test.go
Normal file
@@ -0,0 +1,71 @@
|
|||||||
|
package processor_test
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/jwetzell/showbridge-go/internal/processor"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestGoodStringDecode(t *testing.T) {
|
||||||
|
stringDecoder := processor.StringDecode{}
|
||||||
|
tests := []struct {
|
||||||
|
processor processor.Processor
|
||||||
|
name string
|
||||||
|
payload any
|
||||||
|
expected string
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
processor: &stringDecoder,
|
||||||
|
name: "hello",
|
||||||
|
payload: []byte{0x68, 0x65, 0x6c, 0x6c, 0x6f},
|
||||||
|
expected: "hello",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, test := range tests {
|
||||||
|
t.Run(test.name, func(t *testing.T) {
|
||||||
|
got, err := test.processor.Process(t.Context(), test.payload)
|
||||||
|
|
||||||
|
gotString, ok := got.(string)
|
||||||
|
if !ok {
|
||||||
|
t.Errorf("string.decode returned a %T payload: %s", got, got)
|
||||||
|
}
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("string.decode failed: %s", err)
|
||||||
|
}
|
||||||
|
if gotString != test.expected {
|
||||||
|
t.Errorf("string.decode got %s, expected %s", got, test.expected)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestBadStringDecode(t *testing.T) {
|
||||||
|
stringDecoder := processor.StringDecode{}
|
||||||
|
tests := []struct {
|
||||||
|
processor processor.Processor
|
||||||
|
name string
|
||||||
|
payload any
|
||||||
|
errorString string
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
processor: &stringDecoder,
|
||||||
|
name: "non-[]byte input",
|
||||||
|
payload: "hello",
|
||||||
|
errorString: "string.decode processor only accepts a []byte",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, test := range tests {
|
||||||
|
t.Run(test.name, func(t *testing.T) {
|
||||||
|
got, err := test.processor.Process(t.Context(), test.payload)
|
||||||
|
|
||||||
|
if err == nil {
|
||||||
|
t.Errorf("string.decode expected to fail but got payload: %s", got)
|
||||||
|
}
|
||||||
|
if err.Error() != test.errorString {
|
||||||
|
t.Errorf("string.decode got error '%s', expected '%s'", err.Error(), test.errorString)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,12 +1,14 @@
|
|||||||
package processing
|
package processor
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
|
"github.com/jwetzell/showbridge-go/internal/config"
|
||||||
)
|
)
|
||||||
|
|
||||||
type StringEncode struct {
|
type StringEncode struct {
|
||||||
config ProcessorConfig
|
config config.ProcessorConfig
|
||||||
}
|
}
|
||||||
|
|
||||||
func (se *StringEncode) Process(ctx context.Context, payload any) (any, error) {
|
func (se *StringEncode) Process(ctx context.Context, payload any) (any, error) {
|
||||||
@@ -28,7 +30,7 @@ func (se *StringEncode) Type() string {
|
|||||||
func init() {
|
func init() {
|
||||||
RegisterProcessor(ProcessorRegistration{
|
RegisterProcessor(ProcessorRegistration{
|
||||||
Type: "string.encode",
|
Type: "string.encode",
|
||||||
New: func(config ProcessorConfig) (Processor, error) {
|
New: func(config config.ProcessorConfig) (Processor, error) {
|
||||||
return &StringEncode{config: config}, nil
|
return &StringEncode{config: config}, nil
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
72
internal/processor/string-encode_test.go
Normal file
72
internal/processor/string-encode_test.go
Normal file
@@ -0,0 +1,72 @@
|
|||||||
|
package processor_test
|
||||||
|
|
||||||
|
import (
|
||||||
|
"slices"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/jwetzell/showbridge-go/internal/processor"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestGoodStringEncode(t *testing.T) {
|
||||||
|
stringEncoder := processor.StringEncode{}
|
||||||
|
tests := []struct {
|
||||||
|
processor processor.Processor
|
||||||
|
name string
|
||||||
|
payload any
|
||||||
|
expected []byte
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
processor: &stringEncoder,
|
||||||
|
name: "hello",
|
||||||
|
payload: "hello",
|
||||||
|
expected: []byte{0x68, 0x65, 0x6c, 0x6c, 0x6f},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, test := range tests {
|
||||||
|
t.Run(test.name, func(t *testing.T) {
|
||||||
|
got, err := test.processor.Process(t.Context(), test.payload)
|
||||||
|
|
||||||
|
gotBytes, ok := got.([]byte)
|
||||||
|
if !ok {
|
||||||
|
t.Errorf("string.encode returned a %T payload: %s", got, got)
|
||||||
|
}
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("string.encode failed: %s", err)
|
||||||
|
}
|
||||||
|
if !slices.Equal(gotBytes, test.expected) {
|
||||||
|
t.Errorf("string.encode got %s, expected %s", got, test.expected)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestBadStringEncode(t *testing.T) {
|
||||||
|
stringEncoder := processor.StringEncode{}
|
||||||
|
tests := []struct {
|
||||||
|
processor processor.Processor
|
||||||
|
name string
|
||||||
|
payload any
|
||||||
|
errorString string
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
processor: &stringEncoder,
|
||||||
|
name: "non-string input",
|
||||||
|
payload: []byte{0x68, 0x65, 0x6c, 0x6c, 0x6f},
|
||||||
|
errorString: "string.encode processor only accepts a string",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, test := range tests {
|
||||||
|
t.Run(test.name, func(t *testing.T) {
|
||||||
|
got, err := test.processor.Process(t.Context(), test.payload)
|
||||||
|
|
||||||
|
if err == nil {
|
||||||
|
t.Errorf("string.encode expected to fail but got payload: %s", got)
|
||||||
|
}
|
||||||
|
if err.Error() != test.errorString {
|
||||||
|
t.Errorf("string.encode got error '%s', expected '%s'", err.Error(), test.errorString)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,13 +1,15 @@
|
|||||||
package processing
|
package processor
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
"regexp"
|
"regexp"
|
||||||
|
|
||||||
|
"github.com/jwetzell/showbridge-go/internal/config"
|
||||||
)
|
)
|
||||||
|
|
||||||
type StringFilter struct {
|
type StringFilter struct {
|
||||||
config ProcessorConfig
|
config config.ProcessorConfig
|
||||||
Pattern *regexp.Regexp
|
Pattern *regexp.Regexp
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -32,7 +34,7 @@ func (se *StringFilter) Type() string {
|
|||||||
func init() {
|
func init() {
|
||||||
RegisterProcessor(ProcessorRegistration{
|
RegisterProcessor(ProcessorRegistration{
|
||||||
Type: "string.filter",
|
Type: "string.filter",
|
||||||
New: func(config ProcessorConfig) (Processor, error) {
|
New: func(config config.ProcessorConfig) (Processor, error) {
|
||||||
params := config.Params
|
params := config.Params
|
||||||
|
|
||||||
pattern, ok := params["pattern"]
|
pattern, ok := params["pattern"]
|
||||||
@@ -1,13 +1,15 @@
|
|||||||
package processing
|
package processor
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
"github.com/jwetzell/showbridge-go/internal/config"
|
||||||
)
|
)
|
||||||
|
|
||||||
type StringSplit struct {
|
type StringSplit struct {
|
||||||
config ProcessorConfig
|
config config.ProcessorConfig
|
||||||
Separator string
|
Separator string
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -30,7 +32,7 @@ func (se *StringSplit) Type() string {
|
|||||||
func init() {
|
func init() {
|
||||||
RegisterProcessor(ProcessorRegistration{
|
RegisterProcessor(ProcessorRegistration{
|
||||||
Type: "string.split",
|
Type: "string.split",
|
||||||
New: func(config ProcessorConfig) (Processor, error) {
|
New: func(config config.ProcessorConfig) (Processor, error) {
|
||||||
params := config.Params
|
params := config.Params
|
||||||
|
|
||||||
separator, ok := params["separator"]
|
separator, ok := params["separator"]
|
||||||
70
internal/processor/string-split_test.go
Normal file
70
internal/processor/string-split_test.go
Normal file
@@ -0,0 +1,70 @@
|
|||||||
|
package processor_test
|
||||||
|
|
||||||
|
import (
|
||||||
|
"slices"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/jwetzell/showbridge-go/internal/processor"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestGoodStringSplit(t *testing.T) {
|
||||||
|
tests := []struct {
|
||||||
|
processor processor.Processor
|
||||||
|
name string
|
||||||
|
payload any
|
||||||
|
expected []string
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
processor: &processor.StringSplit{Separator: ","},
|
||||||
|
name: "comma separated",
|
||||||
|
payload: "part1,part2,part3",
|
||||||
|
expected: []string{"part1", "part2", "part3"},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, test := range tests {
|
||||||
|
t.Run(test.name, func(t *testing.T) {
|
||||||
|
got, err := test.processor.Process(t.Context(), test.payload)
|
||||||
|
|
||||||
|
gotStrings, ok := got.([]string)
|
||||||
|
if !ok {
|
||||||
|
t.Errorf("string.split returned a %T payload: %s", got, got)
|
||||||
|
}
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("string.split failed: %s", err)
|
||||||
|
}
|
||||||
|
if !slices.Equal(gotStrings, test.expected) {
|
||||||
|
t.Errorf("string.split got %s, expected %s", got, test.expected)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestBasStringSplit(t *testing.T) {
|
||||||
|
tests := []struct {
|
||||||
|
processor processor.Processor
|
||||||
|
name string
|
||||||
|
payload any
|
||||||
|
errorString string
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
processor: &processor.StringSplit{Separator: ","},
|
||||||
|
name: "hello",
|
||||||
|
payload: []byte{0x68, 0x65, 0x6c, 0x6c, 0x6f},
|
||||||
|
errorString: "string.split only accepts a string",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, test := range tests {
|
||||||
|
t.Run(test.name, func(t *testing.T) {
|
||||||
|
got, err := test.processor.Process(t.Context(), test.payload)
|
||||||
|
|
||||||
|
if err == nil {
|
||||||
|
t.Errorf("string.split expected error but got none, payload: %s", got)
|
||||||
|
}
|
||||||
|
if err.Error() != test.errorString {
|
||||||
|
t.Errorf("string.split got error '%s', expected '%s'", err.Error(), test.errorString)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,13 +1,15 @@
|
|||||||
package processing
|
package processor
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
|
||||||
|
"github.com/jwetzell/showbridge-go/internal/config"
|
||||||
)
|
)
|
||||||
|
|
||||||
type UintParse struct {
|
type UintParse struct {
|
||||||
config ProcessorConfig
|
config config.ProcessorConfig
|
||||||
}
|
}
|
||||||
|
|
||||||
func (up *UintParse) Process(ctx context.Context, payload any) (any, error) {
|
func (up *UintParse) Process(ctx context.Context, payload any) (any, error) {
|
||||||
@@ -32,7 +34,7 @@ func (up *UintParse) Type() string {
|
|||||||
func init() {
|
func init() {
|
||||||
RegisterProcessor(ProcessorRegistration{
|
RegisterProcessor(ProcessorRegistration{
|
||||||
Type: "uint.parse",
|
Type: "uint.parse",
|
||||||
New: func(config ProcessorConfig) (Processor, error) {
|
New: func(config config.ProcessorConfig) (Processor, error) {
|
||||||
return &UintParse{config: config}, nil
|
return &UintParse{config: config}, nil
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
80
internal/processor/uint-parse_test.go
Normal file
80
internal/processor/uint-parse_test.go
Normal file
@@ -0,0 +1,80 @@
|
|||||||
|
package processor_test
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/jwetzell/showbridge-go/internal/processor"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestGoodUintParse(t *testing.T) {
|
||||||
|
uintParser := processor.UintParse{}
|
||||||
|
tests := []struct {
|
||||||
|
processor processor.Processor
|
||||||
|
name string
|
||||||
|
payload any
|
||||||
|
expected uint64
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
name: "positive number",
|
||||||
|
payload: "12345",
|
||||||
|
expected: 12345,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "zero",
|
||||||
|
payload: "0",
|
||||||
|
expected: 0,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, test := range tests {
|
||||||
|
t.Run(test.name, func(t *testing.T) {
|
||||||
|
got, err := uintParser.Process(t.Context(), test.payload)
|
||||||
|
|
||||||
|
gotUint, ok := got.(uint64)
|
||||||
|
if !ok {
|
||||||
|
t.Errorf("uint.parse returned a %T payload: %s", got, got)
|
||||||
|
}
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("uint.parse failed: %s", err)
|
||||||
|
}
|
||||||
|
if gotUint != test.expected {
|
||||||
|
t.Errorf("uint.parse got %d, expected %d", gotUint, test.expected)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestBadUintParse(t *testing.T) {
|
||||||
|
uintParser := processor.UintParse{}
|
||||||
|
tests := []struct {
|
||||||
|
processor processor.Processor
|
||||||
|
name string
|
||||||
|
payload any
|
||||||
|
errorString string
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
name: "non-string input",
|
||||||
|
payload: []byte{0x01},
|
||||||
|
errorString: "uint.parse processor only accepts a string",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "not uint string",
|
||||||
|
payload: "-1234",
|
||||||
|
errorString: "strconv.ParseUint: parsing \"-1234\": invalid syntax",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, test := range tests {
|
||||||
|
t.Run(test.name, func(t *testing.T) {
|
||||||
|
got, err := uintParser.Process(t.Context(), test.payload)
|
||||||
|
|
||||||
|
if err == nil {
|
||||||
|
t.Errorf("uint.parse expected to fail but succeeded, got: %v", got)
|
||||||
|
|
||||||
|
}
|
||||||
|
if err.Error() != test.errorString {
|
||||||
|
t.Errorf("uint.parse got error '%s', expected '%s'", err.Error(), test.errorString)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
86
internal/route/route.go
Normal file
86
internal/route/route.go
Normal file
@@ -0,0 +1,86 @@
|
|||||||
|
package route
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
"github.com/jwetzell/showbridge-go/internal/config"
|
||||||
|
"github.com/jwetzell/showbridge-go/internal/processor"
|
||||||
|
)
|
||||||
|
|
||||||
|
type RouteError struct {
|
||||||
|
Index int
|
||||||
|
Config config.RouteConfig
|
||||||
|
Error error
|
||||||
|
}
|
||||||
|
|
||||||
|
type RouteIOError struct {
|
||||||
|
Index int
|
||||||
|
Error error
|
||||||
|
}
|
||||||
|
|
||||||
|
type RouteIO interface {
|
||||||
|
HandleInput(sourceId string, payload any) []RouteIOError
|
||||||
|
HandleOutput(sourceId string, destinationId string, payload any) error
|
||||||
|
}
|
||||||
|
|
||||||
|
type Route interface {
|
||||||
|
Input() string
|
||||||
|
Output() string
|
||||||
|
HandleInput(ctx context.Context, sourceId string, payload any, router RouteIO) error
|
||||||
|
HandleOutput(ctx context.Context, sourceId string, payload any, router RouteIO) error
|
||||||
|
}
|
||||||
|
|
||||||
|
type ProcessorRoute struct {
|
||||||
|
input string
|
||||||
|
processors []processor.Processor
|
||||||
|
output string
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewRoute(config config.RouteConfig) (Route, error) {
|
||||||
|
processors := []processor.Processor{}
|
||||||
|
|
||||||
|
if len(config.Processors) > 0 {
|
||||||
|
for _, processorDecl := range config.Processors {
|
||||||
|
processorInfo, ok := processor.ProcessorRegistry[processorDecl.Type]
|
||||||
|
if !ok {
|
||||||
|
return nil, fmt.Errorf("problem loading processor registration for processor type: %s", processorDecl.Type)
|
||||||
|
}
|
||||||
|
|
||||||
|
processor, err := processorInfo.New(processorDecl)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
processors = append(processors, processor)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return &ProcessorRoute{input: config.Input, processors: processors, output: config.Output}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *ProcessorRoute) Input() string {
|
||||||
|
return r.input
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *ProcessorRoute) Output() string {
|
||||||
|
return r.output
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *ProcessorRoute) HandleInput(ctx context.Context, sourceId string, payload any, router RouteIO) error {
|
||||||
|
var err error
|
||||||
|
for _, processor := range r.processors {
|
||||||
|
payload, err = processor.Process(ctx, payload)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
//NOTE(jwetzell) nil payload will result in the route being "terminated"
|
||||||
|
if payload == nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return r.HandleOutput(ctx, sourceId, payload, router)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *ProcessorRoute) HandleOutput(ctx context.Context, sourceId string, payload any, router RouteIO) error {
|
||||||
|
return router.HandleOutput(sourceId, r.output, payload)
|
||||||
|
}
|
||||||
67
route.go
67
route.go
@@ -1,67 +0,0 @@
|
|||||||
package showbridge
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
|
|
||||||
"github.com/jwetzell/showbridge-go/internal/processing"
|
|
||||||
)
|
|
||||||
|
|
||||||
type RouteError struct {
|
|
||||||
Index int
|
|
||||||
Config RouteConfig
|
|
||||||
Error error
|
|
||||||
}
|
|
||||||
|
|
||||||
type Route struct {
|
|
||||||
index int
|
|
||||||
Input string
|
|
||||||
Processors []processing.Processor
|
|
||||||
Output string
|
|
||||||
router *Router
|
|
||||||
}
|
|
||||||
|
|
||||||
type RouteConfig struct {
|
|
||||||
Input string `json:"input"`
|
|
||||||
Processors []processing.ProcessorConfig `json:"processors"`
|
|
||||||
Output string `json:"output"`
|
|
||||||
}
|
|
||||||
|
|
||||||
func NewRoute(index int, config RouteConfig, router *Router) (*Route, error) {
|
|
||||||
processors := []processing.Processor{}
|
|
||||||
|
|
||||||
if len(config.Processors) > 0 {
|
|
||||||
for _, processorDecl := range config.Processors {
|
|
||||||
processorInfo, ok := processing.ProcessorRegistry[processorDecl.Type]
|
|
||||||
if !ok {
|
|
||||||
return nil, fmt.Errorf("problem loading processor registration for processor type: %s", processorDecl.Type)
|
|
||||||
}
|
|
||||||
|
|
||||||
processor, err := processorInfo.New(processorDecl)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
processors = append(processors, processor)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return &Route{Input: config.Input, Processors: processors, Output: config.Output, router: router, index: index}, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (r *Route) HandleInput(sourceId string, payload any) error {
|
|
||||||
var err error
|
|
||||||
for _, processor := range r.Processors {
|
|
||||||
payload, err = processor.Process(r.router.Context, payload)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
//NOTE(jwetzell) nil payload will result in the route being "terminated"
|
|
||||||
if payload == nil {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return r.HandleOutput(sourceId, payload)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (r *Route) HandleOutput(sourceId string, payload any) error {
|
|
||||||
return r.router.HandleOutput(sourceId, r.Output, payload)
|
|
||||||
}
|
|
||||||
68
router.go
68
router.go
@@ -6,23 +6,21 @@ import (
|
|||||||
"log/slog"
|
"log/slog"
|
||||||
"os"
|
"os"
|
||||||
"sync"
|
"sync"
|
||||||
)
|
|
||||||
|
|
||||||
type RoutingError struct {
|
"github.com/jwetzell/showbridge-go/internal/config"
|
||||||
Index int
|
"github.com/jwetzell/showbridge-go/internal/module"
|
||||||
Error error
|
"github.com/jwetzell/showbridge-go/internal/route"
|
||||||
}
|
)
|
||||||
|
|
||||||
type Router struct {
|
type Router struct {
|
||||||
contextCancel context.CancelFunc
|
contextCancel context.CancelFunc
|
||||||
Context context.Context
|
Context context.Context
|
||||||
ModuleInstances []Module
|
ModuleInstances []module.Module
|
||||||
RouteInstances []*Route
|
RouteInstances []route.Route
|
||||||
moduleWait sync.WaitGroup
|
moduleWait sync.WaitGroup
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewRouter(ctx context.Context, config Config) (*Router, []ModuleError, []RouteError) {
|
func NewRouter(ctx context.Context, config config.Config) (*Router, []module.ModuleError, []route.RouteError) {
|
||||||
|
|
||||||
logger := slog.New(slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{
|
logger := slog.New(slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{
|
||||||
Level: slog.LevelInfo,
|
Level: slog.LevelInfo,
|
||||||
}))
|
}))
|
||||||
@@ -35,20 +33,20 @@ func NewRouter(ctx context.Context, config Config) (*Router, []ModuleError, []Ro
|
|||||||
router := Router{
|
router := Router{
|
||||||
Context: routerContext,
|
Context: routerContext,
|
||||||
contextCancel: cancel,
|
contextCancel: cancel,
|
||||||
ModuleInstances: []Module{},
|
ModuleInstances: []module.Module{},
|
||||||
RouteInstances: []*Route{},
|
RouteInstances: []route.Route{},
|
||||||
}
|
}
|
||||||
|
|
||||||
var moduleErrors []ModuleError
|
var moduleErrors []module.ModuleError
|
||||||
|
|
||||||
for moduleIndex, moduleDecl := range config.Modules {
|
for moduleIndex, moduleDecl := range config.Modules {
|
||||||
|
|
||||||
moduleInfo, ok := moduleRegistry[moduleDecl.Type]
|
moduleInfo, ok := module.ModuleRegistry[moduleDecl.Type]
|
||||||
if !ok {
|
if !ok {
|
||||||
if moduleErrors == nil {
|
if moduleErrors == nil {
|
||||||
moduleErrors = []ModuleError{}
|
moduleErrors = []module.ModuleError{}
|
||||||
}
|
}
|
||||||
moduleErrors = append(moduleErrors, ModuleError{
|
moduleErrors = append(moduleErrors, module.ModuleError{
|
||||||
Index: moduleIndex,
|
Index: moduleIndex,
|
||||||
Config: moduleDecl,
|
Config: moduleDecl,
|
||||||
Error: fmt.Errorf("module type not defined"),
|
Error: fmt.Errorf("module type not defined"),
|
||||||
@@ -61,9 +59,9 @@ func NewRouter(ctx context.Context, config Config) (*Router, []ModuleError, []Ro
|
|||||||
if moduleInstance.Id() == moduleDecl.Id {
|
if moduleInstance.Id() == moduleDecl.Id {
|
||||||
moduleInstanceExists = true
|
moduleInstanceExists = true
|
||||||
if moduleErrors == nil {
|
if moduleErrors == nil {
|
||||||
moduleErrors = []ModuleError{}
|
moduleErrors = []module.ModuleError{}
|
||||||
}
|
}
|
||||||
moduleErrors = append(moduleErrors, ModuleError{
|
moduleErrors = append(moduleErrors, module.ModuleError{
|
||||||
Index: moduleIndex,
|
Index: moduleIndex,
|
||||||
Config: moduleDecl,
|
Config: moduleDecl,
|
||||||
Error: fmt.Errorf("duplicate module id"),
|
Error: fmt.Errorf("duplicate module id"),
|
||||||
@@ -73,12 +71,12 @@ func NewRouter(ctx context.Context, config Config) (*Router, []ModuleError, []Ro
|
|||||||
}
|
}
|
||||||
|
|
||||||
if !moduleInstanceExists {
|
if !moduleInstanceExists {
|
||||||
moduleInstance, err := moduleInfo.New(moduleDecl)
|
moduleInstance, err := moduleInfo.New(router.Context, moduleDecl, &router)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if moduleErrors == nil {
|
if moduleErrors == nil {
|
||||||
moduleErrors = []ModuleError{}
|
moduleErrors = []module.ModuleError{}
|
||||||
}
|
}
|
||||||
moduleErrors = append(moduleErrors, ModuleError{
|
moduleErrors = append(moduleErrors, module.ModuleError{
|
||||||
Index: moduleIndex,
|
Index: moduleIndex,
|
||||||
Config: moduleDecl,
|
Config: moduleDecl,
|
||||||
Error: err,
|
Error: err,
|
||||||
@@ -91,26 +89,21 @@ func NewRouter(ctx context.Context, config Config) (*Router, []ModuleError, []Ro
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
var routeErrors []RouteError
|
var routeErrors []route.RouteError
|
||||||
for routeIndex, routeDecl := range config.Routes {
|
for routeIndex, routeDecl := range config.Routes {
|
||||||
route, err := NewRoute(routeIndex, routeDecl, &router)
|
routeInstance, err := route.NewRoute(routeDecl)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if routeErrors == nil {
|
if routeErrors == nil {
|
||||||
routeErrors = []RouteError{}
|
routeErrors = []route.RouteError{}
|
||||||
}
|
}
|
||||||
routeErrors = append(routeErrors, RouteError{
|
routeErrors = append(routeErrors, route.RouteError{
|
||||||
Index: routeIndex,
|
Index: routeIndex,
|
||||||
Config: routeDecl,
|
Config: routeDecl,
|
||||||
Error: err,
|
Error: err,
|
||||||
})
|
})
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
router.RouteInstances = append(router.RouteInstances, route)
|
router.RouteInstances = append(router.RouteInstances, routeInstance)
|
||||||
}
|
|
||||||
|
|
||||||
for _, moduleInstance := range router.ModuleInstances {
|
|
||||||
slog.Debug("registering router with module", "id", moduleInstance.Id())
|
|
||||||
moduleInstance.RegisterRouter(&router)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return &router, moduleErrors, routeErrors
|
return &router, moduleErrors, routeErrors
|
||||||
@@ -119,7 +112,6 @@ func NewRouter(ctx context.Context, config Config) (*Router, []ModuleError, []Ro
|
|||||||
func (r *Router) Run() {
|
func (r *Router) Run() {
|
||||||
slog.Info("running router")
|
slog.Info("running router")
|
||||||
for _, moduleInstance := range r.ModuleInstances {
|
for _, moduleInstance := range r.ModuleInstances {
|
||||||
moduleInstance.RegisterRouter(r)
|
|
||||||
r.moduleWait.Add(1)
|
r.moduleWait.Add(1)
|
||||||
go func() {
|
go func() {
|
||||||
err := moduleInstance.Run()
|
err := moduleInstance.Run()
|
||||||
@@ -138,16 +130,16 @@ func (r *Router) Stop() {
|
|||||||
r.contextCancel()
|
r.contextCancel()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *Router) HandleInput(sourceId string, payload any) []RoutingError {
|
func (r *Router) HandleInput(sourceId string, payload any) []route.RouteIOError {
|
||||||
var routingErrors []RoutingError
|
var routingErrors []route.RouteIOError
|
||||||
for routeIndex, route := range r.RouteInstances {
|
for routeIndex, routeInstance := range r.RouteInstances {
|
||||||
if route.Input == sourceId {
|
if routeInstance.Input() == sourceId {
|
||||||
err := route.HandleInput(sourceId, payload)
|
err := routeInstance.HandleInput(r.Context, sourceId, payload, r)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if routingErrors == nil {
|
if routingErrors == nil {
|
||||||
routingErrors = []RoutingError{}
|
routingErrors = []route.RouteIOError{}
|
||||||
}
|
}
|
||||||
routingErrors = append(routingErrors, RoutingError{
|
routingErrors = append(routingErrors, route.RouteIOError{
|
||||||
Index: routeIndex,
|
Index: routeIndex,
|
||||||
Error: err,
|
Error: err,
|
||||||
})
|
})
|
||||||
|
|||||||
Reference in New Issue
Block a user