Skip to content

Commit

Permalink
Adapted Conversion Logic to new file interfaces
Browse files Browse the repository at this point in the history
  • Loading branch information
Penny79 committed Jun 29, 2017
1 parent b8663b8 commit 34459b9
Show file tree
Hide file tree
Showing 10 changed files with 342 additions and 460 deletions.
40 changes: 21 additions & 19 deletions HkwgConverter/Core/InboundConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,18 +36,18 @@ public InboundConverter(WorkflowStore store, Settings config, BusinessConfigurat

#region private methods

private List<HkwgInputItem> ReadCsvFile(string fileName)
private List<CsvLineItem> ReadCsvFile(string fileName)
{
var lines = File.ReadAllLines(fileName)
.Skip(2)
.Select(x => x.Split(';'))
.Select(x => new HkwgInputItem()
.Select(x => new CsvLineItem()
{
Time = x[0],
FPLast = decimal.Parse(x[1], System.Globalization.CultureInfo.InvariantCulture),
FlexPos = decimal.Parse(x[2], System.Globalization.CultureInfo.InvariantCulture),
FlexNeg = decimal.Parse(x[3], System.Globalization.CultureInfo.InvariantCulture),
MarginalCost = decimal.Parse(x[4], System.Globalization.CultureInfo.InvariantCulture)
FlexPosDemand = decimal.Parse(x[1], System.Globalization.CultureInfo.InvariantCulture),
FlexPosMarginalCost = decimal.Parse(x[2], System.Globalization.CultureInfo.InvariantCulture),
FlexNegDemand = decimal.Parse(x[3], System.Globalization.CultureInfo.InvariantCulture),
FlexNegMarginalCost = decimal.Parse(x[4], System.Globalization.CultureInfo.InvariantCulture)
});

return lines.ToList();
Expand All @@ -58,29 +58,31 @@ private List<HkwgInputItem> ReadCsvFile(string fileName)
/// </summary>
/// <param name="content"></param>
/// <returns></returns>
private List<HkwgInputItem> Transform(List<HkwgInputItem> content)
private List<CsvLineItem> Transform(List<CsvLineItem> content)
{
for (int i = 0; i < content.Count; i+=4)
{

var quarterhours = content.Skip(i).Take(4);

var avgflexPos = quarterhours.Average(x => x.FlexPos);
var avgflexNeg = quarterhours.Average(x => x.FlexNeg);
var avgCost = quarterhours.Average(x => x.MarginalCost);
var avgflexPosTotal = quarterhours.Average(x => x.FlexPosDemand);
var avgflexPosCosts = quarterhours.Average(x => x.FlexPosMarginalCost);
var avgflexNegTotal = quarterhours.Average(x => x.FlexNegDemand);
var avgflexNegCosts = quarterhours.Average(x => x.FlexNegMarginalCost);

for (int k = i; k <= i + 3; k++)
{
content[k].FlexPos = avgflexPos;
content[k].FlexNeg = avgflexNeg;
content[k].MarginalCost = avgCost;
content[k].FlexPosDemand = avgflexPosTotal;
content[k].FlexPosMarginalCost = avgflexPosCosts;
content[k].FlexNegDemand = avgflexNegTotal;
content[k].FlexNegMarginalCost = avgflexNegCosts;
}
}

return content;
}

private string GenerateKissFile(FileInfo csvFile, List<HkwgInputItem> data, DateTime deliveryDay, int nextVersion, bool isPurchase)
private string GenerateKissFile(FileInfo csvFile, List<CsvLineItem> data, DateTime deliveryDay, int nextVersion, bool isPurchase)
{
MemoryStream ms = new MemoryStream(Resource.Template_Kiss_Input);

Expand All @@ -100,15 +102,15 @@ private string GenerateKissFile(FileInfo csvFile, List<HkwgInputItem> data, Date
int currentRow = rowOffset + i;
worksheet.Cell(currentRow, 1).SetValue(data[i].Time);
worksheet.Cell(currentRow, 2).SetValue(toTime);
worksheet.Cell(currentRow, 3).SetValue(isPurchase ? data[i].FlexPos : data[i].FlexNeg);
worksheet.Cell(currentRow, 4).SetValue(data[i].MarginalCost);
worksheet.Cell(currentRow, 3).SetValue(isPurchase ? data[i].FlexPosDemand : data[i].FlexNegDemand);
worksheet.Cell(currentRow, 4).SetValue(isPurchase ? data[i].FlexPosMarginalCost : data[i].FlexNegMarginalCost);

if ((i + 1) % 4 == 0)
{
worksheet.Range("A" + currentRow.ToString(), "D" + currentRow.ToString()).Style.Border.BottomBorder = XLBorderStyleValues.Thin;
}

totalEnergy += isPurchase ? data[i].FlexPos : data[i].FlexNeg;
totalEnergy += isPurchase ? data[i].FlexPosDemand : data[i].FlexNegDemand;
}

//Some styling
Expand Down Expand Up @@ -207,13 +209,13 @@ private void ProcessFile(FileInfo csvFile)
var version = workflowStore.GetNextInputVersionNumer(deliveryDay);

// Only generate the file if there any non zero demand values
if (content.Any(x => x.FlexPos != 0.0m))
if (content.Any(x => x.FlexPosDemand != 0.0m))
{
flexPosFile = this.GenerateKissFile(csvFile, content, deliveryDay, version, true);
}

// Only generate the file if there any non zero demand values
if (content.Any(x => x.FlexNeg != 0.0m))
if (content.Any(x => x.FlexNegDemand != 0.0m))
{
flexNegFile = this.GenerateKissFile(csvFile, content, deliveryDay, version, false);
}
Expand Down
57 changes: 20 additions & 37 deletions HkwgConverter/Core/OutboundConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public class OutboundConverter
private static LogWrapper log = LogWrapper.GetLogger(LogManager.GetCurrentClassLogger());
private WorkflowStore workflowStore;
private Settings configData;
private const string outputCsvFilePrefix = "OB_NewSchedule_";
private const string outputCsvFilePrefix = "Cottbus_ConfirmedDeal_";
private BusinessConfigurationSection businessSettings;

#endregion
Expand Down Expand Up @@ -56,7 +56,7 @@ private int DetermineColumnFromSettlementAreas(IXLWorksheet workssheet, bool isF
return -1;
}

private List<HkwgInputItem> ReadFile(string fileName)
private List<CsvLineItem> ReadFile(string fileName)
{
XLWorkbook workBook = new XLWorkbook(fileName);
var worksheet = workBook.Worksheets.Skip(1).FirstOrDefault();
Expand All @@ -71,7 +71,7 @@ private List<HkwgInputItem> ReadFile(string fileName)
var lastrow = worksheet.Rows()
.FirstOrDefault(x => x.FirstCell().Value.ToString().Contains("23:45"));

var lines = new List<HkwgInputItem>();
var lines = new List<CsvLineItem>();

int flexPosCol = DetermineColumnFromSettlementAreas(worksheet, true);
int flexNegCol = DetermineColumnFromSettlementAreas(worksheet, false);
Expand All @@ -87,11 +87,11 @@ private List<HkwgInputItem> ReadFile(string fileName)
{
var currentRow = worksheet.Row(i);

var entry = new HkwgInputItem()
var entry = new CsvLineItem()
{
Time = currentRow.Cell(1).GetString(),
FlexNeg = currentRow.Cell(flexNegCol).GetValue<decimal>(),
FlexPos = currentRow.Cell(flexPosCol).GetValue<decimal>()
FlexNegDemand = currentRow.Cell(flexNegCol).GetValue<decimal>(),
FlexPosDemand = currentRow.Cell(flexPosCol).GetValue<decimal>()
};

lines.Add(entry);
Expand All @@ -115,48 +115,31 @@ private void ProcessFile(FileInfo excelFile)
{
log.Error("Die Datei {0} kann nicht verarbeitet werden weil es für den Liefertag keinen offenen Prozess gibt.");
return;
}

var originalData = this.ReadOriginalCsvFile(latestWorkflow.CsvFile);
}

this.WriteCsvFile(latestWorkflow, content, originalData);
this.WriteCsvFile(latestWorkflow, content);
}


private List<HkwgInputItem> ReadOriginalCsvFile(string fileName)
{
var lines = File.ReadAllLines(Path.Combine(this.configData.InboundSuccessFolder, fileName))
.Skip(2)
.Select(x => x.Split(';'))
.Select(x => new HkwgInputItem()
{
Time = x[0],
FPLast = decimal.Parse(x[1], CultureInfo.InvariantCulture),
FlexPos = decimal.Parse(x[2], CultureInfo.InvariantCulture),
FlexNeg = decimal.Parse(x[3], CultureInfo.InvariantCulture),
MarginalCost = decimal.Parse(x[4], CultureInfo.InvariantCulture),
});

return lines.ToList();
}

private void WriteCsvFile(Workflow workflow, List<HkwgInputItem> newData, List<HkwgInputItem> originalData)
private void WriteCsvFile(Workflow workflow, List<CsvLineItem> newData)
{
StringBuilder sb = new StringBuilder();
sb.AppendLine("Startzeit; FP - Änderung");
sb.AppendLine("[yyyy-mm-dd hh:mm:ss];[mw,kw]");
sb.AppendLine("Zeitstempel;FlexPos_Change;FlexNeg_Change");
sb.AppendLine("[dd.MM.yyyy hh:mm:ss];[mw.kw];[mw.kw]");

for (int i = 0; i < originalData.Count; i++)
for (int i = 0; i < newData.Count; i++)
{

var newDemandValue = originalData[i].FPLast + newData[i].FlexPos - newData[i].FlexNeg;
string time = DateTime.Parse(originalData[i].Time).ToString("yyyy-MM-dd HH:mm:ss");

string time = DateTime.Parse(newData[i].Time).ToString("dd.MM.yyyy HH:mm:ss");

sb.Append(time);
sb.Append(";");
sb.AppendLine(newDemandValue.ToString("0.00", CultureInfo.InvariantCulture));
sb.Append(newData[i].FlexPosDemand.ToString("0.00", CultureInfo.InvariantCulture));
sb.Append(";");
sb.AppendLine(newData[i].FlexNegDemand.ToString("0.00", CultureInfo.InvariantCulture));
}

var targetFile = Path.Combine(this.configData.OutboundDropFolder, outputCsvFilePrefix+ "_" + workflow.CsvFile);
var targetFile = Path.Combine(this.configData.OutboundDropFolder, outputCsvFilePrefix + workflow.CsvFile.Replace("Cottbus_FlexDayAhead_", ""));

File.WriteAllText(targetFile, sb.ToString());

Expand Down
9 changes: 2 additions & 7 deletions HkwgConverter/HkwgConverter.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
<Prefer32Bit>false</Prefer32Bit>
</PropertyGroup>
<PropertyGroup>
<ApplicationIcon>enviam %281%29.ico</ApplicationIcon>
<ApplicationIcon>enviam.ico</ApplicationIcon>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Prod|AnyCPU'">
<OutputPath>bin\Prod\</OutputPath>
Expand Down Expand Up @@ -122,12 +122,10 @@
<None Include="packages.config" />
<None Include="Sample\20170201_FLEXPOS_HKWG_01.xlsx" />
<None Include="Sample\Anmeldung_20161212_11XSWCOTTBUS---M_11XENVIAMBILANZD_V0051.xlsx" />
<None Include="Sample\Beispieldateien.zip" />
<None Include="Sample\copy_input.bat" />
<None Include="Sample\input-neg-only.csv" />
<None Include="Sample\input-pos-neg.csv" />
<None Include="Sample\input-pos-only.csv" />
<None Include="Sample\OB_NewSchedule_20160705_20160704050122_+02.csv" />
<None Include="Sample\outbound-beispiel.xlsx" />
<None Include="Sample\Template_Kiss_Input.xlsx" />
<None Include="Sample\Testverzeichnisse_anlegen.bat" />
Expand Down Expand Up @@ -155,12 +153,9 @@
</BootstrapperPackage>
</ItemGroup>
<ItemGroup>
<Content Include="enviam %281%29.ico" />
<Content Include="enviam.ico" />
</ItemGroup>
<ItemGroup>
<Folder Include="Sample\root\Inbound\" />
</ItemGroup>
<ItemGroup />
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<PropertyGroup>
<PostBuildEvent>
Expand Down
10 changes: 5 additions & 5 deletions HkwgConverter/Model/HkwgInputItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,17 @@
/// <summary>
/// abstraction for the items in the Cottbus CSV file
/// </summary>
public class HkwgInputItem
public class CsvLineItem
{
public string Time { get; set; }

public decimal FPLast { get; set; }
public decimal FlexPosDemand { get; set; }

public decimal FlexPos { get; set; }
public decimal FlexPosMarginalCost { get; set; }

public decimal FlexNeg { get; set; }
public decimal FlexNegDemand { get; set; }

public decimal MarginalCost { get; set; }
public decimal FlexNegMarginalCost { get; set; }

}
}
Binary file removed HkwgConverter/Sample/Beispieldateien.zip
Binary file not shown.

This file was deleted.

Loading

0 comments on commit 34459b9

Please sign in to comment.