public void AddOrUpadateLeg(BaseLeg leg)
        {
            if (leg != null)
            {
                try
                {
                    var airportLegs = _database.GetCollection <BaseLeg>("airportLegs");
                    var dblegs      = airportLegs.Find(dbLeg => dbLeg.Id == leg.Id);
                    var haveLegs    = dblegs.Any();

                    if (haveLegs)
                    {
                        var update = Builders <BaseLeg> .Update
                                     .Set(dbLeg => dbLeg.InEmergency, leg.InEmergency)
                                     .Set(dbLeg => dbLeg.LegType, leg.LegType)
                                     .Set(dbLeg => dbLeg.InPlanes, leg.InPlanes);

                        airportLegs.UpdateOne(dbLeg => dbLeg.Id == leg.Id, update);
                    }
                    else
                    {
                        airportLegs.InsertOne(leg);
                    }
                }
                catch (Exception ex)
                {
                    Debug.WriteLine(ex);
                }
            }
        }
        private bool CanGo(BaseLeg legTo)
        {
            // verific if the plne can go to leg type
            if (legTo != null)
            {
                switch (legTo.LegType)
                {
                case EState.InAir:
                case EState.AirWaiting1:
                case EState.AirWaiting2:
                case EState.AirWaiting3:
                    return(!InLand);

                case EState.AccessAfterLanding:
                case EState.Boarding:
                case EState.AccessToDeparting:
                case EState.Terminal:
                    return(InLand);

                case EState.LandingRoad:
                default:
                    return(true);
                }
            }
            else
            {
                // if leg is null the plane end flight
                // the plane can end flight
                //if it was in land on landing or in air on departing
                return(DestinationOut != InLand);
            }
        }
        private async Task <bool> MovePlane(BaseLeg legTo)
        {
            //Call to real plane.
            await Task.Delay(1000);

            return(true);
        }
 public void SetWarning(Plane plane = null, BaseLeg leg = null)
 {
     // create an emergency in pane or in leg or random in airport
     if (plane != null)
     {
         var tPlane = Tower.Planes.First(p => p.Id == plane.Id);
         if (tPlane != null)
         {
             tPlane.SetEmergency();
         }
     }
     else if (leg != null)
     {
         var tLeg = Tower.Legs.First(l => l.Id == leg.Id);
         if (tLeg != null)
         {
             SetLegEmergency(tLeg);
         }
     }
     else
     {                             // by default emergency will be choosen randomly
         if (_rnd.Next() % 2 == 0) // mod 2 emergency in plane
         {
             int count = Tower.Planes.Count();
             Tower.Planes.ElementAt(_rnd.Next(count)).SetEmergency();
         }
         else
         {
             int count    = Tower.Legs.Count();
             var legTower = Tower.Legs.ElementAt(_rnd.Next(count));
             SetLegEmergency(legTower);
         }
     }
 }
예제 #5
0
 public Leg(BaseLeg dbLeg)
 {
     InPlanes = new Plane[1];
     if (dbLeg != null && dbLeg.InPlanes != null && dbLeg.InPlanes[0] != null)
     {
         InPlanes[0] = new FlightPlane(dbLeg.InPlanes[0]);
     }
 }
        public override async Task <bool> GoTo(BaseLeg legTo)
        {
            // Change the proprety of plane in move
            // the result is if the move be complete
            bool res = false;

            //If is in move skip to no spen time in block
            //and no doble move
            if (!InMove)
            {
                // the change of proprety and move delay
                // have ben in block, to dont have data broken
                lock (_gate)
                {
                    InMove = true;
                    if (CanGo(legTo))
                    {
                        if (MovePlane(legTo).Result)
                        {
                            if (legTo != null)
                            {
                                State = legTo.LegType;

                                if (State == EState.LandingRoad)
                                {
                                    if (DestinationOut)
                                    {
                                        InLand = false;
                                    }
                                    else
                                    {
                                        InLand = true;
                                    }
                                }
                            }
                            else
                            {
                                State = DestinationOut ? EState.InAir : EState.Terminal;
                            }

                            EnterStateTime = DateTime.Now;

                            res    = true;
                            InMove = false;
                        }
                        else
                        {
                        }
                    }
                    else
                    {
                    }
                }
            }
            return(res);
        }
        private async void LegIsEmpetyAction(BaseLeg leg)
        {
            // leg empety enter a plane
            // search and enter them
            if (leg != null && leg.IsOpen)
            {
                LogMessage($"Leg {leg.Id} be open.");
                var bestPlane = await GetPriorityPlane(leg.LegType);

                EnterPlaneInLeg(bestPlane, leg);
            }
        }
 public void StopWarning(BaseLeg leg)
 {
     if (leg != null)
     {
         var tLeg = Tower.Legs.First(l => l.Id == leg.Id);
         if (tLeg != null)
         {
             tLeg.EmergencyStop();
             DataService.AddOrUpadateLeg(tLeg);
         }
     }
 }
 public async void UpadateLeg(BaseLeg leg)
 {
     if (!_disable)
     {
         try
         {
             var response = await _hub.Invoke <BaseLeg>("UpadateLeg", leg);
         }
         catch (Exception ex)
         {
         }
     }
 }
        private void SetLegEmergency(BaseLeg leg)
        {
            // simulate leg emergency close leg
            leg.InEmergency = true;

            DataService.AddOrUpadateLeg(leg);

            //  call real help
            //Thread.Sleep(10000);
            //leg.EmergencyStop();
            //DataService.AddOrUpadateLeg(leg);
            //_httpSocket.UpadateLeg(leg);
        }
 public void StopEmergency(BaseLeg leg = null)
 {
     Clients.Others.onEmergencyStop(leg);
 }
 public void SetEmergency(Plane plane = null, BaseLeg leg = null)
 {
     Clients.Others.onEmergencyStart(plane, leg);
 }
 public void UpadateLeg(BaseLeg leg)
 {
     // in chage emergency status, plane enter or leave
     Clients.Others.upadateLeg(leg);
 }
        private async void EnterPlaneInLeg(Plane plane, BaseLeg nextLeg)
        {
            // move the plane to next leg.
            //nextleg null flight end.
            // plane null because have not plane how dont block to enter this leg
            if (plane != null)
            {
                // verific if plane can enter and enter plane have be atom func
                bool planeEnter = false;
                lock (_moveLocer)
                {
                    // verific if enter in this leg can do block
                    if (!_piority.IsBlock(plane).Result&& nextLeg != null)
                    {
                        planeEnter = nextLeg.Enter(plane).Result;
                    }
                }
                // enter the plane in leg or return to factory if null
                if (nextLeg == null || planeEnter)
                {
                    // move the plane
                    if (await plane.GoTo(nextLeg))
                    {
                        _dataService.AddOrUpadateLeg(nextLeg);
                        _clientHtttpSocketService.UpadateLeg(nextLeg);

                        // update the move date to no use disfrezzertimer.
                        LastMoveDateUpDate();

                        // Keep Tasks in order to know when they will end
                        var allTasks = new List <Task>();

                        // remove the plane e from order legs
                        foreach (var leg in GetPlaneLegs(plane).Where(leg => leg != nextLeg))
                        {
                            var localLeg = leg;
                            // update the after leave the plane
                            // using ContunueWhit to do some thing in ohters legs
                            var task = localLeg.Leave(plane).ContinueWith((Task) =>
                            {
                                _dataService.AddOrUpadateLeg(localLeg);
                                _clientHtttpSocketService.UpadateLeg(localLeg);
                            });
                            allTasks.Add(task);
                        }

                        // only update the plane after remove from all legs
                        await Task.WhenAll(allTasks);

                        _inAirPlanes.Remove(plane);
                        _inTermialPlanes.Remove(plane);
                        _dataService.RemovePlane(plane);
                        _clientHtttpSocketService.UpadatePlane(plane);

                        if (nextLeg != null)
                        {
                            //in end of move try to move the plane to next leg
                            LogMessage($"Plane {plane.Id}, enter to leg {nextLeg.Id}");
                            EnterPlaneInLeg(plane, GetNextLeg(nextLeg.LegType, plane.DestinationOut));
                        }
                        else
                        {
                            // if end the flight return plane to factory, nextleg null end flight
                            string destinationStr = plane.DestinationOut ? "depart" : "land";
                            LogMessage($"Plane {plane.Id}, end {destinationStr}.");
                            OnPlaneReturn?.Invoke(plane);
                            await _httpService.ReturnPlaneToFactory(plane);
                        }
                    }
                    else
                    {
                        // the plane fail to enter in leg
                        if (nextLeg != null)
                        {
                            await Task.Delay(1000);

                            LogMessage($"Plane {plane.Id} fail to enter in next leg {nextLeg.Id}.");
                            await nextLeg.Leave(plane);

                            // _dataService.AddOrUpadateLeg(nextLeg);
                            //_clientHtttpSocketService.UpadateLeg(nextLeg);
                        }
                    }
                }
            }
        }