diff --git a/parser/telegram.go b/parser/telegram.go index 2281189..9b96e8b 100644 --- a/parser/telegram.go +++ b/parser/telegram.go @@ -4,26 +4,31 @@ import ( "regexp" "strconv" "strings" + "time" ) type Telegram struct { - Text string - Complete bool - Parsed bool - Fields []string - Sequence int - Type string + Text string + Complete bool + Parsed bool + Fields []string + Sequence int + Type string + FlopValue []string + FlightNumber string + RegisterNumber string + Departure string + DepartureTime time.Time + Destination string + DestinationTime time.Time + DateOfFlight time.Time + ParseErrors []error } - - const ( - Start = "ZCZC" - End = "NNNN" + Start = "ZCZC" + End = "NNNN" TmqRegex = `TMQ(?P\d{4})` - FlopRegex = `^\((?P\w{3})-.*` - - TypePLN = "PLN" ) //Parse @@ -33,11 +38,17 @@ func Parse(text string) (*Telegram, error) { telegram.Fields = strings.Split(strings.Replace(text, "\r\n", "\n", -1), "\n") telegram.Fields = clean(telegram.Fields) seq := getFields(TmqRegex, telegram.Fields[0]) - telegram.Sequence, _ = strconv.Atoi( seq["tmq"]) + telegram.Sequence, _ = strconv.Atoi(seq["tmq"]) if isPln(*telegram) { telegram.Type = TypePLN - } else if t := getFields(FlopRegex, telegram.Fields[3]); t != nil { - telegram.Type = t["flop"] + } else if flopValues := getFields(FlopRegex, telegram.Text); len(flopValues) > 0 { + telegram.Type = flopValues["flop"] + r := regexp.MustCompile(FlopRegex) + data := r.FindString(telegram.Text) + switch telegram.Type { + case TypeArrival: + parseArr(telegram, data) + } } return telegram, nil } @@ -55,8 +66,7 @@ func clean(fields []string) []string { return values[:len(values)-1] } - -func getFields(regEx, field string) (fields map[string]string) { +func getFields(regEx string, field string) (fields map[string]string) { var compRegEx = regexp.MustCompile(regEx) match := compRegEx.FindStringSubmatch(field) @@ -70,11 +80,22 @@ func getFields(regEx, field string) (fields map[string]string) { return } -func isPln(telegram Telegram) bool { - for _, value := range telegram.Fields { - if strings.HasPrefix(value, TypePLN) { - return true - } +func getFlopValues(s string) []string { + // get rid of () + s = strings.TrimSpace(s) + s = s[1 : len(s)-1] + return strings.Split(s, "-") +} + +func parseFlight(s string) (flightNumber string, registerNumber string) { + if l := strings.Index(s, "/"); l > 0 { + return s[0:l], s[l+1:] } - return false + return s, "" +} + +func getAirportAndTime(s string) (airport string, arrivalTime time.Time) { + fields := getFields(DestinationRegex, s) + arrivalTime, _ = time.Parse("1504", fields["time"]) + return fields["dest"], arrivalTime } diff --git a/parser/telegram_test.go b/parser/telegram_test.go index a88cb40..9665b6b 100644 --- a/parser/telegram_test.go +++ b/parser/telegram_test.go @@ -5,15 +5,15 @@ import ( "testing" ) -func TestBasicFlop(t *testing.T) { +func TestBasicFlop(t *testing.T) { ast := assert.New(t) - s :=`ZCZC TMQ2526 141605 + s := `ZCZC TMQ2526 141605 FF ZBTJZPZX 141604 ZBACZQZX (ARR-JAE7433/A0132-RKSI-ZBTJ1604) NNNN` tele, err := Parse(s) - ast.Nil(err, "Should be no error") + 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]) @@ -42,11 +42,61 @@ SI:A/C CHG = NNNN` tele, err := Parse(s) - ast.Nil(err, "Should be no error") + ast.Nil(err, "Should be no error") ast.Equal(tele.Complete, true, "Should be complete telegram") ast.Equal(15, len(tele.Fields), "get 15 lines") ast.Equal("ZCZC TMQ2886 142347", tele.Fields[0]) ast.Equal(2886, tele.Sequence, "TMQ must be 2886") ast.Equal(true, tele.Type == "PLN", "This is a plan") -} \ No newline at end of file +} + +func TestGetValues(t *testing.T) { + ast := assert.New(t) + text := "ZBTJ1604" + values := getFields(DestinationRegex, text) + + ast.Equal(2, len(values)) + ast.Equal("ZBTJ", values["dest"]) + ast.Equal("1604", values["time"]) +} + +func TestParseDestAndTime(t *testing.T) { + ast := assert.New(t) + text := `ZBTJ1604` + dest, destTime := getAirportAndTime(text) + ast.Equal("ZBTJ", dest) + ast.Equal(16, destTime.Hour()) + ast.Equal(4, destTime.Minute()) + + text = `ZBTJ` + dest, _ = getAirportAndTime(text) + ast.Equal("ZBTJ", dest) + +} + +func TestGetFlopValues(t *testing.T) { + ast := assert.New(t) + text := `(ARR-JAE7433/A0132-RKSI-ZBTJ1604-ZBTJ)` + values := getFlopValues(text) + ast.Equal(5, len(values)) + ast.Equal("ARR", values[0]) + ast.Equal("JAE7433/A0132", values[1]) + ast.Equal("RKSI", values[2]) + ast.Equal("ZBTJ1604", values[3]) + ast.Equal("ZBTJ", values[4]) + +} + +func TestGetFlight(t *testing.T) { + ast := assert.New(t) + text := `JAE7433/A0132` + fn, rn := parseFlight(text) + ast.Equal("JAE7433", fn) + ast.Equal("A0132", rn) + + text = "JAE1234" + fn, rn = parseFlight(text) + ast.Equal("JAE1234", fn) + ast.Equal("", rn) +}