go-emlid/cmd/discover/main.go

41 lines
771 B
Go
Raw Normal View History

2024-08-02 12:57:07 +02:00
package main
import (
"context"
"encoding/json"
"fmt"
"os"
2024-08-06 09:32:02 +02:00
"time"
2024-08-02 12:57:07 +02:00
"forge.cadoles.com/cadoles/go-emlid/reach/discovery"
"github.com/pkg/errors"
)
func main() {
2024-08-06 09:32:02 +02:00
resolver := discovery.NewResolver()
found, err := resolver.Scan(context.Background(), 1*time.Second)
2024-08-02 12:57:07 +02:00
if err != nil {
fmt.Printf("[FATAL] %+v", errors.WithStack(err))
os.Exit(1)
}
2024-08-06 09:32:02 +02:00
for srv := range found {
2024-08-02 12:57:07 +02:00
data, err := json.MarshalIndent(struct {
2024-08-06 09:32:02 +02:00
Addr string `json:"addr"`
Name string `json:"name"`
Device string `json:"device"`
2024-08-02 12:57:07 +02:00
}{
2024-08-06 09:32:02 +02:00
Name: srv.Name,
Addr: fmt.Sprintf("%s:%d", srv.AddrV4.String(), srv.Port),
Device: srv.Device,
2024-08-02 12:57:07 +02:00
}, "", " ")
if err != nil {
fmt.Printf("[FATAL] %+v", errors.WithStack(err))
os.Exit(1)
}
fmt.Printf("%s\n", data)
}
}