-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathparser.go
141 lines (109 loc) · 2.87 KB
/
parser.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
package sbs1
import (
"errors"
"strconv"
"time"
"github.com/golang/geo/s1"
"github.com/golang/geo/s2"
)
func Parse(fields []string) (*Message, error) {
message := &Message{}
switch fields[0] {
case "SEL":
message.MessageType = MessageTypeSelectionChange
case "ID":
message.MessageType = MessageTypeNewId
case "AIR":
message.MessageType = MessageTypeNewAircraft
case "STA":
message.MessageType = MessageTypeStatusAircraft
case "CLK":
message.MessageType = MessageTypeClick
case "MSG":
message.MessageType = MessageTypeTransmission
default:
return nil, ErrUnkownMessageType
}
switch fields[1] {
case "1":
message.TransmissionType = TransmissionTypeESIdentAndCategory
case "2":
message.TransmissionType = TransmissionTypeESSurfacePos
case "3":
message.TransmissionType = TransmissionTypeESAirbornePos
case "4":
message.TransmissionType = TransmissionTypeESAirborneVel
case "5":
message.TransmissionType = TransmissionTypeSurveillanceAlt
case "6":
message.TransmissionType = TransmissionTypeSurveillanceId
case "7":
message.TransmissionType = TransmissionTypeAirToAir
case "8":
message.TransmissionType = TransmissionTypeAllCallReply
default:
return nil, errors.New("unknown transmission type: " + fields[1])
}
message.SessionId = fields[2]
message.AircraftId = fields[3]
message.HexId = fields[4]
message.FlightId = fields[5]
if len(fields[6]) > 0 {
generated, err := time.Parse(timeFormat, fields[6]+"T"+fields[7])
if err != nil {
return nil, err
}
message.Generated = generated
}
if len(fields[8]) > 0 {
logged, err := time.Parse(timeFormat, fields[8]+"T"+fields[9])
if err != nil {
return nil, err
}
message.Logged = logged
}
message.Callsign = fields[10]
if len(fields) > 11 && len(fields[11]) > 0 {
altitude, err := strconv.ParseInt(fields[11], 10, 32)
if err != nil {
return nil, err
}
message.Altitude = int32(altitude)
}
if len(fields) > 12 && len(fields[12]) > 0 {
groundSpeed, err := strconv.ParseFloat(fields[12], 16)
if err != nil {
return nil, err
}
message.GroundSpeed = int32(groundSpeed)
}
if len(fields) > 13 && len(fields[13]) > 0 {
track, err := strconv.ParseFloat(fields[13], 64)
if err != nil {
return nil, err
}
message.Track = s1.Angle(track) * s1.Degree
}
if len(fields) > 15 && len(fields[14]) > 0 && len(fields[15]) > 0 {
latitude, err := strconv.ParseFloat(fields[14], 64)
if err != nil {
return nil, err
}
longitude, err := strconv.ParseFloat(fields[15], 64)
if err != nil {
return nil, err
}
message.Coordinates = s2.LatLngFromDegrees(latitude, longitude)
}
if len(fields) > 16 && len(fields[16]) > 0 {
verticalRate, err := strconv.ParseFloat(fields[16], 16)
if err != nil {
return nil, err
}
message.VerticalRate = int16(verticalRate)
}
if len(fields) > 17 {
message.Squawk = fields[17]
}
return message, nil
}