-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
103 lines (93 loc) · 2.28 KB
/
main.go
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
package main
import (
"context"
"fmt"
"github.com/charmbracelet/log"
"github.com/meshnet-gophers/meshtastic-go"
"github.com/meshnet-gophers/meshtastic-go/emulated"
pb "github.com/meshnet-gophers/meshtastic-go/meshtastic"
"github.com/meshnet-gophers/meshtastic-go/mqtt"
"github.com/meshnet-gophers/meshtastic-go/radio"
"github.com/meshnet-gophers/meshtastic-go/transport"
"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,
TCPListenAddr: "127.0.0.1:4403",
})
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 {
conn, err := transport.NewClientStreamConn(r.Conn(egCtx))
if err != nil {
return fmt.Errorf("creating connection: %w", err)
}
msg := &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 := conn.Write(msg); err != nil {
return fmt.Errorf("writing to radio: %w", err)
}
for {
select {
case <-egCtx.Done():
return nil
default:
}
msg := &pb.FromRadio{}
if err := conn.Read(msg); err != nil {
return fmt.Errorf("reading from radio: %w", err)
}
log.Info("FromRadio!!", "packet", msg)
}
})
if err := eg.Wait(); err != nil {
panic(err)
}
}