/// <summary>
    /// Обрабатывает категорию.
    /// </summary>
    /// <param name="ProtocolStream">ASTERIX пакет.</param>
    /// <param name="message">Таблица маршрутных точек.</param>
    public static void Decode(MemoryStream ProtocolStream, DataTable message, int category)
    {
        while (ProtocolStream.Position != ProtocolStream.Length)
        {
            byte[] TargetAddressbytes          = new byte[4];
            byte[] TimePosition                = new byte[4];
            byte[] Latitudebytes               = new byte[4];
            byte[] Longitudebytes              = new byte[4];
            byte[] AircraftIdentificationbytes = new byte[6];
            byte[] AirportDepaturebytes        = new byte[4];
            byte[] AirportArrivalbytes         = new byte[4];
            byte[] Callsing    = new byte[7];
            byte[] Heightbytes = new byte[2];
            byte[] Mode3Abytes = new byte[2];

            string TargetAddress          = "";
            string AircraftIdentification = "";
            string Latitude        = "";
            string Longitude       = "";
            string EmitterCategory = "";
            string AirportDepature = "";
            string AirportArrival  = "";
            string Height          = "-1";
            string SAC             = "";
            string SIC             = "";
            string Mode3A          = "";
            string CAT             = Convert.ToString(category);

            FSPEC = Protocol.GetVariableField(ProtocolStream);
            if (FSPEC.Length <= FSPECtable.Rows.Count)
            {
                for (int FSPECbit = 0; FSPECbit < FSPEC.Length; FSPECbit++)
                {
                    if (FSPEC.Get((FSPEC.Length - 1) - FSPECbit) == true)
                    {
                        string di = Convert.ToString(FSPECtable.Rows[FSPECbit]["Data Item"]);
                        switch (di)
                        {
                        case "010":
                        {
                            if (Protocol.ChekEndPacket(ProtocolStream, 2))
                            {
                                SAC = Convert.ToString(ProtocolStream.ReadByte());
                                SIC = Convert.ToString(ProtocolStream.ReadByte());
                            }
                            break;
                        }

                        case "060":
                        {
                            if (Protocol.ChekEndPacket(ProtocolStream, 2))
                            {
                                ProtocolStream.Read(Mode3Abytes, 0, 2);
                                Mode3A = Mode3ADecoder070(Mode3Abytes);
                            }
                            break;
                        }

                        case "070":
                        {
                            if (Protocol.ChekEndPacket(ProtocolStream, 3))
                            {
                                ProtocolStream.Read(TimePosition, 1, 3);
                            }
                            break;
                        }

                        case "105":
                        {
                            if (Protocol.ChekEndPacket(ProtocolStream, 4))
                            {
                                ProtocolStream.Read(Latitudebytes, 0, 4);
                                Latitude = Convert.ToString(CoordinateDecoder105(Latitudebytes));
                            }
                            else
                            {
                                break;
                            }
                            if (Protocol.ChekEndPacket(ProtocolStream, 4))
                            {
                                ProtocolStream.Read(Longitudebytes, 0, 4);
                                Longitude = Convert.ToString(CoordinateDecoder105(Longitudebytes));
                            }
                            else
                            {
                                break;
                            }
                            break;
                        }

                        case "110":
                        {
                            BitArray DataAgesFSPEC = Protocol.GetVariableField(ProtocolStream);
                            for (int bit = DataAgesFSPEC.Length - 1; bit >= 0; bit--)
                            {
                                if ((DataAgesFSPEC.Get(bit) == true) && (bit > DataAgesFSPEC.Length - FSPEC110Length.Length))
                                {
                                    if (Protocol.ChekEndPacket(ProtocolStream, FSPEC110Length[DataAgesFSPEC.Length - bit - 1]))
                                    {
                                        ProtocolStream.Seek(FSPEC110Length[DataAgesFSPEC.Length - bit - 1], SeekOrigin.Current);
                                    }
                                    else
                                    {
                                        break;
                                    }
                                }
                            }
                            break;
                        }

                        case "290":
                        {
                            BitArray DataAgesFSPEC = Protocol.GetVariableField(ProtocolStream);
                            for (int bit = DataAgesFSPEC.Length - 1; bit >= 0; bit--)
                            {
                                if ((DataAgesFSPEC.Get(bit) == true) && (bit > DataAgesFSPEC.Length - FSPEC290Length.Length))
                                {
                                    if (Protocol.ChekEndPacket(ProtocolStream, FSPEC290Length[DataAgesFSPEC.Length - bit - 1]))
                                    {
                                        ProtocolStream.Seek(FSPEC290Length[DataAgesFSPEC.Length - bit - 1], SeekOrigin.Current);
                                    }
                                    else
                                    {
                                        break;
                                    }
                                }
                            }
                            break;
                        }

                        case "295":
                        {
                            BitArray DataAgesFSPEC = Protocol.GetVariableField(ProtocolStream);
                            for (int bit = DataAgesFSPEC.Length - 1; bit >= 0; bit--)
                            {
                                if ((DataAgesFSPEC.Get(bit) == true) && (bit > DataAgesFSPEC.Length - FSPEC295Length.Length))
                                {
                                    if (Protocol.ChekEndPacket(ProtocolStream, FSPEC295Length[DataAgesFSPEC.Length - bit - 1]))
                                    {
                                        ProtocolStream.Seek(FSPEC295Length[DataAgesFSPEC.Length - bit - 1], SeekOrigin.Current);
                                    }
                                    else
                                    {
                                        break;
                                    }
                                }
                            }
                            break;
                        }

                        case "340":
                        {
                            BitArray DataAgesFSPEC = Protocol.GetVariableField(ProtocolStream);
                            for (int bit = DataAgesFSPEC.Length - 1; bit >= 0; bit--)
                            {
                                if ((DataAgesFSPEC.Get(bit) == true) && (bit > DataAgesFSPEC.Length - FSPEC340Length.Length))
                                {
                                    if (Protocol.ChekEndPacket(ProtocolStream, FSPEC340Length[DataAgesFSPEC.Length - bit - 1]))
                                    {
                                        ProtocolStream.Seek(FSPEC340Length[DataAgesFSPEC.Length - bit - 1], SeekOrigin.Current);
                                    }
                                    else
                                    {
                                        break;
                                    }
                                }
                            }
                            break;
                        }

                        case "380":
                        {
                            BitArray DataAgesFSPEC = Protocol.GetVariableField(ProtocolStream);
                            for (int bit = DataAgesFSPEC.Length - 1; bit >= 0; bit--)
                            {
                                if (DataAgesFSPEC.Get(bit) == true)
                                {
                                    if (bit == DataAgesFSPEC.Length - 1)
                                    {
                                        if (Protocol.ChekEndPacket(ProtocolStream, 3))
                                        {
                                            ProtocolStream.Read(TargetAddressbytes, 0, 3);
                                        }
                                        else
                                        {
                                            break;
                                        }
                                        TargetAddress = "";
                                        for (int i = 0; i < 3; i++)
                                        {
                                            if (Convert.ToString(TargetAddressbytes[i], 16).Length < 2)
                                            {
                                                TargetAddress += "0";
                                            }
                                            TargetAddress += Convert.ToString(TargetAddressbytes[i], 16).ToUpper();
                                        }
                                    }
                                    else
                                    {
                                        if (bit == DataAgesFSPEC.Length - 2)
                                        {
                                            if (Protocol.ChekEndPacket(ProtocolStream, 6))
                                            {
                                                ProtocolStream.Read(AircraftIdentificationbytes, 0, 6);
                                            }
                                            else
                                            {
                                                break;
                                            }
                                            AircraftIdentification = Protocol.ASCIIDecoder(AircraftIdentificationbytes, ASCIIlist);
                                        }
                                        else
                                        {
                                            if (bit > DataAgesFSPEC.Length - FSPEC380Length.Length)
                                            {
                                                if (Protocol.ChekEndPacket(ProtocolStream, FSPEC380Length[DataAgesFSPEC.Length - bit - 1]))
                                                {
                                                    ProtocolStream.Seek(FSPEC380Length[DataAgesFSPEC.Length - bit - 1], SeekOrigin.Current);
                                                }
                                                else
                                                {
                                                    break;
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                            break;
                        }

                        case "390":
                        {
                            BitArray DataAgesFSPEC = Protocol.GetVariableField(ProtocolStream);
                            for (int bit = DataAgesFSPEC.Length - 1; bit >= 0; bit--)
                            {
                                if (DataAgesFSPEC.Get(bit) == true)
                                {
                                    if ((bit == DataAgesFSPEC.Length - 2) || (bit == DataAgesFSPEC.Length - 6) || (bit == DataAgesFSPEC.Length - 7) || (bit == DataAgesFSPEC.Length - 9))
                                    {
                                        if (bit == DataAgesFSPEC.Length - 2)
                                        {
                                            if (Protocol.ChekEndPacket(ProtocolStream, 7))
                                            {
                                                ProtocolStream.Read(Callsing, 0, 7);
                                            }
                                            else
                                            {
                                                break;
                                            }
                                            AircraftIdentification = Encoding.ASCII.GetString(Callsing).Replace("'", "");
                                        }
                                        if (bit == DataAgesFSPEC.Length - 6)
                                        {
                                            switch (Convert.ToChar(ProtocolStream.ReadByte()))
                                            {
                                            case 'L':
                                            {
                                                EmitterCategory = "LIGHT AIRCRAFT";
                                                break;
                                            }

                                            case 'M':
                                            {
                                                EmitterCategory = "MEDIUM AIRCRAFT";
                                                break;
                                            }

                                            case 'H':
                                            {
                                                EmitterCategory = "HIGH AIRCRAFT";
                                                break;
                                            }
                                            }
                                        }
                                        if (bit == DataAgesFSPEC.Length - 7)
                                        {
                                            if (Protocol.ChekEndPacket(ProtocolStream, 4))
                                            {
                                                ProtocolStream.Read(AirportDepaturebytes, 0, 4);
                                            }
                                            else
                                            {
                                                break;
                                            }
                                            AirportDepature = Encoding.ASCII.GetString(AirportDepaturebytes);
                                        }
                                        if (bit == DataAgesFSPEC.Length - 9)
                                        {
                                            if (Protocol.ChekEndPacket(ProtocolStream, 4))
                                            {
                                                ProtocolStream.Read(AirportArrivalbytes, 0, 4);
                                            }
                                            else
                                            {
                                                break;
                                            }
                                            AirportArrival = Encoding.ASCII.GetString(AirportArrivalbytes);
                                        }
                                    }
                                    else
                                    {
                                        if (bit > DataAgesFSPEC.Length - FSPEC390Length.Length)
                                        {
                                            if (Protocol.ChekEndPacket(ProtocolStream, FSPEC390Length[DataAgesFSPEC.Length - bit - 1]))
                                            {
                                                ProtocolStream.Seek(FSPEC390Length[DataAgesFSPEC.Length - bit - 1], SeekOrigin.Current);
                                            }
                                            else
                                            {
                                                break;
                                            }
                                        }
                                    }
                                }
                            }
                            break;
                        }

                        case "500":
                        {
                            BitArray DataAgesFSPEC = Protocol.GetVariableField(ProtocolStream);
                            for (int bit = DataAgesFSPEC.Length - 1; bit >= 0; bit--)
                            {
                                if ((DataAgesFSPEC.Get(bit) == true) && (bit > DataAgesFSPEC.Length - FSPEC500Length.Length))
                                {
                                    if (Protocol.ChekEndPacket(ProtocolStream, FSPEC500Length[DataAgesFSPEC.Length - bit - 1]))
                                    {
                                        ProtocolStream.Seek(FSPEC500Length[DataAgesFSPEC.Length - bit - 1], SeekOrigin.Current);
                                    }
                                    else
                                    {
                                        break;
                                    }
                                }
                            }
                            break;
                        }

                        case "SP":
                        {
                            if (Protocol.ChekEndPacket(ProtocolStream, ProtocolStream.ReadByte() - 1))
                            {
                                ProtocolStream.Position -= 1;
                                ProtocolStream.Seek(ProtocolStream.ReadByte() - 1, SeekOrigin.Current);
                            }
                            break;
                        }

                        default:
                        {
                            string length = Convert.ToString(FSPECtable.Rows[FSPECbit]["length"]);
                            switch (length)
                            {
                            case "variable":
                            {
                                Protocol.GetVariableField(ProtocolStream);
                                break;
                            }

                            case "":
                            {
                                break;
                            }

                            default:
                            {
                                ProtocolStream.Seek(Convert.ToInt32(length), SeekOrigin.Current);
                                break;
                            }
                            }
                            break;
                        }
                        }
                    }
                }
                if ((TargetAddress != "") && (Latitude != "") && (Longitude != ""))
                {
                    DataRow newMessage = message.NewRow();
                    newMessage["TargetAddress"]          = TargetAddress;
                    newMessage["AircraftIdentification"] = AircraftIdentification;
                    newMessage["EmitterCategory"]        = EmitterCategory;
                    newMessage["AirportDepature"]        = AirportDepature;
                    newMessage["AirportArrival"]         = AirportArrival;
                    newMessage["Latitude"]  = Latitude;
                    newMessage["Longitude"] = Longitude;
                    newMessage["Height"]    = Height;
                    newMessage["DTime"]     = Convert.ToDouble(BitConverter.ToInt32(TimePosition.Reverse().ToArray(), 0) / 128);
                    newMessage["SAC"]       = SAC;
                    newMessage["SIC"]       = SIC;
                    newMessage["Mode3A"]    = Mode3A;
                    newMessage["CAT"]       = CAT;
                    message.Rows.Add(newMessage);
                }
            }
        }
    }
Example #2
0
    /// <summary>
    /// Обрабатывает категорию.
    /// </summary>
    /// <param name="ProtocolStream">ASTERIX пакет.</param>
    /// <param name="message">Таблица маршрутных точек.</param>
    public static void Decode(MemoryStream ProtocolStream, DataTable message, int category)
    {
        while (ProtocolStream.Position != ProtocolStream.Length)
        {
            byte[] TargetAddressbytes          = new byte[4];
            byte[] TimePosition                = new byte[4];
            byte[] Latitudebytes               = new byte[4];
            byte[] Longitudebytes              = new byte[4];
            byte[] AircraftIdentificationbytes = new byte[6];
            byte[] AirportDepaturebytes        = new byte[4];
            byte[] AirportArrivalbytes         = new byte[4];
            byte[] Callsing    = new byte[7];
            byte[] Heightbytes = new byte[2];
            byte[] Mode3Abytes = new byte[2];

            string TargetAddress          = "";
            string AircraftIdentification = "";
            string Latitude        = "";
            string Longitude       = "";
            string EmitterCategory = "";
            string AirportDepature = "";
            string AirportArrival  = "";
            string Height          = "-1";
            string SAC             = "";
            string SIC             = "";
            string Mode3A          = "";
            string CAT             = Convert.ToString(category);

            FSPEC = Protocol.GetVariableField(ProtocolStream);
            if (FSPEC.Length <= FSPECtable.Rows.Count)
            {
                for (int FSPECbit = 0; FSPECbit < FSPEC.Length; FSPECbit++)
                {
                    if (FSPEC.Get((FSPEC.Length - 1) - FSPECbit) == true)
                    {
                        string di = Convert.ToString(FSPECtable.Rows[FSPECbit]["Data Item"]);

                        switch (di)
                        {
                        case "010":
                        {
                            if (Protocol.ChekEndPacket(ProtocolStream, 2))
                            {
                                SAC = Convert.ToString(ProtocolStream.ReadByte());
                                SIC = Convert.ToString(ProtocolStream.ReadByte());
                            }
                            break;
                        }

                        case "020":
                        {
                            if (Protocol.ChekEndPacket(ProtocolStream, 1))
                            {
                                EmitterCategory = EmitterCategorylist[ProtocolStream.ReadByte()];
                            }
                            break;
                        }

                        case "070":
                        {
                            if (Protocol.ChekEndPacket(ProtocolStream, 2))
                            {
                                ProtocolStream.Read(Mode3Abytes, 0, 2);
                                Mode3A = Mode3ADecoder070(Mode3Abytes);
                            }
                            break;
                        }

                        case "073":
                        {
                            if (Protocol.ChekEndPacket(ProtocolStream, 3))
                            {
                                ProtocolStream.Read(TimePosition, 1, 3);
                            }
                            break;
                        }

                        case "080":
                        {
                            if (Protocol.ChekEndPacket(ProtocolStream, 3))
                            {
                                ProtocolStream.Read(TargetAddressbytes, 0, 3);

                                TargetAddress = "";
                                for (int i = 0; i < 3; i++)
                                {
                                    if (Convert.ToString(TargetAddressbytes[i], 16).Length < 2)
                                    {
                                        TargetAddress += "0";
                                    }
                                    TargetAddress += Convert.ToString(TargetAddressbytes[i], 16).ToUpper();
                                }
                            }
                            break;
                        }

                        case "130":
                        {
                            if (Protocol.ChekEndPacket(ProtocolStream, 3))
                            {
                                ProtocolStream.Read(Latitudebytes, 1, 3);
                                Latitude = Convert.ToString(CoordinateDecoder130(Latitudebytes));
                            }
                            if (Protocol.ChekEndPacket(ProtocolStream, 3))
                            {
                                ProtocolStream.Read(Longitudebytes, 1, 3);
                                Longitude = Convert.ToString(CoordinateDecoder130(Longitudebytes));
                            }
                            break;
                        }

                        case "131":
                        {
                            if (Protocol.ChekEndPacket(ProtocolStream, 4))
                            {
                                ProtocolStream.Read(Latitudebytes, 0, 4);
                                Latitude = Convert.ToString(CoordinateDecoder131(Latitudebytes));
                            }
                            if (Protocol.ChekEndPacket(ProtocolStream, 4))
                            {
                                ProtocolStream.Read(Longitudebytes, 0, 4);
                                Longitude = Convert.ToString(CoordinateDecoder131(Longitudebytes));
                            }
                            break;
                        }

                        case "140":
                        {
                            if (Protocol.ChekEndPacket(ProtocolStream, 2))
                            {
                                ProtocolStream.Read(Heightbytes, 0, 2);
                                Height = Convert.ToString(HeightDecoder140(Heightbytes));
                            }
                            break;
                        }

                        case "170":
                        {
                            if (Protocol.ChekEndPacket(ProtocolStream, 6))
                            {
                                ProtocolStream.Read(AircraftIdentificationbytes, 0, 6);
                                AircraftIdentification = Protocol.ASCIIDecoder(AircraftIdentificationbytes, ASCIIlist);
                            }
                            break;
                        }

                        case "295":
                        {
                            BitArray DataAgesFSPEC = Protocol.GetVariableField(ProtocolStream);
                            int      countoctet    = 0;
                            for (int bit = 0; bit < DataAgesFSPEC.Length; bit++)
                            {
                                if (DataAgesFSPEC.Get(bit) == true)
                                {
                                    countoctet++;
                                }
                            }
                            countoctet -= (DataAgesFSPEC.Length / 8 - 1);
                            if (Protocol.ChekEndPacket(ProtocolStream, countoctet))
                            {
                                ProtocolStream.Seek(countoctet, SeekOrigin.Current);
                            }
                            break;
                        }

                        case "RE":
                        {
                            if (Protocol.ChekEndPacket(ProtocolStream, ProtocolStream.ReadByte() - 1))
                            {
                                ProtocolStream.Position -= 1;
                                ProtocolStream.Seek(ProtocolStream.ReadByte() - 1, SeekOrigin.Current);
                            }
                            break;
                        }

                        case "SP":
                        {
                            if (Protocol.ChekEndPacket(ProtocolStream, ProtocolStream.ReadByte() - 1))
                            {
                                ProtocolStream.Position -= 1;
                                ProtocolStream.Seek(ProtocolStream.ReadByte() - 1, SeekOrigin.Current);
                            }
                            break;
                        }

                        default:
                        {
                            string length = Convert.ToString(FSPECtable.Rows[FSPECbit]["length"]);
                            switch (length)
                            {
                            case "variable":
                            {
                                Protocol.GetVariableField(ProtocolStream);
                                break;
                            }

                            case "":
                            {
                                break;
                            }

                            default:
                            {
                                ProtocolStream.Seek(Convert.ToInt32(length), SeekOrigin.Current);
                                break;
                            }
                            }
                            break;
                        }
                        }
                    }
                }
                if ((TargetAddress != "") && (Latitude != "") && (Longitude != ""))
                {
                    DataRow newMessage = message.NewRow();
                    newMessage["TargetAddress"]          = TargetAddress;
                    newMessage["AircraftIdentification"] = AircraftIdentification;
                    newMessage["EmitterCategory"]        = EmitterCategory;
                    newMessage["AirportDepature"]        = AirportDepature;
                    newMessage["AirportArrival"]         = AirportArrival;
                    newMessage["Latitude"]  = Latitude;
                    newMessage["Longitude"] = Longitude;
                    newMessage["Height"]    = Height;
                    newMessage["DTime"]     = Convert.ToDouble(BitConverter.ToInt32(TimePosition.Reverse().ToArray(), 0) / 128);
                    newMessage["SAC"]       = SAC;
                    newMessage["SIC"]       = SIC;
                    newMessage["Mode3A"]    = Mode3A;
                    newMessage["CAT"]       = CAT;

                    message.Rows.Add(newMessage);
                }
            }
        }
    }