Files
tele-proc/parser/telegram.go
T

117 lines
2.7 KiB
Go
Raw Normal View History

2020-12-31 14:31:06 +08:00
package parser
2020-12-31 17:57:24 +08:00
import (
"regexp"
"strconv"
"strings"
2021-01-04 15:33:14 +08:00
"time"
2020-12-31 17:57:24 +08:00
)
2020-12-31 14:31:06 +08:00
type Telegram struct {
2021-01-04 15:33:14 +08:00
Text string
Complete bool
Parsed bool
Fields []string
Sequence int
Type string
FlopValue []string
FlightNumber string
2021-01-04 18:07:48 +08:00
Plane string
2021-01-05 18:04:17 +08:00
Aircraft string
2021-01-04 15:33:14 +08:00
RegisterNumber string
Departure string
DepartureTime time.Time
Destination string
DestinationTime time.Time
DateOfFlight time.Time
ParseErrors []error
2020-12-31 14:31:06 +08:00
}
2020-12-31 17:57:24 +08:00
const (
2021-01-04 15:33:14 +08:00
Start = "ZCZC"
End = "NNNN"
2020-12-31 17:57:24 +08:00
TmqRegex = `TMQ(?P<tmq>\d{4})`
)
2020-12-31 14:31:06 +08:00
//Parse
func Parse(text string) (*Telegram, error) {
var telegram = &Telegram{Text: text}
telegram.Complete = isStartAndEnd(text)
2020-12-31 17:57:24 +08:00
telegram.Fields = strings.Split(strings.Replace(text, "\r\n", "\n", -1), "\n")
telegram.Fields = clean(telegram.Fields)
seq := getFields(TmqRegex, telegram.Fields[0])
2021-01-04 15:33:14 +08:00
telegram.Sequence, _ = strconv.Atoi(seq["tmq"])
2020-12-31 17:57:24 +08:00
if isPln(*telegram) {
telegram.Type = TypePLN
2021-01-04 15:33:14 +08:00
} else if flopValues := getFields(FlopRegex, telegram.Text); len(flopValues) > 0 {
2021-01-04 18:07:48 +08:00
telegram.Type = flopValues["type"]
data := flopRegex.FindString(telegram.Text)
2021-01-04 15:33:14 +08:00
switch telegram.Type {
2021-01-04 16:02:13 +08:00
case TypeArrival, TypeDeparture:
parseArrOrDep(telegram, data)
2021-01-04 18:07:48 +08:00
case TypeFlightPlan:
parseFlightPlan(telegram, data)
case TypeDelay:
parseDelay(telegram, data)
2021-01-04 15:33:14 +08:00
}
2020-12-31 17:57:24 +08:00
}
2020-12-31 14:31:06 +08:00
return telegram, nil
}
func isStartAndEnd(s string) bool {
text := strings.TrimSpace(s)
return strings.HasPrefix(text, Start) && strings.HasSuffix(text, End)
}
2020-12-31 17:57:24 +08:00
func clean(fields []string) []string {
var values []string
for _, s := range fields {
values = append(values, strings.TrimSpace(s))
}
return values[:len(values)-1]
}
2021-01-04 15:33:14 +08:00
func getFields(regEx string, field string) (fields map[string]string) {
2020-12-31 17:57:24 +08:00
var compRegEx = regexp.MustCompile(regEx)
match := compRegEx.FindStringSubmatch(field)
fields = make(map[string]string)
for i, name := range compRegEx.SubexpNames() {
if i > 0 && i <= len(match) {
fields[name] = match[i]
}
}
return
}
2021-01-04 18:07:48 +08:00
func getSplitValues(s string) []string {
2021-01-04 15:33:14 +08:00
// get rid of ()
s = strings.TrimSpace(s)
s = s[1 : len(s)-1]
return strings.Split(s, "-")
}
func parseFlight(s string) (flightNumber string, registerNumber string) {
if l := strings.Index(s, "/"); l > 0 {
return s[0:l], s[l+1:]
2020-12-31 17:57:24 +08:00
}
2021-01-04 15:33:14 +08:00
return s, ""
}
2021-01-05 12:43:04 +08:00
func parseAirportAndTime(s string) (airport string, arrivalTime time.Time) {
2021-01-04 15:33:14 +08:00
fields := getFields(DestinationRegex, s)
arrivalTime, _ = time.Parse("1504", fields["time"])
return fields["dest"], arrivalTime
2020-12-31 17:57:24 +08:00
}
2021-01-05 12:43:04 +08:00
func parseDof(text string) time.Time {
value := getFields(DofValueRex, text)
2021-01-05 18:11:51 +08:00
dof, _ := time.Parse("060102", value[KeyTime])
2021-01-05 12:43:04 +08:00
return dof
}
2021-01-05 18:04:17 +08:00
func getOtherRegexWithName(name string) string {
return strings.ReplaceAll(OtherRegex, "NAME", name)
}