-
Notifications
You must be signed in to change notification settings - Fork 0
/
nationalGrid.py
executable file
·179 lines (144 loc) · 5.69 KB
/
nationalGrid.py
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
170
171
172
173
174
175
176
177
178
179
#!/usr/bin/python3
# General imports
import datetime
import aiohttp
# Local imports
from .const import ( DFS_DATASTORE, DFS_QUERY_PARAMETERS, CACHE_EXPIRY )
## Used to aid sorting
def dfs_start_time(dfs_requirement):
return dfs_requirement['start']
class NationalGridClient:
def __init__(self, session: aiohttp.ClientSession):
self._polling_dfs = False
self._last_dfs_poll = None
self._session = session
self._dfsData = None
##
## Pulls the current data from NG ESO and then extracts only the records that apply today.
##
async def async_get_todays_dfs_requirements(self, dfsDate=None):
self._polling_dfs = True
requirements = list()
jsonData = None
queryDFS = False
today = datetime.date.today()
if dfsDate is not None:
today = dfsDate
## Retain an element of control over how often we actually bug NG ESO
if self._dfsData is None:
queryDFS = True
else:
if self._last_dfs_poll is None:
queryDFS = True
else:
cacheExpiryTime = (self._last_dfs_poll+datetime.timedelta(minutes=CACHE_EXPIRY))
queryDFS = ( datetime.datetime.now() > cacheExpiryTime )
if queryDFS:
if DFS_DATASTORE is not None:
queryURL = DFS_DATASTORE
if DFS_QUERY_PARAMETERS is not None:
queryURL = queryURL+"&"+DFS_QUERY_PARAMETERS
async with self._session.get(queryURL) as response:
jsonData = await response.json()
if jsonData:
for record in jsonData['result']['records']:
requestDate = str(record['Delivery Date'])
if requestDate == str(today):
start = datetime.datetime.strptime( requestDate+" "+record['From GMT'], "%Y-%m-%d %H:%M" )
end = datetime.datetime.strptime( requestDate+" "+record['To GMT'], "%Y-%m-%d %H:%M" )
dfsRequirement = { 'start': start, 'end': end, 'minutes': ((end - start).total_seconds() // 60) }
requirements.append( dfsRequirement )
requirements.sort(key=dfs_start_time)
self._dfsData = requirements
self._polling_dfs = False
self._last_dfs_poll = datetime.datetime.now()
##
## Derives current DFS status based on time of day and requirements:
## Inactive : No requirements until at least midnight.
## Scheduled : DFS requirements scheduled for later in the day.
## Active : Currently in a DFS requirement window.
##
def get_current_dfs_status(self, dfsDate=None) -> str:
currentStatus = 'Unknown'
currentTime = datetime.datetime.now()
if dfsDate is not None:
currentTime = datetime.datetime.strptime( dfsDate+" "+currentTime.strftime('%H:%M'), "%Y-%m-%d %H:%M" )
if self._dfsData is not None:
currentStatus = 'Inactive'
for thisAsk in self._dfsData:
if thisAsk['start'] < currentTime < thisAsk['end']:
currentStatus = 'Active'
break
if currentTime < thisAsk['start']:
currentStatus = 'Scheduled'
return currentStatus
##
## Gets the start time of the current (DFS active) or next (DFS scheduled) session.
## Returns None if inactive.
##
def get_dfs_session_start(self, dfsDate=None):
currentTime = datetime.datetime.now()
if dfsDate is not None:
currentTime = datetime.datetime.strptime( dfsDate+" "+currentTime.strftime('%H:%M'), "%Y-%m-%d %H:%M" )
sessionStart = None
# It's actually easier to do this by working backwards from the identified end of session.
# Otherwise you have to work out the end of every session from the first record to avoid reporting a shrinking
# session.
sessionEnd = self.get_dfs_session_end(dfsDate)
if self._dfsData is not None:
dfsData = self._dfsData
dfsData.sort(reverse=True, key=dfs_start_time)
for request in dfsData:
if sessionStart is None:
if sessionEnd == request['end']:
sessionStart = request['start']
else:
if request['end'] == sessionStart:
sessionStart = request['start']
else:
break
return sessionStart
##
## Gets the end time of the current (DFS active) or next (DFS scheduled) session.
## Returns None if inactive.
##
def get_dfs_session_end(self, dfsDate=None):
currentTime = datetime.datetime.now()
if dfsDate is not None:
currentTime = datetime.datetime.strptime( dfsDate+" "+currentTime.strftime('%H:%M'), "%Y-%m-%d %H:%M" )
sessionEnd = None
if self._dfsData is not None:
dfsData = self._dfsData
dfsData.sort(key=dfs_start_time)
for request in dfsData:
if sessionEnd is None:
if (request['start'] >= currentTime) or (request['start'] < currentTime < request['end']):
sessionEnd = request['end']
else:
if request['start'] == sessionEnd:
sessionEnd = request['end']
else:
break
return sessionEnd
##
## Total time ESO is requesting flexibility today
##
def get_todays_total_dfs_request(self) -> int:
requestedMinutes = 0
if self._dfsData is not None:
for request in self._dfsData:
requestedMinutes = requestedMinutes + request['minutes']
return requestedMinutes
##
## Remaining time ESO is requesting flexibility today
##
def get_todays_outstanding_dfs_request(self) -> int:
outstandingMinutes = 0
currentTime = datetime.datetime.now()
if self._dfsData is not None:
for request in self._dfsData:
if request['start'] > currentTime:
outstandingMinutes = outstandingMinutes + request['minutes']
if request['start'] < currentTime < request['end']:
outstandingMinutes = outstandingMinutes + ((request['end']-currentTime).total_seconds() // 60)
return outstandingMinutes