Пример #1
0
        private void buttonFloorDownClick(object sender, RoutedEventArgs e)
        {
            // get pressed button
            Button button = e.OriginalSource as Button;

            // get selected floor number (starts from 1)
            int selectedFloor = (int)button.Tag;

            FloorCallButtonsList[_floorsTotal - selectedFloor].floorDownCall = true;

            // update ListView data and repaint
            floorListView.Items.Refresh();

            // add data to task list
            double targetFloorPositionY = (_floorsTotal - selectedFloor + 1) * FLOOR_HEIGHT - LIFT_HEIGHT - 1;

            // create new task item
            CallTaskItem newTaskItem = new CallTaskItem
            {
                floorNumber    = selectedFloor,
                floorPositionY = targetFloorPositionY,
                callType       = CallButtonTypeEnum.FLOOR_DOWN_BUTTON
            };

            AddData(newTaskItem);

            ShowTestData();
        }
Пример #2
0
        private void buttonLiftClick(object sender, RoutedEventArgs e)
        {
            Button button = e.OriginalSource as Button;

            int selectedFloor = 0;

            // https://msdn.microsoft.com/ru-ru/library/system.convert(v=vs.110).aspx
            try
            {
                selectedFloor = Convert.ToInt32(button.Content); // convert from object to int
            }
            catch
            {
                MessageBox.Show("Can`t convert to int");
                return;
            }

            double targetFloorPositionY = (_floorsTotal - selectedFloor + 1) * FLOOR_HEIGHT - LIFT_HEIGHT - 1;

            // if current pos = target pos then DO NOTHING
            if (targetFloorPositionY == _currentLiftPositionY)
            {
                //MessageBox.Show("NOWHERE TO MOVE");
                ShowTestData();
                return;
            }

            LiftControlButtonsList[_floorsTotal - selectedFloor].liftInnerCall = true;

            // update ListView data and repaint
            liftListView.Items.Refresh();

            CallTaskItem newTaskItem = new CallTaskItem
            {
                floorNumber    = selectedFloor,
                floorPositionY = targetFloorPositionY,
                callType       = CallButtonTypeEnum.INNER_LIFT_BUTTON
            };

            // add data to task list
            AddData(newTaskItem);

            ShowTestData();
        }
Пример #3
0
        private void AddData(CallTaskItem callTaskItem)
        {
            // if such target floor already has ??
            for (int i = 0; i < _liftCallsList.Count; i++)
            {
                if (_liftCallsList[i].callType == callTaskItem.callType &&
                    _liftCallsList[i].floorNumber == callTaskItem.floorNumber
                    )
                {
                    //MessageBox.Show("already has");
                    return;
                }
            }

            // add new task to tasks list
            _liftCallsList.Add(callTaskItem);

            // MAIN LIFT LOGIC
            SetLiftMoveSequence();

            ShowTestData();
        }
Пример #4
0
        private void SetClosestTarget()
        {
            // initial minimal value
            double minValue = FLOOR_HEIGHT * _floorsTotal;

            int targetIndex = -1;

            if (_liftState == LiftStateEnum.MOVE_UP)
            {
                for (int i = 0; i < _liftCallsList.Count; i++)
                {
                    double newMin = Math.Abs(_currentLiftPositionY - _liftCallsList[i].floorPositionY);

                    if (newMin < minValue)
                    {
                        minValue = newMin; // set found min value

                        // if closest target upper then current position
                        if (_liftCallsList[i].floorPositionY < _currentLiftPositionY)
                        {
                            targetIndex = i;
                        }
                    }
                }

                if (targetIndex == -1)                    // if no upper floors
                {
                    _liftState = LiftStateEnum.MOVE_DOWN; // then change lift direction
                    Debug.WriteLine("change lift direction: DOWN");
                }

                Debug.WriteLine("SORT - UP: targetIndex = " + targetIndex);
            }


            if (_liftState == LiftStateEnum.MOVE_DOWN)
            {
                for (int i = 0; i < _liftCallsList.Count; i++)
                {
                    double newMin = Math.Abs(_currentLiftPositionY - _liftCallsList[i].floorPositionY);

                    if (newMin < minValue)
                    {
                        minValue = newMin; // set found min value

                        // if closest target upper then current position
                        if (_liftCallsList[i].floorPositionY > _currentLiftPositionY)
                        {
                            targetIndex = i;
                        }
                    }
                }

                if (targetIndex == -1)                  // if no upper floors
                {
                    _liftState = LiftStateEnum.MOVE_UP; // then change lift direction
                    Debug.WriteLine("change lift direction: UP");
                }
            }


            if (_liftState == LiftStateEnum.MOVE_UP)
            {
                for (int i = 0; i < _liftCallsList.Count; i++)
                {
                    double newMin = Math.Abs(_currentLiftPositionY - _liftCallsList[i].floorPositionY);

                    if (newMin < minValue)
                    {
                        minValue = newMin; // set found min value

                        // if closest target upper then current position
                        if (_liftCallsList[i].floorPositionY < _currentLiftPositionY)
                        {
                            targetIndex = i;
                        }
                    }
                }


                if (targetIndex == -1)                    // if no upper floors
                {
                    _liftState = LiftStateEnum.MOVE_DOWN; // then change lift direction
                    Debug.WriteLine("change lift direction: DOWN");
                }

                Debug.WriteLine("SORT - UP: targetIndex = " + targetIndex);
            }

            if (targetIndex >= 0 && targetIndex < _liftCallsList.Count) // add !!!
            {
                // set target element to 0 position (swap elements)
                lock (_lockObject)
                {
                    CallTaskItem tempItem = _liftCallsList[0];
                    _liftCallsList[0]           = _liftCallsList[targetIndex];
                    _liftCallsList[targetIndex] = tempItem;
                }
            }
        }
Пример #5
0
        private void SetLiftMoveSequence()
        {
            // initial minimal value
            double minValue = FLOOR_HEIGHT * _floorsTotal;

            int targetIndex = -1;

            Debug.WriteLine("SetLiftMoveSequence: _currentLiftPositionY = " + _currentLiftPositionY);

            if (_liftState == LiftStateEnum.MOVE_UP)
            {
                for (int i = 0; i < _liftCallsList.Count; i++)
                {
                    double newMin = Math.Abs(_currentLiftPositionY - _liftCallsList[i].floorPositionY);

                    if (newMin < minValue &&
                        _liftCallsList[i].floorPositionY < _currentLiftPositionY
                        &&
                        (_liftCallsList[i].callType == CallButtonTypeEnum.FLOOR_UP_BUTTON ||
                         _liftCallsList[i].callType == CallButtonTypeEnum.INNER_LIFT_BUTTON
                        ))
                    {
                        minValue = newMin; // set found min value

                        targetIndex = i;
                    }
                }

                if (targetIndex == -1)                    // if no upper floor tasks
                {
                    _liftState = LiftStateEnum.MOVE_DOWN; // then change lift direction
                    Debug.WriteLine("NO FOUND UP TASKS !!");
                }
            }

            Debug.WriteLine("SORT - UP: targetIndex = " + targetIndex);

            // find lower tasks
            if (_liftState == LiftStateEnum.MOVE_DOWN)
            {
                for (int i = 0; i < _liftCallsList.Count; i++)
                {
                    double newMin = Math.Abs(_currentLiftPositionY - _liftCallsList[i].floorPositionY);

                    if (newMin < minValue &&
                        _liftCallsList[i].floorPositionY > _currentLiftPositionY
                        &&
                        (_liftCallsList[i].callType == CallButtonTypeEnum.FLOOR_UP_BUTTON ||
                         _liftCallsList[i].callType == CallButtonTypeEnum.INNER_LIFT_BUTTON
                        ))
                    {
                        minValue = newMin; // set found min value

                        targetIndex = i;
                    }
                }

                if (targetIndex == -1)                  // if no lower floors
                {
                    _liftState = LiftStateEnum.MOVE_UP; // then change lift direction
                    Debug.WriteLine("NO FOUND DOWN TASKS !!");
                }
            }

            if (targetIndex >= 0 && targetIndex < _liftCallsList.Count) // add !!!
            {
                // set target element to 0 position (swap elements)
                lock (_lockObject)
                {
                    CallTaskItem tempItem = _liftCallsList[0];
                    _liftCallsList[0]           = _liftCallsList[targetIndex];
                    _liftCallsList[targetIndex] = tempItem;
                }
            }

            ShowTestData();

            // run lift processing
            if (!_liftProcessing)
            {
                // move to target
                _backgroundWorkerCloseDoor.RunWorkerAsync(_liftCallsList[0].floorPositionY);
            }
        }