-
Notifications
You must be signed in to change notification settings - Fork 1
/
StarDatabase.cs
134 lines (124 loc) · 4.51 KB
/
StarDatabase.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Threading;
using System.Text;
using System.Threading.Tasks;
using CsvHelper;
using CsvHelper.Configuration;
namespace ControlProgram
{
class objectDataRecordsMap : ClassMap<ObjectDataRecords>
{
public objectDataRecordsMap()
{
AutoMap(CultureInfo.InvariantCulture);
Map(member => member.E).Ignore();
Map(member => member.Q).Ignore();
Map(member => member.I).Ignore();
Map(member => member.Omega).Ignore();
Map(member => member.W).Ignore();
Map(member => member.Tp).Ignore();
Map(member => member.N).Ignore();
Map(member => member.M).Ignore();
Map(member => member.Nu).Ignore();
Map(member => member.A).Ignore();
Map(member => member.AD).Ignore();
Map(member => member.PR).Ignore();
Map(member => member.Calculations).Ignore();
}
}
class StarDatabase
{
public List<ObjectDataRecords> data = new List<ObjectDataRecords>();
private int totalCount;
private int currentCount = 0;
private Term term;
private Logger logger;
public StarDatabase()
{
logger = new Logger("DATABASE", Logger.Level.INFO);
}
public void load(Term term)
{
this.term = term;
logger.log(Logger.Level.INFO, "Loading all planet data...");
string topPath = "./Horizons/data/";
if(term != null)
{
totalCount = Directory.GetFiles(topPath, "*.csv", SearchOption.AllDirectories).Length;
Thread barGraphThread = new Thread(barGraphWork);
barGraphThread.Start();
processDir(topPath);
barGraphThread.Join();
}
else
{
processDir(topPath);
}
}
public int search(string name)
{
logger.log(Logger.Level.INFO, "Searching for " + name);
int loc = data.FindIndex(x => (x.Name.ToUpper() == name.ToUpper() && x.Date >= DateTime.UtcNow));
if(loc < 0)
logger.log(Logger.Level.INFO, "Could not find " + name);
else
logger.log(Logger.Level.INFO, "Found " + name + " at " + loc);
return loc;
}
private void barGraphWork()
{
while (currentCount < totalCount)
{
term.barGraph(new Term.CursorAddress { x = 0, y = 2 }, new Term.CursorAddress { x = 0, y = 2 }, 80, "#", currentCount, totalCount);
Thread.Sleep(80);
}
}
private void processDir(string Path)
{
string[] fileEntries = Directory.GetFiles(Path);
foreach (var file in fileEntries)
{
if (file.EndsWith(".csv"))
{
logger.log(Logger.Level.INFO, "Found File \"" + file + "\" ");
processFile(file);
currentCount++;
}
}
string[] subDirectoryEntries = Directory.GetDirectories(Path);
foreach (var dir in subDirectoryEntries)
processDir(dir);
}
private void processFile(string filePath)
{
DateTime todayDate = DateTime.UtcNow.Subtract(TimeSpan.FromHours(100));
DateTime futureDate = DateTime.UtcNow.AddDays(5);
using (var reader = new StreamReader(filePath))
using (var csv = new CsvReader(reader, CultureInfo.InvariantCulture))
{
csv.Configuration.RegisterClassMap<objectDataRecordsMap>();
try
{
var record = csv.GetRecords<ObjectDataRecords>();
foreach (var row in record)
{
if (row.Date >= todayDate && row.Date <= futureDate)
{
data.Add(row);
logger.log(Logger.Level.DEBUG, "Stored " + record);
}
else
logger.log(Logger.Level.DEBUG, "Too far in advance to load...");
}
}
catch (Exception ex)
{
logger.log(Logger.Level.WARNING, ex.ToString());
}
}
}
}
}