Skip to content
This repository has been archived by the owner on Jan 8, 2025. It is now read-only.

Commit

Permalink
Merge pull request #4 from 2nd-Semester-Project/SE2SP-2-Source-Data-M…
Browse files Browse the repository at this point in the history
…anager

Se2 sp 2 source data manager
  • Loading branch information
svenons authored Mar 14, 2024
2 parents 1b44435 + 2a52a8f commit a316f6d
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 4 deletions.
58 changes: 56 additions & 2 deletions HeatOptimiser/Classes/SourceDataManager.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,64 @@
using OfficeOpenXml; // dotnet add package EPPlus
using System.Globalization;

namespace HeatOptimiser
{
public class SourceDataPoint
{
public DateTime? TimeFrom;
public DateTime? TimeTo;
public double? HeatDemand;
public double? ElectricityPrice;
}
public class SourceData {
public List<SourceDataPoint> SummerData;
public List<SourceDataPoint> WinterData;
public SourceData(){
SourceDataManager sourceManager = new SourceDataManager();
SummerData = sourceManager.LoadXLSXFile("data/sourcedata.xlsx", 4, 2);
WinterData = sourceManager.LoadXLSXFile("data/sourcedata.xlsx", 4, 7);
}
}
public class SourceDataManager : ISourceDataManager
{
public SourceDataManager()
// Example usage: List<SourceDataPoint> SourceList = SourceManager.LoadXLSXFile("data/sourcedata.xlsx", 4, 2); Columns and Rows start with 1!!!!
public List<SourceDataPoint> LoadXLSXFile(string file, int rowStart, int columnStart, int workSheetNumber = 0)
{
throw new System.NotImplementedException();
var sourceList = new List<SourceDataPoint>();

ExcelPackage.LicenseContext = LicenseContext.NonCommercial; // EPPlus license

using (var package = new ExcelPackage(new FileInfo(file)))
{
var worksheet = package.Workbook.Worksheets[workSheetNumber];

if (worksheet.Dimension == null)
{
Console.WriteLine("The worksheet is empty.");
return sourceList;
}
for (int row = rowStart; row <= worksheet.Dimension.End.Row; row++)
{
try {
DateTime temp;
string[] formats = { "dd/MM/yyyy HH.mm.ss", "dd/MM/yyyy HH:mm:ss", "HH.mm.ss", "HH:mm:ss" };
SourceDataPoint sourceData = new SourceDataPoint
{
TimeFrom = DateTime.TryParseExact(worksheet.Cells[row, columnStart].Value?.ToString(), formats, CultureInfo.InvariantCulture, DateTimeStyles.None, out temp) ? temp : (DateTime?)null,
TimeTo = DateTime.TryParseExact(worksheet.Cells[row, columnStart + 1].Value?.ToString(), formats, CultureInfo.InvariantCulture, DateTimeStyles.None, out temp) ? temp : (DateTime?)null,
HeatDemand = worksheet.Cells[row, columnStart + 2]?.Value != null ? double.Parse(worksheet.Cells[row, columnStart + 2].Value.ToString()!) : null,
ElectricityPrice = worksheet.Cells[row, columnStart + 3]?.Value != null ? double.Parse(worksheet.Cells[row, columnStart + 3].Value.ToString()!) : null
};
sourceList.Add(sourceData);
}
catch (Exception e)
{
Console.WriteLine($"Error: {e}");
}
}
}

return sourceList;
}
}
}
4 changes: 4 additions & 0 deletions HeatOptimiser/HeatOptimiser.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,8 @@
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="EPPlus" Version="7.0.10" />
</ItemGroup>

</Project>
2 changes: 1 addition & 1 deletion HeatOptimiser/Interfaces/ISourceDataManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ namespace HeatOptimiser
{
public interface ISourceDataManager
{
//Lizard do you see this
public List<SourceDataPoint> LoadXLSXFile(string filePath, int rowStartd, int columnStart, int workSheetNumber);
}
}
2 changes: 1 addition & 1 deletion HeatOptimiser/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ class Program {
public static void Main()
{
new TextBasedUI().example();
}
}
}
}
6 changes: 6 additions & 0 deletions HeatOptimiser/UserInterfaces/Textbased.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ public class TextBasedUI: IUserInterface {
public void example()
{
Console.WriteLine("Hello, GitHub!");

SourceData sourceData = new SourceData();
Console.WriteLine();
Console.WriteLine("Here comes the stats from SourceDataManager data:");
Console.WriteLine($"Summer data contains {sourceData.SummerData.Count} entries.");
Console.WriteLine($"Winter data contains {sourceData.WinterData.Count} entries.");
}
}
}
Binary file added HeatOptimiser/data/sourcedata.xlsx
Binary file not shown.

0 comments on commit a316f6d

Please sign in to comment.