From 74cec37a79bda4b89e47d45dc64b613ddc4b2d4e Mon Sep 17 00:00:00 2001 From: Mark Crossley <1196094+mcrossley@users.noreply.github.com> Date: Mon, 22 Aug 2022 13:11:46 +0100 Subject: [PATCH 1/4] Console message tidying --- CreateMissing.csproj | 4 +-- DayFile.cs | 17 +++++----- Program.cs | 75 ++++++++++++++++++++++++++++++-------------- 3 files changed, 63 insertions(+), 33 deletions(-) diff --git a/CreateMissing.csproj b/CreateMissing.csproj index 375b02a..4658d7d 100644 --- a/CreateMissing.csproj +++ b/CreateMissing.csproj @@ -3,8 +3,8 @@ Exe v4.5.2 - 1.3.0.1 - 1.3.0.1 + 1.3.1.2 + 1.3.1.2 CreateMissing.Program net48 diff --git a/DayFile.cs b/DayFile.cs index e683f20..8548f39 100644 --- a/DayFile.cs +++ b/DayFile.cs @@ -25,8 +25,8 @@ public DayFile() if (File.Exists(dayFileName + ".sav")) { - Console.WriteLine("The dayfile.txt backup file dayfile.txt.sav already exists, aborting to prevent overwriting the original data."); - Console.WriteLine("Press any key to exit"); + Program.LogConsole("The dayfile.txt backup file dayfile.txt.sav already exists, aborting to prevent overwriting the original data.", ConsoleColor.Cyan); + Program.LogConsole("Press any key to exit", ConsoleColor.DarkYellow); Console.ReadKey(true); Console.WriteLine("Exiting..."); Environment.Exit(1); @@ -77,7 +77,8 @@ public void LoadDayFile() if (newRec.Date < lastDate) { Program.LogMessage($"LoadDayFile: Error - Date is out of order at line {linenum} of {dayFileName}, '{newRec.Date.ToString("dd" + DateSep + "MM" + DateSep + "yy")}'"); - Console.WriteLine($"\nError, date is out of order at line {linenum} of {dayFileName}, '{newRec.Date.ToString("dd" + DateSep + "MM" + DateSep + "yy")}'"); + Console.WriteLine(); + Program.LogConsole($"Error, date is out of order at line {linenum} of {dayFileName}, '{newRec.Date.ToString("dd" + DateSep + "MM" + DateSep + "yy")}'", ConsoleColor.Red); Environment.Exit(3); } @@ -88,7 +89,8 @@ public void LoadDayFile() if (newRec.Date == lastDate) { Program.LogMessage($"LoadDayFile: Error - Duplicate date at line {linenum} of {dayFileName}, '{newRec.Date.ToString("dd" + DateSep + "MM" + DateSep + "yy")}'"); - Console.WriteLine($"\nError, duplicate date at line {linenum} of {dayFileName}, '{newRec.Date.ToString("dd" + DateSep + "MM" + DateSep + "yy")}'"); + Console.WriteLine(); + Program.LogConsole($"Error, duplicate date at line {linenum} of {dayFileName}, '{newRec.Date.ToString("dd" + DateSep + "MM" + DateSep + "yy")}'", ConsoleColor.Red); Environment.Exit(4); } @@ -106,8 +108,9 @@ public void LoadDayFile() if (errorCount >= 20) { Program.LogMessage($"LoadDayFile: Too many errors reading {dayFileName} - aborting load of daily data"); - Console.WriteLine($"Too many errors reading {dayFileName} - aborting load of daily data"); - Console.WriteLine("Please see the log file for more details"); + Console.WriteLine(); + Program.LogConsole($"Too many errors reading {dayFileName} - aborting load of daily data", ConsoleColor.Red); + Program.LogConsole("Please see the log file for more details", ConsoleColor.Red); Environment.Exit(5); } } @@ -125,7 +128,7 @@ public void LoadDayFile() else { Program.LogMessage("LoadDayFile: No Dayfile found - No entries added to recent daily data list"); - Console.WriteLine("No Dayfile found - No entries added to recent daily data list"); + Program.LogConsole("No Dayfile found - No entries added to recent daily data list", ConsoleColor.Cyan); // add a rcord for yesterday, just so we have something to process, // if it is left at default we will not write it out var newRec = new Dayfilerec diff --git a/Program.cs b/Program.cs index fdf7621..3a6562d 100644 --- a/Program.cs +++ b/Program.cs @@ -12,6 +12,7 @@ class Program { public static Cumulus cumulus; public static string location; + public static ConsoleColor defConsoleColour; private static DayFile dayfile; private static readonly List CurrentLogLines = new List(); @@ -35,13 +36,17 @@ static void Main() Trace.Listeners.Add(myTextListener); Trace.AutoFlush = true; + defConsoleColour = Console.ForegroundColor; + var fullVer = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version; var version = $"{fullVer.Major}.{fullVer.Minor}.{fullVer.Build}"; LogMessage("CreateMissing v." + version); Console.WriteLine("CreateMissing v." + version); LogMessage("Processing started"); - Console.WriteLine($"\nProcessing started: {DateTime.Now:U}\n"); + 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; @@ -59,7 +64,8 @@ static void Main() LogMessage($"First dayfile record: {dayfileStart:d}"); LogMessage($"Records Began Date : {currDate:d}"); Console.WriteLine($"First dayfile record: {dayfileStart:d}"); - Console.WriteLine($"Records Began Date : {currDate:d}\n"); + Console.WriteLine($"Records Began Date : {currDate:d}"); + Console.WriteLine(); // Sanity check #1. Is the first date in the day file order than the records began date? if (dayfileStart < currDate) @@ -80,8 +86,8 @@ static void Main() if (currDate >= DateTime.Today) { LogMessage("Start date is today!???"); - Console.WriteLine("Start date is today!???"); - Console.WriteLine("Press any key to exit"); + LogConsole("Start date is today!???", ConsoleColor.Cyan); + LogConsole("Press any key to exit", ConsoleColor.DarkYellow); Console.ReadKey(true); Console.WriteLine("Exiting..."); @@ -124,14 +130,14 @@ static void Main() if (newRec == null) { LogMessage($"Date: {currDate:d} : No monthly data was found, not creating a record"); - Console.WriteLine($"\nDate: {currDate:d} : No monthly data was found, not creating a record"); + LogConsole("No monthly data was found, not creating a record", ConsoleColor.Yellow); RecsNoData++; } else { newRec = GetSolarDayRecFromMonthly(currDate, newRec); dayfile.DayfileRecs.Insert(i, newRec); - Console.WriteLine(" done."); + LogConsole("done.", ConsoleColor.Green); RecsAdded++; i++; } @@ -167,7 +173,8 @@ static void Main() else { LogMessage($"Date: {currDate:d} : Entry is OK"); - Console.WriteLine($"Date: {currDate:d} : Entry is OK"); + Console.Write($"Date: {currDate:d} : "); + LogConsole("Entry is OK", ConsoleColor.Green); RecsOK++; } } @@ -209,14 +216,14 @@ static void Main() if (newRec == null) { LogMessage($"Date: {currDate:d} : No monthly data was found, not creating a record"); - Console.WriteLine($"\nDate: {currDate:d} : No monthly data was found, not creating a record"); + LogConsole("No monthly data was found, not creating a record", ConsoleColor.Yellow); RecsNoData++; } else { newRec = GetSolarDayRecFromMonthly(currDate, newRec); dayfile.DayfileRecs.Add(newRec); - Console.WriteLine(" done."); + LogConsole("done.", ConsoleColor.Green); RecsAdded++; } currDate = IncrementMeteoDate(currDate); @@ -230,7 +237,8 @@ static void Main() // create the new dayfile.txt with a different name LogMessage("Saving new dayfile.txt"); - Console.WriteLine("\nSaving new dayfile.txt"); + Console.WriteLine(); + Console.WriteLine("Saving new dayfile.txt"); dayfile.WriteDayFile(); @@ -242,23 +250,26 @@ static void Main() LogMessage($"Number of records No Data: {RecsNoData}"); LogMessage($"Number of records were OK: {RecsOK}"); - Console.WriteLine($"\nNumber of records processed: {RecsAdded + RecsUpdated + RecsNoData + RecsOK}"); + Console.WriteLine(); + Console.WriteLine($"Number of records processed: {RecsAdded + RecsUpdated + RecsNoData + RecsOK}"); + Console.WriteLine($" Were OK: {RecsOK}"); Console.WriteLine($" Added : {RecsAdded}"); Console.WriteLine($" Updated: {RecsUpdated}"); - Console.Write( $" No Data: {RecsNoData}"); + LogConsole( $" No Data: {RecsNoData}", ConsoleColor.Red, false); if (RecsNoData > 0) { - Console.WriteLine(" - please check the log file for the errors"); + LogConsole(" - please check the log file for the errors", ConsoleColor.Cyan); } else { Console.WriteLine(); } - Console.WriteLine($" Were OK: {RecsOK}"); - LogMessage("\nProcessing complete."); - Console.WriteLine("\n\nProcessing complete."); - Console.WriteLine("Press any key to exit"); + LogMessage("Processing complete."); + Console.WriteLine(); + Console.WriteLine(); + Console.WriteLine("Processing complete."); + LogConsole("Press any key to exit", ConsoleColor.DarkYellow); Console.ReadKey(true); } @@ -267,6 +278,22 @@ public static void LogMessage(string message) Trace.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff ") + message); } + 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 Dayfilerec GetDayRecFromMonthly(DateTime date) { var rec = new Dayfilerec() @@ -862,8 +889,8 @@ private static Dayfilerec GetDayRecFromMonthly(DateTime date) { LogMessage($"LogFile: Error at line {CurrentLogLineNum}, field {idx + 1} of {fileName} : {e.Message}"); LogMessage("LogFile: Please edit the file to correct the error"); - Console.WriteLine($"Error at line {CurrentLogLineNum}, field {idx + 1} of {fileName} : {e.Message}"); - Console.WriteLine("Please edit the file to correct the error"); + LogConsole($"Error at line {CurrentLogLineNum}, field {idx + 1} of {fileName} : {e.Message}", ConsoleColor.Red); + LogConsole("Please edit the file to correct the error", ConsoleColor.Red); Environment.Exit(1); } @@ -1015,8 +1042,8 @@ private static Dayfilerec GetSolarDayRecFromMonthly(DateTime date, Dayfilerec re { LogMessage($"Solar: Error at line {CurrentSolarLogLineNum + 1} of {fileName} : {e.Message}"); LogMessage("Solar: Please edit the file to correct the error"); - Console.WriteLine($"Error at line {CurrentSolarLogLineNum + 1} of {fileName} : {e.Message}"); - Console.WriteLine("Please edit the file to correct the error"); + LogConsole($"Error at line {CurrentSolarLogLineNum + 1} of {fileName} : {e.Message}", ConsoleColor.Red); + LogConsole("Please edit the file to correct the error", ConsoleColor.Red); Environment.Exit(1); } @@ -1055,7 +1082,7 @@ private static void AddMissingData(int idx, DateTime metDate) { RecsNoData++; LogMessage($"{metDate:d} : No monthly data was found, not updating this record"); - Console.WriteLine($"\n{metDate:d} : No monthly data was found, not updating this record"); + LogConsole("No monthly data was found, not updating this record", ConsoleColor.Yellow); return; } @@ -1208,7 +1235,7 @@ private static void AddMissingData(int idx, DateTime metDate) dayfile.DayfileRecs[idx].HighRain24h = newRec.HighRain24h; dayfile.DayfileRecs[idx].HighRain24hTime = newRec.HighRain24hTime; } - Console.WriteLine("done."); + LogConsole("done.", ConsoleColor.Green); RecsUpdated++; } @@ -1323,7 +1350,7 @@ private static void AddLastHoursRainEntry(DateTime ts, double rain, ref Queue Date: Tue, 20 Dec 2022 17:36:00 +0000 Subject: [PATCH 2/4] Now skips (with a warning in the diags log) blank lines in the monthly log files --- CreateMissing.csproj | 4 ++-- Program.cs | 25 +++++++++++++++++++++---- Updates.txt | 4 ++++ 3 files changed, 27 insertions(+), 6 deletions(-) diff --git a/CreateMissing.csproj b/CreateMissing.csproj index 4658d7d..2bbc4e6 100644 --- a/CreateMissing.csproj +++ b/CreateMissing.csproj @@ -3,8 +3,8 @@ Exe v4.5.2 - 1.3.1.2 - 1.3.1.2 + 1.3.1.3 + 1.3.2.3 CreateMissing.Program net48 diff --git a/Program.cs b/Program.cs index 3a6562d..9e86be1 100644 --- a/Program.cs +++ b/Program.cs @@ -12,7 +12,8 @@ class Program { public static Cumulus cumulus; public static string location; - public static ConsoleColor defConsoleColour; + + private static ConsoleColor defConsoleColour; private static DayFile dayfile; private static readonly List CurrentLogLines = new List(); @@ -409,6 +410,14 @@ private static Dayfilerec GetDayRecFromMonthly(DateTime date) // we use idx 0 & 1 together (date/time), set the idx to 1 idx = 1; // process each record in the file + // first a sanity check for an empty line! + if (string.IsNullOrWhiteSpace(CurrentLogLines[CurrentLogLineNum])) + { + CurrentLogLineNum++; + LogMessage($"LogFile: Error at line {CurrentLogLineNum}, an empty line was detected!"); + continue; + } + //var st = new List(Regex.Split(line, CultureInfo.CurrentCulture.TextInfo.ListSeparator)); // Regex is very expensive, let's assume the separator is always a single character var st = new List(CurrentLogLines[CurrentLogLineNum++].Split(dayfile.FieldSep[0])); @@ -831,7 +840,7 @@ private static Dayfilerec GetDayRecFromMonthly(DateTime date) raintoday += (raincounter - lastentrycounter) * cumulus.CalibRainMult; } - // add last hours rain for this last record. + // add last hours rain for this last record. AddLastHoursRainEntry(entrydate, totalRainfall + raintoday, ref rain1hLog, ref rain24hLog); // rainfall in last hour @@ -858,8 +867,8 @@ private static Dayfilerec GetDayRecFromMonthly(DateTime date) finished = true; } } - - + + if (started && recCount >= 5) // need at least five records to create a day { // we were in the right day, now we aren't @@ -1014,6 +1023,14 @@ private static Dayfilerec GetSolarDayRecFromMonthly(DateTime date, Dayfilerec re while (CurrentSolarLogLineNum < CurrentSolarLogLines.Count) { // process each record in the file + // first a sanity check for an empty line! + if (string.IsNullOrWhiteSpace(CurrentLogLines[CurrentLogLineNum])) + { + CurrentLogLineNum++; + LogMessage($"Solar: Error at line {CurrentLogLineNum}, an empty line was detected!"); + continue; + } + // Regex is very expensive, let's assume the separator is always a single character var st = new List(CurrentSolarLogLines[CurrentSolarLogLineNum].Split(dayfile.FieldSep[0])); var entrydate = Utils.DdmmyyhhmmStrToDate(st[0], st[1]); diff --git a/Updates.txt b/Updates.txt index adfe7e8..0ff5b5b 100644 --- a/Updates.txt +++ b/Updates.txt @@ -1,3 +1,7 @@ +1.3.1 +————— +- Now skips (with a warning in the diags log) blank lines in the monthly log files + 1.3.0 ————— - Support for CMX v3.20.0 - adds 24 hour rainfall From 92e1fd88c786ebadf39fd94347d00b30a2dcf9f5 Mon Sep 17 00:00:00 2001 From: Mark Crossley Date: Tue, 20 Dec 2022 17:40:10 +0000 Subject: [PATCH 3/4] fix file version --- CreateMissing.csproj | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CreateMissing.csproj b/CreateMissing.csproj index 2bbc4e6..2e91356 100644 --- a/CreateMissing.csproj +++ b/CreateMissing.csproj @@ -4,7 +4,7 @@ Exe v4.5.2 1.3.1.3 - 1.3.2.3 + 1.3.1.3 CreateMissing.Program net48 From 0ffe1c9c8e2d730903ab79d451d079d7a4a80adf Mon Sep 17 00:00:00 2001 From: Mark Crossley Date: Wed, 9 Aug 2023 11:42:59 +0100 Subject: [PATCH 4/4] Add post build tasks --- CreateMissing.csproj | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/CreateMissing.csproj b/CreateMissing.csproj index 2e91356..31315a3 100644 --- a/CreateMissing.csproj +++ b/CreateMissing.csproj @@ -16,4 +16,8 @@ DEBUG;TRACE + + + +