public async Task getDataTrips(Vehiculo vehiculo, string clientId, string token) { DateTime now = DateTime.Now; var firstDayOfMonth = new DateTime(now.Year, now.Month, 1, 0, 0, 0); var currentDay = new DateTime(now.Year, now.Month, now.Day, 23, 59, 59); var desde = firstDayOfMonth.ToString("yyyy-MM-ddTHH:mm:ss"); var hasta = currentDay.ToString("yyyy-MM-ddTHH:mm:ss"); var url = "https://fleet-api.location-world.com/devices"; var urlQuery = url + "/" + vehiculo.gps_id + "/trips?clientId=" + clientId + "&from=" + desde + "&to=" + hasta + "&page=0&pageSize=50"; var stringPayload = await Task.Run(() => JsonConvert.SerializeObject("")); var httpContent = new StringContent(stringPayload, Encoding.UTF8, "application/json"); VehiculoGps vehicleGPS = new VehiculoGps(); using (HttpClient cliente = new HttpClient()) { cliente.DefaultRequestHeaders.Add("domain", "fleet"); cliente.DefaultRequestHeaders.Add("subdomain", "condelpi"); cliente.DefaultRequestHeaders.Add("Authorization", "Bearer " + token); using (HttpResponseMessage response = await cliente.GetAsync(urlQuery)) { var content = response.Content.ReadAsStringAsync(); var resp = (JObject)JsonConvert.DeserializeObject(content.Result); var valores = resp["items"]; var date = ""; var day = ""; var year = ""; var month = ""; var trips = 0; var distance = 0M; var sharpAcceleration = 0; var speeding = 0; var hardBreaking = 0; var score = 0M; var longitude = ""; var latitude = ""; var sharpTurn = 0; if (valores.Count() > 0) { foreach (var elem in valores) { distance += elem["distance"].Value <decimal>(); score += elem["score"].Value <decimal>(); speeding += elem["overSpeeds"].Value <int>(); hardBreaking += elem["suddenBreakings"].Value <int>(); sharpAcceleration += elem["harshAccelerations"].Value <int>(); } JArray items = (JArray)valores; var dateDB = valores.First["endedOn"].Value <string>(); var dateDT = DateTime.ParseExact(dateDB, "MM/dd/yyyy HH:mm:ss", CultureInfo.InvariantCulture); latitude = valores.First["endLatitude"].Value <string>(); longitude = valores.First["endLongitude"].Value <string>(); //vehicle_id = id; date = dateDT.ToString(); day = dateDT.Day.ToString(); year = dateDT.Year.ToString(); month = dateDT.Month.ToString(); trips = items.Count; //score /= trips; //hardBreaking /= trips; //sharpAcceleration /= trips; var datosgps = await _vehiculoGpsRepository.GetVehiculoByDateAsync(DateTime.Now.Day, DateTime.Now.Month, DateTime.Now.Year, 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 suma = 0M; if (incidencias != null) { suma = incidencias.ExcesoVelocidad * Convert.ToInt32(speeding) + incidencias.FrenazoBrusco * Convert.ToInt32(hardBreaking) + incidencias.AceleracionesBruscas * Convert.ToInt32(sharpAcceleration) + incidencias.GiroBrusco * Convert.ToInt32(sharpTurn); //TODO } else { suma = 0; } if (distance > 0) { score = 100 - (suma / (Convert.ToDecimal(distance) / 100)); } if (datosgps == null) { VehiculoGps vehiculoGPS = new VehiculoGps { vehiculo = vehiculo, dia = DateTime.Now.Day, mes = DateTime.Now.Month, anio = DateTime.Now.Year, //kilometerstraveled = Convert.ToInt32(response["odometer"].Value<string>().Replace(".","")), kilometerstraveled = distance, trips = trips, speeding = speeding, hardbraking = hardBreaking, sharpacceleration = sharpAcceleration, sharpturn = 0, ///no tiene latitude = latitude, longitude = longitude, 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 = trips; datosgps.speeding = speeding; datosgps.hardbraking = hardBreaking; datosgps.sharpacceleration = sharpAcceleration; datosgps.sharpturn = 0; datosgps.latitude = latitude; datosgps.longitude = longitude; 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); } //return vehicleGPS; } } } }
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); } }