go-emlid/cmd/broadcast/main.go

62 lines
1.1 KiB
Go
Raw Normal View History

2024-07-30 14:28:39 +02:00
package main
import (
"context"
"encoding/json"
"flag"
"fmt"
"os"
reach "forge.cadoles.com/cadoles/go-emlid/reach/client"
"github.com/pkg/errors"
)
var (
host string = "192.168.42.1"
filter string = ""
)
func init() {
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)
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))
}
}