Exemplo n.º 1
0
        private void CheckPrices(Object source, ElapsedEventArgs e)
        {
            if (!PriceChecker.CheckingPrices)
            {
                try
                {
                    List <AmazonWatchItem> itemList = Application.Current.Dispatcher.Invoke(() => ((MainWindow)Application.Current.MainWindow).GetWatchItems());
                    PriceChecker.CheckPrices(itemList);
                    CommonFunctions.UserSettings.SetNextScheduledPriceCheck(DateTime.Now.AddMilliseconds(this.MillisecondsUntilExecution));
                }
                catch (Exception ex)
                {
                    CommonFunctions.Log.Error("Error checking prices by scheduler", ex);
                }
            }
            else
            {
                try
                {
                    DisplayLogItem rescheduleLog = new DisplayLogItem();

                    //Manually was checking prices when scheduler tried to kicked off. Reschedule the scheduler to the settings time

                    switch (CommonFunctions.UserSettings.SchedulerOption)
                    {
                    case (Settings.ScheduleType.Hours):
                        rescheduleLog.LogText = DisplayLogItem.CreateLogText($"Price checker was already checking prices when the scheduler tried to begin.",
                                                                             $"Rescheduling for: {CommonFunctions.UserSettings.ScheduleTime} hours");
                        rescheduleLog.LogImage = DisplayLogItem.GetLogIcon(DisplayLogItem.LogIcon.Warning);
                        break;

                    case (Settings.ScheduleType.Days):
                        rescheduleLog.LogText = DisplayLogItem.CreateLogText($"Price checker was already checking prices when the scheduler tried to begin",
                                                                             $"Rescheduling for: {CommonFunctions.UserSettings.ScheduleTime} days");
                        rescheduleLog.LogImage = DisplayLogItem.GetLogIcon(DisplayLogItem.LogIcon.Warning);
                        break;
                    }

                    CommonFunctions.Log.Debug($"Rescheduling price checker for {this.MillisecondsUntilExecution} ms");
                    rescheduleLog.Log();
                    UpdateScheduleTime(this.MillisecondsUntilExecution);
                }
                catch (Exception rescheduleEx)
                {
                    CommonFunctions.Log.Error("Error rescheduling scheduler", rescheduleEx);
                }
            }
        }
Exemplo n.º 2
0
        private static void ComparePrices()
        {
            List <AmazonWatchItem> notificationList = new List <AmazonWatchItem>();

            DataTable itemsTable = DBHelper.GetItemInformation(CommonFunctions.ItemsConnectionString);

            foreach (DataRow row in itemsTable.Rows)
            {
                try
                {
                    DisplayLogItem priceLog = new DisplayLogItem();

                    int    itemID       = Int32.Parse(row["ItemID"].ToString());
                    string title        = row["Title"].ToString();
                    string url          = row["Url"].ToString();
                    float  amazonPrice  = float.Parse(row["AmazonPrice"].ToString());
                    float  desiredPrice = float.Parse(row["DesiredPrice"].ToString());
                    DateTime.TryParse(row["CreateDate"].ToString(), out DateTime createDate);
                    DateTime.TryParse(row["LastModifiedDate"].ToString(), out DateTime lastModifiedDate);
                    DateTime.TryParse(row["LastNotifiedDate"].ToString(), out DateTime lastNotifiedDate);

                    CommonFunctions.Log.Debug($"Comparing price for {title}:= ${amazonPrice} vs ${desiredPrice}");

                    if (amazonPrice <= desiredPrice && amazonPrice > 0)
                    {
                        if (!CommonFunctions.UserSettings.LimitNotifications)
                        {
                            AmazonWatchItem notifyItem = new AmazonWatchItem(itemID, title, url, amazonPrice, desiredPrice, createDate, lastModifiedDate, lastNotifiedDate);
                            notificationList.Add(notifyItem);
                        }
                        else if (CommonFunctions.UserSettings.LimitNotifications && lastNotifiedDate.Date != DateTime.Now.Date)
                        {
                            AmazonWatchItem notifyItem = new AmazonWatchItem(itemID, title, url, amazonPrice, desiredPrice, createDate, lastModifiedDate, lastNotifiedDate);
                            notificationList.Add(notifyItem);
                        }

                        DisplayLogItem notifyLog = new DisplayLogItem
                        {
                            LogText = DisplayLogItem.CreateLogText($"{CommonFunctions.RemoveSQLCharacters(title)}",
                                                                   $"is at or below your desired price of ${desiredPrice}! You should consider buying!",
                                                                   $"Current Price: ${amazonPrice}",
                                                                   $"Desired Price: ${desiredPrice}"),
                            LogImage = DisplayLogItem.GetLogIcon(DisplayLogItem.LogIcon.Buy)
                        };


                        notifyLog.Log();
                    }
                    else
                    {
                        priceLog.LogText = DisplayLogItem.CreateLogText($"{CommonFunctions.RemoveSQLCharacters(title)}",
                                                                        amazonPrice > 0 ? $"Current Price: ${amazonPrice}" : $"Current Price: Unable to find",
                                                                        $"Desired Price: ${desiredPrice}");

                        priceLog.LogImage = amazonPrice > 0 ? DisplayLogItem.GetLogIcon(DisplayLogItem.LogIcon.Item) : DisplayLogItem.GetLogIcon(DisplayLogItem.LogIcon.NoPriceItem);
                        priceLog.Log();
                    }
                }
                catch (Exception compareError)
                {
                    CommonFunctions.Log.Error("Error comparing desired price and purchase price", compareError);

                    DisplayLogItem compareErrorLog = new DisplayLogItem("The was an error comparing prices for one of your items", DisplayLogItem.LogIcon.Error);
                    compareErrorLog.Log();
                }
            }

            if (notificationList.Count > 0)
            {
                if (CommonFunctions.UserSettings.EmailNotifications || CommonFunctions.UserSettings.TextNotifications)
                {
                    DisplayLogItem notifyLog = new DisplayLogItem();
                    notifyLog.LogText  = DisplayLogItem.CreateLogText("Notifications", $"Sending alert for {notificationList.Count} " + (notificationList.Count > 1 ? "items" : "item"));
                    notifyLog.LogImage = DisplayLogItem.GetLogIcon(DisplayLogItem.LogIcon.Notification);
                    notifyLog.Log();
                }

                Notifier notifier = new Notifier(notificationList);
                notifier.Notify();
            }
        }
Exemplo n.º 3
0
        public static void CheckPrices(List <AmazonWatchItem> currentItems)
        {
            CheckingPrices = true;

            Application.Current.Dispatcher.Invoke(new Action(() =>
            {
                //Clear previous logs
                MainWindow.LogDataGrid.Items.Clear();
                MainWindow.LogDataGrid.UpdateLayout();

                //Update Last Price Check
                CommonFunctions.UserSettings.SetLastExecutedPriceCheck(DateTime.Now);
                ((MainWindow)Application.Current.MainWindow).RefreshLastPriceCheck();

                //Update button
                ((MainWindow)Application.Current.MainWindow).startPriceCheck_btn.Content   = "Checking Prices...";
                ((MainWindow)Application.Current.MainWindow).startPriceCheck_btn.IsEnabled = false;
            }));


            var watch = System.Diagnostics.Stopwatch.StartNew();

            List <AmazonWatchItem> updatedItemList = new List <AmazonWatchItem>();
            DisplayLogItem         startLog        = new DisplayLogItem(DisplayLogItem.CreateLogText("Starting Price Check",
                                                                                                     $"{ DateTime.Now }"),
                                                                        DisplayLogItem.LogIcon.Start);

            startLog.Log();

            try
            {
                Task.Factory.StartNew(() =>
                {
                    //Check Prices
                    PriceCheckWorker priceChecker = new PriceCheckWorker(currentItems);
                    updatedItemList = priceChecker.CheckPrices();
                })
                .ContinueWith(task =>
                {
                    watch.Stop();
                    var elapsedSeconds = watch.ElapsedMilliseconds * 0.001;
                    CommonFunctions.Log.Debug($"Checking amazon prices");


                    //Update the DB with new prices
                    UpdateItemsDB(updatedItemList);

                    //Compare the prices and notify if necessary
                    ComparePrices();

                    DisplayLogItem finishLog = new DisplayLogItem(DisplayLogItem.CreateLogText($"Price Check Complete",
                                                                                               updatedItemList.Count > 1 ? $"{updatedItemList.Count} items have been checked" : $"{updatedItemList.Count} item has been checked",
                                                                                               $"Execution time: {elapsedSeconds}s"),
                                                                  DisplayLogItem.LogIcon.Success);
                    finishLog.Log();
                    CommonFunctions.Log.Debug($"Finished checking prices:= {elapsedSeconds}s");

                    //Finally re-enable the start button after the thread has finished and update the watchlist grid since we know the price check is complete
                    Application.Current.Dispatcher.Invoke(new Action(() =>
                    {
                        ((MainWindow)Application.Current.MainWindow).startPriceCheck_btn.Content   = "Start Price Check";
                        ((MainWindow)Application.Current.MainWindow).startPriceCheck_btn.IsEnabled = true;
                        ((MainWindow)Application.Current.MainWindow).FillWatchList();
                    }));
                });
            }
            catch (Exception e)
            {
                CommonFunctions.Log.Error("Unable to check prices", e);
                DisplayLogItem errorLog = new DisplayLogItem($"An error occurred when checking prices", DisplayLogItem.LogIcon.Error);
                errorLog.Log();
            }
            finally
            {
                CheckingPrices = false;
            }
        }