-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Program.cs
169 lines (144 loc) · 6.38 KB
/
Program.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
// © James Singleton. EUPL-1.2 (see the LICENSE file for the full license governing this code).
using CsvHelper;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Linq;
namespace octoyosu
{
public static class Program
{
public static void Main(string[] args)
{
try
{
var sw = Stopwatch.StartNew();
var readingsPath = "detailedReadings.csv";
if (args.Length > 0)
{
readingsPath = args[0];
}
var pricingPath =
Directory.GetFiles(".", "csv_agile_*.csv", SearchOption.TopDirectoryOnly)
.FirstOrDefault();
if (args.Length > 1)
{
pricingPath = args[1];
}
const decimal sgUr = 0.15288m;
const decimal sgSc = 0.2163m;
const decimal aoSc = 0.21m;
Console.WriteLine();
Console.WriteLine("🐙 🐙 🐙 🐙 🐙 🐙 🐙 🐙 🐙 🐙 🐙 🐙 🐙 🐙 🐙");
Console.WriteLine();
var rates = new Dictionary<DateTime, decimal>();
var usages = new List<Usage>();
using (var reader = new StreamReader(readingsPath))
using (var csv = new CsvReader(reader, CultureInfo.InvariantCulture))
{
csv.Read();
csv.ReadHeader();
var usage = new Usage
{
KWh = 0,
Time = DateTime.UnixEpoch,
};
while (csv.Read())
{
var kwh = csv.GetField<decimal>(3);
if (kwh <= 0) continue;
// Local time, not UTC - e.g. gap at 20200329 00:45
var time = DateTime.ParseExact(csv.GetField<string>(0), "yyyyMMdd HH:mm",
CultureInfo.InvariantCulture, DateTimeStyles.AssumeLocal).ToUniversalTime();
if (time.Minute != 00 && time.Minute != 30)
{
usage.KWh += kwh;
continue;
}
usage = new Usage
{
KWh = kwh,
Time = time,
};
usages.Add(usage);
}
}
var min = usages.Min(u => u.Time);
var max = usages.Max(u => u.Time);
var totalDays = (decimal) (max - min).TotalDays;
Console.WriteLine($"{min:ddd dd MMM yyyy} to {max:ddd dd MMM yyyy} ({totalDays:0} days)");
var totalKwh = usages.Sum(u => u.KWh);
PrintAverages(totalKwh, totalDays, "kWh");
var sgTotalUnitCost = totalKwh * sgUr;
var sgTotalStandingCharge = totalDays * sgSc;
var sgTotalCostGbpIncVat = sgTotalUnitCost + sgTotalStandingCharge;
Console.WriteLine("Super Green:");
PrintAverages(sgTotalCostGbpIncVat, totalDays, "GBP inc. VAT");
Console.WriteLine("Loading agile pricing and calculating...");
Console.WriteLine();
using (var reader = new StreamReader(pricingPath))
using (var csv = new CsvReader(reader, CultureInfo.InvariantCulture))
{
while (csv.Read())
{
var time = DateTime.Parse(csv.GetField<string>(0)); // UTC
if (time >= min && time <= max)
{
rates.Add(time, csv.GetField<decimal>(4)); // inc. VAT
}
}
}
var aoTotalUnitCost = (
from usage in usages
let rate = rates.SingleOrDefault(r =>
r.Key == usage.Time)
select rate.Value * 0.01m * usage.KWh
).Sum();
var aoTotalStandingCharge = totalDays * aoSc;
var aoTotalCostIncVat = aoTotalUnitCost + aoTotalStandingCharge;
Console.WriteLine("Agile:");
PrintAverages(aoTotalCostIncVat, totalDays, "GBP inc. VAT");
Console.WriteLine("Savings:");
PrintAverages(sgTotalCostGbpIncVat - aoTotalCostIncVat, totalDays, "GBP inc. VAT");
var agilePercentage = aoTotalCostIncVat / sgTotalCostGbpIncVat * 100;
Console.WriteLine("Super Green 100%: 🐙🐙🐙🐙🐙🐙🐙🐙🐙🐙");
Console.Write($" Agile {agilePercentage:0}%: ");
for (var i = 10; i < agilePercentage; i += 10)
{
Console.Write("🐙");
}
Console.WriteLine();
Console.WriteLine();
sw.Stop();
#if DEBUG
Console.WriteLine($"Done in {sw.ElapsedMilliseconds:#,###}ms");
#endif
}
catch (FileNotFoundException fileNotFoundException)
{
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine($"Could not find file: {fileNotFoundException.FileName}");
Console.WriteLine("Usage: ./octoyosu [readingsFile.csv] [pricingFile.csv]");
}
catch (Exception exception)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine($"An unexpected error occurred: {exception}");
}
Console.ResetColor();
}
private static void PrintAverages(decimal total, decimal days, string unit)
{
var day = total / days;
var year = day * 365; // ignore leap year
var month = year / 12;
Console.WriteLine($"Period total {total:0.00} {unit} (approx)");
Console.WriteLine($"Daily average {day:0.00} {unit} (approx)");
Console.WriteLine($"Yearly average {year:0} {unit} (approx)");
Console.WriteLine($"Monthly average {month:0} {unit} (approx)");
Console.WriteLine();
}
}
}