-
Notifications
You must be signed in to change notification settings - Fork 0
/
AsyncDebugReader.cs
93 lines (78 loc) · 3.17 KB
/
AsyncDebugReader.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
using ChiaAdjutant.Data;
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Diagnostics;
using System.Globalization;
namespace ChiaAdjutant
{
public class AsyncDebugReader
{
private const string _pathToFile = @"%USERPROFILE%\.chia\mainnet\log\debug.log";
private Regex _basicRegExp = new Regex(@"harvester chia.harvester.harvester: INFO");
private List<PlotPassEventData> _plotEvents = new List<PlotPassEventData>();
public AsyncDebugReader()
{
ReadData();
}
public async void ReadData()
{
await Task.Run(()=> ReadDataBlock());
}
public async void ReadDataBlock()
{
Stream fileStream = new FileStream(Environment.ExpandEnvironmentVariables(_pathToFile), FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
StreamReader streamReader = new StreamReader(fileStream);
string lineBuffer = null;
while(true)
{
lineBuffer = streamReader.ReadLine();
if(lineBuffer != null && _basicRegExp.IsMatch(lineBuffer))
{
GeneratePlotData(lineBuffer);
}
else if(lineBuffer == null)
{
await Task.Delay(3000);
}
}
}
private void GeneratePlotData(string data)
{
Regex dateTimeReg = new Regex(@"^(\d+-\d+-\d+T\d+:\d+:\d+.\d+)");
Regex cleaningReg = new Regex(@"\D+");
string dateTimeS = dateTimeReg.Match(data).Value;
dateTimeS = cleaningReg.Replace(dateTimeS, " ");
string[] splitedData = dateTimeS.Split(' ');
int[] dateElement = new int[7];
for(int i = 0;i< dateElement.Length;i++)
{
dateElement[i] = int.Parse(splitedData[i]);
}
DateTime dateTime = new DateTime(dateElement[0], dateElement[1], dateElement[2],
dateElement[3], dateElement[4], dateElement[5], dateElement[6]);
Regex eligableRef = new Regex(@"(\d+\splots were eligible)");
int plotsPassed = int.Parse(eligableRef.Match(data).Value.Split(' ')[0]);
Regex proofRef = new Regex(@"(\d+\sproofs)");
int proofPassed = int.Parse(proofRef.Match(data).Value.Split(' ')[0]);
Regex passTimeRef = new Regex(@"(Time: \S+)");
IFormatProvider formatter = new NumberFormatInfo { NumberDecimalSeparator = "." };
double passTime = double.Parse(passTimeRef.Match(data).Value.Split(' ')[1], formatter);
PlotPassEventData passEventData = new PlotPassEventData(dateTime, plotsPassed, proofPassed, passTime);
_plotEvents.Add(passEventData);
}
public PlotPassEventData GetNextPassEvent()
{
if(_plotEvents.Count == 0)
{
return null;
}
PlotPassEventData passEventData = _plotEvents[0];
_plotEvents.RemoveAt(0);
return passEventData;
}
}
}