Skip to content

Commit

Permalink
Merge pull request #15 from mcrossley/main
Browse files Browse the repository at this point in the history
v1.9.1
  • Loading branch information
mcrossley authored Aug 9, 2023
2 parents c1766cf + 3825f7b commit d4f78e2
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 44 deletions.
5 changes: 4 additions & 1 deletion ExportToMySQL.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@
<WarningLevel>4</WarningLevel>
<Prefer32Bit>false</Prefer32Bit>
</PropertyGroup>
<PropertyGroup>
<StartupObject />
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Core" />
Expand Down Expand Up @@ -76,7 +79,7 @@
</ItemGroup>
<ItemGroup>
<PackageReference Include="MySqlConnector">
<Version>2.2.5</Version>
<Version>2.2.7</Version>
</PackageReference>
<PackageReference Include="System.Diagnostics.DiagnosticSource">
<Version>7.0.2</Version>
Expand Down
103 changes: 62 additions & 41 deletions Program.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Data.SqlTypes;
using System.Globalization;
using System.IO;
using System.Text;
Expand All @@ -26,7 +27,7 @@ private static void Main(string[] args)
if (args.Length == 0)
{
Console.WriteLine("Specify 'dayfile', 'monthly', or the path to a monthly log file");
Environment.Exit(0);
Environment.Exit(1);
}
else
{
Expand All @@ -37,7 +38,7 @@ private static void Main(string[] args)
if (!File.Exists("Cumulus.ini"))
{
Console.WriteLine("Cannot find Cumulus.ini");
Environment.Exit(0);
Environment.Exit(1);
}

IniFile ini = new IniFile("Cumulus.ini");
Expand All @@ -54,27 +55,6 @@ private static void Main(string[] args)
Database = ini.GetValue("MySQL", "Database", "database")
};

if (File.Exists("strings.ini"))
{
IniFile iniStrs = new IniFile("strings.ini");
compassp[0] = iniStrs.GetValue("Compass", "N", "N");
compassp[1] = iniStrs.GetValue("Compass", "NNE", "NNE");
compassp[2] = iniStrs.GetValue("Compass", "NE", "NE");
compassp[3] = iniStrs.GetValue("Compass", "ENE", "ENE");
compassp[4] = iniStrs.GetValue("Compass", "E", "E");
compassp[5] = iniStrs.GetValue("Compass", "ESE", "ESE");
compassp[6] = iniStrs.GetValue("Compass", "SE", "SE");
compassp[7] = iniStrs.GetValue("Compass", "SSE", "SSE");
compassp[8] = iniStrs.GetValue("Compass", "S", "S");
compassp[9] = iniStrs.GetValue("Compass", "SSW", "SSW");
compassp[10] = iniStrs.GetValue("Compass", "SW", "SW");
compassp[11] = iniStrs.GetValue("Compass", "WSW", "WSW");
compassp[12] = iniStrs.GetValue("Compass", "W", "W");
compassp[13] = iniStrs.GetValue("Compass", "WNW", "WNW");
compassp[14] = iniStrs.GetValue("Compass", "NW", "NW");
compassp[15] = iniStrs.GetValue("Compass", "NNW", "NNW");
}

try
{
mySqlConn = new MySqlConnection(ConnString.ToString());
Expand All @@ -83,14 +63,9 @@ private static void Main(string[] args)
{
Console.WriteLine("Error encountered opening MySQL connection");
Console.WriteLine(ex.Message);
Environment.Exit(0);
Environment.Exit(1);
}

cmd = new MySqlCommand
{
Connection = mySqlConn
};

try
{
mySqlConn.Open();
Expand All @@ -99,9 +74,34 @@ private static void Main(string[] args)
{
Console.WriteLine("Error encountered opening MySQL connection");
Console.WriteLine(ex.Message);
Environment.Exit(0);
Environment.Exit(1);
}

cmd = new MySqlCommand
{
Connection = mySqlConn
};

if (File.Exists("strings.ini"))
{
IniFile iniStrs = new IniFile("strings.ini");
compassp[0] = iniStrs.GetValue("Compass", "N", "N");
compassp[1] = iniStrs.GetValue("Compass", "NNE", "NNE");
compassp[2] = iniStrs.GetValue("Compass", "NE", "NE");
compassp[3] = iniStrs.GetValue("Compass", "ENE", "ENE");
compassp[4] = iniStrs.GetValue("Compass", "E", "E");
compassp[5] = iniStrs.GetValue("Compass", "ESE", "ESE");
compassp[6] = iniStrs.GetValue("Compass", "SE", "SE");
compassp[7] = iniStrs.GetValue("Compass", "SSE", "SSE");
compassp[8] = iniStrs.GetValue("Compass", "S", "S");
compassp[9] = iniStrs.GetValue("Compass", "SSW", "SSW");
compassp[10] = iniStrs.GetValue("Compass", "SW", "SW");
compassp[11] = iniStrs.GetValue("Compass", "WSW", "WSW");
compassp[12] = iniStrs.GetValue("Compass", "W", "W");
compassp[13] = iniStrs.GetValue("Compass", "WNW", "WNW");
compassp[14] = iniStrs.GetValue("Compass", "NW", "NW");
compassp[15] = iniStrs.GetValue("Compass", "NNW", "NNW");
}

if (param.ToLower().Equals("dayfile"))
{
Expand Down Expand Up @@ -130,28 +130,38 @@ private static void Main(string[] args)

private static void DoSingleMonthlyExport(string filename)
{
Console.WriteLine("Processing file:" + filename);

var StartOfMonthlyInsertSQL = "INSERT IGNORE INTO " + MySqlMonthlyTable + " (LogDateTime,Temp,Humidity,Dewpoint,Windspeed,Windgust,Windbearing,RainRate,TodayRainSoFar,Pressure,Raincounter,InsideTemp,InsideHumidity,LatestWindGust,WindChill,HeatIndex,UVindex,SolarRad,Evapotrans,AnnualEvapTran,ApparentTemp,MaxSolarRad,HrsSunShine,CurrWindBearing,RG11rain,RainSinceMidnight,FeelsLike,Humidex,WindbearingSym,CurrWindBearingSym)";

using (var sr = new StreamReader(filename))
{
const int MaxBatchSize = 1000;
StringBuilder sb = new StringBuilder("", MaxBatchSize * 2100);

int linenum = 0;
var linenum = 0;
var line = string.Empty;

do
{
sb.Clear();
sb.Append(StartOfMonthlyInsertSQL + " Values ");
sb.Append(StartOfMonthlyInsertSQL + " VALUES ");

// now process each record in the file
try
{
for (int a = 0; a < MaxBatchSize && !(sr.EndOfStream); a++)
{
var line = sr.ReadLine();
line = sr.ReadLine();
linenum++;
var st = new List<string>(Regex.Split(line, CultureInfo.CurrentCulture.TextInfo.ListSeparator));

if (st.Count < 16)
{
Console.WriteLine($"Error: Line {linenum} is too short. Detected {st.Count} fields present, but 16 is the minimum");
continue;
}

var logfiledate = st[0];
// 01234567
// dd/mm/yy
Expand Down Expand Up @@ -202,9 +212,12 @@ private static void DoSingleMonthlyExport(string filename)
}
catch (Exception ex)
{
Console.WriteLine("Error processing line " + linenum);
Console.WriteLine(ex.Message);
}
} while (!(sr.EndOfStream));
Console.WriteLine("SQL = " + sb.ToString());
Console.WriteLine("Src = " + line + "\n");
}
} while (!(sr.EndOfStream));
}
}

Expand Down Expand Up @@ -239,18 +252,20 @@ private static void DoDayfileExport()
Console.WriteLine("Dayfile exists, beginning export");
string StartOfDayfileInsertSQL = "INSERT IGNORE INTO " + MySqlDayfileTable + " (LogDate,HighWindGust,HWindGBear,THWindG,MinTemp,TMinTemp,MaxTemp,TMaxTemp,MinPress,TMinPress,MaxPress,TMaxPress,MaxRainRate,TMaxRR,TotRainFall,AvgTemp,TotWindRun,HighAvgWSpeed,THAvgWSpeed,LowHum,TLowHum,HighHum,THighHum,TotalEvap,HoursSun,HighHeatInd,THighHeatInd,HighAppTemp,THighAppTemp,LowAppTemp,TLowAppTemp,HighHourRain,THighHourRain,LowWindChill,TLowWindChill,HighDewPoint,THighDewPoint,LowDewPoint,TLowDewPoint,DomWindDir,HeatDegDays,CoolDegDays,HighSolarRad,THighSolarRad,HighUV,THighUV,MaxFeelsLike,TMaxFeelsLike,MinFeelsLike,TMinFeelsLike,MaxHumidex,TMaxHumidex,ChillHours,HighRain24h,THighRain24h,HWindGBearSym,DomWindDirSym)";

int linenum = 0;
var linenum = 0;
var line = string.Empty;

using (var sr = new StreamReader(filename))
{
Console.WriteLine("Dayfile opened");

do
{
// now process each record in the file
try
{
var line = sr.ReadLine();
// now process each record in the file
StringBuilder sb = new StringBuilder(StartOfDayfileInsertSQL + " Values(");
try
{
line = sr.ReadLine();
linenum++;
var st = new List<string>(Regex.Split(line, CultureInfo.CurrentCulture.TextInfo.ListSeparator));

Expand All @@ -262,7 +277,7 @@ private static void DoDayfileExport()

Console.Write(sqldate + "\r");

StringBuilder sb = new StringBuilder(StartOfDayfileInsertSQL + " Values('" + sqldate + "',");
sb.Append($"'{sqldate}',");

for (int i = 1; i < 55; i++)
{
Expand Down Expand Up @@ -295,13 +310,19 @@ private static void DoDayfileExport()
catch (Exception ex)
{
Console.WriteLine(ex.Message);
Console.WriteLine("SQL = " + sb.ToString());
Console.WriteLine("Src = " + line + "\n");
}
} while (!(sr.EndOfStream));
}

Console.WriteLine();
Console.WriteLine(linenum+" entries processed");
}
else
{
Console.WriteLine("Dafile not found - " + filename);
}
}

private static string CompassPoint(int bearing)
Expand Down
4 changes: 2 additions & 2 deletions Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,5 @@
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.8.1.0")]
[assembly: AssemblyFileVersion("1.8.1.0")]
[assembly: AssemblyVersion("1.9.1.0")]
[assembly: AssemblyFileVersion("1.9.1.0")]

0 comments on commit d4f78e2

Please sign in to comment.