-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathProgram.cs
136 lines (112 loc) · 5.04 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
// © James Singleton. EUPL-1.2 (see the LICENSE file for the full license governing this code).
using System;
using System.Linq;
using System.Net.Http;
using System.Threading.Tasks;
using octoplot.api;
namespace octoplot
{
public static class Program
{
public static async Task Main(string[] args)
{
// Grid Supply Point - https://en.wikipedia.org/wiki/Distribution_network_operator
var gsp = Region.NorthScotland;
if (args.Length == 1 && args[0].Length == 1)
{
gsp = args[0].ToUpper();
}
var url = $"products/AGILE-18-02-21/electricity-tariffs/E-1R-AGILE-18-02-21-{gsp}/standard-unit-rates/";
Console.WriteLine("Getting pricing data...");
Console.WriteLine();
var client = new HttpClient
{
BaseAddress = new Uri("https://api.octopus.energy/v1/")
};
var response = await client.GetAsync(url);
if (!response.IsSuccessStatusCode)
{
Console.WriteLine("Could not get pricing data! :(");
Console.WriteLine(response.ReasonPhrase);
return;
}
var result = await response.Content.ReadAsAsync<Product>();
var futureResults = result.Results.Where(r => r.Valid_To > DateTime.UtcNow).ToList();
var gmtZone = TimeZoneInfo.FindSystemTimeZoneById("Europe/London");
var start = TimeZoneInfo.ConvertTimeFromUtc(futureResults.Min(r => r.Valid_From), gmtZone);
var end = TimeZoneInfo.ConvertTimeFromUtc(futureResults.Max(r => r.Valid_To), gmtZone);
Console.WriteLine($"Pricing data from {start:ddd dd MMM HH:mm} to {end:ddd dd MMM HH:mm} for region {gsp}");
Console.WriteLine();
// Main body of graph
var min = futureResults.Min(r => r.Value_Inc_Vat);
var max = futureResults.Max(r => r.Value_Inc_Vat);
var ratesInTimeOrder = futureResults.OrderBy(r => r.Valid_From).ToList();
for (var ii = Math.Round(max, MidpointRounding.AwayFromZero);
ii > Math.Min(Math.Round(min, MidpointRounding.AwayFromZero), 0);
ii--)
{
Console.ForegroundColor = ConsoleColor.Green;
if (ii >= 0) Console.ForegroundColor = ConsoleColor.White;
if (ii > 7) Console.ForegroundColor = ConsoleColor.Yellow;
if (ii > 15) Console.ForegroundColor = ConsoleColor.Red;
if (ii >= 0) Console.Write(" ");
Console.Write($"{ii:00}p ");
foreach (var rate in ratesInTimeOrder)
{
Console.Write(rate.Value_Inc_Vat > ii ? "£" : " ");
}
if (ii == 0)
{
Console.WriteLine();
Console.Write("-----");
for (var jj = 0; jj < ratesInTimeOrder.Count; jj++)
{
Console.Write("-");
}
}
Console.WriteLine();
}
Console.ResetColor();
// X-axis time scale
var hours = ratesInTimeOrder
.Where(r => r.Valid_From.Minute == 0)
.Select(r => $"{TimeZoneInfo.ConvertTimeFromUtc(r.Valid_From, gmtZone).Hour:00}").ToList();
Console.Write(" ");
foreach (var tens in hours.Select(h => h.First()))
{
Console.Write($"{(tens != '0' ? tens : ' ')} ");
}
Console.WriteLine();
Console.Write(" ");
foreach (var ones in hours.Select(h => h.Last()))
{
Console.Write($"{ones} ");
}
Console.WriteLine();
Console.WriteLine();
// Textual message
var minPeriods = ratesInTimeOrder.Where(r => r.Value_Inc_Vat == min)
.OrderBy(r => r.Valid_From).ToList();
var minPeriod = minPeriods.FirstOrDefault();
if (null == minPeriod) return;
// Use first and last date if same price over multiple _contiguous_ periods
if (minPeriods.Count > 1)
{
foreach (var period in minPeriods.Skip(1))
{
if (minPeriod.Valid_To == period.Valid_From)
{
minPeriod.Valid_To = period.Valid_To;
}
}
}
var prefix = minPeriod.Value_Inc_Vat < 0 ? "Paid most for electricity" :
minPeriod.Value_Inc_Vat == 0 ? "Free electricity" : "Electricity is cheapest";
Console.WriteLine(
$"{prefix} between {TimeZoneInfo.ConvertTimeFromUtc(minPeriod.Valid_From, gmtZone):ddd dd MMM HH:mm} " +
$"and {TimeZoneInfo.ConvertTimeFromUtc(minPeriod.Valid_To, gmtZone):ddd dd MMM HH:mm} " +
$"({minPeriod.Value_Inc_Vat}p/kWh inc. VAT)");
Console.WriteLine();
}
}
}