-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathProgram.cs
More file actions
342 lines (277 loc) · 10 KB
/
Program.cs
File metadata and controls
342 lines (277 loc) · 10 KB
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
using System.Diagnostics;
namespace ImportWLK
{
static class Program
{
public static Cumulus Cumulus { get; set; }
public static string Location { get; set; }
private static ConsoleColor defConsoleColour;
private const int headerBytes = 212; // 16 + 4 + 32 * (2 + 4)
static void Main()
{
// Tell the user what is happening
TextWriterTraceListener myTextListener = new TextWriterTraceListener($"MXdiags{Path.DirectorySeparatorChar}ImportWLK-{DateTime.Now:yyyyMMdd-HHmmss}.txt", "WLKlog");
Trace.Listeners.Add(myTextListener);
Trace.AutoFlush = true;
defConsoleColour = Console.ForegroundColor;
var fullVer = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version ?? new Version(0, 0, 0, 0);
var version = $"{fullVer.Major}.{fullVer.Minor}.{fullVer.Build}";
LogMessage("ImportWLK v." + version);
Console.WriteLine("ImportWLK v." + version);
LogMessage("Processing started");
Console.WriteLine();
Console.WriteLine($"Processing started: {DateTime.Now:U}");
Console.WriteLine();
// get the location of the exe - we will assume this is in the Cumulus root folder
Location = AppDomain.CurrentDomain.BaseDirectory;
// Read the Cumulus.ini file
Cumulus = new Cumulus();
// Check meteo day
if (Cumulus.RolloverHour != 0)
{
LogMessage("Cumulus is not configured for a midnight rollover, so Import cannot create any day file entries");
LogConsole("Cumulus is not configured for a midnight rollover, so no day file entries will be created", ConsoleColor.DarkYellow);
LogConsole("You must run CreateMissing after this Import to create the day file entries", ConsoleColor.DarkYellow);
}
else
{
LogMessage("Cumulus is configured for a midnight rollover, Import will create day file entries");
LogConsole("Cumulus is configured for a midnight rollover, so day file entries will be created", ConsoleColor.Cyan);
LogConsole("You must still run CreateMissing after this Import to add missing details to those day file entries", ConsoleColor.Cyan);
}
Console.WriteLine();
// Load the existing day file if it exists
DayFile.ReadDayFile();
// Find all the wlk files
// naming convention YYYY-MM.wlk, eg 2024-05.wlk
LogMessage("Searching for wlk files");
Console.WriteLine("Searching for wlk log files...");
var dirsep = Path.DirectorySeparatorChar;
if (!Directory.Exists(Location + dirsep + "wlk"))
{
LogMessage($"The source directory '{Location}{dirsep}wlk' does not exist, aborting");
LogConsole($"The source directory '{Location}{dirsep}wlk' does not exist, aborting", ConsoleColor.Red);
Environment.Exit(1);
}
var dirInfo = new DirectoryInfo(Location + dirsep + "wlk");
var wlkFiles = dirInfo.GetFiles("????-??.wlk");
LogMessage($"Found {wlkFiles.Length} wlk log files");
LogConsole($"Found {wlkFiles.Length} wlk log files", defConsoleColour);
// sort the file list
var wlkList = wlkFiles.OrderBy(f => f.Name).ToList();
// foreach file
// get header
// foreach day
// daily summaries to dayfile entry
// archive entries to log file entries
// archive entries to extra log file entries
var lastYear = 0;
foreach (var wlk in wlkList)
{
FileStream file;
try
{
file = File.Open(wlk.FullName, FileMode.Open, FileAccess.Read, FileShare.Read);
}
catch (Exception ex)
{
LogMessage($"Error opening file {wlk.Name} - {ex.Message}");
LogConsole($"Error opening file {wlk.Name} - {ex.Message}", ConsoleColor.Red);
LogConsole("Skipping to next file", defConsoleColour);
// abort this file
continue;
}
// get the year/month from the file name
var arr = wlk.Name.Split('-');
var year = int.Parse(arr[0]);
var month = int.Parse(arr[1].Split('.')[0]);
using var binReader = new BinaryReader(file);
var header = new WlkFileHeader();
header.ReadFile(binReader);
LogConsole($"Processing {wlk.Name}...", ConsoleColor.Gray);
LogMessage($"File {wlk.Name} contains {header.TotalRecords} records covering {header.DayIndices.Length} days");
var logFileWritten = false;
var date = DateTime.MinValue;
for (var day = 1; day < header.DayIndices.Length; day++)
{
if (header.DayIndices[day].RecordsInDay == 0)
{
LogMessage($"Day {day} contains no records");
LogConsole($" Day {day} contains no records", ConsoleColor.Gray);
continue;
}
LogFile.HumidexHigh = -9999;
LogFile.ApparentHigh = -9999;
LogFile.ApparentLow = 9999;
LogFile.FeelsLikeHigh = -9999;
LogFile.FeelsLikeLow = 9999;
LogFile.WindAvgHigh = -9999;
LogFile.ClicksToday = 0;
if (lastYear != year)
{
lastYear = year;
LogFile.ClicksThisYear = 0;
}
LogMessage($"{year:D4}/{month:D2}/{day:D2} contains {header.DayIndices[day].RecordsInDay} records");
LogConsole($" {year:D4}/{month:D2}/{day:D2} contains {header.DayIndices[day].RecordsInDay} records", ConsoleColor.Gray);
date = new DateTime(year, month, day, 0, 0, 0, DateTimeKind.Local);
binReader.BaseStream.Seek(headerBytes + header.DayIndices[day].StartPosition * 88, SeekOrigin.Begin);
for (var i = 0; i < header.DayIndices[day].RecordsInDay; i++)
{
// get the first record type
byte type;
try
{
type = binReader.ReadByte();
if (type == 1)
{
// archive record
LogDebugMessage($"Processing archive record {i} type 1");
var rec = new WlkArchiveRecord();
rec.ReadRecord(binReader);
rec.Timestamp = date.AddMinutes(rec.PackedTime);
LogDebugMessage(" Log entry time: " + rec.Timestamp);
// The wlk file contains the midnight entry for the following month, Cumulus starts the day at midnight rather than ending it midnight
if (rec.Timestamp.Month != month)
{
// set the daily records for derived values
SetExtraValues(date);
// save the log file
LogFile.WriteLogFile();
logFileWritten = true;
LogFile.Initialise();
// save the extra log file
ExtraLogFile.WriteLogFile();
ExtraLogFile.Initialise();
}
if (LogFile.LastTimeStamp != DateTime.MinValue && LogFile.LastTimeStamp.Month != rec.Timestamp.Month)
{
// looks like the last entry from the previous month does not match the current month (have we skipped a month or two?) - so discard it
LogFile.Initialise();
ExtraLogFile.Initialise();
}
LogFile.AddRecord(rec);
ExtraLogFile.AddRecord(rec);
}
else if (type == 2)
{
// daily summary1
LogDebugMessage($"Processing archive record {i} type 2");
var rec = new WlkDailySummary1();
rec.ReadRecord(binReader);
rec.Date = date;
DayFile.AddRecord1(rec);
}
else if (type == 3)
{
// daily summary2
LogDebugMessage($"Processing archive record {i} type 3");
var rec = new WlkDailySummary2();
rec.ReadRecord(binReader);
rec.Date = date;
DayFile.AddRecord2(rec);
}
else
{
// unknown
LogMessage($"Unknown record type found - {type}. Aborting processing of this file");
LogConsole($"Unknown record type found - {type}. Aborting processing of this file", ConsoleColor.Red);
}
}
catch (EndOfStreamException)
{
binReader.Close();
}
catch (Exception ex)
{
LogMessage("Error - " + ex.Message);
LogConsole(" Error - " + ex.Message, ConsoleColor.Red);
}
}
// set the daily records for derived values
SetExtraValues(date);
}
// save the log file if not done already
if (!logFileWritten)
{
// set the daily records for derived values
SetExtraValues(date);
LogFile.WriteLogFile();
LogFile.Initialise();
// save the extra log file
ExtraLogFile.WriteLogFile();
ExtraLogFile.Initialise();
}
}
// save dayfile
DayFile.WriteDayFile();
// summary
LogConsole($"Wrote {DayFile.Records.Count} records to the day file", ConsoleColor.Green);
LogMessage($"Wrote {DayFile.Records.Count} records to the day file");
LogConsole("Finished", ConsoleColor.Green);
LogMessage("Finished");
}
public static void LogMessage(string message)
{
Trace.TraceInformation(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff ") + message);
}
public static void LogDebugMessage(string message)
{
#if DEBUG
// Trace.TraceInformation(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff ") + message);
#endif
}
public static void LogConsole(string msg, ConsoleColor colour, bool newLine = true)
{
Console.ForegroundColor = colour;
if (newLine)
{
Console.WriteLine(msg);
}
else
{
Console.Write(msg);
}
Console.ForegroundColor = defConsoleColour;
}
private static void SetExtraValues(DateTime date)
{
if (!DayFile.Records.TryGetValue(date, out var value))
{
LogConsole($"No summary data for {date:yyyy-MM-dd}, cannot create day file entry. Try using CreateMissing after this import", ConsoleColor.Red);
return;
}
// set the daily records for derived values
if (LogFile.HumidexHigh > -9999)
{
value.HighHumidex = LogFile.HumidexHigh;
value.HighHumidexTime = LogFile.HumidexHighTime;
}
if (LogFile.ApparentHigh > -9999)
{
value.HighAppTemp = LogFile.ApparentHigh;
value.HighAppTempTime = LogFile.ApparentHighTime;
}
if (LogFile.ApparentLow < 9999)
{
value.LowAppTemp = LogFile.ApparentLow;
value.LowAppTempTime = LogFile.ApparentLowTime;
}
if (LogFile.FeelsLikeHigh > -9999)
{
value.HighFeelsLike = LogFile.FeelsLikeHigh;
value.HighFeelsLikeTime = LogFile.FeelsLikeHighTime;
}
if (LogFile.FeelsLikeLow < 9999)
{
value.LowFeelsLike = LogFile.FeelsLikeLow;
value.LowFeelsLikeTime = LogFile.FeelsLikeLowTime;
}
if (LogFile.WindAvgHigh > -9999)
{
value.HighAvgWind = LogFile.WindAvgHigh;
value.HighAvgWindTime = LogFile.WindAvgHighTime;
}
}
}
}