refactor: Update ARR body parsing logic

This commit is contained in:
windyboy
2024-07-23 19:12:08 +08:00
parent 6d1364df7e
commit e5492c20ca
4 changed files with 39 additions and 21 deletions
+1 -7
View File
@@ -126,13 +126,7 @@ const (
//
// The regular expression uses named capture groups for each segment, allowing for easy extraction of specific information from a matched flight plan string.
// fplPatternString = `\((?P<category>[A-Z]{3})\-(?P<number>[A-Z]+\d+)\-(?P<indicator>[A-Z]{2})(.*\n)?(.*\n)?\-(?P<aircraft>[A-Z]+\d+\/?[A-Z]?)\s*\-(?P<surve>.*)(.*\n)?\-(?P<departure>[A-Z]{4})(?P<departure_time>\d{4})(.*\n)?\-(?P<speed>[A-Z]+\d+)(?P<level>[A-Z0-9]+)\s(?P<route>.*)(.*\n)?\-(?P<destination>[A-Z]{4})(?P<estt>\d{4})\s(?P<alter>[A-Z]{4})(.*\n)?\-(?P<pbn>PBN\/[A-Z0-9]+)\s(?P<nav>NAV\/\w+)\sREG\/(?P<reg>[A-Z0-9]+)\sEET\/(?P<eet>\w{4}\d{4})\sSEL\/(?P<sel>\w+)\sPER\/(?P<performance>\w)\sRIF\/(?P<rif>\w+\s[A-Z0-9]+\s[A-Z]+)\s*RMK\/(?P<remark>.*)\)$`
fplPatternString = `\((?P<category>[A-Z]{3})-(?P<number>[A-Z]+\d+)-(?P<indicator>[A-Z]{2})\n` +
`-(?P<aircraft>[A-Z]+\d+\/?[A-Z]?)\n` +
`-(?P<surve>.*)\n` +
`-(?P<departure>[A-Z]{4})(?P<departure_time>\d{4})\n` +
`-(?P<speed>[A-Z]+\d+)(?P<level>[A-Z0-9]+)\s+(?P<route>.*)\n` +
`-(?P<destination>[A-Z]{4})(?P<estt>\d{4})\s+(?P<alter>[A-Z]{4})\n` +
`-(?P<pbn>PBN\/[A-Z0-9]+)\s+(?P<nav>NAV\/\w+)\s+REG\/(?P<reg>[A-Z0-9]+)\s+EET\/(?P<eet>\w{4}\d{4})\s+SEL\/(?P<sel>\w+)\s+PER\/(?P<performance>\w)\s+RIF\/(?P<rif>\w+\s+[A-Z0-9]+\s+[A-Z]+)\s*RMK\/(?P<remark>.*)\)`
fplPatternString = `\((?P<category>[A-Z]{3})-(?P<number>[A-Z]+\d+)-(?P<indicator>[A-Z]{2})\n-(?P<aircraft>[A-Z]+\d+\/?[A-Z]?)\n?-(?P<surve>.*)\n?-(?P<departure>[A-Z]{4})(?P<departure_time>\d{4})\n?-(?P<speed>[A-Z]+\d+)(?P<level>[A-Z0-9]+)\s+(?P<route>(.|\n)+)\n-(?P<destination>[A-Z]{4})(?P<estt>\d{4})\s+(?P<alter>[A-Z]{4})\n?-([A-Z]{3}\/(?:[A-Z]{4}\d{4}\s?)+)?(?P<other>(?m)[A-Z]{3}\/(.|\n)*)\)$`
)
// Initialize the bodyPatterns map
+1
View File
@@ -73,6 +73,7 @@ type FPL struct {
NavigationEquipment string `json:"navigation_equipment"` // 导航设备: Navigation equipment (e.g., 'NAV/ABAS').
EstimatedElapsedTime string `json:"estimated_elapsed_time"` // 估计飞行时间: Estimated elapsed time (e.g., 'EET/ZMUB0100').
SELCALCode string `json:"selcal_code"` // SELCAL代码: SELCAL code (e.g., 'JLAD').
Register string `json:"register,omitempty"` // 注册号(可选): Aircraft registration number (e.g., 'B2422').
PerformanceCategory string `json:"performance_category"` // 性能类别: Aircraft performance category (e.g., 'C').
RerouteInformation string `json:"reroute_information,omitempty"` // 重航信息(可选): Reroute information (e.g., 'RIF/FRT N640 ZBYN').
Remarks string `json:"remarks,omitempty"` // 备注(可选): Remarks (e.g., 'RMK/TCAS EQUIPPED').
+34 -11
View File
@@ -20,6 +20,11 @@ var (
emptyLineRemove = regexp.MustCompile(`(?m)^\s*$`)
bodyOnly = regexp.MustCompile(`(.|\n)?(ZCZC(.|\n)*)NNNN(.|\n)?$`)
originator = regexp.MustCompile(`(?P<originatorDateTime>[0-9]+)\s(?P<originator>[A-Z]+)`)
navPattern = regexp.MustCompile(`(?m)^NAV\/(?P<nav>.*)$`)
remarkPattern = regexp.MustCompile(`(?s)^RMK\/(?P<remark>.*)$`)
selPattern = regexp.MustCompile(`(?m)SEL\/(?P<sel>\w+)`)
pbnPattern = regexp.MustCompile(`(?m)PBN\/(?P<pbn>[A-Z0-9]+)`)
otherPatterns = []regexp.Regexp{*navPattern, *remarkPattern, *selPattern, *pbnPattern}
)
type BodyParser struct {
@@ -122,6 +127,7 @@ func createBodyData(data map[string]string) (string, interface{}, error) {
Destination: data["arrival"],
}, nil
case "FPL":
otherData := parseOther(data["other"])
return category, &domain.FPL{
Category: data["category"],
FlightNumber: data["number"],
@@ -135,17 +141,16 @@ func createBodyData(data map[string]string) (string, interface{}, error) {
Route: data["route"],
DestinationAndTotalTime: data["destination"] + data["estt"],
AlternateAirport: data["alter"],
OtherInfo: fmt.Sprintf("%s %s REG/%s EET/%s SEL/%s PER/%s RIF/%s",
data["pbn"], data["nav"], data["reg"], data["eet"], data["sel"], data["performance"], data["rif"]),
SupplementaryInfo: "RMK/" + data["remark"],
EstimatedArrivalTime: data["estimated_arrival_time"],
PBN: data["pbn"],
NavigationEquipment: data["nav"],
EstimatedElapsedTime: data["eet"],
SELCALCode: data["sel"],
PerformanceCategory: data["performance"],
RerouteInformation: data["rif"],
Remarks: data["remark"],
OtherInfo: data["other"],
Register: otherData["reg"],
EstimatedArrivalTime: data["estt"],
PBN: otherData["pbn"],
NavigationEquipment: otherData["nav"],
EstimatedElapsedTime: data["eet"],
SELCALCode: otherData["sel"],
// PerformanceCategory: data["performance"],
// RerouteInformation: data["rif"],
Remarks: otherData["remark"],
}, nil
default:
return category, nil, fmt.Errorf("invalid message type: %s", category)
@@ -306,3 +311,21 @@ func getOriginator(line string) (string, string) {
return "", ""
}
func parseOther(text string) map[string]string {
// fmt.Printf("Parsing other: %s\n", text)
data := make(map[string]string)
for _, re := range otherPatterns {
match := re.FindStringSubmatch(text)
if len(match) > 0 { // Corrected condition
// fmt.Println("Matched: ", match)
for i, name := range re.SubexpNames() {
// fmt.Printf("index: %d, name: %s\n", i, name)
if i != 0 && name != "" {
data[name] = match[i]
}
}
}
}
return data // Return the data map instead of nil
}
+3 -3
View File
@@ -209,9 +209,9 @@ NNNN`
Expect(fplMessage.Route).To(Equal("PIAKS G330 PIMOL A539 BTO W82 DOGAR"))
Expect(fplMessage.DestinationAndTotalTime).To(Equal("ZBAA0153"))
Expect(fplMessage.AlternateAirport).To(Equal("ZBYN"))
Expect(fplMessage.OtherInfo).To(Equal("PBN/A1B2B3B4B5D1L1 NAV/ABAS REG/B6513 EET/ZBPE0112 SEL/KMAL PER/C RIF/FRT N640 ZBYN"))
Expect(fplMessage.SupplementaryInfo).To(Equal("RMK/TCAS EQUIPPED"))
Expect(fplMessage.PBN).To(Equal("PBN/A1B2B3B4B5D1L1"))
Expect(fplMessage.OtherInfo).To(Equal("PBN/A1B2B3B4B5D1L1 NAV/ABAS REG/B6513 EET/ZBPE0112 SEL/KMAL PER/C RIF/FRT N640 ZBYN RMK/TCAS EQUIPPED"))
// Expect(fplMessage.SupplementaryInfo).To(Equal("RMK/TCAS EQUIPPED"))
Expect(fplMessage.PBN).To(Equal("A1B2B3B4B5D1L1"))
Expect(fplMessage.EstimatedElapsedTime).To(Equal("ZBPE0112"))
Expect(fplMessage.SELCALCode).To(Equal("KMAL"))
Expect(fplMessage.PerformanceCategory).To(Equal("C"))