예제 #1
0
 /// <summary>
 /// The stop.
 /// </summary>
 /// <param name="monitor">
 /// The monitor.
 /// </param>
 internal void Stop(FareRequestMonitor monitor)
 {
     this._monitors.Clear(monitor.GetType());
 }
예제 #2
0
        /// <summary>
        /// Create new monitor for requests depending on the operation mode
        /// </summary>
        /// <param name="requests">
        /// The requests.
        /// </param>
        /// <param name="operationMode">
        /// The operation Mode.
        /// </param>
        /// <returns>
        /// The <see cref="FareRequestMonitor"/>.
        /// </returns>
        internal FareRequestMonitor CreateMonitor(List<FareMonitorRequest> requests, OperationMode operationMode)
        {
            if (requests == null || requests.Count < 1)
            {
                return null;
            }

            FareRequestMonitor newMon = null;
            FareMonitorEvents monitorEvents = null;
            if (operationMode == OperationMode.LiveMonitor)
            {
                newMon = new LiveFareMonitor(this._liveFareStorage, new TaskbarFlightNotifier(), WebFareBrowserControlFactory.Instance);
                monitorEvents = this.Events[OperationMode.LiveMonitor];
            }
            else
            {
                if (operationMode == OperationMode.ShowFare)
                {
                    newMon = new FareRequestMonitor(WebFareBrowserControlFactory.Instance);
                    monitorEvents = this.Events[OperationMode.ShowFare];
                }
                else if (operationMode == OperationMode.GetFareAndSave)
                {
                    newMon = new FareExportMonitor(
                        AppContext.MonitorEnvironment.ArchiveManager,
                        WebFareBrowserControlFactory.Instance,
                        this.View.AutoSync);
                    monitorEvents = this.Events[OperationMode.GetFareAndSave];
                }
                else
                {
                    throw new ApplicationException("Unsupported opearation mode: " + operationMode);
                }
            }

            monitorEvents.Attach(newMon);
            newMon.Enqueue(requests);

            return newMon;
        }
예제 #3
0
        /// <summary>
        /// The fare export monitor_ on request completed.
        /// </summary>
        /// <param name="sender">
        /// The sender.
        /// </param>
        /// <param name="args">
        /// The args.
        /// </param>
        private void FareExportMonitor_OnRequestCompleted(FareRequestMonitor sender, FareBrowserRequestArg args)
        {
            var browser = args.Request.BrowserControl;
            if (browser == null)
            {
                this._logger.Warn("There is no browser control associated with the request");
                return;
            }

            TravelRoute route = browser.LastRetrievedRoute;
            lock (this._lockObj)
            {
                var pendingCount = sender.PendingRequests.Count;
                var processCount = sender.ProcessingRequests.Count;
                this._logger.DebugFormat("There are {0} pending requests, {1} processing requests", pendingCount, processCount);
                bool isLastRequest = pendingCount == 0 && processCount == 0;
                if (browser.RequestState == DataRequestState.Ok && route != null && route.Journeys.Count > 0)
                {
                    this._cachedRoutes.Add(route); // Cache journeys

                    if (this._cachedRoutes.Count >= CACHE_AMOUNT || isLastRequest)
                    {
                        // Export journeys to file to reduce memory load
                        this.FlushCache();
                    }
                }

                if (isLastRequest)
                {
                    this._logger.Debug("There is no more active request for the monitor");
                    this.FinalizeData();
                }
            }
        }
예제 #4
0
 /// <summary>
 /// The start.
 /// </summary>
 /// <param name="monitor">
 /// The monitor.
 /// </param>
 internal void Start(FareRequestMonitor monitor)
 {
     this._monitors.Start(monitor);
 }
 /// <summary>
 /// The start.
 /// </summary>
 /// <param name="monitor">
 /// The monitor.
 /// </param>
 public void Start(FareRequestMonitor monitor)
 {
     monitor.Start();
     this._startedMonitors.Enqueue(monitor);
 }
예제 #6
0
        /// <summary>
        /// The live fare monitor_ on request completed.
        /// </summary>
        /// <param name="sender">
        /// The sender.
        /// </param>
        /// <param name="args">
        /// The args.
        /// </param>
        private void LiveFareMonitor_OnRequestCompleted(FareRequestMonitor sender, FareBrowserRequestArg args)
        {
            var request = args.Request;
            var browser = args.Request.BrowserControl;
            if (browser == null)
            {
                return;
            }

            lock (this._notifier)
            {
                try
                {
                    string travelPeriodStr = StringUtil.GetPeriodString(request.DepartureDate, request.ReturnDate);
                    if (browser.RequestState == DataRequestState.NoData)
                    {
                        string header = travelPeriodStr + Environment.NewLine + "From: " + request.Departure + Environment.NewLine + "To: "
                                        + request.Destination;
                        this._notifier.Show(
                            "No AirportData",
                            "There is no flight data for this travel period" + Environment.NewLine + header,
                            null,
                            7000,
                            NotificationType.Warning,
                            true);
                        return;
                    }

                    var route = browser.LastRetrievedRoute;
                    if (route == null || route.Journeys.Count < 1 || route.Journeys[0].Data.Count < 1)
                    {
                        return;
                    }

                    var curJourney = route.Journeys[0];

                    // Live Monitor: Store the current journey data
                    Journey oldJourney = this._currentMonitorJourneys.FirstOrDefault(j => j.IsSameTrip(curJourney));
                    var flightData = curJourney.Data[0].Flights;
                    var flightItems = new FlightDisplayItemsCollection();

                    if (oldJourney == null)
                    {
                        flightItems.AddRange(flightData, FlightStatus.New);
                    }
                    else
                    {
                        var oldFlights = oldJourney.Data[0].Flights;

                        // Compare each flight
                        foreach (Flight currentFlight in flightData)
                        {
                            Flight comparableFlight = oldFlights.FirstOrDefault(f => f.IsSameFlight(currentFlight));
                            if (comparableFlight == null)
                            {
                                // New flight was found (or first appearance)
                                flightItems.Add(currentFlight, FlightStatus.New, 0);
                            }
                            else
                            {
                                double priceDiff = currentFlight.Price - comparableFlight.Price;
                                if (Math.Abs(priceDiff) > 1)
                                {
                                    // Minimum price change is 1 EUR
                                    if ((priceDiff > 0 && comparableFlight.Price < this.PriceLimit)

                                        // If price was increased and old price is still within price limit
                                        || currentFlight.Price <= this.PriceLimit)
                                    {
                                        // or prices has been decreased enough to the limit
                                        flightItems.Add(
                                            currentFlight,
                                            priceDiff > 0 ? FlightStatus.PriceIncreased : FlightStatus.PriceDecreased,
                                            comparableFlight.Price);
                                    }
                                }
                            }
                        }
                    }

                    // There are changes in the list of flights
                    if (flightItems.Count > 0)
                    {
                        if (this._fareStorage != null)
                        {
                            try
                            {
                                this._fareStorage.SaveLiveFare(flightItems.Select(i => i.FlightData));
                            }
                            catch (Exception ex)
                            {
                                AppContext.Logger.ErrorFormat("Could not save live data [{0}]: {1}", travelPeriodStr, ex);
                                this._notifier.Show(
                                    travelPeriodStr,
                                    "Could not save live data: " + ex.Message,
                                    null,
                                    5000,
                                    NotificationType.Error,
                                    true);
                            }
                        }

                        string currencyCode = curJourney.Data[0].Currency;
                        string currencySymbol = AppContext.MonitorEnvironment.CurrencyProvider.GetCurrencyInfo(currencyCode).Symbol;
                        string header = travelPeriodStr + " (Currency: " + currencyCode
                                        + (currencySymbol == currencyCode ? null : " - " + currencySymbol) + ")" + Environment.NewLine + "From: "
                                        + request.Departure + Environment.NewLine + "To: " + request.Destination;
                        this._notifier.Show("Fare data was updated", header, flightItems, 5000, NotificationType.Info, true);
                    }

                    if (oldJourney != null)
                    {
                        this._currentMonitorJourneys.Remove(oldJourney);
                    }

                    this._currentMonitorJourneys.Add(curJourney);
                }
                finally
                {
                    // Repeat the process
                    if (this.State == MonitorState.Running && browser.RequestState != DataRequestState.NoData)
                    {
                        args.Request.Reset();
            #if !DEBUG // Put some delay if we are not running DEBUG mode
                    var interval = TimeSpan.FromMinutes(1 + PendingRequests.Count);
                    System.Threading.Thread.Sleep(interval);
            #endif
                        this.Enqueue(args.Request);
                    }
                }
            }
        }
예제 #7
0
        /// <summary>
        /// The fare monitor_ request completed.
        /// </summary>
        /// <param name="sender">
        /// The sender.
        /// </param>
        /// <param name="args">
        /// The args.
        /// </param>
        private void FareMonitor_RequestCompleted(FareRequestMonitor sender, FareBrowserRequestArg args)
        {
            // This method should be triggered both before stopping the request or after the request is completed
            var browser = args.Request.BrowserControl; // The browser might be nulled
            this.InvokeActionIfNeeded(
                new Action(
                    () =>
                        {
                            SwitchButton senderBtn = null;
                            var monType = sender.GetType();
                            var resultRoute = browser == null ? null : browser.LastRetrievedRoute;

                            if (monType == typeof(FareRequestMonitor))
                            {
                                // Browser can be nulled if the request was aborted before it is event started
                                if (browser != null && resultRoute != null && !this.btnSummary.Enabled)
                                {
                                    this.SetDataProcessor(true);
                                }

                                senderBtn = this.btnShowFare;
                            }
                            else if (monType == typeof(FareExportMonitor))
                            {
                                senderBtn = this.btnGetFareAndSave;
                            }

                            this.IncreaseProgress();
                            this.CheckProgress();

                            if (!this.loadProgress.Visible && senderBtn != null)
                            {
                                var senderMonitor = senderBtn.Tag as FareRequestMonitor;
                                if (senderMonitor == sender && senderBtn.IsSecondState)
                                {
                                    senderBtn.IsSecondState = false; // Reset the button after everything is done!
                                }
                            }
                        }));
        }
예제 #8
0
        /// <summary>
        /// The fare monitor_ request starting.
        /// </summary>
        /// <param name="sender">
        /// The sender.
        /// </param>
        /// <param name="args">
        /// The args.
        /// </param>
        private void FareMonitor_RequestStarting(FareRequestMonitor sender, FareBrowserRequestArg args)
        {
            this.InvokeActionIfNeeded(
                new Action(
                    () =>
                        {
                            FareMonitorRequest request = args.Request;
                            request.Initialize(); // Initialize request here: So that the control is created on the UI thread
                            var browserTabPage =
                                new TabPage(
                                    request.DepartureDate.ToString(NamingRule.DATE_FORMAT)
                                    + (request.IsRoundTrip ? " - " + request.ReturnDate.ToString(NamingRule.DATE_FORMAT) : string.Empty));

                            var browser = request.BrowserControl as Control;
                            if (browser != null)
                            {
                                browser.Dock = DockStyle.Fill;
                                browserTabPage.Controls.Add(browser);
                                browserTabPage.BackColor = this.BrowserStartingColor;
                                browserTabPage.ImageIndex = 0;
                                browserTabPage.Tag = request;
                                browserTabPage.ToolTipText = request.Detail;
                                this.fareBrowserTabs.TabPages.Add(browserTabPage);
                            }

                            this.SetStatus(request);
                        }));
        }
예제 #9
0
        /// <summary>
        /// The close and export_ request completed.
        /// </summary>
        /// <param name="sender">
        /// The sender.
        /// </param>
        /// <param name="args">
        /// The args.
        /// </param>
        private void CloseAndExport_RequestCompleted(FareRequestMonitor sender, FareBrowserRequestArg args)
        {
            this.SafeInvoke(
                new Action(
                    () =>
                        {
                            this.FareMonitor_RequestCompleted(sender, args);
                            var browserControl = args.Request.BrowserControl as WebFareBrowserControl;
                            var tabPageObj = browserControl.Parent as TabPage;
                            if (tabPageObj != null)
                            {
                                this.fareBrowserTabs.TabPages.Remove(tabPageObj);
                                tabPageObj.Dispose();
                            }

                            if (sender.State == MonitorState.Stopped && this.chkExitAfterDoneStrip.ControlItem.Checked)
                            {
                                Environment.Exit(0);
                            }
                        }));
        }
예제 #10
0
        /// <summary>
        /// The show fare_ request completed.
        /// </summary>
        /// <param name="sender">
        /// The sender.
        /// </param>
        /// <param name="args">
        /// The args.
        /// </param>
        private void ShowFare_RequestCompleted(FareRequestMonitor sender, FareBrowserRequestArg args)
        {
            this.InvokeActionIfNeeded(
                new Action(
                    () =>
                        {
                            var fareBrowserObj = args.Request.BrowserControl as WebFareBrowserControl;
                            if (fareBrowserObj.IsDestructed() || fareBrowserObj.LastRequestInitiatedDate != args.RequestInitiatedDate
                                || fareBrowserObj.RequestState == DataRequestState.Pending)
                            {
                                return;
                            }

                            var browserTabPage = fareBrowserObj.Parent as TabPage;
                            if (browserTabPage == null)
                            {
                                return;
                            }

                            if (fareBrowserObj.RequestState == DataRequestState.Pending || fareBrowserObj.RequestState == DataRequestState.Requested)
                            {
                                browserTabPage.BackColor = this.BrowserStartingColor;
                            }
                            else if (fareBrowserObj.RequestState == DataRequestState.Ok)
                            {
                                browserTabPage.BackColor = this.BrowserSuccessColor;
                            }
                            else
                            {
                                browserTabPage.BackColor = this.BrowserFailedColor;
                            }

                            if (this.chkAutoFocusTabStrip.ControlItem.Checked)
                            {
                                this.fareBrowserTabs.SelectedTab = browserTabPage;
                            }

                            this.FareMonitor_RequestCompleted(sender, args);
                        }));
        }
예제 #11
0
        /// <summary>
        /// The on monitor starting.
        /// </summary>
        /// <param name="monitor">
        /// The monitor.
        /// </param>
        private void OnMonitorStarting(FareRequestMonitor monitor)
        {
            this.SetScanner(false);
            this.loadProgress.ControlItem.Value = this.loadProgress.ControlItem.Maximum = 0;
            this.loadProgress.ControlItem.ShowInTaskbar = false;
            if (monitor.Mode == OperationMode.ShowFare)
            {
                this.SetDataProcessor(false);
            }

            if (monitor.Mode == OperationMode.LiveMonitor)
            {
                this.trayIcon.Visible = true;
            }
            else
            {
                this.ClearBrowserTabs();
                this.loadProgress.ControlItem.Maximum = monitor.PendingRequests.Count;
                this.loadProgress.ControlItem.Style = monitor.PendingRequests.Count == 1 ? ProgressBarStyle.Marquee : ProgressBarStyle.Continuous;
                this.loadProgress.Visible = this.loadProgress.ControlItem.ShowInTaskbar = true;
            }

            this.SetScanner(true);
        }
예제 #12
0
 /// <summary>
 /// The live fare_ request starting.
 /// </summary>
 /// <param name="sender">
 /// The sender.
 /// </param>
 /// <param name="args">
 /// The args.
 /// </param>
 private void LiveFare_RequestStarting(FareRequestMonitor sender, FareBrowserRequestArg args)
 {
     args.Request.Initialize();
     var monitor = sender as LiveFareMonitor;
     monitor.PriceLimit = (double)this.numPriceLimit.Value;
 }
예제 #13
0
 /// <summary>
 /// The live fare monitor stopping.
 /// </summary>
 /// <param name="sender">
 /// The sender.
 /// </param>
 private void LiveFareMonitorStopping(FareRequestMonitor sender)
 {
     this.btnLiveMonitor.IsSecondState = false;
 }