skyview/internal/parser/sbs1.go

95 lines
2.1 KiB
Go
Raw Normal View History

package parser
import (
"strconv"
"strings"
"time"
)
type Aircraft struct {
Hex string `json:"hex"`
Flight string `json:"flight,omitempty"`
Altitude int `json:"alt_baro,omitempty"`
GroundSpeed int `json:"gs,omitempty"`
Track int `json:"track,omitempty"`
Latitude float64 `json:"lat,omitempty"`
Longitude float64 `json:"lon,omitempty"`
VertRate int `json:"vert_rate,omitempty"`
Squawk string `json:"squawk,omitempty"`
Emergency bool `json:"emergency,omitempty"`
OnGround bool `json:"on_ground,omitempty"`
LastSeen time.Time `json:"last_seen"`
Messages int `json:"messages"`
}
type AircraftData struct {
Now int64 `json:"now"`
Messages int `json:"messages"`
Aircraft map[string]Aircraft `json:"aircraft"`
}
func ParseSBS1Line(line string) (*Aircraft, error) {
parts := strings.Split(strings.TrimSpace(line), ",")
if len(parts) < 22 {
return nil, nil
}
messageType := parts[1]
if messageType != "1" && messageType != "3" && messageType != "4" {
return nil, nil
}
aircraft := &Aircraft{
Hex: strings.TrimSpace(parts[4]),
LastSeen: time.Now(),
Messages: 1,
}
if parts[10] != "" {
aircraft.Flight = strings.TrimSpace(parts[10])
}
if parts[11] != "" {
if alt, err := strconv.Atoi(parts[11]); err == nil {
aircraft.Altitude = alt
}
}
if parts[12] != "" {
if gs, err := strconv.Atoi(parts[12]); err == nil {
aircraft.GroundSpeed = gs
}
}
if parts[13] != "" {
if track, err := strconv.Atoi(parts[13]); err == nil {
aircraft.Track = track
}
}
if parts[14] != "" && parts[15] != "" {
if lat, err := strconv.ParseFloat(parts[14], 64); err == nil {
aircraft.Latitude = lat
}
if lon, err := strconv.ParseFloat(parts[15], 64); err == nil {
aircraft.Longitude = lon
}
}
if parts[16] != "" {
if vr, err := strconv.Atoi(parts[16]); err == nil {
aircraft.VertRate = vr
}
}
if parts[17] != "" {
aircraft.Squawk = strings.TrimSpace(parts[17])
}
if parts[21] != "" {
aircraft.OnGround = parts[21] == "1"
}
return aircraft, nil
}