fix composite
This commit is contained in:
+40
-27
@@ -2,8 +2,8 @@ package parser
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
|
"fmt"
|
||||||
"regexp"
|
"regexp"
|
||||||
"strings"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
@@ -15,31 +15,57 @@ const (
|
|||||||
TypeFlightPlan = "FPL"
|
TypeFlightPlan = "FPL"
|
||||||
TypeDelay = "DLA"
|
TypeDelay = "DLA"
|
||||||
|
|
||||||
DepRegex = `-?(?P<dep>\w+\d{0,4})`
|
DepRegex = `(?P<dep>[A-Z]+\d{0,4})`
|
||||||
ArrRegex = `-?(?P<arr>\w+\d{0,4})`
|
ArrRegex = `(?P<arr>[A-Z]+\d{0,4})`
|
||||||
PlaneRegex = `-?(?P<plane>\w+\d+\/?\w\d+)`
|
PlaneRegex = `(?P<plane>\w+\d+\/?\w\d+)`
|
||||||
|
AircraftRegex = `-?(?P<aircraft>[\w\/]+)`
|
||||||
DofRegex = `-?(?P<dof>DOF\/\d{6})?`
|
DofRegex = `-?(?P<dof>DOF\/\d{6})?`
|
||||||
KeyPlane = "plane"
|
OtherRegex = `-?(?P<NAME>[A-Z0-9\/ \n\r]+)`
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* example:
|
* example:
|
||||||
* (ARR-CSN3126/A5040-ZSPD-ZBTJ1601)
|
* (ARR-CSN3126/A5040-ZSPD-ZBTJ1601)
|
||||||
* (DEP-CSN3125/A4553-ZGHA0554-ZBTJ-DOF/120820)
|
* (DEP-CSN3125/A4553-ZGHA0554-ZBTJ-DOF/120820)
|
||||||
*/
|
*/
|
||||||
CompositeDepArr = `(?m)\(\[A-Z]{3}` + PlaneRegex + DepRegex + ArrRegex + DofRegex + `\)`
|
CompositeDepArr = `(?m)\([A-Z]{3}-` + PlaneRegex + "-" + DepRegex + "-" + ArrRegex + DofRegex + `\)`
|
||||||
/**
|
/**
|
||||||
* sample:
|
* sample:
|
||||||
* (DLA‐ABC123‐NZAA0145‐VTBS‐DOF/091120)
|
* (DLA‐ABC123‐NZAA0145‐VTBS‐DOF/091120)
|
||||||
*/
|
*/
|
||||||
CompositeDelay = `(?m)\(DLA` + PlaneRegex + DepRegex + ArrRegex + DofRegex + `\)`
|
CompositeDelay = `(?m)\(DLA-` + PlaneRegex + "-" + DepRegex + "-" + ArrRegex + DofRegex + `\)`
|
||||||
|
|
||||||
KeyDeparture = "dep"
|
KeyDeparture = "dep"
|
||||||
KeyArrival = "arr"
|
KeyArrival = "arr"
|
||||||
KeyDof = "dof"
|
KeyDof = "dof"
|
||||||
|
KeyPlane = "plane"
|
||||||
|
KeyAircraft = "aircraft"
|
||||||
|
KeyOther = "other"
|
||||||
|
KeyTime = "time"
|
||||||
)
|
)
|
||||||
|
|
||||||
var flopRegex = regexp.MustCompile(FlopRegex)
|
var flopRegex = regexp.MustCompile(FlopRegex)
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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
|
||||||
|
*/
|
||||||
|
var CompositeFlightPlan = `(?m)\(FPL-` + PlaneRegex + `-IS( |\n)+` +
|
||||||
|
AircraftRegex + getOtherRegexWithName("p1") + "-" +
|
||||||
|
DepRegex + `[ \n]+-` +
|
||||||
|
getOtherRegexWithName("p2") + "-" +
|
||||||
|
ArrRegex + ` \w+[ \n]+-` +
|
||||||
|
getOtherRegexWithName("p3") + `\)`
|
||||||
|
|
||||||
func parseArrOrDep(telegram *Telegram, data string) {
|
func parseArrOrDep(telegram *Telegram, data string) {
|
||||||
|
fmt.Println(CompositeDepArr)
|
||||||
fields := getFields(CompositeDepArr, data)
|
fields := getFields(CompositeDepArr, data)
|
||||||
if len(fields) < 1 {
|
if len(fields) < 1 {
|
||||||
telegram.ParseErrors = append(telegram.ParseErrors, errors.New("data less than 4 "))
|
telegram.ParseErrors = append(telegram.ParseErrors, errors.New("data less than 4 "))
|
||||||
@@ -59,26 +85,13 @@ func parseArrOrDep(telegram *Telegram, data string) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func parseFlightPlan(telegram *Telegram, data string) {
|
func parseFlightPlan(telegram *Telegram, data string) {
|
||||||
/**
|
fmt.Println(CompositeFlightPlan)
|
||||||
* example:
|
fields := getFields(CompositeFlightPlan, data)
|
||||||
* (FPL-AAR997-IS
|
telegram.FlightNumber, _ = parseFlight(fields[KeyPlane])
|
||||||
* -B763/H-SDHIPRWZ/S
|
telegram.Aircraft = fields[KeyAircraft]
|
||||||
* -RKSI0120
|
telegram.Departure, telegram.DepartureTime = parseAirportAndTime(fields[KeyDeparture])
|
||||||
* -N0473F320 NOPIK1B NOPIK G597 GONAV/N0473F320 G597 AGAVO/K0876S0980
|
telegram.Destination, telegram.DestinationTime = parseAirportAndTime(fields[KeyArrival])
|
||||||
* G597 DONVO A326 ANRAT ANRAT2A
|
telegram.DateOfFlight = parseDof(fields["p3"])
|
||||||
* -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 = parseAirportAndTime(values[5])
|
|
||||||
destString := strings.Split(values[7], " ")
|
|
||||||
telegram.Destination, telegram.DestinationTime = parseAirportAndTime(destString[0])
|
|
||||||
telegram.DateOfFlight = parseDof(values[8])
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func parseDelay(telegram *Telegram, data string) {
|
func parseDelay(telegram *Telegram, data string) {
|
||||||
|
|||||||
+34
-3
@@ -7,7 +7,7 @@ import (
|
|||||||
|
|
||||||
func TestAirportAndTimeRegex(t *testing.T) {
|
func TestAirportAndTimeRegex(t *testing.T) {
|
||||||
ast := assert.New(t)
|
ast := assert.New(t)
|
||||||
text := "ZBTJ0554"
|
text := "-ZBTJ0554"
|
||||||
|
|
||||||
fields := getFields(DepRegex, text)
|
fields := getFields(DepRegex, text)
|
||||||
ast.Equal(1, len(fields), "Should get 1 filed")
|
ast.Equal(1, len(fields), "Should get 1 filed")
|
||||||
@@ -23,13 +23,21 @@ func TestAirportAndTimeRegex(t *testing.T) {
|
|||||||
|
|
||||||
func TestPlaneRegex(t *testing.T) {
|
func TestPlaneRegex(t *testing.T) {
|
||||||
ast := assert.New(t)
|
ast := assert.New(t)
|
||||||
text := "JAE7433/A0132"
|
text := "-JAE7433/A0132"
|
||||||
fields := getFields(PlaneRegex, text)
|
fields := getFields(PlaneRegex, text)
|
||||||
ast.Equal(1, len(fields), "Should get plane value")
|
ast.Equal(1, len(fields), "Should get plane value")
|
||||||
ast.Equal("JAE7433/A0132", fields[KeyPlane])
|
ast.Equal("JAE7433/A0132", fields[KeyPlane])
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestAircraftRegex(t *testing.T) {
|
||||||
|
ast := assert.New(t)
|
||||||
|
text := "-B763/H-SDHIPRWZ/S"
|
||||||
|
fields := getFields(AircraftRegex, text)
|
||||||
|
ast.Equal(1, len(fields))
|
||||||
|
ast.Equal("B763/H", fields[KeyAircraft])
|
||||||
|
}
|
||||||
|
|
||||||
func TestDofRegex(t *testing.T) {
|
func TestDofRegex(t *testing.T) {
|
||||||
ast := assert.New(t)
|
ast := assert.New(t)
|
||||||
text := "DOF/120820"
|
text := "DOF/120820"
|
||||||
@@ -43,6 +51,29 @@ func TestDofRegex(t *testing.T) {
|
|||||||
ast.Equal(20, d.Day())
|
ast.Equal(20, d.Day())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestGetOtherRegex(t *testing.T) {
|
||||||
|
ast := assert.New(t)
|
||||||
|
regex := getOtherRegexWithName("p1")
|
||||||
|
ast.Equal(`-?(?P<p1>[A-Z0-9\/ \n\r]+)`, regex)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestOthers(t *testing.T) {
|
||||||
|
ast := assert.New(t)
|
||||||
|
text := `-N0473F320 NOPIK1B
|
||||||
|
NOPIK G597 GONAV/N0473F320 G597 AGAVO/K0876S0980 G597 DONVO A326
|
||||||
|
ANRAT ANRATA`
|
||||||
|
fields := getFields(OtherRegex, text)
|
||||||
|
|
||||||
|
ast.Equal(1, len(fields))
|
||||||
|
ast.Equal(text[1:], fields["NAME"])
|
||||||
|
|
||||||
|
text = "-N0473F320 NOPIK1B"
|
||||||
|
fields = getFields(OtherRegex, text)
|
||||||
|
|
||||||
|
ast.Equal(1, len(fields))
|
||||||
|
ast.Equal(text[1:], fields["NAME"])
|
||||||
|
}
|
||||||
|
|
||||||
func TestArrDep(t *testing.T) {
|
func TestArrDep(t *testing.T) {
|
||||||
ast := assert.New(t)
|
ast := assert.New(t)
|
||||||
s := `ZCZC TMQ2526 141605
|
s := `ZCZC TMQ2526 141605
|
||||||
@@ -121,7 +152,7 @@ NNNN
|
|||||||
ast.Equal(false, tele.Type == "PLN", "Not a plan")
|
ast.Equal(false, tele.Type == "PLN", "Not a plan")
|
||||||
ast.Equal("FPL", tele.Type, "Flop type is FPL")
|
ast.Equal("FPL", tele.Type, "Flop type is FPL")
|
||||||
ast.Equal("AAR997", tele.FlightNumber, "flight number is AAR997")
|
ast.Equal("AAR997", tele.FlightNumber, "flight number is AAR997")
|
||||||
ast.Equal("B763/H", tele.Plane, "plane is B763/H")
|
ast.Equal("B763/H", tele.Aircraft, "aircraft is B763/H")
|
||||||
ast.Equal("RKSI", tele.Departure, "departure airport is RKSI")
|
ast.Equal("RKSI", tele.Departure, "departure airport is RKSI")
|
||||||
ast.Equal(1, tele.DepartureTime.Hour())
|
ast.Equal(1, tele.DepartureTime.Hour())
|
||||||
ast.Equal(20, tele.DepartureTime.Minute())
|
ast.Equal(20, tele.DepartureTime.Minute())
|
||||||
|
|||||||
@@ -17,6 +17,7 @@ type Telegram struct {
|
|||||||
FlopValue []string
|
FlopValue []string
|
||||||
FlightNumber string
|
FlightNumber string
|
||||||
Plane string
|
Plane string
|
||||||
|
Aircraft string
|
||||||
RegisterNumber string
|
RegisterNumber string
|
||||||
Departure string
|
Departure string
|
||||||
DepartureTime time.Time
|
DepartureTime time.Time
|
||||||
@@ -109,3 +110,7 @@ func parseDof(text string) time.Time {
|
|||||||
dof, _ := time.Parse("060102", value["time"])
|
dof, _ := time.Parse("060102", value["time"])
|
||||||
return dof
|
return dof
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func getOtherRegexWithName(name string) string {
|
||||||
|
return strings.ReplaceAll(OtherRegex, "NAME", name)
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user