-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCustomLogFile.cs
140 lines (114 loc) · 3.31 KB
/
CustomLogFile.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
using System.Globalization;
using System.Text;
namespace ImportWC
{
static partial class CustomLogFile
{
private static readonly SortedList<DateTime, CustomLogFileRec> records = [];
public static DateTime LastTimeStamp { get; set; }
public static int RecordsCount { get => records.Count; }
internal static void Initialise()
{
records.Clear();
LastTimeStamp = DateTime.MinValue;
}
internal static void AddRecord(WeatherCatRecord rec)
{
LastTimeStamp = rec.Timestamp;
if (!records.TryGetValue(rec.Timestamp, out var value))
{
value = new CustomLogFileRec() { LogTime = rec.Timestamp};
records.Add(rec.Timestamp, value);
}
// Synthetic Channel 1-10
for (var i = 0; i < 10; i++)
{
value.Synth[i] = rec.Synth[i];
}
}
public static void WriteLogFile()
{
if (records.Count == 0)
{
Program.LogMessage("No records to write to Extra Log file!");
return;
}
var logfilename = "data" + Path.DirectorySeparatorChar + GetCustomLogFileName(records.First().Key);
Program.LogMessage($"Writing {records.Count} to {logfilename}");
Program.LogConsole($" Writing to {logfilename}", ConsoleColor.Gray);
// backup old logfile
if (File.Exists(logfilename))
{
if (!File.Exists(logfilename + ".sav"))
{
File.Move(logfilename, logfilename + ".sav");
}
else
{
var i = 1;
do
{
if (!File.Exists(logfilename + ".sav" + i))
{
File.Move(logfilename, logfilename + ".sav" + i);
break;
}
else
{
i++;
}
} while (true);
}
}
try
{
using FileStream fs = new FileStream(logfilename, FileMode.Append, FileAccess.Write, FileShare.Read);
using StreamWriter file = new StreamWriter(fs);
Program.LogMessage($"{logfilename} opened for writing {records.Count} records");
foreach (var rec in records)
{
var line = RecToCsv(rec);
if (null != line)
file.WriteLine(line);
}
file.Close();
Program.LogMessage($"{logfilename} write complete");
}
catch (Exception ex)
{
Program.LogMessage($"Error writing to {logfilename}: {ex.Message}");
}
}
public static string RecToCsv(KeyValuePair<DateTime, CustomLogFileRec> keyval)
{
// Writes an entry to the n-minute extralogfile. Fields are comma-separated:
// 0 Date in the form dd/mm/yy (the slash may be replaced by a dash in some cases)
// 1 Current time - hh:mm
// 2-11 Synthetic Channel 1-10
var rec = keyval.Value;
// make sure solar max is calculated for those stations without a solar sensor
Program.LogDebugMessage("DoCustomLogFile: Writing log entry for " + rec.LogTime);
var inv = CultureInfo.InvariantCulture;
var sep = ',';
var sb = new StringBuilder(256);
sb.Append(rec.LogTime.ToString("dd/MM/yy", inv) + sep);
sb.Append(rec.LogTime.ToString("HH:mm", inv) + sep);
// Sythetic channel 1-10
for (int i = 0; i < 10; i++)
{
var v = rec.Synth[i] ?? -99999;
sb.Append((v > -99999 ? v.ToString("F1", inv) : string.Empty) + sep);
}
return sb.ToString();
}
private static string GetCustomLogFileName(DateTime thedate)
{
return "SythLog" + thedate.ToString("yyyyMM") + ".txt";
}
}
internal class CustomLogFileRec
{
public DateTime LogTime;
public double?[] Synth { get; set; } = new double?[10];
}
}