Archived
1
0

add a journal stream to get new events

This commit is contained in:
Florian Stinglmayr 2021-08-25 12:16:28 +02:00
parent 9d4342a6f8
commit 1abeb7af93
5 changed files with 104 additions and 0 deletions

View File

@ -84,5 +84,12 @@ namespace NonaBGS.Journal {
public JObject JSON { public JObject JSON {
get { return this.json; } get { return this.json; }
} }
public override string ToString() {
if (json == null) {
return "";
}
return json.ToString();
}
} }
} }

View File

@ -20,6 +20,13 @@ namespace NonaBGS.Journal
private static string iso8601 = "yyyyMMddTHHmmss"; private static string iso8601 = "yyyyMMddTHHmmss";
private static string iso8601_notime = "yyyyMMdd"; private static string iso8601_notime = "yyyyMMdd";
public static bool VerifyFile(string path) {
string filename = Path.GetFileName(path);
var matches = fileregex.Matches(filename);
return matches.Count != 0;
}
private void SetFilename(string path) { private void SetFilename(string path) {
string filename = Path.GetFileName(path); string filename = Path.GetFileName(path);
if (!File.Exists(path) || filename == null) { if (!File.Exists(path) || filename == null) {

87
Journal/JournalStream.cs Normal file
View File

@ -0,0 +1,87 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using NonaBGS.Journal;
using System.IO;
namespace NonaBGS.Journal {
public class JournalStream {
private PlayerJournal journal = null;
private FileSystemWatcher watcher = null;
private Dictionary<string, StreamReader> streams = new Dictionary<string, StreamReader>();
public delegate void NewJournalEntryDelegate(Entry entry);
public event NewJournalEntryDelegate NewJournalEntry;
public PlayerJournal Journal {
get => journal;
set => journal = value;
}
public JournalStream() {
}
public JournalStream(PlayerJournal journal) {
this.journal = journal;
Open();
}
public void Open() {
if (watcher != null) {
return;
}
watcher = new FileSystemWatcher(journal.Location);
watcher.NotifyFilter = NotifyFilters.FileName |
NotifyFilters.LastWrite |
NotifyFilters.Size;
watcher.Changed += Watcher_Changed;
watcher.Created += Watcher_Created;
watcher.Filter = "*.log";
watcher.EnableRaisingEvents = true;
}
public void Close() {
if (watcher != null) {
watcher.EnableRaisingEvents = false;
}
watcher = null;
streams.Clear();
}
private void Watcher_Created(object sender, FileSystemEventArgs e) {
if (!streams.ContainsKey(e.FullPath) && JournalFile.VerifyFile(e.FullPath)) {
streams[e.FullPath] = new StreamReader(e.FullPath);
}
}
private void Watcher_Changed(object sender, FileSystemEventArgs e) {
if (!streams.ContainsKey(e.FullPath) && JournalFile.VerifyFile(e.FullPath)) {
var filestream = new FileStream(e.FullPath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
streams[e.FullPath] = new StreamReader(filestream);
}
var stream = streams[e.FullPath];
if (stream == null) {
return;
}
string line;
while ((line = stream.ReadLine()) != null) {
try {
Entry entry = Entry.Parse(line);
NewJournalEntry?.Invoke(entry);
} catch (Exception) {
}
}
}
}
}

View File

@ -11,6 +11,8 @@ namespace NonaBGS.Journal {
private List<JournalFile> journalfiles = new List<JournalFile>(); private List<JournalFile> journalfiles = new List<JournalFile>();
private string basepath = null; private string basepath = null;
public string Location => basepath;
public PlayerJournal() { public PlayerJournal() {
Initialise(DefaultPath); Initialise(DefaultPath);
} }

View File

@ -77,6 +77,7 @@
<Compile Include="EDDB\Stations.cs" /> <Compile Include="EDDB\Stations.cs" />
<Compile Include="Journal\Credits.cs" /> <Compile Include="Journal\Credits.cs" />
<Compile Include="Journal\EliteDangerous.cs" /> <Compile Include="Journal\EliteDangerous.cs" />
<Compile Include="Journal\JournalStream.cs" />
<Compile Include="Journal\MarketSellEntry.cs" /> <Compile Include="Journal\MarketSellEntry.cs" />
<Compile Include="Journal\MultiSellExplorationDataEntry.cs" /> <Compile Include="Journal\MultiSellExplorationDataEntry.cs" />
<Compile Include="Journal\RedeemVoucherEntry.cs" /> <Compile Include="Journal\RedeemVoucherEntry.cs" />