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); } } }
private static void UpdateItemsDB(List <AmazonWatchItem> itemList) { try { foreach (AmazonWatchItem item in itemList) { DBHelper.UpdateItem(CommonFunctions.ItemsConnectionString, item.Id, item.Title, item.Price, item.LastModifiedDate); } } catch (Exception ex) { CommonFunctions.Log.Error("Unable to update DB with new prices", ex); DisplayLogItem errorLog = new DisplayLogItem { LogText = "An error occurred when updating the items", LogImage = DisplayLogItem.GetLogIcon(DisplayLogItem.LogIcon.Error) }; errorLog.Log(); } }
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(); } }