mirror of
https://github.com/jwetzell/showbridge-go.git
synced 2026-04-30 14:55:28 +00:00
Compare commits
106 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
e6099b6acd | ||
|
|
b93d754c19 | ||
|
|
710dcf3a02 | ||
|
|
3ef41d0026 | ||
|
|
8dcb70bfee | ||
|
|
59f00c1a32 | ||
|
|
6e88d259b8 | ||
|
|
bb33974e1c | ||
|
|
bd2a68ff6e | ||
|
|
7e2d76ef3a | ||
|
|
d1cec1e094 | ||
|
|
477d70fad0 | ||
|
|
70f4636522 | ||
|
|
0248ca6973 | ||
|
|
4aa586427b | ||
|
|
6d3cf6692f | ||
|
|
a263b10690 | ||
|
|
3ce2909b0f | ||
|
|
b15e282d59 | ||
|
|
f97f9b9fc9 | ||
|
|
12de947f3d | ||
|
|
7335ba973a | ||
|
|
a994286402 | ||
|
|
b639a5c786 | ||
|
|
d170958d9b | ||
|
|
ed4c14e82b | ||
|
|
fcb1378784 | ||
|
|
97f4cbeace | ||
|
|
8ffc7d02a5 | ||
|
|
3458b52206 | ||
|
|
e12b6e098e | ||
|
|
58cb7766fe | ||
|
|
407f1f3618 | ||
|
|
3c0f555a6f | ||
|
|
6c6e50e0eb | ||
|
|
0ed4d6669f | ||
|
|
cc37559bde | ||
|
|
18b307bca1 | ||
|
|
17106b5096 | ||
|
|
df9ffdda9e | ||
|
|
479cf41ea0 | ||
|
|
af833cb212 | ||
|
|
a6a8e275ee | ||
|
|
6968539c59 | ||
|
|
e96913230b | ||
|
|
f1dff33704 | ||
|
|
ff426994e4 | ||
|
|
a1275b3b69 | ||
|
|
227d042feb | ||
|
|
e53515267e | ||
|
|
502586dec6 | ||
|
|
cad3714664 | ||
|
|
cf6519b594 | ||
|
|
7dcac9470a | ||
|
|
e5268f42f8 | ||
|
|
ccff105e37 | ||
|
|
6611821155 | ||
|
|
7cde1c244a | ||
|
|
70878e6df6 | ||
|
|
bd005da358 | ||
|
|
25f2ec30c2 | ||
|
|
e679bf2b46 | ||
|
|
460d2f051d | ||
|
|
efdcbae5c4 | ||
|
|
076a13f48a | ||
|
|
971eea6e41 | ||
|
|
1172159455 | ||
|
|
ecb415f321 | ||
|
|
b552672011 | ||
|
|
168265f0a9 | ||
|
|
caee7b269c | ||
|
|
b0cc47236f | ||
|
|
f6c2b1d9ac | ||
|
|
f977b845be | ||
|
|
0166383978 | ||
|
|
66cfaa3091 | ||
|
|
0904d2fcb8 | ||
|
|
6a13c38e77 | ||
|
|
1a8ccfc64a | ||
|
|
2e3bb408c3 | ||
|
|
3d75165a61 | ||
|
|
50f755f914 | ||
|
|
b10e296d0a | ||
|
|
91c44420cb | ||
|
|
c1b2fa714e | ||
|
|
ff1949ce69 | ||
|
|
76583b1fb8 | ||
|
|
e6433878b5 | ||
|
|
3ad067cbb8 | ||
|
|
30b406d601 | ||
|
|
7315d40015 | ||
|
|
e7105b8b39 | ||
|
|
ec40194ecb | ||
|
|
85d8dc5787 | ||
|
|
b27bfb1a6d | ||
|
|
fb9c0bc657 | ||
|
|
d32d23041b | ||
|
|
b4149df00a | ||
|
|
61bd4b64f5 | ||
|
|
38d73881c9 | ||
|
|
3138bdfcdb | ||
|
|
ba862300b2 | ||
|
|
e414bf336e | ||
|
|
da95fb5c75 | ||
|
|
1fdc30aa9e | ||
|
|
128920dff9 |
5
.dockerignore
Normal file
5
.dockerignore
Normal file
@@ -0,0 +1,5 @@
|
||||
Dockerfile
|
||||
.github
|
||||
.vscode
|
||||
README.md
|
||||
LICENSE
|
||||
39
.github/workflows/release-showbridge.yaml
vendored
39
.github/workflows/release-showbridge.yaml
vendored
@@ -3,7 +3,7 @@ name: showbridge release
|
||||
on:
|
||||
push:
|
||||
tags:
|
||||
- "*"
|
||||
- "v*"
|
||||
|
||||
permissions:
|
||||
contents: write
|
||||
@@ -29,3 +29,40 @@ jobs:
|
||||
args: release --clean
|
||||
env:
|
||||
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
||||
docker:
|
||||
runs-on: ubuntu-latest
|
||||
needs: goreleaser
|
||||
steps:
|
||||
- name: Check out repository
|
||||
uses: actions/checkout@1af3b93b6815bc44a9784bd300feb67ff0d1eeb3 # v6.0.0
|
||||
|
||||
- name: Set up QEMU
|
||||
uses: docker/setup-qemu-action@c7c53464625b32c7a7e944ae62b3e17d2b600130 # v3.7.0
|
||||
|
||||
- name: Set up Docker Buildx
|
||||
uses: docker/setup-buildx-action@e468171a9de216ec08956ac3ada2f0791b6bd435 # v3.11.1
|
||||
|
||||
- name: Login to Docker Hub
|
||||
uses: docker/login-action@5e57cd118135c172c3672efd75eb46360885c0ef # v3.6.0
|
||||
with:
|
||||
username: ${{ secrets.DOCKERHUB_USERNAME }}
|
||||
password: ${{ secrets.DOCKERHUB_TOKEN }}
|
||||
|
||||
- name: Setup Docker metadata
|
||||
id: meta
|
||||
uses: docker/metadata-action@c299e40c65443455700f0fdfc63efafe5b349051 # v5.10.0
|
||||
with:
|
||||
images: |
|
||||
jwetzell/showbridge
|
||||
|
||||
- name: Build and push
|
||||
uses: docker/build-push-action@263435318d21b8e681c14492fe198d362a7d2c83 # v6.18.0
|
||||
with:
|
||||
push: true
|
||||
context: ./
|
||||
file: ./Dockerfile
|
||||
tags: ${{ steps.meta.outputs.tags }}
|
||||
labels: ${{ steps.meta.outputs.labels }}
|
||||
cache-from: type=gha
|
||||
cache-to: type=gha,mode=max
|
||||
platforms: linux/amd64,linux/arm64,linux/arm/v7
|
||||
|
||||
4
.vscode/launch.json
vendored
4
.vscode/launch.json
vendored
@@ -10,7 +10,9 @@
|
||||
"type": "go",
|
||||
"request": "launch",
|
||||
"mode": "auto",
|
||||
"program": "cmd/showbridge"
|
||||
"program": "cmd/showbridge",
|
||||
"args": ["--log-level", "debug"],
|
||||
"cwd": "./"
|
||||
}
|
||||
]
|
||||
}
|
||||
15
Dockerfile
Normal file
15
Dockerfile
Normal file
@@ -0,0 +1,15 @@
|
||||
ARG GO_VERSION=1.25.5
|
||||
FROM golang:${GO_VERSION}-alpine AS build
|
||||
RUN apk --no-cache add ca-certificates tzdata
|
||||
WORKDIR /build
|
||||
COPY go.mod go.sum ./
|
||||
RUN go mod download
|
||||
COPY . .
|
||||
RUN CGO_ENABLED=0 GOOS=linux go build ./cmd/showbridge
|
||||
|
||||
FROM scratch
|
||||
WORKDIR /app
|
||||
COPY --from=build /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/ca-certificates.crt
|
||||
COPY --from=build /usr/share/zoneinfo /usr/share/zoneinfo
|
||||
COPY --from=build /build/showbridge /app/showbridge
|
||||
ENTRYPOINT [ "/app/showbridge" ]
|
||||
35
README.md
35
README.md
@@ -6,28 +6,37 @@ Simple protocol router _/s_
|
||||
|
||||
</div>
|
||||
|
||||
<p align="center">
|
||||
<a href="https://github.com/jwetzell/showbridge-go/releases">Releases</a> ·
|
||||
<a href="https://docs.showbridge.io">Documentation</a>
|
||||
</p>
|
||||
|
||||
### Supported Protocols
|
||||
- HTTP
|
||||
- client
|
||||
- server
|
||||
- UDP
|
||||
- client
|
||||
- server
|
||||
- TCP
|
||||
- client
|
||||
- server
|
||||
- [MQTT](https://mqtt.org/)
|
||||
- client
|
||||
- [NATS](https://nats.io/)
|
||||
- client
|
||||
- [PosiStageNet](https://posistage.net/)
|
||||
- client
|
||||
- MIDI
|
||||
- client (not included in pre-built binaries yet)
|
||||
- MIDI (not included in pre-built binaries yet)
|
||||
- Serial (not included in pre-built binaries yet)
|
||||
- [OSC](https://opensoundcontrol.stanford.edu/spec-1_0.html)
|
||||
- [FreeD](https://ptzoptics.com/freed/)
|
||||
- [SIP](https://en.wikipedia.org/wiki/Session_Initiation_Protocol)
|
||||
- call server
|
||||
- [DTMF](https://en.wikipedia.org/wiki/Dual-tone_multi-frequency_signaling) server
|
||||
|
||||
### CLI Usage
|
||||
|
||||
```
|
||||
NAME:
|
||||
showbridge - Simple protocol router /s
|
||||
|
||||
USAGE:
|
||||
showbridge [global options]
|
||||
|
||||
GLOBAL OPTIONS:
|
||||
--config string path to config file (default: "./config.yaml")
|
||||
--log-level string set log level (default: "info")
|
||||
--log-format string log format to use (default: "text")
|
||||
--help, -h show help
|
||||
--version, -v print the version
|
||||
```
|
||||
|
||||
@@ -2,10 +2,13 @@ package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"os"
|
||||
"os/signal"
|
||||
"slices"
|
||||
"sync"
|
||||
|
||||
"github.com/jwetzell/showbridge-go"
|
||||
"github.com/jwetzell/showbridge-go/internal/config"
|
||||
@@ -20,37 +23,43 @@ var (
|
||||
func main() {
|
||||
cmd := &cli.Command{
|
||||
Name: "showbridge",
|
||||
Usage: "Simple protocol router /s",
|
||||
Version: version,
|
||||
Flags: []cli.Flag{
|
||||
&cli.StringFlag{
|
||||
Name: "config",
|
||||
Value: "./config.yaml",
|
||||
Usage: "path to config file",
|
||||
},
|
||||
},
|
||||
Action: func(ctx context.Context, c *cli.Command) error {
|
||||
configPath := c.String("config")
|
||||
if configPath == "" {
|
||||
return fmt.Errorf("config value cannot be empty")
|
||||
&cli.StringFlag{
|
||||
Name: "log-level",
|
||||
Value: "info",
|
||||
Usage: "set log level",
|
||||
Validator: func(level string) error {
|
||||
levels := []string{"debug", "info", "warn", "error"}
|
||||
if !slices.Contains(levels, level) {
|
||||
return fmt.Errorf("unknown log level: %s", level)
|
||||
}
|
||||
|
||||
config, err := readConfig(configPath)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
router, moduleErrors, routeErrors := showbridge.NewRouter(ctx, config)
|
||||
for _, moduleError := range moduleErrors {
|
||||
slog.Error("problem initializing module", "index", moduleError.Index, "error", moduleError.Error)
|
||||
}
|
||||
|
||||
for _, routeError := range routeErrors {
|
||||
slog.Error("problem initializing route", "index", routeError.Index, "error", routeError.Error)
|
||||
}
|
||||
router.Run()
|
||||
return nil
|
||||
},
|
||||
},
|
||||
&cli.StringFlag{
|
||||
Name: "log-format",
|
||||
Value: "text",
|
||||
Usage: "log format to use",
|
||||
Validator: func(format string) error {
|
||||
formats := []string{"text", "json"}
|
||||
if !slices.Contains(formats, format) {
|
||||
return fmt.Errorf("unknown log format: %s", format)
|
||||
}
|
||||
return nil
|
||||
},
|
||||
},
|
||||
},
|
||||
Action: run,
|
||||
}
|
||||
|
||||
ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt, os.Interrupt)
|
||||
ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt)
|
||||
defer cancel()
|
||||
err := cmd.Run(ctx, os.Args)
|
||||
|
||||
@@ -76,3 +85,78 @@ func readConfig(configPath string) (config.Config, error) {
|
||||
|
||||
return cfg, nil
|
||||
}
|
||||
|
||||
func run(ctx context.Context, c *cli.Command) error {
|
||||
configPath := c.String("config")
|
||||
if configPath == "" {
|
||||
return errors.New("config value cannot be empty")
|
||||
}
|
||||
|
||||
config, err := readConfig(configPath)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
logLevel := slog.LevelInfo
|
||||
|
||||
logLevelFromFlag := c.String("log-level")
|
||||
|
||||
switch logLevelFromFlag {
|
||||
case "debug":
|
||||
logLevel = slog.LevelDebug
|
||||
case "info":
|
||||
logLevel = slog.LevelInfo
|
||||
case "warn":
|
||||
logLevel = slog.LevelWarn
|
||||
case "error":
|
||||
logLevel = slog.LevelError
|
||||
default:
|
||||
logLevel = slog.LevelInfo
|
||||
}
|
||||
|
||||
logHandlerOptions := &slog.HandlerOptions{
|
||||
Level: logLevel,
|
||||
}
|
||||
|
||||
logOutput := os.Stderr
|
||||
|
||||
var logHandler slog.Handler
|
||||
|
||||
logFormat := c.String("log-format")
|
||||
|
||||
switch logFormat {
|
||||
case "json":
|
||||
logHandler = slog.NewJSONHandler(logOutput, logHandlerOptions)
|
||||
case "text":
|
||||
logHandler = slog.NewTextHandler(logOutput, logHandlerOptions)
|
||||
default:
|
||||
logHandler = slog.NewTextHandler(logOutput, logHandlerOptions)
|
||||
}
|
||||
|
||||
slog.SetDefault(slog.New(logHandler))
|
||||
|
||||
commandLogger := slog.Default().With("component", "cmd")
|
||||
|
||||
router, moduleErrors, routeErrors := showbridge.NewRouter(context.Background(), config)
|
||||
|
||||
for _, moduleError := range moduleErrors {
|
||||
commandLogger.Error("problem initializing module", "index", moduleError.Index, "error", moduleError.Error)
|
||||
}
|
||||
|
||||
for _, routeError := range routeErrors {
|
||||
commandLogger.Error("problem initializing route", "index", routeError.Index, "error", routeError.Error)
|
||||
}
|
||||
|
||||
routerRunner := sync.WaitGroup{}
|
||||
|
||||
routerRunner.Go(func() {
|
||||
router.Run()
|
||||
})
|
||||
|
||||
<-ctx.Done()
|
||||
commandLogger.Debug("shutting down router")
|
||||
router.Stop()
|
||||
commandLogger.Debug("waiting for router to exit")
|
||||
routerRunner.Wait()
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
modules:
|
||||
- id: http
|
||||
type: net.http.server
|
||||
type: http.server
|
||||
params:
|
||||
port: 3000
|
||||
- id: udp
|
||||
|
||||
9
go.mod
9
go.mod
@@ -1,16 +1,17 @@
|
||||
module github.com/jwetzell/showbridge-go
|
||||
|
||||
go 1.25.3
|
||||
go 1.25.5
|
||||
|
||||
require (
|
||||
github.com/eclipse/paho.mqtt.golang v1.5.1
|
||||
github.com/emiago/diago v0.23.1-0.20251211215055-e1d875617111
|
||||
github.com/emiago/sipgo v1.0.1-alpha.0.20251212165843-9c9bcdf9126f
|
||||
github.com/expr-lang/expr v1.17.6
|
||||
github.com/emiago/sipgo v1.0.1
|
||||
github.com/expr-lang/expr v1.17.7
|
||||
github.com/jwetzell/artnet-go v0.0.0-20251223201031-5097901aa9db
|
||||
github.com/jwetzell/free-d-go v0.1.0
|
||||
github.com/jwetzell/osc-go v0.1.0
|
||||
github.com/jwetzell/psn-go v0.3.0
|
||||
github.com/nats-io/nats.go v1.47.0
|
||||
github.com/nats-io/nats.go v1.48.0
|
||||
github.com/urfave/cli/v3 v3.6.1
|
||||
gitlab.com/gomidi/midi/v2 v2.3.18
|
||||
go.bug.st/serial v1.6.4
|
||||
|
||||
14
go.sum
14
go.sum
@@ -8,10 +8,10 @@ github.com/eclipse/paho.mqtt.golang v1.5.1 h1:/VSOv3oDLlpqR2Epjn1Q7b2bSTplJIeV2I
|
||||
github.com/eclipse/paho.mqtt.golang v1.5.1/go.mod h1:1/yJCneuyOoCOzKSsOTUc0AJfpsItBGWvYpBLimhArU=
|
||||
github.com/emiago/diago v0.23.1-0.20251211215055-e1d875617111 h1:jqhOZbH40pf2jiUhGaBNk334wOtNYvAaXg/mHOXhy/Y=
|
||||
github.com/emiago/diago v0.23.1-0.20251211215055-e1d875617111/go.mod h1:3vLCCq8/G/Ei5I64IHtrmBTag+nPLcgXcKeN1KkLtuc=
|
||||
github.com/emiago/sipgo v1.0.1-alpha.0.20251212165843-9c9bcdf9126f h1:n3wpx7ZyJBr0popXyATnTUZp/AdLIHjKbnKzr4ruF7g=
|
||||
github.com/emiago/sipgo v1.0.1-alpha.0.20251212165843-9c9bcdf9126f/go.mod h1:DuwAxBZhKMqIzQFPGZb1MVAGU6Wuxj64oTOhd5dx/FY=
|
||||
github.com/expr-lang/expr v1.17.6 h1:1h6i8ONk9cexhDmowO/A64VPxHScu7qfSl2k8OlINec=
|
||||
github.com/expr-lang/expr v1.17.6/go.mod h1:8/vRC7+7HBzESEqt5kKpYXxrxkr31SaO8r40VO/1IT4=
|
||||
github.com/emiago/sipgo v1.0.1 h1:8eCZ6L/VX3isyByyv1RrBoQ5GyBoRXBHkNMYjwacRfk=
|
||||
github.com/emiago/sipgo v1.0.1/go.mod h1:DuwAxBZhKMqIzQFPGZb1MVAGU6Wuxj64oTOhd5dx/FY=
|
||||
github.com/expr-lang/expr v1.17.7 h1:Q0xY/e/2aCIp8g9s/LGvMDCC5PxYlvHgDZRQ4y16JX8=
|
||||
github.com/expr-lang/expr v1.17.7/go.mod h1:8/vRC7+7HBzESEqt5kKpYXxrxkr31SaO8r40VO/1IT4=
|
||||
github.com/go-audio/riff v1.0.0 h1:d8iCGbDvox9BfLagY94fBynxSPHO80LmZCaOsmKxokA=
|
||||
github.com/go-audio/riff v1.0.0/go.mod h1:l3cQwc85y79NQFCRB7TiPoNiaijp6q8Z0Uv38rVG498=
|
||||
github.com/gobwas/httphead v0.1.0 h1:exrUm0f4YX0L7EBwZHuCF4GDp8aJfVeBrlLQrs6NqWU=
|
||||
@@ -30,6 +30,8 @@ github.com/hashicorp/golang-lru/v2 v2.0.7 h1:a+bsQ5rvGLjzHuww6tVxozPZFVghXaHOwFs
|
||||
github.com/hashicorp/golang-lru/v2 v2.0.7/go.mod h1:QeFd9opnmA6QUJc5vARoKUSoFhyfM2/ZepoAG6RGpeM=
|
||||
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.0.0-20251223201031-5097901aa9db h1:PR9sdDEWBufi8/3yiQHRXncLNKUHBMKiEBDtdNWsK/E=
|
||||
github.com/jwetzell/artnet-go v0.0.0-20251223201031-5097901aa9db/go.mod h1:gli97Z32a0kMkZ6taoTiK7/lqHcF/dhiGjGJdx/PxqA=
|
||||
github.com/jwetzell/free-d-go v0.1.0 h1:xHt6dvyit98X+OC3jVzV0aLidxbyzi3vI9QiYkteEtA=
|
||||
github.com/jwetzell/free-d-go v0.1.0/go.mod h1:KmrkooRARRaxJTBSPvwt/6IMAIaHH1R8bSA8cwbbELw=
|
||||
github.com/jwetzell/osc-go v0.1.0 h1:EXxup5VWBErHot2Ri4MFToPf6KCzLDTbCt2x6GLfw8I=
|
||||
@@ -44,8 +46,8 @@ github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
|
||||
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
|
||||
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
|
||||
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
|
||||
github.com/nats-io/nats.go v1.47.0 h1:YQdADw6J/UfGUd2Oy6tn4Hq6YHxCaJrVKayxxFqYrgM=
|
||||
github.com/nats-io/nats.go v1.47.0/go.mod h1:iRWIPokVIFbVijxuMQq4y9ttaBTMe0SFdlZfMDd+33g=
|
||||
github.com/nats-io/nats.go v1.48.0 h1:pSFyXApG+yWU/TgbKCjmm5K4wrHu86231/w84qRVR+U=
|
||||
github.com/nats-io/nats.go v1.48.0/go.mod h1:iRWIPokVIFbVijxuMQq4y9ttaBTMe0SFdlZfMDd+33g=
|
||||
github.com/nats-io/nkeys v0.4.11 h1:q44qGV008kYd9W1b1nEBkNzvnWxtRSQ7A8BoqRrcfa0=
|
||||
github.com/nats-io/nkeys v0.4.11/go.mod h1:szDimtgmfOi9n25JpfIdGw12tZFYXqhGxjhVxsatHVE=
|
||||
github.com/nats-io/nuid v1.0.1 h1:5iA8DT8V7q8WK2EScv2padNa/rTESc1KdnPw4TC2paw=
|
||||
|
||||
@@ -1,9 +1,5 @@
|
||||
package framer
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
)
|
||||
|
||||
type Framer interface {
|
||||
Decode([]byte) [][]byte
|
||||
Encode([]byte) []byte
|
||||
@@ -11,19 +7,19 @@ type Framer interface {
|
||||
Buffer() []byte
|
||||
}
|
||||
|
||||
func GetFramer(framingType string) (Framer, error) {
|
||||
func GetFramer(framingType string) Framer {
|
||||
switch framingType {
|
||||
case "CR":
|
||||
return NewByteSeparatorFramer([]byte{'\r'}), nil
|
||||
return NewByteSeparatorFramer([]byte{'\r'})
|
||||
case "LF":
|
||||
return NewByteSeparatorFramer([]byte{'\n'}), nil
|
||||
return NewByteSeparatorFramer([]byte{'\n'})
|
||||
case "CRLF":
|
||||
return NewByteSeparatorFramer([]byte{'\r', '\n'}), nil
|
||||
return NewByteSeparatorFramer([]byte{'\r', '\n'})
|
||||
case "SLIP":
|
||||
return NewSlipFramer(), nil
|
||||
return NewSlipFramer()
|
||||
case "RAW":
|
||||
return NewRawFramer(), nil
|
||||
return NewRawFramer()
|
||||
default:
|
||||
return nil, fmt.Errorf("unknown framing method: %s", framingType)
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
15
internal/framer/framer_test.go
Normal file
15
internal/framer/framer_test.go
Normal file
@@ -0,0 +1,15 @@
|
||||
package framer_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/jwetzell/showbridge-go/internal/framer"
|
||||
)
|
||||
|
||||
func TestNilGetFramer(t *testing.T) {
|
||||
nilFramer := framer.GetFramer("asldfiudchuehrkbjbkjrbb")
|
||||
|
||||
if nilFramer != nil {
|
||||
t.Fatalf("Expected nil framer, got %v", nilFramer)
|
||||
}
|
||||
}
|
||||
78
internal/framer/raw_test.go
Normal file
78
internal/framer/raw_test.go
Normal file
@@ -0,0 +1,78 @@
|
||||
package framer_test
|
||||
|
||||
import (
|
||||
"slices"
|
||||
"testing"
|
||||
|
||||
"github.com/jwetzell/showbridge-go/internal/framer"
|
||||
)
|
||||
|
||||
func TestGoodRawFramerDecode(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
framer framer.Framer
|
||||
input []byte
|
||||
expected [][]byte
|
||||
}{
|
||||
{
|
||||
name: "basic raw framer",
|
||||
framer: framer.GetFramer("RAW"),
|
||||
input: []byte("Hello\nWorld\nThis is a test\n"),
|
||||
expected: [][]byte{
|
||||
[]byte("Hello\nWorld\nThis is a test\n"),
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
frames := test.framer.Decode(test.input)
|
||||
if len(frames) != len(test.expected) {
|
||||
t.Fatalf("raw framer got %d frames, expected %d", len(frames), len(test.expected))
|
||||
}
|
||||
for i, frame := range frames {
|
||||
if !slices.Equal(frame, test.expected[i]) {
|
||||
t.Errorf("raw framer frame %d got %s, expected %s", i, frame, test.expected[i])
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestGoodRawFramerEncode(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
framer framer.Framer
|
||||
expected []byte
|
||||
input []byte
|
||||
}{
|
||||
{
|
||||
name: "basic raw framer",
|
||||
framer: framer.GetFramer("RAW"),
|
||||
expected: []byte("Hello\nWorld\nThis is a test\n"),
|
||||
input: []byte("Hello\nWorld\nThis is a test\n"),
|
||||
},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
frame := test.framer.Encode(test.input)
|
||||
if !slices.Equal(frame, test.expected) {
|
||||
t.Fatalf("raw frame encode got %s, expected %s", frame, test.expected)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestRawFramerBuffer(t *testing.T) {
|
||||
framer := framer.GetFramer("RAW")
|
||||
framer.Decode([]byte("Hello, World!"))
|
||||
|
||||
if !slices.Equal(framer.Buffer(), []byte{}) {
|
||||
t.Fatalf("raw framer buffer got %s, expected empty", framer.Buffer())
|
||||
}
|
||||
framer.Clear()
|
||||
if !slices.Equal(framer.Buffer(), []byte{}) {
|
||||
t.Fatalf("raw framer buffer got %s, expected empty after clear", framer.Buffer())
|
||||
}
|
||||
}
|
||||
@@ -7,7 +7,7 @@ import (
|
||||
"github.com/jwetzell/showbridge-go/internal/framer"
|
||||
)
|
||||
|
||||
func TestGoodSeparatorFramer(t *testing.T) {
|
||||
func TestGoodSeparatorFramerDecode(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
framer framer.Framer
|
||||
@@ -17,7 +17,7 @@ func TestGoodSeparatorFramer(t *testing.T) {
|
||||
}{
|
||||
{
|
||||
name: "new line separator",
|
||||
framer: framer.NewByteSeparatorFramer([]byte{0x0a}),
|
||||
framer: framer.GetFramer("LF"),
|
||||
input: []byte("Hello\nWorld\nThis is a test\n"),
|
||||
expected: [][]byte{
|
||||
[]byte("Hello"),
|
||||
@@ -26,9 +26,20 @@ func TestGoodSeparatorFramer(t *testing.T) {
|
||||
},
|
||||
buffer: []byte{},
|
||||
},
|
||||
{
|
||||
name: "CR separator",
|
||||
framer: framer.GetFramer("CR"),
|
||||
input: []byte("Hello\rWorld\rThis is a test\r"),
|
||||
expected: [][]byte{
|
||||
[]byte("Hello"),
|
||||
[]byte("World"),
|
||||
[]byte("This is a test"),
|
||||
},
|
||||
buffer: []byte{},
|
||||
},
|
||||
{
|
||||
name: "CRLF separator",
|
||||
framer: framer.NewByteSeparatorFramer([]byte{0x0d, 0x0a}),
|
||||
framer: framer.GetFramer("CRLF"),
|
||||
input: []byte("Hello\r\nWorld\r\nThis is a test\r\n"),
|
||||
expected: [][]byte{
|
||||
[]byte("Hello"),
|
||||
@@ -39,7 +50,7 @@ func TestGoodSeparatorFramer(t *testing.T) {
|
||||
},
|
||||
{
|
||||
name: "extra data after separator",
|
||||
framer: framer.NewByteSeparatorFramer([]byte{0x0d, 0x0a}),
|
||||
framer: framer.GetFramer("CRLF"),
|
||||
input: []byte("Hello\r\nWorld\r\nThis is a test\r\nextra"),
|
||||
expected: [][]byte{
|
||||
[]byte("Hello"),
|
||||
@@ -54,7 +65,7 @@ func TestGoodSeparatorFramer(t *testing.T) {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
frames := test.framer.Decode(test.input)
|
||||
if len(frames) != len(test.expected) {
|
||||
t.Errorf("separator framer got %d frames, expected %d", len(frames), len(test.expected))
|
||||
t.Fatalf("separator framer got %d frames, expected %d", len(frames), len(test.expected))
|
||||
}
|
||||
for i, frame := range frames {
|
||||
if !slices.Equal(frame, test.expected[i]) {
|
||||
@@ -62,8 +73,58 @@ func TestGoodSeparatorFramer(t *testing.T) {
|
||||
}
|
||||
}
|
||||
if !slices.Equal(test.framer.Buffer(), test.buffer) {
|
||||
t.Errorf("separator framer buffer got %s, expected %s", test.framer.Buffer(), test.buffer)
|
||||
t.Fatalf("separator framer buffer got %s, expected %s", test.framer.Buffer(), test.buffer)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestGoodSeparatorFramerEncode(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
framer framer.Framer
|
||||
input []byte
|
||||
expected []byte
|
||||
}{
|
||||
{
|
||||
name: "new line separator",
|
||||
framer: framer.GetFramer("LF"),
|
||||
input: []byte("Hello"),
|
||||
expected: []byte("Hello\n"),
|
||||
},
|
||||
{
|
||||
name: "CR separator",
|
||||
framer: framer.GetFramer("CR"),
|
||||
input: []byte("Hello"),
|
||||
expected: []byte("Hello\r"),
|
||||
},
|
||||
{
|
||||
name: "CRLF separator",
|
||||
framer: framer.GetFramer("CRLF"),
|
||||
input: []byte("Hello"),
|
||||
expected: []byte("Hello\r\n"),
|
||||
},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
frame := test.framer.Encode(test.input)
|
||||
if !slices.Equal(frame, test.expected) {
|
||||
t.Fatalf("separator framer got %s, expected %s", frame, test.expected)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestSeparatorFrameBuffer(t *testing.T) {
|
||||
framer := framer.GetFramer("LF")
|
||||
framer.Decode([]byte("Hello\nWorld\nThis is a test\nextra"))
|
||||
if !slices.Equal(framer.Buffer(), []byte("extra")) {
|
||||
t.Fatalf("separator framer buffer got %s, expected %s", framer.Buffer(), []byte("extra"))
|
||||
}
|
||||
|
||||
framer.Clear()
|
||||
if !slices.Equal(framer.Buffer(), []byte{}) {
|
||||
t.Fatalf("separator framer buffer got %s, expected empty slice", framer.Buffer())
|
||||
}
|
||||
}
|
||||
|
||||
@@ -59,9 +59,9 @@ func (sf *SlipFramer) Encode(data []byte) []byte {
|
||||
for _, byteToEncode := range data {
|
||||
switch byteToEncode {
|
||||
case END:
|
||||
encodedBytes = append(encodedBytes, ESC_END)
|
||||
encodedBytes = append(encodedBytes, ESC, ESC_END)
|
||||
case ESC:
|
||||
encodedBytes = append(encodedBytes, ESC_ESC)
|
||||
encodedBytes = append(encodedBytes, ESC, ESC_ESC)
|
||||
default:
|
||||
encodedBytes = append(encodedBytes, byteToEncode)
|
||||
}
|
||||
|
||||
@@ -7,7 +7,7 @@ import (
|
||||
"github.com/jwetzell/showbridge-go/internal/framer"
|
||||
)
|
||||
|
||||
func TestGoodSLIPFramer(t *testing.T) {
|
||||
func TestGoodSLIPFramerDecode(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
framer framer.Framer
|
||||
@@ -17,20 +17,34 @@ func TestGoodSLIPFramer(t *testing.T) {
|
||||
}{
|
||||
{
|
||||
name: "OSC SLIP messages",
|
||||
framer: framer.NewSlipFramer(),
|
||||
framer: framer.GetFramer("SLIP"),
|
||||
input: []byte{0xc0, 0x2f, 0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x00, 0x00, 0x2c, 0x00, 0x00, 0x00, 0xc0},
|
||||
expected: [][]byte{
|
||||
{0x2f, 0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x00, 0x00, 0x2c, 0x00, 0x00, 0x00},
|
||||
},
|
||||
buffer: []byte{},
|
||||
},
|
||||
{
|
||||
name: "SLIP decode escaped end",
|
||||
framer: framer.GetFramer("SLIP"),
|
||||
expected: [][]byte{{0xc0}},
|
||||
input: []byte{0xc0, 0xdb, 0xdc, 0xc0},
|
||||
buffer: []byte{},
|
||||
},
|
||||
{
|
||||
name: "SLIP decode escaped escape",
|
||||
framer: framer.GetFramer("SLIP"),
|
||||
expected: [][]byte{{0xdb}},
|
||||
input: []byte{0xc0, 0xdb, 0xdd, 0xc0},
|
||||
buffer: []byte{},
|
||||
},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
frames := test.framer.Decode(test.input)
|
||||
if len(frames) != len(test.expected) {
|
||||
t.Errorf("SLIP framer got %d frames, expected %d", len(frames), len(test.expected))
|
||||
t.Fatalf("SLIP framer got %d frames, expected %d", len(frames), len(test.expected))
|
||||
}
|
||||
for i, frame := range frames {
|
||||
if !slices.Equal(frame, test.expected[i]) {
|
||||
@@ -38,8 +52,59 @@ func TestGoodSLIPFramer(t *testing.T) {
|
||||
}
|
||||
}
|
||||
if !slices.Equal(test.framer.Buffer(), test.buffer) {
|
||||
t.Errorf("SLIP framer buffer got %s, expected %s", test.framer.Buffer(), test.buffer)
|
||||
t.Fatalf("SLIP framer buffer got %s, expected %s", test.framer.Buffer(), test.buffer)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestGoodSLIPFramerEncode(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
framer framer.Framer
|
||||
input []byte
|
||||
expected []byte
|
||||
}{
|
||||
{
|
||||
name: "OSC SLIP messages",
|
||||
framer: framer.GetFramer("SLIP"),
|
||||
input: []byte{
|
||||
0x2f, 0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x00, 0x00, 0x2c, 0x00, 0x00, 0x00,
|
||||
},
|
||||
expected: []byte{0xc0, 0x2f, 0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x00, 0x00, 0x2c, 0x00, 0x00, 0x00, 0xc0},
|
||||
},
|
||||
{
|
||||
name: "SLIP encode end",
|
||||
framer: framer.GetFramer("SLIP"),
|
||||
input: []byte{0xc0},
|
||||
expected: []byte{0xc0, 0xdb, 0xdc, 0xc0},
|
||||
},
|
||||
{
|
||||
name: "SLIP encode esc",
|
||||
framer: framer.GetFramer("SLIP"),
|
||||
input: []byte{0xdb},
|
||||
expected: []byte{0xc0, 0xdb, 0xdd, 0xc0},
|
||||
},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
frame := test.framer.Encode(test.input)
|
||||
if !slices.Equal(frame, test.expected) {
|
||||
t.Fatalf("SLIP framer frame got %s, expected %s", frame, test.expected)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestSlipFramerBuffer(t *testing.T) {
|
||||
framer := framer.GetFramer("SLIP")
|
||||
framer.Decode([]byte{0xc0, 0x2f, 0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x00, 0x00, 0x2c, 0x00, 0x00, 0x00, 0xc0, 0xc0, 0x45})
|
||||
if !slices.Equal(framer.Buffer(), []byte{0x45}) {
|
||||
t.Fatalf("SLIP framer buffer got %s, expected %s", framer.Buffer(), []byte{0x45})
|
||||
}
|
||||
framer.Clear()
|
||||
if !slices.Equal(framer.Buffer(), []byte{}) {
|
||||
t.Fatalf("SLIP framer buffer got %s, expected empty slice", framer.Buffer())
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,7 +2,7 @@ package module
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"errors"
|
||||
"log/slog"
|
||||
"net/http"
|
||||
"time"
|
||||
@@ -16,14 +16,21 @@ type HTTPClient struct {
|
||||
ctx context.Context
|
||||
client *http.Client
|
||||
router route.RouteIO
|
||||
logger *slog.Logger
|
||||
}
|
||||
|
||||
func init() {
|
||||
RegisterModule(ModuleRegistration{
|
||||
Type: "http.client",
|
||||
New: func(ctx context.Context, config config.ModuleConfig, router route.RouteIO) (Module, error) {
|
||||
New: func(ctx context.Context, config config.ModuleConfig) (Module, error) {
|
||||
|
||||
return &HTTPClient{config: config, ctx: ctx, router: router}, nil
|
||||
router, ok := ctx.Value(route.RouterContextKey).(route.RouteIO)
|
||||
|
||||
if !ok {
|
||||
return nil, errors.New("http.client unable to get router from context")
|
||||
}
|
||||
|
||||
return &HTTPClient{config: config, ctx: ctx, router: router, logger: CreateLogger(config)}, nil
|
||||
},
|
||||
})
|
||||
}
|
||||
@@ -43,20 +50,20 @@ func (hc *HTTPClient) Run() error {
|
||||
}
|
||||
|
||||
<-hc.ctx.Done()
|
||||
slog.Debug("router context done in module", "id", hc.Id())
|
||||
hc.logger.Debug("done")
|
||||
return nil
|
||||
}
|
||||
|
||||
func (hc *HTTPClient) Output(payload any) error {
|
||||
func (hc *HTTPClient) Output(ctx context.Context, payload any) error {
|
||||
|
||||
payloadRequest, ok := payload.(*http.Request)
|
||||
|
||||
if !ok {
|
||||
return fmt.Errorf("http.client is only able to output an http.Request")
|
||||
return errors.New("http.client is only able to output an http.Request")
|
||||
}
|
||||
|
||||
if hc.client == nil {
|
||||
return fmt.Errorf("http.client client is nil")
|
||||
return errors.New("http.client client is nil")
|
||||
}
|
||||
|
||||
response, err := hc.client.Do(payloadRequest)
|
||||
@@ -66,7 +73,7 @@ func (hc *HTTPClient) Output(payload any) error {
|
||||
}
|
||||
|
||||
if hc.router != nil {
|
||||
hc.router.HandleInput(hc.Id(), response)
|
||||
hc.router.HandleInput(hc.ctx, hc.Id(), response)
|
||||
}
|
||||
|
||||
return nil
|
||||
|
||||
@@ -3,11 +3,13 @@ package module
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"net/http"
|
||||
|
||||
"github.com/jwetzell/showbridge-go/internal/config"
|
||||
"github.com/jwetzell/showbridge-go/internal/processor"
|
||||
"github.com/jwetzell/showbridge-go/internal/route"
|
||||
)
|
||||
|
||||
@@ -16,30 +18,62 @@ type HTTPServer struct {
|
||||
Port uint16
|
||||
ctx context.Context
|
||||
router route.RouteIO
|
||||
logger *slog.Logger
|
||||
}
|
||||
|
||||
type ResponseData struct {
|
||||
type ResponseIOError struct {
|
||||
Index int `json:"index"`
|
||||
OutputErrors []string `json:"outputErrors"`
|
||||
ProcessError *string `json:"processError"`
|
||||
InputError *string `json:"inputError"`
|
||||
}
|
||||
|
||||
type IOResponseData struct {
|
||||
IOErrors []ResponseIOError `json:"ioErrors"`
|
||||
Message string `json:"message"`
|
||||
Status string `json:"status"`
|
||||
}
|
||||
|
||||
type httpServerContextKey string
|
||||
|
||||
type HTTPServerResponseWriter struct {
|
||||
http.ResponseWriter
|
||||
done bool
|
||||
}
|
||||
|
||||
func (hsrw *HTTPServerResponseWriter) WriteHeader(status int) {
|
||||
hsrw.done = true
|
||||
hsrw.ResponseWriter.WriteHeader(status)
|
||||
}
|
||||
|
||||
func (hsrw *HTTPServerResponseWriter) Write(data []byte) (int, error) {
|
||||
hsrw.done = true
|
||||
return hsrw.ResponseWriter.Write(data)
|
||||
}
|
||||
|
||||
func init() {
|
||||
RegisterModule(ModuleRegistration{
|
||||
Type: "http.server",
|
||||
New: func(ctx context.Context, config config.ModuleConfig, router route.RouteIO) (Module, error) {
|
||||
New: func(ctx context.Context, config config.ModuleConfig) (Module, error) {
|
||||
params := config.Params
|
||||
port, ok := params["port"]
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("http.server requires a port parameter")
|
||||
return nil, errors.New("http.server requires a port parameter")
|
||||
}
|
||||
|
||||
portNum, ok := port.(float64)
|
||||
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("http.server port must be uint16")
|
||||
return nil, errors.New("http.server port must be uint16")
|
||||
}
|
||||
|
||||
return &HTTPServer{Port: uint16(portNum), config: config, ctx: ctx, router: router}, nil
|
||||
router, ok := ctx.Value(route.RouterContextKey).(route.RouteIO)
|
||||
|
||||
if !ok {
|
||||
return nil, errors.New("http.server unable to get router from context")
|
||||
}
|
||||
|
||||
return &HTTPServer{Port: uint16(portNum), config: config, ctx: ctx, router: router, logger: CreateLogger(config)}, nil
|
||||
},
|
||||
})
|
||||
}
|
||||
@@ -52,58 +86,119 @@ func (hs *HTTPServer) Type() string {
|
||||
return hs.config.Type
|
||||
}
|
||||
|
||||
func (hs *HTTPServer) HandleDefault(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
func (hs *HTTPServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
responseWriter := HTTPServerResponseWriter{ResponseWriter: w}
|
||||
|
||||
response := ResponseData{
|
||||
response := IOResponseData{
|
||||
Message: "routing successful",
|
||||
Status: "ok",
|
||||
}
|
||||
|
||||
if hs.router != nil {
|
||||
routingErrors := hs.router.HandleInput(hs.Id(), r)
|
||||
inputContext := context.WithValue(hs.ctx, httpServerContextKey("responseWriter"), &responseWriter)
|
||||
aRouteFound, routingErrors := hs.router.HandleInput(inputContext, hs.Id(), r)
|
||||
if !responseWriter.done {
|
||||
if aRouteFound {
|
||||
if routingErrors != nil {
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
response.Status = "error"
|
||||
response.Message = "routing failed"
|
||||
|
||||
response.IOErrors = []ResponseIOError{}
|
||||
for _, responseIOError := range routingErrors {
|
||||
errorToAdd := ResponseIOError{
|
||||
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.OutputErrors != nil {
|
||||
outputErrorMsgs := []string{}
|
||||
|
||||
for _, outputError := range responseIOError.OutputErrors {
|
||||
outputErrorMsgs = append(outputErrorMsgs, outputError.Error())
|
||||
}
|
||||
|
||||
errorToAdd.OutputErrors = outputErrorMsgs
|
||||
}
|
||||
|
||||
response.IOErrors = append(response.IOErrors, errorToAdd)
|
||||
|
||||
}
|
||||
json.NewEncoder(w).Encode(response)
|
||||
return
|
||||
} else {
|
||||
w.WriteHeader(http.StatusOK)
|
||||
response.Message = "routing successful"
|
||||
json.NewEncoder(w).Encode(response)
|
||||
return
|
||||
}
|
||||
} else {
|
||||
w.WriteHeader(http.StatusNotFound)
|
||||
response.Status = "error"
|
||||
response.Message = "no matching routes found"
|
||||
json.NewEncoder(w).Encode(response)
|
||||
return
|
||||
}
|
||||
}
|
||||
} else {
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
response.Message = "no router registered"
|
||||
response.Status = "error"
|
||||
}
|
||||
|
||||
json.NewEncoder(w).Encode(response)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
func (hs *HTTPServer) Run() error {
|
||||
http.HandleFunc("/", hs.HandleDefault)
|
||||
|
||||
httpServer := &http.Server{
|
||||
Addr: fmt.Sprintf(":%d", hs.Port),
|
||||
Handler: http.DefaultServeMux,
|
||||
Handler: hs,
|
||||
}
|
||||
|
||||
go func() {
|
||||
<-hs.ctx.Done()
|
||||
slog.Debug("router context done in module", "id", hs.Id())
|
||||
httpServer.Close()
|
||||
}()
|
||||
|
||||
err := httpServer.ListenAndServe()
|
||||
slog.Debug("http.server closed", "id", hs.Id())
|
||||
// TODO(jwetzell): handle server closed error differently
|
||||
if err != nil {
|
||||
if err.Error() != "http: Server closed" {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
<-hs.ctx.Done()
|
||||
hs.logger.Debug("done")
|
||||
return nil
|
||||
}
|
||||
|
||||
func (hs *HTTPServer) Output(payload any) error {
|
||||
return fmt.Errorf("http.server output is not implemented")
|
||||
func (hs *HTTPServer) Output(ctx context.Context, payload any) error {
|
||||
responseWriter, ok := ctx.Value(httpServerContextKey("responseWriter")).(*HTTPServerResponseWriter)
|
||||
|
||||
if !ok {
|
||||
return errors.New("http.server output must originate from an http.server input")
|
||||
}
|
||||
|
||||
payloadResponse, ok := payload.(processor.HTTPResponse)
|
||||
|
||||
if !ok {
|
||||
return errors.New("http.server is only able to output HTTPResponse")
|
||||
}
|
||||
|
||||
if responseWriter.done {
|
||||
return errors.New("http.server response writer has already been written to")
|
||||
}
|
||||
|
||||
responseWriter.WriteHeader(payloadResponse.Status)
|
||||
responseWriter.Write(payloadResponse.Body)
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -1,73 +0,0 @@
|
||||
package module
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"time"
|
||||
|
||||
"github.com/jwetzell/showbridge-go/internal/config"
|
||||
"github.com/jwetzell/showbridge-go/internal/route"
|
||||
)
|
||||
|
||||
type Interval struct {
|
||||
config config.ModuleConfig
|
||||
Duration uint32
|
||||
ctx context.Context
|
||||
router route.RouteIO
|
||||
ticker *time.Ticker
|
||||
}
|
||||
|
||||
func init() {
|
||||
RegisterModule(ModuleRegistration{
|
||||
Type: "gen.interval",
|
||||
New: func(ctx context.Context, config config.ModuleConfig, router route.RouteIO) (Module, error) {
|
||||
params := config.Params
|
||||
|
||||
duration, ok := params["duration"]
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("gen.interval requires a duration parameter")
|
||||
}
|
||||
|
||||
durationNum, ok := duration.(float64)
|
||||
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("gen.interval duration must be number")
|
||||
}
|
||||
|
||||
return &Interval{Duration: uint32(durationNum), config: config, ctx: ctx, router: router}, nil
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
func (i *Interval) Id() string {
|
||||
return i.config.Id
|
||||
}
|
||||
|
||||
func (i *Interval) Type() string {
|
||||
return i.config.Type
|
||||
}
|
||||
|
||||
func (i *Interval) Run() error {
|
||||
ticker := time.NewTicker(time.Millisecond * time.Duration(i.Duration))
|
||||
i.ticker = ticker
|
||||
defer ticker.Stop()
|
||||
|
||||
for {
|
||||
select {
|
||||
case <-i.ctx.Done():
|
||||
slog.Debug("router context done in module", "id", i.Id())
|
||||
return nil
|
||||
case <-ticker.C:
|
||||
if i.router != nil {
|
||||
i.router.HandleInput(i.Id(), time.Now())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func (i *Interval) Output(payload any) error {
|
||||
i.ticker.Reset(time.Millisecond * time.Duration(i.Duration))
|
||||
return nil
|
||||
}
|
||||
@@ -4,6 +4,7 @@ package module
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
|
||||
@@ -19,49 +20,56 @@ type MIDIInput struct {
|
||||
router route.RouteIO
|
||||
Port string
|
||||
SendFunc func(midi.Message) error
|
||||
logger *slog.Logger
|
||||
}
|
||||
|
||||
func init() {
|
||||
RegisterModule(ModuleRegistration{
|
||||
Type: "midi.input",
|
||||
New: func(ctx context.Context, config config.ModuleConfig, router route.RouteIO) (Module, error) {
|
||||
New: func(ctx context.Context, config config.ModuleConfig) (Module, error) {
|
||||
params := config.Params
|
||||
port, ok := params["port"]
|
||||
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("midi.input requires a port parameter")
|
||||
return nil, errors.New("midi.input requires a port parameter")
|
||||
}
|
||||
|
||||
portString, ok := port.(string)
|
||||
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("midi.input port must be a string")
|
||||
return nil, errors.New("midi.input port must be a string")
|
||||
}
|
||||
|
||||
return &MIDIInput{config: config, Port: portString, ctx: ctx, router: router}, nil
|
||||
router, ok := ctx.Value(route.RouterContextKey).(route.RouteIO)
|
||||
|
||||
if !ok {
|
||||
return nil, errors.New("midi.input unable to get router from context")
|
||||
}
|
||||
|
||||
return &MIDIInput{config: config, Port: portString, ctx: ctx, router: router, logger: CreateLogger(config)}, nil
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
func (mc *MIDIInput) Id() string {
|
||||
return mc.config.Id
|
||||
func (mi *MIDIInput) Id() string {
|
||||
return mi.config.Id
|
||||
}
|
||||
|
||||
func (mc *MIDIInput) Type() string {
|
||||
return mc.config.Type
|
||||
func (mi *MIDIInput) Type() string {
|
||||
return mi.config.Type
|
||||
}
|
||||
|
||||
func (mc *MIDIInput) Run() error {
|
||||
func (mi *MIDIInput) Run() error {
|
||||
defer midi.CloseDriver()
|
||||
|
||||
in, err := midi.FindInPort(mc.Port)
|
||||
in, err := midi.FindInPort(mi.Port)
|
||||
if err != nil {
|
||||
return fmt.Errorf("midi.input can't find input port: %s", mc.Port)
|
||||
return fmt.Errorf("midi.input can't find input port: %s", mi.Port)
|
||||
}
|
||||
|
||||
stop, err := midi.ListenTo(in, func(msg midi.Message, timestampms int32) {
|
||||
if mc.router != nil {
|
||||
mc.router.HandleInput(mc.Id(), msg)
|
||||
if mi.router != nil {
|
||||
mi.router.HandleInput(mi.ctx, mi.Id(), msg)
|
||||
}
|
||||
}, midi.UseSysEx())
|
||||
|
||||
@@ -71,11 +79,11 @@ func (mc *MIDIInput) Run() error {
|
||||
|
||||
defer stop()
|
||||
|
||||
<-mc.ctx.Done()
|
||||
slog.Debug("router context done in module", "id", mc.Id())
|
||||
<-mi.ctx.Done()
|
||||
mi.logger.Debug("done")
|
||||
return nil
|
||||
}
|
||||
|
||||
func (mc *MIDIInput) Output(payload any) error {
|
||||
return fmt.Errorf("midi.input output is not implemented")
|
||||
func (mi *MIDIInput) Output(ctx context.Context, payload any) error {
|
||||
return errors.New("midi.input output is not implemented")
|
||||
}
|
||||
|
||||
@@ -1,89 +0,0 @@
|
||||
//go:build cgo
|
||||
|
||||
package module
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
|
||||
"github.com/jwetzell/showbridge-go/internal/config"
|
||||
"github.com/jwetzell/showbridge-go/internal/route"
|
||||
"gitlab.com/gomidi/midi/v2"
|
||||
_ "gitlab.com/gomidi/midi/v2/drivers/rtmididrv"
|
||||
)
|
||||
|
||||
type MIDIOutput struct {
|
||||
config config.ModuleConfig
|
||||
ctx context.Context
|
||||
router route.RouteIO
|
||||
Port string
|
||||
SendFunc func(midi.Message) error
|
||||
}
|
||||
|
||||
func init() {
|
||||
RegisterModule(ModuleRegistration{
|
||||
//TODO(jwetzell): find a better namespace than "misc"
|
||||
Type: "midi.output",
|
||||
New: func(ctx context.Context, config config.ModuleConfig, router route.RouteIO) (Module, error) {
|
||||
params := config.Params
|
||||
|
||||
port, ok := params["port"]
|
||||
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("midi.output requires a port parameter")
|
||||
}
|
||||
|
||||
portString, ok := port.(string)
|
||||
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("midi.output port must be a string")
|
||||
}
|
||||
|
||||
return &MIDIOutput{config: config, Port: portString, ctx: ctx, router: router}, nil
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
func (mc *MIDIOutput) Id() string {
|
||||
return mc.config.Id
|
||||
}
|
||||
|
||||
func (mc *MIDIOutput) Type() string {
|
||||
return mc.config.Type
|
||||
}
|
||||
|
||||
func (mc *MIDIOutput) Run() error {
|
||||
defer midi.CloseDriver()
|
||||
|
||||
out, err := midi.FindOutPort(mc.Port)
|
||||
|
||||
if err != nil {
|
||||
return fmt.Errorf("midi.output can't find output port: %s", mc.Port)
|
||||
}
|
||||
|
||||
send, err := midi.SendTo(out)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
mc.SendFunc = send
|
||||
|
||||
<-mc.ctx.Done()
|
||||
slog.Debug("router context done in module", "id", mc.Id())
|
||||
return nil
|
||||
}
|
||||
|
||||
func (mc *MIDIOutput) Output(payload any) error {
|
||||
if mc.SendFunc == nil {
|
||||
return fmt.Errorf("midi.output output is not setup")
|
||||
}
|
||||
|
||||
payloadMessage, ok := payload.(midi.Message)
|
||||
|
||||
if !ok {
|
||||
return fmt.Errorf("midi.output can only ouptut midi.Message")
|
||||
}
|
||||
|
||||
return mc.SendFunc(payloadMessage)
|
||||
}
|
||||
96
internal/module/midi-output.go
Normal file
96
internal/module/midi-output.go
Normal file
@@ -0,0 +1,96 @@
|
||||
//go:build cgo
|
||||
|
||||
package module
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
|
||||
"github.com/jwetzell/showbridge-go/internal/config"
|
||||
"github.com/jwetzell/showbridge-go/internal/route"
|
||||
"gitlab.com/gomidi/midi/v2"
|
||||
_ "gitlab.com/gomidi/midi/v2/drivers/rtmididrv"
|
||||
)
|
||||
|
||||
type MIDIOutput struct {
|
||||
config config.ModuleConfig
|
||||
ctx context.Context
|
||||
router route.RouteIO
|
||||
Port string
|
||||
SendFunc func(midi.Message) error
|
||||
logger *slog.Logger
|
||||
}
|
||||
|
||||
func init() {
|
||||
RegisterModule(ModuleRegistration{
|
||||
Type: "midi.output",
|
||||
New: func(ctx context.Context, config config.ModuleConfig) (Module, error) {
|
||||
params := config.Params
|
||||
|
||||
port, ok := params["port"]
|
||||
|
||||
if !ok {
|
||||
return nil, errors.New("midi.output requires a port parameter")
|
||||
}
|
||||
|
||||
portString, ok := port.(string)
|
||||
|
||||
if !ok {
|
||||
return nil, errors.New("midi.output port must be a string")
|
||||
}
|
||||
|
||||
router, ok := ctx.Value(route.RouterContextKey).(route.RouteIO)
|
||||
|
||||
if !ok {
|
||||
return nil, errors.New("midi.output unable to get router from context")
|
||||
}
|
||||
|
||||
return &MIDIOutput{config: config, Port: portString, ctx: ctx, router: router, logger: CreateLogger(config)}, nil
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
func (mo *MIDIOutput) Id() string {
|
||||
return mo.config.Id
|
||||
}
|
||||
|
||||
func (mo *MIDIOutput) Type() string {
|
||||
return mo.config.Type
|
||||
}
|
||||
|
||||
func (mo *MIDIOutput) Run() error {
|
||||
defer midi.CloseDriver()
|
||||
|
||||
out, err := midi.FindOutPort(mo.Port)
|
||||
|
||||
if err != nil {
|
||||
return fmt.Errorf("midi.output can't find output port: %s", mo.Port)
|
||||
}
|
||||
|
||||
send, err := midi.SendTo(out)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
mo.SendFunc = send
|
||||
|
||||
<-mo.ctx.Done()
|
||||
mo.logger.Debug("done")
|
||||
return nil
|
||||
}
|
||||
|
||||
func (mo *MIDIOutput) Output(ctx context.Context, payload any) error {
|
||||
if mo.SendFunc == nil {
|
||||
return errors.New("midi.output output is not setup")
|
||||
}
|
||||
|
||||
payloadMessage, ok := payload.(midi.Message)
|
||||
|
||||
if !ok {
|
||||
return errors.New("midi.output can only ouptut midi.Message")
|
||||
}
|
||||
|
||||
return mo.SendFunc(payloadMessage)
|
||||
}
|
||||
@@ -3,10 +3,10 @@ package module
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"sync"
|
||||
|
||||
"github.com/jwetzell/showbridge-go/internal/config"
|
||||
"github.com/jwetzell/showbridge-go/internal/route"
|
||||
)
|
||||
|
||||
type ModuleError struct {
|
||||
@@ -19,12 +19,12 @@ type Module interface {
|
||||
Id() string
|
||||
Type() string
|
||||
Run() error
|
||||
Output(any) error
|
||||
Output(context.Context, any) error
|
||||
}
|
||||
|
||||
type ModuleRegistration struct {
|
||||
Type string `json:"type"`
|
||||
New func(context.Context, config.ModuleConfig, route.RouteIO) (Module, error)
|
||||
New func(context.Context, config.ModuleConfig) (Module, error)
|
||||
}
|
||||
|
||||
func RegisterModule(mod ModuleRegistration) {
|
||||
@@ -49,3 +49,7 @@ var (
|
||||
moduleRegistryMu sync.RWMutex
|
||||
ModuleRegistry = make(map[string]ModuleRegistration)
|
||||
)
|
||||
|
||||
func CreateLogger(config config.ModuleConfig) *slog.Logger {
|
||||
return slog.Default().With("component", "module", "id", config.Id, "type", config.Type)
|
||||
}
|
||||
|
||||
@@ -2,7 +2,7 @@ package module
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"errors"
|
||||
"log/slog"
|
||||
|
||||
mqtt "github.com/eclipse/paho.mqtt.golang"
|
||||
@@ -18,50 +18,57 @@ type MQTTClient struct {
|
||||
ClientID string
|
||||
Topic string
|
||||
client mqtt.Client
|
||||
logger *slog.Logger
|
||||
}
|
||||
|
||||
func init() {
|
||||
RegisterModule(ModuleRegistration{
|
||||
Type: "mqtt.client",
|
||||
New: func(ctx context.Context, config config.ModuleConfig, router route.RouteIO) (Module, error) {
|
||||
New: func(ctx context.Context, config config.ModuleConfig) (Module, error) {
|
||||
params := config.Params
|
||||
broker, ok := params["broker"]
|
||||
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("mqtt.client requires a broker parameter")
|
||||
return nil, errors.New("mqtt.client requires a broker parameter")
|
||||
}
|
||||
|
||||
brokerString, ok := broker.(string)
|
||||
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("mqtt.client broker must be string")
|
||||
return nil, errors.New("mqtt.client broker must be string")
|
||||
}
|
||||
|
||||
topic, ok := params["topic"]
|
||||
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("mqtt.client requires a topic parameter")
|
||||
return nil, errors.New("mqtt.client requires a topic parameter")
|
||||
}
|
||||
|
||||
topicString, ok := topic.(string)
|
||||
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("mqtt.client topic must be string")
|
||||
return nil, errors.New("mqtt.client topic must be string")
|
||||
}
|
||||
|
||||
clientId, ok := params["clientId"]
|
||||
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("mqtt.client requires a clientId parameter")
|
||||
return nil, errors.New("mqtt.client requires a clientId parameter")
|
||||
}
|
||||
|
||||
clientIdString, ok := clientId.(string)
|
||||
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("mqtt.client clientId must be string")
|
||||
return nil, errors.New("mqtt.client clientId must be string")
|
||||
}
|
||||
|
||||
return &MQTTClient{config: config, Broker: brokerString, Topic: topicString, ClientID: clientIdString, ctx: ctx, router: router}, nil
|
||||
router, ok := ctx.Value(route.RouterContextKey).(route.RouteIO)
|
||||
|
||||
if !ok {
|
||||
return nil, errors.New("mqtt.client unable to get router from context")
|
||||
}
|
||||
|
||||
return &MQTTClient{config: config, Broker: brokerString, Topic: topicString, ClientID: clientIdString, ctx: ctx, router: router, logger: CreateLogger(config)}, nil
|
||||
},
|
||||
})
|
||||
}
|
||||
@@ -83,7 +90,7 @@ func (mc *MQTTClient) Run() 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.Id(), m)
|
||||
mc.router.HandleInput(mc.ctx, mc.Id(), m)
|
||||
})
|
||||
token.Wait()
|
||||
}
|
||||
@@ -99,25 +106,23 @@ func (mc *MQTTClient) Run() error {
|
||||
}
|
||||
|
||||
<-mc.ctx.Done()
|
||||
slog.Debug("router context done in module", "id", mc.Id())
|
||||
mc.logger.Debug("done")
|
||||
return nil
|
||||
}
|
||||
|
||||
func (mc *MQTTClient) Output(payload any) error {
|
||||
func (mc *MQTTClient) Output(ctx context.Context, payload any) error {
|
||||
payloadMessage, ok := payload.(mqtt.Message)
|
||||
|
||||
fmt.Printf("payload type: %T\n", payload)
|
||||
|
||||
if !ok {
|
||||
return fmt.Errorf("mqtt.client is only able to output a MQTTMessage")
|
||||
return errors.New("mqtt.client is only able to output a MQTTMessage")
|
||||
}
|
||||
|
||||
if mc.client == nil {
|
||||
return fmt.Errorf("mqtt.client client is not setup")
|
||||
return errors.New("mqtt.client client is not setup")
|
||||
}
|
||||
|
||||
if !mc.client.IsConnected() {
|
||||
return fmt.Errorf("mqtt.client is not connected")
|
||||
return errors.New("mqtt.client is not connected")
|
||||
}
|
||||
|
||||
token := mc.client.Publish(payloadMessage.Topic(), payloadMessage.Qos(), payloadMessage.Retained(), payloadMessage.Payload())
|
||||
|
||||
@@ -2,7 +2,7 @@ package module
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"errors"
|
||||
"log/slog"
|
||||
|
||||
"github.com/jwetzell/showbridge-go/internal/config"
|
||||
@@ -18,38 +18,45 @@ type NATSClient struct {
|
||||
URL string
|
||||
Subject string
|
||||
client *nats.Conn
|
||||
logger *slog.Logger
|
||||
}
|
||||
|
||||
func init() {
|
||||
RegisterModule(ModuleRegistration{
|
||||
Type: "nats.client",
|
||||
New: func(ctx context.Context, config config.ModuleConfig, router route.RouteIO) (Module, error) {
|
||||
New: func(ctx context.Context, config config.ModuleConfig) (Module, error) {
|
||||
params := config.Params
|
||||
url, ok := params["url"]
|
||||
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("nats.client requires a url parameter")
|
||||
return nil, errors.New("nats.client requires a url parameter")
|
||||
}
|
||||
|
||||
urlString, ok := url.(string)
|
||||
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("nats.client url must be string")
|
||||
return nil, errors.New("nats.client url must be string")
|
||||
}
|
||||
|
||||
subject, ok := params["subject"]
|
||||
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("nats.client requires a subject parameter")
|
||||
return nil, errors.New("nats.client requires a subject parameter")
|
||||
}
|
||||
|
||||
subjectString, ok := subject.(string)
|
||||
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("nats.client subject must be string")
|
||||
return nil, errors.New("nats.client subject must be string")
|
||||
}
|
||||
|
||||
return &NATSClient{config: config, URL: urlString, Subject: subjectString, ctx: ctx, router: router}, nil
|
||||
router, ok := ctx.Value(route.RouterContextKey).(route.RouteIO)
|
||||
|
||||
if !ok {
|
||||
return nil, errors.New("nats.client unable to get router from context")
|
||||
}
|
||||
|
||||
return &NATSClient{config: config, URL: urlString, Subject: subjectString, ctx: ctx, router: router, logger: CreateLogger(config)}, nil
|
||||
},
|
||||
})
|
||||
}
|
||||
@@ -76,7 +83,7 @@ func (nc *NATSClient) Run() error {
|
||||
|
||||
sub, err := nc.client.Subscribe(nc.Subject, func(msg *nats.Msg) {
|
||||
if nc.router != nil {
|
||||
nc.router.HandleInput(nc.Id(), msg)
|
||||
nc.router.HandleInput(nc.ctx, nc.Id(), msg)
|
||||
}
|
||||
})
|
||||
|
||||
@@ -87,24 +94,24 @@ func (nc *NATSClient) Run() error {
|
||||
defer sub.Unsubscribe()
|
||||
|
||||
<-nc.ctx.Done()
|
||||
slog.Debug("router context done in module", "id", nc.Id())
|
||||
nc.logger.Debug("done")
|
||||
return nil
|
||||
}
|
||||
|
||||
func (nc *NATSClient) Output(payload any) error {
|
||||
func (nc *NATSClient) Output(ctx context.Context, payload any) error {
|
||||
|
||||
payloadMessage, ok := payload.(processor.NATSMessage)
|
||||
|
||||
if !ok {
|
||||
return fmt.Errorf("nats.client is only able to output NATSMessage")
|
||||
return errors.New("nats.client is only able to output NATSMessage")
|
||||
}
|
||||
|
||||
if nc.client == nil {
|
||||
return fmt.Errorf("nats.client client is not setup")
|
||||
return errors.New("nats.client client is not setup")
|
||||
}
|
||||
|
||||
if !nc.client.IsConnected() {
|
||||
return fmt.Errorf("nats.client is not connected")
|
||||
return errors.New("nats.client is not connected")
|
||||
}
|
||||
|
||||
err := nc.client.Publish(payloadMessage.Subject, payloadMessage.Payload)
|
||||
|
||||
@@ -2,6 +2,7 @@ package module
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"net"
|
||||
@@ -18,14 +19,19 @@ type PSNClient struct {
|
||||
ctx context.Context
|
||||
router route.RouteIO
|
||||
decoder *psn.Decoder
|
||||
logger *slog.Logger
|
||||
}
|
||||
|
||||
func init() {
|
||||
RegisterModule(ModuleRegistration{
|
||||
Type: "psn.client",
|
||||
New: func(ctx context.Context, config config.ModuleConfig, router route.RouteIO) (Module, error) {
|
||||
New: func(ctx context.Context, config config.ModuleConfig) (Module, error) {
|
||||
router, ok := ctx.Value(route.RouterContextKey).(route.RouteIO)
|
||||
|
||||
return &PSNClient{config: config, decoder: psn.NewDecoder(), ctx: ctx, router: router}, nil
|
||||
if !ok {
|
||||
return nil, errors.New("psn.client unable to get router from context")
|
||||
}
|
||||
return &PSNClient{config: config, decoder: psn.NewDecoder(), ctx: ctx, router: router, logger: CreateLogger(config)}, nil
|
||||
},
|
||||
})
|
||||
}
|
||||
@@ -58,7 +64,7 @@ func (pc *PSNClient) Run() error {
|
||||
select {
|
||||
case <-pc.ctx.Done():
|
||||
// TODO(jwetzell): cleanup?
|
||||
slog.Debug("router context done in module", "id", pc.Id())
|
||||
pc.logger.Debug("done")
|
||||
return nil
|
||||
default:
|
||||
pc.conn.SetDeadline(time.Now().Add(time.Millisecond * 200))
|
||||
@@ -76,21 +82,21 @@ func (pc *PSNClient) Run() error {
|
||||
message := buffer[:numBytes]
|
||||
err := pc.decoder.Decode(message)
|
||||
if err != nil {
|
||||
slog.Error("psn.client problem decoding psn traffic", "id", pc.Id(), "error", err)
|
||||
pc.logger.Error("problem decoding psn traffic", "error", err)
|
||||
}
|
||||
|
||||
if pc.router != nil {
|
||||
for _, tracker := range pc.decoder.Trackers {
|
||||
pc.router.HandleInput(pc.Id(), tracker)
|
||||
pc.router.HandleInput(pc.ctx, pc.Id(), tracker)
|
||||
}
|
||||
} else {
|
||||
slog.Error("psn.client has no router", "id", pc.Id())
|
||||
pc.logger.Error("has no router")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (pc *PSNClient) Output(payload any) error {
|
||||
func (pc *PSNClient) Output(ctx context.Context, payload any) error {
|
||||
return fmt.Errorf("psn.client output is not implemented")
|
||||
}
|
||||
|
||||
@@ -4,6 +4,7 @@ package module
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"time"
|
||||
@@ -22,24 +23,24 @@ type SerialClient struct {
|
||||
Framer framer.Framer
|
||||
Mode *serial.Mode
|
||||
port serial.Port
|
||||
logger *slog.Logger
|
||||
}
|
||||
|
||||
func init() {
|
||||
RegisterModule(ModuleRegistration{
|
||||
//TODO(jwetzell): find a better namespace than "misc"
|
||||
Type: "serial.client",
|
||||
New: func(ctx context.Context, config config.ModuleConfig, router route.RouteIO) (Module, error) {
|
||||
New: func(ctx context.Context, config config.ModuleConfig) (Module, error) {
|
||||
params := config.Params
|
||||
port, ok := params["port"]
|
||||
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("serial.client requires a port parameter")
|
||||
return nil, errors.New("serial.client requires a port parameter")
|
||||
}
|
||||
|
||||
portString, ok := port.(string)
|
||||
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("serial.client port must be a string")
|
||||
return nil, errors.New("serial.client port must be a string")
|
||||
}
|
||||
|
||||
framingMethod := "RAW"
|
||||
@@ -50,107 +51,113 @@ func init() {
|
||||
framingMethodString, ok := framingMethodRaw.(string)
|
||||
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("serial.client framing method must be a string")
|
||||
return nil, errors.New("serial.client framing method must be a string")
|
||||
}
|
||||
framingMethod = framingMethodString
|
||||
}
|
||||
|
||||
framer, err := framer.GetFramer(framingMethod)
|
||||
framer := framer.GetFramer(framingMethod)
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
if framer == nil {
|
||||
return nil, fmt.Errorf("serial.client unknown framing method: %s", framingMethod)
|
||||
}
|
||||
|
||||
buadRate, ok := params["baudRate"]
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("serial.client requires a baudRate parameter")
|
||||
return nil, errors.New("serial.client requires a baudRate parameter")
|
||||
}
|
||||
|
||||
baudRateNum, ok := buadRate.(float64)
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("serial.client baudRate must be a number")
|
||||
return nil, errors.New("serial.client baudRate must be a number")
|
||||
}
|
||||
|
||||
mode := serial.Mode{
|
||||
BaudRate: int(baudRateNum),
|
||||
}
|
||||
|
||||
return &SerialClient{config: config, Port: portString, Framer: framer, Mode: &mode, ctx: ctx, router: router}, nil
|
||||
router, ok := ctx.Value(route.RouterContextKey).(route.RouteIO)
|
||||
|
||||
if !ok {
|
||||
return nil, errors.New("serial.client unable to get router from context")
|
||||
}
|
||||
|
||||
return &SerialClient{config: config, Port: portString, Framer: framer, Mode: &mode, ctx: ctx, router: router, logger: CreateLogger(config)}, nil
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
func (mc *SerialClient) Id() string {
|
||||
return mc.config.Id
|
||||
func (sc *SerialClient) Id() string {
|
||||
return sc.config.Id
|
||||
}
|
||||
|
||||
func (mc *SerialClient) Type() string {
|
||||
return mc.config.Type
|
||||
func (sc *SerialClient) Type() string {
|
||||
return sc.config.Type
|
||||
}
|
||||
|
||||
func (mc *SerialClient) SetupPort() error {
|
||||
func (sc *SerialClient) SetupPort() error {
|
||||
|
||||
port, err := serial.Open(mc.Port, mc.Mode)
|
||||
port, err := serial.Open(sc.Port, sc.Mode)
|
||||
if err != nil {
|
||||
return fmt.Errorf("serial.client can't open input port: %s", mc.Port)
|
||||
return fmt.Errorf("serial.client can't open input port: %s", sc.Port)
|
||||
}
|
||||
|
||||
mc.port = port
|
||||
sc.port = port
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (mc *SerialClient) Run() error {
|
||||
func (sc *SerialClient) Run() error {
|
||||
|
||||
// TODO(jwetzell): shutdown with router.Context properly
|
||||
go func() {
|
||||
<-mc.ctx.Done()
|
||||
slog.Debug("router context done in module", "id", mc.Id())
|
||||
if mc.port != nil {
|
||||
mc.port.Close()
|
||||
<-sc.ctx.Done()
|
||||
sc.logger.Debug("done")
|
||||
if sc.port != nil {
|
||||
sc.port.Close()
|
||||
}
|
||||
}()
|
||||
|
||||
for {
|
||||
err := mc.SetupPort()
|
||||
err := sc.SetupPort()
|
||||
if err != nil {
|
||||
if mc.ctx.Err() != nil {
|
||||
slog.Debug("router context done in module", "id", mc.Id())
|
||||
if sc.ctx.Err() != nil {
|
||||
sc.logger.Debug("done")
|
||||
return nil
|
||||
}
|
||||
slog.Error("serial.client", "id", mc.Id(), "error", err.Error())
|
||||
sc.logger.Error("port setup error", "error", err.Error())
|
||||
time.Sleep(time.Second * 2)
|
||||
continue
|
||||
}
|
||||
|
||||
buffer := make([]byte, 1024)
|
||||
select {
|
||||
case <-mc.ctx.Done():
|
||||
slog.Debug("router context done in module", "id", mc.Id())
|
||||
case <-sc.ctx.Done():
|
||||
sc.logger.Debug("done")
|
||||
return nil
|
||||
default:
|
||||
READ:
|
||||
for {
|
||||
select {
|
||||
case <-mc.ctx.Done():
|
||||
slog.Debug("router context done in module", "id", mc.Id())
|
||||
case <-sc.ctx.Done():
|
||||
sc.logger.Debug("done")
|
||||
return nil
|
||||
default:
|
||||
byteCount, err := mc.port.Read(buffer)
|
||||
byteCount, err := sc.port.Read(buffer)
|
||||
|
||||
if err != nil {
|
||||
mc.Framer.Clear()
|
||||
sc.Framer.Clear()
|
||||
break READ
|
||||
}
|
||||
|
||||
if mc.Framer != nil {
|
||||
if sc.Framer != nil {
|
||||
if byteCount > 0 {
|
||||
messages := mc.Framer.Decode(buffer[0:byteCount])
|
||||
messages := sc.Framer.Decode(buffer[0:byteCount])
|
||||
for _, message := range messages {
|
||||
if mc.router != nil {
|
||||
mc.router.HandleInput(mc.Id(), message)
|
||||
if sc.router != nil {
|
||||
sc.router.HandleInput(sc.ctx, sc.Id(), message)
|
||||
} else {
|
||||
slog.Error("serial.client has no router", "id", mc.Id())
|
||||
sc.logger.Error("input received but no router is configured")
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -161,14 +168,14 @@ func (mc *SerialClient) Run() error {
|
||||
}
|
||||
}
|
||||
|
||||
func (mc *SerialClient) Output(payload any) error {
|
||||
func (sc *SerialClient) Output(ctx context.Context, payload any) error {
|
||||
|
||||
payloadBytes, ok := payload.([]byte)
|
||||
|
||||
if !ok {
|
||||
return fmt.Errorf("serial.client can only ouptut bytes")
|
||||
return errors.New("serial.client can only ouptut bytes")
|
||||
}
|
||||
|
||||
_, err := mc.port.Write(mc.Framer.Encode(payloadBytes))
|
||||
_, err := sc.port.Write(sc.Framer.Encode(payloadBytes))
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -2,9 +2,12 @@ package module
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"log/slog"
|
||||
"os"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/emiago/diago"
|
||||
@@ -12,6 +15,7 @@ import (
|
||||
"github.com/emiago/sipgo"
|
||||
"github.com/emiago/sipgo/sip"
|
||||
"github.com/jwetzell/showbridge-go/internal/config"
|
||||
"github.com/jwetzell/showbridge-go/internal/processor"
|
||||
"github.com/jwetzell/showbridge-go/internal/route"
|
||||
)
|
||||
|
||||
@@ -24,16 +28,24 @@ type SIPCallServer struct {
|
||||
Transport string
|
||||
UserAgent string
|
||||
dg *diago.Diago
|
||||
logger *slog.Logger
|
||||
}
|
||||
|
||||
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",
|
||||
New: func(ctx context.Context, config config.ModuleConfig, router route.RouteIO) (Module, error) {
|
||||
New: func(ctx context.Context, config config.ModuleConfig) (Module, error) {
|
||||
params := config.Params
|
||||
portNum := 5060
|
||||
|
||||
@@ -42,7 +54,7 @@ func init() {
|
||||
specificPortNum, ok := port.(float64)
|
||||
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("sip.call.server port must be a number")
|
||||
return nil, errors.New("sip.call.server port must be a number")
|
||||
}
|
||||
portNum = int(specificPortNum)
|
||||
}
|
||||
@@ -55,7 +67,7 @@ func init() {
|
||||
specificIpString, ok := ip.(string)
|
||||
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("sip.call.server ip must be a string")
|
||||
return nil, errors.New("sip.call.server ip must be a string")
|
||||
}
|
||||
ipString = specificIpString
|
||||
}
|
||||
@@ -68,7 +80,7 @@ func init() {
|
||||
specificTransportString, ok := transport.(string)
|
||||
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("sip.call.server transport must be a string")
|
||||
return nil, errors.New("sip.call.server transport must be a string")
|
||||
}
|
||||
transportString = specificTransportString
|
||||
}
|
||||
@@ -81,28 +93,34 @@ func init() {
|
||||
specificTransportString, ok := userAgent.(string)
|
||||
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("sip.call.server userAgent must be a string")
|
||||
return nil, errors.New("sip.call.server userAgent must be a string")
|
||||
}
|
||||
userAgentString = specificTransportString
|
||||
}
|
||||
return &SIPCallServer{config: config, ctx: ctx, router: router, IP: ipString, Port: int(portNum), Transport: transportString, UserAgent: userAgentString}, nil
|
||||
|
||||
router, ok := ctx.Value(route.RouterContextKey).(route.RouteIO)
|
||||
|
||||
if !ok {
|
||||
return nil, errors.New("sip.call.server unable to get router from context")
|
||||
}
|
||||
return &SIPCallServer{config: config, ctx: ctx, router: router, IP: ipString, Port: int(portNum), Transport: transportString, UserAgent: userAgentString, logger: CreateLogger(config)}, nil
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
func (sds *SIPCallServer) Id() string {
|
||||
return sds.config.Id
|
||||
func (scs *SIPCallServer) Id() string {
|
||||
return scs.config.Id
|
||||
}
|
||||
|
||||
func (sds *SIPCallServer) Type() string {
|
||||
return sds.config.Type
|
||||
func (scs *SIPCallServer) Type() string {
|
||||
return scs.config.Type
|
||||
}
|
||||
|
||||
func (sds *SIPCallServer) Run() error {
|
||||
func (scs *SIPCallServer) Run() error {
|
||||
diagoLogger := slog.New(slog.NewJSONHandler(io.Discard, nil))
|
||||
|
||||
ua, _ := sipgo.NewUA(
|
||||
sipgo.WithUserAgent(sds.UserAgent),
|
||||
sipgo.WithUserAgent(scs.UserAgent),
|
||||
sipgo.WithUserAgentTransportLayerOptions(sip.WithTransportLayerLogger(diagoLogger)),
|
||||
sipgo.WithUserAgentTransactionLayerOptions(sip.WithTransactionLayerLogger(diagoLogger)),
|
||||
)
|
||||
@@ -112,74 +130,99 @@ func (sds *SIPCallServer) Run() error {
|
||||
media.SetDefaultLogger(diagoLogger)
|
||||
dg := diago.NewDiago(ua, diago.WithLogger(diagoLogger), diago.WithTransport(
|
||||
diago.Transport{
|
||||
Transport: sds.Transport,
|
||||
BindHost: sds.IP,
|
||||
BindPort: sds.Port,
|
||||
Transport: scs.Transport,
|
||||
BindHost: scs.IP,
|
||||
BindPort: scs.Port,
|
||||
},
|
||||
))
|
||||
|
||||
go func() {
|
||||
dg.Serve(sds.ctx, func(inDialog *diago.DialogServerSession) {
|
||||
sds.HandleCall(inDialog)
|
||||
dg.Serve(scs.ctx, func(inDialog *diago.DialogServerSession) {
|
||||
scs.HandleCall(inDialog)
|
||||
})
|
||||
}()
|
||||
|
||||
sds.dg = dg
|
||||
scs.dg = dg
|
||||
|
||||
<-sds.ctx.Done()
|
||||
slog.Debug("router context done in module", "id", sds.Id())
|
||||
<-scs.ctx.Done()
|
||||
scs.logger.Debug("done")
|
||||
return nil
|
||||
}
|
||||
|
||||
func (sds *SIPCallServer) HandleCall(inDialog *diago.DialogServerSession) {
|
||||
func (scs *SIPCallServer) HandleCall(inDialog *diago.DialogServerSession) {
|
||||
inDialog.Trying()
|
||||
inDialog.Ringing()
|
||||
inDialog.Answer()
|
||||
sds.router.HandleInput(sds.Id(), SIPCallMessage{
|
||||
|
||||
dialogContext := context.WithValue(scs.ctx, sipCallContextKey("call"), &SIPCall{
|
||||
inDialog: inDialog,
|
||||
})
|
||||
scs.router.HandleInput(dialogContext, scs.Id(), SIPCallMessage{
|
||||
To: inDialog.ToUser(),
|
||||
})
|
||||
<-inDialog.Context().Done()
|
||||
fmt.Println(inDialog.LoadState())
|
||||
}
|
||||
|
||||
func (sds *SIPCallServer) Output(payload any) error {
|
||||
func (scs *SIPCallServer) Output(ctx context.Context, payload any) error {
|
||||
|
||||
call, ok := ctx.Value(sipCallContextKey("call")).(*SIPCall)
|
||||
|
||||
payloadMsg, ok := payload.(string)
|
||||
if !ok {
|
||||
return fmt.Errorf("sip.call.server output payload must be of type string")
|
||||
return errors.New("sip.call.server output must originate from sip.call.server input")
|
||||
}
|
||||
|
||||
if sds.dg == nil {
|
||||
return fmt.Errorf("sip.call.server diago is not initialized")
|
||||
gotLock := call.lock.TryLock()
|
||||
|
||||
if !gotLock {
|
||||
return errors.New("sip.call.server call is already locked")
|
||||
}
|
||||
|
||||
var uri sip.Uri
|
||||
err := sip.ParseUri(payloadMsg, &uri)
|
||||
if err != nil {
|
||||
return fmt.Errorf("sip.call.server output payload is not a valid SIP URI: %v", err)
|
||||
}
|
||||
outDialog, err := sds.dg.NewDialog(uri, diago.NewDialogOptions{
|
||||
Transport: sds.Transport,
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
return fmt.Errorf("sip.call.server failed to create new dialog: %v", err)
|
||||
if call.inDialog.LoadState() == sip.DialogStateEnded {
|
||||
return errors.New("sip.call.server inDialog already ended")
|
||||
}
|
||||
|
||||
err = outDialog.Invite(sds.ctx, diago.InviteClientOptions{})
|
||||
payloadDTMFResponse, ok := payload.(processor.SipDTMFResponse)
|
||||
|
||||
if ok {
|
||||
dtmfWriter := call.inDialog.AudioWriterDTMF()
|
||||
|
||||
time.Sleep(time.Millisecond * time.Duration(payloadDTMFResponse.PreWait))
|
||||
for i, dtmfRune := range payloadDTMFResponse.Digits {
|
||||
err := dtmfWriter.WriteDTMF(dtmfRune)
|
||||
|
||||
if err != nil {
|
||||
return fmt.Errorf("sip.call.server failed to send invite: %v", err)
|
||||
return fmt.Errorf("sip.dtmf.server error output dtmf digit at index %d", i)
|
||||
}
|
||||
}
|
||||
time.Sleep(time.Millisecond * time.Duration(payloadDTMFResponse.PreWait))
|
||||
return nil
|
||||
}
|
||||
|
||||
err = outDialog.Ack(sds.ctx)
|
||||
payloadAudioFileResponse, ok := payload.(processor.SipAudioFileResponse)
|
||||
|
||||
if ok {
|
||||
audioFile, err := os.Open(payloadAudioFileResponse.AudioFile)
|
||||
if err != nil {
|
||||
return fmt.Errorf("sip.call.server failed to send ack: %v", err)
|
||||
return err
|
||||
}
|
||||
// TODO(jwetzell): make this configurable
|
||||
// NOTE(jwetzell): wait 5 seconds before hanging up the call
|
||||
time.Sleep(5 * time.Second)
|
||||
err = outDialog.Hangup(sds.ctx)
|
||||
defer audioFile.Close()
|
||||
|
||||
playback, err := call.inDialog.PlaybackCreate()
|
||||
|
||||
if err != nil {
|
||||
return fmt.Errorf("sip.call.server failed to hangup call: %v", err)
|
||||
return err
|
||||
}
|
||||
|
||||
time.Sleep(time.Millisecond * time.Duration(payloadAudioFileResponse.PreWait))
|
||||
|
||||
_, err = playback.Play(audioFile, "audio/wav")
|
||||
|
||||
time.Sleep(time.Millisecond * time.Duration(payloadAudioFileResponse.PostWait))
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
return errors.New("sip.dtmf.server can only output SipDTMFResponse or SipAudioFileResponse")
|
||||
}
|
||||
|
||||
@@ -2,10 +2,13 @@ package module
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"log/slog"
|
||||
"os"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/emiago/diago"
|
||||
@@ -13,6 +16,7 @@ import (
|
||||
"github.com/emiago/sipgo"
|
||||
"github.com/emiago/sipgo/sip"
|
||||
"github.com/jwetzell/showbridge-go/internal/config"
|
||||
"github.com/jwetzell/showbridge-go/internal/processor"
|
||||
"github.com/jwetzell/showbridge-go/internal/route"
|
||||
)
|
||||
|
||||
@@ -24,6 +28,7 @@ type SIPDTMFServer struct {
|
||||
Port int
|
||||
Transport string
|
||||
Separator string
|
||||
logger *slog.Logger
|
||||
}
|
||||
|
||||
type SIPDTMFMessage struct {
|
||||
@@ -31,10 +36,15 @@ type SIPDTMFMessage struct {
|
||||
Digits string
|
||||
}
|
||||
|
||||
type SIPDTMFCall struct {
|
||||
inDialog *diago.DialogServerSession
|
||||
lock sync.Mutex
|
||||
}
|
||||
|
||||
func init() {
|
||||
RegisterModule(ModuleRegistration{
|
||||
Type: "sip.dtmf.server",
|
||||
New: func(ctx context.Context, config config.ModuleConfig, router route.RouteIO) (Module, error) {
|
||||
New: func(ctx context.Context, config config.ModuleConfig) (Module, error) {
|
||||
params := config.Params
|
||||
portNum := 5060
|
||||
|
||||
@@ -43,7 +53,7 @@ func init() {
|
||||
specificPortNum, ok := port.(float64)
|
||||
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("sip.dtmf.server port must be a number")
|
||||
return nil, errors.New("sip.dtmf.server port must be a number")
|
||||
}
|
||||
portNum = int(specificPortNum)
|
||||
}
|
||||
@@ -56,7 +66,7 @@ func init() {
|
||||
specificIpString, ok := ip.(string)
|
||||
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("sip.dtmf.server ip must be a string")
|
||||
return nil, errors.New("sip.dtmf.server ip must be a string")
|
||||
}
|
||||
ipString = specificIpString
|
||||
}
|
||||
@@ -69,28 +79,33 @@ func init() {
|
||||
specificTransportString, ok := transport.(string)
|
||||
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("sip.dtmf.server transport must be a string")
|
||||
return nil, errors.New("sip.dtmf.server transport must be a string")
|
||||
}
|
||||
transportString = specificTransportString
|
||||
}
|
||||
|
||||
separator, ok := params["separator"]
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("sip.dtmf.server requires a separator parameter")
|
||||
return nil, errors.New("sip.dtmf.server requires a separator parameter")
|
||||
}
|
||||
separatorString, ok := separator.(string)
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("sip.dtmf.server separator must be a string")
|
||||
return nil, errors.New("sip.dtmf.server separator must be a string")
|
||||
}
|
||||
|
||||
if len(separatorString) != 1 {
|
||||
return nil, fmt.Errorf("sip.dtmf.server separator must be a single character")
|
||||
return nil, errors.New("sip.dtmf.server separator must be a single character")
|
||||
}
|
||||
|
||||
if !strings.ContainsRune("0123456789*#ABCD", rune(separatorString[0])) {
|
||||
return nil, fmt.Errorf("sip.dtmf.server separator must be a valid DTMF character")
|
||||
return nil, errors.New("sip.dtmf.server separator must be a valid DTMF character")
|
||||
}
|
||||
return &SIPDTMFServer{config: config, ctx: ctx, router: router, IP: ipString, Port: int(portNum), Transport: transportString, Separator: separatorString}, nil
|
||||
router, ok := ctx.Value(route.RouterContextKey).(route.RouteIO)
|
||||
|
||||
if !ok {
|
||||
return nil, errors.New("sip.dtmf.server unable to get router from context")
|
||||
}
|
||||
return &SIPDTMFServer{config: config, ctx: ctx, router: router, IP: ipString, Port: int(portNum), Transport: transportString, Separator: separatorString, logger: CreateLogger(config)}, nil
|
||||
},
|
||||
})
|
||||
}
|
||||
@@ -131,7 +146,7 @@ func (sds *SIPDTMFServer) Run() error {
|
||||
}
|
||||
|
||||
<-sds.ctx.Done()
|
||||
slog.Debug("router context done in module", "id", sds.Id())
|
||||
sds.logger.Debug("done")
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -142,10 +157,14 @@ func (sds *SIPDTMFServer) HandleCall(inDialog *diago.DialogServerSession) error
|
||||
|
||||
reader := inDialog.AudioReaderDTMF()
|
||||
userString := ""
|
||||
|
||||
return reader.Listen(func(dtmf rune) error {
|
||||
if dtmf == rune(sds.Separator[0]) {
|
||||
if sds.router != nil {
|
||||
sds.router.HandleInput(sds.Id(), SIPDTMFMessage{
|
||||
dialogContext := context.WithValue(sds.ctx, sipCallContextKey("call"), &SIPDTMFCall{
|
||||
inDialog: inDialog,
|
||||
})
|
||||
sds.router.HandleInput(dialogContext, sds.Id(), SIPDTMFMessage{
|
||||
To: inDialog.ToUser(),
|
||||
Digits: userString,
|
||||
})
|
||||
@@ -158,6 +177,66 @@ func (sds *SIPDTMFServer) HandleCall(inDialog *diago.DialogServerSession) error
|
||||
}, 5*time.Second)
|
||||
}
|
||||
|
||||
func (sds *SIPDTMFServer) Output(payload any) error {
|
||||
return fmt.Errorf("sip.dtmf.server output is not implemented")
|
||||
func (sds *SIPDTMFServer) Output(ctx context.Context, payload any) error {
|
||||
call, ok := ctx.Value(sipCallContextKey("call")).(*SIPDTMFCall)
|
||||
|
||||
if !ok {
|
||||
return errors.New("sip.dtmf.server output must originate from sip.dtmf.server input")
|
||||
}
|
||||
|
||||
gotLock := call.lock.TryLock()
|
||||
|
||||
if !gotLock {
|
||||
return errors.New("sip.dtmf.server call is already locked")
|
||||
}
|
||||
|
||||
if call.inDialog.LoadState() == sip.DialogStateEnded {
|
||||
return errors.New("sip.dtmf.server inDialog already ended")
|
||||
}
|
||||
|
||||
payloadDTMFResponse, ok := payload.(processor.SipDTMFResponse)
|
||||
|
||||
if ok {
|
||||
dtmfWriter := call.inDialog.AudioWriterDTMF()
|
||||
|
||||
time.Sleep(time.Millisecond * time.Duration(payloadDTMFResponse.PreWait))
|
||||
for i, dtmfRune := range payloadDTMFResponse.Digits {
|
||||
err := dtmfWriter.WriteDTMF(dtmfRune)
|
||||
|
||||
if err != nil {
|
||||
return fmt.Errorf("sip.dtmf.server error output dtmf digit at index %d", i)
|
||||
}
|
||||
}
|
||||
time.Sleep(time.Millisecond * time.Duration(payloadDTMFResponse.PreWait))
|
||||
return nil
|
||||
}
|
||||
|
||||
payloadAudioFileResponse, ok := payload.(processor.SipAudioFileResponse)
|
||||
|
||||
if ok {
|
||||
audioFile, err := os.Open(payloadAudioFileResponse.AudioFile)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer audioFile.Close()
|
||||
|
||||
playback, err := call.inDialog.PlaybackCreate()
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
time.Sleep(time.Millisecond * time.Duration(payloadAudioFileResponse.PreWait))
|
||||
|
||||
_, err = playback.Play(audioFile, "audio/wav")
|
||||
|
||||
time.Sleep(time.Millisecond * time.Duration(payloadAudioFileResponse.PostWait))
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
return errors.New("sip.dtmf.server can only output SipDTMFResponse or SipAudioFileResponse")
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@ package module
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"net"
|
||||
@@ -19,34 +20,35 @@ type TCPClient struct {
|
||||
ctx context.Context
|
||||
router route.RouteIO
|
||||
Addr *net.TCPAddr
|
||||
logger *slog.Logger
|
||||
}
|
||||
|
||||
func init() {
|
||||
RegisterModule(ModuleRegistration{
|
||||
Type: "net.tcp.client",
|
||||
New: func(ctx context.Context, config config.ModuleConfig, router route.RouteIO) (Module, error) {
|
||||
New: func(ctx context.Context, config config.ModuleConfig) (Module, error) {
|
||||
params := config.Params
|
||||
host, ok := params["host"]
|
||||
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("net.tcp.client requires a host parameter")
|
||||
return nil, errors.New("net.tcp.client requires a host parameter")
|
||||
}
|
||||
|
||||
hostString, ok := host.(string)
|
||||
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("net.tcp.client host must be string")
|
||||
return nil, errors.New("net.tcp.client host must be string")
|
||||
}
|
||||
|
||||
port, ok := params["port"]
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("net.tcp.client requires a port parameter")
|
||||
return nil, errors.New("net.tcp.client requires a port parameter")
|
||||
}
|
||||
|
||||
portNum, ok := port.(float64)
|
||||
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("net.tcp.client port must be a number")
|
||||
return nil, errors.New("net.tcp.client port must be a number")
|
||||
}
|
||||
|
||||
addr, err := net.ResolveTCPAddr("tcp", fmt.Sprintf("%s:%d", hostString, uint16(portNum)))
|
||||
@@ -62,18 +64,24 @@ func init() {
|
||||
framingMethodString, ok := framingMethodRaw.(string)
|
||||
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("misc.serial.client framing method must be a string")
|
||||
return nil, errors.New("net.tcp.client framing method must be a string")
|
||||
}
|
||||
framingMethod = framingMethodString
|
||||
}
|
||||
|
||||
framer, err := framer.GetFramer(framingMethod)
|
||||
framer := framer.GetFramer(framingMethod)
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
if framer == nil {
|
||||
return nil, fmt.Errorf("net.tcp.client unknown framing method: %s", framingMethod)
|
||||
}
|
||||
|
||||
return &TCPClient{framer: framer, Addr: addr, config: config, ctx: ctx, router: router}, nil
|
||||
router, ok := ctx.Value(route.RouterContextKey).(route.RouteIO)
|
||||
|
||||
if !ok {
|
||||
return nil, errors.New("net.tcp.client unable to get router from context")
|
||||
}
|
||||
|
||||
return &TCPClient{framer: framer, Addr: addr, config: config, ctx: ctx, router: router, logger: CreateLogger(config)}, nil
|
||||
},
|
||||
})
|
||||
}
|
||||
@@ -91,7 +99,7 @@ func (tc *TCPClient) Run() error {
|
||||
// TODO(jwetzell): shutdown with router.Context properly
|
||||
go func() {
|
||||
<-tc.ctx.Done()
|
||||
slog.Debug("router context done in module", "id", tc.Id())
|
||||
tc.logger.Debug("done")
|
||||
if tc.conn != nil {
|
||||
tc.conn.Close()
|
||||
}
|
||||
@@ -101,10 +109,10 @@ func (tc *TCPClient) Run() error {
|
||||
err := tc.SetupConn()
|
||||
if err != nil {
|
||||
if tc.ctx.Err() != nil {
|
||||
slog.Debug("router context done in module", "id", tc.Id())
|
||||
tc.logger.Debug("done")
|
||||
return nil
|
||||
}
|
||||
slog.Error("net.tcp.client", "id", tc.Id(), "error", err.Error())
|
||||
tc.logger.Error("connection error", "error", err.Error())
|
||||
time.Sleep(time.Second * 2)
|
||||
continue
|
||||
}
|
||||
@@ -112,14 +120,14 @@ func (tc *TCPClient) Run() error {
|
||||
buffer := make([]byte, 1024)
|
||||
select {
|
||||
case <-tc.ctx.Done():
|
||||
slog.Debug("router context done in module", "id", tc.Id())
|
||||
tc.logger.Debug("done")
|
||||
return nil
|
||||
default:
|
||||
READ:
|
||||
for {
|
||||
select {
|
||||
case <-tc.ctx.Done():
|
||||
slog.Debug("router context done in module", "id", tc.Id())
|
||||
tc.logger.Debug("done")
|
||||
return nil
|
||||
default:
|
||||
byteCount, err := tc.conn.Read(buffer)
|
||||
@@ -134,9 +142,9 @@ func (tc *TCPClient) Run() error {
|
||||
messages := tc.framer.Decode(buffer[0:byteCount])
|
||||
for _, message := range messages {
|
||||
if tc.router != nil {
|
||||
tc.router.HandleInput(tc.Id(), message)
|
||||
tc.router.HandleInput(tc.ctx, tc.Id(), message)
|
||||
} else {
|
||||
slog.Error("net.tcp.client has no router", "id", tc.Id())
|
||||
tc.logger.Error("input received but no router is configured")
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -153,7 +161,7 @@ func (tc *TCPClient) SetupConn() error {
|
||||
return err
|
||||
}
|
||||
|
||||
func (tc *TCPClient) Output(payload any) error {
|
||||
func (tc *TCPClient) Output(ctx context.Context, payload any) error {
|
||||
// NOTE(jwetzell): not sure how this would occur but
|
||||
if tc.conn == nil {
|
||||
err := tc.SetupConn()
|
||||
@@ -163,7 +171,7 @@ func (tc *TCPClient) Output(payload any) error {
|
||||
}
|
||||
payloadBytes, ok := payload.([]byte)
|
||||
if !ok {
|
||||
return fmt.Errorf("net.tcp.client is only able to output bytes")
|
||||
return errors.New("net.tcp.client is only able to output bytes")
|
||||
}
|
||||
_, err := tc.conn.Write(tc.framer.Encode(payloadBytes))
|
||||
return err
|
||||
|
||||
@@ -26,22 +26,23 @@ type TCPServer struct {
|
||||
wg sync.WaitGroup
|
||||
connections []*net.TCPConn
|
||||
connectionsMu sync.RWMutex
|
||||
logger *slog.Logger
|
||||
}
|
||||
|
||||
func init() {
|
||||
RegisterModule(ModuleRegistration{
|
||||
Type: "net.tcp.server",
|
||||
New: func(ctx context.Context, config config.ModuleConfig, router route.RouteIO) (Module, error) {
|
||||
New: func(ctx context.Context, config config.ModuleConfig) (Module, error) {
|
||||
params := config.Params
|
||||
port, ok := params["port"]
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("net.tcp.server requires a port parameter")
|
||||
return nil, errors.New("net.tcp.server requires a port parameter")
|
||||
}
|
||||
|
||||
portNum, ok := port.(float64)
|
||||
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("net.tcp.server port must be a number")
|
||||
return nil, errors.New("net.tcp.server port must be a number")
|
||||
}
|
||||
|
||||
framingMethod := "RAW"
|
||||
@@ -52,15 +53,15 @@ func init() {
|
||||
framingMethodString, ok := framingMethodRaw.(string)
|
||||
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("misc.serial.client framing method must be a string")
|
||||
return nil, errors.New("net.tcp.server framing method must be a string")
|
||||
}
|
||||
framingMethod = framingMethodString
|
||||
}
|
||||
|
||||
framer, err := framer.GetFramer(framingMethod)
|
||||
framer := framer.GetFramer(framingMethod)
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
if framer == nil {
|
||||
return nil, fmt.Errorf("net.tcp.server unknown framing method: %s", framingMethod)
|
||||
}
|
||||
|
||||
ipString := "0.0.0.0"
|
||||
@@ -71,7 +72,7 @@ func init() {
|
||||
specificIpString, ok := ip.(string)
|
||||
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("net.tcp.server ip must be a string")
|
||||
return nil, errors.New("net.tcp.server ip must be a string")
|
||||
}
|
||||
ipString = specificIpString
|
||||
}
|
||||
@@ -81,7 +82,13 @@ func init() {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &TCPServer{Framer: framer, Addr: addr, config: config, quit: make(chan interface{}), ctx: ctx, router: router}, nil
|
||||
router, ok := ctx.Value(route.RouterContextKey).(route.RouteIO)
|
||||
|
||||
if !ok {
|
||||
return nil, errors.New("net.tcp.server unable to get router from context")
|
||||
}
|
||||
|
||||
return &TCPServer{Framer: framer, Addr: addr, config: config, quit: make(chan interface{}), ctx: ctx, router: router, logger: CreateLogger(config)}, nil
|
||||
},
|
||||
})
|
||||
}
|
||||
@@ -98,7 +105,7 @@ func (ts *TCPServer) handleClient(client *net.TCPConn) {
|
||||
ts.connectionsMu.Lock()
|
||||
ts.connections = append(ts.connections, client)
|
||||
ts.connectionsMu.Unlock()
|
||||
slog.Debug("net.tcp.server connection accepted", "id", ts.Id(), "remoteAddr", client.RemoteAddr().String())
|
||||
ts.logger.Debug("net.tcp.server connection accepted", "remoteAddr", client.RemoteAddr().String())
|
||||
defer client.Close()
|
||||
|
||||
buffer := make([]byte, 1024)
|
||||
@@ -125,7 +132,7 @@ ClientRead:
|
||||
break
|
||||
}
|
||||
}
|
||||
slog.Debug("net.tcp.server connection reset", "id", ts.Id(), "remoteAddr", client.RemoteAddr().String())
|
||||
ts.logger.Debug("connection reset", "remoteAddr", client.RemoteAddr().String())
|
||||
ts.connectionsMu.Unlock()
|
||||
}
|
||||
}
|
||||
@@ -138,7 +145,7 @@ ClientRead:
|
||||
break
|
||||
}
|
||||
}
|
||||
slog.Debug("net.tcp.server stream ended", "id", ts.Id(), "remoteAddr", client.RemoteAddr().String())
|
||||
ts.logger.Debug("stream ended", "remoteAddr", client.RemoteAddr().String())
|
||||
ts.connectionsMu.Unlock()
|
||||
}
|
||||
return
|
||||
@@ -148,9 +155,9 @@ ClientRead:
|
||||
messages := ts.Framer.Decode(buffer[0:byteCount])
|
||||
for _, message := range messages {
|
||||
if ts.router != nil {
|
||||
ts.router.HandleInput(ts.Id(), message)
|
||||
ts.router.HandleInput(ts.ctx, ts.Id(), message)
|
||||
} else {
|
||||
slog.Error("net.tcp.server has no router", "id", ts.Id())
|
||||
ts.logger.Error("input received but no router is configured")
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -170,7 +177,7 @@ func (ts *TCPServer) Run() error {
|
||||
<-ts.ctx.Done()
|
||||
close(ts.quit)
|
||||
listener.Close()
|
||||
slog.Debug("router context done in module", "id", ts.Id())
|
||||
ts.logger.Debug("done")
|
||||
}()
|
||||
|
||||
AcceptLoop:
|
||||
@@ -181,7 +188,7 @@ AcceptLoop:
|
||||
case <-ts.quit:
|
||||
break AcceptLoop
|
||||
default:
|
||||
slog.Debug("net.tcp.server problem with listener", "error", err)
|
||||
ts.logger.Debug("problem with listener", "error", err)
|
||||
}
|
||||
} else {
|
||||
ts.wg.Add(1)
|
||||
@@ -196,11 +203,11 @@ AcceptLoop:
|
||||
return nil
|
||||
}
|
||||
|
||||
func (ts *TCPServer) Output(payload any) error {
|
||||
func (ts *TCPServer) Output(ctx context.Context, payload any) error {
|
||||
payloadBytes, ok := payload.([]byte)
|
||||
|
||||
if !ok {
|
||||
return fmt.Errorf("net.tcp.server is only able to output bytes")
|
||||
return errors.New("net.tcp.server is only able to output bytes")
|
||||
}
|
||||
ts.connectionsMu.Lock()
|
||||
errorString := ""
|
||||
@@ -216,5 +223,5 @@ func (ts *TCPServer) Output(payload any) error {
|
||||
if errorString == "" {
|
||||
return nil
|
||||
}
|
||||
return fmt.Errorf("%s", errorString)
|
||||
return fmt.Errorf("net.tcp.server error during output: %s", errorString)
|
||||
}
|
||||
|
||||
80
internal/module/time-interval.go
Normal file
80
internal/module/time-interval.go
Normal file
@@ -0,0 +1,80 @@
|
||||
package module
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"log/slog"
|
||||
"time"
|
||||
|
||||
"github.com/jwetzell/showbridge-go/internal/config"
|
||||
"github.com/jwetzell/showbridge-go/internal/route"
|
||||
)
|
||||
|
||||
type TimeInterval struct {
|
||||
config config.ModuleConfig
|
||||
Duration uint32
|
||||
ctx context.Context
|
||||
router route.RouteIO
|
||||
ticker *time.Ticker
|
||||
logger *slog.Logger
|
||||
}
|
||||
|
||||
func init() {
|
||||
RegisterModule(ModuleRegistration{
|
||||
Type: "time.interval",
|
||||
New: func(ctx context.Context, config config.ModuleConfig) (Module, error) {
|
||||
params := config.Params
|
||||
|
||||
duration, ok := params["duration"]
|
||||
if !ok {
|
||||
return nil, errors.New("time.interval requires a duration parameter")
|
||||
}
|
||||
|
||||
durationNum, ok := duration.(float64)
|
||||
|
||||
if !ok {
|
||||
return nil, errors.New("time.interval duration must be number")
|
||||
}
|
||||
|
||||
router, ok := ctx.Value(route.RouterContextKey).(route.RouteIO)
|
||||
|
||||
if !ok {
|
||||
return nil, errors.New("time.interval unable to get router from context")
|
||||
}
|
||||
|
||||
return &TimeInterval{Duration: uint32(durationNum), config: config, ctx: ctx, router: router, logger: CreateLogger(config)}, nil
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
func (i *TimeInterval) Id() string {
|
||||
return i.config.Id
|
||||
}
|
||||
|
||||
func (i *TimeInterval) Type() string {
|
||||
return i.config.Type
|
||||
}
|
||||
|
||||
func (i *TimeInterval) Run() error {
|
||||
ticker := time.NewTicker(time.Millisecond * time.Duration(i.Duration))
|
||||
i.ticker = ticker
|
||||
defer ticker.Stop()
|
||||
|
||||
for {
|
||||
select {
|
||||
case <-i.ctx.Done():
|
||||
i.logger.Debug("done")
|
||||
return nil
|
||||
case <-ticker.C:
|
||||
if i.router != nil {
|
||||
i.router.HandleInput(i.ctx, i.Id(), time.Now())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func (i *TimeInterval) Output(ctx context.Context, payload any) error {
|
||||
i.ticker.Reset(time.Millisecond * time.Duration(i.Duration))
|
||||
return nil
|
||||
}
|
||||
@@ -2,7 +2,7 @@ package module
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"errors"
|
||||
"log/slog"
|
||||
"time"
|
||||
|
||||
@@ -10,62 +10,69 @@ import (
|
||||
"github.com/jwetzell/showbridge-go/internal/route"
|
||||
)
|
||||
|
||||
type Timer struct {
|
||||
type TimeTimer struct {
|
||||
config config.ModuleConfig
|
||||
Duration uint32
|
||||
ctx context.Context
|
||||
router route.RouteIO
|
||||
timer *time.Timer
|
||||
logger *slog.Logger
|
||||
}
|
||||
|
||||
func init() {
|
||||
RegisterModule(ModuleRegistration{
|
||||
Type: "gen.timer",
|
||||
New: func(ctx context.Context, config config.ModuleConfig, router route.RouteIO) (Module, error) {
|
||||
Type: "time.timer",
|
||||
New: func(ctx context.Context, config config.ModuleConfig) (Module, error) {
|
||||
params := config.Params
|
||||
|
||||
duration, ok := params["duration"]
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("gen.timer requires a duration parameter")
|
||||
return nil, errors.New("time.timer requires a duration parameter")
|
||||
}
|
||||
|
||||
durationNum, ok := duration.(float64)
|
||||
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("gen.timer duration must be a number")
|
||||
return nil, errors.New("time.timer duration must be a number")
|
||||
}
|
||||
|
||||
return &Timer{Duration: uint32(durationNum), config: config, ctx: ctx, router: router}, nil
|
||||
router, ok := ctx.Value(route.RouterContextKey).(route.RouteIO)
|
||||
|
||||
if !ok {
|
||||
return nil, errors.New("net.tcp.client unable to get router from context")
|
||||
}
|
||||
|
||||
return &TimeTimer{Duration: uint32(durationNum), config: config, ctx: ctx, router: router, logger: CreateLogger(config)}, nil
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
func (t *Timer) Id() string {
|
||||
func (t *TimeTimer) Id() string {
|
||||
return t.config.Id
|
||||
}
|
||||
|
||||
func (t *Timer) Type() string {
|
||||
func (t *TimeTimer) Type() string {
|
||||
return t.config.Type
|
||||
}
|
||||
|
||||
func (t *Timer) Run() error {
|
||||
func (t *TimeTimer) Run() error {
|
||||
t.timer = time.NewTimer(time.Millisecond * time.Duration(t.Duration))
|
||||
defer t.timer.Stop()
|
||||
for {
|
||||
select {
|
||||
case <-t.ctx.Done():
|
||||
t.timer.Stop()
|
||||
slog.Debug("router context done in module", "id", t.Id())
|
||||
t.logger.Debug("done")
|
||||
return nil
|
||||
case time := <-t.timer.C:
|
||||
if t.router != nil {
|
||||
t.router.HandleInput(t.Id(), time)
|
||||
t.router.HandleInput(t.ctx, t.Id(), time)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (t *Timer) Output(payload any) error {
|
||||
func (t *TimeTimer) Output(ctx context.Context, payload any) error {
|
||||
t.timer.Reset(time.Millisecond * time.Duration(t.Duration))
|
||||
return nil
|
||||
}
|
||||
@@ -2,6 +2,7 @@ package module
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"net"
|
||||
@@ -17,34 +18,35 @@ type UDPClient struct {
|
||||
conn *net.UDPConn
|
||||
ctx context.Context
|
||||
router route.RouteIO
|
||||
logger *slog.Logger
|
||||
}
|
||||
|
||||
func init() {
|
||||
RegisterModule(ModuleRegistration{
|
||||
Type: "net.udp.client",
|
||||
New: func(ctx context.Context, config config.ModuleConfig, router route.RouteIO) (Module, error) {
|
||||
New: func(ctx context.Context, config config.ModuleConfig) (Module, error) {
|
||||
params := config.Params
|
||||
host, ok := params["host"]
|
||||
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("net.udp.client requires a host parameter")
|
||||
return nil, errors.New("net.udp.client requires a host parameter")
|
||||
}
|
||||
|
||||
hostString, ok := host.(string)
|
||||
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("net.udp.client host must be a string")
|
||||
return nil, errors.New("net.udp.client host must be a string")
|
||||
}
|
||||
|
||||
port, ok := params["port"]
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("net.udp.client requires a port parameter")
|
||||
return nil, errors.New("net.udp.client requires a port parameter")
|
||||
}
|
||||
|
||||
portNum, ok := port.(float64)
|
||||
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("net.udp.client port must be a number")
|
||||
return nil, errors.New("net.udp.client port must be a number")
|
||||
}
|
||||
|
||||
addr, err := net.ResolveUDPAddr("udp", fmt.Sprintf("%s:%d", hostString, uint16(portNum)))
|
||||
@@ -52,7 +54,13 @@ func init() {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &UDPClient{Addr: addr, config: config, ctx: ctx, router: router}, nil
|
||||
router, ok := ctx.Value(route.RouterContextKey).(route.RouteIO)
|
||||
|
||||
if !ok {
|
||||
return nil, errors.New("net.udp.client unable to get router from context")
|
||||
}
|
||||
|
||||
return &UDPClient{Addr: addr, config: config, ctx: ctx, router: router, logger: CreateLogger(config)}, nil
|
||||
},
|
||||
})
|
||||
}
|
||||
@@ -79,18 +87,18 @@ func (uc *UDPClient) Run() error {
|
||||
}
|
||||
|
||||
<-uc.ctx.Done()
|
||||
slog.Debug("router context done in module", "id", uc.Id())
|
||||
uc.logger.Debug("done")
|
||||
if uc.conn != nil {
|
||||
uc.conn.Close()
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (uc *UDPClient) Output(payload any) error {
|
||||
func (uc *UDPClient) Output(ctx context.Context, payload any) error {
|
||||
|
||||
payloadBytes, ok := payload.([]byte)
|
||||
if !ok {
|
||||
return fmt.Errorf("net.udp.client is only able to output bytes")
|
||||
return errors.New("net.udp.client is only able to output bytes")
|
||||
}
|
||||
if uc.conn != nil {
|
||||
_, err := uc.conn.Write(payloadBytes)
|
||||
@@ -99,7 +107,7 @@ func (uc *UDPClient) Output(payload any) error {
|
||||
return err
|
||||
}
|
||||
} else {
|
||||
return fmt.Errorf("net.udp.client client is not setup")
|
||||
return errors.New("net.udp.client client is not setup")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@ package module
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"net"
|
||||
@@ -17,41 +18,48 @@ type UDPMulticast struct {
|
||||
ctx context.Context
|
||||
router route.RouteIO
|
||||
Addr *net.UDPAddr
|
||||
logger *slog.Logger
|
||||
}
|
||||
|
||||
func init() {
|
||||
RegisterModule(ModuleRegistration{
|
||||
Type: "net.udp.multicast",
|
||||
New: func(ctx context.Context, config config.ModuleConfig, router route.RouteIO) (Module, error) {
|
||||
New: func(ctx context.Context, config config.ModuleConfig) (Module, error) {
|
||||
params := config.Params
|
||||
ip, ok := params["ip"]
|
||||
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("net.udp.client requires am ip parameter")
|
||||
return nil, errors.New("net.udp.multicast requires an ip parameter")
|
||||
}
|
||||
|
||||
ipString, ok := ip.(string)
|
||||
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("net.udp.client ip must be a string")
|
||||
return nil, errors.New("net.udp.multicast ip must be a string")
|
||||
}
|
||||
|
||||
port, ok := params["port"]
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("net.udp.client requires a port parameter")
|
||||
return nil, errors.New("net.udp.multicast requires a port parameter")
|
||||
}
|
||||
|
||||
portNum, ok := port.(float64)
|
||||
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("net.udp.client port must be a number")
|
||||
return nil, errors.New("net.udp.multicast port must be a number")
|
||||
}
|
||||
|
||||
addr, err := net.ResolveUDPAddr("udp", fmt.Sprintf("%s:%d", ipString, uint16(portNum)))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &UDPMulticast{config: config, Addr: addr, ctx: ctx, router: router}, nil
|
||||
|
||||
router, ok := ctx.Value(route.RouterContextKey).(route.RouteIO)
|
||||
|
||||
if !ok {
|
||||
return nil, errors.New("net.udp.multicast unable to get router from context")
|
||||
}
|
||||
return &UDPMulticast{config: config, Addr: addr, ctx: ctx, router: router, logger: CreateLogger(config)}, nil
|
||||
},
|
||||
})
|
||||
}
|
||||
@@ -79,7 +87,7 @@ func (um *UDPMulticast) Run() error {
|
||||
select {
|
||||
case <-um.ctx.Done():
|
||||
// TODO(jwetzell): cleanup?
|
||||
slog.Debug("router context done in module", "id", um.Id())
|
||||
um.logger.Debug("done")
|
||||
return nil
|
||||
default:
|
||||
um.conn.SetDeadline(time.Now().Add(time.Millisecond * 200))
|
||||
@@ -97,24 +105,24 @@ func (um *UDPMulticast) Run() error {
|
||||
message := buffer[:numBytes]
|
||||
|
||||
if um.router != nil {
|
||||
um.router.HandleInput(um.Id(), message)
|
||||
um.router.HandleInput(um.ctx, um.Id(), message)
|
||||
} else {
|
||||
slog.Error("net.udp.multicast has no router", "id", um.Id())
|
||||
um.logger.Error("input received but no router is configured")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (um *UDPMulticast) Output(payload any) error {
|
||||
func (um *UDPMulticast) Output(ctx context.Context, payload any) error {
|
||||
|
||||
payloadBytes, ok := payload.([]byte)
|
||||
if !ok {
|
||||
return fmt.Errorf("net.udp.multicast can only output bytes")
|
||||
return errors.New("net.udp.multicast can only output bytes")
|
||||
}
|
||||
|
||||
if um.conn == nil {
|
||||
return fmt.Errorf("net.udp.multicast connection is not setup")
|
||||
return errors.New("net.udp.multicast connection is not setup")
|
||||
}
|
||||
|
||||
_, err := um.conn.Write(payloadBytes)
|
||||
|
||||
@@ -2,6 +2,7 @@ package module
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"log"
|
||||
"log/slog"
|
||||
@@ -14,25 +15,27 @@ import (
|
||||
|
||||
type UDPServer struct {
|
||||
Addr *net.UDPAddr
|
||||
BufferSize int
|
||||
config config.ModuleConfig
|
||||
ctx context.Context
|
||||
router route.RouteIO
|
||||
logger *slog.Logger
|
||||
}
|
||||
|
||||
func init() {
|
||||
RegisterModule(ModuleRegistration{
|
||||
Type: "net.udp.server",
|
||||
New: func(ctx context.Context, config config.ModuleConfig, router route.RouteIO) (Module, error) {
|
||||
New: func(ctx context.Context, config config.ModuleConfig) (Module, error) {
|
||||
params := config.Params
|
||||
port, ok := params["port"]
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("net.udp.server requires a port parameter")
|
||||
return nil, errors.New("net.udp.server requires a port parameter")
|
||||
}
|
||||
|
||||
portNum, ok := port.(float64)
|
||||
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("net.udp.server port must be a number")
|
||||
return nil, errors.New("net.udp.server port must be a number")
|
||||
}
|
||||
|
||||
ipString := "0.0.0.0"
|
||||
@@ -43,7 +46,7 @@ func init() {
|
||||
specificIpString, ok := ip.(string)
|
||||
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("net.udp.server ip must be a string")
|
||||
return nil, errors.New("net.udp.server ip must be a string")
|
||||
}
|
||||
ipString = specificIpString
|
||||
}
|
||||
@@ -53,7 +56,24 @@ func init() {
|
||||
log.Fatalf("error resolving UDP address: %v", err)
|
||||
}
|
||||
|
||||
return &UDPServer{Addr: addr, config: config, ctx: ctx, router: router}, nil
|
||||
bufferSizeNum := 2048
|
||||
bufferSize, ok := params["bufferSize"]
|
||||
|
||||
if ok {
|
||||
bufferSizeFloat, ok := bufferSize.(float64)
|
||||
|
||||
if !ok {
|
||||
return nil, errors.New("net.udp.server bufferSize must be a number")
|
||||
}
|
||||
bufferSizeNum = int(bufferSizeFloat)
|
||||
}
|
||||
|
||||
router, ok := ctx.Value(route.RouterContextKey).(route.RouteIO)
|
||||
|
||||
if !ok {
|
||||
return nil, errors.New("net.udp.server unable to get router from context")
|
||||
}
|
||||
return &UDPServer{Addr: addr, BufferSize: bufferSizeNum, config: config, ctx: ctx, router: router, logger: CreateLogger(config)}, nil
|
||||
},
|
||||
})
|
||||
}
|
||||
@@ -75,13 +95,12 @@ func (us *UDPServer) Run() error {
|
||||
|
||||
defer listener.Close()
|
||||
|
||||
// TODO(jwetzell): make buffer size configurable
|
||||
buffer := make([]byte, 65535)
|
||||
buffer := make([]byte, us.BufferSize)
|
||||
for {
|
||||
select {
|
||||
case <-us.ctx.Done():
|
||||
// TODO(jwetzell): cleanup?
|
||||
slog.Debug("router context done in module", "id", us.Id())
|
||||
us.logger.Debug("done")
|
||||
return nil
|
||||
default:
|
||||
listener.SetDeadline(time.Now().Add(time.Millisecond * 200))
|
||||
@@ -96,15 +115,15 @@ func (us *UDPServer) Run() error {
|
||||
}
|
||||
message := buffer[:numBytes]
|
||||
if us.router != nil {
|
||||
us.router.HandleInput(us.Id(), message)
|
||||
us.router.HandleInput(us.ctx, us.Id(), message)
|
||||
} else {
|
||||
slog.Error("net.udp.server has no router", "id", us.Id())
|
||||
us.logger.Error("input received but no router is configured")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func (us *UDPServer) Output(payload any) error {
|
||||
return fmt.Errorf("net.udp.server output is not implemented")
|
||||
func (us *UDPServer) Output(ctx context.Context, payload any) error {
|
||||
return errors.New("net.udp.server output is not implemented")
|
||||
}
|
||||
|
||||
42
internal/processor/artnet-packet-decode.go
Normal file
42
internal/processor/artnet-packet-decode.go
Normal file
@@ -0,0 +1,42 @@
|
||||
package processor
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"github.com/jwetzell/artnet-go"
|
||||
"github.com/jwetzell/showbridge-go/internal/config"
|
||||
)
|
||||
|
||||
type ArtNetPacketDecode struct {
|
||||
config config.ProcessorConfig
|
||||
}
|
||||
|
||||
func (apd *ArtNetPacketDecode) Process(ctx context.Context, payload any) (any, error) {
|
||||
payloadBytes, ok := payload.([]byte)
|
||||
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("artnet.packet.decode processor only accepts a []byte")
|
||||
}
|
||||
|
||||
payloadMessage, err := artnet.Decode(payloadBytes)
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return payloadMessage, nil
|
||||
}
|
||||
|
||||
func (apd *ArtNetPacketDecode) Type() string {
|
||||
return apd.config.Type
|
||||
}
|
||||
|
||||
func init() {
|
||||
RegisterProcessor(ProcessorRegistration{
|
||||
Type: "artnet.packet.decode",
|
||||
New: func(config config.ProcessorConfig) (Processor, error) {
|
||||
return &ArtNetPacketDecode{config: config}, nil
|
||||
},
|
||||
})
|
||||
}
|
||||
41
internal/processor/artnet-packet-encode.go
Normal file
41
internal/processor/artnet-packet-encode.go
Normal file
@@ -0,0 +1,41 @@
|
||||
package processor
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"github.com/jwetzell/artnet-go"
|
||||
"github.com/jwetzell/showbridge-go/internal/config"
|
||||
)
|
||||
|
||||
type ArtNetPacketEncode struct {
|
||||
config config.ProcessorConfig
|
||||
}
|
||||
|
||||
func (ape *ArtNetPacketEncode) Process(ctx context.Context, payload any) (any, error) {
|
||||
payloadPacket, ok := payload.(artnet.ArtNetPacket)
|
||||
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("artnet.packet.encode processor only accepts an ArtNetPacket")
|
||||
}
|
||||
|
||||
payloadBytes, err := payloadPacket.MarshalBinary()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return payloadBytes, nil
|
||||
}
|
||||
|
||||
func (ape *ArtNetPacketEncode) Type() string {
|
||||
return ape.config.Type
|
||||
}
|
||||
|
||||
func init() {
|
||||
RegisterProcessor(ProcessorRegistration{
|
||||
Type: "artnet.packet.encode",
|
||||
New: func(config config.ProcessorConfig) (Processor, error) {
|
||||
return &ArtNetPacketEncode{config: config}, nil
|
||||
},
|
||||
})
|
||||
}
|
||||
52
internal/processor/artnet-packet-filter.go
Normal file
52
internal/processor/artnet-packet-filter.go
Normal file
@@ -0,0 +1,52 @@
|
||||
package processor
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"github.com/jwetzell/artnet-go"
|
||||
"github.com/jwetzell/showbridge-go/internal/config"
|
||||
)
|
||||
|
||||
type ArtNetPacketFilter struct {
|
||||
config config.ProcessorConfig
|
||||
OpCode uint16
|
||||
}
|
||||
|
||||
func (apf *ArtNetPacketFilter) Process(ctx context.Context, payload any) (any, error) {
|
||||
payloadPacket, ok := payload.(artnet.ArtNetPacket)
|
||||
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("artnet.packet.filter processor only accepts an ArtNetPacket")
|
||||
}
|
||||
|
||||
if payloadPacket.GetOpCode() != apf.OpCode {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
return payloadPacket, nil
|
||||
}
|
||||
|
||||
func (apf *ArtNetPacketFilter) Type() string {
|
||||
return apf.config.Type
|
||||
}
|
||||
|
||||
func init() {
|
||||
RegisterProcessor(ProcessorRegistration{
|
||||
Type: "artnet.packet.filter",
|
||||
New: func(config config.ProcessorConfig) (Processor, error) {
|
||||
params := config.Params
|
||||
|
||||
opCode, ok := params["opCode"]
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("artnet.packet.filter requires an opCode parameter")
|
||||
}
|
||||
opCodeNum, ok := opCode.(float64)
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("artnet.packet.filter opCode must be a number")
|
||||
}
|
||||
|
||||
return &ArtNetPacketFilter{config: config, OpCode: uint16(opCodeNum)}, nil
|
||||
},
|
||||
})
|
||||
}
|
||||
@@ -10,10 +10,11 @@ import (
|
||||
|
||||
type DebugLog struct {
|
||||
config config.ProcessorConfig
|
||||
logger *slog.Logger
|
||||
}
|
||||
|
||||
func (dl *DebugLog) Process(ctx context.Context, payload any) (any, error) {
|
||||
slog.Debug("debug.log", "payload", payload, "payloadType", fmt.Sprintf("%T", payload))
|
||||
dl.logger.Debug("", "payload", payload, "payloadType", fmt.Sprintf("%T", payload))
|
||||
return payload, nil
|
||||
}
|
||||
|
||||
@@ -25,7 +26,7 @@ func init() {
|
||||
RegisterProcessor(ProcessorRegistration{
|
||||
Type: "debug.log",
|
||||
New: func(config config.ProcessorConfig) (Processor, error) {
|
||||
return &DebugLog{config: config}, nil
|
||||
return &DebugLog{config: config, logger: slog.Default().With("component", "processor", "type", config.Type)}, nil
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
39
internal/processor/debug-log_test.go
Normal file
39
internal/processor/debug-log_test.go
Normal file
@@ -0,0 +1,39 @@
|
||||
package processor_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/jwetzell/showbridge-go/internal/config"
|
||||
"github.com/jwetzell/showbridge-go/internal/processor"
|
||||
)
|
||||
|
||||
func TestDebugLogFromRegistry(t *testing.T) {
|
||||
registration, ok := processor.ProcessorRegistry["debug.log"]
|
||||
if !ok {
|
||||
t.Fatalf("debug.log processor not registered")
|
||||
}
|
||||
|
||||
processorInstance, err := registration.New(config.ProcessorConfig{
|
||||
Type: "debug.log",
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
t.Fatalf("failed to create debug.log processor: %s", err)
|
||||
}
|
||||
|
||||
if processorInstance.Type() != "debug.log" {
|
||||
t.Fatalf("debug.log processor has wrong type: %s", processorInstance.Type())
|
||||
}
|
||||
|
||||
payload := "test"
|
||||
expected := "test"
|
||||
|
||||
got, err := processorInstance.Process(t.Context(), payload)
|
||||
if err != nil {
|
||||
t.Fatalf("debug.log processing failed: %s", err)
|
||||
}
|
||||
|
||||
if got != expected {
|
||||
t.Fatalf("debug.log got %+v, expected %+v", got, expected)
|
||||
}
|
||||
}
|
||||
@@ -2,13 +2,14 @@ package processor
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"errors"
|
||||
"strconv"
|
||||
|
||||
"github.com/jwetzell/showbridge-go/internal/config"
|
||||
)
|
||||
|
||||
type FloatParse struct {
|
||||
BitSize int
|
||||
config config.ProcessorConfig
|
||||
}
|
||||
|
||||
@@ -16,11 +17,10 @@ func (fp *FloatParse) Process(ctx context.Context, payload any) (any, error) {
|
||||
payloadString, ok := payload.(string)
|
||||
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("float.parse processor only accepts a string")
|
||||
return nil, errors.New("float.parse processor only accepts a string")
|
||||
}
|
||||
|
||||
// TODO(jwetzell): make bitSize configurable
|
||||
payloadFloat, err := strconv.ParseFloat(payloadString, 64)
|
||||
payloadFloat, err := strconv.ParseFloat(payloadString, fp.BitSize)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -35,7 +35,19 @@ func init() {
|
||||
RegisterProcessor(ProcessorRegistration{
|
||||
Type: "float.parse",
|
||||
New: func(config config.ProcessorConfig) (Processor, error) {
|
||||
return &FloatParse{config: config}, nil
|
||||
params := config.Params
|
||||
bitSizeNum := 64
|
||||
bitSize, ok := params["bitSize"]
|
||||
if ok {
|
||||
bitSizeFloat, ok := bitSize.(float64)
|
||||
|
||||
if !ok {
|
||||
return nil, errors.New("float.parse bitSize must be a number")
|
||||
}
|
||||
|
||||
bitSizeNum = int(bitSizeFloat)
|
||||
}
|
||||
return &FloatParse{config: config, BitSize: bitSizeNum}, nil
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
@@ -3,30 +3,107 @@ package processor_test
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/jwetzell/showbridge-go/internal/config"
|
||||
"github.com/jwetzell/showbridge-go/internal/processor"
|
||||
)
|
||||
|
||||
func TestFloatParseFromRegistry(t *testing.T) {
|
||||
registration, ok := processor.ProcessorRegistry["float.parse"]
|
||||
if !ok {
|
||||
t.Fatalf("float.parse processor not registered")
|
||||
}
|
||||
|
||||
processorInstance, err := registration.New(config.ProcessorConfig{
|
||||
Type: "float.parse",
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
t.Fatalf("failed to create float.parse processor: %s", err)
|
||||
}
|
||||
|
||||
if processorInstance.Type() != "float.parse" {
|
||||
t.Fatalf("float.parse processor has wrong type: %s", processorInstance.Type())
|
||||
}
|
||||
}
|
||||
|
||||
func TestFloatParseBadConfigBitSizeString(t *testing.T) {
|
||||
registration, ok := processor.ProcessorRegistry["float.parse"]
|
||||
if !ok {
|
||||
t.Fatalf("float.parse processor not registered")
|
||||
}
|
||||
|
||||
_, err := registration.New(config.ProcessorConfig{
|
||||
Type: "float.parse",
|
||||
Params: map[string]any{
|
||||
"bitSize": "64",
|
||||
},
|
||||
})
|
||||
|
||||
if err == nil {
|
||||
t.Fatalf("float.parse should have returned an error for bad bitSize config")
|
||||
}
|
||||
}
|
||||
|
||||
func TestFloatParseGoodConfig(t *testing.T) {
|
||||
registration, ok := processor.ProcessorRegistry["float.parse"]
|
||||
if !ok {
|
||||
t.Fatalf("float.parse processor not registered")
|
||||
}
|
||||
|
||||
processorInstance, err := registration.New(config.ProcessorConfig{
|
||||
Type: "float.parse",
|
||||
Params: map[string]any{
|
||||
"bitSize": 64.0,
|
||||
},
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
t.Fatalf("float.parse should have created processor but got error: %s", err)
|
||||
}
|
||||
|
||||
payload := "12345.0"
|
||||
expected := float64(12345.0)
|
||||
|
||||
got, err := processorInstance.Process(t.Context(), payload)
|
||||
if err != nil {
|
||||
t.Fatalf("float.parse processing failed: %s", err)
|
||||
}
|
||||
|
||||
gotFloat, ok := got.(float64)
|
||||
if !ok {
|
||||
t.Fatalf("float.parse returned a %T payload: %s", got, got)
|
||||
}
|
||||
|
||||
if gotFloat != expected {
|
||||
t.Fatalf("float.parse got %f, expected %f", gotFloat, expected)
|
||||
}
|
||||
}
|
||||
|
||||
func TestGoodFloatParse(t *testing.T) {
|
||||
floatParser := processor.FloatParse{}
|
||||
tests := []struct {
|
||||
processor processor.Processor
|
||||
name string
|
||||
payload any
|
||||
bitSize int
|
||||
expected float64
|
||||
}{
|
||||
{
|
||||
name: "positive number",
|
||||
payload: "12345.67",
|
||||
bitSize: 64,
|
||||
expected: 12345.67,
|
||||
},
|
||||
{
|
||||
name: "negative number",
|
||||
payload: "-12345.67",
|
||||
bitSize: 64,
|
||||
expected: -12345.67,
|
||||
},
|
||||
{
|
||||
name: "zero",
|
||||
payload: "0",
|
||||
bitSize: 64,
|
||||
expected: 0,
|
||||
},
|
||||
}
|
||||
@@ -37,48 +114,59 @@ func TestGoodFloatParse(t *testing.T) {
|
||||
|
||||
gotFloat, ok := got.(float64)
|
||||
if !ok {
|
||||
t.Errorf("float.parse returned a %T payload: %s", got, got)
|
||||
t.Fatalf("float.parse returned a %T payload: %s", got, got)
|
||||
}
|
||||
if err != nil {
|
||||
t.Errorf("float.parse failed: %s", err)
|
||||
t.Fatalf("float.parse failed: %s", err)
|
||||
}
|
||||
if gotFloat != test.expected {
|
||||
t.Errorf("float.parse got %f, expected %f", gotFloat, test.expected)
|
||||
t.Fatalf("float.parse got %f, expected %f", gotFloat, test.expected)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestBadFloatParse(t *testing.T) {
|
||||
floatParser := processor.FloatParse{}
|
||||
tests := []struct {
|
||||
processor processor.Processor
|
||||
name string
|
||||
payload any
|
||||
bitSize int
|
||||
errorString string
|
||||
}{
|
||||
{
|
||||
name: "non-string input",
|
||||
payload: []byte{0x01},
|
||||
bitSize: 64,
|
||||
errorString: "float.parse processor only accepts a string",
|
||||
},
|
||||
{
|
||||
name: "not float string",
|
||||
payload: "abcd",
|
||||
bitSize: 64,
|
||||
errorString: "strconv.ParseFloat: parsing \"abcd\": invalid syntax",
|
||||
},
|
||||
{
|
||||
name: "bit size overflow",
|
||||
payload: "1.79e+64",
|
||||
bitSize: 32,
|
||||
errorString: "strconv.ParseFloat: parsing \"1.79e+64\": value out of range",
|
||||
},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
floatParser := processor.FloatParse{
|
||||
BitSize: test.bitSize,
|
||||
}
|
||||
got, err := floatParser.Process(t.Context(), test.payload)
|
||||
|
||||
if err == nil {
|
||||
t.Errorf("float.parse expected to fail but succeeded, got: %v", got)
|
||||
t.Fatalf("float.parse expected to fail but succeeded, got: %v", got)
|
||||
|
||||
}
|
||||
if err.Error() != test.errorString {
|
||||
t.Errorf("float.parse got error '%s', expected '%s'", err.Error(), test.errorString)
|
||||
t.Fatalf("float.parse got error '%s', expected '%s'", err.Error(), test.errorString)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
@@ -3,7 +3,7 @@ package processor
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"fmt"
|
||||
"errors"
|
||||
"strconv"
|
||||
"text/template"
|
||||
|
||||
@@ -190,13 +190,13 @@ func init() {
|
||||
id, ok := params["id"]
|
||||
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("freed.create requires an id parameter")
|
||||
return nil, errors.New("freed.create requires an id parameter")
|
||||
}
|
||||
|
||||
idString, ok := id.(string)
|
||||
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("freed.create id must be a string")
|
||||
return nil, errors.New("freed.create id must be a string")
|
||||
}
|
||||
|
||||
idTemplate, err := template.New("id").Parse(idString)
|
||||
@@ -208,13 +208,13 @@ func init() {
|
||||
pan, ok := params["pan"]
|
||||
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("freed.create requires an pan parameter")
|
||||
return nil, errors.New("freed.create requires a pan parameter")
|
||||
}
|
||||
|
||||
panString, ok := pan.(string)
|
||||
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("freed.create pan must be a string")
|
||||
return nil, errors.New("freed.create pan must be a string")
|
||||
}
|
||||
|
||||
panTemplate, err := template.New("pan").Parse(panString)
|
||||
@@ -222,13 +222,13 @@ func init() {
|
||||
tilt, ok := params["tilt"]
|
||||
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("freed.create requires an tilt parameter")
|
||||
return nil, errors.New("freed.create requires a tilt parameter")
|
||||
}
|
||||
|
||||
tiltString, ok := tilt.(string)
|
||||
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("freed.create tilt must be a string")
|
||||
return nil, errors.New("freed.create tilt must be a string")
|
||||
}
|
||||
|
||||
tiltTemplate, err := template.New("tilt").Parse(tiltString)
|
||||
@@ -236,13 +236,13 @@ func init() {
|
||||
roll, ok := params["roll"]
|
||||
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("freed.create requires an roll parameter")
|
||||
return nil, errors.New("freed.create requires a roll parameter")
|
||||
}
|
||||
|
||||
rollString, ok := roll.(string)
|
||||
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("freed.create roll must be a string")
|
||||
return nil, errors.New("freed.create roll must be a string")
|
||||
}
|
||||
|
||||
rollTemplate, err := template.New("roll").Parse(rollString)
|
||||
@@ -254,13 +254,13 @@ func init() {
|
||||
posX, ok := params["posX"]
|
||||
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("freed.create requires a posX parameter")
|
||||
return nil, errors.New("freed.create requires a posX parameter")
|
||||
}
|
||||
|
||||
posXString, ok := posX.(string)
|
||||
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("freed.create posX must be a string")
|
||||
return nil, errors.New("freed.create posX must be a string")
|
||||
}
|
||||
|
||||
posXTemplate, err := template.New("posX").Parse(posXString)
|
||||
@@ -272,13 +272,13 @@ func init() {
|
||||
posY, ok := params["posY"]
|
||||
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("freed.create requires a posY parameter")
|
||||
return nil, errors.New("freed.create requires a posY parameter")
|
||||
}
|
||||
|
||||
posYString, ok := posY.(string)
|
||||
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("freed.create posY must be a string")
|
||||
return nil, errors.New("freed.create posY must be a string")
|
||||
}
|
||||
|
||||
posYTemplate, err := template.New("posY").Parse(posYString)
|
||||
@@ -290,13 +290,13 @@ func init() {
|
||||
posZ, ok := params["posZ"]
|
||||
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("freed.create requires a posZ parameter")
|
||||
return nil, errors.New("freed.create requires a posZ parameter")
|
||||
}
|
||||
|
||||
posZString, ok := posZ.(string)
|
||||
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("freed.create posZ must be a string")
|
||||
return nil, errors.New("freed.create posZ must be a string")
|
||||
}
|
||||
|
||||
posZTemplate, err := template.New("posZ").Parse(posZString)
|
||||
@@ -308,13 +308,13 @@ func init() {
|
||||
zoom, ok := params["zoom"]
|
||||
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("freed.create requires an zoom parameter")
|
||||
return nil, errors.New("freed.create requires a zoom parameter")
|
||||
}
|
||||
|
||||
zoomString, ok := zoom.(string)
|
||||
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("freed.create zoom must be a string")
|
||||
return nil, errors.New("freed.create zoom must be a string")
|
||||
}
|
||||
|
||||
zoomTemplate, err := template.New("zoom").Parse(zoomString)
|
||||
@@ -322,13 +322,13 @@ func init() {
|
||||
focus, ok := params["focus"]
|
||||
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("freed.create requires an focus parameter")
|
||||
return nil, errors.New("freed.create requires a focus parameter")
|
||||
}
|
||||
|
||||
focusString, ok := focus.(string)
|
||||
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("freed.create focus must be a string")
|
||||
return nil, errors.New("freed.create focus must be a string")
|
||||
}
|
||||
|
||||
focusTemplate, err := template.New("focus").Parse(focusString)
|
||||
|
||||
@@ -2,8 +2,7 @@ package processor
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"errors"
|
||||
|
||||
freeD "github.com/jwetzell/free-d-go"
|
||||
"github.com/jwetzell/showbridge-go/internal/config"
|
||||
@@ -17,12 +16,12 @@ func (fdd *FreeDDecode) Process(ctx context.Context, payload any) (any, error) {
|
||||
payloadBytes, ok := payload.([]byte)
|
||||
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("freed.decode processor only accepts a []byte")
|
||||
return nil, errors.New("freed.decode processor only accepts a []byte")
|
||||
}
|
||||
|
||||
payloadMessage, err := freeD.Decode(payloadBytes)
|
||||
if err != nil {
|
||||
slog.Error("error decoding", "err", err)
|
||||
return nil, err
|
||||
}
|
||||
return payloadMessage, nil
|
||||
}
|
||||
|
||||
@@ -2,7 +2,7 @@ package processor
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"errors"
|
||||
|
||||
freeD "github.com/jwetzell/free-d-go"
|
||||
"github.com/jwetzell/showbridge-go/internal/config"
|
||||
@@ -16,7 +16,7 @@ func (fde *FreeDEncode) Process(ctx context.Context, payload any) (any, error) {
|
||||
payloadPosition, ok := payload.(freeD.FreeDPosition)
|
||||
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("freed.decode processor only accepts a FreeDEncode")
|
||||
return nil, errors.New("freed.decode processor only accepts a FreeDEncode")
|
||||
}
|
||||
|
||||
payloadBytes := freeD.Encode(payloadPosition)
|
||||
|
||||
@@ -3,7 +3,7 @@ package processor
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"fmt"
|
||||
"errors"
|
||||
"net/http"
|
||||
"text/template"
|
||||
|
||||
@@ -16,10 +16,10 @@ type HTTPRequestCreate struct {
|
||||
URL *template.Template
|
||||
}
|
||||
|
||||
func (hre *HTTPRequestCreate) Process(ctx context.Context, payload any) (any, error) {
|
||||
func (hrc *HTTPRequestCreate) Process(ctx context.Context, payload any) (any, error) {
|
||||
|
||||
var urlBuffer bytes.Buffer
|
||||
err := hre.URL.Execute(&urlBuffer, payload)
|
||||
err := hrc.URL.Execute(&urlBuffer, payload)
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -28,7 +28,7 @@ func (hre *HTTPRequestCreate) Process(ctx context.Context, payload any) (any, er
|
||||
urlString := urlBuffer.String()
|
||||
|
||||
//TODO(jwetzell): support body
|
||||
request, err := http.NewRequest(hre.Method, urlString, bytes.NewBuffer([]byte{}))
|
||||
request, err := http.NewRequest(hrc.Method, urlString, bytes.NewBuffer([]byte{}))
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -37,8 +37,8 @@ func (hre *HTTPRequestCreate) Process(ctx context.Context, payload any) (any, er
|
||||
return request, nil
|
||||
}
|
||||
|
||||
func (hre *HTTPRequestCreate) Type() string {
|
||||
return hre.config.Type
|
||||
func (hrc *HTTPRequestCreate) Type() string {
|
||||
return hrc.config.Type
|
||||
}
|
||||
|
||||
func init() {
|
||||
@@ -50,25 +50,25 @@ func init() {
|
||||
method, ok := params["method"]
|
||||
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("http.request.create requires an method parameter")
|
||||
return nil, errors.New("http.request.create requires a method parameter")
|
||||
}
|
||||
|
||||
methodString, ok := method.(string)
|
||||
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("http.request.create url must be a string")
|
||||
return nil, errors.New("http.request.create url must be a string")
|
||||
}
|
||||
|
||||
url, ok := params["url"]
|
||||
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("http.request.create requires an url parameter")
|
||||
return nil, errors.New("http.request.create requires a url parameter")
|
||||
}
|
||||
|
||||
urlString, ok := url.(string)
|
||||
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("http.request.create url must be a string")
|
||||
return nil, errors.New("http.request.create url must be a string")
|
||||
}
|
||||
|
||||
urlTemplate, err := template.New("url").Parse(urlString)
|
||||
|
||||
@@ -2,7 +2,7 @@ package processor
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"errors"
|
||||
"io"
|
||||
"net/http"
|
||||
|
||||
@@ -17,7 +17,7 @@ func (hre *HTTPRequestEncode) Process(ctx context.Context, payload any) (any, er
|
||||
payloadRequest, ok := payload.(*http.Request)
|
||||
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("http.request.encode processor only accepts an http.Request")
|
||||
return nil, errors.New("http.request.encode processor only accepts an http.Request")
|
||||
}
|
||||
|
||||
bytes, err := io.ReadAll(payloadRequest.Body)
|
||||
|
||||
@@ -2,6 +2,7 @@ package processor
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"regexp"
|
||||
@@ -20,7 +21,7 @@ func (hrf *HTTPRequestFilter) Process(ctx context.Context, payload any) (any, er
|
||||
payloadRequest, ok := payload.(*http.Request)
|
||||
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("http.request.filter can only operate on http.Request payloads")
|
||||
return nil, errors.New("http.request.filter can only operate on http.Request payloads")
|
||||
}
|
||||
|
||||
if hrf.Method != "" {
|
||||
@@ -48,13 +49,13 @@ func init() {
|
||||
path, ok := params["path"]
|
||||
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("http.request.filter requires an path parameter")
|
||||
return nil, errors.New("http.request.filter requires a path parameter")
|
||||
}
|
||||
|
||||
pathString, ok := path.(string)
|
||||
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("http.request.filter path must be a string")
|
||||
return nil, errors.New("http.request.filter path must be a string")
|
||||
}
|
||||
|
||||
pathRegexp, err := regexp.Compile(fmt.Sprintf("^%s$", pathString))
|
||||
@@ -69,7 +70,7 @@ func init() {
|
||||
methodString, ok := method.(string)
|
||||
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("http.request.filter method must be a string")
|
||||
return nil, errors.New("http.request.filter method must be a string")
|
||||
}
|
||||
return &HTTPRequestFilter{config: config, Path: pathRegexp, Method: methodString}, nil
|
||||
}
|
||||
|
||||
81
internal/processor/http-response-create.go
Normal file
81
internal/processor/http-response-create.go
Normal file
@@ -0,0 +1,81 @@
|
||||
package processor
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"errors"
|
||||
"text/template"
|
||||
|
||||
"github.com/jwetzell/showbridge-go/internal/config"
|
||||
)
|
||||
|
||||
type HTTPResponseCreate struct {
|
||||
Status int
|
||||
BodyTmpl *template.Template
|
||||
config config.ProcessorConfig
|
||||
}
|
||||
|
||||
type HTTPResponse struct {
|
||||
Status int
|
||||
Body []byte
|
||||
}
|
||||
|
||||
func (hre *HTTPResponseCreate) Process(ctx context.Context, payload any) (any, error) {
|
||||
var bodyBuffer bytes.Buffer
|
||||
err := hre.BodyTmpl.Execute(&bodyBuffer, payload)
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return HTTPResponse{
|
||||
Status: hre.Status,
|
||||
Body: bodyBuffer.Bytes(),
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (hre *HTTPResponseCreate) Type() string {
|
||||
return hre.config.Type
|
||||
}
|
||||
|
||||
func init() {
|
||||
RegisterProcessor(ProcessorRegistration{
|
||||
Type: "http.response.create",
|
||||
New: func(config config.ProcessorConfig) (Processor, error) {
|
||||
params := config.Params
|
||||
|
||||
status, ok := params["status"]
|
||||
|
||||
if !ok {
|
||||
return nil, errors.New("http.response.create requires a status parameter")
|
||||
}
|
||||
|
||||
statusNum, ok := status.(float64)
|
||||
|
||||
if !ok {
|
||||
return nil, errors.New("http.response.create status must be a number")
|
||||
}
|
||||
|
||||
bodyTmpl, ok := params["bodyTemplate"]
|
||||
|
||||
if !ok {
|
||||
return nil, errors.New("http.response.create requires a bodyTemplate parameter")
|
||||
}
|
||||
|
||||
bodyTemplateString, ok := bodyTmpl.(string)
|
||||
|
||||
if !ok {
|
||||
return nil, errors.New("http.response.create bodyTemplate must be a string")
|
||||
}
|
||||
|
||||
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
|
||||
},
|
||||
})
|
||||
}
|
||||
@@ -2,7 +2,7 @@ package processor
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"errors"
|
||||
"io"
|
||||
"net/http"
|
||||
|
||||
@@ -17,7 +17,7 @@ func (hre *HTTPResponseEncode) Process(ctx context.Context, payload any) (any, e
|
||||
payloadResponse, ok := payload.(*http.Response)
|
||||
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("http.response.encode processor only accepts an http.Response")
|
||||
return nil, errors.New("http.response.encode processor only accepts an http.Response")
|
||||
}
|
||||
defer payloadResponse.Body.Close()
|
||||
|
||||
|
||||
@@ -2,13 +2,15 @@ package processor
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"errors"
|
||||
"strconv"
|
||||
|
||||
"github.com/jwetzell/showbridge-go/internal/config"
|
||||
)
|
||||
|
||||
type IntParse struct {
|
||||
Base int
|
||||
BitSize int
|
||||
config config.ProcessorConfig
|
||||
}
|
||||
|
||||
@@ -16,11 +18,10 @@ func (ip *IntParse) Process(ctx context.Context, payload any) (any, error) {
|
||||
payloadString, ok := payload.(string)
|
||||
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("int.parse processor only accepts a string")
|
||||
return nil, errors.New("int.parse processor only accepts a string")
|
||||
}
|
||||
|
||||
// TODO(jwetzell): make base and bitSize configurable
|
||||
payloadInt, err := strconv.ParseInt(payloadString, 10, 64)
|
||||
payloadInt, err := strconv.ParseInt(payloadString, ip.Base, ip.BitSize)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -35,7 +36,32 @@ func init() {
|
||||
RegisterProcessor(ProcessorRegistration{
|
||||
Type: "int.parse",
|
||||
New: func(config config.ProcessorConfig) (Processor, error) {
|
||||
return &IntParse{config: config}, nil
|
||||
params := config.Params
|
||||
|
||||
baseNum := 10
|
||||
base, ok := params["base"]
|
||||
if ok {
|
||||
baseFloat, ok := base.(float64)
|
||||
|
||||
if !ok {
|
||||
return nil, errors.New("int.parse base must be a number")
|
||||
}
|
||||
|
||||
baseNum = int(baseFloat)
|
||||
}
|
||||
|
||||
bitSizeNum := 64
|
||||
bitSize, ok := params["bitSize"]
|
||||
if ok {
|
||||
bitSizeFloat, ok := bitSize.(float64)
|
||||
|
||||
if !ok {
|
||||
return nil, errors.New("int.parse bitSize must be a number")
|
||||
}
|
||||
|
||||
bitSizeNum = int(bitSizeFloat)
|
||||
}
|
||||
return &IntParse{config: config, Base: baseNum, BitSize: bitSizeNum}, nil
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
@@ -3,82 +3,216 @@ package processor_test
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/jwetzell/showbridge-go/internal/config"
|
||||
"github.com/jwetzell/showbridge-go/internal/processor"
|
||||
)
|
||||
|
||||
func TestIntParseFromRegistry(t *testing.T) {
|
||||
registration, ok := processor.ProcessorRegistry["int.parse"]
|
||||
if !ok {
|
||||
t.Fatalf("int.parse processor not registered")
|
||||
}
|
||||
|
||||
processorInstance, err := registration.New(config.ProcessorConfig{
|
||||
Type: "int.parse",
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
t.Fatalf("failed to create int.parse processor: %s", err)
|
||||
}
|
||||
|
||||
if processorInstance.Type() != "int.parse" {
|
||||
t.Fatalf("int.parse processor has wrong type: %s", processorInstance.Type())
|
||||
}
|
||||
}
|
||||
|
||||
func TestIntParseBadConfigBaseString(t *testing.T) {
|
||||
registration, ok := processor.ProcessorRegistry["int.parse"]
|
||||
if !ok {
|
||||
t.Fatalf("int.parse processor not registered")
|
||||
}
|
||||
|
||||
_, err := registration.New(config.ProcessorConfig{
|
||||
Type: "int.parse",
|
||||
Params: map[string]any{
|
||||
"base": "10",
|
||||
},
|
||||
})
|
||||
|
||||
if err == nil {
|
||||
t.Fatalf("int.parse should have returned an error for bad base config")
|
||||
}
|
||||
}
|
||||
|
||||
func TestIntParseBadConfigBitSizeString(t *testing.T) {
|
||||
registration, ok := processor.ProcessorRegistry["int.parse"]
|
||||
if !ok {
|
||||
t.Fatalf("int.parse processor not registered")
|
||||
}
|
||||
|
||||
_, err := registration.New(config.ProcessorConfig{
|
||||
Type: "int.parse",
|
||||
Params: map[string]any{
|
||||
"bitSize": "64",
|
||||
},
|
||||
})
|
||||
|
||||
if err == nil {
|
||||
t.Fatalf("int.parse should have returned an error for bad bitSize config")
|
||||
}
|
||||
}
|
||||
|
||||
func TestIntParseGoodConfig(t *testing.T) {
|
||||
registration, ok := processor.ProcessorRegistry["int.parse"]
|
||||
if !ok {
|
||||
t.Fatalf("int.parse processor not registered")
|
||||
}
|
||||
|
||||
processorInstance, err := registration.New(config.ProcessorConfig{
|
||||
Type: "int.parse",
|
||||
Params: map[string]any{
|
||||
"base": 10.0,
|
||||
"bitSize": 64.0,
|
||||
},
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
t.Fatalf("int.parse should have created processor but got error: %s", err)
|
||||
}
|
||||
|
||||
payload := "12345"
|
||||
expected := int64(12345)
|
||||
|
||||
got, err := processorInstance.Process(t.Context(), payload)
|
||||
if err != nil {
|
||||
t.Fatalf("int.parse processing failed: %s", err)
|
||||
}
|
||||
|
||||
gotInt, ok := got.(int64)
|
||||
if !ok {
|
||||
t.Fatalf("int.parse returned a %T payload: %s", got, got)
|
||||
}
|
||||
|
||||
if gotInt != expected {
|
||||
t.Fatalf("int.parse got %d, expected %d", gotInt, expected)
|
||||
}
|
||||
}
|
||||
|
||||
func TestGoodIntParse(t *testing.T) {
|
||||
intParser := processor.IntParse{}
|
||||
tests := []struct {
|
||||
processor processor.Processor
|
||||
name string
|
||||
payload any
|
||||
expected int64
|
||||
base int
|
||||
bitSize int
|
||||
}{
|
||||
{
|
||||
name: "positive number",
|
||||
payload: "12345",
|
||||
expected: 12345,
|
||||
base: 10,
|
||||
bitSize: 64,
|
||||
},
|
||||
{
|
||||
name: "negative number",
|
||||
payload: "-12345",
|
||||
expected: -12345,
|
||||
base: 10,
|
||||
bitSize: 64,
|
||||
},
|
||||
{
|
||||
name: "zero",
|
||||
payload: "0",
|
||||
expected: 0,
|
||||
base: 10,
|
||||
bitSize: 64,
|
||||
},
|
||||
{
|
||||
name: "binary",
|
||||
payload: "1010101",
|
||||
expected: 85,
|
||||
base: 2,
|
||||
bitSize: 64,
|
||||
},
|
||||
{
|
||||
name: "hex",
|
||||
payload: "15F",
|
||||
expected: 351,
|
||||
base: 16,
|
||||
bitSize: 64,
|
||||
},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
intParser := processor.IntParse{
|
||||
Base: test.base,
|
||||
BitSize: test.bitSize,
|
||||
}
|
||||
|
||||
got, err := intParser.Process(t.Context(), test.payload)
|
||||
|
||||
gotInt, ok := got.(int64)
|
||||
if !ok {
|
||||
t.Errorf("int.parse returned a %T payload: %s", got, got)
|
||||
t.Fatalf("int.parse returned a %T payload: %s", got, got)
|
||||
}
|
||||
if err != nil {
|
||||
t.Errorf("int.parse failed: %s", err)
|
||||
t.Fatalf("int.parse failed: %s", err)
|
||||
}
|
||||
if gotInt != test.expected {
|
||||
t.Errorf("int.parse got %d, expected %d", gotInt, test.expected)
|
||||
t.Fatalf("int.parse got %d, expected %d", gotInt, test.expected)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestBadIntParse(t *testing.T) {
|
||||
intParser := processor.IntParse{}
|
||||
tests := []struct {
|
||||
processor processor.Processor
|
||||
name string
|
||||
payload any
|
||||
base int
|
||||
bitSize int
|
||||
errorString string
|
||||
}{
|
||||
{
|
||||
name: "non-string input",
|
||||
payload: []byte{0x01},
|
||||
base: 10,
|
||||
bitSize: 64,
|
||||
errorString: "int.parse processor only accepts a string",
|
||||
},
|
||||
{
|
||||
name: "not int string",
|
||||
payload: "123.46",
|
||||
base: 10,
|
||||
bitSize: 64,
|
||||
errorString: "strconv.ParseInt: parsing \"123.46\": invalid syntax",
|
||||
},
|
||||
{
|
||||
name: "bit overflow",
|
||||
payload: "12345678901234567890",
|
||||
base: 10,
|
||||
bitSize: 32,
|
||||
errorString: "strconv.ParseInt: parsing \"12345678901234567890\": value out of range",
|
||||
},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
intParser := processor.IntParse{
|
||||
Base: test.base,
|
||||
BitSize: test.bitSize,
|
||||
}
|
||||
got, err := intParser.Process(t.Context(), test.payload)
|
||||
|
||||
if err == nil {
|
||||
t.Errorf("int.parse expected to fail but succeeded, got: %v", got)
|
||||
t.Fatalf("int.parse expected to fail but succeeded, got: %v", got)
|
||||
|
||||
}
|
||||
if err.Error() != test.errorString {
|
||||
t.Errorf("int.parse got error '%s', expected '%s'", err.Error(), test.errorString)
|
||||
t.Fatalf("int.parse got error '%s', expected '%s'", err.Error(), test.errorString)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
61
internal/processor/int-random.go
Normal file
61
internal/processor/int-random.go
Normal file
@@ -0,0 +1,61 @@
|
||||
package processor
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"math/rand/v2"
|
||||
|
||||
"github.com/jwetzell/showbridge-go/internal/config"
|
||||
)
|
||||
|
||||
type IntRandom struct {
|
||||
Min int
|
||||
Max int
|
||||
config config.ProcessorConfig
|
||||
}
|
||||
|
||||
func (up *IntRandom) Process(ctx context.Context, payload any) (any, error) {
|
||||
payloadInt := rand.IntN(up.Max-up.Min+1) + up.Min
|
||||
return payloadInt, nil
|
||||
}
|
||||
|
||||
func (up *IntRandom) Type() string {
|
||||
return up.config.Type
|
||||
}
|
||||
|
||||
func init() {
|
||||
RegisterProcessor(ProcessorRegistration{
|
||||
Type: "int.random",
|
||||
New: func(config config.ProcessorConfig) (Processor, error) {
|
||||
params := config.Params
|
||||
|
||||
min, ok := params["min"]
|
||||
if !ok {
|
||||
return nil, errors.New("int.random requires a min parameter")
|
||||
}
|
||||
|
||||
minFloat, ok := min.(float64)
|
||||
|
||||
if !ok {
|
||||
return nil, errors.New("int.random min must be a number")
|
||||
}
|
||||
|
||||
max, ok := params["max"]
|
||||
if !ok {
|
||||
return nil, errors.New("int.random requires a max parameter")
|
||||
}
|
||||
|
||||
maxFloat, ok := max.(float64)
|
||||
|
||||
if !ok {
|
||||
return nil, errors.New("int.random max must be a number")
|
||||
}
|
||||
|
||||
if maxFloat < minFloat {
|
||||
return nil, errors.New("int.random max must be greater than min")
|
||||
}
|
||||
|
||||
return &IntRandom{config: config, Min: int(minFloat), Max: int(maxFloat)}, nil
|
||||
},
|
||||
})
|
||||
}
|
||||
42
internal/processor/json-encode.go
Normal file
42
internal/processor/json-encode.go
Normal file
@@ -0,0 +1,42 @@
|
||||
package processor
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/json"
|
||||
|
||||
"github.com/jwetzell/showbridge-go/internal/config"
|
||||
)
|
||||
|
||||
type JsonEncode struct {
|
||||
config config.ProcessorConfig
|
||||
}
|
||||
|
||||
func (je *JsonEncode) Process(ctx context.Context, payload any) (any, error) {
|
||||
var payloadBuffer bytes.Buffer
|
||||
|
||||
err := json.NewEncoder(&payloadBuffer).Encode(payload)
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
payloadBytes := payloadBuffer.Bytes()
|
||||
|
||||
payloadBytes = payloadBytes[0 : len(payloadBytes)-1]
|
||||
|
||||
return payloadBytes, nil
|
||||
}
|
||||
|
||||
func (je *JsonEncode) Type() string {
|
||||
return je.config.Type
|
||||
}
|
||||
|
||||
func init() {
|
||||
RegisterProcessor(ProcessorRegistration{
|
||||
Type: "json.encode",
|
||||
New: func(config config.ProcessorConfig) (Processor, error) {
|
||||
return &JsonEncode{config: config}, nil
|
||||
},
|
||||
})
|
||||
}
|
||||
45
internal/processor/json-encode_test.go
Normal file
45
internal/processor/json-encode_test.go
Normal file
@@ -0,0 +1,45 @@
|
||||
package processor_test
|
||||
|
||||
import (
|
||||
"slices"
|
||||
"testing"
|
||||
|
||||
"github.com/jwetzell/osc-go"
|
||||
"github.com/jwetzell/showbridge-go/internal/processor"
|
||||
)
|
||||
|
||||
func TestGoodJsonEncode(t *testing.T) {
|
||||
stringEncoder := processor.JsonEncode{}
|
||||
tests := []struct {
|
||||
processor processor.Processor
|
||||
name string
|
||||
payload any
|
||||
expected []byte
|
||||
}{
|
||||
{
|
||||
processor: &stringEncoder,
|
||||
name: "hello",
|
||||
payload: osc.OSCMessage{
|
||||
Address: "/hello",
|
||||
},
|
||||
expected: []byte("{\"address\":\"/hello\",\"args\":null}"),
|
||||
},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
got, err := test.processor.Process(t.Context(), test.payload)
|
||||
|
||||
gotBytes, ok := got.([]byte)
|
||||
if !ok {
|
||||
t.Fatalf("json.encode returned a %T payload: %s", got, got)
|
||||
}
|
||||
if err != nil {
|
||||
t.Fatalf("json.encode failed: %s", err)
|
||||
}
|
||||
if !slices.Equal(gotBytes, test.expected) {
|
||||
t.Fatalf("json.encode got %x, expected %s", got, test.expected)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -5,6 +5,7 @@ package processor
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"strconv"
|
||||
"text/template"
|
||||
@@ -13,17 +14,18 @@ import (
|
||||
"gitlab.com/gomidi/midi/v2"
|
||||
)
|
||||
|
||||
// TODO(jwetzell): support using numbers in config file treated as hardcoded values
|
||||
type MIDIMessageCreate struct {
|
||||
config config.ProcessorConfig
|
||||
ProcessFunc func(ctx context.Context, payload any) (any, error)
|
||||
}
|
||||
|
||||
func (mmd *MIDIMessageCreate) Process(ctx context.Context, payload any) (any, error) {
|
||||
return mmd.ProcessFunc(ctx, payload)
|
||||
func (mmc *MIDIMessageCreate) Process(ctx context.Context, payload any) (any, error) {
|
||||
return mmc.ProcessFunc(ctx, payload)
|
||||
}
|
||||
|
||||
func (mmd *MIDIMessageCreate) Type() string {
|
||||
return mmd.config.Type
|
||||
func (mmc *MIDIMessageCreate) Type() string {
|
||||
return mmc.config.Type
|
||||
}
|
||||
|
||||
func newMidiNoteOnCreate(config config.ProcessorConfig) (Processor, error) {
|
||||
@@ -33,13 +35,13 @@ func newMidiNoteOnCreate(config config.ProcessorConfig) (Processor, error) {
|
||||
channel, ok := params["channel"]
|
||||
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("midi.message.create NoteOn requires a channel parameter")
|
||||
return nil, errors.New("midi.message.create NoteOn requires a channel parameter")
|
||||
}
|
||||
|
||||
channelString, ok := channel.(string)
|
||||
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("midi.message.create NoteOn channel must be a string")
|
||||
return nil, errors.New("midi.message.create NoteOn channel must be a string")
|
||||
}
|
||||
|
||||
channelTemplate, err := template.New("channel").Parse(channelString)
|
||||
@@ -51,13 +53,13 @@ func newMidiNoteOnCreate(config config.ProcessorConfig) (Processor, error) {
|
||||
note, ok := params["note"]
|
||||
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("midi.message.create NoteOn requires a note parameter")
|
||||
return nil, errors.New("midi.message.create NoteOn requires a note parameter")
|
||||
}
|
||||
|
||||
noteString, ok := note.(string)
|
||||
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("midi.message.create NoteOn note must be a string")
|
||||
return nil, errors.New("midi.message.create NoteOn note must be a string")
|
||||
}
|
||||
|
||||
noteTemplate, err := template.New("note").Parse(noteString)
|
||||
@@ -69,13 +71,13 @@ func newMidiNoteOnCreate(config config.ProcessorConfig) (Processor, error) {
|
||||
velocity, ok := params["velocity"]
|
||||
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("midi.message.create NoteOn requires a velocity parameter")
|
||||
return nil, errors.New("midi.message.create NoteOn requires a velocity parameter")
|
||||
}
|
||||
|
||||
velocityString, ok := velocity.(string)
|
||||
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("midi.message.create NoteOn velocity must be a string")
|
||||
return nil, errors.New("midi.message.create NoteOn velocity must be a string")
|
||||
}
|
||||
|
||||
velocityTemplate, err := template.New("velocity").Parse(velocityString)
|
||||
@@ -124,13 +126,13 @@ func newMidiNoteOffCreate(config config.ProcessorConfig) (Processor, error) {
|
||||
channel, ok := params["channel"]
|
||||
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("midi.message.create NoteOn requires a channel parameter")
|
||||
return nil, errors.New("midi.message.create NoteOn requires a channel parameter")
|
||||
}
|
||||
|
||||
channelString, ok := channel.(string)
|
||||
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("midi.message.create NoteOn channel must be a string")
|
||||
return nil, errors.New("midi.message.create NoteOn channel must be a string")
|
||||
}
|
||||
|
||||
channelTemplate, err := template.New("channel").Parse(channelString)
|
||||
@@ -142,13 +144,13 @@ func newMidiNoteOffCreate(config config.ProcessorConfig) (Processor, error) {
|
||||
note, ok := params["note"]
|
||||
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("midi.message.create NoteOn requires a note parameter")
|
||||
return nil, errors.New("midi.message.create NoteOn requires a note parameter")
|
||||
}
|
||||
|
||||
noteString, ok := note.(string)
|
||||
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("midi.message.create NoteOn note must be a string")
|
||||
return nil, errors.New("midi.message.create NoteOn note must be a string")
|
||||
}
|
||||
|
||||
noteTemplate, err := template.New("note").Parse(noteString)
|
||||
@@ -189,13 +191,13 @@ func newMidiControlChangeCreate(config config.ProcessorConfig) (Processor, error
|
||||
channel, ok := params["channel"]
|
||||
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("midi.message.create ControlChange requires a channel parameter")
|
||||
return nil, errors.New("midi.message.create ControlChange requires a channel parameter")
|
||||
}
|
||||
|
||||
channelString, ok := channel.(string)
|
||||
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("midi.message.create ControlChange channel must be a string")
|
||||
return nil, errors.New("midi.message.create ControlChange channel must be a string")
|
||||
}
|
||||
|
||||
channelTemplate, err := template.New("channel").Parse(channelString)
|
||||
@@ -207,13 +209,13 @@ func newMidiControlChangeCreate(config config.ProcessorConfig) (Processor, error
|
||||
controller, ok := params["controller"]
|
||||
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("midi.message.create ControlChange requires a controller parameter")
|
||||
return nil, errors.New("midi.message.create ControlChange requires a controller parameter")
|
||||
}
|
||||
|
||||
controllerString, ok := controller.(string)
|
||||
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("midi.message.create ControlChange controller must be a string")
|
||||
return nil, errors.New("midi.message.create ControlChange controller must be a string")
|
||||
}
|
||||
|
||||
controllerTemplate, err := template.New("controller").Parse(controllerString)
|
||||
@@ -225,13 +227,13 @@ func newMidiControlChangeCreate(config config.ProcessorConfig) (Processor, error
|
||||
value, ok := params["value"]
|
||||
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("midi.message.create ControlChange requires a value parameter")
|
||||
return nil, errors.New("midi.message.create ControlChange requires a value parameter")
|
||||
}
|
||||
|
||||
valueString, ok := value.(string)
|
||||
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("midi.message.create ControlChange value must be a string")
|
||||
return nil, errors.New("midi.message.create ControlChange value must be a string")
|
||||
}
|
||||
|
||||
valueTemplate, err := template.New("value").Parse(valueString)
|
||||
@@ -281,13 +283,13 @@ func newMidiProgramChangeCreate(config config.ProcessorConfig) (Processor, error
|
||||
channel, ok := params["channel"]
|
||||
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("midi.message.create ProgramChange requires a channel parameter")
|
||||
return nil, errors.New("midi.message.create ProgramChange requires a channel parameter")
|
||||
}
|
||||
|
||||
channelString, ok := channel.(string)
|
||||
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("midi.message.create ProgramChange channel must be a string")
|
||||
return nil, errors.New("midi.message.create ProgramChange channel must be a string")
|
||||
}
|
||||
|
||||
channelTemplate, err := template.New("channel").Parse(channelString)
|
||||
@@ -299,13 +301,13 @@ func newMidiProgramChangeCreate(config config.ProcessorConfig) (Processor, error
|
||||
program, ok := params["program"]
|
||||
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("midi.message.create ProgramChange requires a program parameter")
|
||||
return nil, errors.New("midi.message.create ProgramChange requires a program parameter")
|
||||
}
|
||||
|
||||
programString, ok := program.(string)
|
||||
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("midi.message.create ProgramChange program must be a string")
|
||||
return nil, errors.New("midi.message.create ProgramChange program must be a string")
|
||||
}
|
||||
|
||||
programTemplate, err := template.New("program").Parse(programString)
|
||||
@@ -348,13 +350,13 @@ func init() {
|
||||
msgType, ok := params["type"]
|
||||
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("midi.message.create requires a type parameter")
|
||||
return nil, errors.New("midi.message.create requires a type parameter")
|
||||
}
|
||||
|
||||
msgTypeString, ok := msgType.(string)
|
||||
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("midi.message.create type parameter must be a string")
|
||||
return nil, errors.New("midi.message.create type parameter must be a string")
|
||||
}
|
||||
|
||||
switch msgTypeString {
|
||||
|
||||
@@ -4,7 +4,7 @@ package processor
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"errors"
|
||||
|
||||
"github.com/jwetzell/showbridge-go/internal/config"
|
||||
"gitlab.com/gomidi/midi/v2"
|
||||
@@ -18,7 +18,7 @@ func (mmd *MIDIMessageDecode) Process(ctx context.Context, payload any) (any, er
|
||||
payloadBytes, ok := payload.([]byte)
|
||||
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("midi.message.decode processor only accepts a []byte")
|
||||
return nil, errors.New("midi.message.decode processor only accepts a []byte")
|
||||
}
|
||||
|
||||
payloadMessage := midi.Message(payloadBytes)
|
||||
|
||||
@@ -4,7 +4,7 @@ package processor
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"errors"
|
||||
|
||||
"github.com/jwetzell/showbridge-go/internal/config"
|
||||
"gitlab.com/gomidi/midi/v2"
|
||||
@@ -18,7 +18,7 @@ func (mme *MIDIMessageEncode) Process(ctx context.Context, payload any) (any, er
|
||||
payloadMessage, ok := payload.(midi.Message)
|
||||
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("midi.message.encode processor only accepts an midi.Message")
|
||||
return nil, errors.New("midi.message.encode processor only accepts an midi.Message")
|
||||
}
|
||||
|
||||
return payloadMessage.Bytes(), nil
|
||||
|
||||
@@ -4,7 +4,7 @@ package processor
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"errors"
|
||||
|
||||
"github.com/jwetzell/showbridge-go/internal/config"
|
||||
"gitlab.com/gomidi/midi/v2"
|
||||
@@ -19,7 +19,7 @@ func (mmf *MIDIMessageFilter) Process(ctx context.Context, payload any) (any, er
|
||||
payloadMessage, ok := payload.(midi.Message)
|
||||
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("midi.message.filter processor only accepts an midi.Message")
|
||||
return nil, errors.New("midi.message.filter processor only accepts an midi.Message")
|
||||
}
|
||||
|
||||
if payloadMessage.Type().String() != mmf.MIDIType {
|
||||
@@ -41,12 +41,12 @@ func init() {
|
||||
midiType, ok := params["type"]
|
||||
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("midi.message.filter requires a type parameter")
|
||||
return nil, errors.New("midi.message.filter requires a type parameter")
|
||||
}
|
||||
midiTypeString, ok := midiType.(string)
|
||||
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("midi.message.filter type must be a string")
|
||||
return nil, errors.New("midi.message.filter type must be a string")
|
||||
}
|
||||
|
||||
return &MIDIMessageFilter{config: config, MIDIType: midiTypeString}, nil
|
||||
|
||||
@@ -4,6 +4,7 @@ package processor
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
|
||||
"github.com/jwetzell/showbridge-go/internal/config"
|
||||
@@ -47,7 +48,7 @@ func (mmu *MIDIMessageUnpack) Process(ctx context.Context, payload any) (any, er
|
||||
payloadMidi, ok := payload.(midi.Message)
|
||||
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("midi.message.unpack processor only accepts a midi.Message")
|
||||
return nil, errors.New("midi.message.unpack processor only accepts a midi.Message")
|
||||
}
|
||||
|
||||
switch payloadMidi.Type() {
|
||||
|
||||
@@ -2,7 +2,7 @@ package processor
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"errors"
|
||||
|
||||
"github.com/jwetzell/showbridge-go/internal/config"
|
||||
)
|
||||
@@ -74,44 +74,44 @@ func init() {
|
||||
topic, ok := params["topic"]
|
||||
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("mqtt.message.create requires an topic parameter")
|
||||
return nil, errors.New("mqtt.message.create requires a topic parameter")
|
||||
}
|
||||
|
||||
topicString, ok := topic.(string)
|
||||
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("mqtt.message.create topic must be a string")
|
||||
return nil, errors.New("mqtt.message.create topic must be a string")
|
||||
}
|
||||
|
||||
qos, ok := params["qos"]
|
||||
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("mqtt.message.create requires an qos parameter")
|
||||
return nil, errors.New("mqtt.message.create requires a qos parameter")
|
||||
}
|
||||
|
||||
qosByte, ok := qos.(float64)
|
||||
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("mqtt.message.create qos must be a number")
|
||||
return nil, errors.New("mqtt.message.create qos must be a number")
|
||||
}
|
||||
|
||||
retained, ok := params["retained"]
|
||||
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("mqtt.message.create requires an retained parameter")
|
||||
return nil, errors.New("mqtt.message.create requires a retained parameter")
|
||||
}
|
||||
|
||||
retainedBool, ok := retained.(bool)
|
||||
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("mqtt.message.create retained must be a boolean")
|
||||
return nil, errors.New("mqtt.message.create retained must be a boolean")
|
||||
}
|
||||
|
||||
//TODO(jwetzell): convert payload into []byte or string for sending
|
||||
payload, ok := params["payload"]
|
||||
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("mqtt.message.create requires an payload parameter")
|
||||
return nil, errors.New("mqtt.message.create requires a payload parameter")
|
||||
}
|
||||
|
||||
if payloadBytes, ok := payload.([]byte); ok {
|
||||
@@ -121,7 +121,7 @@ func init() {
|
||||
payloadString, ok := payload.(string)
|
||||
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("mqtt.message.create payload must be a string or byte array")
|
||||
return nil, errors.New("mqtt.message.create payload must be a string or byte array")
|
||||
}
|
||||
|
||||
payloadBytes := []byte(payloadString)
|
||||
|
||||
@@ -2,7 +2,7 @@ package processor
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"errors"
|
||||
|
||||
mqtt "github.com/eclipse/paho.mqtt.golang"
|
||||
"github.com/jwetzell/showbridge-go/internal/config"
|
||||
@@ -16,7 +16,7 @@ func (mme *MQTTMessageEncode) Process(ctx context.Context, payload any) (any, er
|
||||
payloadMessage, ok := payload.(mqtt.Message)
|
||||
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("mqtt.message.encode processor only accepts an mqtt.Message")
|
||||
return nil, errors.New("mqtt.message.encode processor only accepts an mqtt.Message")
|
||||
}
|
||||
|
||||
return payloadMessage.Payload(), nil
|
||||
|
||||
@@ -3,7 +3,7 @@ package processor
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"fmt"
|
||||
"errors"
|
||||
"text/template"
|
||||
|
||||
"github.com/jwetzell/showbridge-go/internal/config"
|
||||
@@ -52,25 +52,25 @@ func init() {
|
||||
subject, ok := params["subject"]
|
||||
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("nats.message.create requires a subject parameter")
|
||||
return nil, errors.New("nats.message.create requires a subject parameter")
|
||||
}
|
||||
|
||||
subjectString, ok := subject.(string)
|
||||
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("nats.message.create subject must be a string")
|
||||
return nil, errors.New("nats.message.create subject must be a string")
|
||||
}
|
||||
|
||||
payload, ok := params["payload"]
|
||||
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("osc.message.create requires a payload parameter")
|
||||
return nil, errors.New("nats.message.create requires a payload parameter")
|
||||
}
|
||||
|
||||
payloadString, ok := payload.(string)
|
||||
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("osc.message.create payload must be a string")
|
||||
return nil, errors.New("nats.message.create payload must be a string")
|
||||
}
|
||||
|
||||
payloadTemplate, err := template.New("payload").Parse(payloadString)
|
||||
|
||||
@@ -2,7 +2,7 @@ package processor
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"errors"
|
||||
|
||||
"github.com/jwetzell/showbridge-go/internal/config"
|
||||
"github.com/nats-io/nats.go"
|
||||
@@ -16,7 +16,7 @@ func (nme *NATSMessageEncode) Process(ctx context.Context, payload any) (any, er
|
||||
payloadMessage, ok := payload.(*nats.Msg)
|
||||
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("nats.message.encode processor only accepts an nats.Msg")
|
||||
return nil, errors.New("nats.message.encode processor only accepts an nats.Msg")
|
||||
}
|
||||
|
||||
return payloadMessage.Data, nil
|
||||
|
||||
@@ -4,6 +4,7 @@ import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/hex"
|
||||
"errors"
|
||||
"fmt"
|
||||
"strconv"
|
||||
"text/template"
|
||||
@@ -19,10 +20,10 @@ type OSCMessageCreate struct {
|
||||
Types string
|
||||
}
|
||||
|
||||
func (o *OSCMessageCreate) Process(ctx context.Context, payload any) (any, error) {
|
||||
func (omc *OSCMessageCreate) Process(ctx context.Context, payload any) (any, error) {
|
||||
|
||||
var addressBuffer bytes.Buffer
|
||||
err := o.Address.Execute(&addressBuffer, payload)
|
||||
err := omc.Address.Execute(&addressBuffer, payload)
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -31,11 +32,11 @@ func (o *OSCMessageCreate) Process(ctx context.Context, payload any) (any, error
|
||||
addressString := addressBuffer.String()
|
||||
|
||||
if len(addressString) == 0 {
|
||||
return nil, fmt.Errorf("osc.message.create address must not be empty")
|
||||
return nil, errors.New("osc.message.create address must not be empty")
|
||||
}
|
||||
|
||||
if addressString[0] != '/' {
|
||||
return nil, fmt.Errorf("osc.message.create address must start with '/'")
|
||||
return nil, errors.New("osc.message.create address must start with '/'")
|
||||
}
|
||||
|
||||
payloadMessage := osc.OSCMessage{
|
||||
@@ -44,7 +45,7 @@ func (o *OSCMessageCreate) Process(ctx context.Context, payload any) (any, error
|
||||
|
||||
args := []osc.OSCArg{}
|
||||
|
||||
for argIndex, argTemplate := range o.Args {
|
||||
for argIndex, argTemplate := range omc.Args {
|
||||
var argBuffer bytes.Buffer
|
||||
err := argTemplate.Execute(&argBuffer, payload)
|
||||
|
||||
@@ -54,7 +55,7 @@ func (o *OSCMessageCreate) Process(ctx context.Context, payload any) (any, error
|
||||
|
||||
argString := argBuffer.String()
|
||||
|
||||
typedArg, err := argToTypedArg(argString, o.Types[argIndex])
|
||||
typedArg, err := argToTypedArg(argString, omc.Types[argIndex])
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -70,8 +71,8 @@ func (o *OSCMessageCreate) Process(ctx context.Context, payload any) (any, error
|
||||
return payloadMessage, nil
|
||||
}
|
||||
|
||||
func (o *OSCMessageCreate) Type() string {
|
||||
return o.config.Type
|
||||
func (omc *OSCMessageCreate) Type() string {
|
||||
return omc.config.Type
|
||||
}
|
||||
|
||||
func init() {
|
||||
@@ -82,13 +83,13 @@ func init() {
|
||||
address, ok := params["address"]
|
||||
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("osc.message.create requires an address parameter")
|
||||
return nil, errors.New("osc.message.create requires an address parameter")
|
||||
}
|
||||
|
||||
addressString, ok := address.(string)
|
||||
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("osc.message.create address must be a string")
|
||||
return nil, errors.New("osc.message.create address must be a string")
|
||||
}
|
||||
|
||||
addressTemplate, err := template.New("address").Parse(addressString)
|
||||
@@ -109,17 +110,17 @@ func init() {
|
||||
types, ok := params["types"]
|
||||
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("osc.message.create requires a types parameter with args")
|
||||
return nil, errors.New("osc.message.create requires a types parameter with args")
|
||||
}
|
||||
|
||||
typesString, ok := types.(string)
|
||||
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("osc.message.create types must be a string")
|
||||
return nil, errors.New("osc.message.create types must be a string")
|
||||
}
|
||||
|
||||
if len(rawArgs) != len(typesString) {
|
||||
return nil, fmt.Errorf("osc.message.create args and types must be the same length")
|
||||
return nil, errors.New("osc.message.create args and types must be the same length")
|
||||
}
|
||||
|
||||
argTemplates := []*template.Template{}
|
||||
@@ -128,7 +129,7 @@ func init() {
|
||||
argString, ok := rawArg.(string)
|
||||
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("osc.message.create arg must be a string")
|
||||
return nil, errors.New("osc.message.create arg must be a string")
|
||||
}
|
||||
|
||||
argTemplate, err := template.New("arg").Parse(argString)
|
||||
|
||||
@@ -2,7 +2,7 @@ package processor
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"errors"
|
||||
|
||||
osc "github.com/jwetzell/osc-go"
|
||||
"github.com/jwetzell/showbridge-go/internal/config"
|
||||
@@ -12,19 +12,19 @@ type OSCMessageDecode struct {
|
||||
config config.ProcessorConfig
|
||||
}
|
||||
|
||||
func (o *OSCMessageDecode) Process(ctx context.Context, payload any) (any, error) {
|
||||
func (omd *OSCMessageDecode) Process(ctx context.Context, payload any) (any, error) {
|
||||
payloadBytes, ok := payload.([]byte)
|
||||
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("osc.message.decode processor only accepts a []byte payload")
|
||||
return nil, errors.New("osc.message.decode processor only accepts a []byte payload")
|
||||
}
|
||||
|
||||
if len(payloadBytes) == 0 {
|
||||
return nil, fmt.Errorf("osc.message.decode processor can't work on empty []byte")
|
||||
return nil, errors.New("osc.message.decode processor can't work on empty []byte")
|
||||
}
|
||||
|
||||
if payloadBytes[0] != '/' {
|
||||
return nil, fmt.Errorf("osc.message.decode processor needs an OSC looking []byte")
|
||||
return nil, errors.New("osc.message.decode processor needs an OSC looking []byte")
|
||||
}
|
||||
|
||||
message, err := osc.MessageFromBytes(payloadBytes)
|
||||
@@ -34,8 +34,8 @@ func (o *OSCMessageDecode) Process(ctx context.Context, payload any) (any, error
|
||||
return message, nil
|
||||
}
|
||||
|
||||
func (o *OSCMessageDecode) Type() string {
|
||||
return o.config.Type
|
||||
func (omd *OSCMessageDecode) Type() string {
|
||||
return omd.config.Type
|
||||
}
|
||||
|
||||
func init() {
|
||||
|
||||
@@ -2,7 +2,7 @@ package processor
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"errors"
|
||||
|
||||
osc "github.com/jwetzell/osc-go"
|
||||
"github.com/jwetzell/showbridge-go/internal/config"
|
||||
@@ -12,19 +12,19 @@ type OSCMessageEncode struct {
|
||||
config config.ProcessorConfig
|
||||
}
|
||||
|
||||
func (o *OSCMessageEncode) Process(ctx context.Context, payload any) (any, error) {
|
||||
func (ome *OSCMessageEncode) Process(ctx context.Context, payload any) (any, error) {
|
||||
payloadMessage, ok := payload.(osc.OSCMessage)
|
||||
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("osc.message.encode processor only accepts an OSCMessage")
|
||||
return nil, errors.New("osc.message.encode processor only accepts an OSCMessage")
|
||||
}
|
||||
|
||||
bytes := payloadMessage.ToBytes()
|
||||
return bytes, nil
|
||||
}
|
||||
|
||||
func (o *OSCMessageEncode) Type() string {
|
||||
return o.config.Type
|
||||
func (ome *OSCMessageEncode) Type() string {
|
||||
return ome.config.Type
|
||||
}
|
||||
|
||||
func init() {
|
||||
|
||||
@@ -2,6 +2,7 @@ package processor
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"regexp"
|
||||
"strings"
|
||||
@@ -15,23 +16,23 @@ type OSCMessageFilter struct {
|
||||
Address *regexp.Regexp
|
||||
}
|
||||
|
||||
func (o *OSCMessageFilter) Process(ctx context.Context, payload any) (any, error) {
|
||||
func (omf *OSCMessageFilter) Process(ctx context.Context, payload any) (any, error) {
|
||||
|
||||
payloadMessage, ok := payload.(osc.OSCMessage)
|
||||
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("osc.message.filter can only operate on OSCMessage payloads")
|
||||
return nil, errors.New("osc.message.filter can only operate on OSCMessage payloads")
|
||||
}
|
||||
|
||||
if !o.Address.MatchString(payloadMessage.Address) {
|
||||
if !omf.Address.MatchString(payloadMessage.Address) {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
return payloadMessage, nil
|
||||
}
|
||||
|
||||
func (o *OSCMessageFilter) Type() string {
|
||||
return o.config.Type
|
||||
func (omf *OSCMessageFilter) Type() string {
|
||||
return omf.config.Type
|
||||
}
|
||||
|
||||
func init() {
|
||||
@@ -42,13 +43,13 @@ func init() {
|
||||
address, ok := params["address"]
|
||||
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("osc.message.filter requires an address parameter")
|
||||
return nil, errors.New("osc.message.filter requires an address parameter")
|
||||
}
|
||||
|
||||
addressString, ok := address.(string)
|
||||
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("osc.message.filter address must be a string")
|
||||
return nil, errors.New("osc.message.filter address must be a string")
|
||||
}
|
||||
|
||||
addressPattern := strings.ReplaceAll(addressString, "?", ".")
|
||||
|
||||
@@ -3,7 +3,7 @@ package processor
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"fmt"
|
||||
"errors"
|
||||
"text/template"
|
||||
|
||||
osc "github.com/jwetzell/osc-go"
|
||||
@@ -15,16 +15,15 @@ type OSCMessageTransform struct {
|
||||
Address *template.Template
|
||||
}
|
||||
|
||||
func (o *OSCMessageTransform) Process(ctx context.Context, payload any) (any, error) {
|
||||
func (omt *OSCMessageTransform) Process(ctx context.Context, payload any) (any, error) {
|
||||
payloadMessage, ok := payload.(osc.OSCMessage)
|
||||
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("osc.message.transform processor only accepts an OSCMessage")
|
||||
return nil, errors.New("osc.message.transform processor only accepts an OSCMessage")
|
||||
}
|
||||
|
||||
var addressBuffer bytes.Buffer
|
||||
//TODO(jwetzell): actually inject data into template
|
||||
err := o.Address.Execute(&addressBuffer, payloadMessage)
|
||||
err := omt.Address.Execute(&addressBuffer, payloadMessage)
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -33,11 +32,11 @@ func (o *OSCMessageTransform) Process(ctx context.Context, payload any) (any, er
|
||||
addressString := addressBuffer.String()
|
||||
|
||||
if len(addressString) == 0 {
|
||||
return nil, fmt.Errorf("osc.message.transform address must not be empty")
|
||||
return nil, errors.New("osc.message.transform address must not be empty")
|
||||
}
|
||||
|
||||
if addressString[0] != '/' {
|
||||
return nil, fmt.Errorf("osc.message.transform address must start with '/'")
|
||||
return nil, errors.New("osc.message.transform address must start with '/'")
|
||||
}
|
||||
|
||||
payloadMessage.Address = addressString
|
||||
@@ -45,8 +44,8 @@ func (o *OSCMessageTransform) Process(ctx context.Context, payload any) (any, er
|
||||
return payloadMessage, nil
|
||||
}
|
||||
|
||||
func (o *OSCMessageTransform) Type() string {
|
||||
return o.config.Type
|
||||
func (omt *OSCMessageTransform) Type() string {
|
||||
return omt.config.Type
|
||||
}
|
||||
|
||||
func init() {
|
||||
@@ -57,13 +56,13 @@ func init() {
|
||||
address, ok := params["address"]
|
||||
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("osc.message.transform requires an address parameter")
|
||||
return nil, errors.New("osc.message.transform requires an address parameter")
|
||||
}
|
||||
|
||||
addressString, ok := address.(string)
|
||||
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("osc.message.transform address must be a string")
|
||||
return nil, errors.New("osc.message.transform address must be a string")
|
||||
}
|
||||
|
||||
addressTemplate, err := template.New("address").Parse(addressString)
|
||||
|
||||
62
internal/processor/processor_test.go
Normal file
62
internal/processor/processor_test.go
Normal file
@@ -0,0 +1,62 @@
|
||||
package processor_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
|
||||
"github.com/jwetzell/showbridge-go/internal/config"
|
||||
"github.com/jwetzell/showbridge-go/internal/processor"
|
||||
)
|
||||
|
||||
type TestProcessor struct {
|
||||
}
|
||||
|
||||
func (p *TestProcessor) Type() string {
|
||||
return "test"
|
||||
}
|
||||
func (p *TestProcessor) Process(ctx context.Context, input any) (any, error) {
|
||||
return input, nil
|
||||
}
|
||||
|
||||
func TestProcessorBadRegistrationNoType(t *testing.T) {
|
||||
defer func() {
|
||||
if r := recover(); r == nil {
|
||||
t.Fatalf("processor registration should have panicked but did not")
|
||||
}
|
||||
}()
|
||||
|
||||
processor.RegisterProcessor(processor.ProcessorRegistration{
|
||||
Type: "",
|
||||
New: func(config config.ProcessorConfig) (processor.Processor, error) {
|
||||
return &TestProcessor{}, nil
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
func TestProcessorBadRegistrationNoNew(t *testing.T) {
|
||||
defer func() {
|
||||
if r := recover(); r == nil {
|
||||
t.Fatalf("processor registration should have panicked but did not")
|
||||
}
|
||||
}()
|
||||
|
||||
processor.RegisterProcessor(processor.ProcessorRegistration{
|
||||
Type: "test",
|
||||
New: nil,
|
||||
})
|
||||
}
|
||||
|
||||
func TestProcessorBadRegistrationExistingType(t *testing.T) {
|
||||
defer func() {
|
||||
if r := recover(); r == nil {
|
||||
t.Fatalf("processor registration should have panicked but did not")
|
||||
}
|
||||
}()
|
||||
|
||||
processor.RegisterProcessor(processor.ProcessorRegistration{
|
||||
Type: "string.create",
|
||||
New: func(config config.ProcessorConfig) (processor.Processor, error) {
|
||||
return &TestProcessor{}, nil
|
||||
},
|
||||
})
|
||||
}
|
||||
@@ -2,7 +2,7 @@ package processor
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"errors"
|
||||
|
||||
"github.com/expr-lang/expr"
|
||||
"github.com/expr-lang/expr/vm"
|
||||
@@ -38,13 +38,13 @@ func init() {
|
||||
expression, ok := params["expression"]
|
||||
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("script.expr requires an expression parameter")
|
||||
return nil, errors.New("script.expr requires an expression parameter")
|
||||
}
|
||||
|
||||
expressionString, ok := expression.(string)
|
||||
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("script.expr expression must be a string")
|
||||
return nil, errors.New("script.expr expression must be a string")
|
||||
}
|
||||
|
||||
program, err := expr.Compile(expressionString)
|
||||
|
||||
171
internal/processor/script-expr_test.go
Normal file
171
internal/processor/script-expr_test.go
Normal file
@@ -0,0 +1,171 @@
|
||||
package processor_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/expr-lang/expr"
|
||||
"github.com/jwetzell/showbridge-go/internal/config"
|
||||
"github.com/jwetzell/showbridge-go/internal/processor"
|
||||
)
|
||||
|
||||
func TestScriptExprFromRegistry(t *testing.T) {
|
||||
registration, ok := processor.ProcessorRegistry["script.expr"]
|
||||
if !ok {
|
||||
t.Fatalf("script.expr processor not registered")
|
||||
}
|
||||
|
||||
processorInstance, err := registration.New(config.ProcessorConfig{
|
||||
Type: "script.expr",
|
||||
Params: map[string]any{
|
||||
"expression": "foo + bar",
|
||||
},
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("failed to create script.expr processor: %s", err)
|
||||
}
|
||||
|
||||
if processorInstance.Type() != "script.expr" {
|
||||
t.Fatalf("script.expr processor has wrong type: %s", processorInstance.Type())
|
||||
}
|
||||
}
|
||||
|
||||
func TestScriptExprNoProgram(t *testing.T) {
|
||||
registration, ok := processor.ProcessorRegistry["script.expr"]
|
||||
if !ok {
|
||||
t.Fatalf("script.expr processor not registered")
|
||||
}
|
||||
|
||||
_, err := registration.New(config.ProcessorConfig{
|
||||
Type: "script.expr",
|
||||
Params: map[string]any{},
|
||||
})
|
||||
|
||||
if err == nil {
|
||||
t.Fatalf("script.expr processor should have thrown an error when creating")
|
||||
}
|
||||
}
|
||||
|
||||
func TestScriptExprBadConfigWrongExpressionType(t *testing.T) {
|
||||
registration, ok := processor.ProcessorRegistry["script.expr"]
|
||||
if !ok {
|
||||
t.Fatalf("script.expr processor not registered")
|
||||
}
|
||||
|
||||
_, err := registration.New(config.ProcessorConfig{
|
||||
Type: "script.expr",
|
||||
Params: map[string]any{
|
||||
"expression": 12345,
|
||||
},
|
||||
})
|
||||
|
||||
if err == nil {
|
||||
t.Fatalf("script.expr processor should have thrown an error when creating with non-string expression")
|
||||
}
|
||||
}
|
||||
|
||||
func TestScriptExprBadConfigNonCompilingExpression(t *testing.T) {
|
||||
registration, ok := processor.ProcessorRegistry["script.expr"]
|
||||
if !ok {
|
||||
t.Fatalf("script.expr processor not registered")
|
||||
}
|
||||
|
||||
_, err := registration.New(config.ProcessorConfig{
|
||||
Type: "script.expr",
|
||||
Params: map[string]any{
|
||||
"expression": "foo + ",
|
||||
},
|
||||
})
|
||||
|
||||
if err == nil {
|
||||
t.Fatalf("script.expr processor should have thrown an error when creating with non-compiling expression")
|
||||
}
|
||||
}
|
||||
|
||||
func TestGoodScriptExpr(t *testing.T) {
|
||||
tests := []struct {
|
||||
program string
|
||||
name string
|
||||
payload map[string]any
|
||||
expected any
|
||||
}{
|
||||
{
|
||||
program: "foo + bar",
|
||||
name: "number",
|
||||
payload: map[string]any{
|
||||
"foo": 1,
|
||||
"bar": 1,
|
||||
},
|
||||
expected: 2,
|
||||
},
|
||||
{
|
||||
program: "foo + bar",
|
||||
name: "string",
|
||||
payload: map[string]any{
|
||||
"foo": "1",
|
||||
"bar": "1",
|
||||
},
|
||||
expected: "11",
|
||||
},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
program, err := expr.Compile(test.program)
|
||||
if err != nil {
|
||||
t.Fatalf("script.expr failed to compile program: %s", err)
|
||||
}
|
||||
|
||||
exprProcessor := &processor.ScriptExpr{Program: program}
|
||||
|
||||
got, err := exprProcessor.Process(t.Context(), test.payload)
|
||||
|
||||
if err != nil {
|
||||
t.Fatalf("script.expr failed: %s", err)
|
||||
}
|
||||
|
||||
//TODO(jwetzell): work out better way to compare the any/any
|
||||
if got != test.expected {
|
||||
t.Fatalf("script.expr got %+v (%T), expected %+v (%T)", got, got, test.expected, test.expected)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestBadScriptExpr(t *testing.T) {
|
||||
tests := []struct {
|
||||
program string
|
||||
name string
|
||||
payload map[string]any
|
||||
errorString string
|
||||
}{
|
||||
{
|
||||
name: "accessing missing field",
|
||||
program: "foo + bar",
|
||||
payload: map[string]any{
|
||||
"foo": 1,
|
||||
},
|
||||
errorString: "invalid operation: int + <nil> (1:5)\n | foo + bar\n | ....^",
|
||||
},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
program, err := expr.Compile(test.program)
|
||||
if err != nil {
|
||||
t.Fatalf("script.expr failed to compile program: %s", err)
|
||||
}
|
||||
|
||||
exprProcessor := &processor.ScriptExpr{Program: program}
|
||||
|
||||
got, err := exprProcessor.Process(t.Context(), test.payload)
|
||||
|
||||
if err == nil {
|
||||
t.Fatalf("script.expr expected to fail but succeeded, got: %v", got)
|
||||
|
||||
}
|
||||
if err.Error() != test.errorString {
|
||||
t.Fatalf("script.expr got error '%s', expected '%s'", err.Error(), test.errorString)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -3,7 +3,7 @@ package processor
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"errors"
|
||||
|
||||
"github.com/jwetzell/showbridge-go/internal/config"
|
||||
"modernc.org/quickjs"
|
||||
@@ -74,13 +74,13 @@ func init() {
|
||||
program, ok := params["program"]
|
||||
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("script.js requires a program parameter")
|
||||
return nil, errors.New("script.js requires a program parameter")
|
||||
}
|
||||
|
||||
programString, ok := program.(string)
|
||||
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("script.js program must be a string")
|
||||
return nil, errors.New("script.js program must be a string")
|
||||
}
|
||||
|
||||
return &ScriptJS{config: config, Program: programString}, nil
|
||||
|
||||
179
internal/processor/script-js_test.go
Normal file
179
internal/processor/script-js_test.go
Normal file
@@ -0,0 +1,179 @@
|
||||
package processor_test
|
||||
|
||||
import (
|
||||
"maps"
|
||||
"testing"
|
||||
|
||||
"github.com/jwetzell/showbridge-go/internal/config"
|
||||
"github.com/jwetzell/showbridge-go/internal/processor"
|
||||
)
|
||||
|
||||
func TestScriptJSFromRegistry(t *testing.T) {
|
||||
registration, ok := processor.ProcessorRegistry["script.js"]
|
||||
if !ok {
|
||||
t.Fatalf("script.js processor not registered")
|
||||
}
|
||||
|
||||
processorInstance, err := registration.New(config.ProcessorConfig{
|
||||
Type: "script.js",
|
||||
Params: map[string]any{
|
||||
"program": `
|
||||
payload = payload + 1
|
||||
`,
|
||||
},
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("failed to create script.js processor: %s", err)
|
||||
}
|
||||
|
||||
if processorInstance.Type() != "script.js" {
|
||||
t.Fatalf("script.js processor has wrong type: %s", processorInstance.Type())
|
||||
}
|
||||
|
||||
payload := 1
|
||||
expected := 2
|
||||
|
||||
got, err := processorInstance.Process(t.Context(), payload)
|
||||
if err != nil {
|
||||
t.Fatalf("script.js processing failed: %s", err)
|
||||
}
|
||||
|
||||
if got != expected {
|
||||
t.Fatalf("script.js got %+v, expected %+v", got, expected)
|
||||
}
|
||||
}
|
||||
|
||||
func TestScriptJSNoProgram(t *testing.T) {
|
||||
registration, ok := processor.ProcessorRegistry["script.js"]
|
||||
if !ok {
|
||||
t.Fatalf("script.js processor not registered")
|
||||
}
|
||||
|
||||
_, err := registration.New(config.ProcessorConfig{
|
||||
Type: "script.js",
|
||||
Params: map[string]any{},
|
||||
})
|
||||
|
||||
if err == nil {
|
||||
t.Fatalf("script.js processor should have thrown an error when creating")
|
||||
}
|
||||
}
|
||||
|
||||
func TestScriptJSBadConfigWrongProgramType(t *testing.T) {
|
||||
registration, ok := processor.ProcessorRegistry["script.js"]
|
||||
if !ok {
|
||||
t.Fatalf("script.js processor not registered")
|
||||
}
|
||||
|
||||
_, err := registration.New(config.ProcessorConfig{
|
||||
Type: "script.js",
|
||||
Params: map[string]any{
|
||||
"program": 12345,
|
||||
},
|
||||
})
|
||||
|
||||
if err == nil {
|
||||
t.Fatalf("script.js processor should have thrown an error when creating with non-string program")
|
||||
}
|
||||
}
|
||||
|
||||
func TestGoodScriptJS(t *testing.T) {
|
||||
tests := []struct {
|
||||
processor processor.Processor
|
||||
name string
|
||||
payload any
|
||||
expected any
|
||||
}{
|
||||
{
|
||||
name: "number",
|
||||
processor: &processor.ScriptJS{Program: `
|
||||
payload = payload + 1
|
||||
`},
|
||||
payload: 1,
|
||||
expected: 2,
|
||||
},
|
||||
{
|
||||
name: "string",
|
||||
processor: &processor.ScriptJS{Program: `
|
||||
payload = payload + "1"
|
||||
`},
|
||||
payload: "1",
|
||||
expected: "11",
|
||||
},
|
||||
{
|
||||
name: "object",
|
||||
processor: &processor.ScriptJS{Program: `
|
||||
payload = { key: payload }
|
||||
`},
|
||||
payload: "1",
|
||||
expected: map[string]any{"key": "1"},
|
||||
},
|
||||
{
|
||||
name: "nil",
|
||||
processor: &processor.ScriptJS{Program: `
|
||||
payload = undefined
|
||||
`},
|
||||
payload: "1",
|
||||
expected: nil,
|
||||
},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
got, err := test.processor.Process(t.Context(), test.payload)
|
||||
|
||||
if err != nil {
|
||||
t.Fatalf("script.js process failed: %s", err)
|
||||
}
|
||||
|
||||
//TODO(jwetzell): work out better way to compare the any/any
|
||||
|
||||
gotMap, ok := got.(map[string]interface{})
|
||||
if ok {
|
||||
// got a map
|
||||
expectedMap, ok := test.expected.(map[string]interface{})
|
||||
if ok {
|
||||
if !maps.Equal(gotMap, expectedMap) {
|
||||
t.Fatalf("script.js got %+v, expected %+v", got, test.expected)
|
||||
}
|
||||
} else {
|
||||
t.Fatalf("script.js got %+v, expected %+v", got, test.expected)
|
||||
}
|
||||
} else {
|
||||
if got != test.expected {
|
||||
t.Fatalf("script.js got %+v, expected %+v", got, test.expected)
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestBadScriptJS(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
processor processor.Processor
|
||||
payload any
|
||||
errorString string
|
||||
}{
|
||||
{
|
||||
name: "accessing not defined variable",
|
||||
processor: &processor.ScriptJS{Program: `paylod = foo`},
|
||||
payload: 0,
|
||||
errorString: "ReferenceError: 'foo' is not defined",
|
||||
},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
got, err := test.processor.Process(t.Context(), test.payload)
|
||||
|
||||
if err == nil {
|
||||
t.Fatalf("script.js expected to fail but succeeded, got: %v", got)
|
||||
}
|
||||
|
||||
if err.Error() != test.errorString {
|
||||
t.Fatalf("script.js got error '%s', expected '%s'", err.Error(), test.errorString)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
97
internal/processor/sip-response-audio-create.go
Normal file
97
internal/processor/sip-response-audio-create.go
Normal file
@@ -0,0 +1,97 @@
|
||||
package processor
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"errors"
|
||||
"text/template"
|
||||
|
||||
"github.com/jwetzell/showbridge-go/internal/config"
|
||||
)
|
||||
|
||||
type SipResponseAudioCreate struct {
|
||||
config config.ProcessorConfig
|
||||
PreWait int
|
||||
PostWait int
|
||||
AudioFile *template.Template
|
||||
}
|
||||
|
||||
type SipAudioFileResponse struct {
|
||||
PreWait int
|
||||
PostWait int
|
||||
AudioFile string
|
||||
}
|
||||
|
||||
func (scc *SipResponseAudioCreate) Process(ctx context.Context, payload any) (any, error) {
|
||||
|
||||
var audioFileBuffer bytes.Buffer
|
||||
err := scc.AudioFile.Execute(&audioFileBuffer, payload)
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
audioFileString := audioFileBuffer.String()
|
||||
|
||||
return SipAudioFileResponse{
|
||||
PreWait: scc.PreWait,
|
||||
PostWait: scc.PostWait,
|
||||
AudioFile: audioFileString,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (scc *SipResponseAudioCreate) Type() string {
|
||||
return scc.config.Type
|
||||
}
|
||||
|
||||
func init() {
|
||||
RegisterProcessor(ProcessorRegistration{
|
||||
Type: "sip.response.audio.create",
|
||||
New: func(config config.ProcessorConfig) (Processor, error) {
|
||||
params := config.Params
|
||||
|
||||
preWait, ok := params["preWait"]
|
||||
|
||||
if !ok {
|
||||
return nil, errors.New("sip.response.audio.create requires a preWait parameter")
|
||||
}
|
||||
|
||||
preWaitNum, ok := preWait.(float64)
|
||||
|
||||
if !ok {
|
||||
return nil, errors.New("sip.response.audio.create preWait must be a number")
|
||||
}
|
||||
|
||||
postWait, ok := params["postWait"]
|
||||
|
||||
if !ok {
|
||||
return nil, errors.New("sip.response.audio.create requires a postWait parameter")
|
||||
}
|
||||
|
||||
postWaitNum, ok := postWait.(float64)
|
||||
|
||||
if !ok {
|
||||
return nil, errors.New("sip.response.audio.create postWait must be a number")
|
||||
}
|
||||
|
||||
audioFile, ok := params["audioFile"]
|
||||
|
||||
if !ok {
|
||||
return nil, errors.New("sip.response.audio.create requires a audioFile parameter")
|
||||
}
|
||||
|
||||
audioFileString, ok := audioFile.(string)
|
||||
|
||||
if !ok {
|
||||
return nil, errors.New("sip.response.audio.create audioFile must be a string")
|
||||
}
|
||||
|
||||
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
|
||||
},
|
||||
})
|
||||
}
|
||||
97
internal/processor/sip-response-dtmf-create.go
Normal file
97
internal/processor/sip-response-dtmf-create.go
Normal file
@@ -0,0 +1,97 @@
|
||||
package processor
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"errors"
|
||||
"text/template"
|
||||
|
||||
"github.com/jwetzell/showbridge-go/internal/config"
|
||||
)
|
||||
|
||||
type SipResponseDTMFCreate struct {
|
||||
config config.ProcessorConfig
|
||||
PreWait int
|
||||
PostWait int
|
||||
Digits *template.Template
|
||||
}
|
||||
|
||||
type SipDTMFResponse struct {
|
||||
PreWait int
|
||||
PostWait int
|
||||
Digits string
|
||||
}
|
||||
|
||||
func (scc *SipResponseDTMFCreate) Process(ctx context.Context, payload any) (any, error) {
|
||||
|
||||
var digitsBuffer bytes.Buffer
|
||||
err := scc.Digits.Execute(&digitsBuffer, payload)
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
digitsString := digitsBuffer.String()
|
||||
|
||||
return SipDTMFResponse{
|
||||
PreWait: scc.PreWait,
|
||||
PostWait: scc.PostWait,
|
||||
Digits: digitsString,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (scc *SipResponseDTMFCreate) Type() string {
|
||||
return scc.config.Type
|
||||
}
|
||||
|
||||
func init() {
|
||||
RegisterProcessor(ProcessorRegistration{
|
||||
Type: "sip.response.dtmf.create",
|
||||
New: func(config config.ProcessorConfig) (Processor, error) {
|
||||
params := config.Params
|
||||
|
||||
preWait, ok := params["preWait"]
|
||||
|
||||
if !ok {
|
||||
return nil, errors.New("sip.response.dtmf.create requires a preWait parameter")
|
||||
}
|
||||
|
||||
preWaitNum, ok := preWait.(float64)
|
||||
|
||||
if !ok {
|
||||
return nil, errors.New("sip.response.dtmf.create preWait must be a number")
|
||||
}
|
||||
|
||||
postWait, ok := params["postWait"]
|
||||
|
||||
if !ok {
|
||||
return nil, errors.New("sip.response.dtmf.create requires a postWait parameter")
|
||||
}
|
||||
|
||||
postWaitNum, ok := postWait.(float64)
|
||||
|
||||
if !ok {
|
||||
return nil, errors.New("sip.response.dtmf.create postWait must be a number")
|
||||
}
|
||||
|
||||
digits, ok := params["digits"]
|
||||
|
||||
if !ok {
|
||||
return nil, errors.New("sip.response.dtmf.create requires a digits parameter")
|
||||
}
|
||||
|
||||
digitsString, ok := digits.(string)
|
||||
|
||||
if !ok {
|
||||
return nil, errors.New("sip.response.dtmf.create digits must be a string")
|
||||
}
|
||||
|
||||
digitsTemplate, err := template.New("digits").Parse(digitsString)
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &SipResponseDTMFCreate{config: config, Digits: digitsTemplate, PreWait: int(preWaitNum), PostWait: int(postWaitNum)}, nil
|
||||
},
|
||||
})
|
||||
}
|
||||
@@ -3,7 +3,7 @@ package processor
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"fmt"
|
||||
"errors"
|
||||
"text/template"
|
||||
|
||||
"github.com/jwetzell/showbridge-go/internal/config"
|
||||
@@ -39,13 +39,13 @@ func init() {
|
||||
tmpl, ok := params["template"]
|
||||
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("string.create requires a template parameter")
|
||||
return nil, errors.New("string.create requires a template parameter")
|
||||
}
|
||||
|
||||
templateString, ok := tmpl.(string)
|
||||
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("string.create template must be a string")
|
||||
return nil, errors.New("string.create template must be a string")
|
||||
}
|
||||
|
||||
templateTemplate, err := template.New("template").Parse(templateString)
|
||||
|
||||
81
internal/processor/string-create_test.go
Normal file
81
internal/processor/string-create_test.go
Normal file
@@ -0,0 +1,81 @@
|
||||
package processor_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"text/template"
|
||||
|
||||
"github.com/jwetzell/showbridge-go/internal/processor"
|
||||
)
|
||||
|
||||
type TestStruct struct {
|
||||
Data string
|
||||
}
|
||||
|
||||
func (t TestStruct) GetData() string {
|
||||
return t.Data
|
||||
}
|
||||
|
||||
func TestGoodStringCreate(t *testing.T) {
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
template string
|
||||
payload any
|
||||
expected string
|
||||
}{
|
||||
{
|
||||
name: "string payload",
|
||||
template: "{{.}}",
|
||||
payload: "hello",
|
||||
expected: "hello",
|
||||
},
|
||||
{
|
||||
name: "number payload",
|
||||
template: "{{.}}",
|
||||
payload: 4,
|
||||
expected: "4",
|
||||
},
|
||||
{
|
||||
name: "boolean payload",
|
||||
template: "{{.}}",
|
||||
payload: true,
|
||||
expected: "true",
|
||||
},
|
||||
{
|
||||
name: "struct payload - field",
|
||||
template: "{{.Data}}",
|
||||
payload: TestStruct{Data: "test"},
|
||||
expected: "test",
|
||||
},
|
||||
{
|
||||
name: "struct payload - method",
|
||||
template: "{{.GetData}}",
|
||||
payload: TestStruct{Data: "test"},
|
||||
expected: "test",
|
||||
},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
template, err := template.New("template").Parse(test.template)
|
||||
if err != nil {
|
||||
t.Fatalf("string.create template parsing failed: %s", err)
|
||||
}
|
||||
|
||||
processor := &processor.StringCreate{Template: template}
|
||||
|
||||
got, err := processor.Process(t.Context(), test.payload)
|
||||
|
||||
gotStrings, ok := got.(string)
|
||||
if !ok {
|
||||
t.Fatalf("string.create returned a %T payload: %s", got, got)
|
||||
}
|
||||
if err != nil {
|
||||
t.Fatalf("string.create failed: %s", err)
|
||||
}
|
||||
if gotStrings != test.expected {
|
||||
t.Fatalf("string.create got %s, expected %s", got, test.expected)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -2,7 +2,7 @@ package processor
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"errors"
|
||||
|
||||
"github.com/jwetzell/showbridge-go/internal/config"
|
||||
)
|
||||
@@ -15,7 +15,7 @@ func (sd *StringDecode) Process(ctx context.Context, payload any) (any, error) {
|
||||
payloadBytes, ok := payload.([]byte)
|
||||
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("string.decode processor only accepts a []byte")
|
||||
return nil, errors.New("string.decode processor only accepts a []byte")
|
||||
}
|
||||
|
||||
payloadMessage := string(payloadBytes)
|
||||
|
||||
@@ -28,13 +28,13 @@ func TestGoodStringDecode(t *testing.T) {
|
||||
|
||||
gotString, ok := got.(string)
|
||||
if !ok {
|
||||
t.Errorf("string.decode returned a %T payload: %s", got, got)
|
||||
t.Fatalf("string.decode returned a %T payload: %s", got, got)
|
||||
}
|
||||
if err != nil {
|
||||
t.Errorf("string.decode failed: %s", err)
|
||||
t.Fatalf("string.decode failed: %s", err)
|
||||
}
|
||||
if gotString != test.expected {
|
||||
t.Errorf("string.decode got %s, expected %s", got, test.expected)
|
||||
t.Fatalf("string.decode got %s, expected %s", got, test.expected)
|
||||
}
|
||||
})
|
||||
}
|
||||
@@ -61,10 +61,10 @@ func TestBadStringDecode(t *testing.T) {
|
||||
got, err := test.processor.Process(t.Context(), test.payload)
|
||||
|
||||
if err == nil {
|
||||
t.Errorf("string.decode expected to fail but got payload: %s", got)
|
||||
t.Fatalf("string.decode expected to fail but got payload: %s", got)
|
||||
}
|
||||
if err.Error() != test.errorString {
|
||||
t.Errorf("string.decode got error '%s', expected '%s'", err.Error(), test.errorString)
|
||||
t.Fatalf("string.decode got error '%s', expected '%s'", err.Error(), test.errorString)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
@@ -2,7 +2,7 @@ package processor
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"errors"
|
||||
|
||||
"github.com/jwetzell/showbridge-go/internal/config"
|
||||
)
|
||||
@@ -15,7 +15,7 @@ func (se *StringEncode) Process(ctx context.Context, payload any) (any, error) {
|
||||
payloadString, ok := payload.(string)
|
||||
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("string.encode processor only accepts a string")
|
||||
return nil, errors.New("string.encode processor only accepts a string")
|
||||
}
|
||||
|
||||
payloadBytes := []byte(payloadString)
|
||||
|
||||
@@ -29,13 +29,13 @@ func TestGoodStringEncode(t *testing.T) {
|
||||
|
||||
gotBytes, ok := got.([]byte)
|
||||
if !ok {
|
||||
t.Errorf("string.encode returned a %T payload: %s", got, got)
|
||||
t.Fatalf("string.encode returned a %T payload: %s", got, got)
|
||||
}
|
||||
if err != nil {
|
||||
t.Errorf("string.encode failed: %s", err)
|
||||
t.Fatalf("string.encode failed: %s", err)
|
||||
}
|
||||
if !slices.Equal(gotBytes, test.expected) {
|
||||
t.Errorf("string.encode got %s, expected %s", got, test.expected)
|
||||
t.Fatalf("string.encode got %s, expected %s", got, test.expected)
|
||||
}
|
||||
})
|
||||
}
|
||||
@@ -62,10 +62,10 @@ func TestBadStringEncode(t *testing.T) {
|
||||
got, err := test.processor.Process(t.Context(), test.payload)
|
||||
|
||||
if err == nil {
|
||||
t.Errorf("string.encode expected to fail but got payload: %s", got)
|
||||
t.Fatalf("string.encode expected to fail but got payload: %s", got)
|
||||
}
|
||||
if err.Error() != test.errorString {
|
||||
t.Errorf("string.encode got error '%s', expected '%s'", err.Error(), test.errorString)
|
||||
t.Fatalf("string.encode got error '%s', expected '%s'", err.Error(), test.errorString)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
@@ -2,7 +2,7 @@ package processor
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"errors"
|
||||
"regexp"
|
||||
|
||||
"github.com/jwetzell/showbridge-go/internal/config"
|
||||
@@ -13,22 +13,22 @@ type StringFilter struct {
|
||||
Pattern *regexp.Regexp
|
||||
}
|
||||
|
||||
func (se *StringFilter) Process(ctx context.Context, payload any) (any, error) {
|
||||
func (sf *StringFilter) Process(ctx context.Context, payload any) (any, error) {
|
||||
payloadString, ok := payload.(string)
|
||||
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("string.filter processor only accepts a string")
|
||||
return nil, errors.New("string.filter processor only accepts a string")
|
||||
}
|
||||
|
||||
if !se.Pattern.MatchString(payloadString) {
|
||||
if !sf.Pattern.MatchString(payloadString) {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
return payloadString, nil
|
||||
}
|
||||
|
||||
func (se *StringFilter) Type() string {
|
||||
return se.config.Type
|
||||
func (sf *StringFilter) Type() string {
|
||||
return sf.config.Type
|
||||
}
|
||||
|
||||
func init() {
|
||||
@@ -40,16 +40,16 @@ func init() {
|
||||
pattern, ok := params["pattern"]
|
||||
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("http.request.filter requires an pattern parameter")
|
||||
return nil, errors.New("string.filter requires a pattern parameter")
|
||||
}
|
||||
|
||||
patternString, ok := pattern.(string)
|
||||
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("http.request.filter pattern must be a string")
|
||||
return nil, errors.New("string.filter pattern must be a string")
|
||||
}
|
||||
|
||||
patternRegexp, err := regexp.Compile(fmt.Sprintf("^%s$", patternString))
|
||||
patternRegexp, err := regexp.Compile(patternString)
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
|
||||
@@ -2,7 +2,7 @@ package processor
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"errors"
|
||||
"strings"
|
||||
|
||||
"github.com/jwetzell/showbridge-go/internal/config"
|
||||
@@ -13,20 +13,20 @@ type StringSplit struct {
|
||||
Separator string
|
||||
}
|
||||
|
||||
func (se *StringSplit) Process(ctx context.Context, payload any) (any, error) {
|
||||
func (ss *StringSplit) Process(ctx context.Context, payload any) (any, error) {
|
||||
payloadString, ok := payload.(string)
|
||||
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("string.split only accepts a string")
|
||||
return nil, errors.New("string.split only accepts a string")
|
||||
}
|
||||
|
||||
payloadParts := strings.Split(payloadString, se.Separator)
|
||||
payloadParts := strings.Split(payloadString, ss.Separator)
|
||||
|
||||
return payloadParts, nil
|
||||
}
|
||||
|
||||
func (se *StringSplit) Type() string {
|
||||
return se.config.Type
|
||||
func (ss *StringSplit) Type() string {
|
||||
return ss.config.Type
|
||||
}
|
||||
|
||||
func init() {
|
||||
@@ -38,13 +38,13 @@ func init() {
|
||||
separator, ok := params["separator"]
|
||||
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("string.split requires a separator")
|
||||
return nil, errors.New("string.split requires a separator")
|
||||
}
|
||||
|
||||
separatorString, ok := separator.(string)
|
||||
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("string.split separator must be a string")
|
||||
return nil, errors.New("string.split separator must be a string")
|
||||
}
|
||||
|
||||
return &StringSplit{config: config, Separator: separatorString}, nil
|
||||
|
||||
@@ -28,19 +28,19 @@ func TestGoodStringSplit(t *testing.T) {
|
||||
|
||||
gotStrings, ok := got.([]string)
|
||||
if !ok {
|
||||
t.Errorf("string.split returned a %T payload: %s", got, got)
|
||||
t.Fatalf("string.split returned a %T payload: %s", got, got)
|
||||
}
|
||||
if err != nil {
|
||||
t.Errorf("string.split failed: %s", err)
|
||||
t.Fatalf("string.split failed: %s", err)
|
||||
}
|
||||
if !slices.Equal(gotStrings, test.expected) {
|
||||
t.Errorf("string.split got %s, expected %s", got, test.expected)
|
||||
t.Fatalf("string.split got %s, expected %s", got, test.expected)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestBasStringSplit(t *testing.T) {
|
||||
func TestBadStringSplit(t *testing.T) {
|
||||
tests := []struct {
|
||||
processor processor.Processor
|
||||
name string
|
||||
@@ -60,10 +60,10 @@ func TestBasStringSplit(t *testing.T) {
|
||||
got, err := test.processor.Process(t.Context(), test.payload)
|
||||
|
||||
if err == nil {
|
||||
t.Errorf("string.split expected error but got none, payload: %s", got)
|
||||
t.Fatalf("string.split expected error but got none, payload: %s", got)
|
||||
}
|
||||
if err.Error() != test.errorString {
|
||||
t.Errorf("string.split got error '%s', expected '%s'", err.Error(), test.errorString)
|
||||
t.Fatalf("string.split got error '%s', expected '%s'", err.Error(), test.errorString)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
45
internal/processor/time-sleep.go
Normal file
45
internal/processor/time-sleep.go
Normal file
@@ -0,0 +1,45 @@
|
||||
package processor
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"time"
|
||||
|
||||
"github.com/jwetzell/showbridge-go/internal/config"
|
||||
)
|
||||
|
||||
type MetaDelay struct {
|
||||
config config.ProcessorConfig
|
||||
Duration time.Duration
|
||||
}
|
||||
|
||||
func (md *MetaDelay) Process(ctx context.Context, payload any) (any, error) {
|
||||
time.Sleep(md.Duration)
|
||||
return payload, nil
|
||||
}
|
||||
|
||||
func (md *MetaDelay) Type() string {
|
||||
return md.config.Type
|
||||
}
|
||||
|
||||
func init() {
|
||||
RegisterProcessor(ProcessorRegistration{
|
||||
Type: "time.sleep",
|
||||
New: func(config config.ProcessorConfig) (Processor, error) {
|
||||
params := config.Params
|
||||
|
||||
duration, ok := params["duration"]
|
||||
if !ok {
|
||||
return nil, errors.New("time.sleep requires a duration parameter")
|
||||
}
|
||||
|
||||
durationNum, ok := duration.(float64)
|
||||
|
||||
if !ok {
|
||||
return nil, errors.New("time.sleep duration must be number")
|
||||
}
|
||||
|
||||
return &MetaDelay{config: config, Duration: time.Millisecond * time.Duration(durationNum)}, nil
|
||||
},
|
||||
})
|
||||
}
|
||||
@@ -2,13 +2,15 @@ package processor
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"errors"
|
||||
"strconv"
|
||||
|
||||
"github.com/jwetzell/showbridge-go/internal/config"
|
||||
)
|
||||
|
||||
type UintParse struct {
|
||||
Base int
|
||||
BitSize int
|
||||
config config.ProcessorConfig
|
||||
}
|
||||
|
||||
@@ -16,11 +18,10 @@ func (up *UintParse) Process(ctx context.Context, payload any) (any, error) {
|
||||
payloadString, ok := payload.(string)
|
||||
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("uint.parse processor only accepts a string")
|
||||
return nil, errors.New("uint.parse processor only accepts a string")
|
||||
}
|
||||
|
||||
// TODO(jwetzell): make base and bitSize configurable
|
||||
payloadUint, err := strconv.ParseUint(payloadString, 10, 64)
|
||||
payloadUint, err := strconv.ParseUint(payloadString, up.Base, up.BitSize)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -35,7 +36,31 @@ func init() {
|
||||
RegisterProcessor(ProcessorRegistration{
|
||||
Type: "uint.parse",
|
||||
New: func(config config.ProcessorConfig) (Processor, error) {
|
||||
return &UintParse{config: config}, nil
|
||||
params := config.Params
|
||||
baseNum := 10
|
||||
base, ok := params["base"]
|
||||
if ok {
|
||||
baseFloat, ok := base.(float64)
|
||||
|
||||
if !ok {
|
||||
return nil, errors.New("uint.parse base must be a number")
|
||||
}
|
||||
|
||||
baseNum = int(baseFloat)
|
||||
}
|
||||
|
||||
bitSizeNum := 64
|
||||
bitSize, ok := params["bitSize"]
|
||||
if ok {
|
||||
bitSizeFloat, ok := bitSize.(float64)
|
||||
|
||||
if !ok {
|
||||
return nil, errors.New("uint.parse bitSize must be a number")
|
||||
}
|
||||
|
||||
bitSizeNum = int(bitSizeFloat)
|
||||
}
|
||||
return &UintParse{config: config, Base: baseNum, BitSize: bitSizeNum}, nil
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
@@ -3,42 +3,158 @@ package processor_test
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/jwetzell/showbridge-go/internal/config"
|
||||
"github.com/jwetzell/showbridge-go/internal/processor"
|
||||
)
|
||||
|
||||
func TestUintParseFromRegistry(t *testing.T) {
|
||||
registration, ok := processor.ProcessorRegistry["uint.parse"]
|
||||
if !ok {
|
||||
t.Fatalf("uint.parse processor not registered")
|
||||
}
|
||||
|
||||
processorInstance, err := registration.New(config.ProcessorConfig{
|
||||
Type: "uint.parse",
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
t.Fatalf("failed to create uint.parse processor: %s", err)
|
||||
}
|
||||
|
||||
if processorInstance.Type() != "uint.parse" {
|
||||
t.Fatalf("uint.parse processor has wrong type: %s", processorInstance.Type())
|
||||
}
|
||||
}
|
||||
|
||||
func TestUintParseBadConfigBaseString(t *testing.T) {
|
||||
registration, ok := processor.ProcessorRegistry["uint.parse"]
|
||||
if !ok {
|
||||
t.Fatalf("uint.parse processor not registered")
|
||||
}
|
||||
|
||||
_, err := registration.New(config.ProcessorConfig{
|
||||
Type: "uint.parse",
|
||||
Params: map[string]any{
|
||||
"base": "10",
|
||||
},
|
||||
})
|
||||
|
||||
if err == nil {
|
||||
t.Fatalf("uint.parse should have returned an error for bad base config")
|
||||
}
|
||||
}
|
||||
|
||||
func TestUintParseBadConfigBitSizeString(t *testing.T) {
|
||||
registration, ok := processor.ProcessorRegistry["uint.parse"]
|
||||
if !ok {
|
||||
t.Fatalf("uint.parse processor not registered")
|
||||
}
|
||||
|
||||
_, err := registration.New(config.ProcessorConfig{
|
||||
Type: "uint.parse",
|
||||
Params: map[string]any{
|
||||
"bitSize": "64",
|
||||
},
|
||||
})
|
||||
|
||||
if err == nil {
|
||||
t.Fatalf("uint.parse should have returned an error for bad bitSize config")
|
||||
}
|
||||
}
|
||||
|
||||
func TestUintParseGoodConfig(t *testing.T) {
|
||||
registration, ok := processor.ProcessorRegistry["uint.parse"]
|
||||
if !ok {
|
||||
t.Fatalf("uint.parse processor not registered")
|
||||
}
|
||||
|
||||
processorInstance, err := registration.New(config.ProcessorConfig{
|
||||
Type: "uint.parse",
|
||||
Params: map[string]any{
|
||||
"base": 10.0,
|
||||
"bitSize": 64.0,
|
||||
},
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
t.Fatalf("uint.parse should have created processor but got error: %s", err)
|
||||
}
|
||||
|
||||
payload := "12345"
|
||||
expected := uint64(12345)
|
||||
|
||||
got, err := processorInstance.Process(t.Context(), payload)
|
||||
if err != nil {
|
||||
t.Fatalf("uint.parse processing failed: %s", err)
|
||||
}
|
||||
|
||||
gotUint, ok := got.(uint64)
|
||||
if !ok {
|
||||
t.Fatalf("uint.parse returned a %T payload: %s", got, got)
|
||||
}
|
||||
|
||||
if gotUint != expected {
|
||||
t.Fatalf("uint.parse got %d, expected %d", gotUint, expected)
|
||||
}
|
||||
}
|
||||
|
||||
func TestGoodUintParse(t *testing.T) {
|
||||
uintParser := processor.UintParse{}
|
||||
|
||||
tests := []struct {
|
||||
processor processor.Processor
|
||||
name string
|
||||
payload any
|
||||
expected uint64
|
||||
base int
|
||||
bitSize int
|
||||
}{
|
||||
{
|
||||
name: "positive number",
|
||||
payload: "12345",
|
||||
expected: 12345,
|
||||
base: 10,
|
||||
bitSize: 64,
|
||||
},
|
||||
{
|
||||
name: "zero",
|
||||
payload: "0",
|
||||
expected: 0,
|
||||
base: 10,
|
||||
bitSize: 64,
|
||||
},
|
||||
{
|
||||
name: "binary",
|
||||
payload: "1010101",
|
||||
expected: 85,
|
||||
base: 2,
|
||||
bitSize: 64,
|
||||
},
|
||||
{
|
||||
name: "hex",
|
||||
payload: "15F",
|
||||
expected: 351,
|
||||
base: 16,
|
||||
bitSize: 64,
|
||||
},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
uintParser := processor.UintParse{
|
||||
Base: test.base,
|
||||
BitSize: test.bitSize,
|
||||
}
|
||||
got, err := uintParser.Process(t.Context(), test.payload)
|
||||
|
||||
gotUint, ok := got.(uint64)
|
||||
if !ok {
|
||||
t.Errorf("uint.parse returned a %T payload: %s", got, got)
|
||||
t.Fatalf("uint.parse returned a %T payload: %s", got, got)
|
||||
}
|
||||
if err != nil {
|
||||
t.Errorf("uint.parse failed: %s", err)
|
||||
t.Fatalf("uint.parse failed: %s", err)
|
||||
}
|
||||
if gotUint != test.expected {
|
||||
t.Errorf("uint.parse got %d, expected %d", gotUint, test.expected)
|
||||
t.Fatalf("uint.parse got %d, expected %d", gotUint, test.expected)
|
||||
}
|
||||
})
|
||||
}
|
||||
@@ -50,18 +166,31 @@ func TestBadUintParse(t *testing.T) {
|
||||
processor processor.Processor
|
||||
name string
|
||||
payload any
|
||||
base int
|
||||
bitSize int
|
||||
errorString string
|
||||
}{
|
||||
{
|
||||
name: "non-string input",
|
||||
payload: []byte{0x01},
|
||||
base: 10,
|
||||
bitSize: 64,
|
||||
errorString: "uint.parse processor only accepts a string",
|
||||
},
|
||||
{
|
||||
name: "not uint string",
|
||||
payload: "-1234",
|
||||
base: 10,
|
||||
bitSize: 64,
|
||||
errorString: "strconv.ParseUint: parsing \"-1234\": invalid syntax",
|
||||
},
|
||||
{
|
||||
name: "bit overflow",
|
||||
payload: "123456789012345678901234567",
|
||||
base: 10,
|
||||
bitSize: 32,
|
||||
errorString: "strconv.ParseUint: parsing \"123456789012345678901234567\": value out of range",
|
||||
},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
@@ -69,11 +198,11 @@ func TestBadUintParse(t *testing.T) {
|
||||
got, err := uintParser.Process(t.Context(), test.payload)
|
||||
|
||||
if err == nil {
|
||||
t.Errorf("uint.parse expected to fail but succeeded, got: %v", got)
|
||||
t.Fatalf("uint.parse expected to fail but succeeded, got: %v", got)
|
||||
|
||||
}
|
||||
if err.Error() != test.errorString {
|
||||
t.Errorf("uint.parse got error '%s', expected '%s'", err.Error(), test.errorString)
|
||||
t.Fatalf("uint.parse got error '%s', expected '%s'", err.Error(), test.errorString)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
61
internal/processor/uint-random.go
Normal file
61
internal/processor/uint-random.go
Normal file
@@ -0,0 +1,61 @@
|
||||
package processor
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"math/rand/v2"
|
||||
|
||||
"github.com/jwetzell/showbridge-go/internal/config"
|
||||
)
|
||||
|
||||
type UintRandom struct {
|
||||
Min uint
|
||||
Max uint
|
||||
config config.ProcessorConfig
|
||||
}
|
||||
|
||||
func (ur *UintRandom) Process(ctx context.Context, payload any) (any, error) {
|
||||
payloadInt := rand.UintN(ur.Max-ur.Min+1) + ur.Min
|
||||
return payloadInt, nil
|
||||
}
|
||||
|
||||
func (ur *UintRandom) Type() string {
|
||||
return ur.config.Type
|
||||
}
|
||||
|
||||
func init() {
|
||||
RegisterProcessor(ProcessorRegistration{
|
||||
Type: "uint.random",
|
||||
New: func(config config.ProcessorConfig) (Processor, error) {
|
||||
params := config.Params
|
||||
|
||||
min, ok := params["min"]
|
||||
if !ok {
|
||||
return nil, errors.New("uint.random requires a min parameter")
|
||||
}
|
||||
|
||||
minFloat, ok := min.(float64)
|
||||
|
||||
if !ok {
|
||||
return nil, errors.New("uint.random min must be a number")
|
||||
}
|
||||
|
||||
max, ok := params["max"]
|
||||
if !ok {
|
||||
return nil, errors.New("uint.random requires a max parameter")
|
||||
}
|
||||
|
||||
maxFloat, ok := max.(float64)
|
||||
|
||||
if !ok {
|
||||
return nil, errors.New("uint.random max must be a number")
|
||||
}
|
||||
|
||||
if maxFloat < minFloat {
|
||||
return nil, errors.New("uint.random max must be greater than min")
|
||||
}
|
||||
|
||||
return &UintRandom{config: config, Min: uint(minFloat), Max: uint(maxFloat)}, nil
|
||||
},
|
||||
})
|
||||
}
|
||||
@@ -8,6 +8,11 @@ import (
|
||||
"github.com/jwetzell/showbridge-go/internal/processor"
|
||||
)
|
||||
|
||||
type routeContextKey string
|
||||
|
||||
var RouterContextKey routeContextKey = routeContextKey("router")
|
||||
var SourceContextKey routeContextKey = routeContextKey("source")
|
||||
|
||||
type RouteError struct {
|
||||
Index int
|
||||
Config config.RouteConfig
|
||||
@@ -16,19 +21,20 @@ type RouteError struct {
|
||||
|
||||
type RouteIOError struct {
|
||||
Index int
|
||||
Error error
|
||||
OutputErrors []error
|
||||
ProcessError error
|
||||
InputError error
|
||||
}
|
||||
|
||||
type RouteIO interface {
|
||||
HandleInput(sourceId string, payload any) []RouteIOError
|
||||
HandleOutput(sourceId string, destinationId string, payload any) error
|
||||
HandleInput(ctx context.Context, sourceId string, payload any) (bool, []RouteIOError)
|
||||
HandleOutput(ctx context.Context, destinationId string, payload any) []error
|
||||
}
|
||||
|
||||
type Route interface {
|
||||
Input() string
|
||||
Output() string
|
||||
HandleInput(ctx context.Context, sourceId string, payload any, router RouteIO) error
|
||||
HandleOutput(ctx context.Context, sourceId string, payload any, router RouteIO) error
|
||||
ProcessPayload(ctx context.Context, payload any) (any, error)
|
||||
}
|
||||
|
||||
type ProcessorRoute struct {
|
||||
@@ -66,21 +72,18 @@ func (r *ProcessorRoute) Output() string {
|
||||
return r.output
|
||||
}
|
||||
|
||||
func (r *ProcessorRoute) HandleInput(ctx context.Context, sourceId string, payload any, router RouteIO) error {
|
||||
var err error
|
||||
func (r *ProcessorRoute) ProcessPayload(ctx context.Context, payload any) (any, error) {
|
||||
for _, processor := range r.processors {
|
||||
payload, err = processor.Process(ctx, payload)
|
||||
processedPayload, err := processor.Process(ctx, payload)
|
||||
if err != nil {
|
||||
return err
|
||||
return nil, err
|
||||
}
|
||||
//NOTE(jwetzell) nil payload will result in the route being "terminated"
|
||||
if payload == nil {
|
||||
return nil
|
||||
if processedPayload == nil {
|
||||
return nil, nil
|
||||
}
|
||||
payload = processedPayload
|
||||
}
|
||||
return r.HandleOutput(ctx, sourceId, payload, router)
|
||||
}
|
||||
|
||||
func (r *ProcessorRoute) HandleOutput(ctx context.Context, sourceId string, payload any, router RouteIO) error {
|
||||
return router.HandleOutput(sourceId, r.output, payload)
|
||||
return payload, nil
|
||||
}
|
||||
|
||||
166
internal/route/route_test.go
Normal file
166
internal/route/route_test.go
Normal file
@@ -0,0 +1,166 @@
|
||||
package route_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"slices"
|
||||
"testing"
|
||||
|
||||
"github.com/jwetzell/showbridge-go/internal/config"
|
||||
"github.com/jwetzell/showbridge-go/internal/route"
|
||||
)
|
||||
|
||||
func TestRouteCreate(t *testing.T) {
|
||||
routeConfig := config.RouteConfig{
|
||||
Input: "input",
|
||||
Output: "output",
|
||||
}
|
||||
|
||||
testRoute, err := route.NewRoute(routeConfig)
|
||||
if err != nil {
|
||||
t.Fatalf("route failed to create: %v", err)
|
||||
}
|
||||
|
||||
if testRoute.Input() != routeConfig.Input {
|
||||
t.Fatalf("route input does not match expected input")
|
||||
}
|
||||
if testRoute.Output() != routeConfig.Output {
|
||||
t.Fatalf("route output does not match expected output")
|
||||
}
|
||||
}
|
||||
|
||||
type MockRouter struct{}
|
||||
|
||||
func (mr *MockRouter) HandleInput(ctx context.Context, sourceId string, payload any) (bool, []route.RouteIOError) {
|
||||
return false, []route.RouteIOError{}
|
||||
}
|
||||
|
||||
func (mr *MockRouter) HandleOutput(ctx context.Context, destinationId string, payload any) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func TestGoodRouteHandleInput(t *testing.T) {
|
||||
routeConfig := config.RouteConfig{
|
||||
Input: "input",
|
||||
Processors: []config.ProcessorConfig{
|
||||
{Type: "string.encode"},
|
||||
},
|
||||
Output: "output",
|
||||
}
|
||||
|
||||
testRoute, err := route.NewRoute(routeConfig)
|
||||
if err != nil {
|
||||
t.Fatalf("route failed to create: %v", err)
|
||||
}
|
||||
|
||||
inputData := "test input data"
|
||||
payload, err := testRoute.ProcessPayload(context.WithValue(t.Context(), route.RouterContextKey, &MockRouter{}), inputData)
|
||||
if err != nil {
|
||||
t.Fatalf("route ProcessPayload returned error: %v", err)
|
||||
}
|
||||
|
||||
payloadBytes, ok := payload.([]byte)
|
||||
if !ok {
|
||||
t.Fatalf("payload should be []byte got %T", payload)
|
||||
}
|
||||
|
||||
if !slices.Equal([]byte(inputData), payloadBytes) {
|
||||
t.Fatalf("route returned the wrong payload. expected: %+v got %+v", inputData, payloadBytes)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRouteHandleInputWithProcessorError(t *testing.T) {
|
||||
routeConfig := config.RouteConfig{
|
||||
Input: "input",
|
||||
Processors: []config.ProcessorConfig{
|
||||
{Type: "string.create", Params: map[string]any{"template": "{{.invalid}}}"}},
|
||||
},
|
||||
Output: "output",
|
||||
}
|
||||
|
||||
testRoute, err := route.NewRoute(routeConfig)
|
||||
if err != nil {
|
||||
t.Fatalf("route failed to create: %v", err)
|
||||
}
|
||||
|
||||
inputData := "test input data"
|
||||
_, err = testRoute.ProcessPayload(context.WithValue(t.Context(), route.RouterContextKey, &MockRouter{}), inputData)
|
||||
if err == nil {
|
||||
t.Fatalf("route HandleOutput did not return error for bad processor")
|
||||
}
|
||||
}
|
||||
|
||||
func TestRouteHandleNilPayload(t *testing.T) {
|
||||
routeConfig := config.RouteConfig{
|
||||
Input: "input",
|
||||
Processors: []config.ProcessorConfig{},
|
||||
Output: "output",
|
||||
}
|
||||
|
||||
testRoute, err := route.NewRoute(routeConfig)
|
||||
if err != nil {
|
||||
t.Fatalf("route failed to create: %v", err)
|
||||
return
|
||||
}
|
||||
|
||||
payload, err := testRoute.ProcessPayload(context.WithValue(t.Context(), route.RouterContextKey, &MockRouter{}), nil)
|
||||
if err != nil {
|
||||
t.Fatalf("route ProcessPayload returned error: %v", err)
|
||||
}
|
||||
if payload != nil {
|
||||
t.Fatalf("route returned the wrong payload")
|
||||
}
|
||||
}
|
||||
|
||||
func TestRouteHandleNilPayloadFromProcessor(t *testing.T) {
|
||||
routeConfig := config.RouteConfig{
|
||||
Input: "input",
|
||||
Processors: []config.ProcessorConfig{
|
||||
{Type: "script.js", Params: map[string]any{"program": "payload = undefined"}},
|
||||
},
|
||||
Output: "output",
|
||||
}
|
||||
|
||||
testRoute, err := route.NewRoute(routeConfig)
|
||||
if err != nil {
|
||||
t.Fatalf("route failed to create: %v", err)
|
||||
}
|
||||
|
||||
payload, err := testRoute.ProcessPayload(context.WithValue(t.Context(), route.RouterContextKey, &MockRouter{}), "test")
|
||||
if err != nil {
|
||||
t.Fatalf("route HandleOutput returned error for nil payload: %v", err)
|
||||
}
|
||||
|
||||
if payload != nil {
|
||||
t.Fatalf("route returned the wrong payload")
|
||||
}
|
||||
}
|
||||
|
||||
func TestRouteUnknownProcessor(t *testing.T) {
|
||||
routeConfig := config.RouteConfig{
|
||||
Input: "input",
|
||||
Processors: []config.ProcessorConfig{
|
||||
{Type: "asdfasdflkjalkj"},
|
||||
},
|
||||
Output: "output",
|
||||
}
|
||||
|
||||
_, err := route.NewRoute(routeConfig)
|
||||
if err == nil {
|
||||
t.Fatalf("route error expected when creating route with an unknown processor, got nil")
|
||||
}
|
||||
}
|
||||
|
||||
func TestRouteBadProcessorConfig(t *testing.T) {
|
||||
routeConfig := config.RouteConfig{
|
||||
Input: "input",
|
||||
Processors: []config.ProcessorConfig{
|
||||
{Type: "string.create", Params: map[string]any{}},
|
||||
},
|
||||
Output: "output",
|
||||
}
|
||||
|
||||
_, err := route.NewRoute(routeConfig)
|
||||
if err == nil {
|
||||
t.Fatalf("route error expected creating route with bad processor, got nil")
|
||||
}
|
||||
}
|
||||
102
router.go
102
router.go
@@ -2,9 +2,8 @@ package showbridge
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"errors"
|
||||
"log/slog"
|
||||
"os"
|
||||
"sync"
|
||||
|
||||
"github.com/jwetzell/showbridge-go/internal/config"
|
||||
@@ -18,25 +17,24 @@ type Router struct {
|
||||
ModuleInstances []module.Module
|
||||
RouteInstances []route.Route
|
||||
moduleWait sync.WaitGroup
|
||||
logger *slog.Logger
|
||||
}
|
||||
|
||||
func NewRouter(ctx context.Context, config config.Config) (*Router, []module.ModuleError, []route.RouteError) {
|
||||
logger := slog.New(slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{
|
||||
Level: slog.LevelInfo,
|
||||
}))
|
||||
|
||||
slog.SetDefault(logger)
|
||||
|
||||
slog.Debug("creating router")
|
||||
|
||||
routerContext, cancel := context.WithCancel(ctx)
|
||||
|
||||
router := Router{
|
||||
Context: routerContext,
|
||||
contextCancel: cancel,
|
||||
ModuleInstances: []module.Module{},
|
||||
RouteInstances: []route.Route{},
|
||||
logger: slog.Default().With("component", "router"),
|
||||
}
|
||||
|
||||
router.Context = context.WithValue(routerContext, route.RouterContextKey, &router)
|
||||
|
||||
router.logger.Debug("creating")
|
||||
|
||||
var moduleErrors []module.ModuleError
|
||||
|
||||
for moduleIndex, moduleDecl := range config.Modules {
|
||||
@@ -49,7 +47,7 @@ func NewRouter(ctx context.Context, config config.Config) (*Router, []module.Mod
|
||||
moduleErrors = append(moduleErrors, module.ModuleError{
|
||||
Index: moduleIndex,
|
||||
Config: moduleDecl,
|
||||
Error: fmt.Errorf("module type not defined"),
|
||||
Error: errors.New("module type not defined"),
|
||||
})
|
||||
continue
|
||||
}
|
||||
@@ -64,14 +62,14 @@ func NewRouter(ctx context.Context, config config.Config) (*Router, []module.Mod
|
||||
moduleErrors = append(moduleErrors, module.ModuleError{
|
||||
Index: moduleIndex,
|
||||
Config: moduleDecl,
|
||||
Error: fmt.Errorf("duplicate module id"),
|
||||
Error: errors.New("duplicate module id"),
|
||||
})
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
if !moduleInstanceExists {
|
||||
moduleInstance, err := moduleInfo.New(router.Context, moduleDecl, &router)
|
||||
moduleInstance, err := moduleInfo.New(router.Context, moduleDecl)
|
||||
if err != nil {
|
||||
if moduleErrors == nil {
|
||||
moduleErrors = []module.ModuleError{}
|
||||
@@ -110,51 +108,89 @@ func NewRouter(ctx context.Context, config config.Config) (*Router, []module.Mod
|
||||
}
|
||||
|
||||
func (r *Router) Run() {
|
||||
slog.Info("running router")
|
||||
r.logger.Info("running")
|
||||
for _, moduleInstance := range r.ModuleInstances {
|
||||
r.moduleWait.Add(1)
|
||||
go func() {
|
||||
r.moduleWait.Go(func() {
|
||||
err := moduleInstance.Run()
|
||||
if err != nil {
|
||||
slog.Error("error encountered running module", "id", moduleInstance.Id(), "error", err)
|
||||
r.logger.Error("error encountered running module", "error", err)
|
||||
}
|
||||
r.moduleWait.Done()
|
||||
}()
|
||||
})
|
||||
}
|
||||
<-r.Context.Done()
|
||||
r.logger.Debug("waiting for modules to exit")
|
||||
r.moduleWait.Wait()
|
||||
slog.Info("router done")
|
||||
r.logger.Info("done")
|
||||
}
|
||||
|
||||
func (r *Router) Stop() {
|
||||
r.logger.Debug("stopping")
|
||||
r.contextCancel()
|
||||
}
|
||||
|
||||
func (r *Router) HandleInput(sourceId string, payload any) []route.RouteIOError {
|
||||
var routingErrors []route.RouteIOError
|
||||
func (r *Router) HandleInput(ctx context.Context, sourceId string, payload any) (bool, []route.RouteIOError) {
|
||||
var routeIOErrors []route.RouteIOError
|
||||
routeFound := false
|
||||
|
||||
var routeWaitGroup sync.WaitGroup
|
||||
|
||||
for routeIndex, routeInstance := range r.RouteInstances {
|
||||
if routeInstance.Input() == sourceId {
|
||||
err := routeInstance.HandleInput(r.Context, sourceId, payload, r)
|
||||
routeWaitGroup.Go(func() {
|
||||
|
||||
routeFound = true
|
||||
routeContext := context.WithValue(ctx, route.SourceContextKey, sourceId)
|
||||
|
||||
payload, err := routeInstance.ProcessPayload(routeContext, payload)
|
||||
if err != nil {
|
||||
if routingErrors == nil {
|
||||
routingErrors = []route.RouteIOError{}
|
||||
if routeIOErrors == nil {
|
||||
routeIOErrors = []route.RouteIOError{}
|
||||
}
|
||||
routingErrors = append(routingErrors, route.RouteIOError{
|
||||
r.logger.Error("unable to process input", "route", routeIndex, "source", sourceId, "error", err)
|
||||
routeIOErrors = append(routeIOErrors, route.RouteIOError{
|
||||
Index: routeIndex,
|
||||
Error: err,
|
||||
ProcessError: err,
|
||||
})
|
||||
slog.Error("router unable to route input", "route", routeIndex, "source", sourceId, "error", err)
|
||||
return
|
||||
}
|
||||
|
||||
if payload == nil {
|
||||
r.logger.Error("no input after processing", "route", routeIndex, "source", sourceId)
|
||||
return
|
||||
}
|
||||
|
||||
outputErrors := r.HandleOutput(routeContext, routeInstance.Output(), payload)
|
||||
if outputErrors != nil {
|
||||
if routeIOErrors == nil {
|
||||
routeIOErrors = []route.RouteIOError{}
|
||||
}
|
||||
routeIOErrors = append(routeIOErrors, route.RouteIOError{
|
||||
Index: routeIndex,
|
||||
OutputErrors: outputErrors,
|
||||
})
|
||||
}
|
||||
})
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
return routingErrors
|
||||
routeWaitGroup.Wait()
|
||||
return routeFound, routeIOErrors
|
||||
}
|
||||
|
||||
func (r *Router) HandleOutput(sourceId string, destinationId string, payload any) error {
|
||||
func (r *Router) HandleOutput(ctx context.Context, destinationId string, payload any) []error {
|
||||
|
||||
var outputErrors []error
|
||||
for _, moduleInstance := range r.ModuleInstances {
|
||||
if moduleInstance.Id() == destinationId {
|
||||
return moduleInstance.Output(payload)
|
||||
err := moduleInstance.Output(ctx, payload)
|
||||
if err != nil {
|
||||
if outputErrors == nil {
|
||||
outputErrors = []error{}
|
||||
}
|
||||
outputErrors = append(outputErrors, err)
|
||||
r.logger.Error("unable to route output", "module", moduleInstance.Id(), "error", err)
|
||||
}
|
||||
}
|
||||
return fmt.Errorf("router could not find module instance for destination %s", destinationId)
|
||||
}
|
||||
return outputErrors
|
||||
}
|
||||
|
||||
341
router_test.go
Normal file
341
router_test.go
Normal file
@@ -0,0 +1,341 @@
|
||||
package showbridge_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"sync"
|
||||
"testing"
|
||||
|
||||
"github.com/jwetzell/showbridge-go"
|
||||
"github.com/jwetzell/showbridge-go/internal/config"
|
||||
"github.com/jwetzell/showbridge-go/internal/module"
|
||||
"github.com/jwetzell/showbridge-go/internal/route"
|
||||
)
|
||||
|
||||
type MockModule struct {
|
||||
config config.ModuleConfig
|
||||
ctx context.Context
|
||||
outputCount int
|
||||
router route.RouteIO
|
||||
logger *slog.Logger
|
||||
}
|
||||
|
||||
func (m *MockModule) Id() string {
|
||||
return m.config.Id
|
||||
}
|
||||
|
||||
func (m *MockModule) Output(context.Context, any) error {
|
||||
m.outputCount += 1
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m *MockModule) Run() error {
|
||||
<-m.ctx.Done()
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m *MockModule) Type() string {
|
||||
return m.config.Type
|
||||
}
|
||||
|
||||
func init() {
|
||||
module.RegisterModule(module.ModuleRegistration{
|
||||
Type: "mock.counter",
|
||||
New: func(ctx context.Context, config config.ModuleConfig) (module.Module, error) {
|
||||
|
||||
router, ok := ctx.Value(route.RouterContextKey).(route.RouteIO)
|
||||
|
||||
if !ok {
|
||||
return nil, errors.New("mock.counter unable to get router from context")
|
||||
}
|
||||
|
||||
return &MockModule{config: config, ctx: ctx, router: router, logger: slog.Default()}, nil
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
func TestNewRouter(t *testing.T) {
|
||||
routerConfig := config.Config{
|
||||
Modules: []config.ModuleConfig{
|
||||
{
|
||||
Id: "mock",
|
||||
Type: "mock.counter",
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
_, moduleErrors, routeErrors := showbridge.NewRouter(t.Context(), routerConfig)
|
||||
|
||||
if moduleErrors != nil {
|
||||
t.Fatalf("router should not have returned any module errors: %v", moduleErrors)
|
||||
}
|
||||
|
||||
if routeErrors != nil {
|
||||
t.Fatalf("router should not have returned any route errors: %v", routeErrors)
|
||||
}
|
||||
}
|
||||
|
||||
func TestNewRouterUnknownModuleType(t *testing.T) {
|
||||
routerConfig := config.Config{
|
||||
Modules: []config.ModuleConfig{
|
||||
{
|
||||
Id: "mock",
|
||||
Type: "asd.fjlkj23oiu4ksldj",
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
_, moduleErrors, _ := showbridge.NewRouter(t.Context(), routerConfig)
|
||||
|
||||
if moduleErrors == nil {
|
||||
t.Fatalf("router should have returned 'unknown module' module errors")
|
||||
}
|
||||
}
|
||||
|
||||
func TestNewRouterDuplicateModuleId(t *testing.T) {
|
||||
routerConfig := config.Config{
|
||||
Modules: []config.ModuleConfig{
|
||||
{
|
||||
Id: "mock",
|
||||
Type: "mock.counter",
|
||||
},
|
||||
{
|
||||
Id: "mock",
|
||||
Type: "mock.counter",
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
_, moduleErrors, _ := showbridge.NewRouter(t.Context(), routerConfig)
|
||||
|
||||
if moduleErrors == nil {
|
||||
t.Fatalf("router should have returned 'duplicate id' module error")
|
||||
}
|
||||
}
|
||||
|
||||
func TestRouterInputSingleRoute(t *testing.T) {
|
||||
routerConfig := config.Config{
|
||||
Modules: []config.ModuleConfig{
|
||||
{
|
||||
Id: "mock",
|
||||
Type: "mock.counter",
|
||||
},
|
||||
},
|
||||
Routes: []config.RouteConfig{
|
||||
{
|
||||
Input: "mock",
|
||||
Output: "mock",
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
router, moduleErrors, routeErrors := showbridge.NewRouter(t.Context(), routerConfig)
|
||||
|
||||
if moduleErrors != nil {
|
||||
t.Fatalf("router should not have returned any module errors: %v", moduleErrors)
|
||||
}
|
||||
|
||||
if routeErrors != nil {
|
||||
t.Fatalf("router should not have returned any route errors: %v", routeErrors)
|
||||
}
|
||||
|
||||
routerRunner := sync.WaitGroup{}
|
||||
|
||||
routerRunner.Go(func() {
|
||||
router.Run()
|
||||
})
|
||||
|
||||
defer router.Stop()
|
||||
|
||||
mockModuleInputCount := 3
|
||||
for i := range mockModuleInputCount {
|
||||
aRouteFound, routingErrors := router.HandleInput(t.Context(), "mock", fmt.Sprintf("test %d", i))
|
||||
|
||||
if routingErrors != nil {
|
||||
t.Fatalf("router should not have encountered routing errors")
|
||||
}
|
||||
|
||||
if !aRouteFound {
|
||||
t.Fatalf("router should have found a valid route for the input")
|
||||
}
|
||||
}
|
||||
|
||||
for _, moduleInstance := range router.ModuleInstances {
|
||||
if moduleInstance.Id() == "mock" {
|
||||
mockModuleInstance, ok := moduleInstance.(*MockModule)
|
||||
if !ok {
|
||||
t.Fatalf("couldn't get mock module")
|
||||
}
|
||||
|
||||
if mockModuleInstance.outputCount != mockModuleInputCount {
|
||||
t.Fatalf("mock module output count did not matched expected: %d got: %d", mockModuleInputCount, mockModuleInstance.outputCount)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestRouterInputMultipleRoutes(t *testing.T) {
|
||||
routerConfig := config.Config{
|
||||
Modules: []config.ModuleConfig{
|
||||
{
|
||||
Id: "mock",
|
||||
Type: "mock.counter",
|
||||
},
|
||||
},
|
||||
Routes: []config.RouteConfig{
|
||||
{
|
||||
Input: "mock",
|
||||
Output: "mock",
|
||||
},
|
||||
{
|
||||
Input: "mock",
|
||||
Output: "mock",
|
||||
},
|
||||
{
|
||||
Input: "mock",
|
||||
Output: "mock",
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
router, moduleErrors, routeErrors := showbridge.NewRouter(t.Context(), routerConfig)
|
||||
|
||||
if moduleErrors != nil {
|
||||
t.Fatalf("router should not have returned any module errors: %v", moduleErrors)
|
||||
}
|
||||
|
||||
if routeErrors != nil {
|
||||
t.Fatalf("router should not have returned any route errors: %v", routeErrors)
|
||||
}
|
||||
|
||||
routerRunner := sync.WaitGroup{}
|
||||
|
||||
routerRunner.Go(func() {
|
||||
router.Run()
|
||||
})
|
||||
|
||||
defer router.Stop()
|
||||
|
||||
mockModuleInputCount := 3
|
||||
for i := range mockModuleInputCount {
|
||||
aRouteFound, routingErrors := router.HandleInput(t.Context(), "mock", fmt.Sprintf("test %d", i))
|
||||
|
||||
if routingErrors != nil {
|
||||
t.Fatalf("router should not have encountered routing errors")
|
||||
}
|
||||
|
||||
if !aRouteFound {
|
||||
t.Fatalf("router should have found a valid route for the input")
|
||||
}
|
||||
}
|
||||
|
||||
for _, moduleInstance := range router.ModuleInstances {
|
||||
if moduleInstance.Id() == "mock" {
|
||||
mockModuleInstance, ok := moduleInstance.(*MockModule)
|
||||
if !ok {
|
||||
t.Fatalf("couldn't get mock module")
|
||||
}
|
||||
|
||||
if mockModuleInstance.outputCount != len(router.RouteInstances)*mockModuleInputCount {
|
||||
t.Fatalf("mock module output count did not matched expected: %d got: %d", len(router.RouteInstances)*mockModuleInputCount, mockModuleInstance.outputCount)
|
||||
}
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestRouterInputMultipleModules(t *testing.T) {
|
||||
routerConfig := config.Config{
|
||||
Modules: []config.ModuleConfig{
|
||||
{
|
||||
Id: "mock1",
|
||||
Type: "mock.counter",
|
||||
},
|
||||
{
|
||||
Id: "mock2",
|
||||
Type: "mock.counter",
|
||||
},
|
||||
},
|
||||
Routes: []config.RouteConfig{
|
||||
{
|
||||
Input: "mock1",
|
||||
Output: "mock1",
|
||||
},
|
||||
{
|
||||
Input: "mock2",
|
||||
Output: "mock2",
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
router, moduleErrors, routeErrors := showbridge.NewRouter(t.Context(), routerConfig)
|
||||
|
||||
if moduleErrors != nil {
|
||||
t.Fatalf("router should not have returned any module errors: %v", moduleErrors)
|
||||
}
|
||||
|
||||
if routeErrors != nil {
|
||||
t.Fatalf("router should not have returned any route errors: %v", routeErrors)
|
||||
}
|
||||
|
||||
routerRunner := sync.WaitGroup{}
|
||||
|
||||
routerRunner.Go(func() {
|
||||
router.Run()
|
||||
})
|
||||
|
||||
defer router.Stop()
|
||||
|
||||
mock1ModuleInputCount := 3
|
||||
for i := range mock1ModuleInputCount {
|
||||
aRouteFound, routingErrors := router.HandleInput(t.Context(), "mock1", fmt.Sprintf("test %d", i))
|
||||
|
||||
if routingErrors != nil {
|
||||
t.Fatalf("router should not have encountered routing errors")
|
||||
}
|
||||
|
||||
if !aRouteFound {
|
||||
t.Fatalf("router should have found a valid route for the input")
|
||||
}
|
||||
}
|
||||
|
||||
mock2ModuleInputCount := 2
|
||||
for i := range mock2ModuleInputCount {
|
||||
aRouteFound, routingErrors := router.HandleInput(t.Context(), "mock2", fmt.Sprintf("test %d", i))
|
||||
|
||||
if routingErrors != nil {
|
||||
t.Fatalf("router should not have encountered routing errors")
|
||||
}
|
||||
|
||||
if !aRouteFound {
|
||||
t.Fatalf("router should have found a valid route for the input")
|
||||
}
|
||||
}
|
||||
|
||||
for _, moduleInstance := range router.ModuleInstances {
|
||||
if moduleInstance.Id() == "mock1" {
|
||||
mockModuleInstance, ok := moduleInstance.(*MockModule)
|
||||
if !ok {
|
||||
t.Fatalf("couldn't get mock module")
|
||||
}
|
||||
|
||||
if mockModuleInstance.outputCount != mock1ModuleInputCount {
|
||||
t.Fatalf("mock module output count did not matched expected: %d got: %d", mock1ModuleInputCount, mockModuleInstance.outputCount)
|
||||
}
|
||||
break
|
||||
}
|
||||
if moduleInstance.Id() == "mock2" {
|
||||
mockModuleInstance, ok := moduleInstance.(*MockModule)
|
||||
if !ok {
|
||||
t.Fatalf("couldn't get mock module")
|
||||
}
|
||||
|
||||
if mockModuleInstance.outputCount != mock2ModuleInputCount {
|
||||
t.Fatalf("mock module output count did not matched expected: %d got: %d", mock2ModuleInputCount, mockModuleInstance.outputCount)
|
||||
}
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user