-
Notifications
You must be signed in to change notification settings - Fork 40
/
Program.cs
214 lines (153 loc) · 7.94 KB
/
Program.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Diagnostics;
using System.Collections.Concurrent;
using System.Collections.Specialized;
using System.Configuration;
using System.Net;
using System.Threading;
using System.Threading.Tasks;
using System.Data;
using System.Data.SqlClient;
using System.ComponentModel;
using System.Timers;
using MOE.Common;
namespace NEWDecodeandImportASC3Logs
{
class Program
{
static void Main(string[] args)
{
List<string> dirList = new List<string>();
List<string> fileList = new List<string>();
string CWD = Properties.Settings.Default.ASC3LogsPath;
string CSV = Properties.Settings.Default.CSVOutPAth;
//var tableCollection = new BlockingCollection<DataTable>();
//DataTable mergedEventsTable = new DataTable();
ParallelOptions options;
if (!Properties.Settings.Default.forceNonParallel)
{
options = new ParallelOptions { MaxDegreeOfParallelism = -1 };
}
else
{
if (Properties.Settings.Default.MaxThreads < 2)
{
options = new ParallelOptions { MaxDegreeOfParallelism = 1 };
}
else
{
options = new ParallelOptions { MaxDegreeOfParallelism = Properties.Settings.Default.MaxThreads };
}
}
foreach (string s in Directory.GetDirectories(CWD))
{
dirList.Add(s);
}
SimplePartitioner<string> sp = new SimplePartitioner<string>(dirList);
//foreach (string dir in dirList)
ParallelOptions optionsMain = new ParallelOptions { MaxDegreeOfParallelism = Properties.Settings.Default.MaxThreadsMain};
Parallel.ForEach(sp, optionsMain, dir =>
{
var ToDelete = new ConcurrentBag<string>();
if (Properties.Settings.Default.WriteToConsole)
{
Console.WriteLine("-----------------------------Starting Signal " + dir);
}
//get the name of the directory and casting it to an int
//This is the only way the program knows the signal number of the controller.
string[] strsplit = dir.Split(new char[] { '\\' });
string dirname = strsplit.Last();
string sigid = dirname;
var mergedEventsTable = new BlockingCollection<MOE.Common.Data.MOE.Controller_Event_LogRow>();
SimplePartitioner<string> sp2 = new SimplePartitioner<string>(Directory.GetFiles(dir, "*.dat"));
Parallel.ForEach(sp2, options, s =>
{
try
{
MOE.Common.Business.LogDecoder.ASC3Decoder.DecodeASC3File(s, sigid, mergedEventsTable);
ToDelete.Add(s);
}
catch { }
}
);
using (MOE.Common.Data.MOE.Controller_Event_LogDataTable EventsTable = new MOE.Common.Data.MOE.Controller_Event_LogDataTable())
{
mergedEventsTable.CopyToDataTable(EventsTable, LoadOption.PreserveChanges);
mergedEventsTable.Dispose();
string connectionString = Properties.Settings.Default.SPMConnectionString;
string destTable = Properties.Settings.Default.DestinationTableNAme;
MOE.Common.Business.BulkCopyOptions Options = new MOE.Common.Business.BulkCopyOptions(connectionString, destTable,
Properties.Settings.Default.WriteToConsole,Properties.Settings.Default.forceNonParallel, Properties.Settings.Default.MaxThreads, Properties.Settings.Default.DeleteFile,
Properties.Settings.Default.EarliestAcceptableDate, Properties.Settings.Default.BulkCopyBatchSize, Properties.Settings.Default.BulkCopyTimeOut);
if (EventsTable.Count > 0)
{
if (MOE.Common.Business.Signal.BulktoDB(EventsTable, Options) && Properties.Settings.Default.DeleteFile)
{
DeleteFiles(ToDelete);
}
//string filename = sigid.ToString();
//filename += "_";
//filename += DateTime.Now.Month.ToString();
//filename += "_";
//filename += DateTime.Now.Day.ToString();
//filename += "_";
//filename += DateTime.Now.Year.ToString();
//filename += "_";
//filename += DateTime.Now.Hour.ToString();
//filename += "_";
//filename += DateTime.Now.Minute.ToString();
//filename += "_";
//filename += DateTime.Now.Second.ToString();
//filename += ".csv";
//SaveAsCSV(EventsTable, Path.Combine(CSV, filename));
//if (Properties.Settings.Default.DeleteFile)
//{
// DeleteFiles(ToDelete);
//}
}
else
{
ConcurrentBag<String> td = new ConcurrentBag<String>();
foreach (string s in ToDelete)
{
if (s.Contains("1970_01_01"))
{
td.Add(s);
}
}
if (td.Count > 0)
{
DeleteFiles(td);
}
}
}
}
);
}
static public bool SaveAsCSV(DataTable Datatable, string Path)
{
StringBuilder sb = new StringBuilder();
IEnumerable<string> columnNames = Datatable.Columns.Cast<DataColumn>().
Select(column => column.ColumnName);
sb.AppendLine(string.Join(",", columnNames));
foreach (DataRow row in Datatable.Rows)
{
IEnumerable<string> fields = row.ItemArray.Select(field => field.ToString());
sb.AppendLine(string.Join(",", fields));
}
File.WriteAllText(Path, sb.ToString());
return true;
}
public static void DeleteFiles(ConcurrentBag<string> Files)
{
foreach (string f in Files)
{
File.Delete(f);
}
}
}
}