From c942d6f227f387f771624214a6d10532ec8e2c52 Mon Sep 17 00:00:00 2001 From: Charles Macanka Date: Sat, 22 Jun 2019 12:06:13 -0400 Subject: [PATCH] Remove magic values and clean up initializations. --- Procurement/Utility/ClientLogFileWatcher.cs | 50 ++++++++++++------- .../ViewModel/TabViewModel/StashViewModel.cs | 7 ++- 2 files changed, 37 insertions(+), 20 deletions(-) diff --git a/Procurement/Utility/ClientLogFileWatcher.cs b/Procurement/Utility/ClientLogFileWatcher.cs index 85eedcde..3a59e3e3 100644 --- a/Procurement/Utility/ClientLogFileWatcher.cs +++ b/Procurement/Utility/ClientLogFileWatcher.cs @@ -9,6 +9,10 @@ namespace Procurement.Utility { class ClientLogFileWatcher { + private const string _clientLogFileLocationConfigName = "ClientLogFileLocation"; + private const string _enableClientLogFileMonitoringConfigName = "EnableClientLogFileMonitoring"; + private const int _clientLogFilePollingIntervalMilliseconds = 30000; // 30 seconds + private static ClientLogFileWatcher _instance; public static ClientLogFileWatcher Instance { @@ -21,7 +25,7 @@ public static ClientLogFileWatcher Instance } } - protected static System.IO.FileSystemWatcher FileWatcher + protected static FileSystemWatcher FileWatcher { get; private set; @@ -62,22 +66,28 @@ protected Regex LocationChangedRegex protected void Initialize() { - if (!Settings.UserSettings.Keys.Contains("ClientLogFileLocation")) + if (!Settings.UserSettings.Keys.Contains(_clientLogFileLocationConfigName)) return; - string fullFilePath = Settings.UserSettings["ClientLogFileLocation"]; + string fullFilePath = Settings.UserSettings[_clientLogFileLocationConfigName]; if (string.IsNullOrWhiteSpace(fullFilePath)) return; - FileWatcher = new System.IO.FileSystemWatcher(); - FileWatcher.Path = System.IO.Path.GetDirectoryName(fullFilePath); - FileWatcher.Filter = System.IO.Path.GetFileName(fullFilePath); - FileWatcher.NotifyFilter = NotifyFilters.LastWrite | NotifyFilters.Size | NotifyFilters.LastAccess; + if (FileWatcher == null) + { + FileWatcher = new FileSystemWatcher(); + FileWatcher.Path = Path.GetDirectoryName(fullFilePath); + FileWatcher.Filter = Path.GetFileName(fullFilePath); + FileWatcher.NotifyFilter = NotifyFilters.LastWrite | NotifyFilters.Size | NotifyFilters.LastAccess; - FileWatcher.Changed += OnFileChanged; + FileWatcher.Changed += OnFileChanged; + } - PollingTimer = new System.Timers.Timer(); - PollingTimer.Elapsed += (s, e) => { ReadClientLogFile(); }; - PollingTimer.Interval = 30000; // 30 seconds + if (PollingTimer == null) + { + PollingTimer = new System.Timers.Timer(); + PollingTimer.Elapsed += (s, e) => { ReadClientLogFile(); }; + PollingTimer.Interval = _clientLogFilePollingIntervalMilliseconds; + } // Regex to catch lines formatted like: // 2019/06/21 19:16:37 245842781 aa1 [INFO Client 19844] : You have entered Sunspire Hideout. @@ -88,9 +98,9 @@ protected void Initialize() internal void Start() { - if (!Settings.UserSettings.Keys.Contains("EnableClientLogFileMonitoring")) + if (!Settings.UserSettings.Keys.Contains(_enableClientLogFileMonitoringConfigName)) return; - var enabled = Convert.ToBoolean(Settings.UserSettings["EnableClientLogFileMonitoring"]); + var enabled = Convert.ToBoolean(Settings.UserSettings[_enableClientLogFileMonitoringConfigName]); if (!enabled) return; @@ -117,8 +127,8 @@ protected void ReadClientLogFile() { try { - Stream stream = new FileStream(Settings.UserSettings["ClientLogFileLocation"], FileMode.Open, - FileAccess.Read, FileShare.ReadWrite); + using (Stream stream = new FileStream(Settings.UserSettings[_clientLogFileLocationConfigName], + FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) using (var reader = new StreamReader(stream)) { // Quit early if the log file is no longer than the last time we read it. @@ -141,7 +151,11 @@ protected void ReadClientLogFile() eventTime = DateTime.ParseExact(match.Groups[1].Value, "yyyy/MM/dd HH:mm:ss", System.Globalization.CultureInfo.InvariantCulture); - long.TryParse(match.Groups[2].Value, out eventTimestamp); + if (!long.TryParse(match.Groups[2].Value, out eventTimestamp)) + { + Logger.Log(string.Format("Failed to parse event timestamp from string '{0}'.", + match.Groups[2].Value)); + } location = match.Groups[3].Value; if ((DateTime.Now - eventTime).TotalSeconds > 600 || @@ -159,14 +173,14 @@ protected void ReadClientLogFile() Instance.LastFileSizeSeen = reader.BaseStream.Length; } } - catch (System.IO.IOException ex) + catch (IOException ex) { Logger.Log(string.Format("Failed to open config log file: {0}", ex.ToString())); } } } - protected static void OnFileChanged(object source, System.IO.FileSystemEventArgs e) + protected static void OnFileChanged(object source, FileSystemEventArgs e) { Instance.ReadClientLogFile(); } diff --git a/Procurement/ViewModel/TabViewModel/StashViewModel.cs b/Procurement/ViewModel/TabViewModel/StashViewModel.cs index 1f21843e..1a8ed08b 100644 --- a/Procurement/ViewModel/TabViewModel/StashViewModel.cs +++ b/Procurement/ViewModel/TabViewModel/StashViewModel.cs @@ -30,6 +30,8 @@ public class StashViewModel : ObservableBase private bool currencyDistributionUsesCount; private string filter; + private const string _enableTabRefreshOnLocationChangedConfigName = "EnableTabRefreshOnLocationChanged"; + public string Filter { get { return filter; } @@ -180,10 +182,11 @@ public StashViewModel(StashView stashView) else configuredOrbType = (OrbType)Enum.Parse(typeof(OrbType), currencyDistributionMetric); - if (Settings.UserSettings.Keys.Contains("EnableTabRefreshOnLocationChanged")) + if (Settings.UserSettings.Keys.Contains(_enableTabRefreshOnLocationChangedConfigName)) { var enabled = false; - if (bool.TryParse(Settings.UserSettings["EnableTabRefreshOnLocationChanged"], out enabled) && enabled) + if (bool.TryParse(Settings.UserSettings[_enableTabRefreshOnLocationChangedConfigName], out enabled) + && enabled) { ClientLogFileWatcher.ClientLogFileChanged -= OnClientLogFileChanged; ClientLogFileWatcher.ClientLogFileChanged += OnClientLogFileChanged;