// --------------------------------------------- // LIFT // lift moving process private void Lift_DoWork(object sender, DoWorkEventArgs e) { BackgroundWorker liftWorker = (BackgroundWorker)sender; // get parameter: target Y-coordinate /// double endFloorHeight = (double)e.Argument; double endFloorHeight = _liftCallsList[0].floorPositionY; // calculate time delay to 1 pixel paint // result will be a little slower int delayMilliseconds = (int)(_floorHeight / _liftSpeed / (double)FLOOR_HEIGHT * 1000 * 0.95d); //MessageBox.Show(delayMilliseconds.ToString()); /// MessageBox.Show("endFloorHeight = " + endFloorHeight + " _liftPositionY = " + _liftPositionY); double start = _currentLiftPositionY; double end = endFloorHeight; if (endFloorHeight > _currentLiftPositionY) // moving DOWN { // set LIFT STATE = MOVING DOWN _liftState = LiftStateEnum.MOVE_DOWN; for (double height = start; height <= end; height++) // add "<=" !!! { if (_backgroundWorkerLift.CancellationPending) { e.Cancel = true; return; } liftWorker.ReportProgress(0, height); // lock threads common variables lock (_lockObject) { _currentLiftPositionY = height; } System.Threading.Thread.Sleep(delayMilliseconds); } e.Result = _currentLiftPositionY; } else // moving UP { // set LIFT STATE = MOVING UP _liftState = LiftStateEnum.MOVE_UP; for (double height = start; height > end - 1; height--) { if (_backgroundWorkerLift.CancellationPending) { e.Cancel = true; return; } liftWorker.ReportProgress(0, height); _currentLiftPositionY = height; System.Threading.Thread.Sleep(delayMilliseconds); } /// e.Result = _currentLiftPositionY; e.Result = _liftCallsList[0].floorPositionY; } Debug.WriteLine("_liftState = " + _liftState.ToString()); }
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; } } }
private void Init() { // set schema canvas height showLift.Height = _floorsTotal * FLOOR_HEIGHT; //TranslateTransformY = _floorsTotal * FLOOR_HEIGHT; //OnPropertyChanged("TranslateTransformY"); //TranslateTransform translateTransform = new TranslateTransform(0, 300); //ScaleTransform scaleTransform = new ScaleTransform(0, -1); //this.showLift.RenderTransform = scaleTransform; //this.showLift.RenderTransform = translateTransform; // ObservableCollection<FloorCallItem> FloorCallButtonsList // -------------------------------------------------- // create floor buttons for (int i = _floorsTotal - 1; i >= 0; i--) { // create element FloorCallItem floorCallItem = new FloorCallItem { floorNumber = i + 1 }; if (i == (_floorsTotal - 1)) // top floor { floorCallItem.buttonUpVisibility = System.Windows.Visibility.Hidden; } else if (i == 0) // first floor { floorCallItem.buttonDownVisibility = System.Windows.Visibility.Hidden; } else // other floors { floorCallItem.buttonUpVisibility = System.Windows.Visibility.Visible; floorCallItem.buttonDownVisibility = System.Windows.Visibility.Visible; } FloorCallButtonsList.Add(floorCallItem); } // -------------------------------------------------- // create inner lift control panel for (int i = (_floorsTotal - 1); i >= 0; i--) { // create element LiftInnerCallItem liftInnerCallItem = new LiftInnerCallItem { targetFloor = i + 1 }; LiftControlButtonsList.Add(liftInnerCallItem); } // -------------------------------------------------- // set default lift state /// _liftState = LiftStateEnum.STOPPED; _liftState = LiftStateEnum.MOVE_UP; // TEST !! _doorState = DoorStateEnum.DOORS_OPENED; /// _liftPositionY = (_floorsTotal) * FLOOR_HEIGHT - LIFT_HEIGHT - 1; // lift on the 1 floor /// _liftPositionY = (_floorsTotal - 2 + 1) * FLOOR_HEIGHT - LIFT_HEIGHT - 1; // lift on the 2 floor CurrentLiftFloor = 2; // lift on the FIRST floor !! _currentLiftPositionY = (_floorsTotal - CurrentLiftFloor + 1) * FLOOR_HEIGHT - LIFT_HEIGHT - 1; // lift on the 1 floor !! // (_floorsTotal - CurrentLiftFloor + 1) = (_currentLiftPositionY + LIFT_HEIGHT + 1) / FLOOR_HEIGHT // CurrentLiftFloor = _floorsTotal + 1 - (_currentLiftPositionY + LIFT_HEIGHT + 1) / FLOOR_HEIGHT //MessageBox.Show("_liftPositionY = " + _liftPositionY.ToString()); // 1919 // --------------------------------------------- // LIFT backgroundWorker _backgroundWorkerLift = new BackgroundWorker { WorkerReportsProgress = true, WorkerSupportsCancellation = true }; // Lift backgroundWorker events //For the performing operation in the background _backgroundWorkerLift.DoWork += Lift_DoWork; //For the display of operation progress to UI _backgroundWorkerLift.ProgressChanged += Lift_ProgressChanged; //After the completation of operation _backgroundWorkerLift.RunWorkerCompleted += Lift_RunWorkerCompleted; // DOOR CLOSE backgroundWorker _backgroundWorkerCloseDoor = new BackgroundWorker { WorkerReportsProgress = true, WorkerSupportsCancellation = false }; // --------------------------------------------- // DOOR CLOSE backgroundWorker //For the performing operation in the background _backgroundWorkerCloseDoor.DoWork += CloseDoor_DoWork; //For the display of operation progress to UI _backgroundWorkerCloseDoor.ProgressChanged += CloseDoor_ProgressChanged; //After the completation of operation _backgroundWorkerCloseDoor.RunWorkerCompleted += CloseDoor_RunWorkerCompleted; // --------------------------------------------- // DOOR OPEN backgroundWorker _backgroundWorkerOpenDoor = new BackgroundWorker { WorkerReportsProgress = true, WorkerSupportsCancellation = false }; //For the performing operation in the background _backgroundWorkerOpenDoor.DoWork += OpenDoor_DoWork; //For the display of operation progress to UI _backgroundWorkerOpenDoor.ProgressChanged += OpenDoor_ProgressChanged; //After the completation of operation _backgroundWorkerOpenDoor.RunWorkerCompleted += OpenDoor_RunWorkerCompleted; // --------------------------------------------- // DOOR OPENED WAIT backgroundWorker _backgroundWorkerDoorWaitOpened = new BackgroundWorker { WorkerReportsProgress = false, WorkerSupportsCancellation = true }; //For the performing operation in the background _backgroundWorkerDoorWaitOpened.DoWork += DoorWaitOpened_DoWork; //For the display of operation progress to UI /// _backgroundWorkerDoorWaitOpened.ProgressChanged += DoorWaitOpened_ProgressChanged; //After the completation of operation _backgroundWorkerDoorWaitOpened.RunWorkerCompleted += DoorWaitOpened_RunWorkerCompleted; // --------------------------------------------- // PAINT LIFT SCHEMA for (int i = _floorsTotal; i > 0; i--) { // -------------------------------------------------- // create floor horizontal line Line floorLine = new Line() { Stroke = System.Windows.Media.Brushes.Black, StrokeThickness = _floorLineStrokeThickness, X1 = 0, Y1 = i * FLOOR_HEIGHT - _floorLineStrokeThickness, X2 = 300, Y2 = i * FLOOR_HEIGHT - _floorLineStrokeThickness, HorizontalAlignment = HorizontalAlignment.Center }; /// myLine.VerticalAlignment = VerticalAlignment.Center; // add to parent control this.showLift.Children.Add(floorLine); // -------------------------------------------------- // create TextBlock for display floor number TextBlock floorNumberTextBlock = new TextBlock { Text = (_floorsTotal - i + 1).ToString() + " этаж", Width = 100, Height = 50, FontSize = 20 //BorderThickness = new Thickness(1), //BorderBrush = new SolidColorBrush(Color.FromRgb(5, 5, 5)), //Margin = new Thickness(20, 20, 0, 0) }; // add to parent control this.showLift.Children.Add(floorNumberTextBlock); // set TextBlock position Canvas.SetLeft(floorNumberTextBlock, 100); // X-position Canvas.SetTop(floorNumberTextBlock, i * FLOOR_HEIGHT - FLOOR_HEIGHT / 2); // Y-position } //TranslateTransform translateTransform = new TranslateTransform(0, 300); //ScaleTransform scaleTransform = new ScaleTransform(0, -1); //this.showLift.RenderTransform = scaleTransform; //this.showLift.RenderTransform = translateTransform; // -------------------------------------------------- // paint LIFT _liftRect = new Rectangle { Stroke = Brushes.Blue, StrokeThickness = 1, Width = LIFT_WIDTH, Height = LIFT_HEIGHT, Fill = new SolidColorBrush(Colors.White) }; // set lift position Canvas.SetLeft(_liftRect, 20); // X Canvas.SetTop(_liftRect, _currentLiftPositionY); // Y // add to parent control this.showLift.Children.Add(_liftRect); // -------------------------------------------------- // paint lift's DOOR _leftDoorRect = new Rectangle { Stroke = Brushes.Blue, StrokeThickness = 2, Fill = new SolidColorBrush(Colors.Blue), Width = 0, // LIFT_WIDTH Height = LIFT_HEIGHT }; // set door position Canvas.SetLeft(_leftDoorRect, 20); Canvas.SetTop(_leftDoorRect, _currentLiftPositionY); // add to parent control this.showLift.Children.Add(_leftDoorRect); // scroll ListView to bottom floorListView.SelectedItem = floorListView.Items[floorListView.Items.Count - 1]; floorListView.ScrollIntoView(floorListView.SelectedItem); floorsScrollViewer.ScrollToBottom(); }
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); } }