mirror of
https://github.com/jwetzell/showbridge-go.git
synced 2026-04-26 21:05:30 +00:00
split out from net and misc module namespaces
This commit is contained in:
@@ -20,7 +20,7 @@ type HTTPClient struct {
|
|||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
RegisterModule(ModuleRegistration{
|
RegisterModule(ModuleRegistration{
|
||||||
Type: "net.http.client",
|
Type: "http.client",
|
||||||
New: func(ctx context.Context, config config.ModuleConfig, router route.RouteIO) (Module, error) {
|
New: func(ctx context.Context, config config.ModuleConfig, router route.RouteIO) (Module, error) {
|
||||||
|
|
||||||
return &HTTPClient{config: config, ctx: ctx, router: router}, nil
|
return &HTTPClient{config: config, ctx: ctx, router: router}, nil
|
||||||
@@ -52,11 +52,11 @@ func (hc *HTTPClient) Output(payload any) error {
|
|||||||
payloadRequest, ok := payload.(*http.Request)
|
payloadRequest, ok := payload.(*http.Request)
|
||||||
|
|
||||||
if !ok {
|
if !ok {
|
||||||
return fmt.Errorf("net.http.client is only able to output an http.Request")
|
return fmt.Errorf("http.client is only able to output an http.Request")
|
||||||
}
|
}
|
||||||
|
|
||||||
if hc.client == nil {
|
if hc.client == nil {
|
||||||
return fmt.Errorf("net.http.client client is nil")
|
return fmt.Errorf("http.client client is nil")
|
||||||
}
|
}
|
||||||
|
|
||||||
response, err := hc.client.Do(payloadRequest)
|
response, err := hc.client.Do(payloadRequest)
|
||||||
|
|||||||
@@ -25,18 +25,18 @@ type ResponseData struct {
|
|||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
RegisterModule(ModuleRegistration{
|
RegisterModule(ModuleRegistration{
|
||||||
Type: "net.http.server",
|
Type: "http.server",
|
||||||
New: func(ctx context.Context, config config.ModuleConfig, router route.RouteIO) (Module, error) {
|
New: func(ctx context.Context, config config.ModuleConfig, router route.RouteIO) (Module, error) {
|
||||||
params := config.Params
|
params := config.Params
|
||||||
port, ok := params["port"]
|
port, ok := params["port"]
|
||||||
if !ok {
|
if !ok {
|
||||||
return nil, fmt.Errorf("net.http.server requires a port parameter")
|
return nil, fmt.Errorf("http.server requires a port parameter")
|
||||||
}
|
}
|
||||||
|
|
||||||
portNum, ok := port.(float64)
|
portNum, ok := port.(float64)
|
||||||
|
|
||||||
if !ok {
|
if !ok {
|
||||||
return nil, fmt.Errorf("net.http.server port must be uint16")
|
return nil, fmt.Errorf("http.server port must be uint16")
|
||||||
}
|
}
|
||||||
|
|
||||||
return &HTTPServer{Port: uint16(portNum), config: config, ctx: ctx, router: router}, nil
|
return &HTTPServer{Port: uint16(portNum), config: config, ctx: ctx, router: router}, nil
|
||||||
@@ -94,7 +94,7 @@ func (hs *HTTPServer) Run() error {
|
|||||||
}()
|
}()
|
||||||
|
|
||||||
err := httpServer.ListenAndServe()
|
err := httpServer.ListenAndServe()
|
||||||
slog.Debug("net.http.server closed", "id", hs.Id())
|
slog.Debug("http.server closed", "id", hs.Id())
|
||||||
// TODO(jwetzell): handle server closed error differently
|
// TODO(jwetzell): handle server closed error differently
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
@@ -105,5 +105,5 @@ func (hs *HTTPServer) Run() error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (hs *HTTPServer) Output(payload any) error {
|
func (hs *HTTPServer) Output(payload any) error {
|
||||||
return fmt.Errorf("net.http.server output is not implemented")
|
return fmt.Errorf("http.server output is not implemented")
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -25,31 +25,31 @@ type MIDIClient struct {
|
|||||||
func init() {
|
func init() {
|
||||||
RegisterModule(ModuleRegistration{
|
RegisterModule(ModuleRegistration{
|
||||||
//TODO(jwetzell): find a better namespace than "misc"
|
//TODO(jwetzell): find a better namespace than "misc"
|
||||||
Type: "misc.midi.client",
|
Type: "midi.client",
|
||||||
New: func(ctx context.Context, config config.ModuleConfig, router route.RouteIO) (Module, error) {
|
New: func(ctx context.Context, config config.ModuleConfig, router route.RouteIO) (Module, error) {
|
||||||
params := config.Params
|
params := config.Params
|
||||||
input, ok := params["input"]
|
input, ok := params["input"]
|
||||||
|
|
||||||
if !ok {
|
if !ok {
|
||||||
return nil, fmt.Errorf("misc.midi.client requires a input parameter")
|
return nil, fmt.Errorf("midi.client requires a input parameter")
|
||||||
}
|
}
|
||||||
|
|
||||||
inputString, ok := input.(string)
|
inputString, ok := input.(string)
|
||||||
|
|
||||||
if !ok {
|
if !ok {
|
||||||
return nil, fmt.Errorf("misc.midi.client input must be a string")
|
return nil, fmt.Errorf("midi.client input must be a string")
|
||||||
}
|
}
|
||||||
|
|
||||||
output, ok := params["output"]
|
output, ok := params["output"]
|
||||||
|
|
||||||
if !ok {
|
if !ok {
|
||||||
return nil, fmt.Errorf("misc.midi.client requires a output parameter")
|
return nil, fmt.Errorf("midi.client requires a output parameter")
|
||||||
}
|
}
|
||||||
|
|
||||||
outputString, ok := output.(string)
|
outputString, ok := output.(string)
|
||||||
|
|
||||||
if !ok {
|
if !ok {
|
||||||
return nil, fmt.Errorf("misc.midi.client output must be a string")
|
return nil, fmt.Errorf("midi.client output must be a string")
|
||||||
}
|
}
|
||||||
|
|
||||||
return &MIDIClient{config: config, InputPort: inputString, OutputPort: outputString, ctx: ctx, router: router}, nil
|
return &MIDIClient{config: config, InputPort: inputString, OutputPort: outputString, ctx: ctx, router: router}, nil
|
||||||
@@ -70,7 +70,7 @@ func (mc *MIDIClient) Run() error {
|
|||||||
|
|
||||||
in, err := midi.FindInPort(mc.InputPort)
|
in, err := midi.FindInPort(mc.InputPort)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("misc.midi.client can't find input port: %s", mc.InputPort)
|
return fmt.Errorf("midi.client can't find input port: %s", mc.InputPort)
|
||||||
}
|
}
|
||||||
|
|
||||||
stop, err := midi.ListenTo(in, func(msg midi.Message, timestampms int32) {
|
stop, err := midi.ListenTo(in, func(msg midi.Message, timestampms int32) {
|
||||||
@@ -89,7 +89,7 @@ func (mc *MIDIClient) Run() error {
|
|||||||
out, err := midi.FindOutPort(mc.OutputPort)
|
out, err := midi.FindOutPort(mc.OutputPort)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("misc.midi.client can't find output port: %s", mc.OutputPort)
|
return fmt.Errorf("midi.client can't find output port: %s", mc.OutputPort)
|
||||||
}
|
}
|
||||||
|
|
||||||
send, err := midi.SendTo(out)
|
send, err := midi.SendTo(out)
|
||||||
@@ -106,13 +106,13 @@ func (mc *MIDIClient) Run() error {
|
|||||||
|
|
||||||
func (mc *MIDIClient) Output(payload any) error {
|
func (mc *MIDIClient) Output(payload any) error {
|
||||||
if mc.SendFunc == nil {
|
if mc.SendFunc == nil {
|
||||||
return fmt.Errorf("misc.midi.client output is not setup")
|
return fmt.Errorf("midi.client output is not setup")
|
||||||
}
|
}
|
||||||
|
|
||||||
payloadMessage, ok := payload.(midi.Message)
|
payloadMessage, ok := payload.(midi.Message)
|
||||||
|
|
||||||
if !ok {
|
if !ok {
|
||||||
return fmt.Errorf("misc.midi.client can only ouptut midi.Message")
|
return fmt.Errorf("midi.client can only ouptut midi.Message")
|
||||||
}
|
}
|
||||||
|
|
||||||
return mc.SendFunc(payloadMessage)
|
return mc.SendFunc(payloadMessage)
|
||||||
|
|||||||
@@ -22,43 +22,43 @@ type MQTTClient struct {
|
|||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
RegisterModule(ModuleRegistration{
|
RegisterModule(ModuleRegistration{
|
||||||
Type: "net.mqtt.client",
|
Type: "mqtt.client",
|
||||||
New: func(ctx context.Context, config config.ModuleConfig, router route.RouteIO) (Module, error) {
|
New: func(ctx context.Context, config config.ModuleConfig, router route.RouteIO) (Module, error) {
|
||||||
params := config.Params
|
params := config.Params
|
||||||
broker, ok := params["broker"]
|
broker, ok := params["broker"]
|
||||||
|
|
||||||
if !ok {
|
if !ok {
|
||||||
return nil, fmt.Errorf("net.mqtt.client requires a broker parameter")
|
return nil, fmt.Errorf("mqtt.client requires a broker parameter")
|
||||||
}
|
}
|
||||||
|
|
||||||
brokerString, ok := broker.(string)
|
brokerString, ok := broker.(string)
|
||||||
|
|
||||||
if !ok {
|
if !ok {
|
||||||
return nil, fmt.Errorf("net.mqtt.client broker must be string")
|
return nil, fmt.Errorf("mqtt.client broker must be string")
|
||||||
}
|
}
|
||||||
|
|
||||||
topic, ok := params["topic"]
|
topic, ok := params["topic"]
|
||||||
|
|
||||||
if !ok {
|
if !ok {
|
||||||
return nil, fmt.Errorf("net.mqtt.client requires a topic parameter")
|
return nil, fmt.Errorf("mqtt.client requires a topic parameter")
|
||||||
}
|
}
|
||||||
|
|
||||||
topicString, ok := topic.(string)
|
topicString, ok := topic.(string)
|
||||||
|
|
||||||
if !ok {
|
if !ok {
|
||||||
return nil, fmt.Errorf("net.mqtt.client topic must be string")
|
return nil, fmt.Errorf("mqtt.client topic must be string")
|
||||||
}
|
}
|
||||||
|
|
||||||
clientId, ok := params["clientId"]
|
clientId, ok := params["clientId"]
|
||||||
|
|
||||||
if !ok {
|
if !ok {
|
||||||
return nil, fmt.Errorf("net.mqtt.client requires a clientId parameter")
|
return nil, fmt.Errorf("mqtt.client requires a clientId parameter")
|
||||||
}
|
}
|
||||||
|
|
||||||
clientIdString, ok := clientId.(string)
|
clientIdString, ok := clientId.(string)
|
||||||
|
|
||||||
if !ok {
|
if !ok {
|
||||||
return nil, fmt.Errorf("net.mqtt.client clientId must be string")
|
return nil, fmt.Errorf("mqtt.client clientId must be string")
|
||||||
}
|
}
|
||||||
|
|
||||||
return &MQTTClient{config: config, Broker: brokerString, Topic: topicString, ClientID: clientIdString, ctx: ctx, router: router}, nil
|
return &MQTTClient{config: config, Broker: brokerString, Topic: topicString, ClientID: clientIdString, ctx: ctx, router: router}, nil
|
||||||
@@ -109,15 +109,15 @@ func (mc *MQTTClient) Output(payload any) error {
|
|||||||
fmt.Printf("payload type: %T\n", payload)
|
fmt.Printf("payload type: %T\n", payload)
|
||||||
|
|
||||||
if !ok {
|
if !ok {
|
||||||
return fmt.Errorf("net.mqtt.client is only able to output a MQTTMessage")
|
return fmt.Errorf("mqtt.client is only able to output a MQTTMessage")
|
||||||
}
|
}
|
||||||
|
|
||||||
if mc.client == nil {
|
if mc.client == nil {
|
||||||
return fmt.Errorf("net.mqtt.client client is not setup")
|
return fmt.Errorf("mqtt.client client is not setup")
|
||||||
}
|
}
|
||||||
|
|
||||||
if !mc.client.IsConnected() {
|
if !mc.client.IsConnected() {
|
||||||
return fmt.Errorf("net.mqtt.client is not connected")
|
return fmt.Errorf("mqtt.client is not connected")
|
||||||
}
|
}
|
||||||
|
|
||||||
token := mc.client.Publish(payloadMessage.Topic(), payloadMessage.Qos(), payloadMessage.Retained(), payloadMessage.Payload())
|
token := mc.client.Publish(payloadMessage.Topic(), payloadMessage.Qos(), payloadMessage.Retained(), payloadMessage.Payload())
|
||||||
|
|||||||
@@ -22,31 +22,31 @@ type NATSClient struct {
|
|||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
RegisterModule(ModuleRegistration{
|
RegisterModule(ModuleRegistration{
|
||||||
Type: "net.nats.client",
|
Type: "nats.client",
|
||||||
New: func(ctx context.Context, config config.ModuleConfig, router route.RouteIO) (Module, error) {
|
New: func(ctx context.Context, config config.ModuleConfig, router route.RouteIO) (Module, error) {
|
||||||
params := config.Params
|
params := config.Params
|
||||||
url, ok := params["url"]
|
url, ok := params["url"]
|
||||||
|
|
||||||
if !ok {
|
if !ok {
|
||||||
return nil, fmt.Errorf("net.nats.client requires a url parameter")
|
return nil, fmt.Errorf("nats.client requires a url parameter")
|
||||||
}
|
}
|
||||||
|
|
||||||
urlString, ok := url.(string)
|
urlString, ok := url.(string)
|
||||||
|
|
||||||
if !ok {
|
if !ok {
|
||||||
return nil, fmt.Errorf("net.nats.client url must be string")
|
return nil, fmt.Errorf("nats.client url must be string")
|
||||||
}
|
}
|
||||||
|
|
||||||
subject, ok := params["subject"]
|
subject, ok := params["subject"]
|
||||||
|
|
||||||
if !ok {
|
if !ok {
|
||||||
return nil, fmt.Errorf("net.nats.client requires a subject parameter")
|
return nil, fmt.Errorf("nats.client requires a subject parameter")
|
||||||
}
|
}
|
||||||
|
|
||||||
subjectString, ok := subject.(string)
|
subjectString, ok := subject.(string)
|
||||||
|
|
||||||
if !ok {
|
if !ok {
|
||||||
return nil, fmt.Errorf("net.nats.client subject must be string")
|
return nil, fmt.Errorf("nats.client subject must be string")
|
||||||
}
|
}
|
||||||
|
|
||||||
return &NATSClient{config: config, URL: urlString, Subject: subjectString, ctx: ctx, router: router}, nil
|
return &NATSClient{config: config, URL: urlString, Subject: subjectString, ctx: ctx, router: router}, nil
|
||||||
@@ -96,15 +96,15 @@ func (nc *NATSClient) Output(payload any) error {
|
|||||||
payloadMessage, ok := payload.(processor.NATSMessage)
|
payloadMessage, ok := payload.(processor.NATSMessage)
|
||||||
|
|
||||||
if !ok {
|
if !ok {
|
||||||
return fmt.Errorf("net.nats.client is only able to output NATSMessage")
|
return fmt.Errorf("nats.client is only able to output NATSMessage")
|
||||||
}
|
}
|
||||||
|
|
||||||
if nc.client == nil {
|
if nc.client == nil {
|
||||||
return fmt.Errorf("net.nats.client client is not setup")
|
return fmt.Errorf("nats.client client is not setup")
|
||||||
}
|
}
|
||||||
|
|
||||||
if !nc.client.IsConnected() {
|
if !nc.client.IsConnected() {
|
||||||
return fmt.Errorf("net.nats.client is not connected")
|
return fmt.Errorf("nats.client is not connected")
|
||||||
}
|
}
|
||||||
|
|
||||||
err := nc.client.Publish(payloadMessage.Subject, payloadMessage.Payload)
|
err := nc.client.Publish(payloadMessage.Subject, payloadMessage.Payload)
|
||||||
|
|||||||
@@ -22,7 +22,7 @@ type PSNClient struct {
|
|||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
RegisterModule(ModuleRegistration{
|
RegisterModule(ModuleRegistration{
|
||||||
Type: "net.psn.client",
|
Type: "psn.client",
|
||||||
New: func(ctx context.Context, config config.ModuleConfig, router route.RouteIO) (Module, error) {
|
New: func(ctx context.Context, config config.ModuleConfig, router route.RouteIO) (Module, error) {
|
||||||
|
|
||||||
return &PSNClient{config: config, decoder: psn.NewDecoder(), ctx: ctx, router: router}, nil
|
return &PSNClient{config: config, decoder: psn.NewDecoder(), ctx: ctx, router: router}, nil
|
||||||
@@ -76,7 +76,7 @@ func (pc *PSNClient) Run() error {
|
|||||||
message := buffer[:numBytes]
|
message := buffer[:numBytes]
|
||||||
err := pc.decoder.Decode(message)
|
err := pc.decoder.Decode(message)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
slog.Error("net.psn.client problem decoding psn traffic", "id", pc.Id(), "error", err)
|
slog.Error("psn.client problem decoding psn traffic", "id", pc.Id(), "error", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if pc.router != nil {
|
if pc.router != nil {
|
||||||
@@ -84,7 +84,7 @@ func (pc *PSNClient) Run() error {
|
|||||||
pc.router.HandleInput(pc.Id(), tracker)
|
pc.router.HandleInput(pc.Id(), tracker)
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
slog.Error("net.psn.client has no router", "id", pc.Id())
|
slog.Error("psn.client has no router", "id", pc.Id())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -92,5 +92,5 @@ func (pc *PSNClient) Run() error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (pc *PSNClient) Output(payload any) error {
|
func (pc *PSNClient) Output(payload any) error {
|
||||||
return fmt.Errorf("net.psn.client output is not implemented")
|
return fmt.Errorf("psn.client output is not implemented")
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -27,19 +27,19 @@ type SerialClient struct {
|
|||||||
func init() {
|
func init() {
|
||||||
RegisterModule(ModuleRegistration{
|
RegisterModule(ModuleRegistration{
|
||||||
//TODO(jwetzell): find a better namespace than "misc"
|
//TODO(jwetzell): find a better namespace than "misc"
|
||||||
Type: "misc.serial.client",
|
Type: "serial.client",
|
||||||
New: func(ctx context.Context, config config.ModuleConfig, router route.RouteIO) (Module, error) {
|
New: func(ctx context.Context, config config.ModuleConfig, router route.RouteIO) (Module, error) {
|
||||||
params := config.Params
|
params := config.Params
|
||||||
port, ok := params["port"]
|
port, ok := params["port"]
|
||||||
|
|
||||||
if !ok {
|
if !ok {
|
||||||
return nil, fmt.Errorf("misc.serial.client requires a port parameter")
|
return nil, fmt.Errorf("serial.client requires a port parameter")
|
||||||
}
|
}
|
||||||
|
|
||||||
portString, ok := port.(string)
|
portString, ok := port.(string)
|
||||||
|
|
||||||
if !ok {
|
if !ok {
|
||||||
return nil, fmt.Errorf("misc.serial.client port must be a string")
|
return nil, fmt.Errorf("serial.client port must be a string")
|
||||||
}
|
}
|
||||||
|
|
||||||
framingMethod := "RAW"
|
framingMethod := "RAW"
|
||||||
@@ -50,7 +50,7 @@ func init() {
|
|||||||
framingMethodString, ok := framingMethodRaw.(string)
|
framingMethodString, ok := framingMethodRaw.(string)
|
||||||
|
|
||||||
if !ok {
|
if !ok {
|
||||||
return nil, fmt.Errorf("misc.serial.client framing method must be a string")
|
return nil, fmt.Errorf("serial.client framing method must be a string")
|
||||||
}
|
}
|
||||||
framingMethod = framingMethodString
|
framingMethod = framingMethodString
|
||||||
}
|
}
|
||||||
@@ -63,12 +63,12 @@ func init() {
|
|||||||
|
|
||||||
buadRate, ok := params["baudRate"]
|
buadRate, ok := params["baudRate"]
|
||||||
if !ok {
|
if !ok {
|
||||||
return nil, fmt.Errorf("misc.serial.client requires a baudRate parameter")
|
return nil, fmt.Errorf("serial.client requires a baudRate parameter")
|
||||||
}
|
}
|
||||||
|
|
||||||
baudRateNum, ok := buadRate.(float64)
|
baudRateNum, ok := buadRate.(float64)
|
||||||
if !ok {
|
if !ok {
|
||||||
return nil, fmt.Errorf("misc.serial.client baudRate must be a number")
|
return nil, fmt.Errorf("serial.client baudRate must be a number")
|
||||||
}
|
}
|
||||||
|
|
||||||
mode := serial.Mode{
|
mode := serial.Mode{
|
||||||
@@ -92,7 +92,7 @@ func (mc *SerialClient) SetupPort() error {
|
|||||||
|
|
||||||
port, err := serial.Open(mc.Port, mc.Mode)
|
port, err := serial.Open(mc.Port, mc.Mode)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("misc.serial.client can't open input port: %s", mc.Port)
|
return fmt.Errorf("serial.client can't open input port: %s", mc.Port)
|
||||||
}
|
}
|
||||||
|
|
||||||
mc.port = port
|
mc.port = port
|
||||||
@@ -118,7 +118,7 @@ func (mc *SerialClient) Run() error {
|
|||||||
slog.Debug("router context done in module", "id", mc.Id())
|
slog.Debug("router context done in module", "id", mc.Id())
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
slog.Error("misc.serial.client", "id", mc.Id(), "error", err.Error())
|
slog.Error("serial.client", "id", mc.Id(), "error", err.Error())
|
||||||
time.Sleep(time.Second * 2)
|
time.Sleep(time.Second * 2)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
@@ -150,7 +150,7 @@ func (mc *SerialClient) Run() error {
|
|||||||
if mc.router != nil {
|
if mc.router != nil {
|
||||||
mc.router.HandleInput(mc.Id(), message)
|
mc.router.HandleInput(mc.Id(), message)
|
||||||
} else {
|
} else {
|
||||||
slog.Error("misc.serial.client has no router", "id", mc.Id())
|
slog.Error("serial.client has no router", "id", mc.Id())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -166,7 +166,7 @@ func (mc *SerialClient) Output(payload any) error {
|
|||||||
payloadBytes, ok := payload.([]byte)
|
payloadBytes, ok := payload.([]byte)
|
||||||
|
|
||||||
if !ok {
|
if !ok {
|
||||||
return fmt.Errorf("misc.serial.client can only ouptut bytes")
|
return fmt.Errorf("serial.client can only ouptut bytes")
|
||||||
}
|
}
|
||||||
|
|
||||||
_, err := mc.port.Write(mc.Framer.Encode(payloadBytes))
|
_, err := mc.port.Write(mc.Framer.Encode(payloadBytes))
|
||||||
|
|||||||
Reference in New Issue
Block a user