Ultima attività 1 month ago

APRS-IS Send / Receive

Revisione a6187433963ed91a690659c0b1f59d44e6aa9e8c

aprs.go Raw
1package main
2
3import (
4 "bufio"
5 "fmt"
6 "log"
7 "net"
8 "strings"
9 "time"
10)
11
12const server = "euro.aprs2.net"
13const port = 14580
14
15const softwareName = "goaprs"
16const softwareVersion = "0.1"
17
18const callsign = "PU2NVX"
19const passcode = "123456"
20const notifyMessage = "HUEBR"
21
22const SendInterval = time.Second * 60 * 5
23
24var myCoordinate = []float32{-23.640000,-46.650000}
25
26var lastCoordinateSent time.Time
27
28var loggedIn = false
29
30func 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
45func 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
49func parseAPRS(msg string, conn net.Conn) {
50 log.Printf("<- APRS: %s\n", msg)
51}
52
53func 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
65func 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
75func 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
96func 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
102func 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
110func 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}