aprs.go
· 3.2 KiB · Go
Brut
package main
import (
"bufio"
"fmt"
"log"
"net"
"strings"
"time"
)
const server = "euro.aprs2.net"
const port = 14580
const softwareName = "goaprs"
const softwareVersion = "0.1"
const callsign = "PU2NVX"
const passcode = "123456"
const notifyMessage = "HUEBR"
const SendInterval = time.Second * 60 * 5
var myCoordinate = []float32{-23.640000,-46.650000}
var lastCoordinateSent time.Time
var loggedIn = false
func Degrees2DMS(val float32) (deg int, min float32, n string) {
n = "+"
if val < 0 {
val = -val
n = "-"
}
deg = int(val)
val -= float32(deg)
val *= 60
min = val
return
}
func buildMessage(callsign, passcode, cmd string) string {
return fmt.Sprintf("user %s pass %s vers %s %s %s", callsign, passcode, softwareName, softwareVersion, cmd)
}
func parseAPRS(msg string, conn net.Conn) {
log.Printf("<- APRS: %s\n", msg)
}
func parseServer(msg string, conn net.Conn) {
log.Printf("<- SERVER: %s\n", msg)
if strings.Index(msg, "verified") > -1 {
log.Printf("-- Logged in as %s\n", callsign)
sendPosition(conn)
lastCoordinateSent = time.Now()
loggedIn = true
}
}
func parseMessage(msg string, conn net.Conn) {
if string(msg[0]) == "#" {
// Server Message
parseServer(msg[2:], conn)
return
}
parseAPRS(msg, conn)
}
func formatPosition() string {
lat := myCoordinate[0]
lon := myCoordinate[1]
latDeg, latMin, latV := Degrees2DMS(lat)
lonDeg, lonMin, lonV := Degrees2DMS(lon)
if latV == "-" {
latV = "S"
} else {
latV = "N"
}
if lonV == "-" {
lonV = "W"
} else {
lonV = "E"
}
return fmt.Sprintf("%02d%02.2f%s/%03d%02.2f%s-000/000/A=000000", latDeg, latMin, latV, lonDeg, lonMin, lonV)
}
func sendPosition(conn net.Conn) {
log.Println("-- Sending Position")
sendMessage(conn, buildMessage(callsign, passcode, "")) // First Line
sendMessage(conn, fmt.Sprintf("%s>APDR15,TCPIP*:=%s %s", callsign, formatPosition(), notifyMessage))
}
func sendMessage(conn net.Conn, msg string) {
log.Printf("-> %s\n" ,msg)
_, err := conn.Write([]byte(msg + "\n"))
if err != nil {
panic(err)
}
}
func main() {
log.Printf("-- Connecting to %s:%d\n", server, port)
conn, err := net.Dial("tcp", fmt.Sprintf("%s:%d", server, port))
if err != nil {
panic(err)
}
reader := bufio.NewReader(conn)
time.Sleep(1 * time.Second)
line, _, err := reader.ReadLine()
if err != nil {
panic(err)
}
log.Printf("<- %s\n", string(line))
log.Println("-- Connected. Sending Login")
msg := buildMessage(callsign, passcode, fmt.Sprintf("filter r/%f/%f/%d", myCoordinate[0], myCoordinate[1], 50))
sendMessage(conn, msg)
if err != nil {
panic(err)
}
log.Println("-- Receiving messages")
for {
line, _, err := reader.ReadLine()
if err != nil {
panic(err)
}
parseMessage(string(line), conn)
if time.Since(lastCoordinateSent) > SendInterval && loggedIn {
sendPosition(conn)
lastCoordinateSent = time.Now()
}
}
}
| 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 | } |