private async void DoWork(object state)
        {
            using (var scope = _serviceScopeFactory.CreateScope())
            {
                var context = scope.ServiceProvider.GetRequiredService <IIturanRepository>();
                try
                {
                    //var resultados = await context.getDataCarsInfo();
                    //Thread.Sleep(60000);
                    //var deviceInfo = await context.getDataDeviceInfo();
                    //Thread.Sleep(60000);
                    IEnumerable <TripsIturanViewModel> viajes = await context.getTrips();

                    //Thread.Sleep(600000);
                    var ultimoregistro = viajes.OrderBy(t => t.TID).LastOrDefault();
                    await context.SaveLastRecord(ultimoregistro.TID.ToString());

                    var vehiculos = await context.GetDataVehiculos();

                    foreach (var item in vehiculos)
                    {
                        //await context.getDataTrips(item, clientId, token);
                        var datos = viajes.Where(p => p.PN == item.Placa).OrderBy(f => f.T1).GroupBy(g => new { g.T1.Year, g.T1.Month, g.T1.Day });
                        if (datos != null)
                        {
                            foreach (var item1 in datos)
                            {
                                var trips                  = item1.Count();
                                var anio                   = item1.Key.Year;
                                var mes                    = item1.Key.Month;
                                var dia                    = item1.Key.Day;
                                var excesovelocidad        = item1.Sum(v => Convert.ToInt32(v.OSO));
                                var excesoaceleracion      = item1.Sum(v => Convert.ToInt32(v.ACO));
                                var excesoaceleracioncurva = item1.Sum(v => Convert.ToInt32(v.CAO));
                                var frenadobrusco          = item1.Sum(v => Convert.ToInt32(v.DCO));
                                var kilometros             = item1.Sum(v => Convert.ToDecimal(v.O2.Replace(".", ",")) - Convert.ToDecimal(v.O1.Replace(".", ",")));
                                var longitud               = item1.LastOrDefault().X2;
                                var latitud                = item1.LastOrDefault().Y2;
                                //foreach (var item2 in item1)
                                //{
                                //    item2.su
                                //}
                                IturanDatosGps ituranDatosGps = new IturanDatosGps
                                {
                                    placa             = item.Placa,
                                    viajes            = trips,
                                    kilometros        = kilometros,
                                    speeding          = excesovelocidad,
                                    hardbraking       = frenadobrusco,
                                    sharpacceleration = excesoaceleracion,
                                    sharpturn         = excesoaceleracioncurva,
                                    latitud           = latitud,
                                    longitud          = longitud,
                                };
                                await context.getDataTrips(item, "", ituranDatosGps, dia, mes, anio);
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    //await _logRepository.SaveLogsGPS("Error", ex.Message, "CGB", "");
                }
            }
        }
        public async Task getDataTrips(Vehiculo vehiculo, string clientId, IturanDatosGps ituranDatosGps, int dia, int mes, int año)
        {
            var datosgps = await _vehiculoGpsRepository.GetVehiculoByDateAsync(dia, mes, año, vehiculo.Id);

            var vehiculopro = await _vehiculoProvGpsRepository.GetVehiculoByIdAsync(vehiculo.Id);

            var incidencias = await _incidenciasRepository.GetIncidenciaByNitAsync(vehiculopro.user.Cedula);

            var conductores = await _datosRepository.GetConductoresAsync(vehiculopro.user.Cedula, vehiculopro.Placa);

            var talleres = await _datosRepository.GetIngresosTallerAsync(vehiculopro.user.Cedula, vehiculopro.Placa);

            var siniestros = await _datosRepository.GetSiniestrosAsync(vehiculopro.user.Cedula, vehiculopro.Placa);

            var sustitutos = await _datosRepository.GetDiasSustitutosAsync(vehiculopro.user.Cedula, vehiculopro.Placa);

            int diassustitutos = 0;

            var conductor = await _datosRepository.GetConductorPlacasAsync(vehiculo.Placa);

            if (sustitutos.Count() > 0)
            {
                var strgama  = sustitutos.ToList().FirstOrDefault().Gama.ToString();
                var diassust = sustitutos.ToList().FirstOrDefault().Dias.ToString();
                var gama     = await _gamaRepository.GetGamaByTypeAsync(strgama);

                if (gama != null)
                {
                    diassustitutos = gama.Monto * Convert.ToInt32(diassust);
                }
            }

            var distance = ituranDatosGps.kilometros;
            var suma     = 0M;
            var score    = 0M;

            if (incidencias != null)
            {
                suma = incidencias.ExcesoVelocidad * ituranDatosGps.speeding
                       + incidencias.FrenazoBrusco * ituranDatosGps.hardbraking
                       + incidencias.AceleracionesBruscas * ituranDatosGps.sharpacceleration
                       + incidencias.GiroBrusco * ituranDatosGps.sharpturn;
            }
            else
            {
                suma = 0;
            }



            if (distance > 0)
            {
                score = 100 - (suma / (Convert.ToDecimal(distance) / 100));
            }

            if (datosgps == null)
            {
                VehiculoGps vehiculoGPS = new VehiculoGps
                {
                    vehiculo = vehiculo,
                    dia      = dia,
                    mes      = mes,
                    anio     = año,
                    //kilometerstraveled = Convert.ToInt32(response["odometer"].Value<string>().Replace(".","")),
                    kilometerstraveled = distance,
                    trips             = ituranDatosGps.viajes,
                    speeding          = ituranDatosGps.speeding,
                    hardbraking       = ituranDatosGps.hardbraking,
                    sharpacceleration = ituranDatosGps.sharpacceleration,
                    sharpturn         = ituranDatosGps.sharpturn, ///no tiene
                    latitude          = ituranDatosGps.latitud,
                    longitude         = ituranDatosGps.longitud,
                    score             = score,
                    conductores       = conductores.Count() > 0 ? conductores.Sum(c => c.Conductores) : 0,
                    talleres          = talleres.Count() > 0 ? talleres.Sum(t => t.Ingresos) : 0,
                    siniestros        = siniestros.Count() > 0 ? siniestros.Sum(s => s.Total_Siniestros) : 0,
                    ahorro            = sustitutos.Count() > 0 ? diassustitutos : 0,
                    usuario           = conductor.ToUpper(),
                };
                try
                {
                    await _vehiculoGpsRepository.CreateAsync(vehiculoGPS);
                }
                catch (Exception ex)
                {
                    var mensaje = ex.Message;
                }
            }
            else
            {
                datosgps.kilometerstraveled = distance;
                datosgps.trips             = ituranDatosGps.viajes;
                datosgps.speeding          = ituranDatosGps.speeding;
                datosgps.hardbraking       = ituranDatosGps.hardbraking;
                datosgps.sharpacceleration = ituranDatosGps.sharpacceleration;
                datosgps.sharpturn         = ituranDatosGps.sharpturn;
                datosgps.latitude          = ituranDatosGps.latitud;
                datosgps.longitude         = ituranDatosGps.longitud;
                datosgps.score             = score;
                datosgps.conductores       = conductores.Count() > 0 ? conductores.Sum(c => c.Conductores) : 0;
                datosgps.talleres          = talleres.Count() > 0 ? talleres.Sum(t => t.Ingresos) : 0;
                datosgps.siniestros        = siniestros.Count() > 0 ? siniestros.Sum(s => s.Total_Siniestros) : 0;
                datosgps.ahorro            = sustitutos.Count() > 0 ? diassustitutos : 0;
                datosgps.usuario           = conductor.ToUpper();
                await _vehiculoGpsRepository.UpdateAsync(datosgps);
            }
        }