aboutsummaryrefslogtreecommitdiff
path: root/emulated/example/main.go
blob: 1e9b82bfb88141164d0a05d1c7c566ca84cc2482 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
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)

	nodeID, err := meshtastic.RandomNodeID()
	if err != nil {
		panic(err)
	}
	r, err := emulated.NewRadio(emulated.Config{
		LongName:   "EXAMPLE",
		ShortName:  "EMPL",
		NodeID:     nodeID,
		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: nodeID.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)
	}
}