Skip to content

Commit

Permalink
Remove magic values and clean up initializations.
Browse files Browse the repository at this point in the history
  • Loading branch information
thailyn committed Jun 22, 2019
1 parent 673c911 commit c942d6f
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 20 deletions.
50 changes: 32 additions & 18 deletions Procurement/Utility/ClientLogFileWatcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -21,7 +25,7 @@ public static ClientLogFileWatcher Instance
}
}

protected static System.IO.FileSystemWatcher FileWatcher
protected static FileSystemWatcher FileWatcher
{
get;
private set;
Expand Down Expand Up @@ -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.
Expand All @@ -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;

Expand All @@ -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.
Expand All @@ -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 ||
Expand All @@ -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();
}
Expand Down
7 changes: 5 additions & 2 deletions Procurement/ViewModel/TabViewModel/StashViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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; }
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit c942d6f

Please sign in to comment.