Operation IParser.Parse(string[] lines)
        {
            Operation         operation = new Operation();
            OperationResource last      = new OperationResource();

            lines = Utilities.Trim(lines);

            CurrentSection section      = CurrentSection.AHeader;
            bool           keywordsOnly = true;

            InnerSection innerSection = InnerSection.AStraße;

            for (int i = 0; i < lines.Length; i++)
            {
                try
                {
                    string line = lines[i];
                    if (line.Length == 0)
                    {
                        continue;
                    }

                    // Try to parse the header and extract date and time if possible
                    operation.Timestamp = ParserUtility.ReadFaxTimestamp(line, operation.Timestamp);


                    if (GetSection(line.Trim(), ref section, ref keywordsOnly))
                    {
                        continue;
                    }

                    string msg    = line;
                    string prefix = "";

                    // Make the keyword check - or not (depends on the section we are in; see above)
                    if (keywordsOnly)
                    {
                        string keyword;
                        if (!ParserUtility.StartsWithKeyword(line, Keywords, out keyword))
                        {
                            continue;
                        }

                        int x = line.IndexOf(':');
                        if (x == -1)
                        {
                            // If there is no colon found (may happen occasionally) then simply remove the length of the keyword from the beginning
                            prefix = keyword;
                            msg    = line.Remove(0, prefix.Length).Trim();
                        }
                        else
                        {
                            prefix = line.Substring(0, x);
                            msg    = line.Substring(x + 1).Trim();
                        }

                        prefix = prefix.Trim().ToUpperInvariant();
                    }

                    // Parse each section
                    switch (section)
                    {
                    case CurrentSection.AHeader:
                    {
                        switch (prefix)
                        {
                        case "ALARM":
                            operation.Timestamp = ParserUtility.ReadFaxTimestamp(msg, DateTime.Now);
                            break;

                        case "EINSATZNUMMER":
                            operation.OperationNumber = msg;
                            break;
                        }
                    }
                    break;

                    case CurrentSection.BMitteiler:
                    {
                        // This switch would not be necessary in this section (there is only "Name")...
                        switch (prefix)
                        {
                        case "NAME":
                            operation.Messenger = msg;
                            break;

                        case "RUFNUMMER":
                            if (operation.Messenger != null)
                            {
                                operation.Messenger += " " + msg;
                            }
                            break;
                        }
                    }
                    break;

                    case CurrentSection.CEinsatzort:
                    {
                        switch (prefix)
                        {
                        case "STRAßE":
                        {
                            innerSection = InnerSection.AStraße;
                            string street, streetNumber, appendix;
                            ParserUtility.AnalyzeStreetLine(msg, out street, out streetNumber, out appendix);
                            operation.CustomData["Einsatzort Zusatz"] = appendix;
                            operation.Einsatzort.Street       = street;
                            operation.Einsatzort.StreetNumber = streetNumber;
                        }
                        break;

                        case "ABSCHNITT":
                            operation.Einsatzort.Intersection = msg;
                            break;

                        case "ORT":
                        {
                            innerSection = InnerSection.BOrt;
                            operation.Einsatzort.ZipCode = ParserUtility.ReadZipCodeFromCity(msg);
                            if (string.IsNullOrWhiteSpace(operation.Einsatzort.ZipCode))
                            {
                                Logger.Instance.LogFormat(LogType.Warning, this, "Could not find a zip code for city '{0}'. Route planning may fail or yield wrong results!", operation.Einsatzort.City);
                            }

                            operation.Einsatzort.City = msg.Remove(0, operation.Einsatzort.ZipCode.Length).Trim();

                            // The City-text often contains a dash after which the administrative city appears multiple times (like "City A - City A City A").
                            // However we can (at least with google maps) omit this information without problems!
                            int dashIndex = operation.Einsatzort.City.IndexOf(" - ");
                            if (dashIndex != -1)
                            {
                                // Ignore everything after the dash
                                operation.Einsatzort.City = operation.Einsatzort.City.Substring(0, dashIndex).Trim();
                            }
                        }
                        break;

                        case "OBJEKT":
                            innerSection = InnerSection.CObjekt;
                            operation.Einsatzort.Property = msg;
                            break;

                        case "EINSATZPLANNUMMER":
                            operation.OperationPlan = msg;
                            break;

                        case "STATION":
                            innerSection = InnerSection.DStation;
                            operation.CustomData["Einsatzort Station"] = msg;
                            break;

                        default:
                            switch (innerSection)
                            {
                            case InnerSection.AStraße:
                                //Quite dirty because of Streetnumber. Looking for better solution
                                operation.Einsatzort.Street += msg;
                                break;

                            case InnerSection.BOrt:
                                operation.Einsatzort.City += msg;
                                break;

                            case InnerSection.CObjekt:
                                operation.Einsatzort.Property += msg;
                                break;

                            case InnerSection.DStation:
                                operation.CustomData["Einsatzort Station"] += msg;
                                break;
                            }
                            break;
                        }
                    }
                    break;

                    case CurrentSection.DZielort:
                    {
                        switch (prefix)
                        {
                        case "STRAßE":
                        {
                            innerSection = InnerSection.AStraße;

                            string street, streetNumber, appendix;
                            ParserUtility.AnalyzeStreetLine(msg, out street, out streetNumber, out appendix);
                            operation.CustomData["Zielort Zusatz"] = appendix;
                            operation.Zielort.Street       = street;
                            operation.Zielort.StreetNumber = streetNumber;
                        }
                        break;

                        case "ORT":
                        {
                            innerSection = InnerSection.BOrt;
                            operation.Zielort.ZipCode = ParserUtility.ReadZipCodeFromCity(msg);
                            if (string.IsNullOrWhiteSpace(operation.Zielort.ZipCode))
                            {
                                Logger.Instance.LogFormat(LogType.Warning, this, "Could not find a zip code for city '{0}'. Route planning may fail or yield wrong results!", operation.Zielort.City);
                            }

                            operation.Zielort.City = msg.Remove(0, operation.Zielort.ZipCode.Length).Trim();
                        }
                        break;

                        case "OBJEKT":
                            innerSection = InnerSection.CObjekt;
                            operation.Zielort.Property = msg;
                            break;

                        case "STATION":
                            innerSection = InnerSection.DStation;
                            operation.CustomData["Zielort Station"] = msg;
                            break;

                        default:
                            switch (innerSection)
                            {
                            case InnerSection.AStraße:
                                //Quite dirty because of Streetnumber. Looking for better solution
                                operation.Zielort.Street += msg;
                                break;

                            case InnerSection.BOrt:
                                operation.Zielort.City += msg;
                                break;

                            case InnerSection.CObjekt:
                                operation.Zielort.Property += msg;
                                break;

                            case InnerSection.DStation:
                                operation.CustomData["Zielort Station"] += msg;
                                break;
                            }
                            break;
                        }
                    }
                    break;

                    case CurrentSection.EEinsatzgrund:
                    {
                        switch (prefix)
                        {
                        case "SCHLAGW.":
                            operation.Keywords.Keyword = msg;
                            break;

                        case "STICHWORT":
                            operation.Keywords.EmergencyKeyword = msg;
                            break;
                        }
                    }
                    break;

                    case CurrentSection.FEinsatzmittel:
                    {
                        switch (prefix)
                        {
                        case "NAME":
                            last.FullName = msg.Trim();
                            break;

                        case "GEF. GERÄTE":
                            // Only add to requested equipment if there is some text,
                            // otherwise the whole vehicle is the requested equipment
                            if (!string.IsNullOrWhiteSpace(msg))
                            {
                                last.RequestedEquipment.Add(msg);
                            }
                            break;

                        case "ALARMIERT":
                            // Only add to requested equipment if there is some text,
                            // otherwise the whole vehicle is the requested equipment
                            if (!string.IsNullOrWhiteSpace(msg))
                            {
                                last.Timestamp = msg;
                            }
                            operation.Resources.Add(last);
                            last = new OperationResource();
                            break;
                        }
                    }
                    break;

                    case CurrentSection.GBemerkung:
                    {
                        // Append with newline at the end in case that the message spans more than one line
                        operation.Comment = operation.Comment.AppendLine(msg);
                    }
                    break;

                    case CurrentSection.HFooter:
                        // The footer can be ignored completely.
                        break;
                    }
                }
                catch (Exception ex)
                {
                    Logger.Instance.LogFormat(LogType.Warning, this, "Error while parsing line '{0}'. The error message was: {1}", i, ex.Message);
                }
            }
            return(operation);
        }
Esempio n. 2
0
        Operation IParser.Parse(string[] lines)
        {
            var operation         = new Operation();
            var operationResource = new OperationResource();

            lines = Utilities.Trim(lines);

            CurrentSection section      = CurrentSection.AHeader;
            bool           keywordsOnly = true;

            InnerSection innerSection = InnerSection.AStraße;

            for (int i = 0; i < lines.Length; i++)
            {
                try
                {
                    string line = lines[i];
                    if (line.Length == 0)
                    {
                        continue;
                    }

                    // Try to parse the header and extract date and time if possible
                    operation.Timestamp = ReadFaxTimestamp(line, operation.Timestamp);


                    if (GetSection(line.Trim(), ref section, ref keywordsOnly))
                    {
                        continue;
                    }

                    string msg    = line;
                    string prefix = "";

                    // Make the keyword check - or not (depends on the section we are in; see above)
                    if (keywordsOnly)
                    {
                        if (!StartsWithKeyword(line, out var keyword))
                        {
                            continue;
                        }

                        int x = line.IndexOf(':');
                        if (x == -1)
                        {
                            // If there is no colon found (may happen occasionally) then simply remove the length of the keyword from the beginning
                            prefix = keyword;
                            msg    = line.Remove(0, prefix.Length).Trim();
                        }
                        else
                        {
                            prefix = line.Substring(0, x);
                            msg    = line.Substring(x + 1).Trim();
                        }

                        prefix = prefix.Trim().ToUpperInvariant();
                    }

                    // Parse each section
                    switch (section)
                    {
                    case CurrentSection.AHeader:
                    {
                        switch (prefix)
                        {
                        case "EINSATZNUMMER":
                            operation.OperationNumber = ParserUtility.GetTextBetween(msg, null, "ALARMZEIT");
                            operation.Timestamp       = ReadFaxTimestamp(ParserUtility.GetTextBetween(msg, "ALARMZEIT", null), DateTime.Now);
                            break;
                        }
                    }
                    break;

                    case CurrentSection.BMitteiler:
                    {
                        // This switch would not be necessary in this section (there is only "Name")...
                        switch (prefix)
                        {
                        case "NAME":
                            operation.Messenger = msg;
                            break;
                        }
                    }
                    break;

                    case CurrentSection.CEinsatzort:
                    {
                        switch (prefix)
                        {
                        case "STRAßE":
                        {
                            innerSection = InnerSection.AStraße;
                            ParserUtility.AnalyzeStreetLine(msg, out var street, out var streetNumber, out var appendix);
                            operation.CustomData["Einsatzort Zusatz"] = appendix;
                            operation.Einsatzort.Street       = street;
                            operation.Einsatzort.StreetNumber = streetNumber;
                        }
                        break;

                        case "ABSCHNITT":
                            break;

                        case "ORT":
                        {
                            innerSection = InnerSection.BOrt;
                            operation.Einsatzort.ZipCode = ReadZipCodeFromCity(msg);
                            if (string.IsNullOrWhiteSpace(operation.Einsatzort.ZipCode))
                            {
                                Logger.Instance.LogFormat(LogType.Warning, this, "Could not find a zip code for city '{0}'. Route planning may fail or yield wrong results!", operation.Einsatzort.City);
                            }

                            operation.Einsatzort.City = msg.Remove(0, operation.Einsatzort.ZipCode.Length).Trim();

                            // The City-text often contains a dash after which the administrative city appears multiple times (like "City A - City A City A").
                            // However we can (at least with google maps) omit this information without problems!
                            int dashIndex = operation.Einsatzort.City.IndexOf(" - ", StringComparison.Ordinal);
                            if (dashIndex != -1)
                            {
                                // Ignore everything after the dash
                                operation.Einsatzort.City = operation.Einsatzort.City.Substring(0, dashIndex).Trim();
                            }
                        }
                        break;

                        case "OBJEKT":
                            innerSection = InnerSection.CObjekt;
                            operation.Einsatzort.Property = msg;
                            break;

                        case "KREUZUNG":
                            operation.Einsatzort.Intersection = msg;
                            break;

                        case "STATION":
                            operation.CustomData.Add("Einsatzort Station:", msg);
                            break;

                        case "KOORDINATE":
                            Regex r       = new Regex(@"\d+");
                            var   matches = r.Matches(line);
                            if (matches.Count == 2)
                            {
                                int geoRechts = Convert.ToInt32(matches[0].Value);
                                int geoHoch   = Convert.ToInt32(matches[1].Value);
                                var geo       = GeographicCoords.FromGaussKrueger(geoRechts, geoHoch);
                                operation.Einsatzort.GeoLatitude  = geo.Latitude;
                                operation.Einsatzort.GeoLongitude = geo.Longitude;
                            }

                            break;

                        default:
                            switch (innerSection)
                            {
                            case InnerSection.AStraße:
                                //Quite dirty because of Streetnumber. Looking for better solution
                                operation.Einsatzort.Street += msg;
                                break;

                            case InnerSection.BOrt:
                                operation.Einsatzort.City += msg;
                                break;

                            case InnerSection.CObjekt:
                                operation.Einsatzort.Property += msg;
                                break;
                            }

                            break;
                        }
                    }
                    break;

                    case CurrentSection.DEinsatzgrund:
                    {
                        switch (prefix)
                        {
                        case "SCHLAGW.":
                            operation.Keywords.Keyword = msg;
                            break;

                        case "STICHWORT B":
                            operation.Keywords.B = ParserUtility.GetTextBetween(msg, null, "STICHWORT RD:");
                            operation.Keywords.R = ParserUtility.GetTextBetween(msg, "STICHWORT RD:", null);
                            break;

                        case "STICHWORT SO":
                            operation.Keywords.S = ParserUtility.GetTextBetween(msg, null, "STICHWORT TH:");
                            operation.Keywords.T = ParserUtility.GetTextBetween(msg, "STICHWORT TH:", "STICHWORT IN:");
                            operation.CustomData.Add("Stichwort IN:", ParserUtility.GetTextBetween(msg, "STICHWORT IN:", null));
                            break;

                        case "PRIO.":
                            operation.Priority = msg;
                            break;
                        }
                    }
                    break;

                    case CurrentSection.EEinsatzmittel:
                    {
                        switch (prefix)
                        {
                        case "EINSATZMITTELNAME":
                            operationResource.FullName = msg.Trim();
                            break;

                        case "GEF. GERÄTE":
                            // Only add to requested equipment if there is some text,
                            // otherwise the whole vehicle is the requested equipment
                            if (!string.IsNullOrWhiteSpace(msg))
                            {
                                operationResource.RequestedEquipment.Add(msg);
                            }

                            operation.Resources.Add(operationResource);
                            operationResource = new OperationResource();
                            break;
                        }
                    }
                    break;

                    case CurrentSection.FBemerkung:
                    {
                        // Append with newline at the end in case that the message spans more than one line
                        operation.Comment = operation.Comment.AppendLine(msg);
                    }
                    break;

                    case CurrentSection.GFooter:
                        // The footer can be ignored completely.
                        break;
                    }
                }
                catch (Exception ex)
                {
                    Logger.Instance.LogFormat(LogType.Warning, this, "Error while parsing line '{0}'. The error message was: {1}", i, ex.Message);
                }
            }

            return(operation);
        }
Esempio n. 3
0
        Operation IParser.Parse(string[] lines)
        {
            Operation operation = new Operation();

            lines = Utilities.Trim(lines);

            CurrentSection section      = CurrentSection.AHeader;
            bool           keywordsOnly = true;

            InnerSection innerSection = InnerSection.AStraße;

            for (int i = 0; i < lines.Length; i++)
            {
                try
                {
                    string line = lines[i];
                    if (line.Length == 0)
                    {
                        continue;
                    }

                    // Try to parse the header and extract date and time if possible
                    operation.Timestamp = ParserUtility.ReadFaxTimestamp(line, operation.Timestamp);


                    if (GetSection(line.Trim(), ref section, ref keywordsOnly))
                    {
                        continue;
                    }

                    string msg    = line;
                    string prefix = "";

                    // Make the keyword check - or not (depends on the section we are in; see above)
                    if (keywordsOnly)
                    {
                        string keyword;
                        if (!ParserUtility.StartsWithKeyword(line, Keywords, out keyword))
                        {
                            continue;
                        }

                        int x = line.IndexOf(':');
                        if (x == -1)
                        {
                            // If there is no colon found (may happen occasionally) then simply remove the length of the keyword from the beginning
                            prefix = keyword;
                            msg    = line.Remove(0, prefix.Length).Trim();
                        }
                        else
                        {
                            prefix = line.Substring(0, x);
                            msg    = line.Substring(x + 1).Trim();
                        }

                        prefix = prefix.Trim().ToUpperInvariant();
                    }

                    // Parse each section
                    switch (section)
                    {
                    case CurrentSection.AHeader:
                        //TODO: Absender unterbringen
                        break;

                    case CurrentSection.BMitteiler:
                    {
                        // This switch would not be necessary in this section (there is only "Name")...
                        switch (prefix)
                        {
                        case "NAME":
                            operation.Messenger = msg;
                            break;
                        }
                    }
                    break;

                    case CurrentSection.CEinsatzort:
                    {
                        switch (prefix)
                        {
                        case "STRAßE":
                        {
                            innerSection = InnerSection.AStraße;
                            string street, streetNumber, appendix;
                            ParserUtility.AnalyzeStreetLine(msg, out street, out streetNumber, out appendix);
                            operation.CustomData["Einsatzort Zusatz"] = appendix;
                            operation.Einsatzort.Street       = street;
                            operation.Einsatzort.StreetNumber = streetNumber;
                        }
                        break;

                        case "ORT":
                        {
                            innerSection = InnerSection.BOrt;
                            operation.Einsatzort.ZipCode = ParserUtility.ReadZipCodeFromCity(msg);
                            if (string.IsNullOrWhiteSpace(operation.Einsatzort.ZipCode))
                            {
                                Logger.Instance.LogFormat(LogType.Warning, this, "Could not find a zip code for city '{0}'. Route planning may fail or yield wrong results!", operation.Einsatzort.City);
                            }

                            operation.Einsatzort.City = msg.Remove(0, operation.Einsatzort.ZipCode.Length).Trim();

                            // The City-text often contains a dash after which the administrative city appears multiple times (like "City A - City A City A").
                            // However we can (at least with google maps) omit this information without problems!
                            int dashIndex = operation.Einsatzort.City.IndexOf(" - ");
                            if (dashIndex != -1)
                            {
                                // Ignore everything after the dash
                                operation.Einsatzort.City = operation.Einsatzort.City.Substring(0, dashIndex).Trim();
                            }
                        }
                        break;

                        case "OBJEKT":
                            innerSection = InnerSection.CObjekt;
                            operation.Einsatzort.Property = msg;
                            break;

                        case "KREUZUNG":
                            operation.Einsatzort.Intersection = msg;
                            break;

                        case "KOORDINATE":
                            Regex r       = new Regex(@"(\d+\.\d+)");
                            var   matches = r.Matches(line);
                            if (matches.Count == 2)
                            {
                                NumberFormatInfo nfi = new NumberFormatInfo {
                                    NumberDecimalSeparator = "."
                                };
                                double geoRechts = Convert.ToDouble(matches[0].Value, nfi);
                                double geoHoch   = Convert.ToDouble(matches[1].Value, nfi);
                                var    geo       = GeographicCoords.FromGaussKrueger(geoRechts, geoHoch);
                                operation.Einsatzort.GeoLatitude  = geo.Latitude;
                                operation.Einsatzort.GeoLongitude = geo.Longitude;
                            }
                            break;

                        default:
                            switch (innerSection)
                            {
                            case InnerSection.AStraße:
                                //Quite dirty because of Streetnumber. Looking for better solution
                                operation.Einsatzort.Street += msg;
                                break;

                            case InnerSection.BOrt:
                                operation.Einsatzort.City += msg;
                                break;

                            case InnerSection.CObjekt:
                                operation.Einsatzort.Property += msg;
                                break;
                            }
                            break;
                        }
                    }
                    break;

                    case CurrentSection.DZielort:
                        switch (prefix)
                        {
                        case "STRAßE":
                        {
                            innerSection = InnerSection.AStraße;

                            string street, streetNumber, appendix;
                            ParserUtility.AnalyzeStreetLine(msg, out street, out streetNumber, out appendix);
                            operation.CustomData["Zielort Zusatz"] = appendix;
                            operation.Zielort.Street       = street;
                            operation.Zielort.StreetNumber = streetNumber;
                        }
                        break;

                        case "ORT":
                        {
                            innerSection = InnerSection.BOrt;
                            operation.Zielort.ZipCode = ParserUtility.ReadZipCodeFromCity(msg);
                            if (string.IsNullOrWhiteSpace(operation.Zielort.ZipCode))
                            {
                                Logger.Instance.LogFormat(LogType.Warning, this, "Could not find a zip code for city '{0}'. Route planning may fail or yield wrong results!", operation.Zielort.City);
                            }

                            operation.Zielort.City = msg.Remove(0, operation.Zielort.ZipCode.Length).Trim();
                        }
                        break;

                        case "OBJEKT":
                            innerSection = InnerSection.CObjekt;
                            operation.Zielort.Property = msg;
                            break;

                        default:
                            switch (innerSection)
                            {
                            case InnerSection.AStraße:
                                //Quite dirty because of Streetnumber. Looking for better solution
                                operation.Einsatzort.Street += msg;
                                break;

                            case InnerSection.BOrt:
                                operation.Einsatzort.City += msg;
                                break;

                            case InnerSection.CObjekt:
                                operation.Einsatzort.Property += msg;
                                break;
                            }
                            break;
                        }
                        break;

                    case CurrentSection.EEinsatzgrund:
                    {
                        switch (prefix)
                        {
                        case "SCHLAGW.":
                            operation.Keywords.Keyword = msg;
                            break;

                        case "STICHWORT":
                            operation.Keywords.EmergencyKeyword = msg;
                            break;
                        }
                    }
                    break;

                    case CurrentSection.FEinsatzmittel:
                    {
                        operation.Resources.Add(new OperationResource {
                                FullName = msg.Substring(0, msg.LastIndexOf('('))
                            });
                    }
                    break;

                    case CurrentSection.GBemerkung:
                    {
                        // Append with newline at the end in case that the message spans more than one line
                        operation.Comment = operation.Comment.AppendLine(msg);
                    }
                    break;

                    case CurrentSection.HFooter:
                        // The footer can be ignored completely.
                        break;
                    }
                }
                catch (Exception ex)
                {
                    Logger.Instance.LogFormat(LogType.Warning, this, "Error while parsing line '{0}'. The error message was: {1}", i, ex.Message);
                }
            }
            return(operation);
        }
Esempio n. 4
0
        Operation IParser.Parse(string[] lines)
        {
            Operation         operation = new Operation();
            OperationResource last      = new OperationResource();

            lines = Utilities.Trim(lines);

            CurrentSection section      = CurrentSection.AHeader;
            bool           keywordsOnly = true;

            InnerSection innerSection = InnerSection.AStraße;

            for (int i = 0; i < lines.Length; i++)
            {
                try
                {
                    string line = lines[i];
                    if (line.Length == 0)
                    {
                        continue;
                    }

                    // Try to parse the header and extract date and time if possible
                    operation.Timestamp = ParserUtility.ReadFaxTimestamp(line, operation.Timestamp);


                    if (GetSection(line.Trim(), ref section, ref keywordsOnly))
                    {
                        continue;
                    }

                    string msg    = line;
                    string prefix = "";

                    // Make the keyword check - or not (depends on the section we are in; see above)
                    if (keywordsOnly)
                    {
                        string keyword;
                        if (!ParserUtility.StartsWithKeyword(line, Keywords, out keyword))
                        {
                            continue;
                        }

                        int x = line.IndexOf(':');
                        if (x == -1)
                        {
                            // If there is no colon found (may happen occasionally) then simply remove the length of the keyword from the beginning
                            prefix = keyword;
                            msg    = line.Remove(0, prefix.Length).Trim();
                        }
                        else
                        {
                            prefix = line.Substring(0, x);
                            msg    = line.Substring(x + 1).Trim();
                        }

                        prefix = prefix.Trim().ToUpperInvariant();
                    }

                    // Parse each section
                    switch (section)
                    {
                    case CurrentSection.AHeader:
                    {
                        switch (prefix)
                        {
                        case "ABSENDER":
                            operation.CustomData["Absender"] = msg;
                            break;

                        case "TERMIN":
                            operation.CustomData["Termin"] = msg;
                            break;

                        case "EINSATZNUMMER":
                            operation.OperationNumber = msg;
                            break;

                        case "BMA MELDER":
                            operation.CustomData["BMA Melder"] = msg;
                            break;
                        }
                    }
                    break;

                    case CurrentSection.BMitteiler:
                    {
                        // This switch would not be necessary in this section (there is only "Name")...
                        switch (prefix)
                        {
                        case "NAME":
                            operation.Messenger = msg;
                            break;
                        }
                    }
                    break;

                    case CurrentSection.CEinsatzort:
                    {
                        switch (prefix)
                        {
                        case "STRAßE":
                        {
                            innerSection = InnerSection.AStraße;
                            string street, streetNumber, appendix;
                            ParserUtility.AnalyzeStreetLine(msg, out street, out streetNumber, out appendix);
                            operation.CustomData["Einsatzort Zusatz"] = appendix;
                            operation.Einsatzort.Street       = street;
                            operation.Einsatzort.StreetNumber = streetNumber;
                        }
                        break;

                        case "STR.ABSCHN":
                            innerSection = InnerSection.BAbschnitt;
                            operation.CustomData["Einsatzort Straße Abschnitt"] = msg;
                            break;

                        case "ORT":
                        {
                            innerSection = InnerSection.COrt;
                            operation.Einsatzort.ZipCode = ParserUtility.ReadZipCodeFromCity(msg);
                            if (string.IsNullOrWhiteSpace(operation.Einsatzort.ZipCode))
                            {
                                Logger.Instance.LogFormat(LogType.Warning, this, "Could not find a zip code for city '{0}'. Route planning may fail or yield wrong results!", operation.Einsatzort.City);
                            }

                            operation.Einsatzort.City = msg.Remove(0, operation.Einsatzort.ZipCode.Length).Trim();

                            // The City-text often contains a dash after which the administrative city appears multiple times (like "City A - City A City A").
                            // However we can (at least with google maps) omit this information without problems!
                            int dashIndex = operation.Einsatzort.City.IndexOf(" - ");
                            if (dashIndex != -1)
                            {
                                // Ignore everything after the dash
                                operation.Einsatzort.City = operation.Einsatzort.City.Substring(0, dashIndex).Trim();
                            }
                        }
                        break;

                        case "OBJEKT":
                            innerSection = InnerSection.DObjekt;
                            operation.Einsatzort.Property = msg;
                            break;

                        case "STATION":
                            innerSection = InnerSection.EStation;
                            operation.CustomData["Einsatzort Station"] = msg;
                            break;

                        default:
                            switch (innerSection)
                            {
                            case InnerSection.AStraße:
                                //Quite dirty because of Streetnumber. Looking for better solution
                                operation.Einsatzort.Street += msg;
                                break;

                            case InnerSection.BAbschnitt:
                                operation.CustomData["Einsatzort Straße Abschnitt"] += msg;
                                break;

                            case InnerSection.COrt:
                                operation.Einsatzort.City += msg;
                                break;

                            case InnerSection.DObjekt:
                                operation.Einsatzort.Property += msg;
                                break;

                            case InnerSection.EStation:
                                operation.CustomData["Einsatzort Station"] += msg;
                                break;
                            }
                            break;
                        }
                    }
                    break;

                    case CurrentSection.EEinsatzgrund:
                    {
                        switch (prefix)
                        {
                        case "SCHLAGW.":
                            operation.Keywords.Keyword = msg;
                            break;

                        case "STICHWORT B":
                            operation.Keywords.B = msg;
                            break;

                        case "STICHWORT T":
                            operation.Keywords.T = msg;
                            break;

                        case "STICHWORT S":
                            operation.Keywords.S = msg;
                            break;

                        case "STICHWORT R":
                            operation.Keywords.R = msg;
                            break;
                        }
                    }
                    break;

                    case CurrentSection.FEinsatzmittel:
                    {
                        if (line.StartsWith("NAME", StringComparison.CurrentCultureIgnoreCase))
                        {
                            msg           = ParserUtility.GetMessageText(line, "NAME");
                            last.FullName = msg.Trim();
                        }

                        else if (line.StartsWith("GEF. GERÄT", StringComparison.CurrentCultureIgnoreCase))
                        {
                            msg = ParserUtility.GetMessageText(line, "GEF. GERÄT");

                            // Only add to requested equipment if there is some text,
                            // otherwise the whole vehicle is the requested equipment
                            if (!string.IsNullOrWhiteSpace(msg))
                            {
                                last.RequestedEquipment.Add(msg);
                            }
                        }
                        else if (line.StartsWith("ALARMIERT", StringComparison.CurrentCultureIgnoreCase))
                        {
                            msg = ParserUtility.GetMessageText(line, "Alarmiert");

                            // In case that parsing the time failed, we just assume that the resource got requested right away.
                            DateTime dt;
                            // Most of the time the OCR-software reads the colon as a "1", so we check this case right here.
                            if (!DateTime.TryParseExact(msg, "dd.MM.yyyy HH1mm", CultureInfo.InvariantCulture, DateTimeStyles.None, out dt))
                            {
                                // If this is NOT the case and it was parsed correctly, try it here
                                DateTime.TryParseExact(msg, "dd.MM.yyyy HH:mm", CultureInfo.InvariantCulture, DateTimeStyles.None, out dt);
                            }

                            last.Timestamp = dt.ToString("dd.MM.yyyy HH:mm");
                            // This line will end the construction of this resource. Add it to the list and go to the next.
                            operation.Resources.Add(last);

                            last = new OperationResource();
                        }
                    }
                    break;

                    case CurrentSection.GBemerkung:
                    {
                        // Append with newline at the end in case that the message spans more than one line
                        operation.Comment = operation.Comment.AppendLine(msg);
                    }
                    break;

                    case CurrentSection.HFooter:
                        // The footer can be ignored completely.
                        break;
                    }
                }
                catch (Exception ex)
                {
                    Logger.Instance.LogFormat(LogType.Warning, this, "Error while parsing line '{0}'. The error message was: {1}", i, ex.Message);
                }
            }
            return(operation);
        }
Esempio n. 5
0
        Operation IParser.Parse(string[] lines)
        {
            Operation         operation = new Operation();
            OperationResource last      = new OperationResource();

            lines = Utilities.Trim(lines);
            CurrentSection section      = CurrentSection.AHeader;
            InnerSection   inner        = InnerSection.None;
            bool           keywordsOnly = true;

            for (int i = 0; i < lines.Length; i++)
            {
                try
                {
                    string line = lines[i];
                    if (line.Length == 0)
                    {
                        continue;
                    }
                    if (GetSection(line.Trim(), ref section, ref keywordsOnly))
                    {
                        continue;
                    }

                    string msg    = line;
                    string prefix = "";

                    // Make the keyword check - or not (depends on the section we are in; see above)
                    string keyword = "";
                    if (keywordsOnly)
                    {
                        if (!ParserUtility.StartsWithKeyword(line, _keywords, out keyword))
                        {
                            continue;
                        }

                        int x = line.IndexOf(':');
                        if (x == -1 || x > keyword.Length + 1)
                        {
                            // If there is no colon found (may happen occasionally) then simply remove the length of the keyword from the beginning
                            prefix = keyword;
                            msg    = line.Remove(0, prefix.Length).Trim();
                        }
                        else
                        {
                            prefix = line.Substring(0, x);
                            msg    = line.Substring(x + 1).Trim();
                        }
                        prefix = prefix.Trim().ToUpperInvariant();
                    }

                    // Parse each section
                    switch (section)
                    {
                    case CurrentSection.AHeader:
                    {
                        if (inner == InnerSection.ENr)
                        {
                            operation.OperationNumber = msg;
                        }
                        switch (prefix)
                        {
                        case "EINSATZ-NR.":
                            inner = InnerSection.ENr;
                            break;
                        }
                    }
                    break;

                    case CurrentSection.Einsatzmeldung:
                        switch (prefix)
                        {
                        case "STR./HAUSNR.":
                            string street, streetNumber, appendix;

                            ParserUtility.AnalyzeStreetLine(msg, out street, out streetNumber, out appendix);
                            operation.CustomData["Einsatzort Zusatz"] = appendix;
                            operation.Einsatzort.Street       = street;
                            operation.Einsatzort.StreetNumber = streetNumber;
                            break;

                        case "SONST. ORTSANGABE.":
                            operation.CustomData["Einsatzort Zusatz"] = (operation.CustomData["Einsatzort Zusatz"] as string).AppendLine(msg);
                            break;

                        case "OBJEKT":
                            operation.Einsatzort.Property = msg;
                            break;

                        case "OBJEKT-NUMMER":
                            operation.Einsatzort.Property = operation.Einsatzort.Property.AppendLine(msg);
                            break;

                        case "ORTST./GEM.":
                            operation.Einsatzort.City = msg;
                            break;

                        case "STICHWORT":
                            operation.Keywords.Keyword = msg;
                            break;

                        case "MELDENDER":
                            operation.Messenger = msg;
                            break;

                        case "TELEFONNUMMER":
                            operation.Messenger = operation.Messenger.AppendLine(msg);
                            break;

                        case "BEMERKUNGEN":
                            operation.Picture = msg;
                            break;
                        }
                        break;

                    case CurrentSection.Hinweise:
                    {
                        operation.Comment = operation.Comment.AppendLine(msg);
                    }
                    break;
                    }
                }
                catch (Exception ex)
                {
                    Logger.Instance.LogFormat(LogType.Warning, this,
                                              "Error while parsing line '{0}'. The error message was: {1}", i, ex.Message);
                }
            }
            return(operation);
        }