arr and dep with composite regex

This commit is contained in:
zhiqiang feng
2021-01-05 12:43:04 +08:00
parent 263f38633f
commit 67a28c2280
4 changed files with 113 additions and 58 deletions
+44 -27
View File
@@ -4,17 +4,28 @@ import (
"errors" "errors"
"regexp" "regexp"
"strings" "strings"
"time"
) )
const ( const (
FlopRegex = `(?m)\((?P<type>\w{3}).*(.|\n|\r).*\)` FlopRegex = `(?m)\((?P<type>\w{3}).*(.|\n|\r).*\)`
DestinationRegex = `(?P<dest>\w+)(?P<time>\d{0,4})?` DestinationRegex = `(?P<dest>[A-Z]+)(?P<time>\d{0,4})`
DOFRegex = `(?m)DOF\/(?P<time>\d{6})` DofValueRex = `(?m)DOF\/(?P<time>\d{6})`
TypeArrival = "ARR" TypeArrival = "ARR"
TypeDeparture = "DEP" TypeDeparture = "DEP"
TypeFlightPlan = "FPL" TypeFlightPlan = "FPL"
TypeDelay = "DLA" TypeDelay = "DLA"
DepRegex = `(?P<dep>\w+\d{0,4})`
ArrRegex = `(?P<arr>\w+\d{0,4})`
PlaneRegex = `(?P<plane>\w+\d+\/?\w\d+)`
DofRegex = `(?P<dof>DOF\/\d{6})`
KeyPlane = "plane"
CompositeDepArr = `(?m)\(` + `\w{3}` + "-" + PlaneRegex + "-" + DepRegex + "-" + ArrRegex + "-?" + DofRegex + `?\)`
KeyDeparture = "dep"
KeyArrival = "arr"
KeyDof = "dof"
) )
var flopRegex = regexp.MustCompile(FlopRegex) var flopRegex = regexp.MustCompile(FlopRegex)
@@ -25,18 +36,30 @@ func parseArrOrDep(telegram *Telegram, data string) {
* (ARR-CSN3126/A5040-ZSPD-ZBTJ1601) * (ARR-CSN3126/A5040-ZSPD-ZBTJ1601)
* (DEP-CSN3125/A4553-ZGHA0554-ZBTJ-DOF/120820) * (DEP-CSN3125/A4553-ZGHA0554-ZBTJ-DOF/120820)
*/ */
values := getSplitValues(data) //values := getSplitValues(data)
l := len(values) //l := len(values)
if l < 4 { //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 = parseAirportAndTime(values[2])
//telegram.Destination, telegram.DestinationTime = parseAirportAndTime(values[3])
//if l == 5 {
// parseDof(telegram, values[4])
//}
//fmt.Println(CompositeDepArr)
fields := getFields(CompositeDepArr, data)
if len(fields) < 1 {
telegram.ParseErrors = append(telegram.ParseErrors, errors.New("arrival data less than 4 ")) telegram.ParseErrors = append(telegram.ParseErrors, errors.New("arrival data less than 4 "))
return
} }
telegram.FlightNumber, telegram.RegisterNumber = parseFlight(fields[KeyPlane])
telegram.FlightNumber, telegram.RegisterNumber = parseFlight(values[1]) telegram.Departure, telegram.DepartureTime = parseAirportAndTime(fields[KeyDeparture])
telegram.Departure, telegram.DepartureTime = getAirportAndTime(values[2]) telegram.Destination, telegram.DestinationTime = parseAirportAndTime(fields[KeyArrival])
telegram.Destination, telegram.DestinationTime = getAirportAndTime(values[3]) dofValue := fields[KeyDof]
if l == 5 { if len(dofValue) > 0 {
parseDof(telegram, values[4]) telegram.DateOfFlight = parseDof(dofValue)
} }
telegram.Parsed = true telegram.Parsed = true
} }
@@ -57,10 +80,10 @@ func parseFlightPlan(telegram *Telegram, data string) {
values := getSplitValues(data) values := getSplitValues(data)
telegram.FlightNumber = values[1] telegram.FlightNumber = values[1]
telegram.Plane = values[3] telegram.Plane = values[3]
telegram.Departure, telegram.DepartureTime = getAirportAndTime(values[5]) telegram.Departure, telegram.DepartureTime = parseAirportAndTime(values[5])
destString := strings.Split(values[7], " ") destString := strings.Split(values[7], " ")
telegram.Destination, telegram.DestinationTime = getAirportAndTime(destString[0]) telegram.Destination, telegram.DestinationTime = parseAirportAndTime(destString[0])
parseDof(telegram, values[8]) //parseDof(telegram, values[8])
} }
@@ -71,15 +94,9 @@ func parseDelay(telegram *Telegram, data string) {
*/ */
values := getSplitValues(data) values := getSplitValues(data)
telegram.FlightNumber = values[1] telegram.FlightNumber = values[1]
telegram.Departure, telegram.DepartureTime = getAirportAndTime(values[2]) telegram.Departure, telegram.DepartureTime = parseAirportAndTime(values[2])
telegram.Destination, telegram.DestinationTime = getAirportAndTime(values[3]) telegram.Destination, telegram.DestinationTime = parseAirportAndTime(values[3])
if len(values) == 5 { //if len(values) == 5 {
parseDof(telegram, values[4]) // 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"])
}
} }
+60 -28
View File
@@ -5,12 +5,50 @@ import (
"testing" "testing"
) )
func TestArr(t *testing.T) { func TestAirportAndTimeRegex(t *testing.T) {
ast := assert.New(t)
text := "ZBTJ0554"
fields := getFields(DepRegex, text)
ast.Equal(1, len(fields), "Should get 1 filed")
ast.Equal("ZBTJ0554", fields[KeyDeparture])
fields = getFields(DestinationRegex, text)
ast.Equal(2, len(fields))
ast.Equal("ZBTJ", fields["dest"])
ast.Equal("0554", fields["time"])
}
func TestPlaneRegex(t *testing.T) {
ast := assert.New(t)
text := "JAE7433/A0132"
fields := getFields(PlaneRegex, text)
ast.Equal(1, len(fields), "Should get plane value")
ast.Equal("JAE7433/A0132", fields[KeyPlane])
}
func TestDofRegex(t *testing.T) {
ast := assert.New(t)
text := "DOF/120820"
fields := getFields(DofRegex, text)
ast.Equal(1, len(fields), "Should get dof value")
ast.Equal("DOF/120820", fields[KeyDof])
d := parseDof(text)
ast.Equal(2012, d.Year())
ast.Equal(8, int(d.Month()))
ast.Equal(20, d.Day())
}
func TestArrDep(t *testing.T) {
ast := assert.New(t) ast := assert.New(t)
s := `ZCZC TMQ2526 141605 s := `ZCZC TMQ2526 141605
FF ZBTJZPZX FF ZBTJZPZX
141604 ZBACZQZX 141604 ZBACZQZX
(ARR-JAE7433/A0132-RKSI-ZBTJ1604-DOF/120820) (ARR-JAE7433/A0132-RKSI-ZBTJ1604)
NNNN` NNNN`
tele, err := Parse(s) tele, err := Parse(s)
ast.Nil(err, "Should be no error") ast.Nil(err, "Should be no error")
@@ -26,6 +64,26 @@ NNNN`
ast.Equal("ZBTJ", tele.Destination, "destination airport is ZBTJ") ast.Equal("ZBTJ", tele.Destination, "destination airport is ZBTJ")
ast.Equal(16, tele.DestinationTime.Hour()) ast.Equal(16, tele.DestinationTime.Hour())
ast.Equal(4, tele.DestinationTime.Minute()) ast.Equal(4, tele.DestinationTime.Minute())
s = `ZCZC TMQ2526 141605
FF ZBTJZPZX
141604 ZBACZQZX
(DEP-JAE7433/A0132-RKSI1604-ZBTJ-DOF/120820)
NNNN`
tele, err = Parse(s)
ast.Nil(err, "Should be no error")
ast.Equal(tele.Complete, true, "Should be complete telegram")
ast.Equal(4, len(tele.Fields), "get 4 lines")
ast.Equal("ZCZC TMQ2526 141605", tele.Fields[0])
ast.Equal(2526, tele.Sequence, "TMQ must be 2526")
ast.Equal(false, tele.Type == "PLN", "Not a plan")
ast.Equal("DEP", tele.Type, "Flop type is DEP")
ast.Equal("JAE7433", tele.FlightNumber, "flight number is JAE7433")
ast.Equal("A0132", tele.RegisterNumber, "register number is A0132")
ast.Equal("RKSI", tele.Departure, "departure airport is RKSI")
ast.Equal("ZBTJ", tele.Destination, "destination airport is ZBTJ")
ast.Equal(16, tele.DepartureTime.Hour())
ast.Equal(4, tele.DepartureTime.Minute())
ast.Equal(2012, tele.DateOfFlight.Year()) ast.Equal(2012, tele.DateOfFlight.Year())
ast.Equal(8, int(tele.DateOfFlight.Month())) ast.Equal(8, int(tele.DateOfFlight.Month()))
ast.Equal(20, tele.DateOfFlight.Day()) ast.Equal(20, tele.DateOfFlight.Day())
@@ -41,32 +99,6 @@ NNNN`
} }
func TestDep(t *testing.T) {
ast := assert.New(t)
text := `ZCZC TMQ2626 142159
FF ZBTJZXZX
142158 ZBTJZPZX
(DEP-JAE7433/A3036-ZBTJ1816-EDDF-DOF/120820)
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(false, tele.Type == "PLN", "Not a plan")
ast.Equal("DEP", tele.Type, "Flop type is DEP")
ast.Equal("JAE7433", tele.FlightNumber, "flight number is JAE7433")
ast.Equal("A3036", tele.RegisterNumber, "register number is A3036")
ast.Equal("ZBTJ", tele.Departure, "departure airport is ZBTJ")
ast.Equal(18, tele.DepartureTime.Hour())
ast.Equal(16, tele.DepartureTime.Minute())
ast.Equal("EDDF", tele.Destination, "destination airport is EDDF")
ast.Equal(2012, tele.DateOfFlight.Year())
ast.Equal(8, int(tele.DateOfFlight.Month()))
ast.Equal(20, tele.DateOfFlight.Day())
}
func TestFpl(t *testing.T) { func TestFpl(t *testing.T) {
ast := assert.New(t) ast := assert.New(t)
text := `ZCZC TMQ2628 151601 text := `ZCZC TMQ2628 151601
+7 -1
View File
@@ -98,8 +98,14 @@ func parseFlight(s string) (flightNumber string, registerNumber string) {
return s, "" return s, ""
} }
func getAirportAndTime(s string) (airport string, arrivalTime time.Time) { func parseAirportAndTime(s string) (airport string, arrivalTime time.Time) {
fields := getFields(DestinationRegex, s) fields := getFields(DestinationRegex, s)
arrivalTime, _ = time.Parse("1504", fields["time"]) arrivalTime, _ = time.Parse("1504", fields["time"])
return fields["dest"], arrivalTime return fields["dest"], arrivalTime
} }
func parseDof(text string) time.Time {
value := getFields(DofValueRex, text)
dof, _ := time.Parse("060102", value["time"])
return dof
}
+2 -2
View File
@@ -64,13 +64,13 @@ func TestGetValues(t *testing.T) {
func TestParseDestAndTime(t *testing.T) { func TestParseDestAndTime(t *testing.T) {
ast := assert.New(t) ast := assert.New(t)
text := `ZBTJ1604` text := `ZBTJ1604`
dest, destTime := getAirportAndTime(text) dest, destTime := parseAirportAndTime(text)
ast.Equal("ZBTJ", dest) ast.Equal("ZBTJ", dest)
ast.Equal(16, destTime.Hour()) ast.Equal(16, destTime.Hour())
ast.Equal(4, destTime.Minute()) ast.Equal(4, destTime.Minute())
text = `ZBTJ` text = `ZBTJ`
dest, _ = getAirportAndTime(text) dest, _ = parseAirportAndTime(text)
ast.Equal("ZBTJ", dest) ast.Equal("ZBTJ", dest)
} }