public void checkAndRetranslate(BasePacket packet)
 {
     if (carsIDs.ContainsKey(packet.IMEI))
     {
         packets.Enqueue(packet);
     }
 }
        public static BasePacket GetFromGlonass(string stringPacket)
        {
// новый блок с датч. топлива
// $$AF863771020234458|AAUA0655.71719N038.23135E000003|01.3|00.8|01.0|20141029095029|20141029095029|000000000000|04020000|00000000|0000|0000|13DDEC5A|0000|0.0000|0047||00000|4D3C

// старый блок
// $$9F359772036674894|AAUA0753.36640N055.93727E000000|02.5|01.8|01.8|20141029103052|20141029103052|000000000000|14121179|00000000|00D1EFFD|0000|0.0000|0124||5E9E
// $$A5359772039754511|AAUA0855.74210N036.86489E000000|02.7|02.2|01.5|20131129130737|20131129130737|001000000000|14171226|00000000|13D249F9|0000|0.0000|0776||00000|B8B3
            const string pattern_old = @"\$\$(?<Len>\w{2})(?<Imei>\d{15})\|(?<AlarmType>\w{2})(?<Chip>U|R)(?<State>A|V)(?<Satellites>\d{2})"
                                       + @"(?<Latitude>[0-9\.]{8})(?<LatitudeLetter>N|S)(?<Longitude>[0-9\.]{9})(?<LongitudeLetter>E|W)(?<Speed>[0-9]{3})(?<Direction>[0-9]{3})"
                                       + @"\|(?<PDOP>[0-9\.]{4})\|(?<HDOP>[0-9\.]{4})\|(?<VDOP>[0-9\.]{4})\|(?<DateTime>[0-9]{14})\|(?<RTC>[0-9]{14})\|(?<Status>[0-9]{12})"
                                       + @"\|(?<Voltage>[0-9]{8})\|(?<ADC>[0-9]{8})\|(?<LACCI>\w{8})\|(?<Temperature>\w{4})\|(?<Odometer>[0-9\.]{6,})\|(?<SerialID>\d{4})\|(?<RFIDNo>\d*)\|"
                                       + @"(?<Checksum>\w{4})";

            const string pattern_9_19_Impuls = @"\$\$(?<Len>\w{2})(?<Imei>\d{15})\|(?<AlarmType>\w{2})(?<Chip>U|R)(?<State>A|V)(?<Satellites>\d{2})"
                                               + @"(?<Latitude>[0-9\.]{8})(?<LatitudeLetter>N|S)(?<Longitude>[0-9\.]{9})(?<LongitudeLetter>E|W)(?<Speed>[0-9]{3})(?<Direction>[0-9]{3})"
                                               + @"\|(?<PDOP>[0-9\.]{4})\|(?<HDOP>[0-9\.]{4})\|(?<VDOP>[0-9\.]{4})\|(?<DateTime>[0-9]{14})\|(?<RTC>[0-9]{14})\|(?<Status>[0-9]{12})"
                                               + @"\|(?<Voltage>[0-9]{8})\|(?<ADC>[0-9]{8})\|(?<LACCI>\w{8})\|(?<Temperature>\w{4})\|(?<Odometer>[0-9\.]{6,})\|(?<SerialID>\d{4})\|(?<RFIDNo>\d*)"
                                               + @"\|(?<FuelImpuls>\d{5})\|(?<Checksum>\w{4})";

            const string pattern_fuel = @"\$\$(?<Len>\w{2})(?<Imei>\d{15})\|(?<AlarmType>\w{2})(?<Chip>U|R)(?<State>A|V)(?<Satellites>\d{2})"
                                        + @"(?<Latitude>[0-9\.]{8})(?<LatitudeLetter>N|S)(?<Longitude>[0-9\.]{9})(?<LongitudeLetter>E|W)(?<Speed>[0-9]{3})(?<Direction>[0-9]{3})"
                                        + @"\|(?<PDOP>[0-9\.]{4})\|(?<HDOP>[0-9\.]{4})\|(?<VDOP>[0-9\.]{4})\|(?<DateTime>[0-9]{14})\|(?<RTC>[0-9]{14})\|(?<Status>[0-9]{12})"
                                        + @"\|(?<Voltage>[0-9]{8})\|(?<ADC>[0-9]{8})\|(?<f1>\w{4})\|(?<f2>\w{4})\|(?<LACCI>\w{8})\|(?<Temperature>\w{4})\|(?<Odometer>[0-9\.]{6,})\|(?<SerialID>\d{4})\|(?<RFIDNo>\d*)"
                                        + @"\|(?<FuelImpuls>\d{5})\|(?<Checksum>\w{4})";


            var cnt_parts = stringPacket.Split('|').Length;

            if (cnt_parts == 14 || cnt_parts == 15)
            {
                return(GetFromGPRMC(stringPacket));
            }
            else
            {
                int    messageType;
                string pattern;
                switch (cnt_parts)
                {
                case 16:
                    messageType = 1;
                    pattern     = pattern_old;
                    break;

                case 17:
                    messageType = 2;
                    pattern     = pattern_9_19_Impuls;
                    break;

                case 19:
                    messageType = 3;
                    pattern     = pattern_fuel;
                    break;

                default:
                    messageType = 0;
                    pattern     = string.Empty;
                    break;
                }

                if (messageType == 0)
                {
                    throw new Exception("bad packet");
                }


                var regex       = new Regex(pattern);
                var match       = regex.Match(stringPacket);
                var matchGroups = match.Groups;

                var result = new BasePacket();

                long imeiCheck;
                if (!long.TryParse(matchGroups["Imei"].Value, out imeiCheck))
                {
                    throw new BadPacketException();
                }
                result.IMEI = matchGroups["Imei"].Value;

                result.AlarmType = matchGroups["AlarmType"].Value;

                char state;
                char.TryParse(matchGroups["State"].Value, out state);
                result.State = state;

                // coordinats
                float latitude;
                float.TryParse(matchGroups["Latitude"].Value, out latitude);
                result.LatitudeOrig = latitude;
                result.Latitude     = ConvertGlonassToBaseCoordinat(latitude);

                char latitudeLetter;
                char.TryParse(matchGroups["LatitudeLetter"].Value, out latitudeLetter);
                result.LatitudeLetter = latitudeLetter;

                float longitude;
                float.TryParse(matchGroups["Longitude"].Value, out longitude);
                result.LongitudeOrig = longitude;
                result.Longitude     = ConvertGlonassToBaseCoordinat(longitude);

                char longitudeLetter;
                char.TryParse(matchGroups["LongitudeLetter"].Value, out longitudeLetter);
                result.LongitudeLetter = longitudeLetter;

                // speed
                int sog;
                int.TryParse(matchGroups["Speed"].Value, out sog);
                result.Speed = sog;

                // speed
                int direction;
                int.TryParse(matchGroups["Direction"].Value, out direction);
                result.Direction = direction;

                //
                float pdop;
                float.TryParse(matchGroups["PDOP"].Value, out pdop);
                result.PDOP = pdop;

                float hdop;
                float.TryParse(matchGroups["HDOP"].Value, out hdop);
                result.HDOP = hdop;

                float vdop;
                float.TryParse(matchGroups["VDOP"].Value, out vdop);
                result.VDOP = vdop;

                result.Status = matchGroups["Status"].Value;

                // RTC
                var rtcString = matchGroups["RTC"].Value;

                int rtcYear;
                int.TryParse(rtcString.Substring(0, 4), out rtcYear);

                int rtcMonth;
                int.TryParse(rtcString.Substring(4, 2), out rtcMonth);

                int rtcDay;
                int.TryParse(rtcString.Substring(6, 2), out rtcDay);

                int rtcHour;
                int.TryParse(rtcString.Substring(8, 2), out rtcHour);

                int rtcMinute;
                int.TryParse(rtcString.Substring(10, 2), out rtcMinute);

                int rtcSeconds;
                int.TryParse(rtcString.Substring(12, 2), out rtcSeconds);

                try
                {
                    result.ValidNavigDateTime = new DateTime(rtcYear, rtcMonth, rtcDay, rtcHour, rtcMinute, rtcSeconds);
                }
                catch
                {
                    result.ValidNavigDateTime = new DateTime();
                }

                //Datetime
                var datetimeString = matchGroups["DateTime"].Value;

                int datetimeYear;
                int.TryParse(datetimeString.Substring(0, 4), out datetimeYear);

                int datetimeMonth;
                int.TryParse(datetimeString.Substring(4, 2), out datetimeMonth);

                int datetimeDay;
                int.TryParse(datetimeString.Substring(6, 2), out datetimeDay);

                int datetimeHour;
                int.TryParse(datetimeString.Substring(8, 2), out datetimeHour);

                int datetimeMinute;
                int.TryParse(datetimeString.Substring(10, 2), out datetimeMinute);

                int datetimeSeconds;
                int.TryParse(datetimeString.Substring(12, 2), out datetimeSeconds);

                try
                {
                    result.RTC = new DateTime(datetimeYear, datetimeMonth, datetimeDay, datetimeHour, datetimeMinute, datetimeSeconds);
                }
                catch
                {
                    result.RTC = result.ValidNavigDateTime;
                }

                //Voltage and etc.
                result.Voltage = matchGroups["Voltage"].Value;
                result.ADC     = matchGroups["ADC"].Value;
                result.LACCI   = matchGroups["LACCI"].Value;

                result.Temperature = matchGroups["Temperature"].Value;

                float odometer;
                float.TryParse(matchGroups["Odometer"].Value, out odometer);
                result.Odometer = odometer;

                int serialID;
                int.TryParse(matchGroups["SerialID"].Value, out serialID);
                result.SerialID = serialID;

                result.RFIDNo = matchGroups["RFIDNo"].Value;

                if (messageType == 2 || messageType == 3)
                {
                    int fuelImpuls;
                    int.TryParse(matchGroups["FuelImpuls"].Value, out fuelImpuls);
                    result.FuelImpuls = fuelImpuls;

                    //var fuelString = GetIntToString(fuelImpuls, 4);
                    //result.ADC = result.ADC.Substring(0, 4) + fuelString;
                }

                return(result);
            }
        }
        public static BasePacket GetFromGPRMC(string stringPacket)
        {
// without coord $$AE353358018980081|AA000000000000000000000000000000000000000000000000000000000000|00.0|00.0|00.0|100001000000|20000000000000|14121262|00000000|1E305333|0000|0.0000|0014|1325
//$$B7359772035557439|AA$GPRMC,173354.771,A,5543.1196,N,03813.8631,E,0.14,79.87,261113,,,A*57|04.5|03.1|03.2|000000000000|20131126173353|03710000|00000000|13DDEBD8|0000|0.0000|0002|3D7C
            const string pattern =
                @"\$\$(?<Len>\w{2})(?<Imei>\d{15})\|(?<AlarmType>\w{2})((\$GPRMC,(?<Time>[0-9\.]{9,11}),(?<State>A|V),(?<Latitude>[0-9\.]{7,10}),(?<LatitudeLetter>N|S),"
                + @"(?<Longitude>[0-9\.]{8,11}),(?<LongitudeLetter>E|W),(?<Speed>[0-9\.]*),(?<Direction>[0-9\.]*),(?<Date>[0-9]{6}),([0-9\.]{1,}|),([0-9\.]{1,}|)(,(A|D|E|N|)|)\*\w{2,})|(\d{1,}))"
                + @"\|(?<PDOP>[0-9\.]{4})\|(?<HDOP>[0-9\.]{4})\|(?<VDOP>[0-9\.]{4})\|(?<Status>[0-9]{12})\|(?<RTC>[0-9]{14})\|(?<Voltage>[0-9]{8})\|(?<ADC>[0-9]{8})"
                + @"\|(?<LACCI>\w{8})\|(?<Temperature>\w{4})\|(?<Odometer>[0-9\.]{6})\|(?<SerialID>\d{4})\|(\|?)(?<Checksum>\w{4})";

            var regex       = new Regex(pattern);
            var match       = regex.Match(stringPacket);
            var matchGroups = match.Groups;

            var result = new BasePacket();

            long imeiCheck;

            if (!long.TryParse(matchGroups["Imei"].Value, out imeiCheck))
            {
                throw new BadPacketException();
            }
            result.IMEI = matchGroups["Imei"].Value;

            result.AlarmType = matchGroups["AlarmType"].Value;

            char state;

            if (!char.TryParse(matchGroups["State"].Value, out state))
            {
                state = 'Z';
            }
            result.State = state;

            // coordinats
            float  latitude;
            string slat = matchGroups["Latitude"].Value;

            float.TryParse(slat, out latitude);
            result.Latitude     = latitude;
            result.LatitudeOrig = int.Parse(slat.Substring(0, 2)) + (float.Parse(slat.Substring(2)) * 60);

            char latitudeLetter;

            char.TryParse(matchGroups["LatitudeLetter"].Value, out latitudeLetter);
            result.LatitudeLetter = latitudeLetter;

            float  longitude;
            string slon = matchGroups["Longitude"].Value;

            float.TryParse(slon, out longitude);
            result.Longitude     = longitude;
            result.LongitudeOrig = int.Parse(slon.Substring(0, 3)) + (float.Parse(slon.Substring(3)) * 60);

            char longitudeLetter;

            char.TryParse(matchGroups["LongitudeLetter"].Value, out longitudeLetter);
            result.LongitudeLetter = longitudeLetter;

            // speed
            float sog;

            if (!float.TryParse(matchGroups["Speed"].Value, out sog))
            {
                sog = 0.0F;
            }
            result.Speed = sog;

            // speed
            float direction;

            if (!float.TryParse(matchGroups["Direction"].Value, out direction))
            {
                direction = 0.0F;
            }
            result.Direction = direction;

            //
            float pdop;

            float.TryParse(matchGroups["PDOP"].Value, out pdop);
            result.PDOP = pdop;

            float hdop;

            float.TryParse(matchGroups["HDOP"].Value, out hdop);
            result.HDOP = hdop;

            float vdop;

            float.TryParse(matchGroups["VDOP"].Value, out vdop);
            result.VDOP = vdop;

            result.Status = matchGroups["Status"].Value;

            // RTC
            var rtcString = matchGroups["RTC"].Value;

            int rtcYear;

            int.TryParse(rtcString.Substring(0, 4), out rtcYear);

            int rtcMonth;

            int.TryParse(rtcString.Substring(4, 2), out rtcMonth);

            int rtcDay;

            int.TryParse(rtcString.Substring(6, 2), out rtcDay);

            int rtcHour;

            int.TryParse(rtcString.Substring(8, 2), out rtcHour);

            int rtcMinute;

            int.TryParse(rtcString.Substring(10, 2), out rtcMinute);

            int rtcSeconds;

            int.TryParse(rtcString.Substring(12, 2), out rtcSeconds);

            try
            {
                result.ValidNavigDateTime = new DateTime(rtcYear, rtcMonth, rtcDay, rtcHour, rtcMinute, rtcSeconds);
            }
            catch (ArgumentOutOfRangeException)
            {
                result.ValidNavigDateTime = new DateTime();
            }

            //Datetime
            var date = matchGroups["Date"].Value;
            var time = matchGroups["Time"].Value;

            if (String.IsNullOrEmpty(date) || String.IsNullOrEmpty(time))
            {
                result.RTC = result.ValidNavigDateTime;
            }
            else
            {
                int datetimeYear;
                int.TryParse(date.Substring(4, 2), out datetimeYear);
                datetimeYear += 2000;

                int datetimeMonth;
                int.TryParse(date.Substring(2, 2), out datetimeMonth);

                int datetimeDay;
                int.TryParse(date.Substring(0, 2), out datetimeDay);

                int datetimeHour;
                int.TryParse(time.Substring(0, 2), out datetimeHour);

                int datetimeMinute;
                int.TryParse(time.Substring(2, 2), out datetimeMinute);

                int datetimeSeconds;
                int.TryParse(time.Substring(4, 2), out datetimeSeconds);

                try
                {
                    result.RTC = new DateTime(datetimeYear, datetimeMonth, datetimeDay, datetimeHour, datetimeMinute, datetimeSeconds);
                }
                catch
                {
                    result.RTC = result.ValidNavigDateTime;
                }
            }

            //Voltage and etc.
            result.Voltage = matchGroups["Voltage"].Value;
            result.ADC     = matchGroups["ADC"].Value;
            result.LACCI   = matchGroups["LACCI"].Value;

            result.Temperature = matchGroups["Temperature"].Value;

            float odometer;

            float.TryParse(matchGroups["Odometer"].Value, out odometer);
            result.Odometer = odometer;

            int serialID;

            int.TryParse(matchGroups["SerialID"].Value, out serialID);
            result.SerialID = serialID;

            result.RFIDNo = "";

            return(result);
        }