Files
go-caatsm/internal/parsers/aviation_parser.go
T

295 lines
8.7 KiB
Go
Raw Normal View History

package parsers
import (
2024-07-20 22:21:12 +08:00
"caatsm/internal/config"
"caatsm/internal/domain"
2024-07-24 14:21:02 +08:00
"caatsm/pkg/utils"
2024-07-20 22:21:12 +08:00
"fmt"
2024-07-20 22:33:22 +08:00
"regexp"
"strings"
2024-07-24 14:21:02 +08:00
"sync"
2024-07-20 22:25:28 +08:00
"time"
)
2024-07-20 18:55:28 +08:00
const (
StartIndicatorPrefix = "ZCZC"
EndHeaderMarker = "."
BeginPartMarker = "BEGIN PART"
)
2024-07-22 08:12:18 +08:00
var (
2024-07-23 19:36:11 +08:00
categoryRegex = regexp.MustCompile(`\((?P<category>[A-Z]+)-`)
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]+)`)
eetPattern = regexp.MustCompile(`(?s)(-?EET\/(?P<eet>(?:[A-Z]{4}\d{4}\s*)+))`)
performancePattern = regexp.MustCompile(`(?s)-?PER\/(?P<per>\w)`)
reroutePattern = regexp.MustCompile(`(?m)RIF\/(?P<rif>.*)[A-Z]{3}\/`)
2024-07-24 10:33:12 +08:00
otherPatterns = []*regexp.Regexp{navPattern, remarkPattern, selPattern, pbnPattern, eetPattern, performancePattern, reroutePattern}
2024-07-22 08:12:18 +08:00
)
2024-07-24 14:21:02 +08:00
var mu sync.Mutex
2024-07-20 22:21:12 +08:00
type BodyParser struct {
2024-07-24 14:21:02 +08:00
bodyMu sync.Mutex
body string
2024-07-20 22:33:22 +08:00
bodyPatterns map[string]config.BodyConfig
2024-07-20 22:21:12 +08:00
}
func NewBodyParser(body string) *BodyParser {
return &BodyParser{bodyPatterns: config.GetBodyPatterns(), body: body}
2024-07-20 22:21:12 +08:00
}
func (bp *BodyParser) GetBodyPatterns() map[string]config.BodyConfig {
2024-07-20 22:33:22 +08:00
return bp.bodyPatterns
2024-07-20 22:21:12 +08:00
}
func (bp *BodyParser) SetBodyPatterns(patterns map[string]config.BodyConfig) {
2024-07-20 22:33:22 +08:00
bp.bodyPatterns = patterns
2024-07-20 22:21:12 +08:00
}
func (bp *BodyParser) Parse() (string, interface{}, error) {
2024-07-24 14:21:02 +08:00
bp.bodyMu.Lock()
defer bp.bodyMu.Unlock()
bp.body = strings.TrimSpace(bp.body)
category := findCategory(bp.body)
if category == "" {
2024-07-23 16:45:03 +08:00
return "", nil, fmt.Errorf("no category found in body text")
}
2024-07-23 16:45:03 +08:00
2024-07-24 10:33:12 +08:00
if patternConfig, exists := bp.bodyPatterns[category]; exists && patternConfig.Patterns != nil {
for _, p := range patternConfig.Patterns {
if match := p.Expression.FindStringSubmatch(bp.body); match != nil {
2024-07-24 10:33:12 +08:00
data := extractData(match, p.Expression)
2024-07-24 14:21:02 +08:00
return bp.createBodyData(data)
2024-07-20 22:21:12 +08:00
}
}
}
return "", nil, fmt.Errorf("no matching pattern found for body: %s", bp.body)
}
func findCategory(body string) string {
2024-07-24 10:33:12 +08:00
if match := categoryRegex.FindStringSubmatch(body); match != nil {
for i, name := range categoryRegex.SubexpNames() {
2024-07-22 12:42:30 +08:00
if i != 0 && name == "category" {
return match[i]
}
}
}
return ""
2024-07-20 22:21:12 +08:00
}
2024-07-20 22:33:22 +08:00
func extractData(match []string, re *regexp.Regexp) map[string]string {
data := make(map[string]string)
for i, name := range re.SubexpNames() {
if i != 0 && name != "" {
2024-07-24 15:20:00 +08:00
data[name] = strings.TrimSpace(match[i])
2024-07-20 22:33:22 +08:00
}
}
return data
}
2024-07-24 14:21:02 +08:00
func (bp *BodyParser) createBodyData(data map[string]string) (string, interface{}, error) {
2024-07-24 10:33:12 +08:00
switch category := data["category"]; category {
2024-07-20 22:21:12 +08:00
case "ARR":
2024-07-23 16:45:03 +08:00
return category, &domain.ARR{
2024-07-21 00:25:16 +08:00
Category: data["category"],
2024-07-20 22:21:12 +08:00
AircraftID: data["number"],
SSRModeAndCode: data["ssr"],
DepartureAirport: data["departure"],
ArrivalAirport: data["arrival"],
ArrivalTime: data["time"],
2024-07-20 22:21:12 +08:00
}, nil
case "DEP":
2024-07-23 16:45:03 +08:00
return category, &domain.DEP{
2024-07-21 00:25:16 +08:00
Category: data["category"],
2024-07-20 22:21:12 +08:00
AircraftID: data["number"],
SSRModeAndCode: data["ssr"],
DepartureAirport: data["departure"],
DepartureTime: data["departure_time"],
Destination: data["arrival"],
}, nil
case "FPL":
2024-07-23 19:12:08 +08:00
otherData := parseOther(data["other"])
2024-07-23 16:45:03 +08:00
return category, &domain.FPL{
2024-07-21 00:25:16 +08:00
Category: data["category"],
2024-07-20 22:21:12 +08:00
FlightNumber: data["number"],
ReferenceData: data["reference_data"],
AircraftID: data["aircraft"],
SSRModeAndCode: data["surve"],
FlightRulesAndType: data["indicator"],
CruisingSpeedAndLevel: data["speed"] + data["level"],
DepartureAirport: data["departure"],
DepartureTime: data["departure_time"],
Route: data["route"],
DestinationAndTotalTime: data["destination"] + data["estt"],
AlternateAirport: data["alter"],
2024-07-23 19:12:08 +08:00
OtherInfo: data["other"],
Register: otherData["reg"],
EstimatedArrivalTime: data["estt"],
PBN: otherData["pbn"],
NavigationEquipment: otherData["nav"],
2024-07-23 19:36:11 +08:00
EstimatedElapsedTime: otherData["eet"],
2024-07-23 19:12:08 +08:00
SELCALCode: otherData["sel"],
2024-07-23 19:36:11 +08:00
PerformanceCategory: otherData["per"],
RerouteInformation: otherData["rif"],
Remarks: otherData["remark"],
2024-07-20 22:21:12 +08:00
}, nil
default:
2024-07-24 14:21:02 +08:00
return category, nil, fmt.Errorf("cannot parse: %s", category)
2024-07-20 22:21:12 +08:00
}
}
func Parse(rawText string) (*domain.ParsedMessage, error) {
2024-07-24 14:21:02 +08:00
mu.Lock()
defer mu.Unlock()
2024-07-20 22:21:12 +08:00
message, err := ParseHeader(rawText)
if err != nil {
return nil, err
}
bodyParser := NewBodyParser(message.BodyAndFooter)
category, bodyData, err := bodyParser.Parse()
2024-07-23 16:45:03 +08:00
message.Category = category
2024-07-24 10:33:12 +08:00
message.ParsedAt = time.Now()
2024-07-23 16:45:03 +08:00
2024-07-20 22:21:12 +08:00
if err != nil {
return &message, err
2024-07-20 22:21:12 +08:00
}
2024-07-20 22:21:12 +08:00
message.BodyData = bodyData
return &message, nil
}
func cleanMessage(text string) string {
cleanedText := emptyLineRemove.ReplaceAllString(text, "")
cleanText := strings.ReplaceAll(cleanedText, "\n\n", "\n")
2024-07-24 10:33:12 +08:00
if match := bodyOnly.FindStringSubmatch(cleanText); len(match) > 1 {
bodyContent := match[2]
if bodyContent[len(bodyContent)-1] == '\n' {
return bodyContent[:len(bodyContent)-1]
}
2024-07-24 10:33:12 +08:00
return bodyContent
2024-07-22 08:12:18 +08:00
}
return ""
}
2024-07-20 18:55:28 +08:00
func ParseHeader(fullMessage string) (domain.ParsedMessage, error) {
2024-07-24 14:21:02 +08:00
log := utils.GetSugaredLogger()
fullMessage = cleanMessage(fullMessage)
2024-07-20 22:33:22 +08:00
lines := strings.Split(fullMessage, "\n")
2024-07-24 14:21:02 +08:00
if len(lines) < 3 {
log.Warnf("invalid message format: %s", fullMessage)
return domain.ParsedMessage{}, fmt.Errorf("invalid message format: %s", fullMessage)
}
_, messageID, dateTime, err := parseStartIndicator(lines[0])
2024-07-20 18:55:28 +08:00
if err != nil {
2024-07-20 22:33:22 +08:00
return domain.ParsedMessage{}, err
}
priorityIndicator, primaryAddress := parsePriorityAndPrimary(lines[1])
2024-07-20 18:55:28 +08:00
secondaryAddresses, originator, originatorDateTime, bodyAndFooter := parseRemainingLines(lines[2:])
return domain.ParsedMessage{
MessageID: messageID,
DateTime: dateTime,
PriorityIndicator: priorityIndicator,
PrimaryAddress: primaryAddress,
SecondaryAddresses: secondaryAddresses,
Originator: originator,
OriginatorDateTime: originatorDateTime,
2024-07-20 18:55:28 +08:00
BodyAndFooter: bodyAndFooter,
2024-07-20 22:25:28 +08:00
ReceivedAt: time.Now(),
2024-07-20 18:55:28 +08:00
}, nil
}
func parseStartIndicator(line string) (string, string, string, error) {
2024-07-20 22:33:22 +08:00
parts := strings.Fields(line)
if len(parts) >= 3 && strings.HasPrefix(parts[0], StartIndicatorPrefix) {
return parts[0], parts[1], parts[2], nil
}
2024-07-24 14:21:02 +08:00
utils.GetSugaredLogger().Warnf("invalid start indicator line format: %s", line)
2024-07-20 22:33:22 +08:00
return "", "", "", fmt.Errorf("invalid start indicator line format: %s", line)
2024-07-20 18:55:28 +08:00
}
func parsePriorityAndPrimary(line string) (string, string) {
2024-07-20 18:55:28 +08:00
parts := strings.Fields(line)
if len(parts) >= 2 {
return parts[0], parts[1]
2024-07-20 18:55:28 +08:00
}
2024-07-24 14:21:02 +08:00
utils.GetSugaredLogger().Warnf("invalid priority and primary address line format: %s", line)
return "", ""
2024-07-20 18:55:28 +08:00
}
func parseRemainingLines(lines []string) ([]string, string, string, string) {
var (
secondaryAddresses []string
originator string
originatorDateTime string
bodyAndFooter strings.Builder
headerEnded bool
)
for _, line := range lines {
line = strings.TrimSpace(line)
if headerEnded {
bodyAndFooter.WriteString(line + "\n")
} else {
switch {
case line == EndHeaderMarker:
case strings.HasPrefix(line, "."):
originatorInfo := strings.Fields(line[1:])
if len(originatorInfo) >= 2 {
originator = originatorInfo[0]
originatorDateTime = originatorInfo[1]
}
headerEnded = true
case strings.HasPrefix(line, BeginPartMarker) || strings.HasPrefix(line, "("):
headerEnded = true
if strings.Index(line, "NNNN") > 0 {
break
}
2024-07-20 18:55:28 +08:00
bodyAndFooter.WriteString(line + "\n")
default:
2024-07-23 16:45:03 +08:00
if o1, o2 := getOriginator(line); o1 != "" {
originatorDateTime = o1
originator = o2
} else {
secondaryAddresses = append(secondaryAddresses, line)
}
2024-07-20 18:55:28 +08:00
}
}
}
return secondaryAddresses, originator, originatorDateTime, bodyAndFooter.String()
}
2024-07-23 16:45:03 +08:00
func getOriginator(line string) (string, string) {
match := originator.FindStringSubmatch(line)
if len(match) >= 3 {
return match[1], match[2]
}
2024-07-24 14:21:02 +08:00
utils.GetSugaredLogger().Warnf("invalid originator line format: %s", line)
2024-07-23 16:45:03 +08:00
return "", ""
}
2024-07-23 19:12:08 +08:00
func parseOther(text string) map[string]string {
data := make(map[string]string)
for _, re := range otherPatterns {
2024-07-24 10:33:12 +08:00
if match := re.FindStringSubmatch(text); len(match) > 0 {
2024-07-23 19:12:08 +08:00
for i, name := range re.SubexpNames() {
if i != 0 && name != "" {
2024-07-23 19:36:11 +08:00
data[name] = strings.TrimSpace(match[i])
2024-07-23 19:12:08 +08:00
}
}
}
}
2024-07-24 10:33:12 +08:00
return data
2024-07-23 19:12:08 +08:00
}