add fpl dla
This commit is contained in:
@@ -0,0 +1,85 @@
|
||||
package parser
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"regexp"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
const (
|
||||
FlopRegex = `(?m)\((?P<type>\w{3}).*(.|\n|\r).*\)`
|
||||
DestinationRegex = `(?P<dest>\w+)(?P<time>\d{0,4})?`
|
||||
DOFRegex = `(?m)DOF\/(?P<time>\d{6})`
|
||||
TypeArrival = "ARR"
|
||||
TypeDeparture = "DEP"
|
||||
TypeFlightPlan = "FPL"
|
||||
TypeDelay = "DLA"
|
||||
)
|
||||
|
||||
var flopRegex = regexp.MustCompile(FlopRegex)
|
||||
|
||||
func parseArrOrDep(telegram *Telegram, data string) {
|
||||
/**
|
||||
* example:
|
||||
* (ARR-CSN3126/A5040-ZSPD-ZBTJ1601)
|
||||
* (DEP-CSN3125/A4553-ZGHA0554-ZBTJ-DOF/120820)
|
||||
*/
|
||||
values := getSplitValues(data)
|
||||
l := len(values)
|
||||
if l < 4 {
|
||||
telegram.ParseErrors = append(telegram.ParseErrors, errors.New("arrival data less than 4 "))
|
||||
return
|
||||
}
|
||||
|
||||
telegram.FlightNumber, telegram.RegisterNumber = parseFlight(values[1])
|
||||
telegram.Departure, telegram.DepartureTime = getAirportAndTime(values[2])
|
||||
telegram.Destination, telegram.DestinationTime = getAirportAndTime(values[3])
|
||||
if l == 5 {
|
||||
parseDof(telegram, values[4])
|
||||
}
|
||||
telegram.Parsed = true
|
||||
}
|
||||
|
||||
func parseFlightPlan(telegram *Telegram, data string) {
|
||||
/**
|
||||
* example:
|
||||
* (FPL-AAR997-IS
|
||||
* -B763/H-SDHIPRWZ/S
|
||||
* -RKSI0120
|
||||
* -N0473F320 NOPIK1B NOPIK G597 GONAV/N0473F320 G597 AGAVO/K0876S0980
|
||||
* G597 DONVO A326 ANRAT ANRAT2A
|
||||
* -ZBTJ0126 ZBAA
|
||||
* -EET/ZSHA0025 ZYSH0039 ZBPE0059 REG/HL7507 SEL/KRHJ OPR/AAR
|
||||
* DOF/090516 RMK/TCAS II EQUIPPED NAV/RNAV1 RNAV5 STS/CARGO)
|
||||
* NNNN
|
||||
*/
|
||||
values := getSplitValues(data)
|
||||
telegram.FlightNumber = values[1]
|
||||
telegram.Plane = values[3]
|
||||
telegram.Departure, telegram.DepartureTime = getAirportAndTime(values[5])
|
||||
destString := strings.Split(values[7], " ")
|
||||
telegram.Destination, telegram.DestinationTime = getAirportAndTime(destString[0])
|
||||
parseDof(telegram, values[8])
|
||||
|
||||
}
|
||||
|
||||
func parseDelay(telegram *Telegram, data string) {
|
||||
/**
|
||||
* sample:
|
||||
* (DLA‐ABC123‐NZAA0145‐VTBS‐DOF/091120)
|
||||
*/
|
||||
values := getSplitValues(data)
|
||||
telegram.FlightNumber = values[1]
|
||||
telegram.Departure, telegram.DepartureTime = getAirportAndTime(values[2])
|
||||
telegram.Destination, telegram.DestinationTime = getAirportAndTime(values[3])
|
||||
if len(values) == 5 {
|
||||
parseDof(telegram, values[4])
|
||||
}
|
||||
}
|
||||
|
||||
func parseDof(telegram *Telegram, text string) {
|
||||
if dof := getFields(DOFRegex, text); len(dof) == 1 {
|
||||
telegram.DateOfFlight, _ = time.Parse("060102", dof["time"])
|
||||
}
|
||||
}
|
||||
@@ -66,3 +66,57 @@ NNNN`
|
||||
ast.Equal(8, int(tele.DateOfFlight.Month()))
|
||||
ast.Equal(20, tele.DateOfFlight.Day())
|
||||
}
|
||||
|
||||
func TestFpl(t *testing.T) {
|
||||
ast := assert.New(t)
|
||||
text := `ZCZC TMQ2628 151601
|
||||
FF ZBTJZPZX
|
||||
151600 RKSSAARO
|
||||
(FPL-AAR997-IS
|
||||
-B763/H-SDHIPRWZ/S
|
||||
-RKSI0120
|
||||
-N0473F320 NOPIK1B NOPIK G597 GONAV/N0473F320 G597 AGAVO/K0876S0980
|
||||
G597 DONVO A326 ANRAT ANRAT2A
|
||||
-ZBTJ0126 ZBAA
|
||||
-EET/ZSHA0025 ZYSH0039 ZBPE0059 REG/HL7507 SEL/KRHJ OPR/AAR
|
||||
DOF/090516 RMK/TCAS II EQUIPPED NAV/RNAV1 RNAV5 STS/CARGO)
|
||||
NNNN
|
||||
`
|
||||
tele, err := Parse(text)
|
||||
ast.Nil(err, "Should be no error")
|
||||
ast.Equal(12, len(tele.Fields), "get 12 lines")
|
||||
ast.Equal(2628, tele.Sequence, "TMQ must be 2628")
|
||||
ast.Equal(false, tele.Type == "PLN", "Not a plan")
|
||||
ast.Equal("FPL", tele.Type, "Flop type is FPL")
|
||||
ast.Equal("AAR997", tele.FlightNumber, "flight number is AAR997")
|
||||
ast.Equal("B763/H", tele.Plane, "plane is B763/H")
|
||||
ast.Equal("RKSI", tele.Departure, "departure airport is RKSI")
|
||||
ast.Equal(1, tele.DepartureTime.Hour())
|
||||
ast.Equal(20, tele.DepartureTime.Minute())
|
||||
ast.Equal("ZBTJ", tele.Destination)
|
||||
ast.Equal(1, tele.DestinationTime.Hour())
|
||||
ast.Equal(26, tele.DestinationTime.Minute())
|
||||
ast.Equal(2009, tele.DateOfFlight.Year())
|
||||
ast.Equal(5, int(tele.DateOfFlight.Month()))
|
||||
ast.Equal(16, tele.DateOfFlight.Day())
|
||||
}
|
||||
|
||||
func TestDelay(t *testing.T) {
|
||||
ast := assert.New(t)
|
||||
text := `ZCZC TMQ2626 142159
|
||||
FF ZBTJZXZX
|
||||
142158 ZBTJZPZX
|
||||
(DLA-JAE7433-NZAA0145-VTBS-DOF/091120)
|
||||
NNNN`
|
||||
|
||||
tele, err := Parse(text)
|
||||
ast.Nil(err, "Should be no error")
|
||||
ast.Equal(4, len(tele.Fields), "get 4 lines")
|
||||
ast.Equal("ZCZC TMQ2626 142159", tele.Fields[0])
|
||||
ast.Equal(2626, tele.Sequence, "TMQ must be 2626")
|
||||
ast.Equal("DLA", tele.Type, "Flop type is DLA")
|
||||
ast.Equal("JAE7433", tele.FlightNumber)
|
||||
//ast.Equal("NZAA", tele.Departure)
|
||||
//ast.Equal(1, tele.DepartureTime.Hour())
|
||||
//ast.Equal(45, tele.DepartureTime.Minute())
|
||||
}
|
||||
@@ -1,37 +0,0 @@
|
||||
package parser
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"time"
|
||||
)
|
||||
|
||||
const (
|
||||
FlopRegex = `(?m)^\((?P<flop>\w{3})-.*`
|
||||
DestinationRegex = `(?P<dest>[A-Z]+)(?P<time>\d{0,4})?`
|
||||
DOFRegex = `(?P<dof>\w+)/(?P<time>\d{6})`
|
||||
TypeArrival = "ARR"
|
||||
TypeDeparture = "DEP"
|
||||
)
|
||||
|
||||
func parseArrOrDep(telegram *Telegram, data string) {
|
||||
values := getFlopValues(data)
|
||||
l := len(values)
|
||||
if l < 4 {
|
||||
telegram.ParseErrors = append(telegram.ParseErrors, errors.New("arrival data less than 4 "))
|
||||
return
|
||||
}
|
||||
|
||||
telegram.FlightNumber, telegram.RegisterNumber = parseFlight(values[1])
|
||||
telegram.Departure, telegram.DepartureTime = getAirportAndTime(values[2])
|
||||
telegram.Destination, telegram.DestinationTime = getAirportAndTime(values[3])
|
||||
if l == 5 {
|
||||
parseDof(telegram, values[4])
|
||||
}
|
||||
telegram.Parsed = true
|
||||
}
|
||||
|
||||
func parseDof(telegram *Telegram, text string) {
|
||||
if dof := getFields(DOFRegex, text); dof["dof"] == "DOF" {
|
||||
telegram.DateOfFlight, _ = time.Parse("060102", dof["time"])
|
||||
}
|
||||
}
|
||||
+8
-4
@@ -16,6 +16,7 @@ type Telegram struct {
|
||||
Type string
|
||||
FlopValue []string
|
||||
FlightNumber string
|
||||
Plane string
|
||||
RegisterNumber string
|
||||
Departure string
|
||||
DepartureTime time.Time
|
||||
@@ -42,12 +43,15 @@ func Parse(text string) (*Telegram, error) {
|
||||
if isPln(*telegram) {
|
||||
telegram.Type = TypePLN
|
||||
} else if flopValues := getFields(FlopRegex, telegram.Text); len(flopValues) > 0 {
|
||||
telegram.Type = flopValues["flop"]
|
||||
r := regexp.MustCompile(FlopRegex)
|
||||
data := r.FindString(telegram.Text)
|
||||
telegram.Type = flopValues["type"]
|
||||
data := flopRegex.FindString(telegram.Text)
|
||||
switch telegram.Type {
|
||||
case TypeArrival, TypeDeparture:
|
||||
parseArrOrDep(telegram, data)
|
||||
case TypeFlightPlan:
|
||||
parseFlightPlan(telegram, data)
|
||||
case TypeDelay:
|
||||
parseDelay(telegram, data)
|
||||
}
|
||||
}
|
||||
return telegram, nil
|
||||
@@ -80,7 +84,7 @@ func getFields(regEx string, field string) (fields map[string]string) {
|
||||
return
|
||||
}
|
||||
|
||||
func getFlopValues(s string) []string {
|
||||
func getSplitValues(s string) []string {
|
||||
// get rid of ()
|
||||
s = strings.TrimSpace(s)
|
||||
s = s[1 : len(s)-1]
|
||||
|
||||
@@ -77,8 +77,8 @@ func TestParseDestAndTime(t *testing.T) {
|
||||
|
||||
func TestGetFlopValues(t *testing.T) {
|
||||
ast := assert.New(t)
|
||||
text := `(ARR-JAE7433/A0132-RKSI-ZBTJ1604-ZBTJ)`
|
||||
values := getFlopValues(text)
|
||||
text := "(ARR-JAE7433/A0132-RKSI-ZBTJ1604-ZBTJ)"
|
||||
values := getSplitValues(text)
|
||||
ast.Equal(5, len(values))
|
||||
ast.Equal("ARR", values[0])
|
||||
ast.Equal("JAE7433/A0132", values[1])
|
||||
@@ -86,6 +86,12 @@ func TestGetFlopValues(t *testing.T) {
|
||||
ast.Equal("ZBTJ1604", values[3])
|
||||
ast.Equal("ZBTJ", values[4])
|
||||
|
||||
values = getSplitValues("(DLA-JAE7433-NZAA0145-VTBS-DOF/091120)")
|
||||
ast.Equal(5, len(values))
|
||||
ast.Equal("JAE7433", values[1])
|
||||
ast.Equal("NZAA0145", values[2])
|
||||
ast.Equal("VTBS", values[3])
|
||||
ast.Equal("DOF/091120", values[4])
|
||||
}
|
||||
|
||||
func TestGetFlight(t *testing.T) {
|
||||
|
||||
Reference in New Issue
Block a user