Пример #1
0
        void Start()
        {
            foreach (var i in incidentsToProcess.GetConsumingEnumerable())
            {
                logger.Info("try to reallocate incident");
                var existingAllocation = allocationRepository.GetAllocationForIncident(i.IncidentId);

                RouterPoint incidentPoint;
                try {
                    incidentPoint = mapService.ResolvePoint(i.Latitude, i.Longitude);
                } catch (ResolveFailedException) {
                    logger.Info("Incident unreachable");
                    incidentRepository.MarkUnreachable(i.IncidentId);
                    continue;
                }

                try {
                    // Compute timing from ambulance position to incident. Take inTraffic into account.
                    var         timing = -1f;
                    RouterPoint ambulancePoint;
                    Route       route;
                    try {
                        var currentAmbulance = ambulanceRepository.Get(existingAllocation.AmbulanceId);
                        ambulancePoint = mapService.ResolvePoint(currentAmbulance.Latitude, currentAmbulance.Longitude);
                        route          = mapService.Calculate(ambulancePoint, incidentPoint);
                        timing         = route.TotalTime * 3;                 // inTraffic penalty
                    } catch (ResolveFailedException) {
                        logger.Info("Ambulance position cannot be resolved.");
                        timing = -1f;
                    }

                    // Search for a better ambulance
                    Ambulance closestAmbulance = null;
                    foreach (var ambulance in ambulanceRepository.GetAvailableAmbulances(existingAllocation.IncidentId))
                    {
                        try {
                            ambulancePoint = mapService.ResolvePoint(ambulance.Latitude, ambulance.Longitude);
                        } catch (ResolveFailedException) {
                            logger.Info("Ambulance position cannot be resolved.");
                            continue;
                        }

                        try {
                            route = mapService.Calculate(ambulancePoint, incidentPoint);
                        } catch (RouteNotFoundException) {
                            continue;
                        }

                        if (!(timing > 0) || route.TotalTime < timing)
                        {
                            closestAmbulance = ambulance;
                            timing           = route.TotalTime;
                        }
                    }

                    if (closestAmbulance == null)
                    {
                        logger.Info("No better available ambulance can reach the incident {0}", i.IncidentId);
                        continue;
                    }
                    else
                    {
                        logger.Info("Better ambulance found to reach the incident {0}", i.IncidentId);
                    }

                    Hospital closestHospital = null;
                    timing = -1f;
                    foreach (var hospital in hospitalRepository.GetOpenHospitals())
                    {
                        RouterPoint hospitalPoint;
                        try {
                            hospitalPoint = mapService.ResolvePoint(hospital.Latitude, hospital.Longitude);
                        } catch (ResolveFailedException) {
                            logger.Info("Hospital position cannot be resolved.");
                            hospitalRepository.Close(hospital.HospitalId);
                            continue;
                        }

                        try {
                            route = mapService.Calculate(incidentPoint, hospitalPoint);
                        } catch (RouteNotFoundException) {
                            continue;
                        }

                        if (!(timing > 0) || route.TotalTime < timing)
                        {
                            closestHospital = hospital;
                            timing          = route.TotalTime;
                        }
                    }

                    if (closestHospital == null)
                    {
                        logger.Info("No hospital can be reached from incident {0}", i.IncidentId);
                        incidentRepository.MarkUnreachable(i.IncidentId);
                    }

                    allocationRepository.CancelAllocation(existingAllocation.AllocationId, false);

                    var allocationId = incidentRepository.Allocate(i.IncidentId,
                                                                   closestAmbulance.AmbulanceId,
                                                                   closestHospital.HospitalId);
                    logger.Info("Allocated ambulance {0} on incident {1} (allocation {3})",
                                i.IncidentId, closestAmbulance.AmbulanceId, allocationId);
                } catch (Exception e) {
                    logger.Error("Error while allocation: " + e.Message);
                    logger.Error(e.StackTrace);
                }
            }
        }
Пример #2
0
        void Start()
        {
            foreach (var i in incidentsToProcess.GetConsumingEnumerable())
            {
                RouterPoint incidentPoint;
                try {
                    incidentPoint = mapService.ResolvePoint(i.Latitude, i.Longitude);
                } catch (ResolveFailedException) {
                    logger.Info("Incident unreachable");
                    incidentRepository.MarkUnreachable(i.IncidentId);
                    continue;
                }

                try {
                    Ambulance closestAmbulance = null;
                    var       timing           = -1f;
                    var       ambulances       = ambulanceRepository.GetAvailableAmbulances(i.IncidentId);
                    GetClosestAmbulance(incidentPoint, ref closestAmbulance, ref timing,
                                        ambulances.Where(x => x.Status != AmbulanceStatus.AvailableRadio));

                    if (closestAmbulance == null)
                    {
                        logger.Info("No available ambulance can reach the incident {0}", i.IncidentId);
                        logger.Info("Trying without taking existing allocations for that incident into account.");
                        ambulances = ambulanceRepository.GetAvailableAmbulances();
                        GetClosestAmbulance(incidentPoint, ref closestAmbulance, ref timing, ambulances);

                        if (closestAmbulance == null)
                        {
                            continue;
                        }
                    }

                    Hospital closestHospital = null;
                    timing = -1f;
                    foreach (var hospital in hospitalRepository.GetOpenHospitals())
                    {
                        RouterPoint hospitalPoint;
                        try {
                            hospitalPoint = mapService.ResolvePoint(hospital.Latitude, hospital.Longitude);
                        } catch (ResolveFailedException) {
                            logger.Info("Hospital position cannot be resolved.");
                            hospitalRepository.Close(hospital.HospitalId);
                            continue;
                        }

                        Route route;
                        try {
                            route = mapService.Calculate(incidentPoint, hospitalPoint);
                        } catch (RouteNotFoundException) {
                            continue;
                        }

                        if (!(timing > 0) || route.TotalTime < timing)
                        {
                            closestHospital = hospital;
                            timing          = route.TotalTime;
                        }
                    }

                    if (closestHospital == null)
                    {
                        logger.Info("No hospital can be reached from incident {0}", i.IncidentId);
                        incidentRepository.MarkUnreachable(i.IncidentId);
                    }

                    logger.Info("Allocated ambulance {0} on incident {1} for hospital {2}",
                                closestAmbulance.AmbulanceId, i.IncidentId, closestHospital.HospitalId);
                    var allocationId = incidentRepository.Allocate(i.IncidentId,
                                                                   closestAmbulance.AmbulanceId,
                                                                   closestHospital.HospitalId);
                    logger.Info("Allocation id = " + allocationId);
                } catch (Exception e) {
                    logger.Error("Error while allocation: " + e.Message);
                    logger.Error(e.StackTrace);
                }
            }
        }