private void UpdateTrainArrivalAtUI(object sender, TrainProcessEventArgs e) { try { _synchronizationContext.Post((evnt) => { try { var evntargs = (evnt as TrainProcessEventArgs); if (evnt != null && tvwTracks != null) { foreach (TreeNode node in tvwTracks.Nodes[0].Nodes) { if (node.Text.Contains(evntargs.TrackId)) { if (node.Nodes.Count > 0) { node.Nodes.Clear(); } } } } tvwTracks.ExpandAll(); } catch (Exception exp) { MessageBox.Show(exp.Message); } }, e); } catch (Exception exp) { MessageBox.Show(exp.Message); } }
private void UpdateShipmentLoadedIntoTrainProgress(object sender, TrainProcessEventArgs e) { try { _synchronizationContext.Post((evnt) => { try { var evntargs = (evnt as TrainProcessEventArgs); if (evnt != null && tvwTrains != null) { foreach (TreeNode node in tvwTrains.Nodes[0].Nodes) { if (node.Text.Contains(evntargs.TrainId)) { TreeNode newChildNode = new TreeNode(evntargs.Message); node.Nodes.Add(newChildNode); } } } tvwTrains.ExpandAll(); } catch (Exception exp) { MessageBox.Show(exp.Message); } }, e); } catch (Exception exp) { MessageBox.Show(exp.Message); } }
public void OnTrainShipmentUnloadEvent(object sender, TrainProcessEventArgs args) { var trainShipmentUnloadEvent = TrainShipmentUnloadEvent; if (trainShipmentUnloadEvent != null) { trainShipmentUnloadEvent(sender, args); } }
public void OnTrainOtherEvent(object sender, TrainProcessEventArgs args) { var trainOtherEvent = TrainOtherEvent; if (trainOtherEvent != null) { trainOtherEvent(sender, args); } }
public void OnTrainArrivedAtStation(object sender, TrainProcessEventArgs args) { var trainArrivedAtStationEvent = TrainArrivedAtStationEvent; if (trainArrivedAtStationEvent != null) { trainArrivedAtStationEvent(sender, args); } }
private void UpdateTrainOtherEventsAtUI(object sender, TrainProcessEventArgs e) { try { _synchronizationContext.Post((evnt) => { try { var evntargs = (evnt as TrainProcessEventArgs); if (evnt != null && tvwTrains != null) { foreach (TreeNode node in tvwTrains.Nodes[0].Nodes) { if (node.Text.Contains(evntargs.TrainId)) { if (node.Nodes.Count > 0) { if (node.Nodes[0].Tag == null || string.IsNullOrEmpty(node.Nodes[0].Tag.ToString())) { node.Nodes[0].Text = evntargs.Message; } else { TreeNode newChildNode = new TreeNode(evntargs.Message); TreeNodeCollection nodes = node.Nodes; node.Nodes.Clear(); node.Nodes.Add(newChildNode); foreach (TreeNode existingNode in nodes) { node.Nodes.Add(existingNode); } } } else { TreeNode newChildNode = new TreeNode(evntargs.Message); node.Nodes.Add(newChildNode); } } } } tvwTrains.ExpandAll(); } catch (Exception exp) { MessageBox.Show(exp.Message); } }, e); } catch (Exception exp) { MessageBox.Show(exp.Message); } }
private void UpdateShipmentUnloadedFromTrainProgress(object sender, TrainProcessEventArgs e) { try { _synchronizationContext.Post((evnt) => { try { var evntargs = (evnt as TrainProcessEventArgs); if (evnt != null && tvwTrains != null) { foreach (TreeNode node in tvwTrains.Nodes[0].Nodes) { if (node.Text.Contains(evntargs.TrainId)) { if (node.Nodes.Count > 0) { TreeNode nodeToRemove = null; foreach (TreeNode shipmentNode in node.Nodes) { if (shipmentNode.Tag != null && shipmentNode.Tag.ToString().Equals(evntargs.DeliveredShipment.ShipmentIdentifier)) { nodeToRemove = shipmentNode; break; } } if (nodeToRemove != null) { node.Nodes.Remove(nodeToRemove); } } } } } tvwTrains.ExpandAll(); } catch (Exception exp) { MessageBox.Show(exp.Message); } }, e); } catch (Exception exp) { MessageBox.Show(exp.Message); } }
private void ProcessShipmentAtStation() { _signalEvent.WaitOne(); // Process Running Trains List <TrainCurrentLocation> lstToMovetoUnLoadingQueue = new List <TrainCurrentLocation>(); foreach (var train in _dicTrainCurrentLocations.Keys) { var trainCurrentLocation = _dicTrainCurrentLocations[train]; if (train.CurrentRunningStatus == TrainRunningStatus.Running && trainCurrentLocation.DistanceLeft <= 0) { lstToMovetoUnLoadingQueue.Add(trainCurrentLocation); } else if (train.CurrentRunningStatus == TrainRunningStatus.Running) { trainCurrentLocation.DistanceLeft -= train.SpeedFactor; if (trainCurrentLocation.DistanceLeft <= 0) { trainCurrentLocation.DistanceLeft = 0; } else { TrainProcessEventArgs args = new TrainProcessEventArgs(); args.Message = trainCurrentLocation.Train + " has to travel distance : " + trainCurrentLocation.DistanceLeft; args.TrainId = trainCurrentLocation.Train.ToString(); args.TrackId = trainCurrentLocation.Track.ToString(); OnTrainProgressed(this, args); } } } List <TrainCurrentLocation> lstToAddToLoadingTrainsQueue = new List <TrainCurrentLocation>(); // Unloading shipment foreach (var trainCurrentLocation in _lstOfTrainsForUnloading) { if (trainCurrentLocation.Train.IsAnyShipmentForStation(trainCurrentLocation.Track.DestinationStationId)) { var shipment = trainCurrentLocation.Train.UnloadShipment(trainCurrentLocation.Track.DestinationStationId); TrainProcessEventArgs args = new TrainProcessEventArgs(); args.Message = "<<" + shipment + " shipment unloaded at " + shipment.DestinationStation; args.TrainId = trainCurrentLocation.Train.ToString(); args.TrackId = trainCurrentLocation.Track.ToString(); args.DeliveredShipment = shipment; OnTrainShipmentUnloadEvent(this, args); } else { trainCurrentLocation.Train.CurrentRunningStatus = TrainRunningStatus.LoadingShipment; TrainProcessEventArgs args = new TrainProcessEventArgs(); args.Message = "There is no shipment to unload at Station" + trainCurrentLocation.Track.DestinationStationId; args.TrainId = trainCurrentLocation.Train.ToString(); args.TrackId = trainCurrentLocation.Track.ToString(); OnTrainOtherEvent(this, args); lstToAddToLoadingTrainsQueue.Add(trainCurrentLocation); } } List <TrainCurrentLocation> lstToAddToWaitingTrainsQueue = new List <TrainCurrentLocation>(); // loading shipment foreach (var trainCurrentLocation in _lstOfTrainsForLoading) { Station station = LookupCache.GetInstance().GetStation(trainCurrentLocation.Track.DestinationStationId); if (!station.IsStationShipmentEmpty() && !trainCurrentLocation.Train.IsShipmentCapacityFull()) { Shipment shipment = station.DeliverShipmentToTrain(); trainCurrentLocation.Train.LoadShipment(shipment); TrainProcessEventArgs args = new TrainProcessEventArgs(); args.Message = ">> " + shipment + " loaded from " + shipment.DestinationStation; args.TrainId = trainCurrentLocation.Train.ToString(); args.TrackId = trainCurrentLocation.Track.ToString(); args.DeliveredShipment = shipment; OnTrainShipmentLoadEvent(this, args); } else if (station.IsStationShipmentEmpty()) { TrainProcessEventArgs args = new TrainProcessEventArgs(); args.Message = station + " has no items to load into train"; args.TrainId = trainCurrentLocation.Train.ToString(); args.TrackId = trainCurrentLocation.Track.ToString(); OnTrainOtherEvent(this, args); lstToAddToWaitingTrainsQueue.Add(trainCurrentLocation); trainCurrentLocation.Train.CurrentRunningStatus = TrainRunningStatus.Stopped; } else if (trainCurrentLocation.Train.IsShipmentCapacityFull()) { TrainProcessEventArgs args = new TrainProcessEventArgs(); args.Message = "Train shipment capacity is full now."; args.TrainId = trainCurrentLocation.Train.ToString(); args.TrackId = trainCurrentLocation.Track.ToString(); OnTrainOtherEvent(this, args); lstToAddToWaitingTrainsQueue.Add(trainCurrentLocation); trainCurrentLocation.Train.CurrentRunningStatus = TrainRunningStatus.Stopped; } } List <TrainCurrentLocation> _listToRemoveFromWaiting = new List <TrainCurrentLocation>(); // check track token to start. foreach (var trainCurrentLocation in _lstTrainsToStart) { if (trainCurrentLocation.Train.CurrentRunningStatus == TrainRunningStatus.Stopped) { var track = LookupCache.GetInstance().GetNextTrack(trainCurrentLocation.Track.DestinationStationId); trainCurrentLocation.Track = track; trainCurrentLocation.DistanceLeft = trainCurrentLocation.Track.TrackLength; trainCurrentLocation.Train.CurrentRunningStatus = TrainRunningStatus.Waiting; } if (trainCurrentLocation.Train.CurrentRunningStatus == TrainRunningStatus.Waiting) { if (TrackTokenController.GetInstance().CheckTrackTokenAvailability(trainCurrentLocation.Track.SourceStationId, trainCurrentLocation.Track.DestinationStationId)) { TrackTokenController.GetInstance().AcquireTrackToken(trainCurrentLocation.Track.SourceStationId, trainCurrentLocation.Track.DestinationStationId, trainCurrentLocation.Train.TrainId); trainCurrentLocation.Train.CurrentRunningStatus = TrainRunningStatus.Running; _dicTrainCurrentLocations.Add(trainCurrentLocation.Train, trainCurrentLocation); TrainProcessEventArgs args = new TrainProcessEventArgs(); args.Message = "Train departured from Station" + trainCurrentLocation.Track.SourceStationId; args.TrainId = trainCurrentLocation.Train.ToString(); args.TrackId = trainCurrentLocation.Track.ToString(); OnTrainOtherEvent(this, args); _listToRemoveFromWaiting.Add(trainCurrentLocation); } else { TrainProcessEventArgs args = new TrainProcessEventArgs(); args.Message = "Train waiting for track " + trainCurrentLocation.Track + " to be free."; args.TrainId = trainCurrentLocation.Train.ToString(); args.TrackId = trainCurrentLocation.Track.ToString(); OnTrainOtherEvent(this, args); } } } // Remove from waiting list. foreach (var train in _listToRemoveFromWaiting) { if (_lstTrainsToStart.Contains(train)) { _lstTrainsToStart.Remove(train); } } // Move from running to unloading foreach (var trainCurrentLocation in lstToMovetoUnLoadingQueue) { if (_dicTrainCurrentLocations.ContainsKey(trainCurrentLocation.Train)) { TrackTokenController.GetInstance().ReleaseTrackToken(trainCurrentLocation.Track.SourceStationId, trainCurrentLocation.Track.DestinationStationId); _dicTrainCurrentLocations.Remove(trainCurrentLocation.Train); _lstOfTrainsForUnloading.Add(trainCurrentLocation); TrainProcessEventArgs args = new TrainProcessEventArgs(); args.TrainId = trainCurrentLocation.Train.ToString(); args.TrackId = trainCurrentLocation.Track.ToString(); OnTrainArrivedAtStation(this, args); } } // Move from unloading to loading foreach (var trainCurrentLocation in lstToAddToLoadingTrainsQueue) { if (_lstOfTrainsForUnloading.Contains(trainCurrentLocation)) { _lstOfTrainsForUnloading.Remove(trainCurrentLocation); _lstOfTrainsForLoading.Add(trainCurrentLocation); } } // Move from loading to waiting foreach (var trainCurrentLocation in lstToAddToWaitingTrainsQueue) { if (_lstOfTrainsForLoading.Contains(trainCurrentLocation)) { _lstOfTrainsForLoading.Remove(trainCurrentLocation); _lstTrainsToStart.Add(trainCurrentLocation); } } ProcessShipmentAtStation(); }