public async Task Handle(TlFuelEevents notification, CancellationToken cancellationToken) { var fuelRecordMsg = notification.Events.OrderBy(x => x.DateTimeUtc).Last(); var lastRecord = await _context.FuelConsumptions.OrderByDescending(x => x.DateTimeUtc) // ReSharper disable once TooManyChainedReferences .FirstOrDefaultAsync(x => x.VehicleId == fuelRecordMsg.VehicleId, cancellationToken: cancellationToken).ConfigureAwait(false); var fuelConsumption = SetFuelConsumption(fuelRecordMsg); // ReSharper disable once ComplexConditionExpression if (lastRecord != null && fuelRecordMsg.MileStoneCalculated || lastRecord == null) { if (lastRecord != null) { fuelConsumption.Milestone += lastRecord.Milestone; } } else if (!fuelRecordMsg.MileStoneCalculated) { fuelConsumption.TotalFuelConsumed = fuelRecordMsg.FuelConsumption; } _context.FuelConsumptions.Add(fuelConsumption); await _context.SaveChangesAsync(cancellationToken).ConfigureAwait(false); }
public async Task Handle(TLGpsDataEvents notification, CancellationToken cancellationToken) { try { await _semaphore.WaitAsync(cancellationToken).ConfigureAwait(false); var lastEvent = notification.Events.LastOrDefault(); var box = await GetboxAsync(lastEvent).ConfigureAwait(false); List <TLEcoDriverAlertEvent> ecoDriveEvents = new List <TLEcoDriverAlertEvent>(); List <TLGpsDataEvent> gpsDataEvents = new List <TLGpsDataEvent>(); List <TLFuelMilstoneEvent> tlFuelMilstoneEvents = new List <TLFuelMilstoneEvent>(); List <TLExcessSpeedEvent> speedEvents = new List <TLExcessSpeedEvent>(); foreach (var teltonikaGps in notification.Events) { if (box == null) { continue; } // envoi des données GPs var gpsDataEvent = _mapper.Map <TLGpsDataEvent>(teltonikaGps); gpsDataEvent.BoxId = box.Id; Trace.WriteLine(gpsDataEvent.DateTimeUtc + " lat:" + gpsDataEvent.Lat + " long:" + gpsDataEvent.Long); //await mediator.Publish(gpsDataEvent).ConfigureAwait(false); gpsDataEvents.Add(gpsDataEvent); if (box.Vehicle == null) { continue; } InitAllIoElements(teltonikaGps); var canInfo = ProceedTNCANFilters(teltonikaGps); if (canInfo != default(TLFuelMilstoneEvent)) { canInfo.VehicleId = box.Vehicle.Id; if (box.Vehicle.CustomerId.HasValue) { canInfo.CustomerId = box.Vehicle.CustomerId.Value; } // calcul de la distance par rapport au dernier point GPS if (canInfo.Milestone <= 0) { var positionQuery = await GetLastPositionAsync(box.Id); if (positionQuery != null) { var distance = GetGpsDistance(gpsDataEvents); Trace.TraceInformation($"time :{gpsDataEvent.DateTimeUtc} Ditance: " + distance); canInfo.Milestone = distance; canInfo.MileStoneCalculated = true; if (distance > 0 && box.VehicleId != null) { await mediator.Publish(new TLMilestoneVehicleEvent { Milestone = distance, VehicleId = box.VehicleId.Value, EventUtc = teltonikaGps.DateTimeUtc }, cancellationToken).ConfigureAwait(false); } } } // await mediator.Publish(canInfo); tlFuelMilstoneEvents.Add(canInfo); } // ReSharper disable once ComplexConditionExpression if (box.Vehicle.SpeedAlertEnabled && box.Vehicle.MaxSpeed <= teltonikaGps.Speed || teltonikaGps.Speed > 85) { var alertExeedSpeed = ProceedTLSpeedingAlert(teltonikaGps, box.Vehicle.Id, box.Vehicle.CustomerId); // await mediator.Publish(alertExeedSpeed); speedEvents.Add(alertExeedSpeed); } var ecoDriveEvent = ProceedEcoDriverEvents(teltonikaGps, box.Vehicle.Id, box.Vehicle.CustomerId); if (ecoDriveEvent != default(TLEcoDriverAlertEvent)) { //ecoDriveEvents.Add(ecoDriveEvent); await mediator.Publish(ecoDriveEvent, cancellationToken).ConfigureAwait(false); } } if (speedEvents.Any()) { await mediator.Publish(speedEvents.OrderBy(x => x.EventUtc).LastOrDefault(), cancellationToken).ConfigureAwait(false); } if (gpsDataEvents.Any()) { await GeoReverseCodeGpsDataAsync(gpsDataEvents).ConfigureAwait(false); var gpSdata = gpsDataEvents.OrderBy(x => x.DateTimeUtc).LastOrDefault(); await mediator.Publish(gpSdata, cancellationToken).ConfigureAwait(false); } if (tlFuelMilstoneEvents.Any()) { var events = new TlFuelEevents { Id = Guid.NewGuid(), Events = tlFuelMilstoneEvents, //TlGpsDataEvents = gpsDataEvents }; await mediator.Publish(events, cancellationToken).ConfigureAwait(false); } _semaphore.Release(); } catch (Exception e) { Trace.TraceWarning(e.Message + " details:" + e.StackTrace); //throw; } }
public async Task Consume(ConsumeContext <TLGpsDataEvents> context) { try { var box = await GetBoxAsync(context.Message.Events.LastOrDefault()).ConfigureAwait(false); List <TLEcoDriverAlertEvent> ecoDriveEvents = new List <TLEcoDriverAlertEvent>(); List <TLGpsDataEvent> gpsDataEvents = new List <TLGpsDataEvent>(); List <TLFuelMilstoneEvent> tlFuelMilestoneEvents = new List <TLFuelMilstoneEvent>(); List <TLExcessSpeedEvent> speedEvents = new List <TLExcessSpeedEvent>(); List <TlIdentifierEvent> identifierEvents = new List <TlIdentifierEvent>(); EcoDriveService ecoDrive; DriverCardService cardService; foreach (var source in context.Message.Events) { if (box == null) { continue; } ecoDrive = new EcoDriveService(data: source); cardService = new DriverCardService(); // envoi des données GPs var gpsDataEvent = _mappe.Map <TLGpsDataEvent>(source); gpsDataEvent.BoxId = box.Id; Trace.WriteLine( gpsDataEvent.DateTimeUtc + " lat:" + gpsDataEvent.Lat + " long:" + gpsDataEvent.Long); if (box.Vehicle == null) { break; } if (box.Vehicle == null) { break; } InitAllIoElements(source); if (source.AllIoElements.ContainsKey(TNIoProperty.Ignition)) { gpsDataEvent.Ignition = Convert.ToUInt32(source.AllIoElements[TNIoProperty.Ignition]) == 1; } gpsDataEvents.Add(gpsDataEvent); var canInfo = ecoDrive.ProceedTNCANFilters(); identifierEvents = cardService.ProceedDriverCardDetection(source, box.Vehicle.CustomerId.Value); if (canInfo != default(TLFuelMilstoneEvent)) { canInfo.VehicleId = box.Vehicle.Id; if (box.Vehicle?.CustomerId != null) { canInfo.CustomerId = box.Vehicle.CustomerId.Value; } // calcul de la distance par rapport au dernier point GPS if (canInfo.Milestone <= 0) { var positionQuery = await GetLastPositionAsync(box.Id).ConfigureAwait(false); if (positionQuery != null) { var distance = GetGpsDistance(gpsDataEvents); Trace.TraceInformation($"time :{gpsDataEvent.DateTimeUtc} Ditance: " + distance); canInfo.Milestone = distance; canInfo.MileStoneCalculated = true; if (distance > 0 && box.VehicleId != null) { await context.Publish(new TLMilestoneVehicleEvent { Milestone = distance, VehicleId = box.VehicleId.Value, EventUtc = source.DateTimeUtc }).ConfigureAwait(false); } } } tlFuelMilestoneEvents.Add(canInfo); } // ReSharper disable once ComplexConditionExpression if (box.Vehicle.SpeedAlertEnabled && box.Vehicle.MaxSpeed <= source.Speed || source.Speed > 85) { var alertExceedSpeed = ecoDrive.ProceedTLSpeedingAlert(box.Vehicle.Id, box.Vehicle.CustomerId); speedEvents.Add(alertExceedSpeed); } var ecoDriveEvent = ecoDrive.ProceedEcoDriverEvents(box.Vehicle.Id, box.Vehicle.CustomerId); if (ecoDriveEvent != default(TLEcoDriverAlertEvent)) { await context.Publish(ecoDriveEvent).ConfigureAwait(false); } } if (identifierEvents.Any()) { foreach (var tlIdentifierEvent in identifierEvents) { await context.Publish(tlIdentifierEvent).ConfigureAwait(false); } } if (speedEvents.Any()) { await context.Publish(speedEvents.OrderBy(x => x.EventUtc).LastOrDefault()).ConfigureAwait(false); } if (gpsDataEvents.Any()) { try { await GeoReverseCodeGpsDataAsync(gpsDataEvents).ConfigureAwait(false); } catch (Exception e) { Console.WriteLine(e.Message); } foreach (var @event in gpsDataEvents) { await context.Publish(@event).ConfigureAwait(false); } } if (tlFuelMilestoneEvents.Any()) { var events = new TlFuelEevents { Id = Guid.NewGuid(), Events = tlFuelMilestoneEvents }; await context.Publish(events).ConfigureAwait(false); } } catch (Exception e) { Trace.TraceWarning(e.Message + " details:" + e.StackTrace); throw; } }