private void ReceiveCall(ElevatorCall currCall)
        {
            this.CurrAgent = currCall.Agent;
            int waitTime = Math.Abs((int)CurrFloor - (int)currCall.TargetFloor);

            Console.WriteLine($"Elevator called by agent {CurrAgent.Id} from {(int)this.CurrFloor} floor to {(int)currCall.TargetFloor} floor.");
            Thread.Sleep(1000 * waitTime);
            Console.WriteLine(waitTime + " second passed.");
            this.CurrFloor = currCall.TargetFloor;



            int targetFloor = currCall.Agent.ChooseFloor();

            int waitTimeSecond = Math.Abs((int)CurrFloor - targetFloor);

            Console.WriteLine($"Elevator went for {targetFloor} floor. requested by agent {CurrAgent.Id}");
            Thread.Sleep(1000 * waitTimeSecond);
            this.CurrFloor = (Floor)targetFloor;

            while ((int)CurrFloor > CurrAgent.SecurityClearenceLevel)
            {
                Console.WriteLine($"Agent {CurrAgent.Id} security clearence is not high enough , choosing another floor!");
                targetFloor = CurrAgent.ChooseFloor();
                Console.WriteLine($"Elevator went for {targetFloor} floor. requested by agent {CurrAgent.Id}");
                waitTimeSecond = Math.Abs((int)CurrFloor - targetFloor);
                Thread.Sleep(1000 * waitTimeSecond);
                Console.WriteLine($"{waitTime} seconds passed");
                this.CurrFloor = (Floor)targetFloor;
            }

            Console.WriteLine($"Elevator reached {(int)CurrFloor} floor after {waitTimeSecond} seconds.");

            semaphore.Release();
        }
        private async Task ProcessQueue(ElevatorCall currCall)
        {
            //Console.WriteLine($"{currCall.Agent.Id} - first element");

            await Task.Run(() => ReceiveCall(currCall));

            //ElevatorCalls.Dequeue();
        }
        public async Task Call(ElevatorCall currCall)
        {
            //ElevatorCalls.Enqueue(currCall);
            semaphore.WaitOne();
            //while (ElevatorCalls.Peek().Agent.Id != currCall.Agent.Id)
            //{
            //   await ProcessQueue(currCall);

            //}

            await ProcessQueue(currCall);
        }
Пример #4
0
        static async Task ControlAgent(Agent agent)
        {
            int random  = rnd.Next(2, 5);
            int counter = 0;

            while (counter < random)
            {
                Thread.Sleep(rnd.Next(1, 4) * 1000);
                Console.WriteLine("             Agent " + agent.Id + " called the elevator.");

                ElevatorCall ec = agent.CallElevator();
                await elevator.Call(ec);

                counter++;
            }
            Console.WriteLine("             Agent " + agent.Id + " went home.");
        }