mirror of
https://github.com/jwetzell/showbridge-go.git
synced 2026-08-07 15:33:39 +00:00
Compare commits
69 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| b7e0d879bc | |||
| 9b76ee8195 | |||
| d48a89d1ef | |||
| b01bebea54 | |||
| 73905e1274 | |||
| 56566cf666 | |||
| 5d1e6622cb | |||
| 81ae00c943 | |||
| 49e351c760 | |||
| e90356762c | |||
| 68586cd588 | |||
| 5e87cdcc5e | |||
| 2d6fa2ad19 | |||
| 3ab2cade81 | |||
| 2078d8533f | |||
| 9b5a9ce6d0 | |||
| dbeb1e8c29 | |||
| 0b494fce91 | |||
| 52ca801fbd | |||
| 3adbae8068 | |||
| 4ba718e2c6 | |||
| fa595a027b | |||
| 5660acfca6 | |||
| 3a1bc74fcf | |||
| 62ce5bf2f8 | |||
| 0f70596eb0 | |||
| db3d0530a5 | |||
| 6b53211267 | |||
| 821d192eb7 | |||
| b50882fc4a | |||
| 438c396aeb | |||
| f175bed084 | |||
| 80f8152dfb | |||
| 0ff212742a | |||
| 8fe0f7e6a2 | |||
| 9db958b523 | |||
| ea427bd2b6 | |||
| 5a2aefbdd5 | |||
| 4323a21015 | |||
| fb3c775765 | |||
| cf41bcae85 | |||
| 4cedd58a76 | |||
| be0b1c4a5f | |||
| 290a89d38e | |||
| b495446926 | |||
| fa4eeddbbf | |||
| 1bbe0cc045 | |||
| 70c4aa172a | |||
| 849a59a63b | |||
| 00b03197c2 | |||
| bb44e3b813 | |||
| 099a8af271 | |||
| f640dac0ad | |||
| 3c5b1cac4d | |||
| 7c8695b9fe | |||
| 5db23627d6 | |||
| 9f47318b7a | |||
| be1e01cc3f | |||
| 434dcec25f | |||
| d871eaaa16 | |||
| 06a2a156c5 | |||
| 7913f5febc | |||
| 99132c90c6 | |||
| b2e07d7452 | |||
| 5def922d93 | |||
| a09249a047 | |||
| a991b264a6 | |||
| 11bbf1a703 | |||
| bd8368fbb1 |
@@ -211,7 +211,8 @@ jobs:
|
||||
files: showbridge_*.tar.gz
|
||||
docker:
|
||||
runs-on: ubuntu-latest
|
||||
needs: linux_amd64
|
||||
needs: setup_release
|
||||
if: github.ref_type == 'tag' && github.event_name != 'workflow_dispatch'
|
||||
steps:
|
||||
- name: Check out repository
|
||||
uses: actions/checkout@1af3b93b6815bc44a9784bd300feb67ff0d1eeb3 # v6.0.0
|
||||
@@ -220,24 +221,23 @@ jobs:
|
||||
uses: docker/setup-qemu-action@ce360397dd3f832beb865e1373c09c0e9f86d70a # v4.0.0
|
||||
|
||||
- name: Set up Docker Buildx
|
||||
uses: docker/setup-buildx-action@4d04d5d9486b7bd6fa91e7baf45bbb4f8b9deedd # v4.0.0
|
||||
uses: docker/setup-buildx-action@d7f5e7f509e45cec5c76c4d5afdd7de93d0b3df5 # v4.1.0
|
||||
|
||||
- name: Login to Docker Hub
|
||||
uses: docker/login-action@4907a6ddec9925e35a0a9e82d7399ccc52663121 # v4.1.0
|
||||
uses: docker/login-action@650006c6eb7dba73a995cc03b0b2d7f5ca915bee # v4.2.0
|
||||
with:
|
||||
username: ${{ secrets.DOCKERHUB_USERNAME }}
|
||||
password: ${{ secrets.DOCKERHUB_TOKEN }}
|
||||
|
||||
- name: Setup Docker metadata
|
||||
id: meta
|
||||
uses: docker/metadata-action@030e881283bb7a6894de51c315a6bfe6a94e05cf # v6.0.0
|
||||
uses: docker/metadata-action@80c7e94dd9b9319bd5eb7a0e0fe9291e23a2a2e9 # v6.1.0
|
||||
with:
|
||||
images: |
|
||||
jwetzell/showbridge
|
||||
|
||||
- name: Build and push
|
||||
uses: docker/build-push-action@bcafcacb16a39f128d818304e6c9c0c18556b85f # v7.1.0
|
||||
if: github.ref_type == 'tag'
|
||||
uses: docker/build-push-action@f9f3042f7e2789586610d6e8b85c8f03e5195baf # v7.2.0
|
||||
with:
|
||||
push: true
|
||||
context: ./
|
||||
|
||||
@@ -40,7 +40,6 @@ GLOBAL OPTIONS:
|
||||
--config string path to config file (default: "./config.yaml") [$SHOWBRIDGE_CONFIG]
|
||||
--log-level string set log level (default: "info") [$SHOWBRIDGE_LOG_LEVEL]
|
||||
--log-format string log format to use (default: "text") [$SHOWBRIDGE_LOG_FORMAT]
|
||||
--trace enable OpenTelemetry tracing [$SHOWBRIDGE_TRACE]
|
||||
--help, -h show help
|
||||
--version, -v print the version
|
||||
```
|
||||
|
||||
+2
-49
@@ -16,11 +16,6 @@ import (
|
||||
"github.com/jwetzell/showbridge-go/internal/config"
|
||||
"github.com/jwetzell/showbridge-go/internal/schema"
|
||||
"github.com/urfave/cli/v3"
|
||||
"go.opentelemetry.io/otel"
|
||||
"go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp"
|
||||
"go.opentelemetry.io/otel/sdk/resource"
|
||||
sdktrace "go.opentelemetry.io/otel/sdk/trace"
|
||||
semconv "go.opentelemetry.io/otel/semconv/v1.40.0"
|
||||
"sigs.k8s.io/yaml"
|
||||
)
|
||||
|
||||
@@ -67,12 +62,6 @@ func main() {
|
||||
},
|
||||
Sources: cli.EnvVars("SHOWBRIDGE_LOG_FORMAT"),
|
||||
},
|
||||
&cli.BoolFlag{
|
||||
Name: "trace",
|
||||
Value: false,
|
||||
Usage: "enable OpenTelemetry tracing",
|
||||
Sources: cli.EnvVars("SHOWBRIDGE_TRACE"),
|
||||
},
|
||||
},
|
||||
Action: run,
|
||||
}
|
||||
@@ -117,12 +106,12 @@ func readConfig(configPath string) (config.Config, error) {
|
||||
|
||||
err = schema.ApplyDefaults(&yamlMap)
|
||||
if err != nil {
|
||||
return config.Config{}, err
|
||||
return config.Config{}, fmt.Errorf("failed to apply defaults: %w", err)
|
||||
}
|
||||
|
||||
err = schema.ValidateConfig(yamlMap)
|
||||
if err != nil {
|
||||
return config.Config{}, err
|
||||
return config.Config{}, fmt.Errorf("failed to validate config: %w", err)
|
||||
}
|
||||
|
||||
validatedConfigBytes, err := json.Marshal(yamlMap)
|
||||
@@ -196,19 +185,6 @@ func run(ctx context.Context, c *cli.Command) error {
|
||||
|
||||
slog.SetDefault(slog.New(logHandler))
|
||||
|
||||
if c.Bool("trace") {
|
||||
exporter, err := otlptracehttp.New(ctx)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to create trace exporter: %w", err)
|
||||
}
|
||||
|
||||
tracerProvider := newTracerProvider(exporter)
|
||||
otel.SetTracerProvider(tracerProvider)
|
||||
defer tracerProvider.Shutdown(ctx)
|
||||
|
||||
otel.SetTracerProvider(tracerProvider)
|
||||
}
|
||||
|
||||
showbridgeApp := &showbridgeApp{
|
||||
ctx: ctx,
|
||||
configPath: configPath,
|
||||
@@ -228,9 +204,6 @@ func run(ctx context.Context, c *cli.Command) error {
|
||||
return fmt.Errorf("errors initializing modules or routes")
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to initialize router: %w", err)
|
||||
}
|
||||
showbridgeApp.routerMutex.Lock()
|
||||
showbridgeApp.router = router
|
||||
|
||||
@@ -289,23 +262,3 @@ func (app *showbridgeApp) logConfigErrors(moduleErrors []config.ModuleError, rou
|
||||
app.logger.Error("problem initializing route", "index", routeError.Index, "error", routeError.Error)
|
||||
}
|
||||
}
|
||||
|
||||
func newTracerProvider(exp sdktrace.SpanExporter) *sdktrace.TracerProvider {
|
||||
r, err := resource.Merge(
|
||||
resource.Default(),
|
||||
resource.NewWithAttributes(
|
||||
semconv.SchemaURL,
|
||||
semconv.ServiceName("showbridge"),
|
||||
semconv.ServiceVersion(version),
|
||||
),
|
||||
)
|
||||
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
return sdktrace.NewTracerProvider(
|
||||
sdktrace.WithBatcher(exp),
|
||||
sdktrace.WithResource(r),
|
||||
)
|
||||
}
|
||||
|
||||
+58
-8
@@ -38,20 +38,45 @@ modules:
|
||||
type: websocket.client
|
||||
params:
|
||||
url: "wss://echo.websocket.org"
|
||||
- id: natsserver
|
||||
type: nats.server
|
||||
params:
|
||||
port: 4222
|
||||
- id: natsout
|
||||
type: nats.client
|
||||
params:
|
||||
url: "nats://localhost:4222"
|
||||
subject: "hello"
|
||||
- id: natsin
|
||||
type: nats.client
|
||||
params:
|
||||
url: "nats://localhost:4222"
|
||||
subject: "test"
|
||||
routes:
|
||||
- input: ticker
|
||||
processors:
|
||||
- type: string.create
|
||||
params:
|
||||
template: "hello"
|
||||
- type: router.output
|
||||
template: "wsecho: {{.Payload.UnixMilli}}"
|
||||
- type: module.output
|
||||
params:
|
||||
module: wsecho
|
||||
- input: ticker
|
||||
processors:
|
||||
- type: string.create
|
||||
params:
|
||||
template: "tcpout: {{.Payload.UnixMilli}}"
|
||||
- type: string.encode
|
||||
- type: router.output
|
||||
- type: module.output
|
||||
params:
|
||||
module: tcpout
|
||||
- type: router.output
|
||||
- input: ticker
|
||||
processors:
|
||||
- type: string.create
|
||||
params:
|
||||
template: "udpout: {{.Payload.UnixMilli}}"
|
||||
- type: string.encode
|
||||
- type: module.output
|
||||
params:
|
||||
module: udpout
|
||||
- input: ticker
|
||||
@@ -60,14 +85,15 @@ routes:
|
||||
params:
|
||||
url: "http://localhost:3000/echo"
|
||||
method: "GET"
|
||||
- type: struct.field.get
|
||||
params:
|
||||
name: "Body"
|
||||
- type: string.decode
|
||||
- type: debug.log
|
||||
- input: tcpin
|
||||
processors:
|
||||
- type: string.decode
|
||||
- type: debug.log
|
||||
- input: httpin
|
||||
processors:
|
||||
- type: debug.log
|
||||
- input: udpin
|
||||
processors:
|
||||
- type: string.decode
|
||||
@@ -82,4 +108,28 @@ routes:
|
||||
module: "sqlite"
|
||||
query: "SELECT 1;"
|
||||
- type: debug.log
|
||||
|
||||
- input: natsin
|
||||
processors:
|
||||
- type: struct.field.get
|
||||
params:
|
||||
name: "Data"
|
||||
- type: string.decode
|
||||
- type: debug.log
|
||||
- input: httpin
|
||||
processors:
|
||||
- type: http.response.create
|
||||
params:
|
||||
status: 200
|
||||
bodyTemplate: "Hello, World!"
|
||||
- type: module.output
|
||||
params:
|
||||
module: httpin
|
||||
- input: ticker
|
||||
processors:
|
||||
- type: string.create
|
||||
params:
|
||||
template: "natsout: {{.Payload}}"
|
||||
- type: pubsub.publish
|
||||
params:
|
||||
module: natsout
|
||||
topic: "test"
|
||||
|
||||
+1
-1
@@ -18,6 +18,6 @@ routes:
|
||||
params:
|
||||
address: "{{.Payload.URL.Path}}"
|
||||
- type: osc.message.encode
|
||||
- type: router.output
|
||||
- type: module.output
|
||||
params:
|
||||
module: udp
|
||||
|
||||
@@ -10,43 +10,35 @@ require (
|
||||
github.com/extism/go-sdk v1.7.1
|
||||
github.com/google/jsonschema-go v0.4.3
|
||||
github.com/gorilla/websocket v1.5.3
|
||||
github.com/jwetzell/artnet-go v0.2.2
|
||||
github.com/jwetzell/free-d-go v0.1.1
|
||||
github.com/jwetzell/artnet-go v0.3.0
|
||||
github.com/jwetzell/free-d-go v0.2.0
|
||||
github.com/jwetzell/osc-go v0.3.0
|
||||
github.com/jwetzell/psn-go v0.3.0
|
||||
github.com/nats-io/nats-server/v2 v2.14.0
|
||||
github.com/nats-io/nats-server/v2 v2.14.1
|
||||
github.com/nats-io/nats.go v1.52.0
|
||||
github.com/redis/go-redis/v9 v9.19.0
|
||||
github.com/urfave/cli/v3 v3.9.0
|
||||
gitlab.com/gomidi/midi/v2 v2.3.23
|
||||
go.bug.st/serial v1.6.4
|
||||
go.opentelemetry.io/otel v1.43.0
|
||||
go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp v1.43.0
|
||||
go.opentelemetry.io/otel/sdk v1.43.0
|
||||
go.opentelemetry.io/otel/trace v1.43.0
|
||||
modernc.org/quickjs v0.18.1
|
||||
go.bug.st/serial v1.7.1
|
||||
golang.org/x/time v0.15.0
|
||||
modernc.org/quickjs v0.18.2
|
||||
modernc.org/sqlite v1.50.1
|
||||
sigs.k8s.io/yaml v1.6.0
|
||||
)
|
||||
|
||||
require (
|
||||
github.com/antithesishq/antithesis-sdk-go v0.7.0 // indirect
|
||||
github.com/cenkalti/backoff/v5 v5.0.3 // indirect
|
||||
github.com/cespare/xxhash/v2 v2.3.0 // indirect
|
||||
github.com/creack/goselect v0.1.3 // indirect
|
||||
github.com/dustin/go-humanize v1.0.1 // indirect
|
||||
github.com/dylibso/observe-sdk/go v0.0.0-20240828172851-9145d8ad07e1 // indirect
|
||||
github.com/emiago/dtls/v3 v3.0.0-20260122183559-8b8d23e359c0 // indirect
|
||||
github.com/go-audio/riff v1.0.0 // indirect
|
||||
github.com/go-logr/logr v1.4.3 // indirect
|
||||
github.com/go-logr/stdr v1.2.2 // indirect
|
||||
github.com/gobwas/glob v0.2.3 // indirect
|
||||
github.com/gobwas/httphead v0.1.0 // indirect
|
||||
github.com/gobwas/pool v0.2.1 // indirect
|
||||
github.com/gobwas/ws v1.4.0 // indirect
|
||||
github.com/google/go-tpm v0.9.8 // indirect
|
||||
github.com/google/uuid v1.6.0 // indirect
|
||||
github.com/grpc-ecosystem/grpc-gateway/v2 v2.28.0 // indirect
|
||||
github.com/ianlancetaylor/demangle v0.0.0-20260505044615-1ff4bf46051f // indirect
|
||||
github.com/icholy/digest v1.1.0 // indirect
|
||||
github.com/klauspost/compress v1.18.6 // indirect
|
||||
@@ -63,28 +55,23 @@ require (
|
||||
github.com/pion/srtp/v3 v3.0.10 // indirect
|
||||
github.com/pion/transport/v4 v4.0.1 // indirect
|
||||
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec // indirect
|
||||
github.com/rogpeppe/go-internal v1.14.1 // indirect
|
||||
github.com/tetratelabs/wabin v0.0.0-20230304001439-f6f874872834 // indirect
|
||||
github.com/tetratelabs/wazero v1.11.0 // indirect
|
||||
github.com/zaf/g711 v1.4.0 // indirect
|
||||
go.opentelemetry.io/auto/sdk v1.2.1 // indirect
|
||||
go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.43.0 // indirect
|
||||
go.opentelemetry.io/otel/metric v1.43.0 // indirect
|
||||
go.opentelemetry.io/proto/otlp v1.10.0 // indirect
|
||||
go.uber.org/atomic v1.11.0 // indirect
|
||||
go.yaml.in/yaml/v2 v2.4.2 // indirect
|
||||
go.yaml.in/yaml/v3 v3.0.4 // indirect
|
||||
golang.org/x/crypto v0.51.0 // indirect
|
||||
golang.org/x/net v0.54.0 // indirect
|
||||
golang.org/x/sync v0.20.0 // indirect
|
||||
golang.org/x/sys v0.44.0 // indirect
|
||||
golang.org/x/text v0.37.0 // indirect
|
||||
golang.org/x/time v0.15.0 // indirect
|
||||
google.golang.org/genproto/googleapis/api v0.0.0-20260401024825-9d38bb4040a9 // indirect
|
||||
google.golang.org/genproto/googleapis/rpc v0.0.0-20260401024825-9d38bb4040a9 // indirect
|
||||
google.golang.org/grpc v1.80.0 // indirect
|
||||
golang.org/x/tools v0.44.0 // indirect
|
||||
google.golang.org/protobuf v1.36.11 // indirect
|
||||
gopkg.in/hraban/opus.v2 v2.0.0-20230925203106-0188a62cb302 // indirect
|
||||
modernc.org/libc v1.72.3 // indirect
|
||||
modernc.org/libquickjs v0.12.7 // indirect
|
||||
modernc.org/libquickjs v0.12.8 // indirect
|
||||
modernc.org/mathutil v1.7.1 // indirect
|
||||
modernc.org/memory v1.11.0 // indirect
|
||||
)
|
||||
|
||||
@@ -4,12 +4,8 @@ github.com/bsm/ginkgo/v2 v2.12.0 h1:Ny8MWAHyOepLGlLKYmXG4IEkioBysk6GpaRTLC8zwWs=
|
||||
github.com/bsm/ginkgo/v2 v2.12.0/go.mod h1:SwYbGRRDovPVboqFv0tPTcG1sN61LM1Z4ARdbAV9g4c=
|
||||
github.com/bsm/gomega v1.27.10 h1:yeMWxP2pV2fG3FgAODIY8EiRE3dy0aeFYt4l7wh6yKA=
|
||||
github.com/bsm/gomega v1.27.10/go.mod h1:JyEr/xRbxbtgWNi8tIEVPUYZ5Dzef52k01W3YH0H+O0=
|
||||
github.com/cenkalti/backoff/v5 v5.0.3 h1:ZN+IMa753KfX5hd8vVaMixjnqRZ3y8CuJKRKj1xcsSM=
|
||||
github.com/cenkalti/backoff/v5 v5.0.3/go.mod h1:rkhZdG3JZukswDf7f0cwqPNk4K0sa+F97BxZthm/crw=
|
||||
github.com/cespare/xxhash/v2 v2.3.0 h1:UL815xU9SqsFlibzuggzjXhog7bL6oX9BbNZnL2UFvs=
|
||||
github.com/cespare/xxhash/v2 v2.3.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
|
||||
github.com/creack/goselect v0.1.3 h1:MaGNMclRo7P2Jl21hBpR1Cn33ITSbKP6E49RtfblLKc=
|
||||
github.com/creack/goselect v0.1.3/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/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/dustin/go-humanize v1.0.1 h1:GzkhY7T5VNhEkwH0PVJgjz+fX1rhBrR7pRT3mDkpeCY=
|
||||
@@ -30,11 +26,6 @@ github.com/extism/go-sdk v1.7.1 h1:lWJos6uY+tRFdlIHR+SJjwFDApY7OypS/2nMhiVQ9Sw=
|
||||
github.com/extism/go-sdk v1.7.1/go.mod h1:IT+Xdg5AZM9hVtpFUA+uZCJMge/hbvshl8bwzLtFyKA=
|
||||
github.com/go-audio/riff v1.0.0 h1:d8iCGbDvox9BfLagY94fBynxSPHO80LmZCaOsmKxokA=
|
||||
github.com/go-audio/riff v1.0.0/go.mod h1:l3cQwc85y79NQFCRB7TiPoNiaijp6q8Z0Uv38rVG498=
|
||||
github.com/go-logr/logr v1.2.2/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A=
|
||||
github.com/go-logr/logr v1.4.3 h1:CjnDlHq8ikf6E492q6eKboGOC0T8CDaOvkHCIg8idEI=
|
||||
github.com/go-logr/logr v1.4.3/go.mod h1:9T104GzyrTigFIr8wt5mBrctHMim0Nb2HLGrmQ40KvY=
|
||||
github.com/go-logr/stdr v1.2.2 h1:hSWxHoqTgW2S2qGc0LTAI563KZ5YKYRhT3MFKZMbjag=
|
||||
github.com/go-logr/stdr v1.2.2/go.mod h1:mMo/vtBO5dYbehREoey6XUKy/eSumjCCveDpRre4VKE=
|
||||
github.com/gobwas/glob v0.2.3 h1:A4xDbljILXROh+kObIiy5kIaPYD8e96x1tgBhUI5J+Y=
|
||||
github.com/gobwas/glob v0.2.3/go.mod h1:d3Ez4x06l9bZtSvzIay5+Yzi0fmZzPgnTbPcKjJAkT8=
|
||||
github.com/gobwas/httphead v0.1.0 h1:exrUm0f4YX0L7EBwZHuCF4GDp8aJfVeBrlLQrs6NqWU=
|
||||
@@ -43,8 +34,6 @@ github.com/gobwas/pool v0.2.1 h1:xfeeEhW7pwmX8nuLVlqbzVc7udMDrwetjEv+TZIz1og=
|
||||
github.com/gobwas/pool v0.2.1/go.mod h1:q8bcK0KcYlCgd9e7WYLm9LpyS+YeLd8JVDW6WezmKEw=
|
||||
github.com/gobwas/ws v1.4.0 h1:CTaoG1tojrh4ucGPcoJFiAQUAsEWekEWvLy7GsVNqGs=
|
||||
github.com/gobwas/ws v1.4.0/go.mod h1:G3gNqMNtPppf5XUz7O4shetPpcZ1VJ7zt18dlUeakrc=
|
||||
github.com/golang/protobuf v1.5.4 h1:i7eJL8qZTpSEXOPTxNKhASYpMn+8e5Q6AdndVa1dWek=
|
||||
github.com/golang/protobuf v1.5.4/go.mod h1:lnTiLA8Wa4RWRcIUkrtSVa5nRhsEGBg48fD6rSs7xps=
|
||||
github.com/google/go-cmp v0.7.0 h1:wk8382ETsv4JYUZwIsn6YpYiWiBsYLSJiTsyBybVuN8=
|
||||
github.com/google/go-cmp v0.7.0/go.mod h1:pXiqmnSA92OHEEa9HXL2W4E7lf9JzCmGVUdgjX3N/iU=
|
||||
github.com/google/go-tpm v0.9.8 h1:slArAR9Ft+1ybZu0lBwpSmpwhRXaa85hWtMinMyRAWo=
|
||||
@@ -57,18 +46,16 @@ github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
|
||||
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
|
||||
github.com/gorilla/websocket v1.5.3 h1:saDtZ6Pbx/0u+bgYQ3q96pZgCzfhKXGPqt7kZ72aNNg=
|
||||
github.com/gorilla/websocket v1.5.3/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
|
||||
github.com/grpc-ecosystem/grpc-gateway/v2 v2.28.0 h1:HWRh5R2+9EifMyIHV7ZV+MIZqgz+PMpZ14Jynv3O2Zs=
|
||||
github.com/grpc-ecosystem/grpc-gateway/v2 v2.28.0/go.mod h1:JfhWUomR1baixubs02l85lZYYOm7LV6om4ceouMv45c=
|
||||
github.com/hashicorp/golang-lru/v2 v2.0.7 h1:a+bsQ5rvGLjzHuww6tVxozPZFVghXaHOwFs4luLUK2k=
|
||||
github.com/hashicorp/golang-lru/v2 v2.0.7/go.mod h1:QeFd9opnmA6QUJc5vARoKUSoFhyfM2/ZepoAG6RGpeM=
|
||||
github.com/ianlancetaylor/demangle v0.0.0-20260505044615-1ff4bf46051f h1:NW3E2QSchEk63/fjeEvWOa2cE02FSv9ox//VE/N4c8g=
|
||||
github.com/ianlancetaylor/demangle v0.0.0-20260505044615-1ff4bf46051f/go.mod h1:gx7rwoVhcfuVKG5uya9Hs3Sxj7EIvldVofAWIUtGouw=
|
||||
github.com/icholy/digest v1.1.0 h1:HfGg9Irj7i+IX1o1QAmPfIBNu/Q5A5Tu3n/MED9k9H4=
|
||||
github.com/icholy/digest v1.1.0/go.mod h1:QNrsSGQ5v7v9cReDI0+eyjsXGUoRSUZQHeQ5C4XLa0Y=
|
||||
github.com/jwetzell/artnet-go v0.2.2 h1:vaXSmRrSlEdETPZ/gvw6aAmVqzQm0i+gfAwyIQkVsB0=
|
||||
github.com/jwetzell/artnet-go v0.2.2/go.mod h1:gli97Z32a0kMkZ6taoTiK7/lqHcF/dhiGjGJdx/PxqA=
|
||||
github.com/jwetzell/free-d-go v0.1.1 h1:pzY2c4qRxKFBZ2jO3z512ysvUI0BUmgd4mSlWPiFVRM=
|
||||
github.com/jwetzell/free-d-go v0.1.1/go.mod h1:KmrkooRARRaxJTBSPvwt/6IMAIaHH1R8bSA8cwbbELw=
|
||||
github.com/jwetzell/artnet-go v0.3.0 h1:MDHElOSbpgmIbYGHO9SoxsT7ynrYtV5/UHGT82geHk0=
|
||||
github.com/jwetzell/artnet-go v0.3.0/go.mod h1:zqRsSsrd6rPwJacwscPteDqGIqevjZ3Z6dpp092VJ1w=
|
||||
github.com/jwetzell/free-d-go v0.2.0 h1:8WQW4du8Sf3d18aUzk9n5jZtHE6WdDQHDFjkFffvycc=
|
||||
github.com/jwetzell/free-d-go v0.2.0/go.mod h1:yHuRy51NeDfN26eJ6wzZgys8qyQiS7hkYILw8yF+UJ4=
|
||||
github.com/jwetzell/osc-go v0.3.0 h1:z75TxuQSEmdcmZ56OAepkDa3m88SdZh//3m4nBb/XZI=
|
||||
github.com/jwetzell/osc-go v0.3.0/go.mod h1:kCs329JxY6Qjga08tRQ/Gl0PqhgQzLIMpOhm6uszvIc=
|
||||
github.com/jwetzell/psn-go v0.3.0 h1:WVpCEmExYE8a+I5hQak5jNJJp2x35VdGX/VuMUKPmhY=
|
||||
@@ -87,8 +74,8 @@ github.com/minio/highwayhash v1.0.4 h1:asJizugGgchQod2ja9NJlGOWq4s7KsAWr5XUc9Clg
|
||||
github.com/minio/highwayhash v1.0.4/go.mod h1:GGYsuwP/fPD6Y9hMiXuapVvlIUEhFhMTh0rxU3ik1LQ=
|
||||
github.com/nats-io/jwt/v2 v2.8.1 h1:V0xpGuD/N8Mi+fQNDynXohVvp7ZztevW5io8CUWlPmU=
|
||||
github.com/nats-io/jwt/v2 v2.8.1/go.mod h1:nWnOEEiVMiKHQpnAy4eXlizVEtSfzacZ1Q43LIRavZg=
|
||||
github.com/nats-io/nats-server/v2 v2.14.0 h1:+8q0HrDFotwLLcGH/legOEOnowunhK+aZ4GYBIWpQlM=
|
||||
github.com/nats-io/nats-server/v2 v2.14.0/go.mod h1:ImVUUDvfClJbb6cuJQRc1VmgDCXKM5ds0OoiG9MVOKo=
|
||||
github.com/nats-io/nats-server/v2 v2.14.1 h1:wXs/a5fw9Hzm3CvuzLxGeIwpjPulSa7gMT3eSuhGkcg=
|
||||
github.com/nats-io/nats-server/v2 v2.14.1/go.mod h1:4N17zLpuS7WMbG8T9gsE2B7z9hC9PraPyulVBfpK6nU=
|
||||
github.com/nats-io/nats.go v1.52.0 h1:n3avV4VBsCgsdwh71TppsTwtv+QdPs7ntSKM8qJLGsc=
|
||||
github.com/nats-io/nats.go v1.52.0/go.mod h1:26HypzazeOkyO3/mqd1zZd53STJN0EjCYF9Uy2ZOBno=
|
||||
github.com/nats-io/nkeys v0.4.15 h1:JACV5jRVO9V856KOapQ7x+EY8Jo3qw1vJt/9Jpwzkk4=
|
||||
@@ -131,38 +118,20 @@ github.com/zeebo/xxh3 v1.1.0 h1:s7DLGDK45Dyfg7++yxI0khrfwq9661w9EN78eP/UZVs=
|
||||
github.com/zeebo/xxh3 v1.1.0/go.mod h1:IisAie1LELR4xhVinxWS5+zf1lA4p0MW4T+w+W07F5s=
|
||||
gitlab.com/gomidi/midi/v2 v2.3.23 h1:P8NxV4EzV9c+BjpwTeB+G/qa+Xdq/UTazS2fKxY0O0g=
|
||||
gitlab.com/gomidi/midi/v2 v2.3.23/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.opentelemetry.io/auto/sdk v1.2.1 h1:jXsnJ4Lmnqd11kwkBV2LgLoFMZKizbCi5fNZ/ipaZ64=
|
||||
go.opentelemetry.io/auto/sdk v1.2.1/go.mod h1:KRTj+aOaElaLi+wW1kO/DZRXwkF4C5xPbEe3ZiIhN7Y=
|
||||
go.opentelemetry.io/otel v1.43.0 h1:mYIM03dnh5zfN7HautFE4ieIig9amkNANT+xcVxAj9I=
|
||||
go.opentelemetry.io/otel v1.43.0/go.mod h1:JuG+u74mvjvcm8vj8pI5XiHy1zDeoCS2LB1spIq7Ay0=
|
||||
go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.43.0 h1:88Y4s2C8oTui1LGM6bTWkw0ICGcOLCAI5l6zsD1j20k=
|
||||
go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.43.0/go.mod h1:Vl1/iaggsuRlrHf/hfPJPvVag77kKyvrLeD10kpMl+A=
|
||||
go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp v1.43.0 h1:3iZJKlCZufyRzPzlQhUIWVmfltrXuGyfjREgGP3UUjc=
|
||||
go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp v1.43.0/go.mod h1:/G+nUPfhq2e+qiXMGxMwumDrP5jtzU+mWN7/sjT2rak=
|
||||
go.opentelemetry.io/otel/metric v1.43.0 h1:d7638QeInOnuwOONPp4JAOGfbCEpYb+K6DVWvdxGzgM=
|
||||
go.opentelemetry.io/otel/metric v1.43.0/go.mod h1:RDnPtIxvqlgO8GRW18W6Z/4P462ldprJtfxHxyKd2PY=
|
||||
go.opentelemetry.io/otel/sdk v1.43.0 h1:pi5mE86i5rTeLXqoF/hhiBtUNcrAGHLKQdhg4h4V9Dg=
|
||||
go.opentelemetry.io/otel/sdk v1.43.0/go.mod h1:P+IkVU3iWukmiit/Yf9AWvpyRDlUeBaRg6Y+C58QHzg=
|
||||
go.opentelemetry.io/otel/sdk/metric v1.43.0 h1:S88dyqXjJkuBNLeMcVPRFXpRw2fuwdvfCGLEo89fDkw=
|
||||
go.opentelemetry.io/otel/sdk/metric v1.43.0/go.mod h1:C/RJtwSEJ5hzTiUz5pXF1kILHStzb9zFlIEe85bhj6A=
|
||||
go.opentelemetry.io/otel/trace v1.43.0 h1:BkNrHpup+4k4w+ZZ86CZoHHEkohws8AY+WTX09nk+3A=
|
||||
go.opentelemetry.io/otel/trace v1.43.0/go.mod h1:/QJhyVBUUswCphDVxq+8mld+AvhXZLhe+8WVFxiFff0=
|
||||
go.bug.st/serial v1.7.1 h1:5aP8wYL0UjEYOVs3oPAGscjaSfRQLHtCvBFXNN/rwtc=
|
||||
go.bug.st/serial v1.7.1/go.mod h1:d0MmS16Qt9b1m06yoYRNUXhRRTJV5Qg2S5EKqQtnayQ=
|
||||
go.opentelemetry.io/proto/otlp v1.10.0 h1:IQRWgT5srOCYfiWnpqUYz9CVmbO8bFmKcwYxpuCSL2g=
|
||||
go.opentelemetry.io/proto/otlp v1.10.0/go.mod h1:/CV4QoCR/S9yaPj8utp3lvQPoqMtxXdzn7ozvvozVqk=
|
||||
go.uber.org/atomic v1.11.0 h1:ZvwS0R+56ePWxUNi+Atn9dWONBPp/AUETXlHW0DxSjE=
|
||||
go.uber.org/atomic v1.11.0/go.mod h1:LUxbIzbOniOlMKjJjyPfpl4v+PKK2cNJn91OQbhoJI0=
|
||||
go.uber.org/goleak v1.3.0 h1:2K3zAYmnTNqV73imy9J1T3WC+gmCePx2hEGkimedGto=
|
||||
go.uber.org/goleak v1.3.0/go.mod h1:CoHD4mav9JJNrW/WLlf7HGZPjdw8EucARQHekz1X6bE=
|
||||
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/v3 v3.0.4 h1:tfq32ie2Jv2UxXFdLJdh3jXuOzWiL1fo0bu/FbuKpbc=
|
||||
go.yaml.in/yaml/v3 v3.0.4/go.mod h1:DhzuOOF2ATzADvBadXxruRBLzYTpT36CKvDb3+aBEFg=
|
||||
golang.org/x/crypto v0.51.0 h1:IBPXwPfKxY7cWQZ38ZCIRPI50YLeevDLlLnyC5wRGTI=
|
||||
golang.org/x/crypto v0.51.0/go.mod h1:8AdwkbraGNABw2kOX6YFPs3WM22XqI4EXEd8g+x7Oc8=
|
||||
golang.org/x/mod v0.35.0 h1:Ww1D637e6Pg+Zb2KrWfHQUnH2dQRLBQyAtpr/haaJeM=
|
||||
golang.org/x/mod v0.35.0/go.mod h1:+GwiRhIInF8wPm+4AoT6L0FA1QWAad3OMdTRx4tFYlU=
|
||||
golang.org/x/mod v0.36.0 h1:JJjpVx6myfUsUdAzZuOSTTmRE0PfZeNWzzvKrP7amb4=
|
||||
golang.org/x/mod v0.36.0/go.mod h1:moc6ELqsWcOw5Ef3xVprK5ul/MvtVvkIXLziUOICjUQ=
|
||||
golang.org/x/net v0.54.0 h1:2zJIZAxAHV/OHCDTCOHAYehQzLfSXuf/5SoL/Dv6w/w=
|
||||
golang.org/x/net v0.54.0/go.mod h1:Sj4oj8jK6XmHpBZU/zWHw3BV3abl4Kvi+Ut7cQcY+cQ=
|
||||
golang.org/x/sync v0.20.0 h1:e0PTpb7pjO8GAtTs2dQ6jYa5BWYlMuX047Dco/pItO4=
|
||||
@@ -171,20 +140,10 @@ golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.21.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
|
||||
golang.org/x/sys v0.44.0 h1:ildZl3J4uzeKP07r2F++Op7E9B29JRUy+a27EibtBTQ=
|
||||
golang.org/x/sys v0.44.0/go.mod h1:4GL1E5IUh+htKOUEOaiffhrAeqysfVGipDYzABqnCmw=
|
||||
golang.org/x/text v0.37.0 h1:Cqjiwd9eSg8e0QAkyCaQTNHFIIzWtidPahFWR83rTrc=
|
||||
golang.org/x/text v0.37.0/go.mod h1:a5sjxXGs9hsn/AJVwuElvCAo9v8QYLzvavO5z2PiM38=
|
||||
golang.org/x/time v0.15.0 h1:bbrp8t3bGUeFOx08pvsMYRTCVSMk89u4tKbNOZbp88U=
|
||||
golang.org/x/time v0.15.0/go.mod h1:Y4YMaQmXwGQZoFaVFk4YpCt4FLQMYKZe9oeV/f4MSno=
|
||||
golang.org/x/tools v0.44.0 h1:UP4ajHPIcuMjT1GqzDWRlalUEoY+uzoZKnhOjbIPD2c=
|
||||
golang.org/x/tools v0.44.0/go.mod h1:KA0AfVErSdxRZIsOVipbv3rQhVXTnlU6UhKxHd1seDI=
|
||||
gonum.org/v1/gonum v0.17.0 h1:VbpOemQlsSMrYmn7T2OUvQ4dqxQXU+ouZFQsZOx50z4=
|
||||
gonum.org/v1/gonum v0.17.0/go.mod h1:El3tOrEuMpv2UdMrbNlKEh9vd86bmQ6vqIcDwxEOc1E=
|
||||
google.golang.org/genproto/googleapis/api v0.0.0-20260401024825-9d38bb4040a9 h1:VPWxll4HlMw1Vs/qXtN7BvhZqsS9cdAittCNvVENElA=
|
||||
google.golang.org/genproto/googleapis/api v0.0.0-20260401024825-9d38bb4040a9/go.mod h1:7QBABkRtR8z+TEnmXTqIqwJLlzrZKVfAUm7tY3yGv0M=
|
||||
google.golang.org/genproto/googleapis/rpc v0.0.0-20260401024825-9d38bb4040a9 h1:m8qni9SQFH0tJc1X0vmnpw/0t+AImlSvp30sEupozUg=
|
||||
google.golang.org/genproto/googleapis/rpc v0.0.0-20260401024825-9d38bb4040a9/go.mod h1:4Hqkh8ycfw05ld/3BWL7rJOSfebL2Q+DVDeRgYgxUU8=
|
||||
google.golang.org/grpc v1.80.0 h1:Xr6m2WmWZLETvUNvIUmeD5OAagMw3FiKmMlTdViWsHM=
|
||||
google.golang.org/grpc v1.80.0/go.mod h1:ho/dLnxwi3EDJA4Zghp7k2Ec1+c2jqup0bFkw07bwF4=
|
||||
google.golang.org/protobuf v1.36.11 h1:fV6ZwhNocDyBLK0dj+fg8ektcVegBBuEolpbTQyBNVE=
|
||||
google.golang.org/protobuf v1.36.11/go.mod h1:HTf+CrKn2C3g5S8VImy6tdcUvCska2kB7j23XfzDpco=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
@@ -210,16 +169,16 @@ modernc.org/goabi0 v0.2.0 h1:HvEowk7LxcPd0eq6mVOAEMai46V+i7Jrj13t4AzuNks=
|
||||
modernc.org/goabi0 v0.2.0/go.mod h1:CEFRnnJhKvWT1c1JTI3Avm+tgOWbkOu5oPA8eH8LnMI=
|
||||
modernc.org/libc v1.72.3 h1:ZnDF4tXn4NBXFutMMQC4vtbTFSXhhKzR73fv0beZEAU=
|
||||
modernc.org/libc v1.72.3/go.mod h1:dn0dZNnnn1clLyvRxLxYExxiKRZIRENOfqQ8XEeg4Qs=
|
||||
modernc.org/libquickjs v0.12.7 h1:jSU4+KWDnA6ILat9SAy5zMBSU3Cxtco1NzSJ7DZgCKc=
|
||||
modernc.org/libquickjs v0.12.7/go.mod h1:ajFW8dWHtF8ggFWGbU2BBEM6FI0vemVWrd8nEHAPZpE=
|
||||
modernc.org/libquickjs v0.12.8 h1:wJ2GGxEDcQb7j8ILZScNNP1WsXFILlo1qQ8cS3q/tPw=
|
||||
modernc.org/libquickjs v0.12.8/go.mod h1:nIPezuUN/J6aDLSaR76qNaPNNy79FH5Gkjb/x9wa/gU=
|
||||
modernc.org/mathutil v1.7.1 h1:GCZVGXdaN8gTqB1Mf/usp1Y/hSqgI2vAGGP4jZMCxOU=
|
||||
modernc.org/mathutil v1.7.1/go.mod h1:4p5IwJITfppl0G4sUEDtCr4DthTaT47/N3aT6MhfgJg=
|
||||
modernc.org/memory v1.11.0 h1:o4QC8aMQzmcwCK3t3Ux/ZHmwFPzE6hf2Y5LbkRs+hbI=
|
||||
modernc.org/memory v1.11.0/go.mod h1:/JP4VbVC+K5sU2wZi9bHoq2MAkCnrt2r98UGeSK7Mjw=
|
||||
modernc.org/opt v0.2.0 h1:tGyef5ApycA7FSEOMraay9SaTk5zmbx7Tu+cJs4QKZg=
|
||||
modernc.org/opt v0.2.0/go.mod h1:03fq9lsNfvkYSfxrfUhZCWPk1lm4cq4N+Bh//bEtgns=
|
||||
modernc.org/quickjs v0.18.1 h1:exrw1Bp1smLUVXiZ4M9yfUP24EeU1C2wCKD1IHN2JFk=
|
||||
modernc.org/quickjs v0.18.1/go.mod h1:wMyqS7/VurEMUms6XwVQoX7UhJakAr9EMc8aPSL5Xjc=
|
||||
modernc.org/quickjs v0.18.2 h1:v/KDawJfs4L7jSCItAKrkrHCmOL5VnDLMJOXpU8Evdk=
|
||||
modernc.org/quickjs v0.18.2/go.mod h1:U11Fhn2hC71v+DyvEN3cY4IXkjU5XxrYT0DQWvZ43+A=
|
||||
modernc.org/sortutil v1.2.1 h1:+xyoGf15mM3NMlPDnFqrteY07klSFxLElE2PVuWIJ7w=
|
||||
modernc.org/sortutil v1.2.1/go.mod h1:7ZI3a3REbai7gzCLcotuw9AC4VZVpYMjDzETGsSMqJE=
|
||||
modernc.org/sqlite v1.50.1 h1:l+cQvn0sd0zJJtfygGHuQJ5AjlrwXmWPw4KP3ZMwr9w=
|
||||
|
||||
@@ -89,7 +89,7 @@ func GetAnyAsByteSlice(value any) ([]byte, bool) {
|
||||
|
||||
// check for a slice
|
||||
v := reflect.ValueOf(value)
|
||||
if v.Kind() != reflect.Slice {
|
||||
if v.Kind() != reflect.Slice && v.Kind() != reflect.Array {
|
||||
return nil, false
|
||||
}
|
||||
|
||||
@@ -108,7 +108,7 @@ func GetAnyAsByteSlice(value any) ([]byte, bool) {
|
||||
|
||||
func GetAnyAsIntSlice(value any) ([]int, bool) {
|
||||
|
||||
// already a []byte
|
||||
// already a []int
|
||||
intSlice, ok := value.([]int)
|
||||
if ok {
|
||||
return intSlice, true
|
||||
@@ -116,7 +116,7 @@ func GetAnyAsIntSlice(value any) ([]int, bool) {
|
||||
|
||||
// check for a slice
|
||||
v := reflect.ValueOf(value)
|
||||
if v.Kind() != reflect.Slice {
|
||||
if v.Kind() != reflect.Slice && v.Kind() != reflect.Array {
|
||||
return nil, false
|
||||
}
|
||||
|
||||
|
||||
@@ -8,7 +8,7 @@ import (
|
||||
type Module interface {
|
||||
Id() string
|
||||
Type() string
|
||||
Start(context.Context, RouteIO) error
|
||||
Start(context.Context, InputHandler) error
|
||||
Stop()
|
||||
}
|
||||
|
||||
@@ -17,10 +17,14 @@ type OutputModule interface {
|
||||
}
|
||||
|
||||
type KeyValueModule interface {
|
||||
Get(key string) (any, error)
|
||||
Set(key string, value any) error
|
||||
Get(ctx context.Context, key string) (any, error)
|
||||
Set(ctx context.Context, key string, value any) error
|
||||
}
|
||||
|
||||
type DatabaseModule interface {
|
||||
Database() (*sql.DB, error)
|
||||
QueryContext(ctx context.Context, query string, args ...any) (*sql.Rows, error)
|
||||
}
|
||||
|
||||
type PubSubModule interface {
|
||||
Publish(ctx context.Context, topic string, payload any) error
|
||||
}
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
package common
|
||||
|
||||
type WrappedPayload struct {
|
||||
Payload any
|
||||
Router RouteIO
|
||||
Modules map[string]Module
|
||||
Source string
|
||||
End bool
|
||||
Payload any
|
||||
InputHandler InputHandler
|
||||
Modules map[string]Module
|
||||
Source string
|
||||
End bool
|
||||
}
|
||||
|
||||
@@ -4,14 +4,9 @@ import (
|
||||
"context"
|
||||
)
|
||||
|
||||
type RouteIO interface {
|
||||
HandleInput(ctx context.Context, sourceId string, payload any) (bool, []RouteIOError)
|
||||
HandleOutput(ctx context.Context, destinationId string, payload any) error
|
||||
}
|
||||
type InputHandler func(ctx context.Context, sourceId string, payload any) (bool, []RouteIOError)
|
||||
|
||||
type RouteIOError struct {
|
||||
Index int `json:"index"`
|
||||
OutputError error `json:"outputError"`
|
||||
ProcessError error `json:"processError"`
|
||||
InputError error `json:"inputError"`
|
||||
}
|
||||
|
||||
@@ -10,15 +10,15 @@ type Framer interface {
|
||||
func GetFramer(framingType string) Framer {
|
||||
switch framingType {
|
||||
case "CR":
|
||||
return NewByteSeparatorFramer([]byte{'\r'})
|
||||
return newByteSeparatorFramer([]byte{'\r'})
|
||||
case "LF":
|
||||
return NewByteSeparatorFramer([]byte{'\n'})
|
||||
return newByteSeparatorFramer([]byte{'\n'})
|
||||
case "CRLF":
|
||||
return NewByteSeparatorFramer([]byte{'\r', '\n'})
|
||||
return newByteSeparatorFramer([]byte{'\r', '\n'})
|
||||
case "SLIP":
|
||||
return NewSlipFramer()
|
||||
return newSlipFramer()
|
||||
case "RAW":
|
||||
return NewRawFramer()
|
||||
return newRawFramer()
|
||||
default:
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -1,23 +1,23 @@
|
||||
package framer
|
||||
|
||||
type RawFramer struct{}
|
||||
type rawFramer struct{}
|
||||
|
||||
func NewRawFramer() *RawFramer {
|
||||
return &RawFramer{}
|
||||
func newRawFramer() *rawFramer {
|
||||
return &rawFramer{}
|
||||
}
|
||||
|
||||
func (rf *RawFramer) Decode(data []byte) [][]byte {
|
||||
func (rf *rawFramer) Decode(data []byte) [][]byte {
|
||||
return [][]byte{data}
|
||||
}
|
||||
|
||||
func (rf *RawFramer) Encode(data []byte) []byte {
|
||||
func (rf *rawFramer) Encode(data []byte) []byte {
|
||||
return data
|
||||
}
|
||||
|
||||
func (rf *RawFramer) Clear() {
|
||||
func (rf *rawFramer) Clear() {
|
||||
// NOTE(jwetzell): no internal state to clear
|
||||
}
|
||||
|
||||
func (rf *RawFramer) Buffer() []byte {
|
||||
func (rf *rawFramer) Buffer() []byte {
|
||||
return []byte{}
|
||||
}
|
||||
|
||||
@@ -4,16 +4,16 @@ import (
|
||||
"bytes"
|
||||
)
|
||||
|
||||
type ByteSeparatorFramer struct {
|
||||
type byteSeparatorFramer struct {
|
||||
buffer []byte
|
||||
separator []byte
|
||||
}
|
||||
|
||||
func NewByteSeparatorFramer(separator []byte) *ByteSeparatorFramer {
|
||||
return &ByteSeparatorFramer{separator: separator, buffer: []byte{}}
|
||||
func newByteSeparatorFramer(separator []byte) *byteSeparatorFramer {
|
||||
return &byteSeparatorFramer{separator: separator, buffer: []byte{}}
|
||||
}
|
||||
|
||||
func (bsf *ByteSeparatorFramer) Decode(data []byte) [][]byte {
|
||||
func (bsf *byteSeparatorFramer) Decode(data []byte) [][]byte {
|
||||
messages := [][]byte{}
|
||||
|
||||
bsf.buffer = append(bsf.buffer, data...)
|
||||
@@ -28,14 +28,14 @@ func (bsf *ByteSeparatorFramer) Decode(data []byte) [][]byte {
|
||||
return messages
|
||||
}
|
||||
|
||||
func (bsf *ByteSeparatorFramer) Encode(data []byte) []byte {
|
||||
func (bsf *byteSeparatorFramer) Encode(data []byte) []byte {
|
||||
return append(data, bsf.separator...)
|
||||
}
|
||||
|
||||
func (bsf *ByteSeparatorFramer) Clear() {
|
||||
func (bsf *byteSeparatorFramer) Clear() {
|
||||
bsf.buffer = []byte{}
|
||||
}
|
||||
|
||||
func (bsf *ByteSeparatorFramer) Buffer() []byte {
|
||||
func (bsf *byteSeparatorFramer) Buffer() []byte {
|
||||
return bsf.buffer
|
||||
}
|
||||
|
||||
@@ -1,14 +1,14 @@
|
||||
package framer
|
||||
|
||||
type SlipFramer struct {
|
||||
type slipFramer struct {
|
||||
buffer []byte
|
||||
}
|
||||
|
||||
func NewSlipFramer() *SlipFramer {
|
||||
return &SlipFramer{buffer: []byte{}}
|
||||
func newSlipFramer() *slipFramer {
|
||||
return &slipFramer{buffer: []byte{}}
|
||||
}
|
||||
|
||||
func (sf *SlipFramer) Decode(data []byte) [][]byte {
|
||||
func (sf *slipFramer) Decode(data []byte) [][]byte {
|
||||
messages := [][]byte{}
|
||||
|
||||
END := byte(0xc0)
|
||||
@@ -49,7 +49,7 @@ func (sf *SlipFramer) Decode(data []byte) [][]byte {
|
||||
return messages
|
||||
}
|
||||
|
||||
func (sf *SlipFramer) Encode(data []byte) []byte {
|
||||
func (sf *slipFramer) Encode(data []byte) []byte {
|
||||
END := byte(0xc0)
|
||||
ESC := byte(0xdb)
|
||||
ESC_END := byte(0xdc)
|
||||
@@ -72,10 +72,10 @@ func (sf *SlipFramer) Encode(data []byte) []byte {
|
||||
return encodedBytes
|
||||
}
|
||||
|
||||
func (sf *SlipFramer) Clear() {
|
||||
func (sf *slipFramer) Clear() {
|
||||
sf.buffer = []byte{}
|
||||
}
|
||||
|
||||
func (sf *SlipFramer) Buffer() []byte {
|
||||
func (sf *slipFramer) Buffer() []byte {
|
||||
return sf.buffer
|
||||
}
|
||||
|
||||
@@ -14,17 +14,6 @@ import (
|
||||
_ "modernc.org/sqlite"
|
||||
)
|
||||
|
||||
type DbSqlite struct {
|
||||
config config.ModuleConfig
|
||||
Dsn string
|
||||
ctx context.Context
|
||||
router common.RouteIO
|
||||
db *sql.DB
|
||||
logger *slog.Logger
|
||||
dbMu sync.Mutex
|
||||
cancel context.CancelFunc
|
||||
}
|
||||
|
||||
func init() {
|
||||
RegisterModule(ModuleRegistration{
|
||||
Type: "db.sqlite",
|
||||
@@ -33,8 +22,10 @@ func init() {
|
||||
Type: "object",
|
||||
Properties: map[string]*jsonschema.Schema{
|
||||
"dsn": {
|
||||
Type: "string",
|
||||
MinLength: new(1),
|
||||
Title: "Data Source Name",
|
||||
Description: "the data source name (DSN) for the SQLite database",
|
||||
Type: "string",
|
||||
MinLength: new(1),
|
||||
},
|
||||
},
|
||||
Required: []string{"dsn"},
|
||||
@@ -53,51 +44,60 @@ func init() {
|
||||
})
|
||||
}
|
||||
|
||||
func (t *DbSqlite) Id() string {
|
||||
return t.config.Id
|
||||
type DbSqlite struct {
|
||||
config config.ModuleConfig
|
||||
Dsn string
|
||||
ctx context.Context
|
||||
inputHandler common.InputHandler
|
||||
db *sql.DB
|
||||
logger *slog.Logger
|
||||
dbMu sync.Mutex
|
||||
cancel context.CancelFunc
|
||||
}
|
||||
|
||||
func (t *DbSqlite) Type() string {
|
||||
return t.config.Type
|
||||
func (dbs *DbSqlite) Id() string {
|
||||
return dbs.config.Id
|
||||
}
|
||||
|
||||
func (t *DbSqlite) Start(ctx context.Context, router common.RouteIO) error {
|
||||
t.logger.Debug("running")
|
||||
t.router = router
|
||||
func (dbs *DbSqlite) Type() string {
|
||||
return dbs.config.Type
|
||||
}
|
||||
|
||||
func (dbs *DbSqlite) Start(ctx context.Context, inputHandler common.InputHandler) error {
|
||||
dbs.logger.Debug("running")
|
||||
dbs.inputHandler = inputHandler
|
||||
moduleContext, cancel := context.WithCancel(ctx)
|
||||
t.ctx = moduleContext
|
||||
t.cancel = cancel
|
||||
dbs.ctx = moduleContext
|
||||
dbs.cancel = cancel
|
||||
|
||||
db, err := sql.Open("sqlite", t.Dsn)
|
||||
db, err := sql.Open("sqlite", dbs.Dsn)
|
||||
if err != nil {
|
||||
return fmt.Errorf("db.sqlite error opening database: %w", err)
|
||||
}
|
||||
t.dbMu.Lock()
|
||||
t.db = db
|
||||
t.dbMu.Unlock()
|
||||
<-t.ctx.Done()
|
||||
dbs.dbMu.Lock()
|
||||
dbs.db = db
|
||||
dbs.dbMu.Unlock()
|
||||
<-dbs.ctx.Done()
|
||||
dbs.logger.Debug("done")
|
||||
return nil
|
||||
}
|
||||
|
||||
func (t *DbSqlite) Stop() {
|
||||
if t.cancel != nil {
|
||||
t.cancel()
|
||||
func (dbs *DbSqlite) Stop() {
|
||||
if dbs.cancel != nil {
|
||||
defer dbs.cancel()
|
||||
}
|
||||
t.dbMu.Lock()
|
||||
defer t.dbMu.Unlock()
|
||||
if t.db != nil {
|
||||
t.db.Close()
|
||||
t.db = nil
|
||||
dbs.dbMu.Lock()
|
||||
defer dbs.dbMu.Unlock()
|
||||
if dbs.db != nil {
|
||||
dbs.db.Close()
|
||||
}
|
||||
t.logger.Debug("done")
|
||||
}
|
||||
|
||||
// TODO(jwetzell): get a database module layout that doesn't require handing the DB over
|
||||
func (t *DbSqlite) Database() (*sql.DB, error) {
|
||||
t.dbMu.Lock()
|
||||
defer t.dbMu.Unlock()
|
||||
if t.db == nil {
|
||||
func (dbs *DbSqlite) QueryContext(ctx context.Context, query string, args ...any) (*sql.Rows, error) {
|
||||
dbs.dbMu.Lock()
|
||||
defer dbs.dbMu.Unlock()
|
||||
if dbs.db == nil {
|
||||
return nil, fmt.Errorf("database not initialized")
|
||||
}
|
||||
return t.db, nil
|
||||
return dbs.db.QueryContext(ctx, query, args...)
|
||||
}
|
||||
|
||||
@@ -16,22 +16,49 @@ import (
|
||||
"github.com/jwetzell/showbridge-go/internal/processor"
|
||||
)
|
||||
|
||||
func init() {
|
||||
RegisterModule(ModuleRegistration{
|
||||
Type: "http.server",
|
||||
Title: "HTTP Server",
|
||||
ParamsSchema: &jsonschema.Schema{
|
||||
Type: "object",
|
||||
Properties: map[string]*jsonschema.Schema{
|
||||
"port": {
|
||||
Title: "Port",
|
||||
Description: "the port for the HTTP server to listen on",
|
||||
Type: "integer",
|
||||
Minimum: jsonschema.Ptr[float64](1024),
|
||||
Maximum: jsonschema.Ptr[float64](65535),
|
||||
},
|
||||
},
|
||||
Required: []string{"port"},
|
||||
AdditionalProperties: &jsonschema.Schema{Not: &jsonschema.Schema{}},
|
||||
},
|
||||
New: func(config config.ModuleConfig) (common.Module, error) {
|
||||
params := config.Params
|
||||
portNum, err := params.GetInt("port")
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("http.server port error: %w", err)
|
||||
}
|
||||
return &HTTPServer{Port: uint16(portNum), config: config, logger: CreateLogger(config)}, nil
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
type HTTPServer struct {
|
||||
config config.ModuleConfig
|
||||
Port uint16
|
||||
ctx context.Context
|
||||
router common.RouteIO
|
||||
logger *slog.Logger
|
||||
cancel context.CancelFunc
|
||||
server *http.Server
|
||||
serverMu sync.Mutex
|
||||
config config.ModuleConfig
|
||||
Port uint16
|
||||
ctx context.Context
|
||||
inputHandler common.InputHandler
|
||||
logger *slog.Logger
|
||||
cancel context.CancelFunc
|
||||
server *http.Server
|
||||
serverMu sync.Mutex
|
||||
}
|
||||
|
||||
type ResponseIOError struct {
|
||||
Index int `json:"index"`
|
||||
OutputError *string `json:"outputError"`
|
||||
ProcessError *string `json:"processError"`
|
||||
InputError *string `json:"inputError"`
|
||||
}
|
||||
|
||||
type IOResponseData struct {
|
||||
@@ -57,34 +84,6 @@ func (hsrw *HTTPServerResponseWriter) Write(data []byte) (int, error) {
|
||||
return hsrw.ResponseWriter.Write(data)
|
||||
}
|
||||
|
||||
func init() {
|
||||
RegisterModule(ModuleRegistration{
|
||||
Type: "http.server",
|
||||
Title: "HTTP Server",
|
||||
ParamsSchema: &jsonschema.Schema{
|
||||
Type: "object",
|
||||
Properties: map[string]*jsonschema.Schema{
|
||||
"port": {
|
||||
Title: "Port",
|
||||
Type: "integer",
|
||||
Minimum: jsonschema.Ptr[float64](1024),
|
||||
Maximum: jsonschema.Ptr[float64](65535),
|
||||
},
|
||||
},
|
||||
Required: []string{"port"},
|
||||
AdditionalProperties: &jsonschema.Schema{Not: &jsonschema.Schema{}},
|
||||
},
|
||||
New: func(config config.ModuleConfig) (common.Module, error) {
|
||||
params := config.Params
|
||||
portNum, err := params.GetInt("port")
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("http.server port error: %w", err)
|
||||
}
|
||||
return &HTTPServer{Port: uint16(portNum), config: config, logger: CreateLogger(config)}, nil
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
func (hs *HTTPServer) Id() string {
|
||||
return hs.config.Id
|
||||
}
|
||||
@@ -100,9 +99,9 @@ func (hs *HTTPServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
Message: "routing successful",
|
||||
Status: "ok",
|
||||
}
|
||||
if hs.router != nil {
|
||||
if hs.inputHandler != nil {
|
||||
inputContext := context.WithValue(hs.ctx, httpServerContextKey("responseWriter"), &responseWriter)
|
||||
aRouteFound, routingErrors := hs.router.HandleInput(inputContext, hs.Id(), r)
|
||||
aRouteFound, routingErrors := hs.inputHandler(inputContext, hs.Id(), r)
|
||||
if !responseWriter.done {
|
||||
if aRouteFound {
|
||||
if routingErrors != nil {
|
||||
@@ -116,21 +115,11 @@ func (hs *HTTPServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
Index: responseIOError.Index,
|
||||
}
|
||||
|
||||
if responseIOError.InputError != nil {
|
||||
errorMsg := responseIOError.InputError.Error()
|
||||
errorToAdd.InputError = &errorMsg
|
||||
}
|
||||
|
||||
if responseIOError.ProcessError != nil {
|
||||
errorMsg := responseIOError.ProcessError.Error()
|
||||
errorToAdd.ProcessError = &errorMsg
|
||||
}
|
||||
|
||||
if responseIOError.OutputError != nil {
|
||||
errorMsg := responseIOError.OutputError.Error()
|
||||
errorToAdd.OutputError = &errorMsg
|
||||
}
|
||||
|
||||
response.IOErrors = append(response.IOErrors, errorToAdd)
|
||||
|
||||
}
|
||||
@@ -159,9 +148,9 @@ func (hs *HTTPServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
}
|
||||
|
||||
func (hs *HTTPServer) Start(ctx context.Context, router common.RouteIO) error {
|
||||
func (hs *HTTPServer) Start(ctx context.Context, inputHandler common.InputHandler) error {
|
||||
hs.logger.Debug("running")
|
||||
hs.router = router
|
||||
hs.inputHandler = inputHandler
|
||||
moduleContext, cancel := context.WithCancel(ctx)
|
||||
hs.ctx = moduleContext
|
||||
hs.cancel = cancel
|
||||
@@ -179,12 +168,13 @@ func (hs *HTTPServer) Start(ctx context.Context, router common.RouteIO) error {
|
||||
err := httpServer.ListenAndServe()
|
||||
// TODO(jwetzell): handle server closed error differently
|
||||
if err != nil {
|
||||
if err.Error() != "http: Server closed" {
|
||||
if !errors.Is(err, http.ErrServerClosed) {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
<-hs.ctx.Done()
|
||||
hs.logger.Debug("done")
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -212,7 +202,7 @@ func (hs *HTTPServer) Output(ctx context.Context, payload any) error {
|
||||
|
||||
func (hs *HTTPServer) Stop() {
|
||||
if hs.cancel != nil {
|
||||
hs.cancel()
|
||||
defer hs.cancel()
|
||||
}
|
||||
hs.serverMu.Lock()
|
||||
defer hs.serverMu.Unlock()
|
||||
@@ -221,7 +211,5 @@ func (hs *HTTPServer) Stop() {
|
||||
hs.server.Shutdown(shutdownCtx)
|
||||
shutdownCancel()
|
||||
<-shutdownCtx.Done()
|
||||
hs.server = nil
|
||||
}
|
||||
hs.logger.Debug("done")
|
||||
}
|
||||
|
||||
@@ -14,16 +14,6 @@ import (
|
||||
_ "gitlab.com/gomidi/midi/v2/drivers/rtmididrv"
|
||||
)
|
||||
|
||||
type MIDIInput struct {
|
||||
config config.ModuleConfig
|
||||
ctx context.Context
|
||||
router common.RouteIO
|
||||
Port string
|
||||
logger *slog.Logger
|
||||
cancel context.CancelFunc
|
||||
stop func()
|
||||
}
|
||||
|
||||
func init() {
|
||||
RegisterModule(ModuleRegistration{
|
||||
Type: "midi.input",
|
||||
@@ -32,8 +22,9 @@ func init() {
|
||||
Type: "object",
|
||||
Properties: map[string]*jsonschema.Schema{
|
||||
"port": {
|
||||
Title: "Port",
|
||||
Type: "string",
|
||||
Title: "Port",
|
||||
Description: "the name of the MIDI port to listen to",
|
||||
Type: "string",
|
||||
},
|
||||
},
|
||||
Required: []string{"port"},
|
||||
@@ -51,6 +42,16 @@ func init() {
|
||||
})
|
||||
}
|
||||
|
||||
type MIDIInput struct {
|
||||
config config.ModuleConfig
|
||||
ctx context.Context
|
||||
inputHandler common.InputHandler
|
||||
Port string
|
||||
logger *slog.Logger
|
||||
cancel context.CancelFunc
|
||||
stop func()
|
||||
}
|
||||
|
||||
func (mi *MIDIInput) Id() string {
|
||||
return mi.config.Id
|
||||
}
|
||||
@@ -59,9 +60,9 @@ func (mi *MIDIInput) Type() string {
|
||||
return mi.config.Type
|
||||
}
|
||||
|
||||
func (mi *MIDIInput) Start(ctx context.Context, router common.RouteIO) error {
|
||||
func (mi *MIDIInput) Start(ctx context.Context, inputHandler common.InputHandler) error {
|
||||
mi.logger.Debug("running")
|
||||
mi.router = router
|
||||
mi.inputHandler = inputHandler
|
||||
moduleContext, cancel := context.WithCancel(ctx)
|
||||
mi.ctx = moduleContext
|
||||
mi.cancel = cancel
|
||||
@@ -72,8 +73,8 @@ func (mi *MIDIInput) Start(ctx context.Context, router common.RouteIO) error {
|
||||
}
|
||||
|
||||
stop, err := midi.ListenTo(in, func(msg midi.Message, timestampms int32) {
|
||||
if mi.router != nil {
|
||||
mi.router.HandleInput(mi.ctx, mi.Id(), msg)
|
||||
if mi.inputHandler != nil {
|
||||
mi.inputHandler(mi.ctx, mi.Id(), msg)
|
||||
}
|
||||
}, midi.UseSysEx())
|
||||
|
||||
@@ -83,17 +84,16 @@ func (mi *MIDIInput) Start(ctx context.Context, router common.RouteIO) error {
|
||||
mi.stop = stop
|
||||
|
||||
<-mi.ctx.Done()
|
||||
mi.logger.Debug("done")
|
||||
return nil
|
||||
}
|
||||
|
||||
func (mi *MIDIInput) Stop() {
|
||||
if mi.cancel != nil {
|
||||
mi.cancel()
|
||||
defer mi.cancel()
|
||||
}
|
||||
if mi.stop != nil {
|
||||
mi.stop()
|
||||
mi.stop = nil
|
||||
}
|
||||
midi.CloseDriver()
|
||||
mi.logger.Debug("done")
|
||||
}
|
||||
|
||||
@@ -16,17 +16,6 @@ import (
|
||||
_ "gitlab.com/gomidi/midi/v2/drivers/rtmididrv"
|
||||
)
|
||||
|
||||
type MIDIOutput struct {
|
||||
config config.ModuleConfig
|
||||
ctx context.Context
|
||||
router common.RouteIO
|
||||
Port string
|
||||
sendFunc func(midi.Message) error
|
||||
logger *slog.Logger
|
||||
cancel context.CancelFunc
|
||||
sendFuncMu sync.Mutex
|
||||
}
|
||||
|
||||
func init() {
|
||||
RegisterModule(ModuleRegistration{
|
||||
Type: "midi.output",
|
||||
@@ -35,8 +24,9 @@ func init() {
|
||||
Type: "object",
|
||||
Properties: map[string]*jsonschema.Schema{
|
||||
"port": {
|
||||
Title: "Port",
|
||||
Type: "string",
|
||||
Title: "Port",
|
||||
Description: "the name of the MIDI port to send messages to",
|
||||
Type: "string",
|
||||
},
|
||||
},
|
||||
Required: []string{"port"},
|
||||
@@ -55,6 +45,17 @@ func init() {
|
||||
})
|
||||
}
|
||||
|
||||
type MIDIOutput struct {
|
||||
config config.ModuleConfig
|
||||
ctx context.Context
|
||||
inputHandler common.InputHandler
|
||||
Port string
|
||||
sendFunc func(midi.Message) error
|
||||
logger *slog.Logger
|
||||
cancel context.CancelFunc
|
||||
sendFuncMu sync.Mutex
|
||||
}
|
||||
|
||||
func (mo *MIDIOutput) Id() string {
|
||||
return mo.config.Id
|
||||
}
|
||||
@@ -63,9 +64,9 @@ func (mo *MIDIOutput) Type() string {
|
||||
return mo.config.Type
|
||||
}
|
||||
|
||||
func (mo *MIDIOutput) Start(ctx context.Context, router common.RouteIO) error {
|
||||
func (mo *MIDIOutput) Start(ctx context.Context, inputHandler common.InputHandler) error {
|
||||
mo.logger.Debug("running")
|
||||
mo.router = router
|
||||
mo.inputHandler = inputHandler
|
||||
moduleContext, cancel := context.WithCancel(ctx)
|
||||
mo.ctx = moduleContext
|
||||
mo.cancel = cancel
|
||||
@@ -86,6 +87,7 @@ func (mo *MIDIOutput) Start(ctx context.Context, router common.RouteIO) error {
|
||||
mo.sendFuncMu.Unlock()
|
||||
|
||||
<-mo.ctx.Done()
|
||||
mo.logger.Debug("done")
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -107,8 +109,7 @@ func (mo *MIDIOutput) Output(ctx context.Context, payload any) error {
|
||||
|
||||
func (mo *MIDIOutput) Stop() {
|
||||
if mo.cancel != nil {
|
||||
mo.cancel()
|
||||
defer mo.cancel()
|
||||
}
|
||||
midi.CloseDriver()
|
||||
mo.logger.Debug("done")
|
||||
}
|
||||
|
||||
@@ -30,15 +30,36 @@ func RegisterModule(mod ModuleRegistration) {
|
||||
moduleRegistryMu.Lock()
|
||||
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))
|
||||
}
|
||||
ModuleRegistry[string(mod.Type)] = mod
|
||||
moduleRegistry[string(mod.Type)] = mod
|
||||
}
|
||||
|
||||
type ModuleRegistry map[string]ModuleRegistration
|
||||
|
||||
func GetModuleRegistration(moduleType string) (ModuleRegistration, bool) {
|
||||
moduleRegistryMu.RLock()
|
||||
defer moduleRegistryMu.RUnlock()
|
||||
|
||||
mod, ok := moduleRegistry[moduleType]
|
||||
return mod, ok
|
||||
}
|
||||
|
||||
func GetModuleRegistrations() []ModuleRegistration {
|
||||
moduleRegistryMu.RLock()
|
||||
defer moduleRegistryMu.RUnlock()
|
||||
|
||||
registrations := make([]ModuleRegistration, 0, len(moduleRegistry))
|
||||
for _, mod := range moduleRegistry {
|
||||
registrations = append(registrations, mod)
|
||||
}
|
||||
return registrations
|
||||
}
|
||||
|
||||
var (
|
||||
moduleRegistryMu sync.RWMutex
|
||||
ModuleRegistry = make(map[string]ModuleRegistration)
|
||||
moduleRegistry = make(ModuleRegistry)
|
||||
)
|
||||
|
||||
func CreateLogger(config config.ModuleConfig) *slog.Logger {
|
||||
|
||||
@@ -2,6 +2,7 @@ package module
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
@@ -13,19 +14,6 @@ import (
|
||||
"github.com/jwetzell/showbridge-go/internal/config"
|
||||
)
|
||||
|
||||
type MQTTClient struct {
|
||||
config config.ModuleConfig
|
||||
ctx context.Context
|
||||
router common.RouteIO
|
||||
Broker string
|
||||
ClientID string
|
||||
Topic string
|
||||
client mqtt.Client
|
||||
logger *slog.Logger
|
||||
cancel context.CancelFunc
|
||||
clientMu sync.Mutex
|
||||
}
|
||||
|
||||
func init() {
|
||||
RegisterModule(ModuleRegistration{
|
||||
Type: "mqtt.client",
|
||||
@@ -34,23 +22,40 @@ func init() {
|
||||
Type: "object",
|
||||
Properties: map[string]*jsonschema.Schema{
|
||||
"broker": {
|
||||
Title: "Broker URL",
|
||||
Type: "string",
|
||||
Title: "Broker URL",
|
||||
Description: "the URL of the MQTT broker to connect to",
|
||||
Type: "string",
|
||||
},
|
||||
"topic": {
|
||||
Title: "Topic",
|
||||
Type: "string",
|
||||
Title: "Topic",
|
||||
Description: "an MQTT topic to subscribe to",
|
||||
Type: "string",
|
||||
},
|
||||
"clientId": {
|
||||
Title: "Client ID",
|
||||
Type: "string",
|
||||
Title: "Client ID",
|
||||
Description: "the client ID to use when connecting to the MQTT broker",
|
||||
Type: "string",
|
||||
},
|
||||
"qos": {
|
||||
Title: "QoS",
|
||||
Description: "the QoS level to use when publishing messages",
|
||||
Type: "integer",
|
||||
Minimum: jsonschema.Ptr[float64](0),
|
||||
Maximum: jsonschema.Ptr[float64](2),
|
||||
Default: json.RawMessage(`0`),
|
||||
},
|
||||
"retained": {
|
||||
Title: "Retained",
|
||||
Description: "whether to set the retained flag when publishing messages",
|
||||
Type: "boolean",
|
||||
Default: json.RawMessage(`false`),
|
||||
},
|
||||
},
|
||||
Required: []string{"broker", "topic", "clientId"},
|
||||
AdditionalProperties: &jsonschema.Schema{Not: &jsonschema.Schema{}},
|
||||
},
|
||||
New: func(config config.ModuleConfig) (common.Module, error) {
|
||||
params := config.Params
|
||||
New: func(moduleConfig config.ModuleConfig) (common.Module, error) {
|
||||
params := moduleConfig.Params
|
||||
brokerString, err := params.GetString("broker")
|
||||
|
||||
if err != nil {
|
||||
@@ -69,11 +74,46 @@ func init() {
|
||||
return nil, fmt.Errorf("mqtt.client clientId error: %w", err)
|
||||
}
|
||||
|
||||
return &MQTTClient{config: config, Broker: brokerString, Topic: topicString, ClientID: clientIdString, logger: CreateLogger(config)}, nil
|
||||
qosString, err := params.GetInt("qos")
|
||||
|
||||
if err != nil {
|
||||
if errors.Is(err, config.ErrParamNotFound) {
|
||||
qosString = 0
|
||||
} else {
|
||||
return nil, fmt.Errorf("mqtt.client qos error: %w", err)
|
||||
}
|
||||
}
|
||||
|
||||
retainedBool, err := params.GetBool("retained")
|
||||
|
||||
if err != nil {
|
||||
if errors.Is(err, config.ErrParamNotFound) {
|
||||
retainedBool = false
|
||||
} else {
|
||||
return nil, fmt.Errorf("mqtt.client retained error: %w", err)
|
||||
}
|
||||
}
|
||||
|
||||
return &MQTTClient{config: moduleConfig, Broker: brokerString, Topic: topicString, ClientID: clientIdString, QoS: byte(qosString), Retained: retainedBool, logger: CreateLogger(moduleConfig)}, nil
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
type MQTTClient struct {
|
||||
config config.ModuleConfig
|
||||
ctx context.Context
|
||||
inputHandler common.InputHandler
|
||||
Broker string
|
||||
ClientID string
|
||||
Topic string
|
||||
QoS byte
|
||||
Retained bool
|
||||
client mqtt.Client
|
||||
logger *slog.Logger
|
||||
cancel context.CancelFunc
|
||||
clientMu sync.Mutex
|
||||
}
|
||||
|
||||
func (mc *MQTTClient) Id() string {
|
||||
return mc.config.Id
|
||||
}
|
||||
@@ -82,9 +122,9 @@ func (mc *MQTTClient) Type() string {
|
||||
return mc.config.Type
|
||||
}
|
||||
|
||||
func (mc *MQTTClient) Start(ctx context.Context, router common.RouteIO) error {
|
||||
func (mc *MQTTClient) Start(ctx context.Context, inputHandler common.InputHandler) error {
|
||||
mc.logger.Debug("running")
|
||||
mc.router = router
|
||||
mc.inputHandler = inputHandler
|
||||
moduleContext, cancel := context.WithCancel(ctx)
|
||||
mc.ctx = moduleContext
|
||||
mc.cancel = cancel
|
||||
@@ -97,7 +137,9 @@ func (mc *MQTTClient) Start(ctx context.Context, router common.RouteIO) error {
|
||||
|
||||
opts.OnConnect = func(c mqtt.Client) {
|
||||
token := mc.client.Subscribe(mc.Topic, 1, func(c mqtt.Client, m mqtt.Message) {
|
||||
mc.router.HandleInput(mc.ctx, mc.Id(), m)
|
||||
if mc.inputHandler != nil {
|
||||
mc.inputHandler(mc.ctx, mc.Id(), m)
|
||||
}
|
||||
})
|
||||
token.Wait()
|
||||
}
|
||||
@@ -115,14 +157,19 @@ func (mc *MQTTClient) Start(ctx context.Context, router common.RouteIO) error {
|
||||
mc.clientMu.Unlock()
|
||||
|
||||
<-mc.ctx.Done()
|
||||
mc.logger.Debug("done")
|
||||
return nil
|
||||
}
|
||||
|
||||
func (mc *MQTTClient) Output(ctx context.Context, payload any) error {
|
||||
payloadMessage, ok := common.GetAnyAs[mqtt.Message](payload)
|
||||
func (mc *MQTTClient) Publish(ctx context.Context, topic string, payload any) error {
|
||||
payloadBytes, ok := common.GetAnyAsByteSlice(payload)
|
||||
|
||||
if !ok {
|
||||
return errors.New("mqtt.client is only able to output a MQTTMessage")
|
||||
payloadString, ok := common.GetAnyAs[string](payload)
|
||||
if !ok {
|
||||
return errors.New("mqtt.client is only able to publish bytes or string")
|
||||
}
|
||||
payloadBytes = []byte(payloadString)
|
||||
}
|
||||
|
||||
if mc.client == nil {
|
||||
@@ -133,7 +180,7 @@ func (mc *MQTTClient) Output(ctx context.Context, payload any) error {
|
||||
return errors.New("mqtt.client is not connected")
|
||||
}
|
||||
|
||||
token := mc.client.Publish(payloadMessage.Topic(), payloadMessage.Qos(), payloadMessage.Retained(), payloadMessage.Payload())
|
||||
token := mc.client.Publish(topic, mc.QoS, mc.Retained, payloadBytes)
|
||||
|
||||
token.Wait()
|
||||
|
||||
@@ -148,7 +195,5 @@ func (mc *MQTTClient) Stop() {
|
||||
defer mc.clientMu.Unlock()
|
||||
if mc.client != nil {
|
||||
mc.client.Disconnect(250)
|
||||
mc.client = nil
|
||||
}
|
||||
mc.logger.Debug("done")
|
||||
}
|
||||
|
||||
@@ -9,24 +9,9 @@ import (
|
||||
"github.com/google/jsonschema-go/jsonschema"
|
||||
"github.com/jwetzell/showbridge-go/internal/common"
|
||||
"github.com/jwetzell/showbridge-go/internal/config"
|
||||
"github.com/jwetzell/showbridge-go/internal/processor"
|
||||
"github.com/nats-io/nats.go"
|
||||
)
|
||||
|
||||
type NATSClient struct {
|
||||
config config.ModuleConfig
|
||||
ctx context.Context
|
||||
router common.RouteIO
|
||||
URL string
|
||||
Subject string
|
||||
client *nats.Conn
|
||||
logger *slog.Logger
|
||||
cancel context.CancelFunc
|
||||
sub *nats.Subscription
|
||||
subMu sync.Mutex
|
||||
clientMu sync.Mutex
|
||||
}
|
||||
|
||||
func init() {
|
||||
RegisterModule(ModuleRegistration{
|
||||
Type: "nats.client",
|
||||
@@ -35,12 +20,14 @@ func init() {
|
||||
Type: "object",
|
||||
Properties: map[string]*jsonschema.Schema{
|
||||
"url": {
|
||||
Title: "NATS Server URL",
|
||||
Type: "string",
|
||||
Title: "NATS Server URL",
|
||||
Description: "the URL of the NATS server to connect to",
|
||||
Type: "string",
|
||||
},
|
||||
"subject": {
|
||||
Title: "Subject",
|
||||
Type: "string",
|
||||
Title: "Subject",
|
||||
Description: "the subject to subscribe to",
|
||||
Type: "string",
|
||||
},
|
||||
},
|
||||
Required: []string{"url", "subject"},
|
||||
@@ -64,6 +51,20 @@ func init() {
|
||||
})
|
||||
}
|
||||
|
||||
type NATSClient struct {
|
||||
config config.ModuleConfig
|
||||
ctx context.Context
|
||||
inputHandler common.InputHandler
|
||||
URL string
|
||||
Subject string
|
||||
client *nats.Conn
|
||||
logger *slog.Logger
|
||||
cancel context.CancelFunc
|
||||
sub *nats.Subscription
|
||||
subMu sync.Mutex
|
||||
clientMu sync.Mutex
|
||||
}
|
||||
|
||||
func (nc *NATSClient) Id() string {
|
||||
return nc.config.Id
|
||||
}
|
||||
@@ -72,9 +73,9 @@ func (nc *NATSClient) Type() string {
|
||||
return nc.config.Type
|
||||
}
|
||||
|
||||
func (nc *NATSClient) Start(ctx context.Context, router common.RouteIO) error {
|
||||
func (nc *NATSClient) Start(ctx context.Context, inputHandler common.InputHandler) error {
|
||||
nc.logger.Debug("running")
|
||||
nc.router = router
|
||||
nc.inputHandler = inputHandler
|
||||
moduleContext, cancel := context.WithCancel(ctx)
|
||||
nc.ctx = moduleContext
|
||||
nc.cancel = cancel
|
||||
@@ -90,8 +91,8 @@ func (nc *NATSClient) Start(ctx context.Context, router common.RouteIO) error {
|
||||
nc.clientMu.Unlock()
|
||||
|
||||
sub, err := nc.client.Subscribe(nc.Subject, func(msg *nats.Msg) {
|
||||
if nc.router != nil {
|
||||
nc.router.HandleInput(nc.ctx, nc.Id(), msg)
|
||||
if nc.inputHandler != nil {
|
||||
nc.inputHandler(nc.ctx, nc.Id(), msg)
|
||||
}
|
||||
})
|
||||
|
||||
@@ -103,15 +104,20 @@ func (nc *NATSClient) Start(ctx context.Context, router common.RouteIO) error {
|
||||
nc.subMu.Unlock()
|
||||
|
||||
<-nc.ctx.Done()
|
||||
nc.logger.Debug("done")
|
||||
return nil
|
||||
}
|
||||
|
||||
func (nc *NATSClient) Output(ctx context.Context, payload any) error {
|
||||
func (nc *NATSClient) Publish(ctx context.Context, topic string, payload any) error {
|
||||
|
||||
payloadMessage, ok := common.GetAnyAs[processor.NATSMessage](payload)
|
||||
payloadBytes, ok := common.GetAnyAsByteSlice(payload)
|
||||
|
||||
if !ok {
|
||||
return errors.New("nats.client is only able to output NATSMessage")
|
||||
payloadString, ok := common.GetAnyAs[string](payload)
|
||||
if !ok {
|
||||
return errors.New("nats.client is only able to publish bytes or string")
|
||||
}
|
||||
payloadBytes = []byte(payloadString)
|
||||
}
|
||||
|
||||
nc.clientMu.Lock()
|
||||
@@ -125,20 +131,19 @@ func (nc *NATSClient) Output(ctx context.Context, payload any) error {
|
||||
return errors.New("nats.client is not connected")
|
||||
}
|
||||
|
||||
err := nc.client.Publish(payloadMessage.Subject, payloadMessage.Payload)
|
||||
err := nc.client.Publish(topic, payloadBytes)
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
func (nc *NATSClient) Stop() {
|
||||
if nc.cancel != nil {
|
||||
nc.cancel()
|
||||
defer nc.cancel()
|
||||
}
|
||||
nc.subMu.Lock()
|
||||
defer nc.subMu.Unlock()
|
||||
if nc.sub != nil {
|
||||
nc.sub.Unsubscribe()
|
||||
nc.sub = nil
|
||||
}
|
||||
|
||||
nc.clientMu.Lock()
|
||||
@@ -147,7 +152,5 @@ func (nc *NATSClient) Stop() {
|
||||
nc.client.Drain()
|
||||
// TODO(jwetzell): setup closed callback to get when client is fully closed
|
||||
nc.client.Close()
|
||||
nc.client = nil
|
||||
}
|
||||
nc.logger.Debug("done")
|
||||
}
|
||||
|
||||
@@ -16,18 +16,6 @@ import (
|
||||
"github.com/nats-io/nats-server/v2/server"
|
||||
)
|
||||
|
||||
type NATSServer struct {
|
||||
config config.ModuleConfig
|
||||
ctx context.Context
|
||||
Ip string
|
||||
Port int
|
||||
router common.RouteIO
|
||||
server *server.Server
|
||||
logger *slog.Logger
|
||||
cancel context.CancelFunc
|
||||
serverMu sync.Mutex
|
||||
}
|
||||
|
||||
func init() {
|
||||
RegisterModule(ModuleRegistration{
|
||||
Type: "nats.server",
|
||||
@@ -36,16 +24,18 @@ func init() {
|
||||
Type: "object",
|
||||
Properties: map[string]*jsonschema.Schema{
|
||||
"ip": {
|
||||
Title: "IP",
|
||||
Type: "string",
|
||||
Default: json.RawMessage(`"0.0.0.0"`),
|
||||
Title: "IP",
|
||||
Description: "the IP address to bind the NATS server to",
|
||||
Type: "string",
|
||||
Default: json.RawMessage(`"0.0.0.0"`),
|
||||
},
|
||||
"port": {
|
||||
Title: "Port",
|
||||
Type: "integer",
|
||||
Minimum: jsonschema.Ptr[float64](1024),
|
||||
Maximum: jsonschema.Ptr[float64](65535),
|
||||
Default: json.RawMessage(`4222`),
|
||||
Title: "Port",
|
||||
Description: "the port for the NATS server to listen on",
|
||||
Type: "integer",
|
||||
Minimum: jsonschema.Ptr[float64](1024),
|
||||
Maximum: jsonschema.Ptr[float64](65535),
|
||||
Default: json.RawMessage(`4222`),
|
||||
},
|
||||
},
|
||||
Required: []string{},
|
||||
@@ -80,6 +70,18 @@ func init() {
|
||||
})
|
||||
}
|
||||
|
||||
type NATSServer struct {
|
||||
config config.ModuleConfig
|
||||
ctx context.Context
|
||||
Ip string
|
||||
Port int
|
||||
inputHandler common.InputHandler
|
||||
server *server.Server
|
||||
logger *slog.Logger
|
||||
cancel context.CancelFunc
|
||||
serverMu sync.Mutex
|
||||
}
|
||||
|
||||
func (ns *NATSServer) Id() string {
|
||||
return ns.config.Id
|
||||
}
|
||||
@@ -88,17 +90,18 @@ func (ns *NATSServer) Type() string {
|
||||
return ns.config.Type
|
||||
}
|
||||
|
||||
func (ns *NATSServer) Start(ctx context.Context, router common.RouteIO) error {
|
||||
func (ns *NATSServer) Start(ctx context.Context, inputHandler common.InputHandler) error {
|
||||
ns.logger.Debug("running")
|
||||
ns.router = router
|
||||
ns.inputHandler = inputHandler
|
||||
moduleContext, cancel := context.WithCancel(ctx)
|
||||
ns.ctx = moduleContext
|
||||
ns.cancel = cancel
|
||||
|
||||
natsServer, err := server.NewServer(&server.Options{
|
||||
Host: ns.Ip,
|
||||
Port: ns.Port,
|
||||
NoLog: true,
|
||||
Host: ns.Ip,
|
||||
Port: ns.Port,
|
||||
NoLog: true,
|
||||
NoSigs: true,
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
@@ -107,27 +110,26 @@ func (ns *NATSServer) Start(ctx context.Context, router common.RouteIO) error {
|
||||
|
||||
ns.serverMu.Lock()
|
||||
ns.server = natsServer
|
||||
defer ns.serverMu.Unlock()
|
||||
natsServer.Start()
|
||||
|
||||
if !natsServer.ReadyForConnections(5 * time.Second) {
|
||||
return errors.New("nats.server failed to start")
|
||||
}
|
||||
ns.logger.Info("NATS server started", "client_url", natsServer.ClientURL())
|
||||
ns.serverMu.Unlock()
|
||||
|
||||
<-ns.ctx.Done()
|
||||
|
||||
ns.logger.Debug("done")
|
||||
return nil
|
||||
}
|
||||
|
||||
func (ns *NATSServer) Stop() {
|
||||
if ns.cancel != nil {
|
||||
ns.cancel()
|
||||
defer ns.cancel()
|
||||
}
|
||||
ns.serverMu.Lock()
|
||||
defer ns.serverMu.Unlock()
|
||||
if ns.server != nil {
|
||||
ns.server.Shutdown()
|
||||
ns.server.WaitForShutdown()
|
||||
}
|
||||
ns.logger.Debug("done")
|
||||
}
|
||||
|
||||
@@ -12,17 +12,6 @@ import (
|
||||
"github.com/jwetzell/showbridge-go/internal/config"
|
||||
)
|
||||
|
||||
type PSNClient struct {
|
||||
config config.ModuleConfig
|
||||
conn *net.UDPConn
|
||||
ctx context.Context
|
||||
router common.RouteIO
|
||||
decoder *psn.Decoder
|
||||
logger *slog.Logger
|
||||
cancel context.CancelFunc
|
||||
connMu sync.Mutex
|
||||
}
|
||||
|
||||
func init() {
|
||||
RegisterModule(ModuleRegistration{
|
||||
Type: "psn.client",
|
||||
@@ -33,6 +22,17 @@ func init() {
|
||||
})
|
||||
}
|
||||
|
||||
type PSNClient struct {
|
||||
config config.ModuleConfig
|
||||
conn *net.UDPConn
|
||||
ctx context.Context
|
||||
inputHandler common.InputHandler
|
||||
decoder *psn.Decoder
|
||||
logger *slog.Logger
|
||||
cancel context.CancelFunc
|
||||
connMu sync.Mutex
|
||||
}
|
||||
|
||||
func (pc *PSNClient) Id() string {
|
||||
return pc.config.Id
|
||||
}
|
||||
@@ -41,9 +41,9 @@ func (pc *PSNClient) Type() string {
|
||||
return pc.config.Type
|
||||
}
|
||||
|
||||
func (pc *PSNClient) Start(ctx context.Context, router common.RouteIO) error {
|
||||
func (pc *PSNClient) Start(ctx context.Context, inputHandler common.InputHandler) error {
|
||||
pc.logger.Debug("running")
|
||||
pc.router = router
|
||||
pc.inputHandler = inputHandler
|
||||
moduleContext, cancel := context.WithCancel(ctx)
|
||||
pc.ctx = moduleContext
|
||||
pc.cancel = cancel
|
||||
@@ -63,7 +63,7 @@ func (pc *PSNClient) Start(ctx context.Context, router common.RouteIO) error {
|
||||
pc.connMu.Unlock()
|
||||
|
||||
buffer := make([]byte, 2048)
|
||||
for {
|
||||
for pc.ctx.Err() == nil {
|
||||
select {
|
||||
case <-pc.ctx.Done():
|
||||
return nil
|
||||
@@ -88,28 +88,29 @@ func (pc *PSNClient) Start(ctx context.Context, router common.RouteIO) error {
|
||||
pc.logger.Error("problem decoding psn traffic", "error", err)
|
||||
}
|
||||
|
||||
if pc.router != nil {
|
||||
if pc.inputHandler != nil {
|
||||
// TODO(jwetzell): better input handling
|
||||
for _, tracker := range pc.decoder.Trackers {
|
||||
pc.router.HandleInput(pc.ctx, pc.Id(), tracker)
|
||||
pc.inputHandler(pc.ctx, pc.Id(), tracker)
|
||||
}
|
||||
} else {
|
||||
pc.logger.Error("has no router")
|
||||
pc.logger.Error("has no input handler")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
<-pc.ctx.Done()
|
||||
pc.logger.Debug("done")
|
||||
return nil
|
||||
}
|
||||
|
||||
func (pc *PSNClient) Stop() {
|
||||
if pc.cancel != nil {
|
||||
pc.cancel()
|
||||
defer pc.cancel()
|
||||
}
|
||||
pc.connMu.Lock()
|
||||
defer pc.connMu.Unlock()
|
||||
if pc.conn != nil {
|
||||
pc.conn.Close()
|
||||
pc.conn = nil
|
||||
}
|
||||
pc.logger.Debug("done")
|
||||
}
|
||||
|
||||
@@ -13,18 +13,6 @@ import (
|
||||
"github.com/redis/go-redis/v9"
|
||||
)
|
||||
|
||||
type RedisClient struct {
|
||||
config config.ModuleConfig
|
||||
ctx context.Context
|
||||
router common.RouteIO
|
||||
Host string
|
||||
Port uint16
|
||||
client *redis.Client
|
||||
logger *slog.Logger
|
||||
cancel context.CancelFunc
|
||||
clientMu sync.Mutex
|
||||
}
|
||||
|
||||
func init() {
|
||||
RegisterModule(ModuleRegistration{
|
||||
Type: "redis.client",
|
||||
@@ -33,12 +21,16 @@ func init() {
|
||||
Type: "object",
|
||||
Properties: map[string]*jsonschema.Schema{
|
||||
"host": {
|
||||
Type: "string",
|
||||
Title: "Host",
|
||||
Description: "the hostname or IP address of the Redis server to connect to",
|
||||
Type: "string",
|
||||
},
|
||||
"port": {
|
||||
Type: "integer",
|
||||
Minimum: jsonschema.Ptr[float64](1),
|
||||
Maximum: jsonschema.Ptr[float64](65535),
|
||||
Title: "Port",
|
||||
Description: "the port to use when connecting to the Redis server",
|
||||
Type: "integer",
|
||||
Minimum: jsonschema.Ptr[float64](1),
|
||||
Maximum: jsonschema.Ptr[float64](65535),
|
||||
},
|
||||
},
|
||||
Required: []string{"host", "port"},
|
||||
@@ -62,6 +54,18 @@ func init() {
|
||||
})
|
||||
}
|
||||
|
||||
type RedisClient struct {
|
||||
config config.ModuleConfig
|
||||
ctx context.Context
|
||||
inputHandler common.InputHandler
|
||||
Host string
|
||||
Port uint16
|
||||
client *redis.Client
|
||||
logger *slog.Logger
|
||||
cancel context.CancelFunc
|
||||
clientMu sync.Mutex
|
||||
}
|
||||
|
||||
func (rc *RedisClient) Id() string {
|
||||
return rc.config.Id
|
||||
}
|
||||
@@ -75,10 +79,10 @@ func (rc *RedisClient) Printf(ctx context.Context, format string, v ...any) {
|
||||
rc.logger.Debug(msg)
|
||||
}
|
||||
|
||||
func (rc *RedisClient) Start(ctx context.Context, router common.RouteIO) error {
|
||||
func (rc *RedisClient) Start(ctx context.Context, inputHandler common.InputHandler) error {
|
||||
redis.SetLogger(rc)
|
||||
rc.logger.Debug("running")
|
||||
rc.router = router
|
||||
rc.inputHandler = inputHandler
|
||||
moduleContext, cancel := context.WithCancel(ctx)
|
||||
rc.ctx = moduleContext
|
||||
rc.cancel = cancel
|
||||
@@ -94,25 +98,24 @@ func (rc *RedisClient) Start(ctx context.Context, router common.RouteIO) error {
|
||||
rc.clientMu.Unlock()
|
||||
|
||||
<-rc.ctx.Done()
|
||||
rc.logger.Debug("done")
|
||||
return nil
|
||||
}
|
||||
|
||||
func (rc *RedisClient) Stop() {
|
||||
if rc.cancel != nil {
|
||||
rc.cancel()
|
||||
defer rc.cancel()
|
||||
}
|
||||
rc.clientMu.Lock()
|
||||
defer rc.clientMu.Unlock()
|
||||
if rc.client != nil {
|
||||
rc.client.Close()
|
||||
rc.client = nil
|
||||
}
|
||||
rc.logger.Debug("done")
|
||||
}
|
||||
|
||||
func (rc *RedisClient) Get(key string) (any, error) {
|
||||
func (rc *RedisClient) Get(ctx context.Context, key string) (any, error) {
|
||||
if rc.client != nil {
|
||||
val, err := rc.client.Get(rc.ctx, key).Result()
|
||||
val, err := rc.client.Get(ctx, key).Result()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -121,9 +124,9 @@ func (rc *RedisClient) Get(key string) (any, error) {
|
||||
return nil, errors.New("redis.client not setup")
|
||||
}
|
||||
|
||||
func (rc *RedisClient) Set(key string, value any) error {
|
||||
func (rc *RedisClient) Set(ctx context.Context, key string, value any) error {
|
||||
if rc.client != nil {
|
||||
status := rc.client.Set(rc.ctx, key, value, 0)
|
||||
status := rc.client.Set(ctx, key, value, 0)
|
||||
return status.Err()
|
||||
}
|
||||
return errors.New("redis.client not setup")
|
||||
|
||||
@@ -17,19 +17,6 @@ import (
|
||||
"go.bug.st/serial"
|
||||
)
|
||||
|
||||
type SerialClient struct {
|
||||
config config.ModuleConfig
|
||||
ctx context.Context
|
||||
router common.RouteIO
|
||||
Port string
|
||||
Framer framer.Framer
|
||||
Mode *serial.Mode
|
||||
port serial.Port
|
||||
logger *slog.Logger
|
||||
cancel context.CancelFunc
|
||||
portMu sync.Mutex
|
||||
}
|
||||
|
||||
func init() {
|
||||
RegisterModule(ModuleRegistration{
|
||||
Type: "serial.client",
|
||||
@@ -38,17 +25,20 @@ func init() {
|
||||
Type: "object",
|
||||
Properties: map[string]*jsonschema.Schema{
|
||||
"port": {
|
||||
Title: "Port",
|
||||
Type: "string",
|
||||
Title: "Port",
|
||||
Description: "the name of the serial port to connect to",
|
||||
Type: "string",
|
||||
},
|
||||
"baudRate": {
|
||||
Title: "Baud Rate",
|
||||
Type: "integer",
|
||||
Title: "Baud Rate",
|
||||
Description: "the baud rate to use when connecting to the serial port",
|
||||
Type: "integer",
|
||||
},
|
||||
"framing": {
|
||||
Title: "Framing Method",
|
||||
Type: "string",
|
||||
Enum: []any{"LF", "CR", "CRLF", "SLIP", "RAW"},
|
||||
Title: "Framing Method",
|
||||
Description: "the method to use for framing messages on the serial port",
|
||||
Type: "string",
|
||||
Enum: []any{"LF", "CR", "CRLF", "SLIP", "RAW"},
|
||||
},
|
||||
},
|
||||
Required: []string{"port", "baudRate", "framing"},
|
||||
@@ -66,12 +56,14 @@ func init() {
|
||||
return nil, fmt.Errorf("serial.client framing error: %w", err)
|
||||
}
|
||||
|
||||
framer := framer.GetFramer(framingMethodString)
|
||||
inFramer := framer.GetFramer(framingMethodString)
|
||||
|
||||
if framer == nil {
|
||||
if inFramer == nil {
|
||||
return nil, fmt.Errorf("serial.client unknown framing method: %s", framingMethodString)
|
||||
}
|
||||
|
||||
outFramer := framer.GetFramer(framingMethodString)
|
||||
|
||||
baudRateInt, err := params.GetInt("baudRate")
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("serial.client baudRate error: %w", err)
|
||||
@@ -81,11 +73,25 @@ func init() {
|
||||
BaudRate: baudRateInt,
|
||||
}
|
||||
|
||||
return &SerialClient{config: config, Port: portString, Framer: framer, Mode: &mode, logger: CreateLogger(config)}, nil
|
||||
return &SerialClient{config: config, Port: portString, inFramer: inFramer, outFramer: outFramer, Mode: &mode, logger: CreateLogger(config)}, nil
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
type SerialClient struct {
|
||||
config config.ModuleConfig
|
||||
ctx context.Context
|
||||
inputHandler common.InputHandler
|
||||
Port string
|
||||
inFramer framer.Framer
|
||||
outFramer framer.Framer
|
||||
Mode *serial.Mode
|
||||
port serial.Port
|
||||
logger *slog.Logger
|
||||
cancel context.CancelFunc
|
||||
portMu sync.Mutex
|
||||
}
|
||||
|
||||
func (sc *SerialClient) Id() string {
|
||||
return sc.config.Id
|
||||
}
|
||||
@@ -107,9 +113,9 @@ func (sc *SerialClient) SetupPort() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (sc *SerialClient) Start(ctx context.Context, router common.RouteIO) error {
|
||||
func (sc *SerialClient) Start(ctx context.Context, inputHandler common.InputHandler) error {
|
||||
sc.logger.Debug("running")
|
||||
sc.router = router
|
||||
sc.inputHandler = inputHandler
|
||||
moduleContext, cancel := context.WithCancel(ctx)
|
||||
sc.ctx = moduleContext
|
||||
sc.cancel = cancel
|
||||
@@ -139,16 +145,16 @@ func (sc *SerialClient) Start(ctx context.Context, router common.RouteIO) error
|
||||
byteCount, err := sc.port.Read(buffer)
|
||||
|
||||
if err != nil {
|
||||
sc.Framer.Clear()
|
||||
sc.inFramer.Clear()
|
||||
break READ
|
||||
}
|
||||
|
||||
if sc.Framer != nil {
|
||||
if sc.inFramer != nil {
|
||||
if byteCount > 0 {
|
||||
messages := sc.Framer.Decode(buffer[0:byteCount])
|
||||
messages := sc.inFramer.Decode(buffer[0:byteCount])
|
||||
for _, message := range messages {
|
||||
if sc.router != nil {
|
||||
sc.router.HandleInput(sc.ctx, sc.Id(), message)
|
||||
if sc.inputHandler != nil {
|
||||
sc.inputHandler(sc.ctx, sc.Id(), message)
|
||||
} else {
|
||||
sc.logger.Error("input received but no router is configured")
|
||||
}
|
||||
@@ -159,6 +165,8 @@ func (sc *SerialClient) Start(ctx context.Context, router common.RouteIO) error
|
||||
}
|
||||
}
|
||||
}
|
||||
<-sc.ctx.Done()
|
||||
sc.logger.Debug("done")
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -170,19 +178,25 @@ func (sc *SerialClient) Output(ctx context.Context, payload any) error {
|
||||
return errors.New("serial.client can only output bytes")
|
||||
}
|
||||
|
||||
_, err := sc.port.Write(sc.Framer.Encode(payloadBytes))
|
||||
_, err := sc.port.Write(sc.outFramer.Encode(payloadBytes))
|
||||
return err
|
||||
}
|
||||
|
||||
func (sc *SerialClient) Stop() {
|
||||
if sc.cancel != nil {
|
||||
sc.cancel()
|
||||
defer sc.cancel()
|
||||
}
|
||||
sc.portMu.Lock()
|
||||
defer sc.portMu.Unlock()
|
||||
if sc.port != nil {
|
||||
sc.port.Close()
|
||||
sc.port = nil
|
||||
}
|
||||
sc.logger.Debug("done")
|
||||
|
||||
if sc.inFramer != nil {
|
||||
sc.inFramer.Clear()
|
||||
}
|
||||
|
||||
if sc.outFramer != nil {
|
||||
sc.outFramer.Clear()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -21,31 +21,6 @@ import (
|
||||
"github.com/jwetzell/showbridge-go/internal/processor"
|
||||
)
|
||||
|
||||
type SIPCallServer struct {
|
||||
config config.ModuleConfig
|
||||
ctx context.Context
|
||||
router common.RouteIO
|
||||
IP string
|
||||
Port int
|
||||
Transport string
|
||||
UserAgent string
|
||||
logger *slog.Logger
|
||||
cancel context.CancelFunc
|
||||
ua *sipgo.UserAgent
|
||||
uaMu sync.Mutex
|
||||
}
|
||||
|
||||
type SIPCallMessage struct {
|
||||
To string
|
||||
}
|
||||
|
||||
type SIPCall struct {
|
||||
inDialog *diago.DialogServerSession
|
||||
lock sync.Mutex
|
||||
}
|
||||
|
||||
type sipCallContextKey string
|
||||
|
||||
func init() {
|
||||
RegisterModule(ModuleRegistration{
|
||||
Type: "sip.call.server",
|
||||
@@ -54,27 +29,31 @@ func init() {
|
||||
Type: "object",
|
||||
Properties: map[string]*jsonschema.Schema{
|
||||
"ip": {
|
||||
Title: "IP",
|
||||
Type: "string",
|
||||
Default: json.RawMessage(`"0.0.0.0"`),
|
||||
Title: "IP",
|
||||
Description: "the IP address to bind the SIP server to",
|
||||
Type: "string",
|
||||
Default: json.RawMessage(`"0.0.0.0"`),
|
||||
},
|
||||
"port": {
|
||||
Title: "Port",
|
||||
Type: "integer",
|
||||
Minimum: jsonschema.Ptr[float64](1024),
|
||||
Maximum: jsonschema.Ptr[float64](65535),
|
||||
Default: json.RawMessage(`5060`),
|
||||
Title: "Port",
|
||||
Description: "the port for the SIP server to listen on",
|
||||
Type: "integer",
|
||||
Minimum: jsonschema.Ptr[float64](1024),
|
||||
Maximum: jsonschema.Ptr[float64](65535),
|
||||
Default: json.RawMessage(`5060`),
|
||||
},
|
||||
"transport": {
|
||||
Title: "Transport",
|
||||
Type: "string",
|
||||
Enum: []any{"udp", "tcp", "ws", "udp4", "tcp4"},
|
||||
Default: json.RawMessage(`"udp"`),
|
||||
Title: "Transport",
|
||||
Description: "the transport protocol to use for the SIP server",
|
||||
Type: "string",
|
||||
Enum: []any{"udp", "tcp", "ws", "udp4", "tcp4"},
|
||||
Default: json.RawMessage(`"udp"`),
|
||||
},
|
||||
"userAgent": {
|
||||
Title: "User Agent",
|
||||
Type: "string",
|
||||
Default: json.RawMessage(`"showbridge"`),
|
||||
Title: "User Agent",
|
||||
Description: "the user agent string to use",
|
||||
Type: "string",
|
||||
Default: json.RawMessage(`"showbridge"`),
|
||||
},
|
||||
},
|
||||
Required: []string{},
|
||||
@@ -124,6 +103,31 @@ func init() {
|
||||
})
|
||||
}
|
||||
|
||||
type SIPCallServer struct {
|
||||
config config.ModuleConfig
|
||||
ctx context.Context
|
||||
inputHandler common.InputHandler
|
||||
IP string
|
||||
Port int
|
||||
Transport string
|
||||
UserAgent string
|
||||
logger *slog.Logger
|
||||
cancel context.CancelFunc
|
||||
ua *sipgo.UserAgent
|
||||
uaMu sync.Mutex
|
||||
}
|
||||
|
||||
type SIPCallMessage struct {
|
||||
To string
|
||||
}
|
||||
|
||||
type SIPCall struct {
|
||||
inDialog *diago.DialogServerSession
|
||||
lock sync.Mutex
|
||||
}
|
||||
|
||||
type sipCallContextKey string
|
||||
|
||||
func (scs *SIPCallServer) Id() string {
|
||||
return scs.config.Id
|
||||
}
|
||||
@@ -132,9 +136,9 @@ func (scs *SIPCallServer) Type() string {
|
||||
return scs.config.Type
|
||||
}
|
||||
|
||||
func (scs *SIPCallServer) Start(ctx context.Context, router common.RouteIO) error {
|
||||
func (scs *SIPCallServer) Start(ctx context.Context, inputHandler common.InputHandler) error {
|
||||
scs.logger.Debug("running")
|
||||
scs.router = router
|
||||
scs.inputHandler = inputHandler
|
||||
moduleContext, cancel := context.WithCancel(ctx)
|
||||
scs.ctx = moduleContext
|
||||
scs.cancel = cancel
|
||||
@@ -168,6 +172,7 @@ func (scs *SIPCallServer) Start(ctx context.Context, router common.RouteIO) erro
|
||||
}
|
||||
|
||||
<-scs.ctx.Done()
|
||||
scs.logger.Debug("done")
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -179,9 +184,11 @@ func (scs *SIPCallServer) HandleCall(inDialog *diago.DialogServerSession) {
|
||||
dialogContext := context.WithValue(scs.ctx, sipCallContextKey("call"), &SIPCall{
|
||||
inDialog: inDialog,
|
||||
})
|
||||
scs.router.HandleInput(dialogContext, scs.Id(), SIPCallMessage{
|
||||
To: inDialog.ToUser(),
|
||||
})
|
||||
if scs.inputHandler != nil {
|
||||
scs.inputHandler(dialogContext, scs.Id(), SIPCallMessage{
|
||||
To: inDialog.ToUser(),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func (scs *SIPCallServer) Output(ctx context.Context, payload any) error {
|
||||
@@ -249,12 +256,11 @@ func (scs *SIPCallServer) Output(ctx context.Context, payload any) error {
|
||||
|
||||
func (scs *SIPCallServer) Stop() {
|
||||
if scs.cancel != nil {
|
||||
scs.cancel()
|
||||
defer scs.cancel()
|
||||
}
|
||||
scs.uaMu.Lock()
|
||||
defer scs.uaMu.Unlock()
|
||||
if scs.ua != nil {
|
||||
scs.ua.Close()
|
||||
}
|
||||
scs.logger.Debug("done")
|
||||
}
|
||||
|
||||
@@ -22,31 +22,6 @@ import (
|
||||
"github.com/jwetzell/showbridge-go/internal/processor"
|
||||
)
|
||||
|
||||
type SIPDTMFServer struct {
|
||||
config config.ModuleConfig
|
||||
ctx context.Context
|
||||
router common.RouteIO
|
||||
IP string
|
||||
Port int
|
||||
Transport string
|
||||
UserAgent string
|
||||
Separator string
|
||||
logger *slog.Logger
|
||||
cancel context.CancelFunc
|
||||
ua *sipgo.UserAgent
|
||||
uaMu sync.Mutex
|
||||
}
|
||||
|
||||
type SIPDTMFMessage struct {
|
||||
To string
|
||||
Digits string
|
||||
}
|
||||
|
||||
type SIPDTMFCall struct {
|
||||
inDialog *diago.DialogServerSession
|
||||
lock sync.Mutex
|
||||
}
|
||||
|
||||
func init() {
|
||||
RegisterModule(ModuleRegistration{
|
||||
Type: "sip.dtmf.server",
|
||||
@@ -55,33 +30,38 @@ func init() {
|
||||
Type: "object",
|
||||
Properties: map[string]*jsonschema.Schema{
|
||||
"ip": {
|
||||
Title: "IP",
|
||||
Type: "string",
|
||||
Default: json.RawMessage(`"0.0.0.0"`),
|
||||
Title: "IP",
|
||||
Description: "the IP address to bind the SIP server to",
|
||||
Type: "string",
|
||||
Default: json.RawMessage(`"0.0.0.0"`),
|
||||
},
|
||||
"port": {
|
||||
Title: "Port",
|
||||
Type: "integer",
|
||||
Minimum: jsonschema.Ptr[float64](1024),
|
||||
Maximum: jsonschema.Ptr[float64](65535),
|
||||
Default: json.RawMessage(`5060`),
|
||||
Title: "Port",
|
||||
Description: "the port for the SIP server to listen on",
|
||||
Type: "integer",
|
||||
Minimum: jsonschema.Ptr[float64](1024),
|
||||
Maximum: jsonschema.Ptr[float64](65535),
|
||||
Default: json.RawMessage(`5060`),
|
||||
},
|
||||
"transport": {
|
||||
Title: "Transport",
|
||||
Type: "string",
|
||||
Enum: []any{"udp", "tcp", "ws", "udp4", "tcp4"},
|
||||
Default: json.RawMessage(`"udp"`),
|
||||
Title: "Transport",
|
||||
Description: "the transport protocol to use for the SIP server",
|
||||
Type: "string",
|
||||
Enum: []any{"udp", "tcp", "ws", "udp4", "tcp4"},
|
||||
Default: json.RawMessage(`"udp"`),
|
||||
},
|
||||
"userAgent": {
|
||||
Title: "User Agent",
|
||||
Type: "string",
|
||||
Default: json.RawMessage(`"showbridge"`),
|
||||
Title: "User Agent",
|
||||
Description: "the user agent string to use",
|
||||
Type: "string",
|
||||
Default: json.RawMessage(`"showbridge"`),
|
||||
},
|
||||
"separator": {
|
||||
Title: "DTMF Separator",
|
||||
Type: "string",
|
||||
MinLength: new(1),
|
||||
MaxLength: new(1),
|
||||
Title: "DTMF Separator",
|
||||
Description: "the DTMF character to use as a separator between DTMF digit groups",
|
||||
Type: "string",
|
||||
MinLength: new(1),
|
||||
MaxLength: new(1),
|
||||
},
|
||||
},
|
||||
Required: []string{"separator"},
|
||||
@@ -144,6 +124,31 @@ func init() {
|
||||
})
|
||||
}
|
||||
|
||||
type SIPDTMFServer struct {
|
||||
config config.ModuleConfig
|
||||
ctx context.Context
|
||||
inputHandler common.InputHandler
|
||||
IP string
|
||||
Port int
|
||||
Transport string
|
||||
UserAgent string
|
||||
Separator string
|
||||
logger *slog.Logger
|
||||
cancel context.CancelFunc
|
||||
ua *sipgo.UserAgent
|
||||
uaMu sync.Mutex
|
||||
}
|
||||
|
||||
type SIPDTMFMessage struct {
|
||||
To string
|
||||
Digits string
|
||||
}
|
||||
|
||||
type SIPDTMFCall struct {
|
||||
inDialog *diago.DialogServerSession
|
||||
lock sync.Mutex
|
||||
}
|
||||
|
||||
func (sds *SIPDTMFServer) Id() string {
|
||||
return sds.config.Id
|
||||
}
|
||||
@@ -152,9 +157,9 @@ func (sds *SIPDTMFServer) Type() string {
|
||||
return sds.config.Type
|
||||
}
|
||||
|
||||
func (sds *SIPDTMFServer) Start(ctx context.Context, router common.RouteIO) error {
|
||||
func (sds *SIPDTMFServer) Start(ctx context.Context, inputHandler common.InputHandler) error {
|
||||
sds.logger.Debug("running")
|
||||
sds.router = router
|
||||
sds.inputHandler = inputHandler
|
||||
moduleContext, cancel := context.WithCancel(ctx)
|
||||
sds.ctx = moduleContext
|
||||
sds.cancel = cancel
|
||||
@@ -190,6 +195,7 @@ func (sds *SIPDTMFServer) Start(ctx context.Context, router common.RouteIO) erro
|
||||
}
|
||||
|
||||
<-sds.ctx.Done()
|
||||
sds.logger.Debug("done")
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -203,11 +209,11 @@ func (sds *SIPDTMFServer) HandleCall(inDialog *diago.DialogServerSession) error
|
||||
|
||||
return reader.Listen(func(dtmf rune) error {
|
||||
if dtmf == rune(sds.Separator[0]) {
|
||||
if sds.router != nil {
|
||||
if sds.inputHandler != nil {
|
||||
dialogContext := context.WithValue(sds.ctx, sipCallContextKey("call"), &SIPDTMFCall{
|
||||
inDialog: inDialog,
|
||||
})
|
||||
sds.router.HandleInput(dialogContext, sds.Id(), SIPDTMFMessage{
|
||||
sds.inputHandler(dialogContext, sds.Id(), SIPDTMFMessage{
|
||||
To: inDialog.ToUser(),
|
||||
Digits: userString,
|
||||
})
|
||||
@@ -286,12 +292,11 @@ func (sds *SIPDTMFServer) Output(ctx context.Context, payload any) error {
|
||||
|
||||
func (sds *SIPDTMFServer) Stop() {
|
||||
if sds.cancel != nil {
|
||||
sds.cancel()
|
||||
defer sds.cancel()
|
||||
}
|
||||
sds.uaMu.Lock()
|
||||
defer sds.uaMu.Unlock()
|
||||
if sds.ua != nil {
|
||||
sds.ua.Close()
|
||||
}
|
||||
sds.logger.Debug("done")
|
||||
}
|
||||
|
||||
@@ -15,18 +15,6 @@ import (
|
||||
"github.com/jwetzell/showbridge-go/internal/framer"
|
||||
)
|
||||
|
||||
type TCPClient struct {
|
||||
config config.ModuleConfig
|
||||
framer framer.Framer
|
||||
conn *net.TCPConn
|
||||
ctx context.Context
|
||||
router common.RouteIO
|
||||
Addr *net.TCPAddr
|
||||
logger *slog.Logger
|
||||
cancel context.CancelFunc
|
||||
connMu sync.Mutex
|
||||
}
|
||||
|
||||
func init() {
|
||||
RegisterModule(ModuleRegistration{
|
||||
Type: "net.tcp.client",
|
||||
@@ -35,19 +23,22 @@ func init() {
|
||||
Type: "object",
|
||||
Properties: map[string]*jsonschema.Schema{
|
||||
"host": {
|
||||
Title: "Host",
|
||||
Type: "string",
|
||||
Title: "Host",
|
||||
Description: "the hostname or IP address of the TCP server to connect to",
|
||||
Type: "string",
|
||||
},
|
||||
"port": {
|
||||
Title: "Port",
|
||||
Type: "integer",
|
||||
Minimum: jsonschema.Ptr[float64](1),
|
||||
Maximum: jsonschema.Ptr[float64](65535),
|
||||
Title: "Port",
|
||||
Description: "the port of the TCP server to connect to",
|
||||
Type: "integer",
|
||||
Minimum: jsonschema.Ptr[float64](1),
|
||||
Maximum: jsonschema.Ptr[float64](65535),
|
||||
},
|
||||
"framing": {
|
||||
Title: "Framing Method",
|
||||
Type: "string",
|
||||
Enum: []any{"LF", "CR", "CRLF", "SLIP", "RAW"},
|
||||
Title: "Framing Method",
|
||||
Description: "the method used to frame messages over the TCP connection",
|
||||
Type: "string",
|
||||
Enum: []any{"LF", "CR", "CRLF", "SLIP", "RAW"},
|
||||
},
|
||||
},
|
||||
Required: []string{"host", "port", "framing"},
|
||||
@@ -75,16 +66,31 @@ func init() {
|
||||
return nil, fmt.Errorf("net.tcp.client framing error: %w", err)
|
||||
}
|
||||
|
||||
framer := framer.GetFramer(framingMethodString)
|
||||
inFramer := framer.GetFramer(framingMethodString)
|
||||
|
||||
if framer == nil {
|
||||
if inFramer == nil {
|
||||
return nil, fmt.Errorf("net.tcp.client unknown framing method: %s", framingMethodString)
|
||||
}
|
||||
return &TCPClient{framer: framer, Addr: addr, config: config, logger: CreateLogger(config)}, nil
|
||||
|
||||
outFramer := framer.GetFramer(framingMethodString)
|
||||
return &TCPClient{inFramer: inFramer, outFramer: outFramer, Addr: addr, config: config, logger: CreateLogger(config)}, nil
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
type TCPClient struct {
|
||||
config config.ModuleConfig
|
||||
inFramer framer.Framer
|
||||
outFramer framer.Framer
|
||||
conn *net.TCPConn
|
||||
ctx context.Context
|
||||
inputHandler common.InputHandler
|
||||
Addr *net.TCPAddr
|
||||
logger *slog.Logger
|
||||
cancel context.CancelFunc
|
||||
connMu sync.Mutex
|
||||
}
|
||||
|
||||
func (tc *TCPClient) Id() string {
|
||||
return tc.config.Id
|
||||
}
|
||||
@@ -93,18 +99,19 @@ func (tc *TCPClient) Type() string {
|
||||
return tc.config.Type
|
||||
}
|
||||
|
||||
func (tc *TCPClient) Start(ctx context.Context, router common.RouteIO) error {
|
||||
func (tc *TCPClient) Start(ctx context.Context, inputHandler common.InputHandler) error {
|
||||
tc.logger.Debug("running")
|
||||
tc.router = router
|
||||
tc.inputHandler = inputHandler
|
||||
moduleContext, cancel := context.WithCancel(ctx)
|
||||
tc.ctx = moduleContext
|
||||
tc.cancel = cancel
|
||||
|
||||
CONNECT_RETRY:
|
||||
for tc.ctx.Err() == nil {
|
||||
err := tc.SetupConn()
|
||||
if err != nil {
|
||||
if tc.ctx.Err() != nil {
|
||||
return nil
|
||||
break CONNECT_RETRY
|
||||
}
|
||||
tc.logger.Error("connection error", "error", err.Error())
|
||||
time.Sleep(time.Second * 2)
|
||||
@@ -112,39 +119,41 @@ func (tc *TCPClient) Start(ctx context.Context, router common.RouteIO) error {
|
||||
}
|
||||
|
||||
buffer := make([]byte, 1024)
|
||||
select {
|
||||
case <-tc.ctx.Done():
|
||||
return nil
|
||||
default:
|
||||
READ:
|
||||
for {
|
||||
select {
|
||||
case <-tc.ctx.Done():
|
||||
return nil
|
||||
default:
|
||||
byteCount, err := tc.conn.Read(buffer)
|
||||
|
||||
if err != nil {
|
||||
tc.framer.Clear()
|
||||
break READ
|
||||
READ:
|
||||
for tc.ctx.Err() == nil {
|
||||
tc.conn.SetReadDeadline(time.Now().Add(time.Millisecond * 200))
|
||||
byteCount, err := tc.conn.Read(buffer)
|
||||
|
||||
if err != nil {
|
||||
if opErr, ok := err.(*net.OpError); ok {
|
||||
//NOTE(jwetzell) we hit deadline
|
||||
if opErr.Timeout() {
|
||||
continue
|
||||
}
|
||||
}
|
||||
if errors.Is(err, net.ErrClosed) {
|
||||
break CONNECT_RETRY
|
||||
}
|
||||
break READ
|
||||
}
|
||||
|
||||
if tc.framer != nil {
|
||||
if byteCount > 0 {
|
||||
messages := tc.framer.Decode(buffer[0:byteCount])
|
||||
for _, message := range messages {
|
||||
if tc.router != nil {
|
||||
tc.router.HandleInput(tc.ctx, tc.Id(), message)
|
||||
} else {
|
||||
tc.logger.Error("input received but no router is configured")
|
||||
}
|
||||
}
|
||||
if tc.inFramer != nil {
|
||||
if byteCount > 0 {
|
||||
messages := tc.inFramer.Decode(buffer[0:byteCount])
|
||||
for _, message := range messages {
|
||||
if tc.inputHandler != nil {
|
||||
tc.inputHandler(tc.ctx, tc.Id(), message)
|
||||
} else {
|
||||
tc.logger.Error("input received but no input handler is configured")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
<-tc.ctx.Done()
|
||||
tc.logger.Debug("done")
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -166,20 +175,25 @@ func (tc *TCPClient) Output(ctx context.Context, payload any) error {
|
||||
if !ok {
|
||||
return errors.New("net.tcp.client is only able to output bytes")
|
||||
}
|
||||
_, err := tc.conn.Write(tc.framer.Encode(payloadBytes))
|
||||
_, err := tc.conn.Write(tc.inFramer.Encode(payloadBytes))
|
||||
return err
|
||||
}
|
||||
|
||||
func (tc *TCPClient) Stop() {
|
||||
if tc.cancel != nil {
|
||||
tc.cancel()
|
||||
defer tc.cancel()
|
||||
}
|
||||
tc.connMu.Lock()
|
||||
defer tc.connMu.Unlock()
|
||||
if tc.conn != nil {
|
||||
tc.conn.Close()
|
||||
tc.conn = nil
|
||||
}
|
||||
tc.logger.Debug("done")
|
||||
|
||||
if tc.inFramer != nil {
|
||||
tc.inFramer.Clear()
|
||||
}
|
||||
|
||||
if tc.outFramer != nil {
|
||||
tc.outFramer.Clear()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -10,7 +10,6 @@ import (
|
||||
"slices"
|
||||
"strings"
|
||||
"sync"
|
||||
"syscall"
|
||||
"time"
|
||||
|
||||
"github.com/google/jsonschema-go/jsonschema"
|
||||
@@ -19,21 +18,6 @@ import (
|
||||
"github.com/jwetzell/showbridge-go/internal/framer"
|
||||
)
|
||||
|
||||
type TCPServer struct {
|
||||
config config.ModuleConfig
|
||||
Addr *net.TCPAddr
|
||||
Framer framer.Framer
|
||||
ctx context.Context
|
||||
router common.RouteIO
|
||||
wg sync.WaitGroup
|
||||
connections []*net.TCPConn
|
||||
connectionsMu sync.RWMutex
|
||||
logger *slog.Logger
|
||||
cancel context.CancelFunc
|
||||
listener *net.TCPListener
|
||||
listenerMu sync.Mutex
|
||||
}
|
||||
|
||||
func init() {
|
||||
RegisterModule(ModuleRegistration{
|
||||
Type: "net.tcp.server",
|
||||
@@ -42,20 +26,23 @@ func init() {
|
||||
Type: "object",
|
||||
Properties: map[string]*jsonschema.Schema{
|
||||
"ip": {
|
||||
Title: "IP",
|
||||
Type: "string",
|
||||
Default: json.RawMessage(`"0.0.0.0"`),
|
||||
Title: "IP",
|
||||
Description: "the IP address to bind the TCP server to",
|
||||
Type: "string",
|
||||
Default: json.RawMessage(`"0.0.0.0"`),
|
||||
},
|
||||
"port": {
|
||||
Title: "Port",
|
||||
Type: "integer",
|
||||
Minimum: jsonschema.Ptr[float64](1024),
|
||||
Maximum: jsonschema.Ptr[float64](65535),
|
||||
Title: "Port",
|
||||
Description: "the port for the TCP server to listen on",
|
||||
Type: "integer",
|
||||
Minimum: jsonschema.Ptr[float64](1024),
|
||||
Maximum: jsonschema.Ptr[float64](65535),
|
||||
},
|
||||
"framing": {
|
||||
Title: "Framing Method",
|
||||
Type: "string",
|
||||
Enum: []any{"LF", "CR", "CRLF", "SLIP", "RAW"},
|
||||
Title: "Framing Method",
|
||||
Description: "the method used to frame messages over the TCP connection",
|
||||
Type: "string",
|
||||
Enum: []any{"LF", "CR", "CRLF", "SLIP", "RAW"},
|
||||
},
|
||||
},
|
||||
Required: []string{"port", "framing"},
|
||||
@@ -73,12 +60,14 @@ func init() {
|
||||
return nil, fmt.Errorf("net.tcp.server framing error: %w", err)
|
||||
}
|
||||
|
||||
framer := framer.GetFramer(framingMethodString)
|
||||
inFramer := framer.GetFramer(framingMethodString)
|
||||
|
||||
if framer == nil {
|
||||
if inFramer == nil {
|
||||
return nil, fmt.Errorf("net.tcp.server unknown framing method: %s", framingMethodString)
|
||||
}
|
||||
|
||||
outFramer := framer.GetFramer(framingMethodString)
|
||||
|
||||
ipString, err := params.GetString("ip")
|
||||
if err != nil {
|
||||
if errors.Is(err, config.ErrParamNotFound) {
|
||||
@@ -92,11 +81,35 @@ func init() {
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &TCPServer{Framer: framer, Addr: addr, config: moduleConfig, logger: CreateLogger(moduleConfig)}, nil
|
||||
return &TCPServer{inFramer: inFramer, outFramer: outFramer, framerType: framingMethodString, Addr: addr, config: moduleConfig, logger: CreateLogger(moduleConfig)}, nil
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
type tcpConnection struct {
|
||||
conn *net.TCPConn
|
||||
framer framer.Framer
|
||||
}
|
||||
|
||||
type TCPServer struct {
|
||||
config config.ModuleConfig
|
||||
Addr *net.TCPAddr
|
||||
inFramer framer.Framer
|
||||
outFramer framer.Framer
|
||||
framerType string
|
||||
ctx context.Context
|
||||
inputHandler common.InputHandler
|
||||
wg sync.WaitGroup
|
||||
connections []tcpConnection
|
||||
connectionsMu sync.RWMutex
|
||||
logger *slog.Logger
|
||||
cancel context.CancelFunc
|
||||
listener *net.TCPListener
|
||||
listenerMu sync.Mutex
|
||||
connectionShutdownCtx context.Context
|
||||
connectionShutdown context.CancelFunc
|
||||
}
|
||||
|
||||
func (ts *TCPServer) Id() string {
|
||||
return ts.config.Id
|
||||
}
|
||||
@@ -107,75 +120,43 @@ func (ts *TCPServer) Type() string {
|
||||
|
||||
func (ts *TCPServer) handleClient(client *net.TCPConn) {
|
||||
ts.connectionsMu.Lock()
|
||||
ts.connections = append(ts.connections, client)
|
||||
ts.connections = append(ts.connections, tcpConnection{conn: client, framer: framer.GetFramer(ts.framerType)})
|
||||
ts.connectionsMu.Unlock()
|
||||
ts.logger.Debug("connection accepted", "remoteAddr", client.RemoteAddr().String())
|
||||
defer client.Close()
|
||||
defer func() {
|
||||
client.Close()
|
||||
ts.connectionsMu.Lock()
|
||||
for i := 0; i < len(ts.connections); i++ {
|
||||
if ts.connections[i].conn == client {
|
||||
ts.connections = slices.Delete(ts.connections, i, i+1)
|
||||
break
|
||||
}
|
||||
}
|
||||
ts.connectionsMu.Unlock()
|
||||
ts.logger.Debug("connection closed", "remoteAddr", client.RemoteAddr().String())
|
||||
}()
|
||||
|
||||
buffer := make([]byte, 1024)
|
||||
ClientRead:
|
||||
for ts.ctx.Err() == nil {
|
||||
select {
|
||||
case <-ts.ctx.Done():
|
||||
client.Close()
|
||||
ts.connectionsMu.Lock()
|
||||
for i := 0; i < len(ts.connections); i++ {
|
||||
if ts.connections[i] == client {
|
||||
ts.connections = slices.Delete(ts.connections, i, i+1)
|
||||
break
|
||||
for ts.ctx.Err() == nil && ts.connectionShutdownCtx.Err() == nil {
|
||||
client.SetDeadline(time.Now().Add(time.Millisecond * 200))
|
||||
byteCount, err := client.Read(buffer)
|
||||
if err != nil {
|
||||
if opErr, ok := err.(*net.OpError); ok {
|
||||
//NOTE(jwetzell) we hit deadline
|
||||
if opErr.Timeout() {
|
||||
continue
|
||||
}
|
||||
}
|
||||
ts.connectionsMu.Unlock()
|
||||
return
|
||||
default:
|
||||
client.SetDeadline(time.Now().Add(time.Millisecond * 200))
|
||||
byteCount, err := client.Read(buffer)
|
||||
|
||||
if err != nil {
|
||||
if opErr, ok := err.(*net.OpError); ok {
|
||||
//NOTE(jwetzell) we hit deadline
|
||||
if opErr.Timeout() {
|
||||
continue ClientRead
|
||||
}
|
||||
if errors.Is(opErr, syscall.ECONNRESET) {
|
||||
ts.connectionsMu.Lock()
|
||||
for i := 0; i < len(ts.connections); i++ {
|
||||
if ts.connections[i] == client {
|
||||
ts.connections = slices.Delete(ts.connections, i, i+1)
|
||||
break
|
||||
}
|
||||
}
|
||||
ts.logger.Debug("connection reset", "remoteAddr", client.RemoteAddr().String())
|
||||
ts.connectionsMu.Unlock()
|
||||
}
|
||||
}
|
||||
|
||||
if err.Error() == "EOF" {
|
||||
ts.connectionsMu.Lock()
|
||||
for i := 0; i < len(ts.connections); i++ {
|
||||
if ts.connections[i] == client {
|
||||
ts.connections = slices.Delete(ts.connections, i, i+1)
|
||||
break
|
||||
}
|
||||
}
|
||||
ts.connectionsMu.Unlock()
|
||||
}
|
||||
return
|
||||
}
|
||||
if ts.Framer != nil {
|
||||
if byteCount > 0 {
|
||||
messages := ts.Framer.Decode(buffer[0:byteCount])
|
||||
for _, message := range messages {
|
||||
if ts.router != nil {
|
||||
_, ok := client.RemoteAddr().(*net.TCPAddr)
|
||||
if ok {
|
||||
ts.router.HandleInput(ts.ctx, ts.Id(), message)
|
||||
} else {
|
||||
ts.router.HandleInput(ts.ctx, ts.Id(), message)
|
||||
}
|
||||
} else {
|
||||
ts.logger.Error("input received but no router is configured")
|
||||
}
|
||||
break
|
||||
}
|
||||
if ts.inFramer != nil {
|
||||
if byteCount > 0 {
|
||||
messages := ts.inFramer.Decode(buffer[0:byteCount])
|
||||
for _, message := range messages {
|
||||
if ts.inputHandler != nil {
|
||||
ts.inputHandler(ts.ctx, ts.Id(), message)
|
||||
} else {
|
||||
ts.logger.Error("input received but no input handler is configured")
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -183,13 +164,17 @@ ClientRead:
|
||||
}
|
||||
}
|
||||
|
||||
func (ts *TCPServer) Start(ctx context.Context, router common.RouteIO) error {
|
||||
func (ts *TCPServer) Start(ctx context.Context, inputHandler common.InputHandler) error {
|
||||
ts.logger.Debug("running")
|
||||
ts.router = router
|
||||
ts.inputHandler = inputHandler
|
||||
moduleContext, cancel := context.WithCancel(ctx)
|
||||
ts.ctx = moduleContext
|
||||
ts.cancel = cancel
|
||||
|
||||
shutdownCtx, shutdownCancel := context.WithCancel(context.Background())
|
||||
ts.connectionShutdownCtx = shutdownCtx
|
||||
ts.connectionShutdown = shutdownCancel
|
||||
|
||||
listener, err := net.ListenTCP("tcp", ts.Addr)
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -203,6 +188,9 @@ AcceptLoop:
|
||||
for ts.ctx.Err() == nil {
|
||||
conn, err := listener.AcceptTCP()
|
||||
if err != nil {
|
||||
if errors.Is(err, net.ErrClosed) {
|
||||
break AcceptLoop
|
||||
}
|
||||
select {
|
||||
case <-ts.ctx.Done():
|
||||
break AcceptLoop
|
||||
@@ -216,6 +204,8 @@ AcceptLoop:
|
||||
}
|
||||
}
|
||||
ts.wg.Done()
|
||||
<-ts.ctx.Done()
|
||||
ts.logger.Debug("done")
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -226,15 +216,21 @@ func (ts *TCPServer) Output(ctx context.Context, payload any) error {
|
||||
return errors.New("net.tcp.server is only able to output bytes")
|
||||
}
|
||||
ts.connectionsMu.Lock()
|
||||
defer ts.connectionsMu.Unlock()
|
||||
var errorString strings.Builder
|
||||
|
||||
if ts.outFramer == nil {
|
||||
return errors.New("no output framer configured")
|
||||
}
|
||||
|
||||
outputBytes := ts.outFramer.Encode(payloadBytes)
|
||||
|
||||
for _, connection := range ts.connections {
|
||||
_, err := connection.Write(payloadBytes)
|
||||
_, err := connection.conn.Write(outputBytes)
|
||||
if err != nil {
|
||||
fmt.Fprintf(&errorString, "%s\n", err.Error())
|
||||
}
|
||||
}
|
||||
ts.connectionsMu.Unlock()
|
||||
|
||||
if errorString.String() == "" {
|
||||
return nil
|
||||
@@ -244,14 +240,18 @@ func (ts *TCPServer) Output(ctx context.Context, payload any) error {
|
||||
|
||||
func (ts *TCPServer) Stop() {
|
||||
if ts.cancel != nil {
|
||||
ts.cancel()
|
||||
defer ts.cancel()
|
||||
}
|
||||
if ts.connectionShutdown != nil {
|
||||
ts.connectionShutdown()
|
||||
}
|
||||
|
||||
ts.listenerMu.Lock()
|
||||
defer ts.listenerMu.Unlock()
|
||||
if ts.listener != nil {
|
||||
ts.listener.Close()
|
||||
ts.listener = nil
|
||||
}
|
||||
ts.logger.Debug("waiting for connections to close")
|
||||
ts.wg.Wait()
|
||||
ts.logger.Debug("done")
|
||||
ts.logger.Debug("all connections closed")
|
||||
}
|
||||
|
||||
@@ -2,13 +2,14 @@ package module_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/jwetzell/showbridge-go/internal/config"
|
||||
"github.com/jwetzell/showbridge-go/internal/module"
|
||||
)
|
||||
|
||||
func TestDbSqliteFromRegistry(t *testing.T) {
|
||||
registration, ok := module.ModuleRegistry["db.sqlite"]
|
||||
registration, ok := module.GetModuleRegistration("db.sqlite")
|
||||
if !ok {
|
||||
t.Fatalf("db.sqlite module not registered")
|
||||
}
|
||||
@@ -34,6 +35,57 @@ func TestDbSqliteFromRegistry(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestGoodDbSqlite(t *testing.T) {
|
||||
|
||||
testCases := []struct {
|
||||
name string
|
||||
params map[string]any
|
||||
}{
|
||||
{
|
||||
name: "in memory db",
|
||||
params: map[string]any{
|
||||
"dsn": ":memory:",
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "file db",
|
||||
params: map[string]any{
|
||||
"dsn": "test.db",
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
for _, test := range testCases {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
|
||||
registration, ok := module.GetModuleRegistration("db.sqlite")
|
||||
if !ok {
|
||||
t.Fatalf("db.sqlite module not registered")
|
||||
}
|
||||
|
||||
moduleInstance, err := registration.New(config.ModuleConfig{
|
||||
Id: "test",
|
||||
Type: "db.sqlite",
|
||||
Params: test.params,
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
t.Fatalf("db.sqlite failed to create module: %s", err)
|
||||
}
|
||||
// TODO(jwetzell) this is kind of hacky
|
||||
go func() {
|
||||
time.Sleep(1 * time.Second)
|
||||
moduleInstance.Stop()
|
||||
}()
|
||||
err = moduleInstance.Start(t.Context(), nil)
|
||||
|
||||
if err != nil {
|
||||
t.Fatalf("db.sqlite failed to start: %s", err)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestBadDbSqlite(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
@@ -55,7 +107,7 @@ func TestBadDbSqlite(t *testing.T) {
|
||||
for _, test := range tests {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
|
||||
registration, ok := module.ModuleRegistry["db.sqlite"]
|
||||
registration, ok := module.GetModuleRegistration("db.sqlite")
|
||||
if !ok {
|
||||
t.Fatalf("db.sqlite module not registered")
|
||||
}
|
||||
|
||||
@@ -2,13 +2,14 @@ package module_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/jwetzell/showbridge-go/internal/config"
|
||||
"github.com/jwetzell/showbridge-go/internal/module"
|
||||
)
|
||||
|
||||
func TestHTTPServerFromRegistry(t *testing.T) {
|
||||
registration, ok := module.ModuleRegistry["http.server"]
|
||||
registration, ok := module.GetModuleRegistration("http.server")
|
||||
if !ok {
|
||||
t.Fatalf("http.server module not registered")
|
||||
}
|
||||
@@ -34,6 +35,52 @@ func TestHTTPServerFromRegistry(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestGoodHTTPServer(t *testing.T) {
|
||||
|
||||
testCases := []struct {
|
||||
name string
|
||||
params map[string]any
|
||||
}{
|
||||
{
|
||||
name: "minimal config",
|
||||
params: map[string]any{
|
||||
"port": 8000,
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
for _, test := range testCases {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
|
||||
registration, ok := module.GetModuleRegistration("http.server")
|
||||
if !ok {
|
||||
t.Fatalf("http.server module not registered")
|
||||
}
|
||||
|
||||
moduleInstance, err := registration.New(config.ModuleConfig{
|
||||
Id: "test",
|
||||
Type: "http.server",
|
||||
Params: test.params,
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
t.Fatalf("http.server failed to create module: %s", err)
|
||||
}
|
||||
// TODO(jwetzell) this is kind of hacky
|
||||
go func() {
|
||||
time.Sleep(1 * time.Second)
|
||||
moduleInstance.Stop()
|
||||
}()
|
||||
|
||||
err = moduleInstance.Start(t.Context(), nil)
|
||||
|
||||
if err != nil {
|
||||
t.Fatalf("http.server failed to start: %s", err)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestBadHTTPServer(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
@@ -55,7 +102,7 @@ func TestBadHTTPServer(t *testing.T) {
|
||||
for _, test := range tests {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
|
||||
registration, ok := module.ModuleRegistry["http.server"]
|
||||
registration, ok := module.GetModuleRegistration("http.server")
|
||||
if !ok {
|
||||
t.Fatalf("http.server module not registered")
|
||||
}
|
||||
|
||||
@@ -8,7 +8,7 @@ import (
|
||||
)
|
||||
|
||||
func TestMIDIInputFromRegistry(t *testing.T) {
|
||||
registration, ok := module.ModuleRegistry["midi.input"]
|
||||
registration, ok := module.GetModuleRegistration("midi.input")
|
||||
if !ok {
|
||||
t.Fatalf("midi.input module not registered")
|
||||
}
|
||||
@@ -55,7 +55,7 @@ func TestBadMIDIInput(t *testing.T) {
|
||||
for _, test := range tests {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
|
||||
registration, ok := module.ModuleRegistry["midi.input"]
|
||||
registration, ok := module.GetModuleRegistration("midi.input")
|
||||
if !ok {
|
||||
t.Fatalf("midi.input module not registered")
|
||||
}
|
||||
|
||||
@@ -8,7 +8,7 @@ import (
|
||||
)
|
||||
|
||||
func TestMIDIOutputFromRegistry(t *testing.T) {
|
||||
registration, ok := module.ModuleRegistry["midi.output"]
|
||||
registration, ok := module.GetModuleRegistration("midi.output")
|
||||
if !ok {
|
||||
t.Fatalf("midi.output module not registered")
|
||||
}
|
||||
@@ -55,7 +55,7 @@ func TestBadMIDIOutput(t *testing.T) {
|
||||
for _, test := range tests {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
|
||||
registration, ok := module.ModuleRegistry["midi.output"]
|
||||
registration, ok := module.GetModuleRegistration("midi.output")
|
||||
if !ok {
|
||||
t.Fatalf("midi.output module not registered")
|
||||
}
|
||||
|
||||
@@ -19,7 +19,7 @@ func TestModuleBadRegistrationNoType(t *testing.T) {
|
||||
module.RegisterModule(module.ModuleRegistration{
|
||||
Type: "",
|
||||
New: func(config config.ModuleConfig) (common.Module, error) {
|
||||
return &test.TestModule{}, nil
|
||||
return test.NewTestModule("test"), nil
|
||||
},
|
||||
})
|
||||
}
|
||||
@@ -47,14 +47,14 @@ func TestModuleBadRegistrationExistingType(t *testing.T) {
|
||||
module.RegisterModule(module.ModuleRegistration{
|
||||
Type: "module.test",
|
||||
New: func(config config.ModuleConfig) (common.Module, error) {
|
||||
return &test.TestModule{}, nil
|
||||
return test.NewTestModule("test"), nil
|
||||
},
|
||||
})
|
||||
|
||||
module.RegisterModule(module.ModuleRegistration{
|
||||
Type: "module.test",
|
||||
New: func(config config.ModuleConfig) (common.Module, error) {
|
||||
return &test.TestModule{}, nil
|
||||
return test.NewTestModule("test"), nil
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
@@ -8,7 +8,7 @@ import (
|
||||
)
|
||||
|
||||
func TestMQTTClientFromRegistry(t *testing.T) {
|
||||
registration, ok := module.ModuleRegistry["mqtt.client"]
|
||||
registration, ok := module.GetModuleRegistration("mqtt.client")
|
||||
if !ok {
|
||||
t.Fatalf("mqtt.client module not registered")
|
||||
}
|
||||
@@ -98,7 +98,7 @@ func TestBadMQTTClient(t *testing.T) {
|
||||
for _, test := range tests {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
|
||||
registration, ok := module.ModuleRegistry["mqtt.client"]
|
||||
registration, ok := module.GetModuleRegistration("mqtt.client")
|
||||
if !ok {
|
||||
t.Fatalf("mqtt.client module not registered")
|
||||
}
|
||||
|
||||
@@ -8,7 +8,7 @@ import (
|
||||
)
|
||||
|
||||
func TestNATSClientFromRegistry(t *testing.T) {
|
||||
registration, ok := module.ModuleRegistry["nats.client"]
|
||||
registration, ok := module.GetModuleRegistration("nats.client")
|
||||
if !ok {
|
||||
t.Fatalf("nats.client module not registered")
|
||||
}
|
||||
@@ -76,7 +76,7 @@ func TestBadNATSClient(t *testing.T) {
|
||||
for _, test := range tests {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
|
||||
registration, ok := module.ModuleRegistry["nats.client"]
|
||||
registration, ok := module.GetModuleRegistration("nats.client")
|
||||
if !ok {
|
||||
t.Fatalf("nats.client module not registered")
|
||||
}
|
||||
|
||||
@@ -2,13 +2,14 @@ package module_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/jwetzell/showbridge-go/internal/config"
|
||||
"github.com/jwetzell/showbridge-go/internal/module"
|
||||
)
|
||||
|
||||
func TestNATSServerFromRegistry(t *testing.T) {
|
||||
registration, ok := module.ModuleRegistry["nats.server"]
|
||||
registration, ok := module.GetModuleRegistration("nats.server")
|
||||
if !ok {
|
||||
t.Fatalf("nats.server module not registered")
|
||||
}
|
||||
@@ -35,6 +36,52 @@ func TestNATSServerFromRegistry(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestGoodNATSServer(t *testing.T) {
|
||||
|
||||
testCases := []struct {
|
||||
name string
|
||||
params map[string]any
|
||||
}{
|
||||
{
|
||||
name: "minimal config",
|
||||
params: map[string]any{
|
||||
"port": 4222,
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
for _, test := range testCases {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
|
||||
registration, ok := module.GetModuleRegistration("nats.server")
|
||||
if !ok {
|
||||
t.Fatalf("nats.server module not registered")
|
||||
}
|
||||
|
||||
moduleInstance, err := registration.New(config.ModuleConfig{
|
||||
Id: "test",
|
||||
Type: "nats.server",
|
||||
Params: test.params,
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
t.Fatalf("nats.server failed to create module: %s", err)
|
||||
}
|
||||
// TODO(jwetzell) this is kind of hacky
|
||||
go func() {
|
||||
time.Sleep(1 * time.Second)
|
||||
moduleInstance.Stop()
|
||||
}()
|
||||
|
||||
err = moduleInstance.Start(t.Context(), nil)
|
||||
|
||||
if err != nil {
|
||||
t.Fatalf("nats.server failed to start: %s", err)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestBadNATSServer(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
@@ -53,7 +100,7 @@ func TestBadNATSServer(t *testing.T) {
|
||||
for _, test := range tests {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
|
||||
registration, ok := module.ModuleRegistry["nats.server"]
|
||||
registration, ok := module.GetModuleRegistration("nats.server")
|
||||
if !ok {
|
||||
t.Fatalf("nats.server module not registered")
|
||||
}
|
||||
|
||||
@@ -8,7 +8,7 @@ import (
|
||||
)
|
||||
|
||||
func TestPSNClientFromRegistry(t *testing.T) {
|
||||
registration, ok := module.ModuleRegistry["psn.client"]
|
||||
registration, ok := module.GetModuleRegistration("psn.client")
|
||||
if !ok {
|
||||
t.Fatalf("psn.client module not registered")
|
||||
}
|
||||
@@ -41,7 +41,7 @@ func TestBadPSNClient(t *testing.T) {
|
||||
for _, test := range tests {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
|
||||
registration, ok := module.ModuleRegistry["psn.client"]
|
||||
registration, ok := module.GetModuleRegistration("psn.client")
|
||||
if !ok {
|
||||
t.Fatalf("psn.client module not registered")
|
||||
}
|
||||
|
||||
@@ -8,7 +8,7 @@ import (
|
||||
)
|
||||
|
||||
func TestRedisClientFromRegistry(t *testing.T) {
|
||||
registration, ok := module.ModuleRegistry["redis.client"]
|
||||
registration, ok := module.GetModuleRegistration("redis.client")
|
||||
if !ok {
|
||||
t.Fatalf("redis.client module not registered")
|
||||
}
|
||||
@@ -76,7 +76,7 @@ func TestBadRedisClient(t *testing.T) {
|
||||
for _, test := range tests {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
|
||||
registration, ok := module.ModuleRegistry["redis.client"]
|
||||
registration, ok := module.GetModuleRegistration("redis.client")
|
||||
if !ok {
|
||||
t.Fatalf("redis.client module not registered")
|
||||
}
|
||||
|
||||
@@ -8,7 +8,7 @@ import (
|
||||
)
|
||||
|
||||
func TestSerialClientFromRegistry(t *testing.T) {
|
||||
registration, ok := module.ModuleRegistry["serial.client"]
|
||||
registration, ok := module.GetModuleRegistration("serial.client")
|
||||
if !ok {
|
||||
t.Fatalf("serial.client module not registered")
|
||||
}
|
||||
@@ -85,7 +85,7 @@ func TestBadSerialClient(t *testing.T) {
|
||||
for _, test := range tests {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
|
||||
registration, ok := module.ModuleRegistry["serial.client"]
|
||||
registration, ok := module.GetModuleRegistration("serial.client")
|
||||
if !ok {
|
||||
t.Fatalf("serial.client module not registered")
|
||||
}
|
||||
|
||||
@@ -8,7 +8,7 @@ import (
|
||||
)
|
||||
|
||||
func TestSIPCallServerFromRegistry(t *testing.T) {
|
||||
registration, ok := module.ModuleRegistry["sip.call.server"]
|
||||
registration, ok := module.GetModuleRegistration("sip.call.server")
|
||||
if !ok {
|
||||
t.Fatalf("sip.call.server module not registered")
|
||||
}
|
||||
@@ -70,7 +70,7 @@ func TestBadSIPCallServer(t *testing.T) {
|
||||
for _, test := range tests {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
|
||||
registration, ok := module.ModuleRegistry["sip.call.server"]
|
||||
registration, ok := module.GetModuleRegistration("sip.call.server")
|
||||
if !ok {
|
||||
t.Fatalf("sip.call.server module not registered")
|
||||
}
|
||||
|
||||
@@ -8,7 +8,7 @@ import (
|
||||
)
|
||||
|
||||
func TestSIPDTMFServerFromRegistry(t *testing.T) {
|
||||
registration, ok := module.ModuleRegistry["sip.dtmf.server"]
|
||||
registration, ok := module.GetModuleRegistration("sip.dtmf.server")
|
||||
if !ok {
|
||||
t.Fatalf("sip.dtmf.server module not registered")
|
||||
}
|
||||
@@ -89,7 +89,7 @@ func TestBadSIPDTMFServer(t *testing.T) {
|
||||
for _, test := range tests {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
|
||||
registration, ok := module.ModuleRegistry["sip.dtmf.server"]
|
||||
registration, ok := module.GetModuleRegistration("sip.dtmf.server")
|
||||
if !ok {
|
||||
t.Fatalf("sip.dtmf.server module not registered")
|
||||
}
|
||||
|
||||
@@ -8,7 +8,7 @@ import (
|
||||
)
|
||||
|
||||
func TestTCPClientFromRegistry(t *testing.T) {
|
||||
registration, ok := module.ModuleRegistry["net.tcp.client"]
|
||||
registration, ok := module.GetModuleRegistration("net.tcp.client")
|
||||
if !ok {
|
||||
t.Fatalf("net.tcp.client module not registered")
|
||||
}
|
||||
@@ -77,7 +77,7 @@ func TestBadTCPClient(t *testing.T) {
|
||||
for _, test := range tests {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
|
||||
registration, ok := module.ModuleRegistry["net.tcp.client"]
|
||||
registration, ok := module.GetModuleRegistration("net.tcp.client")
|
||||
if !ok {
|
||||
t.Fatalf("net.tcp.client module not registered")
|
||||
}
|
||||
|
||||
@@ -2,13 +2,14 @@ package module_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/jwetzell/showbridge-go/internal/config"
|
||||
"github.com/jwetzell/showbridge-go/internal/module"
|
||||
)
|
||||
|
||||
func TestTCPServerFromRegistry(t *testing.T) {
|
||||
registration, ok := module.ModuleRegistry["net.tcp.server"]
|
||||
registration, ok := module.GetModuleRegistration("net.tcp.server")
|
||||
if !ok {
|
||||
t.Fatalf("net.tcp.server module not registered")
|
||||
}
|
||||
@@ -35,6 +36,52 @@ func TestTCPServerFromRegistry(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestGoodTCPServer(t *testing.T) {
|
||||
|
||||
testCases := []struct {
|
||||
name string
|
||||
params map[string]any
|
||||
}{
|
||||
{
|
||||
name: "minimal config",
|
||||
params: map[string]any{
|
||||
"port": 8000,
|
||||
"framing": "LF",
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
for _, test := range testCases {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
|
||||
registration, ok := module.GetModuleRegistration("net.tcp.server")
|
||||
if !ok {
|
||||
t.Fatalf("net.tcp.server module not registered")
|
||||
}
|
||||
|
||||
moduleInstance, err := registration.New(config.ModuleConfig{
|
||||
Id: "test",
|
||||
Type: "net.tcp.server",
|
||||
Params: test.params,
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
t.Fatalf("net.tcp.server failed to create module: %s", err)
|
||||
}
|
||||
// TODO(jwetzell) this is kind of hacky
|
||||
go func() {
|
||||
time.Sleep(1 * time.Second)
|
||||
moduleInstance.Stop()
|
||||
}()
|
||||
err = moduleInstance.Start(t.Context(), nil)
|
||||
|
||||
if err != nil {
|
||||
t.Fatalf("net.tcp.server failed to start: %s", err)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestBadTCPServer(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
@@ -102,7 +149,7 @@ func TestBadTCPServer(t *testing.T) {
|
||||
for _, test := range tests {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
|
||||
registration, ok := module.ModuleRegistry["net.tcp.server"]
|
||||
registration, ok := module.GetModuleRegistration("net.tcp.server")
|
||||
if !ok {
|
||||
t.Fatalf("net.tcp.server module not registered")
|
||||
}
|
||||
|
||||
Binary file not shown.
@@ -2,13 +2,14 @@ package module_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/jwetzell/showbridge-go/internal/config"
|
||||
"github.com/jwetzell/showbridge-go/internal/module"
|
||||
)
|
||||
|
||||
func TestTimeIntervalFromRegistry(t *testing.T) {
|
||||
registration, ok := module.ModuleRegistry["time.interval"]
|
||||
registration, ok := module.GetModuleRegistration("time.interval")
|
||||
if !ok {
|
||||
t.Fatalf("time.interval module not registered")
|
||||
}
|
||||
@@ -34,6 +35,51 @@ func TestTimeIntervalFromRegistry(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestGoodTimeInterval(t *testing.T) {
|
||||
|
||||
testCases := []struct {
|
||||
name string
|
||||
params map[string]any
|
||||
}{
|
||||
{
|
||||
name: "minimal config",
|
||||
params: map[string]any{
|
||||
"duration": 1000,
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
for _, test := range testCases {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
|
||||
registration, ok := module.GetModuleRegistration("time.interval")
|
||||
if !ok {
|
||||
t.Fatalf("time.interval module not registered")
|
||||
}
|
||||
|
||||
moduleInstance, err := registration.New(config.ModuleConfig{
|
||||
Id: "test",
|
||||
Type: "time.interval",
|
||||
Params: test.params,
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
t.Fatalf("time.interval failed to create module: %s", err)
|
||||
}
|
||||
// TODO(jwetzell) this is kind of hacky
|
||||
go func() {
|
||||
time.Sleep(1 * time.Second)
|
||||
moduleInstance.Stop()
|
||||
}()
|
||||
err = moduleInstance.Start(t.Context(), nil)
|
||||
|
||||
if err != nil {
|
||||
t.Fatalf("time.interval failed to start: %s", err)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestBadTimeInterval(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
@@ -57,7 +103,7 @@ func TestBadTimeInterval(t *testing.T) {
|
||||
for _, test := range tests {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
|
||||
registration, ok := module.ModuleRegistry["time.interval"]
|
||||
registration, ok := module.GetModuleRegistration("time.interval")
|
||||
if !ok {
|
||||
t.Fatalf("time.interval module not registered")
|
||||
}
|
||||
|
||||
@@ -2,13 +2,14 @@ package module_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/jwetzell/showbridge-go/internal/config"
|
||||
"github.com/jwetzell/showbridge-go/internal/module"
|
||||
)
|
||||
|
||||
func TestTimeTimerFromRegistry(t *testing.T) {
|
||||
registration, ok := module.ModuleRegistry["time.timer"]
|
||||
registration, ok := module.GetModuleRegistration("time.timer")
|
||||
if !ok {
|
||||
t.Fatalf("time.timer module not registered")
|
||||
}
|
||||
@@ -34,6 +35,51 @@ func TestTimeTimerFromRegistry(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestGoodTimeTimer(t *testing.T) {
|
||||
|
||||
testCases := []struct {
|
||||
name string
|
||||
params map[string]any
|
||||
}{
|
||||
{
|
||||
name: "minimal config",
|
||||
params: map[string]any{
|
||||
"duration": 2000,
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
for _, test := range testCases {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
|
||||
registration, ok := module.GetModuleRegistration("time.timer")
|
||||
if !ok {
|
||||
t.Fatalf("time.timer module not registered")
|
||||
}
|
||||
|
||||
moduleInstance, err := registration.New(config.ModuleConfig{
|
||||
Id: "test",
|
||||
Type: "time.timer",
|
||||
Params: test.params,
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
t.Fatalf("time.timer failed to create module: %s", err)
|
||||
}
|
||||
// TODO(jwetzell) this is kind of hacky
|
||||
go func() {
|
||||
time.Sleep(1 * time.Second)
|
||||
moduleInstance.Stop()
|
||||
}()
|
||||
err = moduleInstance.Start(t.Context(), nil)
|
||||
|
||||
if err != nil {
|
||||
t.Fatalf("time.timer failed to start: %s", err)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestBadTimeTimer(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
@@ -57,7 +103,7 @@ func TestBadTimeTimer(t *testing.T) {
|
||||
for _, test := range tests {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
|
||||
registration, ok := module.ModuleRegistry["time.timer"]
|
||||
registration, ok := module.GetModuleRegistration("time.timer")
|
||||
if !ok {
|
||||
t.Fatalf("time.timer module not registered")
|
||||
}
|
||||
|
||||
@@ -2,13 +2,14 @@ package module_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/jwetzell/showbridge-go/internal/config"
|
||||
"github.com/jwetzell/showbridge-go/internal/module"
|
||||
)
|
||||
|
||||
func TestUDPClientFromRegistry(t *testing.T) {
|
||||
registration, ok := module.ModuleRegistry["net.udp.client"]
|
||||
registration, ok := module.GetModuleRegistration("net.udp.client")
|
||||
if !ok {
|
||||
t.Fatalf("udp.client module not registered")
|
||||
}
|
||||
@@ -36,6 +37,53 @@ func TestUDPClientFromRegistry(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestGoodUDPClient(t *testing.T) {
|
||||
|
||||
testCases := []struct {
|
||||
name string
|
||||
params map[string]any
|
||||
}{
|
||||
{
|
||||
name: "minimal config",
|
||||
params: map[string]any{
|
||||
"host": "localhost",
|
||||
"port": 8000,
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
for _, test := range testCases {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
|
||||
registration, ok := module.GetModuleRegistration("net.udp.client")
|
||||
if !ok {
|
||||
t.Fatalf("net.udp.client module not registered")
|
||||
}
|
||||
|
||||
moduleInstance, err := registration.New(config.ModuleConfig{
|
||||
Id: "test",
|
||||
Type: "net.udp.client",
|
||||
Params: test.params,
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
t.Fatalf("net.udp.client failed to create module: %s", err)
|
||||
}
|
||||
// TODO(jwetzell) this is kind of hacky
|
||||
go func() {
|
||||
time.Sleep(1 * time.Second)
|
||||
moduleInstance.Stop()
|
||||
}()
|
||||
|
||||
err = moduleInstance.Start(t.Context(), nil)
|
||||
|
||||
if err != nil {
|
||||
t.Fatalf("net.udp.client failed to start: %s", err)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestBadUDPClient(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
@@ -77,7 +125,7 @@ func TestBadUDPClient(t *testing.T) {
|
||||
for _, test := range tests {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
|
||||
registration, ok := module.ModuleRegistry["net.udp.client"]
|
||||
registration, ok := module.GetModuleRegistration("net.udp.client")
|
||||
if !ok {
|
||||
t.Fatalf("net.udp.client module not registered")
|
||||
}
|
||||
|
||||
@@ -8,7 +8,7 @@ import (
|
||||
)
|
||||
|
||||
func TestUDPMulticastFromRegistry(t *testing.T) {
|
||||
registration, ok := module.ModuleRegistry["net.udp.multicast"]
|
||||
registration, ok := module.GetModuleRegistration("net.udp.multicast")
|
||||
if !ok {
|
||||
t.Fatalf("udp.multicast module not registered")
|
||||
}
|
||||
@@ -84,7 +84,7 @@ func TestBadUDPMulticast(t *testing.T) {
|
||||
for _, test := range tests {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
|
||||
registration, ok := module.ModuleRegistry["net.udp.multicast"]
|
||||
registration, ok := module.GetModuleRegistration("net.udp.multicast")
|
||||
if !ok {
|
||||
t.Fatalf("net.udp.multicast module not registered")
|
||||
}
|
||||
|
||||
@@ -2,13 +2,14 @@ package module_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/jwetzell/showbridge-go/internal/config"
|
||||
"github.com/jwetzell/showbridge-go/internal/module"
|
||||
)
|
||||
|
||||
func TestUDPServerFromRegistry(t *testing.T) {
|
||||
registration, ok := module.ModuleRegistry["net.udp.server"]
|
||||
registration, ok := module.GetModuleRegistration("net.udp.server")
|
||||
if !ok {
|
||||
t.Fatalf("net.udp.server module not registered")
|
||||
}
|
||||
@@ -34,6 +35,52 @@ func TestUDPServerFromRegistry(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestGoodUDPServer(t *testing.T) {
|
||||
|
||||
testCases := []struct {
|
||||
name string
|
||||
params map[string]any
|
||||
}{
|
||||
{
|
||||
name: "minimal config",
|
||||
params: map[string]any{
|
||||
"port": 8000,
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
for _, test := range testCases {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
|
||||
registration, ok := module.GetModuleRegistration("net.udp.server")
|
||||
if !ok {
|
||||
t.Fatalf("net.udp.server module not registered")
|
||||
}
|
||||
|
||||
moduleInstance, err := registration.New(config.ModuleConfig{
|
||||
Id: "test",
|
||||
Type: "net.udp.server",
|
||||
Params: test.params,
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
t.Fatalf("net.udp.server failed to create module: %s", err)
|
||||
}
|
||||
// TODO(jwetzell) this is kind of hacky
|
||||
go func() {
|
||||
time.Sleep(1 * time.Second)
|
||||
moduleInstance.Stop()
|
||||
}()
|
||||
|
||||
err = moduleInstance.Start(t.Context(), nil)
|
||||
|
||||
if err != nil {
|
||||
t.Fatalf("net.udp.server failed to start: %s", err)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestBadUDPServer(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
@@ -81,7 +128,7 @@ func TestBadUDPServer(t *testing.T) {
|
||||
for _, test := range tests {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
|
||||
registration, ok := module.ModuleRegistry["net.udp.server"]
|
||||
registration, ok := module.GetModuleRegistration("net.udp.server")
|
||||
if !ok {
|
||||
t.Fatalf("net.udp.server module not registered")
|
||||
}
|
||||
|
||||
@@ -8,7 +8,7 @@ import (
|
||||
)
|
||||
|
||||
func TestWebSocketClientFromRegistry(t *testing.T) {
|
||||
registration, ok := module.ModuleRegistry["websocket.client"]
|
||||
registration, ok := module.GetModuleRegistration("websocket.client")
|
||||
if !ok {
|
||||
t.Fatalf("websocket.client module not registered")
|
||||
}
|
||||
@@ -64,7 +64,7 @@ func TestBadWebSocketClient(t *testing.T) {
|
||||
for _, test := range tests {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
|
||||
registration, ok := module.ModuleRegistry["websocket.client"]
|
||||
registration, ok := module.GetModuleRegistration("websocket.client")
|
||||
if !ok {
|
||||
t.Fatalf("websocket.client module not registered")
|
||||
}
|
||||
|
||||
@@ -11,16 +11,6 @@ import (
|
||||
"github.com/jwetzell/showbridge-go/internal/config"
|
||||
)
|
||||
|
||||
type TimeInterval struct {
|
||||
config config.ModuleConfig
|
||||
Duration uint32
|
||||
ctx context.Context
|
||||
router common.RouteIO
|
||||
ticker *time.Ticker
|
||||
logger *slog.Logger
|
||||
cancel context.CancelFunc
|
||||
}
|
||||
|
||||
func init() {
|
||||
RegisterModule(ModuleRegistration{
|
||||
Type: "time.interval",
|
||||
@@ -30,8 +20,8 @@ func init() {
|
||||
Properties: map[string]*jsonschema.Schema{
|
||||
"duration": {
|
||||
Title: "Duration",
|
||||
Description: "time in milliseconds between emitted events",
|
||||
Type: "integer",
|
||||
Description: "Interval duration in milliseconds",
|
||||
},
|
||||
},
|
||||
Required: []string{"duration"},
|
||||
@@ -49,6 +39,16 @@ func init() {
|
||||
})
|
||||
}
|
||||
|
||||
type TimeInterval struct {
|
||||
config config.ModuleConfig
|
||||
Duration uint32
|
||||
ctx context.Context
|
||||
inputHandler common.InputHandler
|
||||
ticker *time.Ticker
|
||||
logger *slog.Logger
|
||||
cancel context.CancelFunc
|
||||
}
|
||||
|
||||
func (i *TimeInterval) Id() string {
|
||||
return i.config.Id
|
||||
}
|
||||
@@ -57,9 +57,9 @@ func (i *TimeInterval) Type() string {
|
||||
return i.config.Type
|
||||
}
|
||||
|
||||
func (i *TimeInterval) Start(ctx context.Context, router common.RouteIO) error {
|
||||
func (i *TimeInterval) Start(ctx context.Context, inputHandler common.InputHandler) error {
|
||||
i.logger.Debug("running")
|
||||
i.router = router
|
||||
i.inputHandler = inputHandler
|
||||
moduleContext, cancel := context.WithCancel(ctx)
|
||||
i.ctx = moduleContext
|
||||
i.cancel = cancel
|
||||
@@ -67,25 +67,26 @@ func (i *TimeInterval) Start(ctx context.Context, router common.RouteIO) error {
|
||||
ticker := time.NewTicker(time.Millisecond * time.Duration(i.Duration))
|
||||
i.ticker = ticker
|
||||
|
||||
for {
|
||||
for i.ctx.Err() == nil {
|
||||
select {
|
||||
case <-i.ctx.Done():
|
||||
return nil
|
||||
case <-ticker.C:
|
||||
if i.router != nil {
|
||||
i.router.HandleInput(i.ctx, i.Id(), time.Now())
|
||||
if i.inputHandler != nil {
|
||||
i.inputHandler(i.ctx, i.Id(), time.Now())
|
||||
}
|
||||
default:
|
||||
continue
|
||||
}
|
||||
}
|
||||
<-i.ctx.Done()
|
||||
i.logger.Debug("done")
|
||||
return nil
|
||||
}
|
||||
|
||||
func (i *TimeInterval) Stop() {
|
||||
if i.cancel != nil {
|
||||
i.cancel()
|
||||
defer i.cancel()
|
||||
}
|
||||
if i.ticker != nil {
|
||||
i.ticker.Stop()
|
||||
i.ticker = nil
|
||||
}
|
||||
i.logger.Debug("done")
|
||||
}
|
||||
|
||||
@@ -11,16 +11,6 @@ import (
|
||||
"github.com/jwetzell/showbridge-go/internal/config"
|
||||
)
|
||||
|
||||
type TimeTimer struct {
|
||||
config config.ModuleConfig
|
||||
Duration uint32
|
||||
ctx context.Context
|
||||
router common.RouteIO
|
||||
timer *time.Timer
|
||||
logger *slog.Logger
|
||||
cancel context.CancelFunc
|
||||
}
|
||||
|
||||
func init() {
|
||||
RegisterModule(ModuleRegistration{
|
||||
Type: "time.timer",
|
||||
@@ -30,8 +20,8 @@ func init() {
|
||||
Properties: map[string]*jsonschema.Schema{
|
||||
"duration": {
|
||||
Title: "Duration",
|
||||
Description: "time in milliseconds before the timer fires",
|
||||
Type: "integer",
|
||||
Description: "Interval duration in milliseconds",
|
||||
},
|
||||
},
|
||||
Required: []string{"duration"},
|
||||
@@ -50,6 +40,16 @@ func init() {
|
||||
})
|
||||
}
|
||||
|
||||
type TimeTimer struct {
|
||||
config config.ModuleConfig
|
||||
Duration uint32
|
||||
ctx context.Context
|
||||
inputHandler common.InputHandler
|
||||
timer *time.Timer
|
||||
logger *slog.Logger
|
||||
cancel context.CancelFunc
|
||||
}
|
||||
|
||||
func (t *TimeTimer) Id() string {
|
||||
return t.config.Id
|
||||
}
|
||||
@@ -58,33 +58,34 @@ func (t *TimeTimer) Type() string {
|
||||
return t.config.Type
|
||||
}
|
||||
|
||||
func (t *TimeTimer) Start(ctx context.Context, router common.RouteIO) error {
|
||||
func (t *TimeTimer) Start(ctx context.Context, inputHandler common.InputHandler) error {
|
||||
t.logger.Debug("running")
|
||||
t.router = router
|
||||
t.inputHandler = inputHandler
|
||||
moduleContext, cancel := context.WithCancel(ctx)
|
||||
t.ctx = moduleContext
|
||||
t.cancel = cancel
|
||||
|
||||
t.timer = time.NewTimer(time.Millisecond * time.Duration(t.Duration))
|
||||
for {
|
||||
for t.ctx.Err() == nil {
|
||||
select {
|
||||
case <-t.ctx.Done():
|
||||
return nil
|
||||
case time := <-t.timer.C:
|
||||
if t.router != nil {
|
||||
t.router.HandleInput(t.ctx, t.Id(), time)
|
||||
if t.inputHandler != nil {
|
||||
t.inputHandler(t.ctx, t.Id(), time)
|
||||
}
|
||||
}
|
||||
}
|
||||
<-t.ctx.Done()
|
||||
t.logger.Debug("done")
|
||||
return nil
|
||||
}
|
||||
|
||||
func (t *TimeTimer) Stop() {
|
||||
if t.cancel != nil {
|
||||
t.cancel()
|
||||
defer t.cancel()
|
||||
}
|
||||
if t.timer != nil {
|
||||
t.timer.Stop()
|
||||
t.timer = nil
|
||||
}
|
||||
t.logger.Debug("done")
|
||||
}
|
||||
|
||||
@@ -13,18 +13,6 @@ import (
|
||||
"github.com/jwetzell/showbridge-go/internal/config"
|
||||
)
|
||||
|
||||
type UDPClient struct {
|
||||
config config.ModuleConfig
|
||||
Addr *net.UDPAddr
|
||||
Port uint16
|
||||
conn *net.UDPConn
|
||||
ctx context.Context
|
||||
router common.RouteIO
|
||||
logger *slog.Logger
|
||||
cancel context.CancelFunc
|
||||
connMu sync.Mutex
|
||||
}
|
||||
|
||||
func init() {
|
||||
RegisterModule(ModuleRegistration{
|
||||
Type: "net.udp.client",
|
||||
@@ -33,14 +21,16 @@ func init() {
|
||||
Type: "object",
|
||||
Properties: map[string]*jsonschema.Schema{
|
||||
"host": {
|
||||
Title: "Host",
|
||||
Type: "string",
|
||||
Title: "Host",
|
||||
Description: "the hostname or IP address of the UDP server to connect to",
|
||||
Type: "string",
|
||||
},
|
||||
"port": {
|
||||
Title: "Port",
|
||||
Type: "integer",
|
||||
Minimum: jsonschema.Ptr[float64](1),
|
||||
Maximum: jsonschema.Ptr[float64](65535),
|
||||
Title: "Port",
|
||||
Description: "the port of the UDP server to connect to",
|
||||
Type: "integer",
|
||||
Minimum: jsonschema.Ptr[float64](1),
|
||||
Maximum: jsonschema.Ptr[float64](65535),
|
||||
},
|
||||
},
|
||||
Required: []string{"host", "port"},
|
||||
@@ -67,6 +57,18 @@ func init() {
|
||||
})
|
||||
}
|
||||
|
||||
type UDPClient struct {
|
||||
config config.ModuleConfig
|
||||
Addr *net.UDPAddr
|
||||
Port uint16
|
||||
conn *net.UDPConn
|
||||
ctx context.Context
|
||||
inputHandler common.InputHandler
|
||||
logger *slog.Logger
|
||||
cancel context.CancelFunc
|
||||
connMu sync.Mutex
|
||||
}
|
||||
|
||||
func (uc *UDPClient) Id() string {
|
||||
return uc.config.Id
|
||||
}
|
||||
@@ -83,9 +85,9 @@ func (uc *UDPClient) SetupConn() error {
|
||||
return err
|
||||
}
|
||||
|
||||
func (uc *UDPClient) Start(ctx context.Context, router common.RouteIO) error {
|
||||
func (uc *UDPClient) Start(ctx context.Context, inputHandler common.InputHandler) error {
|
||||
uc.logger.Debug("running")
|
||||
uc.router = router
|
||||
uc.inputHandler = inputHandler
|
||||
moduleContext, cancel := context.WithCancel(ctx)
|
||||
uc.ctx = moduleContext
|
||||
uc.cancel = cancel
|
||||
@@ -96,6 +98,7 @@ func (uc *UDPClient) Start(ctx context.Context, router common.RouteIO) error {
|
||||
}
|
||||
|
||||
<-uc.ctx.Done()
|
||||
uc.logger.Debug("done")
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -120,14 +123,11 @@ func (uc *UDPClient) Output(ctx context.Context, payload any) error {
|
||||
|
||||
func (uc *UDPClient) Stop() {
|
||||
if uc.cancel != nil {
|
||||
uc.cancel()
|
||||
defer uc.cancel()
|
||||
}
|
||||
uc.connMu.Lock()
|
||||
defer uc.connMu.Unlock()
|
||||
if uc.conn != nil {
|
||||
uc.conn.Close()
|
||||
uc.conn = nil
|
||||
}
|
||||
|
||||
uc.logger.Debug("done")
|
||||
}
|
||||
|
||||
@@ -14,17 +14,6 @@ import (
|
||||
"github.com/jwetzell/showbridge-go/internal/config"
|
||||
)
|
||||
|
||||
type UDPMulticast struct {
|
||||
config config.ModuleConfig
|
||||
conn *net.UDPConn
|
||||
ctx context.Context
|
||||
router common.RouteIO
|
||||
Addr *net.UDPAddr
|
||||
logger *slog.Logger
|
||||
cancel context.CancelFunc
|
||||
connMu sync.Mutex
|
||||
}
|
||||
|
||||
func init() {
|
||||
RegisterModule(ModuleRegistration{
|
||||
Type: "net.udp.multicast",
|
||||
@@ -33,14 +22,16 @@ func init() {
|
||||
Type: "object",
|
||||
Properties: map[string]*jsonschema.Schema{
|
||||
"ip": {
|
||||
Title: "IP",
|
||||
Type: "string",
|
||||
Title: "IP",
|
||||
Description: "the multicast address to listen on",
|
||||
Type: "string",
|
||||
},
|
||||
"port": {
|
||||
Title: "Port",
|
||||
Type: "integer",
|
||||
Minimum: jsonschema.Ptr[float64](1024),
|
||||
Maximum: jsonschema.Ptr[float64](65535),
|
||||
Title: "Port",
|
||||
Description: "the port to listen on",
|
||||
Type: "integer",
|
||||
Minimum: jsonschema.Ptr[float64](1024),
|
||||
Maximum: jsonschema.Ptr[float64](65535),
|
||||
},
|
||||
},
|
||||
Required: []string{"ip", "port"},
|
||||
@@ -67,6 +58,17 @@ func init() {
|
||||
})
|
||||
}
|
||||
|
||||
type UDPMulticast struct {
|
||||
config config.ModuleConfig
|
||||
conn *net.UDPConn
|
||||
ctx context.Context
|
||||
inputHandler common.InputHandler
|
||||
Addr *net.UDPAddr
|
||||
logger *slog.Logger
|
||||
cancel context.CancelFunc
|
||||
connMu sync.Mutex
|
||||
}
|
||||
|
||||
func (um *UDPMulticast) Id() string {
|
||||
return um.config.Id
|
||||
}
|
||||
@@ -75,9 +77,9 @@ func (um *UDPMulticast) Type() string {
|
||||
return um.config.Type
|
||||
}
|
||||
|
||||
func (um *UDPMulticast) Start(ctx context.Context, router common.RouteIO) error {
|
||||
func (um *UDPMulticast) Start(ctx context.Context, inputHandler common.InputHandler) error {
|
||||
um.logger.Debug("running")
|
||||
um.router = router
|
||||
um.inputHandler = inputHandler
|
||||
moduleContext, cancel := context.WithCancel(ctx)
|
||||
um.ctx = moduleContext
|
||||
um.cancel = cancel
|
||||
@@ -93,35 +95,34 @@ func (um *UDPMulticast) Start(ctx context.Context, router common.RouteIO) error
|
||||
um.connMu.Unlock()
|
||||
|
||||
buffer := make([]byte, 2048)
|
||||
for {
|
||||
select {
|
||||
case <-um.ctx.Done():
|
||||
return nil
|
||||
default:
|
||||
um.connMu.Lock()
|
||||
um.conn.SetDeadline(time.Now().Add(time.Millisecond * 200))
|
||||
for um.ctx.Err() == nil {
|
||||
um.conn.SetDeadline(time.Now().Add(time.Millisecond * 200))
|
||||
|
||||
numBytes, _, err := um.conn.ReadFromUDP(buffer)
|
||||
um.connMu.Unlock()
|
||||
if err != nil {
|
||||
//NOTE(jwetzell) we hit deadline
|
||||
if opErr, ok := err.(*net.OpError); ok && opErr.Timeout() {
|
||||
continue
|
||||
}
|
||||
return err
|
||||
numBytes, _, err := um.conn.ReadFromUDP(buffer)
|
||||
if err != nil {
|
||||
if errors.Is(err, net.ErrClosed) {
|
||||
break
|
||||
}
|
||||
//NOTE(jwetzell) we hit deadline
|
||||
if opErr, ok := err.(*net.OpError); ok && opErr.Timeout() {
|
||||
continue
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
if numBytes > 0 {
|
||||
message := buffer[:numBytes]
|
||||
if numBytes > 0 {
|
||||
message := buffer[:numBytes]
|
||||
|
||||
if um.router != nil {
|
||||
um.router.HandleInput(um.ctx, um.Id(), message)
|
||||
} else {
|
||||
um.logger.Error("input received but no router is configured")
|
||||
}
|
||||
if um.inputHandler != nil {
|
||||
um.inputHandler(um.ctx, um.Id(), message)
|
||||
} else {
|
||||
um.logger.Error("input received but no input handler is configured")
|
||||
}
|
||||
}
|
||||
}
|
||||
<-um.ctx.Done()
|
||||
um.logger.Debug("done")
|
||||
return nil
|
||||
}
|
||||
|
||||
func (um *UDPMulticast) Output(ctx context.Context, payload any) error {
|
||||
@@ -141,13 +142,11 @@ func (um *UDPMulticast) Output(ctx context.Context, payload any) error {
|
||||
|
||||
func (um *UDPMulticast) Stop() {
|
||||
if um.cancel != nil {
|
||||
um.cancel()
|
||||
defer um.cancel()
|
||||
}
|
||||
um.connMu.Lock()
|
||||
defer um.connMu.Unlock()
|
||||
if um.conn != nil {
|
||||
um.conn.Close()
|
||||
um.conn = nil
|
||||
}
|
||||
um.logger.Debug("done")
|
||||
}
|
||||
|
||||
@@ -15,18 +15,6 @@ import (
|
||||
"github.com/jwetzell/showbridge-go/internal/config"
|
||||
)
|
||||
|
||||
type UDPServer struct {
|
||||
Addr *net.UDPAddr
|
||||
BufferSize int
|
||||
config config.ModuleConfig
|
||||
ctx context.Context
|
||||
router common.RouteIO
|
||||
logger *slog.Logger
|
||||
cancel context.CancelFunc
|
||||
listener *net.UDPConn
|
||||
listenerMu sync.Mutex
|
||||
}
|
||||
|
||||
func init() {
|
||||
RegisterModule(ModuleRegistration{
|
||||
Type: "net.udp.server",
|
||||
@@ -35,22 +23,25 @@ func init() {
|
||||
Type: "object",
|
||||
Properties: map[string]*jsonschema.Schema{
|
||||
"ip": {
|
||||
Title: "IP",
|
||||
Type: "string",
|
||||
Default: json.RawMessage(`"0.0.0.0"`),
|
||||
Title: "IP",
|
||||
Description: "the IP address to bind the UDP server to",
|
||||
Type: "string",
|
||||
Default: json.RawMessage(`"0.0.0.0"`),
|
||||
},
|
||||
"port": {
|
||||
Title: "Port",
|
||||
Type: "integer",
|
||||
Minimum: jsonschema.Ptr[float64](1024),
|
||||
Maximum: jsonschema.Ptr[float64](65535),
|
||||
Title: "Port",
|
||||
Description: "the port for the UDP server to listen on",
|
||||
Type: "integer",
|
||||
Minimum: jsonschema.Ptr[float64](1024),
|
||||
Maximum: jsonschema.Ptr[float64](65535),
|
||||
},
|
||||
"bufferSize": {
|
||||
Title: "Buffer Size",
|
||||
Type: "integer",
|
||||
Minimum: jsonschema.Ptr[float64](1),
|
||||
Maximum: jsonschema.Ptr[float64](65535),
|
||||
Default: json.RawMessage("2048"),
|
||||
Title: "Buffer Size",
|
||||
Description: "the size of the buffer for incoming UDP messages",
|
||||
Type: "integer",
|
||||
Minimum: jsonschema.Ptr[float64](1),
|
||||
Maximum: jsonschema.Ptr[float64](65535),
|
||||
Default: json.RawMessage("2048"),
|
||||
},
|
||||
},
|
||||
Required: []string{"port"},
|
||||
@@ -90,6 +81,18 @@ func init() {
|
||||
})
|
||||
}
|
||||
|
||||
type UDPServer struct {
|
||||
Addr *net.UDPAddr
|
||||
BufferSize int
|
||||
config config.ModuleConfig
|
||||
ctx context.Context
|
||||
inputHandler common.InputHandler
|
||||
logger *slog.Logger
|
||||
cancel context.CancelFunc
|
||||
listener *net.UDPConn
|
||||
listenerMu sync.Mutex
|
||||
}
|
||||
|
||||
func (us *UDPServer) Id() string {
|
||||
return us.config.Id
|
||||
}
|
||||
@@ -98,9 +101,9 @@ func (us *UDPServer) Type() string {
|
||||
return us.config.Type
|
||||
}
|
||||
|
||||
func (us *UDPServer) Start(ctx context.Context, router common.RouteIO) error {
|
||||
func (us *UDPServer) Start(ctx context.Context, inputHandler common.InputHandler) error {
|
||||
us.logger.Debug("running")
|
||||
us.router = router
|
||||
us.inputHandler = inputHandler
|
||||
moduleContext, cancel := context.WithCancel(ctx)
|
||||
us.ctx = moduleContext
|
||||
us.cancel = cancel
|
||||
@@ -111,32 +114,29 @@ func (us *UDPServer) Start(ctx context.Context, router common.RouteIO) error {
|
||||
}
|
||||
us.listenerMu.Lock()
|
||||
us.listener = listener
|
||||
us.listenerMu.Unlock()
|
||||
|
||||
buffer := make([]byte, us.BufferSize)
|
||||
for us.ctx.Err() == nil {
|
||||
select {
|
||||
case <-us.ctx.Done():
|
||||
return nil
|
||||
default:
|
||||
listener.SetDeadline(time.Now().Add(time.Millisecond * 200))
|
||||
listener.SetDeadline(time.Now().Add(time.Millisecond * 200))
|
||||
|
||||
numBytes, _, err := listener.ReadFromUDP(buffer)
|
||||
if err != nil {
|
||||
//NOTE(jwetzell) we hit deadline
|
||||
if opErr, ok := err.(*net.OpError); ok && opErr.Timeout() {
|
||||
continue
|
||||
}
|
||||
return err
|
||||
}
|
||||
message := buffer[:numBytes]
|
||||
if us.router != nil {
|
||||
us.router.HandleInput(us.ctx, us.Id(), message)
|
||||
} else {
|
||||
us.logger.Error("input received but no router is configured")
|
||||
numBytes, _, err := listener.ReadFromUDP(buffer)
|
||||
if err != nil {
|
||||
//NOTE(jwetzell) we hit deadline
|
||||
if opErr, ok := err.(*net.OpError); ok && opErr.Timeout() {
|
||||
continue
|
||||
}
|
||||
break
|
||||
}
|
||||
message := buffer[:numBytes]
|
||||
if us.inputHandler != nil {
|
||||
us.inputHandler(us.ctx, us.Id(), message)
|
||||
} else {
|
||||
us.logger.Error("input received but no input handler is configured")
|
||||
}
|
||||
}
|
||||
us.listenerMu.Unlock()
|
||||
<-us.ctx.Done()
|
||||
us.logger.Debug("done")
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -146,13 +146,11 @@ func (us *UDPServer) Output(ctx context.Context, payload any) error {
|
||||
|
||||
func (us *UDPServer) Stop() {
|
||||
if us.cancel != nil {
|
||||
us.cancel()
|
||||
defer us.cancel()
|
||||
}
|
||||
us.listenerMu.Lock()
|
||||
defer us.listenerMu.Unlock()
|
||||
if us.listener != nil {
|
||||
us.listener.Close()
|
||||
us.listener = nil
|
||||
}
|
||||
us.logger.Debug("done")
|
||||
}
|
||||
|
||||
@@ -5,7 +5,6 @@ import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"net"
|
||||
"net/url"
|
||||
"sync"
|
||||
"time"
|
||||
@@ -16,17 +15,6 @@ import (
|
||||
"github.com/jwetzell/showbridge-go/internal/config"
|
||||
)
|
||||
|
||||
type WebSocketClient struct {
|
||||
config config.ModuleConfig
|
||||
URL url.URL
|
||||
ctx context.Context
|
||||
conn *websocket.Conn
|
||||
router common.RouteIO
|
||||
logger *slog.Logger
|
||||
cancel context.CancelFunc
|
||||
connMu sync.Mutex
|
||||
}
|
||||
|
||||
func init() {
|
||||
RegisterModule(ModuleRegistration{
|
||||
Type: "websocket.client",
|
||||
@@ -35,8 +23,9 @@ func init() {
|
||||
Type: "object",
|
||||
Properties: map[string]*jsonschema.Schema{
|
||||
"url": {
|
||||
Title: "URL",
|
||||
Type: "string",
|
||||
Title: "URL",
|
||||
Description: "the URL of the WebSocket server to connect",
|
||||
Type: "string",
|
||||
},
|
||||
},
|
||||
Required: []string{"url"},
|
||||
@@ -63,6 +52,17 @@ func init() {
|
||||
})
|
||||
}
|
||||
|
||||
type WebSocketClient struct {
|
||||
config config.ModuleConfig
|
||||
URL url.URL
|
||||
ctx context.Context
|
||||
conn *websocket.Conn
|
||||
inputHandler common.InputHandler
|
||||
logger *slog.Logger
|
||||
cancel context.CancelFunc
|
||||
connMu sync.Mutex
|
||||
}
|
||||
|
||||
func (wc *WebSocketClient) Id() string {
|
||||
return wc.config.Id
|
||||
}
|
||||
@@ -74,14 +74,37 @@ func (wc *WebSocketClient) Type() string {
|
||||
func (wc *WebSocketClient) SetupConn() error {
|
||||
wc.connMu.Lock()
|
||||
defer wc.connMu.Unlock()
|
||||
if wc.conn != nil {
|
||||
wc.conn.Close()
|
||||
}
|
||||
conn, _, err := websocket.DefaultDialer.Dial(wc.URL.String(), nil)
|
||||
|
||||
if err != nil {
|
||||
return fmt.Errorf("websocket.client dial error: %w", err)
|
||||
}
|
||||
|
||||
conn.SetCloseHandler(func(code int, text string) error {
|
||||
// NOTE(jwetzell): attempt to send close message back to server before closing connection
|
||||
err := wc.conn.WriteControl(
|
||||
websocket.CloseMessage,
|
||||
websocket.FormatCloseMessage(websocket.CloseNormalClosure, ""),
|
||||
time.Now().Add(time.Minute),
|
||||
)
|
||||
wc.connMu.Lock()
|
||||
defer wc.connMu.Unlock()
|
||||
if wc.conn != nil {
|
||||
wc.conn.Close()
|
||||
}
|
||||
return err
|
||||
})
|
||||
|
||||
wc.conn = conn
|
||||
return err
|
||||
}
|
||||
|
||||
func (wc *WebSocketClient) Start(ctx context.Context, router common.RouteIO) error {
|
||||
func (wc *WebSocketClient) Start(ctx context.Context, inputHandler common.InputHandler) error {
|
||||
wc.logger.Debug("running")
|
||||
wc.router = router
|
||||
wc.inputHandler = inputHandler
|
||||
moduleContext, cancel := context.WithCancel(ctx)
|
||||
wc.ctx = moduleContext
|
||||
wc.cancel = cancel
|
||||
@@ -92,13 +115,13 @@ func (wc *WebSocketClient) Start(ctx context.Context, router common.RouteIO) err
|
||||
wc.logger.Error("connection error", "error", err)
|
||||
} else {
|
||||
// NOTE(jwetzell): enter read loop until an error occurs
|
||||
wc.logger.Debug("websocket connection established entering read loop")
|
||||
wc.readLoop()
|
||||
}
|
||||
// NOTE(jwetzell): if connection is lost or read error wait before trying again
|
||||
time.Sleep(2 * time.Second)
|
||||
}
|
||||
<-wc.ctx.Done()
|
||||
wc.logger.Debug("done")
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -108,33 +131,31 @@ func (wc *WebSocketClient) readLoop() {
|
||||
wc.logger.Error("websocket connection is not established")
|
||||
return
|
||||
}
|
||||
// TODO(jwetzell): other ways to timeout?
|
||||
wc.conn.SetReadDeadline(time.Now().Add(5 * time.Second))
|
||||
messageType, message, err := wc.conn.ReadMessage()
|
||||
if err != nil {
|
||||
if opErr, ok := err.(*net.OpError); ok {
|
||||
// NOTE(jwetzell) we hit deadline
|
||||
if opErr.Timeout() {
|
||||
continue
|
||||
}
|
||||
// NOTE(jwetzell) connection was closed
|
||||
if errors.Is(opErr, net.ErrClosed) {
|
||||
continue
|
||||
}
|
||||
}
|
||||
wc.logger.Error("websocket read error", "error", err)
|
||||
return
|
||||
}
|
||||
if wc.router != nil {
|
||||
if wc.inputHandler != nil {
|
||||
switch messageType {
|
||||
case websocket.CloseMessage:
|
||||
return
|
||||
case websocket.PingMessage:
|
||||
err := wc.conn.WriteMessage(websocket.PongMessage, nil)
|
||||
if err != nil {
|
||||
wc.logger.Error("websocket pong error", "error", err)
|
||||
return
|
||||
}
|
||||
case websocket.TextMessage:
|
||||
wc.router.HandleInput(wc.ctx, wc.Id(), string(message))
|
||||
wc.inputHandler(wc.ctx, wc.Id(), string(message))
|
||||
case websocket.BinaryMessage:
|
||||
wc.router.HandleInput(wc.ctx, wc.Id(), message)
|
||||
wc.inputHandler(wc.ctx, wc.Id(), message)
|
||||
default:
|
||||
wc.logger.Warn("unsupported message type received", "messageType", messageType)
|
||||
}
|
||||
} else {
|
||||
wc.logger.Error("input received but no router is configured")
|
||||
wc.logger.Error("input received but no input handler is configured")
|
||||
continue
|
||||
}
|
||||
}
|
||||
@@ -182,13 +203,15 @@ func (wc *WebSocketClient) Output(ctx context.Context, payload any) error {
|
||||
|
||||
func (wc *WebSocketClient) Stop() {
|
||||
if wc.cancel != nil {
|
||||
wc.cancel()
|
||||
defer wc.cancel()
|
||||
}
|
||||
wc.connMu.Lock()
|
||||
defer wc.connMu.Unlock()
|
||||
if wc.conn != nil {
|
||||
err := wc.conn.WriteControl(websocket.CloseMessage, nil, time.Now().Add(time.Minute))
|
||||
if err != nil {
|
||||
wc.logger.Error("websocket close error", "error", err)
|
||||
}
|
||||
wc.conn.Close()
|
||||
wc.conn = nil
|
||||
}
|
||||
wc.logger.Debug("done")
|
||||
}
|
||||
|
||||
@@ -9,6 +9,16 @@ import (
|
||||
"github.com/jwetzell/showbridge-go/internal/config"
|
||||
)
|
||||
|
||||
func init() {
|
||||
RegisterProcessor(ProcessorRegistration{
|
||||
Type: "artnet.packet.decode",
|
||||
Title: "Decode ArtNet Packet",
|
||||
New: func(config config.ProcessorConfig) (Processor, error) {
|
||||
return &ArtNetPacketDecode{config: config}, nil
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
type ArtNetPacketDecode struct {
|
||||
config config.ProcessorConfig
|
||||
}
|
||||
@@ -36,14 +46,4 @@ func (apd *ArtNetPacketDecode) Process(ctx context.Context, wrappedPayload commo
|
||||
|
||||
func (apd *ArtNetPacketDecode) Type() string {
|
||||
return apd.config.Type
|
||||
}
|
||||
|
||||
func init() {
|
||||
RegisterProcessor(ProcessorRegistration{
|
||||
Type: "artnet.packet.decode",
|
||||
Title: "Decode ArtNet Packet",
|
||||
New: func(config config.ProcessorConfig) (Processor, error) {
|
||||
return &ArtNetPacketDecode{config: config}, nil
|
||||
},
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -9,6 +9,16 @@ import (
|
||||
"github.com/jwetzell/showbridge-go/internal/config"
|
||||
)
|
||||
|
||||
func init() {
|
||||
RegisterProcessor(ProcessorRegistration{
|
||||
Type: "artnet.packet.encode",
|
||||
Title: "Encode ArtNet Packet",
|
||||
New: func(config config.ProcessorConfig) (Processor, error) {
|
||||
return &ArtNetPacketEncode{config: config}, nil
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
type ArtNetPacketEncode struct {
|
||||
config config.ProcessorConfig
|
||||
}
|
||||
@@ -36,13 +46,3 @@ func (ape *ArtNetPacketEncode) Process(ctx context.Context, wrappedPayload commo
|
||||
func (ape *ArtNetPacketEncode) Type() string {
|
||||
return ape.config.Type
|
||||
}
|
||||
|
||||
func init() {
|
||||
RegisterProcessor(ProcessorRegistration{
|
||||
Type: "artnet.packet.encode",
|
||||
Title: "Encode ArtNet Packet",
|
||||
New: func(config config.ProcessorConfig) (Processor, error) {
|
||||
return &ArtNetPacketEncode{config: config}, nil
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
@@ -13,6 +13,51 @@ import (
|
||||
"github.com/jwetzell/showbridge-go/internal/config"
|
||||
)
|
||||
|
||||
func init() {
|
||||
RegisterProcessor(ProcessorRegistration{
|
||||
Type: "db.query",
|
||||
Title: "Query Database",
|
||||
ParamsSchema: &jsonschema.Schema{
|
||||
Type: "object",
|
||||
Properties: map[string]*jsonschema.Schema{
|
||||
"module": {
|
||||
Title: "Module ID",
|
||||
Description: "ID of the database module to query",
|
||||
Type: "string",
|
||||
},
|
||||
"query": {
|
||||
Title: "Query",
|
||||
Description: "SQL query to execute",
|
||||
Type: "string",
|
||||
},
|
||||
},
|
||||
Required: []string{"module", "query"},
|
||||
AdditionalProperties: &jsonschema.Schema{Not: &jsonschema.Schema{}},
|
||||
},
|
||||
New: func(config config.ProcessorConfig) (Processor, error) {
|
||||
|
||||
params := config.Params
|
||||
|
||||
moduleIdString, err := params.GetString("module")
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("db.query module error: %w", err)
|
||||
}
|
||||
|
||||
queryString, err := params.GetString("query")
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("db.query query error: %w", err)
|
||||
}
|
||||
|
||||
queryTemplate, err := template.New("query").Parse(queryString)
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &DbQuery{config: config, ModuleId: moduleIdString, Query: queryTemplate, logger: slog.Default().With("component", "processor", "type", config.Type)}, nil
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
type DbQuery struct {
|
||||
config config.ProcessorConfig
|
||||
ModuleId string
|
||||
@@ -42,19 +87,8 @@ func (dq *DbQuery) Process(ctx context.Context, wrappedPayload common.WrappedPay
|
||||
dq.module = dbModule
|
||||
}
|
||||
|
||||
db, err := dq.module.Database()
|
||||
if err != nil {
|
||||
wrappedPayload.End = true
|
||||
return wrappedPayload, fmt.Errorf("db.query error getting database from module: %w", err)
|
||||
}
|
||||
|
||||
if db == nil {
|
||||
wrappedPayload.End = true
|
||||
return wrappedPayload, fmt.Errorf("db.query module with id %s returned nil database", dq.ModuleId)
|
||||
}
|
||||
|
||||
var queryBuffer bytes.Buffer
|
||||
err = dq.Query.Execute(&queryBuffer, wrappedPayload)
|
||||
err := dq.Query.Execute(&queryBuffer, wrappedPayload)
|
||||
|
||||
if err != nil {
|
||||
wrappedPayload.End = true
|
||||
@@ -62,7 +96,7 @@ func (dq *DbQuery) Process(ctx context.Context, wrappedPayload common.WrappedPay
|
||||
}
|
||||
|
||||
// support proper parameterized queries
|
||||
rows, err := db.QueryContext(ctx, queryBuffer.String())
|
||||
rows, err := dq.module.QueryContext(ctx, queryBuffer.String())
|
||||
if err != nil {
|
||||
wrappedPayload.End = true
|
||||
return wrappedPayload, fmt.Errorf("db.query error executing query: %w", err)
|
||||
@@ -112,48 +146,3 @@ func (dq *DbQuery) Process(ctx context.Context, wrappedPayload common.WrappedPay
|
||||
func (dq *DbQuery) Type() string {
|
||||
return dq.config.Type
|
||||
}
|
||||
|
||||
func init() {
|
||||
RegisterProcessor(ProcessorRegistration{
|
||||
Type: "db.query",
|
||||
Title: "Query Database",
|
||||
ParamsSchema: &jsonschema.Schema{
|
||||
Type: "object",
|
||||
Properties: map[string]*jsonschema.Schema{
|
||||
"module": {
|
||||
Title: "Module ID",
|
||||
Type: "string",
|
||||
Description: "ID of the database module to query",
|
||||
},
|
||||
"query": {
|
||||
Title: "Query",
|
||||
Type: "string",
|
||||
Description: "SQL query to execute",
|
||||
},
|
||||
},
|
||||
Required: []string{"module", "query"},
|
||||
AdditionalProperties: &jsonschema.Schema{Not: &jsonschema.Schema{}},
|
||||
},
|
||||
New: func(config config.ProcessorConfig) (Processor, error) {
|
||||
|
||||
params := config.Params
|
||||
|
||||
moduleIdString, err := params.GetString("module")
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("db.query module error: %w", err)
|
||||
}
|
||||
|
||||
queryString, err := params.GetString("query")
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("db.query query error: %w", err)
|
||||
}
|
||||
|
||||
queryTemplate, err := template.New("query").Parse(queryString)
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &DbQuery{config: config, ModuleId: moduleIdString, Query: queryTemplate, logger: slog.Default().With("component", "processor", "type", config.Type)}, nil
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
@@ -9,13 +9,22 @@ import (
|
||||
"github.com/jwetzell/showbridge-go/internal/config"
|
||||
)
|
||||
|
||||
// TODO(jwetzell): maybe make a more useful logging processor
|
||||
func init() {
|
||||
RegisterProcessor(ProcessorRegistration{
|
||||
Type: "debug.log",
|
||||
Title: "Debug Log",
|
||||
New: func(config config.ProcessorConfig) (Processor, error) {
|
||||
return &DebugLog{config: config, logger: slog.Default().With("component", "processor", "type", config.Type)}, nil
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
type DebugLog struct {
|
||||
config config.ProcessorConfig
|
||||
logger *slog.Logger
|
||||
}
|
||||
|
||||
// TODO(jwetzell): maybe make a more useful logging processor
|
||||
func (dl *DebugLog) Process(ctx context.Context, wrappedPayload common.WrappedPayload) (common.WrappedPayload, error) {
|
||||
payload := wrappedPayload.Payload
|
||||
payloadString := fmt.Sprintf("%+v", payload)
|
||||
@@ -28,12 +37,3 @@ func (dl *DebugLog) Type() string {
|
||||
return dl.config.Type
|
||||
}
|
||||
|
||||
func init() {
|
||||
RegisterProcessor(ProcessorRegistration{
|
||||
Type: "debug.log",
|
||||
Title: "Debug Log",
|
||||
New: func(config config.ProcessorConfig) (Processor, error) {
|
||||
return &DebugLog{config: config, logger: slog.Default().With("component", "processor", "type", config.Type)}, nil
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
@@ -8,6 +8,16 @@ import (
|
||||
"github.com/jwetzell/showbridge-go/internal/config"
|
||||
)
|
||||
|
||||
func init() {
|
||||
RegisterProcessor(ProcessorRegistration{
|
||||
Type: "filter.change",
|
||||
Title: "Filter On Change",
|
||||
New: func(config config.ProcessorConfig) (Processor, error) {
|
||||
return &FilterChange{config: config}, nil
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
type FilterChange struct {
|
||||
config config.ProcessorConfig
|
||||
previous any
|
||||
@@ -28,13 +38,3 @@ func (fc *FilterChange) Process(ctx context.Context, wrappedPayload common.Wrapp
|
||||
func (fc *FilterChange) Type() string {
|
||||
return fc.config.Type
|
||||
}
|
||||
|
||||
func init() {
|
||||
RegisterProcessor(ProcessorRegistration{
|
||||
Type: "filter.change",
|
||||
Title: "Filter On Change",
|
||||
New: func(config config.ProcessorConfig) (Processor, error) {
|
||||
return &FilterChange{config: config}, nil
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
@@ -11,6 +11,40 @@ import (
|
||||
"github.com/jwetzell/showbridge-go/internal/config"
|
||||
)
|
||||
|
||||
func init() {
|
||||
RegisterProcessor(ProcessorRegistration{
|
||||
Type: "filter.expr",
|
||||
Title: "Filter by Expr expression",
|
||||
ParamsSchema: &jsonschema.Schema{
|
||||
Type: "object",
|
||||
Properties: map[string]*jsonschema.Schema{
|
||||
"expression": {
|
||||
Title: "Expression",
|
||||
Description: "Expr expression to evaluate, must return a boolean",
|
||||
Type: "string",
|
||||
},
|
||||
},
|
||||
Required: []string{"expression"},
|
||||
AdditionalProperties: &jsonschema.Schema{Not: &jsonschema.Schema{}},
|
||||
},
|
||||
New: func(config config.ProcessorConfig) (Processor, error) {
|
||||
params := config.Params
|
||||
|
||||
expressionString, err := params.GetString("expression")
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("filter.expr expression error: %w", err)
|
||||
}
|
||||
|
||||
program, err := expr.Compile(expressionString)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &FilterExpr{config: config, Program: program}, nil
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
// NOTE(jwetzell): see language definition https://expr-lang.org/docs/language-definition
|
||||
type FilterExpr struct {
|
||||
config config.ProcessorConfig
|
||||
@@ -45,36 +79,3 @@ func (fe *FilterExpr) Process(ctx context.Context, wrappedPayload common.Wrapped
|
||||
func (fe *FilterExpr) Type() string {
|
||||
return fe.config.Type
|
||||
}
|
||||
|
||||
func init() {
|
||||
RegisterProcessor(ProcessorRegistration{
|
||||
Type: "filter.expr",
|
||||
Title: "Filter by Expr expression",
|
||||
ParamsSchema: &jsonschema.Schema{
|
||||
Type: "object",
|
||||
Properties: map[string]*jsonschema.Schema{
|
||||
"expression": {
|
||||
Title: "Expression",
|
||||
Type: "string",
|
||||
},
|
||||
},
|
||||
Required: []string{"expression"},
|
||||
AdditionalProperties: &jsonschema.Schema{Not: &jsonschema.Schema{}},
|
||||
},
|
||||
New: func(config config.ProcessorConfig) (Processor, error) {
|
||||
params := config.Params
|
||||
|
||||
expressionString, err := params.GetString("expression")
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("filter.expr expression error: %w", err)
|
||||
}
|
||||
|
||||
program, err := expr.Compile(expressionString)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &FilterExpr{config: config, Program: program}, nil
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
@@ -0,0 +1,58 @@
|
||||
package processor
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"github.com/google/jsonschema-go/jsonschema"
|
||||
"github.com/jwetzell/showbridge-go/internal/common"
|
||||
"github.com/jwetzell/showbridge-go/internal/config"
|
||||
"golang.org/x/time/rate"
|
||||
)
|
||||
|
||||
func init() {
|
||||
RegisterProcessor(ProcessorRegistration{
|
||||
Type: "filter.rate",
|
||||
Title: "Filter by Rate",
|
||||
ParamsSchema: &jsonschema.Schema{
|
||||
Type: "object",
|
||||
Properties: map[string]*jsonschema.Schema{
|
||||
"rate": {
|
||||
Title: "Rate",
|
||||
Description: "number of events to allow per second",
|
||||
Type: "integer",
|
||||
},
|
||||
},
|
||||
Required: []string{"rate"},
|
||||
},
|
||||
New: func(config config.ProcessorConfig) (Processor, error) {
|
||||
params := config.Params
|
||||
rateInt, err := params.GetInt("rate")
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("filter.rate rate error: %w", err)
|
||||
}
|
||||
|
||||
limiter := rate.NewLimiter(rate.Limit(rateInt), rateInt*2)
|
||||
|
||||
return &FilterRate{config: config, limiter: limiter}, nil
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
type FilterRate struct {
|
||||
config config.ProcessorConfig
|
||||
limiter *rate.Limiter
|
||||
}
|
||||
|
||||
func (fc *FilterRate) Process(ctx context.Context, wrappedPayload common.WrappedPayload) (common.WrappedPayload, error) {
|
||||
err := fc.limiter.Wait(ctx)
|
||||
if err != nil {
|
||||
wrappedPayload.End = true
|
||||
return wrappedPayload, err
|
||||
}
|
||||
return wrappedPayload, nil
|
||||
}
|
||||
|
||||
func (fc *FilterRate) Type() string {
|
||||
return fc.config.Type
|
||||
}
|
||||
@@ -11,6 +11,41 @@ import (
|
||||
"github.com/jwetzell/showbridge-go/internal/config"
|
||||
)
|
||||
|
||||
func init() {
|
||||
RegisterProcessor(ProcessorRegistration{
|
||||
Type: "filter.regex",
|
||||
Title: "Filter by Regex",
|
||||
ParamsSchema: &jsonschema.Schema{
|
||||
Type: "object",
|
||||
Properties: map[string]*jsonschema.Schema{
|
||||
"pattern": {
|
||||
Title: "Pattern",
|
||||
Description: "regex pattern to match against",
|
||||
Type: "string",
|
||||
},
|
||||
},
|
||||
Required: []string{"pattern"},
|
||||
AdditionalProperties: &jsonschema.Schema{Not: &jsonschema.Schema{}},
|
||||
},
|
||||
New: func(config config.ProcessorConfig) (Processor, error) {
|
||||
params := config.Params
|
||||
|
||||
patternString, err := params.GetString("pattern")
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("filter.regex pattern error: %w", err)
|
||||
}
|
||||
|
||||
patternRegexp, err := regexp.Compile(patternString)
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &FilterRegex{config: config, Pattern: patternRegexp}, nil
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
type FilterRegex struct {
|
||||
config config.ProcessorConfig
|
||||
Pattern *regexp.Regexp
|
||||
@@ -37,37 +72,3 @@ func (fr *FilterRegex) Process(ctx context.Context, wrappedPayload common.Wrappe
|
||||
func (fr *FilterRegex) Type() string {
|
||||
return fr.config.Type
|
||||
}
|
||||
|
||||
func init() {
|
||||
RegisterProcessor(ProcessorRegistration{
|
||||
Type: "filter.regex",
|
||||
Title: "Filter by Regex",
|
||||
ParamsSchema: &jsonschema.Schema{
|
||||
Type: "object",
|
||||
Properties: map[string]*jsonschema.Schema{
|
||||
"pattern": {
|
||||
Title: "Pattern",
|
||||
Type: "string",
|
||||
},
|
||||
},
|
||||
Required: []string{"pattern"},
|
||||
AdditionalProperties: &jsonschema.Schema{Not: &jsonschema.Schema{}},
|
||||
},
|
||||
New: func(config config.ProcessorConfig) (Processor, error) {
|
||||
params := config.Params
|
||||
|
||||
patternString, err := params.GetString("pattern")
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("filter.regex pattern error: %w", err)
|
||||
}
|
||||
|
||||
patternRegexp, err := regexp.Compile(patternString)
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &FilterRegex{config: config, Pattern: patternRegexp}, nil
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
@@ -12,6 +12,39 @@ import (
|
||||
"github.com/jwetzell/showbridge-go/internal/config"
|
||||
)
|
||||
|
||||
func init() {
|
||||
RegisterProcessor(ProcessorRegistration{
|
||||
Type: "float.parse",
|
||||
Title: "Parse Float",
|
||||
ParamsSchema: &jsonschema.Schema{
|
||||
Type: "object",
|
||||
Properties: map[string]*jsonschema.Schema{
|
||||
"bitSize": {
|
||||
Title: "Bit Size",
|
||||
Description: "bit size of the resulting float",
|
||||
Type: "integer",
|
||||
Enum: []any{32, 64},
|
||||
Default: json.RawMessage("64"),
|
||||
},
|
||||
},
|
||||
AdditionalProperties: &jsonschema.Schema{Not: &jsonschema.Schema{}},
|
||||
},
|
||||
New: func(moduleConfig config.ProcessorConfig) (Processor, error) {
|
||||
params := moduleConfig.Params
|
||||
|
||||
bitSizeNum, err := params.GetInt("bitSize")
|
||||
if err != nil {
|
||||
if errors.Is(err, config.ErrParamNotFound) {
|
||||
bitSizeNum = 64
|
||||
} else {
|
||||
return nil, fmt.Errorf("float.parse bitSize error: %w", err)
|
||||
}
|
||||
}
|
||||
return &FloatParse{config: moduleConfig, BitSize: bitSizeNum}, nil
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
type FloatParse struct {
|
||||
BitSize int
|
||||
config config.ProcessorConfig
|
||||
@@ -38,35 +71,3 @@ func (fp *FloatParse) Process(ctx context.Context, wrappedPayload common.Wrapped
|
||||
func (fp *FloatParse) Type() string {
|
||||
return fp.config.Type
|
||||
}
|
||||
|
||||
func init() {
|
||||
RegisterProcessor(ProcessorRegistration{
|
||||
Type: "float.parse",
|
||||
Title: "Parse Float",
|
||||
ParamsSchema: &jsonschema.Schema{
|
||||
Type: "object",
|
||||
Properties: map[string]*jsonschema.Schema{
|
||||
"bitSize": {
|
||||
Title: "Bit Size",
|
||||
Type: "integer",
|
||||
Enum: []any{32, 64},
|
||||
Default: json.RawMessage("64"),
|
||||
},
|
||||
},
|
||||
AdditionalProperties: &jsonschema.Schema{Not: &jsonschema.Schema{}},
|
||||
},
|
||||
New: func(moduleConfig config.ProcessorConfig) (Processor, error) {
|
||||
params := moduleConfig.Params
|
||||
|
||||
bitSizeNum, err := params.GetInt("bitSize")
|
||||
if err != nil {
|
||||
if errors.Is(err, config.ErrParamNotFound) {
|
||||
bitSizeNum = 64
|
||||
} else {
|
||||
return nil, fmt.Errorf("float.parse bitSize error: %w", err)
|
||||
}
|
||||
}
|
||||
return &FloatParse{config: moduleConfig, BitSize: bitSizeNum}, nil
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
@@ -12,32 +12,6 @@ import (
|
||||
"github.com/jwetzell/showbridge-go/internal/config"
|
||||
)
|
||||
|
||||
type FloatRandom struct {
|
||||
BitSize int
|
||||
Min float64
|
||||
Max float64
|
||||
config config.ProcessorConfig
|
||||
}
|
||||
|
||||
func (fr *FloatRandom) Process(ctx context.Context, wrappedPayload common.WrappedPayload) (common.WrappedPayload, error) {
|
||||
if fr.BitSize == 32 {
|
||||
payloadFloat := rand.Float32()*(float32(fr.Max)-float32(fr.Min)) + float32(fr.Min)
|
||||
wrappedPayload.Payload = payloadFloat
|
||||
return wrappedPayload, nil
|
||||
}
|
||||
if fr.BitSize == 64 {
|
||||
payloadFloat := rand.Float64()*(fr.Max-fr.Min) + fr.Min
|
||||
wrappedPayload.Payload = payloadFloat
|
||||
return wrappedPayload, nil
|
||||
}
|
||||
wrappedPayload.End = true
|
||||
return wrappedPayload, errors.New("float.random bitSize error: must be 32 or 64")
|
||||
}
|
||||
|
||||
func (fr *FloatRandom) Type() string {
|
||||
return fr.config.Type
|
||||
}
|
||||
|
||||
func init() {
|
||||
RegisterProcessor(ProcessorRegistration{
|
||||
Type: "float.random",
|
||||
@@ -46,18 +20,21 @@ func init() {
|
||||
Type: "object",
|
||||
Properties: map[string]*jsonschema.Schema{
|
||||
"bitSize": {
|
||||
Title: "Bit Size",
|
||||
Type: "integer",
|
||||
Enum: []any{32, 64},
|
||||
Default: json.RawMessage("32"),
|
||||
Title: "Bit Size",
|
||||
Description: "bit size of the resulting float",
|
||||
Type: "integer",
|
||||
Enum: []any{32, 64},
|
||||
Default: json.RawMessage("32"),
|
||||
},
|
||||
"min": {
|
||||
Title: "Minimum",
|
||||
Type: "number",
|
||||
Title: "Minimum",
|
||||
Description: "inclusive minimum value of the random float",
|
||||
Type: "number",
|
||||
},
|
||||
"max": {
|
||||
Title: "Maximum",
|
||||
Type: "number",
|
||||
Title: "Maximum",
|
||||
Description: "exclusive maximum value of the random float",
|
||||
Type: "number",
|
||||
},
|
||||
},
|
||||
Required: []string{"min", "max"},
|
||||
@@ -97,3 +74,29 @@ func init() {
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
type FloatRandom struct {
|
||||
BitSize int
|
||||
Min float64
|
||||
Max float64
|
||||
config config.ProcessorConfig
|
||||
}
|
||||
|
||||
func (fr *FloatRandom) Process(ctx context.Context, wrappedPayload common.WrappedPayload) (common.WrappedPayload, error) {
|
||||
if fr.BitSize == 32 {
|
||||
payloadFloat := rand.Float32()*(float32(fr.Max)-float32(fr.Min)) + float32(fr.Min)
|
||||
wrappedPayload.Payload = payloadFloat
|
||||
return wrappedPayload, nil
|
||||
}
|
||||
if fr.BitSize == 64 {
|
||||
payloadFloat := rand.Float64()*(fr.Max-fr.Min) + fr.Min
|
||||
wrappedPayload.Payload = payloadFloat
|
||||
return wrappedPayload, nil
|
||||
}
|
||||
wrappedPayload.End = true
|
||||
return wrappedPayload, errors.New("float.random bitSize error: must be 32 or 64")
|
||||
}
|
||||
|
||||
func (fr *FloatRandom) Type() string {
|
||||
return fr.config.Type
|
||||
}
|
||||
|
||||
+184
-175
@@ -13,6 +13,190 @@ import (
|
||||
"github.com/jwetzell/showbridge-go/internal/config"
|
||||
)
|
||||
|
||||
func init() {
|
||||
RegisterProcessor(ProcessorRegistration{
|
||||
Type: "freed.create",
|
||||
Title: "Create FreeD",
|
||||
ParamsSchema: &jsonschema.Schema{
|
||||
Type: "object",
|
||||
Properties: map[string]*jsonschema.Schema{
|
||||
"id": {
|
||||
Title: "Camera ID",
|
||||
Description: "ID of the camera this FreeD position corresponds to",
|
||||
Type: "string",
|
||||
},
|
||||
"pan": {
|
||||
Title: "Pan",
|
||||
Description: "pan angle of the camera",
|
||||
Type: "string",
|
||||
},
|
||||
"tilt": {
|
||||
Title: "Tilt",
|
||||
Description: "tilt angle of the camera",
|
||||
Type: "string",
|
||||
},
|
||||
"roll": {
|
||||
Title: "Roll",
|
||||
Description: "roll angle of the camera",
|
||||
Type: "string",
|
||||
},
|
||||
"posX": {
|
||||
Title: "Position X",
|
||||
Description: "X coordinate of the camera's position",
|
||||
Type: "string",
|
||||
},
|
||||
"posY": {
|
||||
Title: "Position Y",
|
||||
Description: "Y coordinate of the camera's position",
|
||||
Type: "string",
|
||||
},
|
||||
"posZ": {
|
||||
Title: "Position Z",
|
||||
Description: "Z coordinate of the camera's position",
|
||||
Type: "string",
|
||||
},
|
||||
"zoom": {
|
||||
Title: "Zoom",
|
||||
Description: "zoom level of the camera",
|
||||
Type: "string",
|
||||
},
|
||||
"focus": {
|
||||
Title: "Focus",
|
||||
Description: "focus level of the camera",
|
||||
Type: "string",
|
||||
},
|
||||
},
|
||||
Required: []string{
|
||||
"id",
|
||||
"pan",
|
||||
"tilt",
|
||||
"roll",
|
||||
"posX",
|
||||
"posY",
|
||||
"posZ",
|
||||
"zoom",
|
||||
"focus",
|
||||
},
|
||||
AdditionalProperties: &jsonschema.Schema{Not: &jsonschema.Schema{}},
|
||||
},
|
||||
New: func(config config.ProcessorConfig) (Processor, error) {
|
||||
|
||||
// TODO(jwetzell): make some params optional
|
||||
params := config.Params
|
||||
idString, err := params.GetString("id")
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("freed.create id error: %w", err)
|
||||
}
|
||||
|
||||
idTemplate, err := template.New("id").Parse(idString)
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
panString, err := params.GetString("pan")
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("freed.create pan error: %w", err)
|
||||
}
|
||||
|
||||
panTemplate, err := template.New("pan").Parse(panString)
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
tiltString, err := params.GetString("tilt")
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("freed.create tilt error: %w", err)
|
||||
}
|
||||
|
||||
tiltTemplate, err := template.New("tilt").Parse(tiltString)
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
rollString, err := params.GetString("roll")
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("freed.create roll error: %w", err)
|
||||
}
|
||||
|
||||
rollTemplate, err := template.New("roll").Parse(rollString)
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
posXString, err := params.GetString("posX")
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("freed.create posX error: %w", err)
|
||||
}
|
||||
|
||||
posXTemplate, err := template.New("posX").Parse(posXString)
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
posYString, err := params.GetString("posY")
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("freed.create posY error: %w", err)
|
||||
}
|
||||
|
||||
posYTemplate, err := template.New("posY").Parse(posYString)
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
posZString, err := params.GetString("posZ")
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("freed.create posZ error: %w", err)
|
||||
}
|
||||
|
||||
posZTemplate, err := template.New("posZ").Parse(posZString)
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
zoomString, err := params.GetString("zoom")
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("freed.create zoom error: %w", err)
|
||||
}
|
||||
|
||||
zoomTemplate, err := template.New("zoom").Parse(zoomString)
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
focusString, err := params.GetString("focus")
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("freed.create focus error: %w", err)
|
||||
}
|
||||
|
||||
focusTemplate, err := template.New("focus").Parse(focusString)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &FreeDCreate{
|
||||
config: config,
|
||||
Id: idTemplate,
|
||||
Pan: panTemplate,
|
||||
Tilt: tiltTemplate,
|
||||
Roll: rollTemplate,
|
||||
PosX: posXTemplate,
|
||||
PosY: posYTemplate,
|
||||
PosZ: posZTemplate,
|
||||
Zoom: zoomTemplate,
|
||||
Focus: focusTemplate,
|
||||
}, nil
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
type FreeDCreate struct {
|
||||
config config.ProcessorConfig
|
||||
Id *template.Template
|
||||
@@ -203,178 +387,3 @@ func (fc *FreeDCreate) Process(ctx context.Context, wrappedPayload common.Wrappe
|
||||
func (fc *FreeDCreate) Type() string {
|
||||
return fc.config.Type
|
||||
}
|
||||
|
||||
func init() {
|
||||
RegisterProcessor(ProcessorRegistration{
|
||||
Type: "freed.create",
|
||||
Title: "Create FreeD",
|
||||
ParamsSchema: &jsonschema.Schema{
|
||||
Type: "object",
|
||||
Properties: map[string]*jsonschema.Schema{
|
||||
"id": {
|
||||
Title: "Camera ID",
|
||||
Type: "string",
|
||||
},
|
||||
"pan": {
|
||||
Title: "Pan",
|
||||
Type: "string",
|
||||
},
|
||||
"tilt": {
|
||||
Title: "Tilt",
|
||||
Type: "string",
|
||||
},
|
||||
"roll": {
|
||||
Title: "Roll",
|
||||
Type: "string",
|
||||
},
|
||||
"posX": {
|
||||
Title: "Position X",
|
||||
Type: "string",
|
||||
},
|
||||
"posY": {
|
||||
Title: "Position Y",
|
||||
Type: "string",
|
||||
},
|
||||
"posZ": {
|
||||
Title: "Position Z",
|
||||
Type: "string",
|
||||
},
|
||||
"zoom": {
|
||||
Title: "Zoom",
|
||||
Type: "string",
|
||||
},
|
||||
"focus": {
|
||||
Title: "Focus",
|
||||
Type: "string",
|
||||
},
|
||||
},
|
||||
Required: []string{
|
||||
"id",
|
||||
"pan",
|
||||
"tilt",
|
||||
"roll",
|
||||
"posX",
|
||||
"posY",
|
||||
"posZ",
|
||||
"zoom",
|
||||
"focus",
|
||||
},
|
||||
AdditionalProperties: &jsonschema.Schema{Not: &jsonschema.Schema{}},
|
||||
},
|
||||
New: func(config config.ProcessorConfig) (Processor, error) {
|
||||
|
||||
// TODO(jwetzell): make some params optional
|
||||
params := config.Params
|
||||
idString, err := params.GetString("id")
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("freed.create id error: %w", err)
|
||||
}
|
||||
|
||||
idTemplate, err := template.New("id").Parse(idString)
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
panString, err := params.GetString("pan")
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("freed.create pan error: %w", err)
|
||||
}
|
||||
|
||||
panTemplate, err := template.New("pan").Parse(panString)
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
tiltString, err := params.GetString("tilt")
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("freed.create tilt error: %w", err)
|
||||
}
|
||||
|
||||
tiltTemplate, err := template.New("tilt").Parse(tiltString)
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
rollString, err := params.GetString("roll")
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("freed.create roll error: %w", err)
|
||||
}
|
||||
|
||||
rollTemplate, err := template.New("roll").Parse(rollString)
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
posXString, err := params.GetString("posX")
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("freed.create posX error: %w", err)
|
||||
}
|
||||
|
||||
posXTemplate, err := template.New("posX").Parse(posXString)
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
posYString, err := params.GetString("posY")
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("freed.create posY error: %w", err)
|
||||
}
|
||||
|
||||
posYTemplate, err := template.New("posY").Parse(posYString)
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
posZString, err := params.GetString("posZ")
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("freed.create posZ error: %w", err)
|
||||
}
|
||||
|
||||
posZTemplate, err := template.New("posZ").Parse(posZString)
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
zoomString, err := params.GetString("zoom")
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("freed.create zoom error: %w", err)
|
||||
}
|
||||
|
||||
zoomTemplate, err := template.New("zoom").Parse(zoomString)
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
focusString, err := params.GetString("focus")
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("freed.create focus error: %w", err)
|
||||
}
|
||||
|
||||
focusTemplate, err := template.New("focus").Parse(focusString)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &FreeDCreate{
|
||||
config: config,
|
||||
Id: idTemplate,
|
||||
Pan: panTemplate,
|
||||
Tilt: tiltTemplate,
|
||||
Roll: rollTemplate,
|
||||
PosX: posXTemplate,
|
||||
PosY: posYTemplate,
|
||||
PosZ: posZTemplate,
|
||||
Zoom: zoomTemplate,
|
||||
Focus: focusTemplate,
|
||||
}, nil
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
@@ -9,10 +9,21 @@ import (
|
||||
"github.com/jwetzell/showbridge-go/internal/config"
|
||||
)
|
||||
|
||||
type FreeDDecode struct {
|
||||
config config.ProcessorConfig
|
||||
func init() {
|
||||
RegisterProcessor(ProcessorRegistration{
|
||||
Type: "freed.decode",
|
||||
Title: "Decode FreeD",
|
||||
New: func(config config.ProcessorConfig) (Processor, error) {
|
||||
return &FreeDDecode{config: config}, nil
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
type FreeDDecode struct {
|
||||
config config.ProcessorConfig
|
||||
buf [29]byte
|
||||
}
|
||||
|
||||
func (fd *FreeDDecode) Process(ctx context.Context, wrappedPayload common.WrappedPayload) (common.WrappedPayload, error) {
|
||||
payload := wrappedPayload.Payload
|
||||
payloadBytes, ok := common.GetAnyAsByteSlice(payload)
|
||||
@@ -22,7 +33,14 @@ func (fd *FreeDDecode) Process(ctx context.Context, wrappedPayload common.Wrappe
|
||||
return wrappedPayload, errors.New("freed.decode processor only accepts a []byte")
|
||||
}
|
||||
|
||||
payloadMessage, err := freeD.Decode(payloadBytes)
|
||||
if len(payloadBytes) != 29 {
|
||||
wrappedPayload.End = true
|
||||
return wrappedPayload, errors.New("freeD packet must be exactly 29 bytes")
|
||||
}
|
||||
|
||||
copy(fd.buf[:], payloadBytes)
|
||||
|
||||
payloadMessage, err := freeD.Decode(fd.buf)
|
||||
if err != nil {
|
||||
wrappedPayload.End = true
|
||||
return wrappedPayload, err
|
||||
@@ -34,13 +52,3 @@ func (fd *FreeDDecode) Process(ctx context.Context, wrappedPayload common.Wrappe
|
||||
func (fd *FreeDDecode) Type() string {
|
||||
return fd.config.Type
|
||||
}
|
||||
|
||||
func init() {
|
||||
RegisterProcessor(ProcessorRegistration{
|
||||
Type: "freed.decode",
|
||||
Title: "Decode FreeD",
|
||||
New: func(config config.ProcessorConfig) (Processor, error) {
|
||||
return &FreeDDecode{config: config}, nil
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
@@ -9,6 +9,16 @@ import (
|
||||
"github.com/jwetzell/showbridge-go/internal/config"
|
||||
)
|
||||
|
||||
func init() {
|
||||
RegisterProcessor(ProcessorRegistration{
|
||||
Type: "freed.encode",
|
||||
Title: "Encode FreeD",
|
||||
New: func(config config.ProcessorConfig) (Processor, error) {
|
||||
return &FreeDEncode{config: config}, nil
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
type FreeDEncode struct {
|
||||
config config.ProcessorConfig
|
||||
}
|
||||
@@ -31,13 +41,3 @@ func (fe *FreeDEncode) Process(ctx context.Context, wrappedPayload common.Wrappe
|
||||
func (fe *FreeDEncode) Type() string {
|
||||
return fe.config.Type
|
||||
}
|
||||
|
||||
func init() {
|
||||
RegisterProcessor(ProcessorRegistration{
|
||||
Type: "freed.encode",
|
||||
Title: "Encode FreeD",
|
||||
New: func(config config.ProcessorConfig) (Processor, error) {
|
||||
return &FreeDEncode{config: config}, nil
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
@@ -14,6 +14,55 @@ import (
|
||||
"github.com/jwetzell/showbridge-go/internal/config"
|
||||
)
|
||||
|
||||
func init() {
|
||||
RegisterProcessor(ProcessorRegistration{
|
||||
Type: "http.request.do",
|
||||
Title: "Do HTTP Request",
|
||||
ParamsSchema: &jsonschema.Schema{
|
||||
Type: "object",
|
||||
Properties: map[string]*jsonschema.Schema{
|
||||
"method": {
|
||||
Title: "HTTP Method",
|
||||
Description: "method to use for the HTTP request",
|
||||
Type: "string",
|
||||
Enum: []any{"GET", "POST", "PUT", "PATCH", "DELETE"},
|
||||
},
|
||||
"url": {
|
||||
Title: "URL",
|
||||
Description: "URL to send the HTTP request to",
|
||||
Type: "string",
|
||||
},
|
||||
},
|
||||
Required: []string{"method", "url"},
|
||||
AdditionalProperties: &jsonschema.Schema{Not: &jsonschema.Schema{}},
|
||||
},
|
||||
New: func(config config.ProcessorConfig) (Processor, error) {
|
||||
params := config.Params
|
||||
|
||||
methodString, err := params.GetString("method")
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("http.request.do method error: %w", err)
|
||||
}
|
||||
|
||||
urlString, err := params.GetString("url")
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("http.request.do url error: %w", err)
|
||||
}
|
||||
|
||||
urlTemplate, err := template.New("url").Parse(urlString)
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
client := &http.Client{
|
||||
Timeout: 10 * time.Second,
|
||||
}
|
||||
|
||||
return &HTTPRequestDo{config: config, URL: urlTemplate, Method: methodString, client: client}, nil
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
type HTTPRequestDo struct {
|
||||
config config.ProcessorConfig
|
||||
client *http.Client
|
||||
@@ -68,50 +117,3 @@ func (hrd *HTTPRequestDo) Process(ctx context.Context, wrappedPayload common.Wra
|
||||
func (hrd *HTTPRequestDo) Type() string {
|
||||
return hrd.config.Type
|
||||
}
|
||||
|
||||
func init() {
|
||||
RegisterProcessor(ProcessorRegistration{
|
||||
Type: "http.request.do",
|
||||
Title: "Do HTTP Request",
|
||||
ParamsSchema: &jsonschema.Schema{
|
||||
Type: "object",
|
||||
Properties: map[string]*jsonschema.Schema{
|
||||
"method": {
|
||||
Title: "HTTP Method",
|
||||
Type: "string",
|
||||
Enum: []any{"GET", "POST", "PUT", "PATCH", "DELETE"},
|
||||
},
|
||||
"url": {
|
||||
Title: "URL",
|
||||
Type: "string",
|
||||
},
|
||||
},
|
||||
Required: []string{"method", "url"},
|
||||
AdditionalProperties: &jsonschema.Schema{Not: &jsonschema.Schema{}},
|
||||
},
|
||||
New: func(config config.ProcessorConfig) (Processor, error) {
|
||||
params := config.Params
|
||||
|
||||
methodString, err := params.GetString("method")
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("http.request.do method error: %w", err)
|
||||
}
|
||||
|
||||
urlString, err := params.GetString("url")
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("http.request.do url error: %w", err)
|
||||
}
|
||||
|
||||
urlTemplate, err := template.New("url").Parse(urlString)
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
client := &http.Client{
|
||||
Timeout: 10 * time.Second,
|
||||
}
|
||||
|
||||
return &HTTPRequestDo{config: config, URL: urlTemplate, Method: methodString, client: client}, nil
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
@@ -11,6 +11,52 @@ import (
|
||||
"github.com/jwetzell/showbridge-go/internal/config"
|
||||
)
|
||||
|
||||
func init() {
|
||||
RegisterProcessor(ProcessorRegistration{
|
||||
Type: "http.response.create",
|
||||
Title: "Create HTTP Response",
|
||||
ParamsSchema: &jsonschema.Schema{
|
||||
Type: "object",
|
||||
Properties: map[string]*jsonschema.Schema{
|
||||
"status": {
|
||||
Title: "Status Code",
|
||||
Description: "status code to set on the response",
|
||||
Type: "integer",
|
||||
},
|
||||
"bodyTemplate": {
|
||||
Title: "Body Template",
|
||||
Description: "template for the response body",
|
||||
Type: "string",
|
||||
},
|
||||
},
|
||||
Required: []string{"status", "bodyTemplate"},
|
||||
AdditionalProperties: &jsonschema.Schema{Not: &jsonschema.Schema{}},
|
||||
},
|
||||
New: func(config config.ProcessorConfig) (Processor, error) {
|
||||
params := config.Params
|
||||
|
||||
statusNum, err := params.GetInt("status")
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("http.response.create status error: %w", err)
|
||||
}
|
||||
|
||||
bodyTemplateString, err := params.GetString("bodyTemplate")
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("http.response.create bodyTemplate error: %w", err)
|
||||
}
|
||||
|
||||
bodyTemplate, err := template.New("body").Parse(bodyTemplateString)
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// TODO(jwetzell): support other body kind (direct bytes from input, from file?)
|
||||
return &HTTPResponseCreate{config: config, Status: int(statusNum), BodyTmpl: bodyTemplate}, nil
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
type HTTPResponseCreate struct {
|
||||
Status int
|
||||
BodyTmpl *template.Template
|
||||
@@ -42,47 +88,3 @@ func (hrc *HTTPResponseCreate) Process(ctx context.Context, wrappedPayload commo
|
||||
func (hrc *HTTPResponseCreate) Type() string {
|
||||
return hrc.config.Type
|
||||
}
|
||||
|
||||
func init() {
|
||||
RegisterProcessor(ProcessorRegistration{
|
||||
Type: "http.response.create",
|
||||
Title: "Create HTTP Response",
|
||||
ParamsSchema: &jsonschema.Schema{
|
||||
Type: "object",
|
||||
Properties: map[string]*jsonschema.Schema{
|
||||
"status": {
|
||||
Title: "Status Code",
|
||||
Type: "integer",
|
||||
},
|
||||
"body": {
|
||||
Title: "Body",
|
||||
Type: "string",
|
||||
},
|
||||
},
|
||||
Required: []string{"status", "body"},
|
||||
AdditionalProperties: &jsonschema.Schema{Not: &jsonschema.Schema{}},
|
||||
},
|
||||
New: func(config config.ProcessorConfig) (Processor, error) {
|
||||
params := config.Params
|
||||
|
||||
statusNum, err := params.GetInt("status")
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("http.response.create status error: %w", err)
|
||||
}
|
||||
|
||||
bodyTemplateString, err := params.GetString("bodyTemplate")
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("http.response.create bodyTemplate error: %w", err)
|
||||
}
|
||||
|
||||
bodyTemplate, err := template.New("body").Parse(bodyTemplateString)
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// TODO(jwetzell): support other body kind (direct bytes from input, from file?)
|
||||
return &HTTPResponseCreate{config: config, Status: int(statusNum), BodyTmpl: bodyTemplate}, nil
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
@@ -12,34 +12,6 @@ import (
|
||||
"github.com/jwetzell/showbridge-go/internal/config"
|
||||
)
|
||||
|
||||
type IntParse struct {
|
||||
Base int
|
||||
BitSize int
|
||||
config config.ProcessorConfig
|
||||
}
|
||||
|
||||
func (ip *IntParse) Process(ctx context.Context, wrappedPayload common.WrappedPayload) (common.WrappedPayload, error) {
|
||||
payload := wrappedPayload.Payload
|
||||
payloadString, ok := common.GetAnyAs[string](payload)
|
||||
|
||||
if !ok {
|
||||
wrappedPayload.End = true
|
||||
return wrappedPayload, errors.New("int.parse processor only accepts a string")
|
||||
}
|
||||
|
||||
payloadInt, err := strconv.ParseInt(payloadString, ip.Base, ip.BitSize)
|
||||
if err != nil {
|
||||
wrappedPayload.End = true
|
||||
return wrappedPayload, err
|
||||
}
|
||||
wrappedPayload.Payload = payloadInt
|
||||
return wrappedPayload, nil
|
||||
}
|
||||
|
||||
func (ip *IntParse) Type() string {
|
||||
return ip.config.Type
|
||||
}
|
||||
|
||||
func init() {
|
||||
RegisterProcessor(ProcessorRegistration{
|
||||
Type: "int.parse",
|
||||
@@ -48,16 +20,18 @@ func init() {
|
||||
Type: "object",
|
||||
Properties: map[string]*jsonschema.Schema{
|
||||
"base": {
|
||||
Title: "Base",
|
||||
Type: "integer",
|
||||
Enum: []any{0, 2, 8, 10, 16},
|
||||
Default: json.RawMessage("10"),
|
||||
Title: "Base",
|
||||
Description: "numerical base to use for parsing the integer",
|
||||
Type: "integer",
|
||||
Enum: []any{0, 2, 8, 10, 16},
|
||||
Default: json.RawMessage("10"),
|
||||
},
|
||||
"bitSize": {
|
||||
Title: "Bit Size",
|
||||
Type: "integer",
|
||||
Enum: []any{0, 8, 16, 32, 64},
|
||||
Default: json.RawMessage("64"),
|
||||
Title: "Bit Size",
|
||||
Description: "bit size of the resulting integer",
|
||||
Type: "integer",
|
||||
Enum: []any{0, 8, 16, 32, 64},
|
||||
Default: json.RawMessage("64"),
|
||||
},
|
||||
},
|
||||
AdditionalProperties: &jsonschema.Schema{Not: &jsonschema.Schema{}},
|
||||
@@ -86,3 +60,31 @@ func init() {
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
type IntParse struct {
|
||||
Base int
|
||||
BitSize int
|
||||
config config.ProcessorConfig
|
||||
}
|
||||
|
||||
func (ip *IntParse) Process(ctx context.Context, wrappedPayload common.WrappedPayload) (common.WrappedPayload, error) {
|
||||
payload := wrappedPayload.Payload
|
||||
payloadString, ok := common.GetAnyAs[string](payload)
|
||||
|
||||
if !ok {
|
||||
wrappedPayload.End = true
|
||||
return wrappedPayload, errors.New("int.parse processor only accepts a string")
|
||||
}
|
||||
|
||||
payloadInt, err := strconv.ParseInt(payloadString, ip.Base, ip.BitSize)
|
||||
if err != nil {
|
||||
wrappedPayload.End = true
|
||||
return wrappedPayload, err
|
||||
}
|
||||
wrappedPayload.Payload = payloadInt
|
||||
return wrappedPayload, nil
|
||||
}
|
||||
|
||||
func (ip *IntParse) Type() string {
|
||||
return ip.config.Type
|
||||
}
|
||||
|
||||
@@ -11,22 +11,6 @@ import (
|
||||
"github.com/jwetzell/showbridge-go/internal/config"
|
||||
)
|
||||
|
||||
type IntRandom struct {
|
||||
Min int
|
||||
Max int
|
||||
config config.ProcessorConfig
|
||||
}
|
||||
|
||||
func (ir *IntRandom) Process(ctx context.Context, wrappedPayload common.WrappedPayload) (common.WrappedPayload, error) {
|
||||
payloadInt := rand.IntN(ir.Max-ir.Min+1) + ir.Min
|
||||
wrappedPayload.Payload = payloadInt
|
||||
return wrappedPayload, nil
|
||||
}
|
||||
|
||||
func (ir *IntRandom) Type() string {
|
||||
return ir.config.Type
|
||||
}
|
||||
|
||||
func init() {
|
||||
RegisterProcessor(ProcessorRegistration{
|
||||
Type: "int.random",
|
||||
@@ -35,12 +19,14 @@ func init() {
|
||||
Type: "object",
|
||||
Properties: map[string]*jsonschema.Schema{
|
||||
"min": {
|
||||
Title: "Minimum",
|
||||
Type: "integer",
|
||||
Title: "Minimum",
|
||||
Description: "inclusive minimum value of the random integer",
|
||||
Type: "integer",
|
||||
},
|
||||
"max": {
|
||||
Title: "Maximum",
|
||||
Type: "integer",
|
||||
Title: "Maximum",
|
||||
Description: "inclusive maximum value of the random integer",
|
||||
Type: "integer",
|
||||
},
|
||||
},
|
||||
Required: []string{"min", "max"},
|
||||
@@ -67,3 +53,19 @@ func init() {
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
type IntRandom struct {
|
||||
Min int
|
||||
Max int
|
||||
config config.ProcessorConfig
|
||||
}
|
||||
|
||||
func (ir *IntRandom) Process(ctx context.Context, wrappedPayload common.WrappedPayload) (common.WrappedPayload, error) {
|
||||
payloadInt := rand.IntN(ir.Max-ir.Min+1) + ir.Min
|
||||
wrappedPayload.Payload = payloadInt
|
||||
return wrappedPayload, nil
|
||||
}
|
||||
|
||||
func (ir *IntRandom) Type() string {
|
||||
return ir.config.Type
|
||||
}
|
||||
|
||||
@@ -10,31 +10,6 @@ import (
|
||||
"github.com/jwetzell/showbridge-go/internal/config"
|
||||
)
|
||||
|
||||
type IntScale struct {
|
||||
OutMin int
|
||||
OutMax int
|
||||
InMin int
|
||||
InMax int
|
||||
config config.ProcessorConfig
|
||||
}
|
||||
|
||||
func (ir *IntScale) Process(ctx context.Context, wrappedPayload common.WrappedPayload) (common.WrappedPayload, error) {
|
||||
payload := wrappedPayload.Payload
|
||||
payloadInt, ok := common.GetAnyAs[int](payload)
|
||||
if !ok {
|
||||
wrappedPayload.End = true
|
||||
return wrappedPayload, errors.New("int.scale can only process an int")
|
||||
}
|
||||
|
||||
payloadInt = (payloadInt-ir.InMin)*(ir.OutMax-ir.OutMin)/(ir.InMax-ir.InMin) + ir.OutMin
|
||||
wrappedPayload.Payload = payloadInt
|
||||
return wrappedPayload, nil
|
||||
}
|
||||
|
||||
func (ir *IntScale) Type() string {
|
||||
return ir.config.Type
|
||||
}
|
||||
|
||||
func init() {
|
||||
RegisterProcessor(ProcessorRegistration{
|
||||
Type: "int.scale",
|
||||
@@ -43,20 +18,24 @@ func init() {
|
||||
Type: "object",
|
||||
Properties: map[string]*jsonschema.Schema{
|
||||
"inMin": {
|
||||
Title: "Input Minimum",
|
||||
Type: "integer",
|
||||
Title: "Input Minimum",
|
||||
Description: "minimum value of the input integer",
|
||||
Type: "integer",
|
||||
},
|
||||
"inMax": {
|
||||
Title: "Input Maximum",
|
||||
Type: "integer",
|
||||
Title: "Input Maximum",
|
||||
Description: "maximum value of the input integer",
|
||||
Type: "integer",
|
||||
},
|
||||
"outMin": {
|
||||
Title: "Output Minimum",
|
||||
Type: "integer",
|
||||
Title: "Output Minimum",
|
||||
Description: "minimum value of the output integer",
|
||||
Type: "integer",
|
||||
},
|
||||
"outMax": {
|
||||
Title: "Output Maximum",
|
||||
Type: "integer",
|
||||
Title: "Output Maximum",
|
||||
Description: "maximum value of the output integer",
|
||||
Type: "integer",
|
||||
},
|
||||
},
|
||||
Required: []string{"inMin", "inMax", "outMin", "outMax"},
|
||||
@@ -97,3 +76,28 @@ func init() {
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
type IntScale struct {
|
||||
OutMin int
|
||||
OutMax int
|
||||
InMin int
|
||||
InMax int
|
||||
config config.ProcessorConfig
|
||||
}
|
||||
|
||||
func (is *IntScale) Process(ctx context.Context, wrappedPayload common.WrappedPayload) (common.WrappedPayload, error) {
|
||||
payload := wrappedPayload.Payload
|
||||
payloadInt, ok := common.GetAnyAs[int](payload)
|
||||
if !ok {
|
||||
wrappedPayload.End = true
|
||||
return wrappedPayload, errors.New("int.scale can only process an int")
|
||||
}
|
||||
|
||||
payloadInt = (payloadInt-is.InMin)*(is.OutMax-is.OutMin)/(is.InMax-is.InMin) + is.OutMin
|
||||
wrappedPayload.Payload = payloadInt
|
||||
return wrappedPayload, nil
|
||||
}
|
||||
|
||||
func (is *IntScale) Type() string {
|
||||
return is.config.Type
|
||||
}
|
||||
|
||||
@@ -9,6 +9,16 @@ import (
|
||||
"github.com/jwetzell/showbridge-go/internal/config"
|
||||
)
|
||||
|
||||
func init() {
|
||||
RegisterProcessor(ProcessorRegistration{
|
||||
Type: "json.decode",
|
||||
Title: "Decode JSON",
|
||||
New: func(config config.ProcessorConfig) (Processor, error) {
|
||||
return &JsonDecode{config: config}, nil
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
type JsonDecode struct {
|
||||
config config.ProcessorConfig
|
||||
}
|
||||
@@ -43,13 +53,3 @@ func (jd *JsonDecode) Process(ctx context.Context, wrappedPayload common.Wrapped
|
||||
func (jd *JsonDecode) Type() string {
|
||||
return jd.config.Type
|
||||
}
|
||||
|
||||
func init() {
|
||||
RegisterProcessor(ProcessorRegistration{
|
||||
Type: "json.decode",
|
||||
Title: "Decode JSON",
|
||||
New: func(config config.ProcessorConfig) (Processor, error) {
|
||||
return &JsonDecode{config: config}, nil
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
@@ -9,6 +9,16 @@ import (
|
||||
"github.com/jwetzell/showbridge-go/internal/config"
|
||||
)
|
||||
|
||||
func init() {
|
||||
RegisterProcessor(ProcessorRegistration{
|
||||
Type: "json.encode",
|
||||
Title: "Encode JSON",
|
||||
New: func(config config.ProcessorConfig) (Processor, error) {
|
||||
return &JsonEncode{config: config}, nil
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
type JsonEncode struct {
|
||||
config config.ProcessorConfig
|
||||
}
|
||||
@@ -35,13 +45,3 @@ func (je *JsonEncode) Process(ctx context.Context, wrappedPayload common.Wrapped
|
||||
func (je *JsonEncode) Type() string {
|
||||
return je.config.Type
|
||||
}
|
||||
|
||||
func init() {
|
||||
RegisterProcessor(ProcessorRegistration{
|
||||
Type: "json.encode",
|
||||
Title: "Encode JSON",
|
||||
New: func(config config.ProcessorConfig) (Processor, error) {
|
||||
return &JsonEncode{config: config}, nil
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
@@ -11,6 +11,45 @@ import (
|
||||
"github.com/jwetzell/showbridge-go/internal/config"
|
||||
)
|
||||
|
||||
func init() {
|
||||
RegisterProcessor(ProcessorRegistration{
|
||||
Type: "kv.get",
|
||||
Title: "Get Key",
|
||||
ParamsSchema: &jsonschema.Schema{
|
||||
Type: "object",
|
||||
Properties: map[string]*jsonschema.Schema{
|
||||
"module": {
|
||||
Title: "Module ID",
|
||||
Description: "ID of the key-value module to get the value from",
|
||||
Type: "string",
|
||||
},
|
||||
"key": {
|
||||
Title: "Key",
|
||||
Description: "key to retrieve from the key-value module",
|
||||
Type: "string",
|
||||
},
|
||||
},
|
||||
Required: []string{"module", "key"},
|
||||
AdditionalProperties: &jsonschema.Schema{Not: &jsonschema.Schema{}},
|
||||
},
|
||||
New: func(config config.ProcessorConfig) (Processor, error) {
|
||||
|
||||
params := config.Params
|
||||
|
||||
moduleIdString, err := params.GetString("module")
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("kv.get module error: %w", err)
|
||||
}
|
||||
|
||||
keyString, err := params.GetString("key")
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("kv.get key error: %w", err)
|
||||
}
|
||||
return &KVGet{config: config, ModuleId: moduleIdString, Key: keyString, logger: slog.Default().With("component", "processor", "type", config.Type)}, nil
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
type KVGet struct {
|
||||
config config.ProcessorConfig
|
||||
ModuleId string
|
||||
@@ -40,7 +79,7 @@ func (kvg *KVGet) Process(ctx context.Context, wrappedPayload common.WrappedPayl
|
||||
kvg.module = kvModule
|
||||
}
|
||||
|
||||
value, err := kvg.module.Get(kvg.Key)
|
||||
value, err := kvg.module.Get(ctx, kvg.Key)
|
||||
if err != nil {
|
||||
wrappedPayload.End = true
|
||||
return wrappedPayload, fmt.Errorf("kv.get error getting key: %w", err)
|
||||
@@ -53,40 +92,3 @@ func (kvg *KVGet) Process(ctx context.Context, wrappedPayload common.WrappedPayl
|
||||
func (kvg *KVGet) Type() string {
|
||||
return kvg.config.Type
|
||||
}
|
||||
|
||||
func init() {
|
||||
RegisterProcessor(ProcessorRegistration{
|
||||
Type: "kv.get",
|
||||
Title: "Get Key",
|
||||
ParamsSchema: &jsonschema.Schema{
|
||||
Type: "object",
|
||||
Properties: map[string]*jsonschema.Schema{
|
||||
"module": {
|
||||
Title: "Module ID",
|
||||
Type: "string",
|
||||
},
|
||||
"key": {
|
||||
Title: "Key",
|
||||
Type: "string",
|
||||
},
|
||||
},
|
||||
Required: []string{"module", "key"},
|
||||
AdditionalProperties: &jsonschema.Schema{Not: &jsonschema.Schema{}},
|
||||
},
|
||||
New: func(config config.ProcessorConfig) (Processor, error) {
|
||||
|
||||
params := config.Params
|
||||
|
||||
moduleIdString, err := params.GetString("module")
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("kv.get module error: %w", err)
|
||||
}
|
||||
|
||||
keyString, err := params.GetString("key")
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("kv.get key error: %w", err)
|
||||
}
|
||||
return &KVGet{config: config, ModuleId: moduleIdString, Key: keyString, logger: slog.Default().With("component", "processor", "type", config.Type)}, nil
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
@@ -1,11 +1,9 @@
|
||||
package processor
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"html/template"
|
||||
"log/slog"
|
||||
|
||||
"github.com/google/jsonschema-go/jsonschema"
|
||||
@@ -13,11 +11,50 @@ import (
|
||||
"github.com/jwetzell/showbridge-go/internal/config"
|
||||
)
|
||||
|
||||
func init() {
|
||||
RegisterProcessor(ProcessorRegistration{
|
||||
Type: "kv.set",
|
||||
Title: "Set Key",
|
||||
ParamsSchema: &jsonschema.Schema{
|
||||
Type: "object",
|
||||
Properties: map[string]*jsonschema.Schema{
|
||||
"module": {
|
||||
Title: "Module ID",
|
||||
Description: "ID of the key-value module to set the value in",
|
||||
Type: "string",
|
||||
},
|
||||
"key": {
|
||||
Title: "Key",
|
||||
Description: "key to set in the key-value module",
|
||||
Type: "string",
|
||||
},
|
||||
},
|
||||
Required: []string{"module", "key"},
|
||||
AdditionalProperties: &jsonschema.Schema{Not: &jsonschema.Schema{}},
|
||||
},
|
||||
New: func(config config.ProcessorConfig) (Processor, error) {
|
||||
|
||||
params := config.Params
|
||||
|
||||
moduleIdString, err := params.GetString("module")
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("kv.set module error: %w", err)
|
||||
}
|
||||
|
||||
keyString, err := params.GetString("key")
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("kv.set key error: %w", err)
|
||||
}
|
||||
|
||||
return &KVSet{config: config, ModuleId: moduleIdString, Key: keyString, logger: slog.Default().With("component", "processor", "type", config.Type)}, nil
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
type KVSet struct {
|
||||
config config.ProcessorConfig
|
||||
ModuleId string
|
||||
Key string
|
||||
Value *template.Template
|
||||
logger *slog.Logger
|
||||
module common.KeyValueModule
|
||||
}
|
||||
@@ -43,15 +80,7 @@ func (kvs *KVSet) Process(ctx context.Context, wrappedPayload common.WrappedPayl
|
||||
kvs.module = kvModule
|
||||
}
|
||||
|
||||
var valueBuffer bytes.Buffer
|
||||
err := kvs.Value.Execute(&valueBuffer, wrappedPayload)
|
||||
|
||||
if err != nil {
|
||||
wrappedPayload.End = true
|
||||
return wrappedPayload, err
|
||||
}
|
||||
|
||||
err = kvs.module.Set(kvs.Key, valueBuffer.String())
|
||||
err := kvs.module.Set(ctx, kvs.Key, wrappedPayload.Payload)
|
||||
if err != nil {
|
||||
wrappedPayload.End = true
|
||||
return wrappedPayload, fmt.Errorf("kv.set error setting key: %w", err)
|
||||
@@ -63,55 +92,3 @@ func (kvs *KVSet) Process(ctx context.Context, wrappedPayload common.WrappedPayl
|
||||
func (kvs *KVSet) Type() string {
|
||||
return kvs.config.Type
|
||||
}
|
||||
|
||||
func init() {
|
||||
RegisterProcessor(ProcessorRegistration{
|
||||
Type: "kv.set",
|
||||
Title: "Set Key",
|
||||
ParamsSchema: &jsonschema.Schema{
|
||||
Type: "object",
|
||||
Properties: map[string]*jsonschema.Schema{
|
||||
"module": {
|
||||
Title: "Module ID",
|
||||
Type: "string",
|
||||
},
|
||||
"key": {
|
||||
Title: "Key",
|
||||
Type: "string",
|
||||
},
|
||||
"value": {
|
||||
Title: "Value",
|
||||
Type: "string",
|
||||
},
|
||||
},
|
||||
Required: []string{"module", "key", "value"},
|
||||
AdditionalProperties: &jsonschema.Schema{Not: &jsonschema.Schema{}},
|
||||
},
|
||||
New: func(config config.ProcessorConfig) (Processor, error) {
|
||||
|
||||
params := config.Params
|
||||
|
||||
moduleIdString, err := params.GetString("module")
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("kv.set module error: %w", err)
|
||||
}
|
||||
|
||||
keyString, err := params.GetString("key")
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("kv.set key error: %w", err)
|
||||
}
|
||||
|
||||
valueString, err := params.GetString("value")
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("kv.set value error: %w", err)
|
||||
}
|
||||
valueTemplate, err := template.New("template").Parse(valueString)
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &KVSet{config: config, ModuleId: moduleIdString, Key: keyString, Value: valueTemplate, logger: slog.Default().With("component", "processor", "type", config.Type)}, nil
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
@@ -15,6 +15,78 @@ import (
|
||||
"gitlab.com/gomidi/midi/v2"
|
||||
)
|
||||
|
||||
func init() {
|
||||
RegisterProcessor(ProcessorRegistration{
|
||||
Type: "midi.control_change.create",
|
||||
Title: "Create MIDI Control Change Message",
|
||||
ParamsSchema: &jsonschema.Schema{
|
||||
Type: "object",
|
||||
Properties: map[string]*jsonschema.Schema{
|
||||
"channel": {
|
||||
Title: "Channel",
|
||||
Description: "channel number for the control change message",
|
||||
Type: "string",
|
||||
},
|
||||
"control": {
|
||||
Title: "Control",
|
||||
Description: "control number for the control change message",
|
||||
Type: "string",
|
||||
},
|
||||
"value": {
|
||||
Title: "Value",
|
||||
Description: "value for the control change message",
|
||||
Type: "string",
|
||||
},
|
||||
},
|
||||
Required: []string{"channel", "control", "value"},
|
||||
AdditionalProperties: &jsonschema.Schema{Not: &jsonschema.Schema{}},
|
||||
},
|
||||
New: func(config config.ProcessorConfig) (Processor, error) {
|
||||
|
||||
params := config.Params
|
||||
|
||||
channelString, err := params.GetString("channel")
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("midi.control_change.create channel error: %w", err)
|
||||
}
|
||||
|
||||
channelTemplate, err := template.New("channel").Parse(channelString)
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
controlString, err := params.GetString("control")
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("midi.control_change.create control error: %w", err)
|
||||
}
|
||||
|
||||
controlTemplate, err := template.New("control").Parse(controlString)
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
valueString, err := params.GetString("value")
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("midi.control_change.create value error: %w", err)
|
||||
}
|
||||
|
||||
valueTemplate, err := template.New("value").Parse(valueString)
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &MIDIControlChangeCreate{
|
||||
config: config,
|
||||
Channel: channelTemplate,
|
||||
Control: controlTemplate,
|
||||
Value: valueTemplate,
|
||||
}, nil
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
type MIDIControlChangeCreate struct {
|
||||
config config.ProcessorConfig
|
||||
Channel *template.Template
|
||||
@@ -78,72 +150,3 @@ func (mccc *MIDIControlChangeCreate) Process(ctx context.Context, wrappedPayload
|
||||
func (mccc *MIDIControlChangeCreate) Type() string {
|
||||
return mccc.config.Type
|
||||
}
|
||||
|
||||
func init() {
|
||||
RegisterProcessor(ProcessorRegistration{
|
||||
Type: "midi.control_change.create",
|
||||
Title: "Create MIDI Control Change Message",
|
||||
ParamsSchema: &jsonschema.Schema{
|
||||
Type: "object",
|
||||
Properties: map[string]*jsonschema.Schema{
|
||||
"channel": {
|
||||
Title: "Channel",
|
||||
Type: "string",
|
||||
},
|
||||
"control": {
|
||||
Title: "Control",
|
||||
Type: "string",
|
||||
},
|
||||
"value": {
|
||||
Title: "Value",
|
||||
Type: "string",
|
||||
},
|
||||
},
|
||||
Required: []string{"channel", "control", "value"},
|
||||
AdditionalProperties: &jsonschema.Schema{Not: &jsonschema.Schema{}},
|
||||
},
|
||||
New: func(config config.ProcessorConfig) (Processor, error) {
|
||||
|
||||
params := config.Params
|
||||
|
||||
channelString, err := params.GetString("channel")
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("midi.control_change.create channel error: %w", err)
|
||||
}
|
||||
|
||||
channelTemplate, err := template.New("channel").Parse(channelString)
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
controlString, err := params.GetString("control")
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("midi.control_change.create control error: %w", err)
|
||||
}
|
||||
|
||||
controlTemplate, err := template.New("control").Parse(controlString)
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
valueString, err := params.GetString("value")
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("midi.control_change.create value error: %w", err)
|
||||
}
|
||||
|
||||
valueTemplate, err := template.New("value").Parse(valueString)
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &MIDIControlChangeCreate{
|
||||
config: config,
|
||||
Channel: channelTemplate,
|
||||
Control: controlTemplate,
|
||||
Value: valueTemplate,
|
||||
}, nil
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
@@ -11,6 +11,16 @@ import (
|
||||
"gitlab.com/gomidi/midi/v2"
|
||||
)
|
||||
|
||||
func init() {
|
||||
RegisterProcessor(ProcessorRegistration{
|
||||
Type: "midi.message.decode",
|
||||
Title: "Decode MIDI Message",
|
||||
New: func(config config.ProcessorConfig) (Processor, error) {
|
||||
return &MIDIMessageDecode{config: config}, nil
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
type MIDIMessageDecode struct {
|
||||
config config.ProcessorConfig
|
||||
}
|
||||
@@ -33,13 +43,3 @@ func (mmd *MIDIMessageDecode) Process(ctx context.Context, wrappedPayload common
|
||||
func (mmd *MIDIMessageDecode) Type() string {
|
||||
return mmd.config.Type
|
||||
}
|
||||
|
||||
func init() {
|
||||
RegisterProcessor(ProcessorRegistration{
|
||||
Type: "midi.message.decode",
|
||||
Title: "Decode MIDI Message",
|
||||
New: func(config config.ProcessorConfig) (Processor, error) {
|
||||
return &MIDIMessageDecode{config: config}, nil
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
@@ -11,6 +11,16 @@ import (
|
||||
"gitlab.com/gomidi/midi/v2"
|
||||
)
|
||||
|
||||
func init() {
|
||||
RegisterProcessor(ProcessorRegistration{
|
||||
Type: "midi.message.encode",
|
||||
Title: "Encode MIDI Message",
|
||||
New: func(config config.ProcessorConfig) (Processor, error) {
|
||||
return &MIDIMessageEncode{config: config}, nil
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
type MIDIMessageEncode struct {
|
||||
config config.ProcessorConfig
|
||||
}
|
||||
@@ -31,13 +41,3 @@ func (mme *MIDIMessageEncode) Process(ctx context.Context, wrappedPayload common
|
||||
func (mme *MIDIMessageEncode) Type() string {
|
||||
return mme.config.Type
|
||||
}
|
||||
|
||||
func init() {
|
||||
RegisterProcessor(ProcessorRegistration{
|
||||
Type: "midi.message.encode",
|
||||
Title: "Encode MIDI Message",
|
||||
New: func(config config.ProcessorConfig) (Processor, error) {
|
||||
return &MIDIMessageEncode{config: config}, nil
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
@@ -12,6 +12,16 @@ import (
|
||||
"gitlab.com/gomidi/midi/v2"
|
||||
)
|
||||
|
||||
func init() {
|
||||
RegisterProcessor(ProcessorRegistration{
|
||||
Type: "midi.message.unpack",
|
||||
Title: "Unpack MIDI Message",
|
||||
New: func(config config.ProcessorConfig) (Processor, error) {
|
||||
return &MIDIMessageUnpack{config: config}, nil
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
type MIDIMessageUnpack struct {
|
||||
config config.ProcessorConfig
|
||||
}
|
||||
@@ -89,13 +99,3 @@ func (mmu *MIDIMessageUnpack) Process(ctx context.Context, wrappedPayload common
|
||||
func (mmu *MIDIMessageUnpack) Type() string {
|
||||
return mmu.config.Type
|
||||
}
|
||||
|
||||
func init() {
|
||||
RegisterProcessor(ProcessorRegistration{
|
||||
Type: "midi.message.unpack",
|
||||
Title: "Unpack MIDI Message",
|
||||
New: func(config config.ProcessorConfig) (Processor, error) {
|
||||
return &MIDIMessageUnpack{config: config}, nil
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
@@ -15,6 +15,77 @@ import (
|
||||
"gitlab.com/gomidi/midi/v2"
|
||||
)
|
||||
|
||||
func init() {
|
||||
RegisterProcessor(ProcessorRegistration{
|
||||
Type: "midi.note_off.create",
|
||||
Title: "Create MIDI Note Off Message",
|
||||
ParamsSchema: &jsonschema.Schema{
|
||||
Type: "object",
|
||||
Properties: map[string]*jsonschema.Schema{
|
||||
"channel": {
|
||||
Title: "Channel",
|
||||
Description: "channel number for the note off message",
|
||||
Type: "string",
|
||||
},
|
||||
"note": {
|
||||
Title: "Note",
|
||||
Description: "note number for the note off message",
|
||||
Type: "string",
|
||||
},
|
||||
"velocity": {
|
||||
Title: "Velocity",
|
||||
Description: "velocity for the note off message",
|
||||
Type: "string",
|
||||
},
|
||||
},
|
||||
Required: []string{"channel", "note", "velocity"},
|
||||
AdditionalProperties: &jsonschema.Schema{Not: &jsonschema.Schema{}},
|
||||
},
|
||||
New: func(config config.ProcessorConfig) (Processor, error) {
|
||||
params := config.Params
|
||||
|
||||
channelString, err := params.GetString("channel")
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("midi.note_off.create channel error: %w", err)
|
||||
}
|
||||
|
||||
channelTemplate, err := template.New("channel").Parse(channelString)
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
noteString, err := params.GetString("note")
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("midi.note_off.create note error: %w", err)
|
||||
}
|
||||
|
||||
noteTemplate, err := template.New("note").Parse(noteString)
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
velocityString, err := params.GetString("velocity")
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("midi.note_off.create velocity error: %w", err)
|
||||
}
|
||||
|
||||
velocityTemplate, err := template.New("velocity").Parse(velocityString)
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &MIDINoteOffCreate{
|
||||
config: config,
|
||||
Channel: channelTemplate,
|
||||
Note: noteTemplate,
|
||||
Velocity: velocityTemplate,
|
||||
}, nil
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
type MIDINoteOffCreate struct {
|
||||
config config.ProcessorConfig
|
||||
Channel *template.Template
|
||||
@@ -78,71 +149,3 @@ func (mnoc *MIDINoteOffCreate) Process(ctx context.Context, wrappedPayload commo
|
||||
func (mnoc *MIDINoteOffCreate) Type() string {
|
||||
return mnoc.config.Type
|
||||
}
|
||||
|
||||
func init() {
|
||||
RegisterProcessor(ProcessorRegistration{
|
||||
Type: "midi.note_off.create",
|
||||
Title: "Create MIDI Note Off Message",
|
||||
ParamsSchema: &jsonschema.Schema{
|
||||
Type: "object",
|
||||
Properties: map[string]*jsonschema.Schema{
|
||||
"channel": {
|
||||
Title: "Channel",
|
||||
Type: "string",
|
||||
},
|
||||
"note": {
|
||||
Title: "Note",
|
||||
Type: "string",
|
||||
},
|
||||
"velocity": {
|
||||
Title: "Velocity",
|
||||
Type: "string",
|
||||
},
|
||||
},
|
||||
Required: []string{"channel", "note", "velocity"},
|
||||
AdditionalProperties: &jsonschema.Schema{Not: &jsonschema.Schema{}},
|
||||
},
|
||||
New: func(config config.ProcessorConfig) (Processor, error) {
|
||||
params := config.Params
|
||||
|
||||
channelString, err := params.GetString("channel")
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("midi.note_off.create channel error: %w", err)
|
||||
}
|
||||
|
||||
channelTemplate, err := template.New("channel").Parse(channelString)
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
noteString, err := params.GetString("note")
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("midi.note_off.create note error: %w", err)
|
||||
}
|
||||
|
||||
noteTemplate, err := template.New("note").Parse(noteString)
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
velocityString, err := params.GetString("velocity")
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("midi.note_off.create velocity error: %w", err)
|
||||
}
|
||||
|
||||
velocityTemplate, err := template.New("velocity").Parse(velocityString)
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &MIDINoteOffCreate{
|
||||
config: config,
|
||||
Channel: channelTemplate,
|
||||
Note: noteTemplate,
|
||||
Velocity: velocityTemplate,
|
||||
}, nil
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
@@ -15,6 +15,77 @@ import (
|
||||
"gitlab.com/gomidi/midi/v2"
|
||||
)
|
||||
|
||||
func init() {
|
||||
RegisterProcessor(ProcessorRegistration{
|
||||
Type: "midi.note_on.create",
|
||||
Title: "Create MIDI Note On Message",
|
||||
ParamsSchema: &jsonschema.Schema{
|
||||
Type: "object",
|
||||
Properties: map[string]*jsonschema.Schema{
|
||||
"channel": {
|
||||
Title: "Channel",
|
||||
Description: "channel number for the note on message",
|
||||
Type: "string",
|
||||
},
|
||||
"note": {
|
||||
Title: "Note",
|
||||
Description: "note number for the note on message",
|
||||
Type: "string",
|
||||
},
|
||||
"velocity": {
|
||||
Title: "Velocity",
|
||||
Description: "velocity for the note on message",
|
||||
Type: "string",
|
||||
},
|
||||
},
|
||||
Required: []string{"channel", "note", "velocity"},
|
||||
AdditionalProperties: &jsonschema.Schema{Not: &jsonschema.Schema{}},
|
||||
},
|
||||
New: func(config config.ProcessorConfig) (Processor, error) {
|
||||
params := config.Params
|
||||
|
||||
channelString, err := params.GetString("channel")
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("midi.note_on.create channel error: %w", err)
|
||||
}
|
||||
|
||||
channelTemplate, err := template.New("channel").Parse(channelString)
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
noteString, err := params.GetString("note")
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("midi.note_on.create note error: %w", err)
|
||||
}
|
||||
|
||||
noteTemplate, err := template.New("note").Parse(noteString)
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
velocityString, err := params.GetString("velocity")
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("midi.note_on.create velocity error: %w", err)
|
||||
}
|
||||
|
||||
velocityTemplate, err := template.New("velocity").Parse(velocityString)
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &MIDINoteOnCreate{
|
||||
config: config,
|
||||
Channel: channelTemplate,
|
||||
Note: noteTemplate,
|
||||
Velocity: velocityTemplate,
|
||||
}, nil
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
type MIDINoteOnCreate struct {
|
||||
config config.ProcessorConfig
|
||||
Channel *template.Template
|
||||
@@ -77,71 +148,3 @@ func (mnoc *MIDINoteOnCreate) Process(ctx context.Context, wrappedPayload common
|
||||
func (mnoc *MIDINoteOnCreate) Type() string {
|
||||
return mnoc.config.Type
|
||||
}
|
||||
|
||||
func init() {
|
||||
RegisterProcessor(ProcessorRegistration{
|
||||
Type: "midi.note_on.create",
|
||||
Title: "Create MIDI Note On Message",
|
||||
ParamsSchema: &jsonschema.Schema{
|
||||
Type: "object",
|
||||
Properties: map[string]*jsonschema.Schema{
|
||||
"channel": {
|
||||
Title: "Channel",
|
||||
Type: "string",
|
||||
},
|
||||
"note": {
|
||||
Title: "Note",
|
||||
Type: "string",
|
||||
},
|
||||
"velocity": {
|
||||
Title: "Velocity",
|
||||
Type: "string",
|
||||
},
|
||||
},
|
||||
Required: []string{"channel", "note", "velocity"},
|
||||
AdditionalProperties: &jsonschema.Schema{Not: &jsonschema.Schema{}},
|
||||
},
|
||||
New: func(config config.ProcessorConfig) (Processor, error) {
|
||||
params := config.Params
|
||||
|
||||
channelString, err := params.GetString("channel")
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("midi.note_on.create channel error: %w", err)
|
||||
}
|
||||
|
||||
channelTemplate, err := template.New("channel").Parse(channelString)
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
noteString, err := params.GetString("note")
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("midi.note_on.create note error: %w", err)
|
||||
}
|
||||
|
||||
noteTemplate, err := template.New("note").Parse(noteString)
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
velocityString, err := params.GetString("velocity")
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("midi.note_on.create velocity error: %w", err)
|
||||
}
|
||||
|
||||
velocityTemplate, err := template.New("velocity").Parse(velocityString)
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &MIDINoteOnCreate{
|
||||
config: config,
|
||||
Channel: channelTemplate,
|
||||
Note: noteTemplate,
|
||||
Velocity: velocityTemplate,
|
||||
}, nil
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
@@ -15,6 +15,60 @@ import (
|
||||
"gitlab.com/gomidi/midi/v2"
|
||||
)
|
||||
|
||||
func init() {
|
||||
RegisterProcessor(ProcessorRegistration{
|
||||
Type: "midi.program_change.create",
|
||||
Title: "Create MIDI Prgoram Change Message",
|
||||
ParamsSchema: &jsonschema.Schema{
|
||||
Type: "object",
|
||||
Properties: map[string]*jsonschema.Schema{
|
||||
"channel": {
|
||||
Title: "Channel",
|
||||
Description: "channel number for the program change message",
|
||||
Type: "string",
|
||||
},
|
||||
"program": {
|
||||
Title: "Program",
|
||||
Description: "program number for the program change message",
|
||||
Type: "string",
|
||||
},
|
||||
},
|
||||
Required: []string{"type", "channel", "program"},
|
||||
AdditionalProperties: &jsonschema.Schema{Not: &jsonschema.Schema{}},
|
||||
},
|
||||
New: func(config config.ProcessorConfig) (Processor, error) {
|
||||
params := config.Params
|
||||
|
||||
channelString, err := params.GetString("channel")
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("midi.program_change.create channel error: %w", err)
|
||||
}
|
||||
|
||||
channelTemplate, err := template.New("channel").Parse(channelString)
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
programString, err := params.GetString("program")
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("midi.program_change.create program error: %w", err)
|
||||
}
|
||||
|
||||
programTemplate, err := template.New("program").Parse(programString)
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &MIDIProgramChangeCreate{
|
||||
config: config,
|
||||
Channel: channelTemplate,
|
||||
Program: programTemplate,
|
||||
}, nil
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
type MIDIProgramChangeCreate struct {
|
||||
config config.ProcessorConfig
|
||||
Channel *template.Template
|
||||
@@ -62,55 +116,3 @@ func (mpcc *MIDIProgramChangeCreate) Process(ctx context.Context, wrappedPayload
|
||||
func (mpcc *MIDIProgramChangeCreate) Type() string {
|
||||
return mpcc.config.Type
|
||||
}
|
||||
|
||||
func init() {
|
||||
RegisterProcessor(ProcessorRegistration{
|
||||
Type: "midi.program_change.create",
|
||||
Title: "Create MIDI Prgoram Change Message",
|
||||
ParamsSchema: &jsonschema.Schema{
|
||||
Type: "object",
|
||||
Properties: map[string]*jsonschema.Schema{
|
||||
"channel": {
|
||||
Title: "Channel",
|
||||
Type: "string",
|
||||
},
|
||||
"program": {
|
||||
Title: "Program",
|
||||
Type: "string",
|
||||
},
|
||||
},
|
||||
Required: []string{"type", "channel", "program"},
|
||||
AdditionalProperties: &jsonschema.Schema{Not: &jsonschema.Schema{}},
|
||||
},
|
||||
New: func(config config.ProcessorConfig) (Processor, error) {
|
||||
params := config.Params
|
||||
|
||||
channelString, err := params.GetString("channel")
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("midi.program_change.create channel error: %w", err)
|
||||
}
|
||||
|
||||
channelTemplate, err := template.New("channel").Parse(channelString)
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
programString, err := params.GetString("program")
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("midi.program_change.create program error: %w", err)
|
||||
}
|
||||
|
||||
programTemplate, err := template.New("program").Parse(programString)
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &MIDIProgramChangeCreate{
|
||||
config: config,
|
||||
Channel: channelTemplate,
|
||||
Program: programTemplate,
|
||||
}, nil
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
@@ -11,44 +11,17 @@ import (
|
||||
"github.com/jwetzell/showbridge-go/internal/config"
|
||||
)
|
||||
|
||||
type RouterOutput struct {
|
||||
config config.ProcessorConfig
|
||||
ModuleId string
|
||||
logger *slog.Logger
|
||||
}
|
||||
|
||||
func (ro *RouterOutput) Process(ctx context.Context, wrappedPayload common.WrappedPayload) (common.WrappedPayload, error) {
|
||||
|
||||
if wrappedPayload.Router == nil {
|
||||
wrappedPayload.End = true
|
||||
return wrappedPayload, errors.New("router.output no router found")
|
||||
}
|
||||
|
||||
err := wrappedPayload.Router.HandleOutput(ctx, ro.ModuleId, wrappedPayload.Payload)
|
||||
|
||||
if err != nil {
|
||||
wrappedPayload.End = true
|
||||
return wrappedPayload, fmt.Errorf("router.output failed to send output: %w", err)
|
||||
}
|
||||
|
||||
return wrappedPayload, nil
|
||||
}
|
||||
|
||||
func (ro *RouterOutput) Type() string {
|
||||
return ro.config.Type
|
||||
}
|
||||
|
||||
func init() {
|
||||
RegisterProcessor(ProcessorRegistration{
|
||||
Type: "router.output",
|
||||
Title: "Router Output",
|
||||
Type: "module.output",
|
||||
Title: "Module Output",
|
||||
ParamsSchema: &jsonschema.Schema{
|
||||
Type: "object",
|
||||
Properties: map[string]*jsonschema.Schema{
|
||||
"module": {
|
||||
Title: "Module ID",
|
||||
Type: "string",
|
||||
Description: "ID of module to send output to",
|
||||
Type: "string",
|
||||
},
|
||||
},
|
||||
Required: []string{"module"},
|
||||
@@ -61,10 +34,53 @@ func init() {
|
||||
moduleId, err := params.GetString("module")
|
||||
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("router.output module error: %w", err)
|
||||
return nil, fmt.Errorf("module.output module error: %w", err)
|
||||
}
|
||||
|
||||
return &RouterOutput{config: config, ModuleId: moduleId, logger: slog.Default().With("component", "processor", "type", config.Type)}, nil
|
||||
return &ModuleOutput{config: config, ModuleId: moduleId, logger: slog.Default().With("component", "processor", "type", config.Type)}, nil
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
type ModuleOutput struct {
|
||||
config config.ProcessorConfig
|
||||
ModuleId string
|
||||
logger *slog.Logger
|
||||
module common.OutputModule
|
||||
}
|
||||
|
||||
func (mo *ModuleOutput) Process(ctx context.Context, wrappedPayload common.WrappedPayload) (common.WrappedPayload, error) {
|
||||
|
||||
if mo.module == nil {
|
||||
if wrappedPayload.Modules == nil {
|
||||
wrappedPayload.End = true
|
||||
return wrappedPayload, errors.New("module.output wrapped payload has no modules")
|
||||
}
|
||||
|
||||
module, ok := wrappedPayload.Modules[mo.ModuleId]
|
||||
if !ok {
|
||||
wrappedPayload.End = true
|
||||
return wrappedPayload, fmt.Errorf("module.output unable to find module with id: %s", mo.ModuleId)
|
||||
}
|
||||
|
||||
outputModule, ok := module.(common.OutputModule)
|
||||
if !ok {
|
||||
wrappedPayload.End = true
|
||||
return wrappedPayload, fmt.Errorf("module.output module with id %s is not an OutputModule", mo.ModuleId)
|
||||
}
|
||||
mo.module = outputModule
|
||||
}
|
||||
|
||||
err := mo.module.Output(ctx, wrappedPayload.Payload)
|
||||
|
||||
if err != nil {
|
||||
wrappedPayload.End = true
|
||||
return wrappedPayload, fmt.Errorf("module.output failed to send output: %w", err)
|
||||
}
|
||||
|
||||
return wrappedPayload, nil
|
||||
}
|
||||
|
||||
func (mo *ModuleOutput) Type() string {
|
||||
return mo.config.Type
|
||||
}
|
||||
@@ -1,145 +0,0 @@
|
||||
package processor
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
|
||||
"github.com/google/jsonschema-go/jsonschema"
|
||||
"github.com/jwetzell/showbridge-go/internal/common"
|
||||
"github.com/jwetzell/showbridge-go/internal/config"
|
||||
)
|
||||
|
||||
type MQTTMessage struct {
|
||||
topic string
|
||||
qos byte
|
||||
payload []byte
|
||||
retained bool
|
||||
}
|
||||
|
||||
type MQTTMessageCreate struct {
|
||||
config config.ProcessorConfig
|
||||
Topic string
|
||||
QoS byte
|
||||
Retained bool
|
||||
Payload []byte
|
||||
}
|
||||
|
||||
func NewMQTTMessage(topic string, qos byte, retained bool, payload []byte) MQTTMessage {
|
||||
return MQTTMessage{
|
||||
topic: topic,
|
||||
qos: qos,
|
||||
retained: retained,
|
||||
payload: payload,
|
||||
}
|
||||
}
|
||||
|
||||
func (mm MQTTMessage) Duplicate() bool {
|
||||
// TODO(jwetzell): implement?
|
||||
return false
|
||||
}
|
||||
|
||||
func (mm MQTTMessage) Qos() byte {
|
||||
return mm.qos
|
||||
}
|
||||
|
||||
func (mm MQTTMessage) Retained() bool {
|
||||
return mm.retained
|
||||
}
|
||||
|
||||
func (mm MQTTMessage) Topic() string {
|
||||
return mm.topic
|
||||
}
|
||||
|
||||
func (mm MQTTMessage) MessageID() uint16 {
|
||||
// TODO(jwetzell): implement?
|
||||
return 0
|
||||
}
|
||||
|
||||
func (mm MQTTMessage) Payload() []byte {
|
||||
return mm.payload
|
||||
}
|
||||
|
||||
func (mm MQTTMessage) Ack() {}
|
||||
|
||||
func (mmc *MQTTMessageCreate) Process(ctx context.Context, wrappedPayload common.WrappedPayload) (common.WrappedPayload, error) {
|
||||
// TODO(jwetzell): support templating
|
||||
|
||||
wrappedPayload.Payload = MQTTMessage{
|
||||
topic: mmc.Topic,
|
||||
qos: mmc.QoS,
|
||||
retained: mmc.Retained,
|
||||
payload: mmc.Payload,
|
||||
}
|
||||
|
||||
return wrappedPayload, nil
|
||||
}
|
||||
|
||||
func (mmc *MQTTMessageCreate) Type() string {
|
||||
return mmc.config.Type
|
||||
}
|
||||
|
||||
func init() {
|
||||
RegisterProcessor(ProcessorRegistration{
|
||||
Type: "mqtt.message.create",
|
||||
Title: "Create MQTT Message",
|
||||
ParamsSchema: &jsonschema.Schema{
|
||||
Type: "object",
|
||||
Properties: map[string]*jsonschema.Schema{
|
||||
"topic": {
|
||||
Title: "Topic",
|
||||
Type: "string",
|
||||
},
|
||||
"qos": {
|
||||
Title: "QoS",
|
||||
Type: "number",
|
||||
},
|
||||
"retained": {
|
||||
Title: "Retained",
|
||||
Type: "boolean",
|
||||
},
|
||||
"payload": {
|
||||
Title: "Payload",
|
||||
Type: "string",
|
||||
},
|
||||
},
|
||||
Required: []string{"topic", "qos", "retained", "payload"},
|
||||
AdditionalProperties: &jsonschema.Schema{Not: &jsonschema.Schema{}},
|
||||
},
|
||||
New: func(processorConfig config.ProcessorConfig) (Processor, error) {
|
||||
params := processorConfig.Params
|
||||
topicString, err := params.GetString("topic")
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("mqtt.message.create topic error: %w", err)
|
||||
}
|
||||
|
||||
qosByte, err := params.GetInt("qos")
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("mqtt.message.create qos error: %w", err)
|
||||
}
|
||||
|
||||
retainedBool, err := params.GetBool("retained")
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("mqtt.message.create retained error: %w", err)
|
||||
}
|
||||
|
||||
//TODO(jwetzell): convert payload into []byte or string for sending
|
||||
payloadString, err := params.GetString("payload")
|
||||
if err != nil {
|
||||
if errors.Is(err, config.ErrParamNotString) {
|
||||
payloadBytes, err := params.GetByteSlice("payload")
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("mqtt.message.create payload error: %w", err)
|
||||
}
|
||||
return &MQTTMessageCreate{config: processorConfig, Topic: topicString, QoS: byte(qosByte), Retained: retainedBool, Payload: payloadBytes}, nil
|
||||
} else {
|
||||
return nil, fmt.Errorf("mqtt.message.create payload error: %w", err)
|
||||
}
|
||||
}
|
||||
|
||||
payloadBytes := []byte(payloadString)
|
||||
|
||||
return &MQTTMessageCreate{config: processorConfig, Topic: topicString, QoS: byte(qosByte), Retained: retainedBool, Payload: payloadBytes}, nil
|
||||
},
|
||||
})
|
||||
}
|
||||
@@ -1,107 +0,0 @@
|
||||
package processor
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"fmt"
|
||||
"text/template"
|
||||
|
||||
"github.com/google/jsonschema-go/jsonschema"
|
||||
"github.com/jwetzell/showbridge-go/internal/common"
|
||||
"github.com/jwetzell/showbridge-go/internal/config"
|
||||
)
|
||||
|
||||
type NATSMessage struct {
|
||||
Subject string
|
||||
Payload []byte
|
||||
}
|
||||
|
||||
type NATSMessageCreate struct {
|
||||
config config.ProcessorConfig
|
||||
Subject *template.Template
|
||||
Payload *template.Template
|
||||
}
|
||||
|
||||
func (nmc *NATSMessageCreate) Process(ctx context.Context, wrappedPayload common.WrappedPayload) (common.WrappedPayload, error) {
|
||||
|
||||
templateData := wrappedPayload
|
||||
|
||||
var payloadBuffer bytes.Buffer
|
||||
err := nmc.Payload.Execute(&payloadBuffer, templateData)
|
||||
|
||||
if err != nil {
|
||||
wrappedPayload.End = true
|
||||
return wrappedPayload, err
|
||||
}
|
||||
|
||||
payloadString := payloadBuffer.String()
|
||||
|
||||
var subjectBuffer bytes.Buffer
|
||||
err = nmc.Subject.Execute(&subjectBuffer, templateData)
|
||||
|
||||
if err != nil {
|
||||
wrappedPayload.End = true
|
||||
return wrappedPayload, err
|
||||
}
|
||||
|
||||
subjectString := subjectBuffer.String()
|
||||
|
||||
wrappedPayload.Payload = NATSMessage{
|
||||
Subject: subjectString,
|
||||
Payload: []byte(payloadString),
|
||||
}
|
||||
|
||||
return wrappedPayload, nil
|
||||
}
|
||||
|
||||
func (nmc *NATSMessageCreate) Type() string {
|
||||
return nmc.config.Type
|
||||
}
|
||||
|
||||
func init() {
|
||||
RegisterProcessor(ProcessorRegistration{
|
||||
Type: "nats.message.create",
|
||||
Title: "Create NATS Message",
|
||||
ParamsSchema: &jsonschema.Schema{
|
||||
Type: "object",
|
||||
Properties: map[string]*jsonschema.Schema{
|
||||
"subject": {
|
||||
Title: "Subject",
|
||||
Type: "string",
|
||||
},
|
||||
"payload": {
|
||||
Title: "Payload",
|
||||
Type: "string",
|
||||
},
|
||||
},
|
||||
Required: []string{"subject", "payload"},
|
||||
AdditionalProperties: &jsonschema.Schema{Not: &jsonschema.Schema{}},
|
||||
},
|
||||
New: func(config config.ProcessorConfig) (Processor, error) {
|
||||
params := config.Params
|
||||
subjectString, err := params.GetString("subject")
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("nats.message.create subject error: %w", err)
|
||||
}
|
||||
|
||||
subjectTemplate, err := template.New("subject").Parse(subjectString)
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
payloadString, err := params.GetString("payload")
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("nats.message.create payload error: %w", err)
|
||||
}
|
||||
|
||||
payloadTemplate, err := template.New("payload").Parse(payloadString)
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &NATSMessageCreate{config: config, Subject: subjectTemplate, Payload: payloadTemplate}, nil
|
||||
},
|
||||
})
|
||||
}
|
||||
@@ -15,6 +15,82 @@ import (
|
||||
"github.com/jwetzell/showbridge-go/internal/config"
|
||||
)
|
||||
|
||||
func init() {
|
||||
RegisterProcessor(ProcessorRegistration{
|
||||
Type: "osc.message.create",
|
||||
Title: "Create OSC Message",
|
||||
ParamsSchema: &jsonschema.Schema{
|
||||
Type: "object",
|
||||
Properties: map[string]*jsonschema.Schema{
|
||||
"address": {
|
||||
Title: "Address",
|
||||
Description: "OSC address for the message",
|
||||
Type: "string",
|
||||
},
|
||||
"args": {
|
||||
Title: "Arguments",
|
||||
Description: "arguments for the OSC message",
|
||||
Type: "array",
|
||||
Items: &jsonschema.Schema{
|
||||
Type: "string",
|
||||
},
|
||||
},
|
||||
"types": {
|
||||
Title: "Argument Types",
|
||||
Description: "string of OSC types corresponding to the arguments in args",
|
||||
Type: "string",
|
||||
},
|
||||
},
|
||||
Required: []string{"address"},
|
||||
AdditionalProperties: &jsonschema.Schema{Not: &jsonschema.Schema{}},
|
||||
},
|
||||
New: func(processorConfig config.ProcessorConfig) (Processor, error) {
|
||||
params := processorConfig.Params
|
||||
addressString, err := params.GetString("address")
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("osc.message.create address error: %w", err)
|
||||
}
|
||||
|
||||
addressTemplate, err := template.New("address").Parse(addressString)
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
argStrings, err := params.GetStringSlice("args")
|
||||
if err != nil {
|
||||
if errors.Is(err, config.ErrParamNotFound) {
|
||||
return &OSCMessageCreate{config: processorConfig, Address: addressTemplate}, nil
|
||||
} else {
|
||||
return nil, fmt.Errorf("osc.message.create args error: %w", err)
|
||||
}
|
||||
}
|
||||
|
||||
typesString, err := params.GetString("types")
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("osc.message.create types error: %w", err)
|
||||
}
|
||||
|
||||
if len(argStrings) != len(typesString) {
|
||||
return nil, errors.New("osc.message.create args and types must be the same length")
|
||||
}
|
||||
|
||||
argTemplates := []*template.Template{}
|
||||
|
||||
for _, argString := range argStrings {
|
||||
|
||||
argTemplate, err := template.New("arg").Parse(argString)
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
argTemplates = append(argTemplates, argTemplate)
|
||||
}
|
||||
return &OSCMessageCreate{config: processorConfig, Address: addressTemplate, Args: argTemplates, Types: typesString}, nil
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
type OSCMessageCreate struct {
|
||||
config config.ProcessorConfig
|
||||
Address *template.Template
|
||||
@@ -85,79 +161,6 @@ func (omc *OSCMessageCreate) Type() string {
|
||||
return omc.config.Type
|
||||
}
|
||||
|
||||
func init() {
|
||||
RegisterProcessor(ProcessorRegistration{
|
||||
Type: "osc.message.create",
|
||||
Title: "Create OSC Message",
|
||||
ParamsSchema: &jsonschema.Schema{
|
||||
Type: "object",
|
||||
Properties: map[string]*jsonschema.Schema{
|
||||
"address": {
|
||||
Title: "Address",
|
||||
Type: "string",
|
||||
},
|
||||
"args": {
|
||||
Title: "Arguments",
|
||||
Type: "array",
|
||||
Items: &jsonschema.Schema{
|
||||
Type: "string",
|
||||
},
|
||||
},
|
||||
"types": {
|
||||
Title: "Argument Types",
|
||||
Type: "string",
|
||||
},
|
||||
},
|
||||
Required: []string{"address"},
|
||||
AdditionalProperties: &jsonschema.Schema{Not: &jsonschema.Schema{}},
|
||||
},
|
||||
New: func(processorConfig config.ProcessorConfig) (Processor, error) {
|
||||
params := processorConfig.Params
|
||||
addressString, err := params.GetString("address")
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("osc.message.create address error: %w", err)
|
||||
}
|
||||
|
||||
addressTemplate, err := template.New("address").Parse(addressString)
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
argStrings, err := params.GetStringSlice("args")
|
||||
if err != nil {
|
||||
if errors.Is(err, config.ErrParamNotFound) {
|
||||
return &OSCMessageCreate{config: processorConfig, Address: addressTemplate}, nil
|
||||
} else {
|
||||
return nil, fmt.Errorf("osc.message.create args error: %w", err)
|
||||
}
|
||||
}
|
||||
|
||||
typesString, err := params.GetString("types")
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("osc.message.create types error: %w", err)
|
||||
}
|
||||
|
||||
if len(argStrings) != len(typesString) {
|
||||
return nil, errors.New("osc.message.create args and types must be the same length")
|
||||
}
|
||||
|
||||
argTemplates := []*template.Template{}
|
||||
|
||||
for _, argString := range argStrings {
|
||||
|
||||
argTemplate, err := template.New("arg").Parse(argString)
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
argTemplates = append(argTemplates, argTemplate)
|
||||
}
|
||||
return &OSCMessageCreate{config: processorConfig, Address: addressTemplate, Args: argTemplates, Types: typesString}, nil
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
func argToTypedArg(rawArg string, oscType byte) (osc.OSCArg, error) {
|
||||
|
||||
switch oscType {
|
||||
|
||||
@@ -10,6 +10,16 @@ import (
|
||||
"github.com/jwetzell/showbridge-go/internal/config"
|
||||
)
|
||||
|
||||
func init() {
|
||||
RegisterProcessor(ProcessorRegistration{
|
||||
Type: "osc.message.decode",
|
||||
Title: "Decode OSC Message",
|
||||
New: func(config config.ProcessorConfig) (Processor, error) {
|
||||
return &OSCMessageDecode{config: config}, nil
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
type OSCMessageDecode struct {
|
||||
config config.ProcessorConfig
|
||||
}
|
||||
@@ -45,13 +55,3 @@ func (omd *OSCMessageDecode) Process(ctx context.Context, wrappedPayload common.
|
||||
func (omd *OSCMessageDecode) Type() string {
|
||||
return omd.config.Type
|
||||
}
|
||||
|
||||
func init() {
|
||||
RegisterProcessor(ProcessorRegistration{
|
||||
Type: "osc.message.decode",
|
||||
Title: "Decode OSC Message",
|
||||
New: func(config config.ProcessorConfig) (Processor, error) {
|
||||
return &OSCMessageDecode{config: config}, nil
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
@@ -14,6 +14,16 @@ type OSCMessageEncode struct {
|
||||
config config.ProcessorConfig
|
||||
}
|
||||
|
||||
func init() {
|
||||
RegisterProcessor(ProcessorRegistration{
|
||||
Type: "osc.message.encode",
|
||||
Title: "Encode OSC Message",
|
||||
New: func(config config.ProcessorConfig) (Processor, error) {
|
||||
return &OSCMessageEncode{config: config}, nil
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
func (ome *OSCMessageEncode) Process(ctx context.Context, wrappedPayload common.WrappedPayload) (common.WrappedPayload, error) {
|
||||
payload := wrappedPayload.Payload
|
||||
payloadMessage, ok := common.GetAnyAs[*osc.OSCMessage](payload)
|
||||
@@ -35,13 +45,3 @@ func (ome *OSCMessageEncode) Process(ctx context.Context, wrappedPayload common.
|
||||
func (ome *OSCMessageEncode) Type() string {
|
||||
return ome.config.Type
|
||||
}
|
||||
|
||||
func init() {
|
||||
RegisterProcessor(ProcessorRegistration{
|
||||
Type: "osc.message.encode",
|
||||
Title: "Encode OSC Message",
|
||||
New: func(config config.ProcessorConfig) (Processor, error) {
|
||||
return &OSCMessageEncode{config: config}, nil
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
@@ -35,13 +35,33 @@ func RegisterProcessor(processor ProcessorRegistration) {
|
||||
processorRegistryMu.Lock()
|
||||
defer processorRegistryMu.Unlock()
|
||||
|
||||
if _, ok := ProcessorRegistry[string(processor.Type)]; ok {
|
||||
if _, ok := processorRegistry[string(processor.Type)]; ok {
|
||||
panic(fmt.Sprintf("processor already registered: %s", processor.Type))
|
||||
}
|
||||
ProcessorRegistry[string(processor.Type)] = processor
|
||||
processorRegistry[string(processor.Type)] = processor
|
||||
}
|
||||
|
||||
type ProcessorRegistry map[string]ProcessorRegistration
|
||||
|
||||
func GetProcessorRegistration(processorType string) (ProcessorRegistration, bool) {
|
||||
processorRegistryMu.RLock()
|
||||
defer processorRegistryMu.RUnlock()
|
||||
processor, ok := processorRegistry[processorType]
|
||||
return processor, ok
|
||||
}
|
||||
|
||||
func GetProcessorRegistrations() []ProcessorRegistration {
|
||||
processorRegistryMu.RLock()
|
||||
defer processorRegistryMu.RUnlock()
|
||||
|
||||
registrations := make([]ProcessorRegistration, 0, len(processorRegistry))
|
||||
for _, processor := range processorRegistry {
|
||||
registrations = append(registrations, processor)
|
||||
}
|
||||
return registrations
|
||||
}
|
||||
|
||||
var (
|
||||
processorRegistryMu sync.RWMutex
|
||||
ProcessorRegistry = make(map[string]ProcessorRegistration)
|
||||
processorRegistry = make(map[string]ProcessorRegistration)
|
||||
)
|
||||
|
||||
@@ -0,0 +1,109 @@
|
||||
package processor
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"text/template"
|
||||
|
||||
"github.com/google/jsonschema-go/jsonschema"
|
||||
"github.com/jwetzell/showbridge-go/internal/common"
|
||||
"github.com/jwetzell/showbridge-go/internal/config"
|
||||
)
|
||||
|
||||
func init() {
|
||||
RegisterProcessor(ProcessorRegistration{
|
||||
Type: "pubsub.publish",
|
||||
Title: "Publish to Pub/Sub Topic",
|
||||
ParamsSchema: &jsonschema.Schema{
|
||||
Type: "object",
|
||||
Properties: map[string]*jsonschema.Schema{
|
||||
"module": {
|
||||
Title: "Module ID",
|
||||
Description: "ID of the module to publish to",
|
||||
Type: "string",
|
||||
},
|
||||
"topic": {
|
||||
Title: "Topic",
|
||||
Description: "topic to publish to",
|
||||
Type: "string",
|
||||
},
|
||||
},
|
||||
Required: []string{"module", "topic"},
|
||||
AdditionalProperties: &jsonschema.Schema{Not: &jsonschema.Schema{}},
|
||||
},
|
||||
New: func(config config.ProcessorConfig) (Processor, error) {
|
||||
|
||||
params := config.Params
|
||||
|
||||
moduleIdString, err := params.GetString("module")
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("pubsub.publish module error: %w", err)
|
||||
}
|
||||
|
||||
topicString, err := params.GetString("topic")
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("pubsub.publish topic error: %w", err)
|
||||
}
|
||||
|
||||
topicTemplate, err := template.New("topic").Parse(topicString)
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &PubSubPublish{config: config, ModuleId: moduleIdString, Topic: topicTemplate, logger: slog.Default().With("component", "processor", "type", config.Type)}, nil
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
type PubSubPublish struct {
|
||||
config config.ProcessorConfig
|
||||
ModuleId string
|
||||
Topic *template.Template
|
||||
logger *slog.Logger
|
||||
module common.PubSubModule
|
||||
}
|
||||
|
||||
func (psp *PubSubPublish) Process(ctx context.Context, wrappedPayload common.WrappedPayload) (common.WrappedPayload, error) {
|
||||
if psp.module == nil {
|
||||
if wrappedPayload.Modules == nil {
|
||||
wrappedPayload.End = true
|
||||
return wrappedPayload, errors.New("pubsub.publish wrapped payload has no modules")
|
||||
}
|
||||
|
||||
module, ok := wrappedPayload.Modules[psp.ModuleId]
|
||||
if !ok {
|
||||
wrappedPayload.End = true
|
||||
return wrappedPayload, fmt.Errorf("pubsub.publish unable to find module with id: %s", psp.ModuleId)
|
||||
}
|
||||
|
||||
dbModule, ok := module.(common.PubSubModule)
|
||||
if !ok {
|
||||
wrappedPayload.End = true
|
||||
return wrappedPayload, fmt.Errorf("pubsub.publish module with id %s is not an OutputModule", psp.ModuleId)
|
||||
}
|
||||
psp.module = dbModule
|
||||
}
|
||||
|
||||
var topicBuffer bytes.Buffer
|
||||
err := psp.Topic.Execute(&topicBuffer, wrappedPayload)
|
||||
|
||||
if err != nil {
|
||||
wrappedPayload.End = true
|
||||
return wrappedPayload, err
|
||||
}
|
||||
|
||||
err = psp.module.Publish(ctx, topicBuffer.String(), wrappedPayload.Payload)
|
||||
if err != nil {
|
||||
wrappedPayload.End = true
|
||||
return wrappedPayload, fmt.Errorf("pubsub.publish error publishing: %w", err)
|
||||
}
|
||||
|
||||
return wrappedPayload, nil
|
||||
}
|
||||
|
||||
func (psp *PubSubPublish) Type() string {
|
||||
return psp.config.Type
|
||||
}
|
||||
@@ -11,36 +11,6 @@ import (
|
||||
"github.com/jwetzell/showbridge-go/internal/config"
|
||||
)
|
||||
|
||||
type RouterInput struct {
|
||||
config config.ProcessorConfig
|
||||
SourceId string
|
||||
logger *slog.Logger
|
||||
}
|
||||
|
||||
func (ro *RouterInput) Process(ctx context.Context, wrappedPayload common.WrappedPayload) (common.WrappedPayload, error) {
|
||||
|
||||
payload := wrappedPayload.Payload
|
||||
if wrappedPayload.Router == nil {
|
||||
wrappedPayload.End = true
|
||||
return wrappedPayload, errors.New("router.input no router found")
|
||||
}
|
||||
|
||||
_, err := wrappedPayload.Router.HandleInput(ctx, ro.SourceId, payload)
|
||||
|
||||
if err != nil {
|
||||
wrappedPayload.End = true
|
||||
return wrappedPayload, errors.New("router.input failed to send input")
|
||||
}
|
||||
|
||||
wrappedPayload.Payload = payload
|
||||
|
||||
return wrappedPayload, nil
|
||||
}
|
||||
|
||||
func (ro *RouterInput) Type() string {
|
||||
return ro.config.Type
|
||||
}
|
||||
|
||||
func init() {
|
||||
RegisterProcessor(ProcessorRegistration{
|
||||
Type: "router.input",
|
||||
@@ -50,8 +20,8 @@ func init() {
|
||||
Properties: map[string]*jsonschema.Schema{
|
||||
"source": {
|
||||
Title: "Source",
|
||||
Type: "string",
|
||||
Description: "source to report as to the router",
|
||||
Type: "string",
|
||||
},
|
||||
},
|
||||
Required: []string{"source"},
|
||||
@@ -71,3 +41,33 @@ func init() {
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
type RouterInput struct {
|
||||
config config.ProcessorConfig
|
||||
SourceId string
|
||||
logger *slog.Logger
|
||||
}
|
||||
|
||||
func (ri *RouterInput) Process(ctx context.Context, wrappedPayload common.WrappedPayload) (common.WrappedPayload, error) {
|
||||
|
||||
payload := wrappedPayload.Payload
|
||||
if wrappedPayload.InputHandler == nil {
|
||||
wrappedPayload.End = true
|
||||
return wrappedPayload, errors.New("router.input no input handler found")
|
||||
}
|
||||
|
||||
_, err := wrappedPayload.InputHandler(ctx, ri.SourceId, payload)
|
||||
|
||||
if err != nil {
|
||||
wrappedPayload.End = true
|
||||
return wrappedPayload, errors.New("router.input failed to send input")
|
||||
}
|
||||
|
||||
wrappedPayload.Payload = payload
|
||||
|
||||
return wrappedPayload, nil
|
||||
}
|
||||
|
||||
func (ri *RouterInput) Type() string {
|
||||
return ri.config.Type
|
||||
}
|
||||
|
||||
@@ -17,24 +17,6 @@ type ScriptExpr struct {
|
||||
Program *vm.Program
|
||||
}
|
||||
|
||||
func (se *ScriptExpr) Process(ctx context.Context, wrappedPayload common.WrappedPayload) (common.WrappedPayload, error) {
|
||||
|
||||
exprEnv := wrappedPayload
|
||||
|
||||
output, err := expr.Run(se.Program, exprEnv)
|
||||
if err != nil {
|
||||
wrappedPayload.End = true
|
||||
return wrappedPayload, err
|
||||
}
|
||||
|
||||
wrappedPayload.Payload = output
|
||||
return wrappedPayload, nil
|
||||
}
|
||||
|
||||
func (se *ScriptExpr) Type() string {
|
||||
return se.config.Type
|
||||
}
|
||||
|
||||
func init() {
|
||||
RegisterProcessor(ProcessorRegistration{
|
||||
Type: "script.expr",
|
||||
@@ -43,8 +25,9 @@ func init() {
|
||||
Type: "object",
|
||||
Properties: map[string]*jsonschema.Schema{
|
||||
"expression": {
|
||||
Title: "Expression",
|
||||
Type: "string",
|
||||
Title: "Expression",
|
||||
Description: "Expr expression to evaluate",
|
||||
Type: "string",
|
||||
},
|
||||
},
|
||||
Required: []string{"expression"},
|
||||
@@ -67,3 +50,21 @@ func init() {
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
func (se *ScriptExpr) Process(ctx context.Context, wrappedPayload common.WrappedPayload) (common.WrappedPayload, error) {
|
||||
|
||||
exprEnv := wrappedPayload
|
||||
|
||||
output, err := expr.Run(se.Program, exprEnv)
|
||||
if err != nil {
|
||||
wrappedPayload.End = true
|
||||
return wrappedPayload, err
|
||||
}
|
||||
|
||||
wrappedPayload.Payload = output
|
||||
return wrappedPayload, nil
|
||||
}
|
||||
|
||||
func (se *ScriptExpr) Type() string {
|
||||
return se.config.Type
|
||||
}
|
||||
|
||||
@@ -10,6 +10,51 @@ import (
|
||||
"modernc.org/quickjs"
|
||||
)
|
||||
|
||||
func init() {
|
||||
RegisterProcessor(ProcessorRegistration{
|
||||
Type: "script.js",
|
||||
Title: "Run JavaScript",
|
||||
ParamsSchema: &jsonschema.Schema{
|
||||
Type: "object",
|
||||
Properties: map[string]*jsonschema.Schema{
|
||||
"program": {
|
||||
Title: "Program",
|
||||
Description: "JavaScript program to run",
|
||||
Type: "string",
|
||||
},
|
||||
},
|
||||
Required: []string{"program"},
|
||||
AdditionalProperties: &jsonschema.Schema{Not: &jsonschema.Schema{}},
|
||||
},
|
||||
New: func(config config.ProcessorConfig) (Processor, error) {
|
||||
params := config.Params
|
||||
|
||||
programString, err := params.GetString("program")
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("script.js program error: %w", err)
|
||||
}
|
||||
|
||||
vm, err := quickjs.NewVM()
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
payloadAtom, err := vm.NewAtom("payload")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
senderAtom, err := vm.NewAtom("sender")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &ScriptJS{config: config, Program: programString, vm: vm, payloadAtom: payloadAtom, senderAtom: senderAtom}, nil
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
type ScriptJS struct {
|
||||
config config.ProcessorConfig
|
||||
vm *quickjs.VM
|
||||
@@ -93,47 +138,3 @@ func (sj *ScriptJS) Process(ctx context.Context, wrappedPayload common.WrappedPa
|
||||
func (sj *ScriptJS) Type() string {
|
||||
return sj.config.Type
|
||||
}
|
||||
|
||||
func init() {
|
||||
RegisterProcessor(ProcessorRegistration{
|
||||
Type: "script.js",
|
||||
Title: "Run JavaScript",
|
||||
ParamsSchema: &jsonschema.Schema{
|
||||
Type: "object",
|
||||
Properties: map[string]*jsonschema.Schema{
|
||||
"program": {
|
||||
Title: "Program",
|
||||
Type: "string",
|
||||
},
|
||||
},
|
||||
Required: []string{"program"},
|
||||
AdditionalProperties: &jsonschema.Schema{Not: &jsonschema.Schema{}},
|
||||
},
|
||||
New: func(config config.ProcessorConfig) (Processor, error) {
|
||||
params := config.Params
|
||||
|
||||
programString, err := params.GetString("program")
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("script.js program error: %w", err)
|
||||
}
|
||||
|
||||
vm, err := quickjs.NewVM()
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
payloadAtom, err := vm.NewAtom("payload")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
senderAtom, err := vm.NewAtom("sender")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &ScriptJS{config: config, Program: programString, vm: vm, payloadAtom: payloadAtom, senderAtom: senderAtom}, nil
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
@@ -12,37 +12,6 @@ import (
|
||||
"github.com/jwetzell/showbridge-go/internal/config"
|
||||
)
|
||||
|
||||
type ScriptWASM struct {
|
||||
config config.ProcessorConfig
|
||||
Program *extism.Plugin
|
||||
Function string
|
||||
}
|
||||
|
||||
func (sw *ScriptWASM) Process(ctx context.Context, wrappedPayload common.WrappedPayload) (common.WrappedPayload, error) {
|
||||
|
||||
payload := wrappedPayload.Payload
|
||||
payloadBytes, ok := common.GetAnyAsByteSlice(payload)
|
||||
|
||||
if !ok {
|
||||
wrappedPayload.End = true
|
||||
return wrappedPayload, fmt.Errorf("script.wasm can only process a byte array")
|
||||
}
|
||||
|
||||
_, output, err := sw.Program.Call(sw.Function, payloadBytes)
|
||||
|
||||
if err != nil {
|
||||
wrappedPayload.End = true
|
||||
return wrappedPayload, err
|
||||
}
|
||||
wrappedPayload.Payload = output
|
||||
|
||||
return wrappedPayload, nil
|
||||
}
|
||||
|
||||
func (sw *ScriptWASM) Type() string {
|
||||
return sw.config.Type
|
||||
}
|
||||
|
||||
func init() {
|
||||
RegisterProcessor(ProcessorRegistration{
|
||||
Type: "script.wasm",
|
||||
@@ -51,18 +20,21 @@ func init() {
|
||||
Type: "object",
|
||||
Properties: map[string]*jsonschema.Schema{
|
||||
"path": {
|
||||
Title: "Path",
|
||||
Type: "string",
|
||||
Title: "Path",
|
||||
Description: "path to the WASM plugin to load",
|
||||
Type: "string",
|
||||
},
|
||||
"function": {
|
||||
Title: "Function",
|
||||
Type: "string",
|
||||
Default: json.RawMessage(`"process"`),
|
||||
Title: "Function",
|
||||
Description: "exported function to call on the WASM plugin",
|
||||
Type: "string",
|
||||
Default: json.RawMessage(`"process"`),
|
||||
},
|
||||
"enableWasi": {
|
||||
Title: "Enable WASI",
|
||||
Type: "boolean",
|
||||
Default: json.RawMessage("false"),
|
||||
Title: "Enable WASI",
|
||||
Description: "whether to enable WASI when running the WASM plugin",
|
||||
Type: "boolean",
|
||||
Default: json.RawMessage("false"),
|
||||
},
|
||||
},
|
||||
Required: []string{"path"},
|
||||
@@ -120,3 +92,34 @@ func init() {
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
type ScriptWASM struct {
|
||||
config config.ProcessorConfig
|
||||
Program *extism.Plugin
|
||||
Function string
|
||||
}
|
||||
|
||||
func (sw *ScriptWASM) Process(ctx context.Context, wrappedPayload common.WrappedPayload) (common.WrappedPayload, error) {
|
||||
|
||||
payload := wrappedPayload.Payload
|
||||
payloadBytes, ok := common.GetAnyAsByteSlice(payload)
|
||||
|
||||
if !ok {
|
||||
wrappedPayload.End = true
|
||||
return wrappedPayload, fmt.Errorf("script.wasm can only process a byte array")
|
||||
}
|
||||
|
||||
_, output, err := sw.Program.Call(sw.Function, payloadBytes)
|
||||
|
||||
if err != nil {
|
||||
wrappedPayload.End = true
|
||||
return wrappedPayload, err
|
||||
}
|
||||
wrappedPayload.Payload = output
|
||||
|
||||
return wrappedPayload, nil
|
||||
}
|
||||
|
||||
func (sw *ScriptWASM) Type() string {
|
||||
return sw.config.Type
|
||||
}
|
||||
|
||||
@@ -11,6 +11,60 @@ import (
|
||||
"github.com/jwetzell/showbridge-go/internal/config"
|
||||
)
|
||||
|
||||
func init() {
|
||||
RegisterProcessor(ProcessorRegistration{
|
||||
Type: "sip.response.audio.create",
|
||||
Title: "Create SIP Audio Response",
|
||||
ParamsSchema: &jsonschema.Schema{
|
||||
Type: "object",
|
||||
Properties: map[string]*jsonschema.Schema{
|
||||
"preWait": {
|
||||
Title: "Pre Wait (ms)",
|
||||
Description: "number of milliseconds to wait before playing the audio",
|
||||
Type: "integer",
|
||||
},
|
||||
"audioFile": {
|
||||
Title: "Audio File",
|
||||
Description: "path to the audio file to play",
|
||||
Type: "string",
|
||||
},
|
||||
"postWait": {
|
||||
Title: "Post Wait (ms)",
|
||||
Description: "number of milliseconds to wait after playing the audio",
|
||||
Type: "integer",
|
||||
},
|
||||
},
|
||||
Required: []string{"preWait", "postWait", "audioFile"},
|
||||
AdditionalProperties: &jsonschema.Schema{Not: &jsonschema.Schema{}},
|
||||
},
|
||||
New: func(config config.ProcessorConfig) (Processor, error) {
|
||||
params := config.Params
|
||||
|
||||
preWaitNum, err := params.GetInt("preWait")
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("sip.response.audio.create preWait error: %w", err)
|
||||
}
|
||||
|
||||
postWaitNum, err := params.GetInt("postWait")
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("sip.response.audio.create postWait error: %w", err)
|
||||
}
|
||||
|
||||
audioFileString, err := params.GetString("audioFile")
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("sip.response.audio.create audioFile error: %w", err)
|
||||
}
|
||||
|
||||
audioFileTemplate, err := template.New("audioFile").Parse(audioFileString)
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &SipResponseAudioCreate{config: config, AudioFile: audioFileTemplate, PreWait: int(preWaitNum), PostWait: int(postWaitNum)}, nil
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
type SipResponseAudioCreate struct {
|
||||
config config.ProcessorConfig
|
||||
PreWait int
|
||||
@@ -49,53 +103,3 @@ func (srac *SipResponseAudioCreate) Process(ctx context.Context, wrappedPayload
|
||||
func (srac *SipResponseAudioCreate) Type() string {
|
||||
return srac.config.Type
|
||||
}
|
||||
|
||||
func init() {
|
||||
RegisterProcessor(ProcessorRegistration{
|
||||
Type: "sip.response.audio.create",
|
||||
Title: "Create SIP Audio Response",
|
||||
ParamsSchema: &jsonschema.Schema{
|
||||
Type: "object",
|
||||
Properties: map[string]*jsonschema.Schema{
|
||||
"preWait": {
|
||||
Title: "Pre Wait (ms)",
|
||||
Type: "integer",
|
||||
},
|
||||
"postWait": {
|
||||
Title: "Post Wait (ms)",
|
||||
Type: "integer",
|
||||
},
|
||||
"audioFile": {
|
||||
Type: "string",
|
||||
},
|
||||
},
|
||||
Required: []string{"preWait", "postWait", "audioFile"},
|
||||
AdditionalProperties: &jsonschema.Schema{Not: &jsonschema.Schema{}},
|
||||
},
|
||||
New: func(config config.ProcessorConfig) (Processor, error) {
|
||||
params := config.Params
|
||||
|
||||
preWaitNum, err := params.GetInt("preWait")
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("sip.response.audio.create preWait error: %w", err)
|
||||
}
|
||||
|
||||
postWaitNum, err := params.GetInt("postWait")
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("sip.response.audio.create postWait error: %w", err)
|
||||
}
|
||||
|
||||
audioFileString, err := params.GetString("audioFile")
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("sip.response.audio.create audioFile error: %w", err)
|
||||
}
|
||||
|
||||
audioFileTemplate, err := template.New("audioFile").Parse(audioFileString)
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &SipResponseAudioCreate{config: config, AudioFile: audioFileTemplate, PreWait: int(preWaitNum), PostWait: int(postWaitNum)}, nil
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user