Last active 1 month ago

APRS-IS Send / Receive

racerxdl's Avatar Lucas Teske revised this gist 7 years ago. Go to revision

1 file changed, 18 insertions, 1 deletion

aprs.go

@@ -20,6 +20,7 @@ const passcode = "123456"
20 20 const notifyMessage = "HUEBR"
21 21
22 22 const SendInterval = time.Second * 60 * 5
23 + const ReconnectInterval = time.Minute * 30
23 24
24 25 var myCoordinate = []float32{-23.640000,-46.650000}
25 26
@@ -107,7 +108,7 @@ func sendMessage(conn net.Conn, msg string) {
107 108 }
108 109 }
109 110
110 - func main() {
111 + func connect() (net.Conn, *bufio.Reader) {
111 112 log.Printf("-- Connecting to %s:%d\n", server, port)
112 113 conn, err := net.Dial("tcp", fmt.Sprintf("%s:%d", server, port))
113 114
@@ -133,6 +134,15 @@ func main() {
133 134 panic(err)
134 135 }
135 136
137 + return conn, reader
138 + }
139 +
140 + func main() {
141 + log.Printf("-- Restart Time: %s\n", ReconnectInterval)
142 + log.Printf("-- Send Interval: %s\n", SendInterval)
143 + connectTime := time.Now()
144 + conn, reader := connect()
145 +
136 146 log.Println("-- Receiving messages")
137 147 for {
138 148 line, _, err := reader.ReadLine()
@@ -145,5 +155,12 @@ func main() {
145 155 sendPosition(conn)
146 156 lastCoordinateSent = time.Now()
147 157 }
158 +
159 + if time.Since(connectTime) > ReconnectInterval {
160 + log.Println("-- Reconnect time exceeded. Reconnecting")
161 + _ = conn.Close()
162 + conn, reader = connect()
163 + connectTime = time.Now()
164 + }
148 165 }
149 166 }

racerxdl's Avatar Lucas Teske revised this gist 7 years ago. Go to revision

1 file changed, 149 insertions

aprs.go(file created)

@@ -0,0 +1,149 @@
1 + package main
2 +
3 + import (
4 + "bufio"
5 + "fmt"
6 + "log"
7 + "net"
8 + "strings"
9 + "time"
10 + )
11 +
12 + const server = "euro.aprs2.net"
13 + const port = 14580
14 +
15 + const softwareName = "goaprs"
16 + const softwareVersion = "0.1"
17 +
18 + const callsign = "PU2NVX"
19 + const passcode = "123456"
20 + const notifyMessage = "HUEBR"
21 +
22 + const SendInterval = time.Second * 60 * 5
23 +
24 + var myCoordinate = []float32{-23.640000,-46.650000}
25 +
26 + var lastCoordinateSent time.Time
27 +
28 + var loggedIn = false
29 +
30 + func Degrees2DMS(val float32) (deg int, min float32, n string) {
31 + n = "+"
32 + if val < 0 {
33 + val = -val
34 + n = "-"
35 + }
36 +
37 + deg = int(val)
38 + val -= float32(deg)
39 + val *= 60
40 + min = val
41 +
42 + return
43 + }
44 +
45 + func buildMessage(callsign, passcode, cmd string) string {
46 + return fmt.Sprintf("user %s pass %s vers %s %s %s", callsign, passcode, softwareName, softwareVersion, cmd)
47 + }
48 +
49 + func parseAPRS(msg string, conn net.Conn) {
50 + log.Printf("<- APRS: %s\n", msg)
51 + }
52 +
53 + func parseServer(msg string, conn net.Conn) {
54 + log.Printf("<- SERVER: %s\n", msg)
55 + if strings.Index(msg, "verified") > -1 {
56 + log.Printf("-- Logged in as %s\n", callsign)
57 +
58 + sendPosition(conn)
59 + lastCoordinateSent = time.Now()
60 +
61 + loggedIn = true
62 + }
63 + }
64 +
65 + func parseMessage(msg string, conn net.Conn) {
66 + if string(msg[0]) == "#" {
67 + // Server Message
68 + parseServer(msg[2:], conn)
69 + return
70 + }
71 +
72 + parseAPRS(msg, conn)
73 + }
74 +
75 + func formatPosition() string {
76 + lat := myCoordinate[0]
77 + lon := myCoordinate[1]
78 +
79 + latDeg, latMin, latV := Degrees2DMS(lat)
80 + lonDeg, lonMin, lonV := Degrees2DMS(lon)
81 +
82 + if latV == "-" {
83 + latV = "S"
84 + } else {
85 + latV = "N"
86 + }
87 + if lonV == "-" {
88 + lonV = "W"
89 + } else {
90 + lonV = "E"
91 + }
92 +
93 + return fmt.Sprintf("%02d%02.2f%s/%03d%02.2f%s-000/000/A=000000", latDeg, latMin, latV, lonDeg, lonMin, lonV)
94 + }
95 +
96 + func sendPosition(conn net.Conn) {
97 + log.Println("-- Sending Position")
98 + sendMessage(conn, buildMessage(callsign, passcode, "")) // First Line
99 + sendMessage(conn, fmt.Sprintf("%s>APDR15,TCPIP*:=%s %s", callsign, formatPosition(), notifyMessage))
100 + }
101 +
102 + func sendMessage(conn net.Conn, msg string) {
103 + log.Printf("-> %s\n" ,msg)
104 + _, err := conn.Write([]byte(msg + "\n"))
105 + if err != nil {
106 + panic(err)
107 + }
108 + }
109 +
110 + func main() {
111 + log.Printf("-- Connecting to %s:%d\n", server, port)
112 + conn, err := net.Dial("tcp", fmt.Sprintf("%s:%d", server, port))
113 +
114 + if err != nil {
115 + panic(err)
116 + }
117 +
118 + reader := bufio.NewReader(conn)
119 + time.Sleep(1 * time.Second)
120 + line, _, err := reader.ReadLine()
121 +
122 + if err != nil {
123 + panic(err)
124 + }
125 + log.Printf("<- %s\n", string(line))
126 +
127 + log.Println("-- Connected. Sending Login")
128 +
129 + msg := buildMessage(callsign, passcode, fmt.Sprintf("filter r/%f/%f/%d", myCoordinate[0], myCoordinate[1], 50))
130 + sendMessage(conn, msg)
131 +
132 + if err != nil {
133 + panic(err)
134 + }
135 +
136 + log.Println("-- Receiving messages")
137 + for {
138 + line, _, err := reader.ReadLine()
139 + if err != nil {
140 + panic(err)
141 + }
142 + parseMessage(string(line), conn)
143 +
144 + if time.Since(lastCoordinateSent) > SendInterval && loggedIn {
145 + sendPosition(conn)
146 + lastCoordinateSent = time.Now()
147 + }
148 + }
149 + }
Newer Older