コード例 #1
0
        private void _timer_Tick(object sender, EventArgs e)
        {
            if (_trackingService != null)
            {
                // we simulate tracking events by selecting a RANDOM ship
                // next tracking type is set to Arrival or Departure depending on the
                // current location of the selected ship
                // if port of selected ship == "AT SEA" then we set the tracking event type
                // as an ARRIVAL and will set ship port to the next port (!= 0) in the Port list
                // if port of selected ship != "AT SEA" then we set the tracking event type
                // as a DEPARTURE and set port to "AT SEA"

                int maxShip = _trackingService.Ships.Count;

                // select a random ship in the list
                _selectedShipId = _randomShip.Next(1, maxShip);

                // set tracked ship to the current selected id
                _trackingService.TrackedShip = _trackingService.Ships[_selectedShipId];

                // set the tracking event type (Arrival or Departure) depening on the current location (PortId 0 is AT-SEA)
                _trackingService.TrackingType = _trackingService.TrackedShip.Location.PortId == 0
                    ? TrackingType.Arrival
                    : TrackingType.Departure;

                // set the time of tracking recording
                _trackingService.Recorded = DateTime.Now;

                // create a unique id for the tracking
                _trackingService.TrackingServiceId = Guid.NewGuid();

                // set the new port of the tracking, this is a random port in
                // the list in case of arrival
                // or port 0 = AT SEA in case of departure
                if (_trackingService.TrackingType == TrackingType.Arrival)
                {
                    int maxPort = _trackingService.Ports.Count;
                    _selectedPortId = _randomPort.Next(1, maxPort);
                }
                else
                {
                    _selectedPortId = 0;
                }
                _trackingService.SetPort = _trackingService.Ports[_selectedPortId];

                // handle the tracking by the tracking service
                // the tracking service will now send an Arrival event or Departure event to the Ship
                _trackingService.RecordTracking(_eventProcessor);

                // augment number of events
                _numberOfEventsCount.Text = _eventProcessor.CountEventLogEntries().ToString();

                // refresh the UI
                this.SetDataSource();
            }
        }