diff options
| author | Noah Stride <[email protected]> | 2024-02-01 00:28:09 +0000 |
|---|---|---|
| committer | GitHub <[email protected]> | 2024-01-31 14:28:09 -1000 |
| commit | 463760ccf1cb99f387f5bf99ed83e31772744671 (patch) | |
| tree | 99feec624d4f04542e0c4aaab037b4e0b937a9f4 /emulated/example/main.go | |
| parent | 202889338e29c416e810de888734a2d41c6b1069 (diff) | |
Rough initial radio emulator (#1)
* Super rough initial subscription to channels and sending node info
* Very ugly hacky FromRadio subscription
* Add more TODOs :weary:
* Add log message before we try to broadcast NodeInfo
* Add basic positioning broadcasting
* demo sending message from consumer
* Use const for BroadcastNodeID
* Config validation and defaults
* Tidy up examples/TODOs
* Tidy up example
* Handle position and devicetelemetry updates
* Fix dodgy merge
* Allow configuring interval for nodeinfo/position seperately
* Make broadcasted position configurable
* Add MQTTProtoTopic constant rather than modifying topic root
* Allow altitude of broadcasted position to be configured
* static check ignore
Diffstat (limited to 'emulated/example/main.go')
| -rw-r--r-- | emulated/example/main.go | 108 |
1 files changed, 108 insertions, 0 deletions
diff --git a/emulated/example/main.go b/emulated/example/main.go new file mode 100644 index 0000000..d0a16e7 --- /dev/null +++ b/emulated/example/main.go @@ -0,0 +1,108 @@ +package main + +import ( + pb "buf.build/gen/go/meshtastic/protobufs/protocolbuffers/go/meshtastic" + "context" + "fmt" + "github.com/charmbracelet/log" + "github.com/crypto-smoke/meshtastic-go" + "github.com/crypto-smoke/meshtastic-go/emulated" + "github.com/crypto-smoke/meshtastic-go/mqtt" + "github.com/crypto-smoke/meshtastic-go/radio" + "golang.org/x/sync/errgroup" + "time" +) + +func main() { + // TODO: Flesh this example out and make it configurable + ctx := context.Background() + log.SetLevel(log.DebugLevel) + + myNodeID := meshtastic.NodeID(3735928559) + r, err := emulated.NewRadio(emulated.Config{ + LongName: "EXAMPLE", + ShortName: "EMPL", + NodeID: myNodeID, + MQTTClient: &mqtt.DefaultClient, + Channels: &pb.ChannelSet{ + Settings: []*pb.ChannelSettings{ + { + Name: "LongFast", + Psk: radio.DefaultKey, + }, + }, + }, + BroadcastNodeInfoInterval: 5 * time.Minute, + + BroadcastPositionInterval: 5 * time.Minute, + // Hardcoded to the position of Buckingham Palace. + PositionLatitudeI: 515014760, + PositionLongitudeI: -1406340, + PositionAltitude: 2, + }) + if err != nil { + panic(err) + } + + eg, egCtx := errgroup.WithContext(ctx) + + eg.Go(func() error { + if err := r.Run(egCtx); err != nil { + return fmt.Errorf("running radio: %w", err) + } + return nil + }) + + eg.Go(func() error { + // Forgive me, for I have sinned. + // TODO: We need a way of knowing the radio has come up and is ready that's better than waiting ten seconds. + select { + case <-egCtx.Done(): + return nil + case <-time.After(10 * time.Second): + } + + err := r.ToRadio(egCtx, &pb.ToRadio{ + PayloadVariant: &pb.ToRadio_Packet{ + Packet: &pb.MeshPacket{ + From: myNodeID.Uint32(), + // This is hard coded to Noah's node ID + To: 2437877602, + PayloadVariant: &pb.MeshPacket_Decoded{ + Decoded: &pb.Data{ + Portnum: pb.PortNum_TEXT_MESSAGE_APP, + Payload: []byte("from main!!"), + }, + }, + }, + }, + }) + if err != nil { + return fmt.Errorf("sending to radio: %w", err) + } + + return nil + }) + + eg.Go(func() error { + ch := make(chan *pb.FromRadio) + defer close(ch) + err := r.FromRadio(egCtx, ch) + if err != nil { + return fmt.Errorf("setting up FromRadio subscriber: %w", err) + } + + for { + select { + case <-egCtx.Done(): + return nil + case fromRadio := <-ch: + log.Info("FromRadio!!", "packet", fromRadio) + } + } + }) + + if err := eg.Wait(); err != nil { + panic(err) + } +} |
