protocol/aprs: refactored
This commit is contained in:
50
protocol/aprs/query.go
Normal file
50
protocol/aprs/query.go
Normal file
@@ -0,0 +1,50 @@
|
||||
package aprs
|
||||
|
||||
import (
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type Query struct {
|
||||
Type string `json:"type"`
|
||||
Latitude float64
|
||||
Longitude float64
|
||||
Radius float64 // radius in meters
|
||||
}
|
||||
|
||||
func (q Query) String() string {
|
||||
return q.Type
|
||||
}
|
||||
|
||||
type queryDecoder struct{}
|
||||
|
||||
func (queryDecoder) CanDecode(frame *Frame) bool {
|
||||
return len(frame.Raw) >= 3 && frame.Raw.Type() == '?'
|
||||
}
|
||||
|
||||
func (queryDecoder) Decode(frame *Frame) (data Data, err error) {
|
||||
var (
|
||||
kind = string(frame.Raw[1:])
|
||||
args string
|
||||
i int
|
||||
)
|
||||
if i = strings.IndexByte(kind, '?'); i == -1 {
|
||||
return &Query{
|
||||
Type: kind,
|
||||
}, nil
|
||||
} else {
|
||||
kind, args = kind[:i], kind[i+1:]
|
||||
}
|
||||
|
||||
query := &Query{Type: kind}
|
||||
|
||||
if part := strings.SplitN(strings.TrimSpace(args), ",", 3); len(part) == 3 {
|
||||
var radius int
|
||||
query.Latitude, _ = strconv.ParseFloat(part[0], 64)
|
||||
query.Longitude, _ = strconv.ParseFloat(part[1], 64)
|
||||
radius, _ = strconv.Atoi(part[2])
|
||||
query.Radius = milesToMeters(float64(radius))
|
||||
}
|
||||
|
||||
return query, nil
|
||||
}
|
||||
Reference in New Issue
Block a user