go-emlid/cmd/broadcast/main.go

74 lines
1.4 KiB
Go
Raw Permalink Normal View History

2024-07-30 14:28:39 +02:00
package main
import (
"context"
"encoding/json"
"flag"
"fmt"
2024-08-02 12:57:07 +02:00
"log/slog"
2024-07-30 14:28:39 +02:00
"os"
reach "forge.cadoles.com/cadoles/go-emlid/reach/client"
2024-08-02 12:57:07 +02:00
"forge.cadoles.com/cadoles/go-emlid/reach/client/logger"
2024-07-30 14:28:39 +02:00
"github.com/pkg/errors"
)
var (
2024-08-02 12:57:07 +02:00
host string = "192.168.42.1"
filter string = ""
rawLogLevel string = "ERROR"
2024-07-30 14:28:39 +02:00
)
func init() {
2024-08-02 12:57:07 +02:00
flag.StringVar(&rawLogLevel, "log-level", rawLogLevel, "log level")
2024-07-30 14:28:39 +02:00
flag.StringVar(&host, "host", host, "the reachrs module host")
flag.StringVar(&filter, "filter", filter, "filter the broadcast messages by name")
}
func main() {
flag.Parse()
ctx := context.Background()
client := reach.NewClient(host)
2024-08-02 12:57:07 +02:00
logLevel, err := logger.ParseLevel(rawLogLevel)
if err != nil {
fmt.Printf("[FATAL] %+v", errors.WithStack(err))
os.Exit(1)
}
slog.SetLogLoggerLevel(logLevel)
2024-07-30 14:28:39 +02:00
if err := client.Connect(ctx); err != nil {
fmt.Printf("[FATAL] %+v", errors.WithStack(err))
os.Exit(1)
}
defer func() {
if err := client.Close(ctx); err != nil {
fmt.Printf("[FATAL] %+v", errors.WithStack(err))
os.Exit(1)
}
}()
broadcasts, err := reach.OnBroadcast(ctx, client)
if err != nil {
fmt.Printf("[FATAL] %+v", errors.WithStack(err))
os.Exit(1)
}
for b := range broadcasts {
if filter != "" && b.Name != filter {
continue
}
data, err := json.MarshalIndent(b, "", " ")
if err != nil {
fmt.Printf("[ERROR] %+v", errors.WithStack(err))
continue
}
fmt.Println(string(data))
}
}