/// <summary> /// Execute command. /// </summary> /// <param name="args"> /// Command args. /// Routes to generate shapes. /// </param> protected override void _Execute(params object[] args) { try { List <Route> routesWithoutGeometry = args[0] as List <Route>; Debug.Assert(routesWithoutGeometry != null); _DoGenerateRoutesShapes(routesWithoutGeometry); } catch (RouteException e) { if (e.InvalidObjects != null) // if exception throw because any Routes or Orders are invalid { _ShowSolveValidationResult(e.InvalidObjects); } else { App.Current.Messenger.AddError(RoutingCmdHelpers.FormatRoutingExceptionMsg(e)); } } catch (Exception e) { Logger.Error(e); if ((e is LicenseException) || (e is AuthenticationException) || (e is CommunicationException)) { CommonHelpers.AddRoutingErrorMessage(e); } else { throw; } } }
/// <summary> /// Starts operation process. /// </summary> /// <param name="args">Operation args.</param> /// <exception cref="Exception">Throws if any unhandles exception occurs in method.</exception> protected override void _Execute(params object[] args) { try { // Get current schedule. if (_schedulePage == null) { _schedulePage = (OptimizeAndEditPage)App.Current.MainWindow.GetPage(PagePaths.SchedulePagePath); } Schedule schedule = _schedulePage.CurrentSchedule; ICollection <Order> selectedOrders = _GetOrdersWhichCanBeUnassignedFromSelection(_schedulePage.SelectedItems); ICollection <Order> orders = RoutingCmdHelpers.GetOrdersIncludingPairs(schedule, selectedOrders); ICollection <Route> routes = ViolationsHelper.GetRouteForUnassignOrders(schedule, orders); if (_CheckRoutingParams(schedule, routes, orders)) { SolveOptions options = new SolveOptions(); options.GenerateDirections = App.Current.MapDisplay.TrueRoute; options.FailOnInvalidOrderGeoLocation = false; _SetOperationStartedStatus((string)App.Current.FindResource(UNASSIGN_ORDERS), (DateTime)schedule.PlannedDate); OperationsIds.Add(App.Current.Solver.UnassignOrdersAsync(schedule, orders, options)); // set solve started message string infoMessage = RoutingMessagesHelper.GetUnassignOperationStartedMessage(orders); if (!string.IsNullOrEmpty(infoMessage)) { App.Current.Messenger.AddInfo(infoMessage); } } } catch (RouteException e) { if (e.InvalidObjects != null) // if exception throw because any Routes or Orders are invalid { _ShowSolveValidationResult(e.InvalidObjects); } else { _ShowErrorMsg(RoutingCmdHelpers.FormatRoutingExceptionMsg(e)); } } catch (Exception e) { Logger.Error(e); if ((e is LicenseException) || (e is AuthenticationException) || (e is CommunicationException)) { CommonHelpers.AddRoutingErrorMessage(e); } else { throw; } } }
/// <summary> /// Handle contains logic to save changes, start new operation or process errors when solve conpleted. /// </summary> /// <param name="sender">Solver.</param> /// <param name="e">Solve completed event args.</param> private void Solver_AsyncSolveCompleted(object sender, AsyncSolveCompletedEventArgs e) { // If event came from any else solve operation - exit. if (e.OperationId != _currentOperationId) { return; } AsyncOperationInfo info = null; App.Current.Solver.GetAsyncOperationInfo(e.OperationId, out info); // Get operation info. // If operation complete successful. if (e.Error == null && !e.Cancelled && !e.Result.IsFailed) { Schedule changedSchedule = info.Schedule; _ProcessSaveSchedule(changedSchedule, info); // Save edited schedule. _SetScheduleProcessed(changedSchedule); // Set schedule Processed to "true". App.Current.Messenger.AddInfo(_FormatSuccessSolveCompletedMsg(changedSchedule, info)); // Add info message. _NotifyScheduleChanged(changedSchedule); UnassignScheduleInfo nextInfo = null; if (_GetNextScheduleToUnassign(out nextInfo)) { _StartNextScheduleUnassigning(nextInfo); return; } _ProcessOrders(_args); // Call abstract method _ProcessOrders overrided in child command (to move orders to other date or delete them). } else if (e.Error != null) // If Error occured during operation. { Logger.Error(e.Error); CommonHelpers.AddRoutingErrorMessage(e.Error); if (e.Result != null) // Result is "null" when connection failed. { // Create violations collection. ICollection <MessageDetail> details = _GetErrorFailedDetails(info, e.Result); _ShowFailedMessage(e.Error, info.Schedule, details); // Show failed message. } } else if (e.Cancelled) // If operation was cancelled. { App.Current.Messenger.AddInfo(_FormatCancelMsg(info.Schedule)); } else if (e.Result.IsFailed) // If operation's failed. { // Create violations collection. ICollection <MessageDetail> details = _GetErrorFailedDetails(info, e.Result); _ShowFailedMessage(e.Error, info.Schedule, details); } _UpdateOptimizeAndEditPageSchedules(); // Update optimize and edit page content. _CleanUp(); _UnlockUI(); }
/// <summary> /// Method unassignes orders from schedule /// </summary> /// <param name="orders"></param> /// <param name="schedule"></param> private void _UnassignOrdersFromSchedule(Schedule schedule, ICollection <Order> orders) { try { ICollection <Order> ordersWithPairs = RoutingCmdHelpers.GetOrdersIncludingPairs(schedule, orders); // Create routes collection. ICollection <Route> routes = ViolationsHelper.GetRouteForUnassignOrders(schedule, ordersWithPairs); if (_CheckRoutingParams(schedule, ordersWithPairs, routes)) { SolveOptions options = new SolveOptions(); options.GenerateDirections = App.Current.MapDisplay.TrueRoute; options.FailOnInvalidOrderGeoLocation = false; string infoMessage = _FormatSuccessUnassigningStartedMessage(schedule, _schedulesToUnassign); // Set operation info status. _SetOperationStartedStatus(infoMessage, (DateTime)schedule.PlannedDate); // Start solve operation. App.Current.Solver.UnassignOrdersAsync(schedule, ordersWithPairs, options); } else // If routing operation was not started - clean collections and unlock UI. { _CleanUp(); _UnlockUI(); } } catch (RouteException e) { App.Current.Messenger.AddError(RoutingCmdHelpers.FormatRoutingExceptionMsg(e)); App.Current.Messenger.AddError(string.Format(OperationIsFailedMessage, schedule)); // Save already edited schedules. _UpdateOptimizeAndEditPageSchedules(); _CleanUp(); _UnlockUI(); } catch (Exception ex) { // Save already edited schedules. _UpdateOptimizeAndEditPageSchedules(); _CleanUp(); _UnlockUI(); if ((ex is LicenseException) || (ex is AuthenticationException) || (ex is CommunicationException)) { CommonHelpers.AddRoutingErrorMessage(ex); } else { throw; } } }
/// <summary> /// Handles exception thrown from a solve operation. /// </summary> /// <param name="exception">The exception thrown from a solve operation.</param> private void _HandleSolveError(Exception exception) { Debug.Assert(exception != null); var routeException = exception as RouteException; if (routeException != null) { if (routeException.InvalidObjects != null) { // exception was thrown because any Routes or Orders are invalid. _ShowSolveValidationResult(routeException.InvalidObjects); } else { _ShowErrorMsg(RoutingCmdHelpers.FormatRoutingExceptionMsg(routeException)); } return; } // If we have error in sync response - show it. var restException = exception as RestException; if (restException != null) { var details = new List <MessageDetail>(); // If exception has details- process them. if (restException.Details != null) { foreach (var detail in restException.Details) { var detailMessage = GuidsReplacer.ReplaceGuids(detail, App.Current.Project); details.Add(new MessageDetail(MessageType.Error, detailMessage)); } } // Process exception message. var message = GuidsReplacer.ReplaceGuids(exception.Message, App.Current.Project); // Show error in messenger. App.Current.Messenger.AddError(message, details); return; } Logger.Error(exception); CommonHelpers.AddRoutingErrorMessage(exception); }
/// <summary> /// React on solve operation finished. /// </summary> /// <param name="sender">Ignored.</param> /// <param name="e">Solve operation event args.</param> private void _AsyncSolveCompleted(object sender, AsyncSolveCompletedEventArgs e) { // Check that finished operation ID equals to current. if (!_operationID.Equals(e.OperationId)) { return; } App.Current.Solver.AsyncSolveCompleted -= _AsyncSolveCompleted; if (e.Cancelled) { App.Current.Messenger.AddInfo( string.Format((string)App.Current.FindResource("GenerateRouteShapesCancelledText"), App.Current.CurrentDate.ToShortDateString())); App.Current.MapDisplay.TrueRoute = false; } else if (e.Error != null) { Debug.Assert(e.Error != null); Logger.Error(e.Error); CommonHelpers.AddRoutingErrorMessage(e.Error); App.Current.MapDisplay.TrueRoute = false; } else { App.Current.Messenger.AddInfo( string.Format((string)App.Current.FindResource("GenerateRouteShapesCompletedText"), App.Current.CurrentDate.ToShortDateString())); App.Current.Project.Save(); // Set "Follow streets option". Set it to false first for notifying all route graphics. App.Current.MapDisplay.TrueRoute = false; App.Current.MapDisplay.TrueRoute = true; } _ReturnUIToDefaultState(); }
/// <summary> /// Do routes shapes generation. /// </summary> /// <param name="routesWithoutGeometry">Route to generate geometry.</param> private void _DoGenerateRoutesShapes(List <Route> routesWithoutGeometry) { Debug.Assert(routesWithoutGeometry != null); Debug.Assert(routesWithoutGeometry.Count > 0); // Set routing status. Route route = routesWithoutGeometry[0]; DateTime date = route.Schedule.PlannedDate.Value; OptimizeAndEditPage optimizeAndEditPage = (OptimizeAndEditPage)App.Current.MainWindow.GetPage(PagePaths.SchedulePagePath); optimizeAndEditPage.SetRoutingStatus((string)App.Current.FindResource("GenerateRouteShapes"), date); // Add routing message to messenger. string message = string.Format((string)App.Current.FindResource("GenerateRouteShapesStartText"), date.ToShortDateString()); App.Current.Messenger.AddInfo(message); // Lock all application UI. App.Current.UIManager.Lock(true); // Subscribe to routing command completed. App.Current.Solver.AsyncSolveCompleted += _AsyncSolveCompleted; try { // Save operation ID. _operationID = App.Current.Solver.GenerateDirectionsAsync(routesWithoutGeometry); } catch (Exception e) { App.Current.Solver.AsyncSolveCompleted -= _AsyncSolveCompleted; Logger.Error(e); CommonHelpers.AddRoutingErrorMessage(e); _ReturnUIToDefaultState(); } }
/// <summary> /// Excecutes command. /// </summary> /// <param name="args"></param> protected override void _Execute(params object[] args) { try { OptimizeAndEditPage schedulePage = (OptimizeAndEditPage)App.Current.MainWindow.GetPage(PagePaths.SchedulePagePath); Schedule schedule = schedulePage.CurrentSchedule; // Get selected orders. Collection <Order> selectedOrders = _GetOrdersWhichCanBeMovedFromSelection(schedulePage.SelectedItems); selectedOrders = RoutingCmdHelpers.GetOrdersIncludingPairs(schedule, selectedOrders) as Collection <Order>; bool keepViolOrdrsUnassigned = false; Debug.Assert(args[0] != null); ICollection <Route> targetRoutes = args[0] as ICollection <Route>; Debug.Assert(targetRoutes != null); string routeName = args[1] as string; if (_CheckRoutingParams(schedule, targetRoutes, selectedOrders)) { SolveOptions options = new SolveOptions(); options.GenerateDirections = App.Current.MapDisplay.TrueRoute; options.FailOnInvalidOrderGeoLocation = false; _SetOperationStartedStatus((string)App.Current.FindResource(ASSIGN_ORDERS), (DateTime)schedule.PlannedDate); // Start "Assign to best other route" operation. OperationsIds.Add(App.Current.Solver.AssignOrdersAsync(schedule, selectedOrders, targetRoutes, null, keepViolOrdrsUnassigned, options)); // Set solve started message string infoMessage = RoutingMessagesHelper.GetAssignOperationStartedMessage(selectedOrders, routeName); if (!string.IsNullOrEmpty(infoMessage)) { App.Current.Messenger.AddInfo(infoMessage); } } } catch (RouteException e) { if (e.InvalidObjects != null) // If exception throw because any Routes or Orders are invalid { _ShowSolveValidationResult(e.InvalidObjects); } else { _ShowErrorMsg(RoutingCmdHelpers.FormatRoutingExceptionMsg(e)); } } catch (Exception e) { Logger.Error(e); if ((e is LicenseException) || (e is AuthenticationException) || (e is CommunicationException)) { CommonHelpers.AddRoutingErrorMessage(e); } else { throw; } } }
protected override void _Execute(params object[] args) { try { ICollection <Route> routes = _GetRoutesFromSelection(_schedulePage.SelectedItems); // get unlocked routes from selected routes // and all orders assigned to converting routes List <Order> orders = new List <Order>(); foreach (Route route in routes) { if (!route.IsLocked) { // get assigned orders List <Order> routeOrders = new List <Order>(); foreach (Stop stop in route.Stops) { if (stop.StopType == StopType.Order) { routeOrders.Add(stop.AssociatedObject as Order); } } orders.AddRange(routeOrders); } } // get current schedule Schedule schedule = _schedulePage.CurrentSchedule; if (_CheckRoutingParams(schedule, routes, orders)) { SolveOptions options = new SolveOptions(); options.GenerateDirections = App.Current.MapDisplay.TrueRoute; options.FailOnInvalidOrderGeoLocation = false; _SetOperationStartedStatus((string)App.Current.FindResource("SequenceRoutes"), (DateTime)schedule.PlannedDate); OperationsIds.Add(App.Current.Solver.SequenceRoutesAsync(schedule, routes, options)); // set solve started message string infoMessage = _FormatSuccesSolveStartedMsg(schedule, routes.Count); if (!string.IsNullOrEmpty(infoMessage)) { App.Current.Messenger.AddInfo(infoMessage); } } } catch (RouteException e) { if (e.InvalidObjects != null) // if exception throw because any Routes or Orders are invalid { _ShowSolveValidationResult(e.InvalidObjects); } else { _ShowErrorMsg(RoutingCmdHelpers.FormatRoutingExceptionMsg(e)); } } catch (Exception e) { Logger.Error(e); if ((e is LicenseException) || (e is AuthenticationException) || (e is CommunicationException)) { CommonHelpers.AddRoutingErrorMessage(e); } else { throw; } } }
protected override void _Execute(params object[] args) { AsyncOperationInfo info = null; // Check whether operation can be started. if (!_CanBuildRoutesBeStarted(out info)) // If BuildRoutes operation cannot be started - show warning and return. { Debug.Assert(info != null); // Must be defined in _CanBuildRoutesBeStarted method. App.Current.MainWindow.MessageWindow.AddWarning( string.Format((string)App.Current.FindResource(ALREADY_BUILDING_ROUTES_MESSAGE_RESOURCE), info.Schedule.PlannedDate.Value.ToShortDateString())); return; } try { Schedule schedule = _optimizeAndEditPage.CurrentSchedule; var inputParams = _GetBuildRoutesParameters(schedule); var routes = inputParams.TargetRoutes; var orders = inputParams.OrdersToAssign; if (_CheckRoutingParams(schedule, routes, orders)) { SolveOptions options = new SolveOptions(); options.GenerateDirections = App.Current.MapDisplay.TrueRoute; options.FailOnInvalidOrderGeoLocation = false; _SetOperationStartedStatus((string)App.Current.FindResource(BUILD_ROUTES_STRING), (DateTime)schedule.PlannedDate); OperationsIds.Add(App.Current.Solver.BuildRoutesAsync( schedule, options, inputParams)); // set solve started message string infoMessage = _FormatSuccesSolveStartedMsg(schedule, inputParams); if (!string.IsNullOrEmpty(infoMessage)) { App.Current.Messenger.AddInfo(infoMessage); } } } catch (RouteException e) { if (e.InvalidObjects != null) // if exception throw because any Routes or Orders are invalid { _ShowSolveValidationResult(e.InvalidObjects); } else { _ShowErrorMsg(RoutingCmdHelpers.FormatRoutingExceptionMsg(e)); } } catch (Exception e) { Logger.Error(e); if ((e is LicenseException) || (e is AuthenticationException) || (e is CommunicationException)) { CommonHelpers.AddRoutingErrorMessage(e); } else { throw; } } }
/// <summary> /// Starts command executing. /// </summary> /// <param name="args">Command args.</param> protected override void _Execute(params object[] args) { // Get OptimizeAndEdit page. OptimizeAndEditPage schedulePage = ((MainWindow)App.Current.MainWindow).GetPage(PagePaths.SchedulePagePath) as OptimizeAndEditPage; Debug.Assert(schedulePage != null); // If editing is in progress - try to save editing. if (schedulePage.IsEditingInProgress) { // If editing cannot be saved - cancel editing. if (!schedulePage.SaveEditedItem()) { schedulePage.CancelObjectEditing(); } } // Orders to assign. Collection <Order> ordersToAssign = (Collection <Order>)args[0]; Debug.Assert(ordersToAssign != null); // Target object. Object target = (Object)args[1]; // Include paired orders unless we are moving one order within the same route bool movingSingleOrderInPair = false; if (ordersToAssign.Count == 1 && (target is Stop)) { Route targetRoute = (target as Stop).Route; Order orderToAssign = ordersToAssign.ElementAt(0); foreach (Stop stop in targetRoute.Stops) { Order associatedOrder = stop.AssociatedObject as Order; if (associatedOrder == null) { continue; } if (associatedOrder == orderToAssign) { movingSingleOrderInPair = true; break; } } } if (!movingSingleOrderInPair) { ordersToAssign = RoutingCmdHelpers.GetOrdersIncludingPairs(schedulePage.CurrentSchedule, ordersToAssign) as Collection <Order>; } int?sequence = null; if (target is Stop) { // Set sequence only if there is a single order to assign. if (ordersToAssign.Count == 1) { sequence = (target as Stop).SequenceNumber; } else // Otherwise change target to route. For more details see CR161083. { target = (target as Stop).Route; } } // Get target routes. ICollection <Route> targetRoutes = _CreateTargetRoutes(target); try { // If target routes count is 0 - orders must be unassigned. if (targetRoutes.Count == 0) { // Start unassigning orders. _UnassignOrders(ordersToAssign); } else { // Start moving orders. _MoveOrders(ordersToAssign, targetRoutes, sequence); } } catch (RouteException e) { if (e.InvalidObjects != null) // If exception throw because any Routes or Orders are invalid. { _ShowSolveValidationResult(e.InvalidObjects); } else { _ShowErrorMsg(RoutingCmdHelpers.FormatRoutingExceptionMsg(e)); } } catch (Exception e) { Logger.Error(e); if ((e is LicenseException) || (e is AuthenticationException) || (e is CommunicationException)) { CommonHelpers.AddRoutingErrorMessage(e); } else { throw; } } }