C#C
C#2y ago
Rome

Handling events in "blocks"

Hi,

I am dealing with a problem where I need to move an Elevator based on the inputs received. The inputs are received by a comma delimited string.
For example: 9,8,2,d3,7,4,3
The d3 indicates that the input is delayed by 3. The event for 9, 8, and 2 will all fire in a sequence. After the Elevator was moved 3 times the events for 7, 4, and 3 will fire.
Basically 9, 8, 2 are pressed "at the same time" and 7, 4, 3 are pressed at the same time after the Elevator was moved 3 times.

The issue is that when the 9 event occurs, the Elevator has started moving towards 9 and that by the time event 2 fires the Elevator has already passed floor 2.
The Elevator does end up going to 2 eventually on its way back down but I need it to go to 2 on the way up since 2 should have been received together with the first block of 9, 8, 2.
How can I wait for the first block of events to occur before moving the Elevator?
  private void OnButtonPushed(object sender, ElevatorControllerEventArgs e)
  {
      requestsQueue.AddLast(e.Floor);
      SetDirection();
  }

private void MoveElevator()
{
    while (requestsQueue.Count > 0)
    {
        if (requestsQueue.Contains(currentFloor))
        {
            requestsQueue.Remove(currentFloor);
            controller.Stop();
        }
        else if (isMovingUp)
        {
            currentFloor++;
            controller.MoveUp();
        }
        else
        {
            currentFloor--;
            controller.MoveDown();
        }

        SetDirection();
    }
}

private void SetDirection()
{
    LinkedList<int> requestsGoingUp = GetAllRequestsAboveCurrentFloor(requestsQueue, currentFloor);
    LinkedList<int> requestsGoingDown = GetAllRequestsBelowCurrentFloor(requestsQueue, currentFloor);
    if (currentFloor == topFloor || requestsGoingUp.Count == 0)
    {
        isMovingUp = false;
    }
    else if (currentFloor == 1 || requestsGoingDown.Count == 0)
    {
        isMovingUp = true;
    }
    MoveElevator();
}
Was this page helpful?