forked from badger707/esb-smart-meter-reading-automation
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathesb-smart-meter-reader.py
271 lines (236 loc) · 10.5 KB
/
esb-smart-meter-reader.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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
# #!/usr/bin/env python3
# Script to read smart meter data from ESB Networks
# https://gist.github.com/schlan/f72d823dd5c1c1d19dfd784eb392dded
# Modified by badger707
# https://github.com/badger707/esb-smart-meter-reading-automation
# forked by vincentezw to directly write to influxdb
# https://github.com/vincentezw/esb-smart-meter-influxdb
import argparse
import configparser
import csv
import json
import logging
import os
import re
import sys
from datetime import datetime, timedelta
import pytz
import requests
from bs4 import BeautifulSoup
from influxdb_client_3 import InfluxDBClient3, Point
parser = argparse.ArgumentParser()
parser.add_argument("--debug", action="store_true", help="Enable debug mode")
args = parser.parse_args()
logging.basicConfig(format="%(message)s", level=logging.INFO, stream=sys.stdout)
if args.debug:
logging.getLogger().setLevel(logging.DEBUG)
logging.info("Debug mode enabled.")
class ESBSmartMeterReader:
json_file_path = "esb_readings.json"
def __init__(
self,
config_file_path,
):
logging.debug("reading config")
config = configparser.ConfigParser()
config.read(config_file_path)
self.esb_meter_mprn = config.get("ESB", "meter_mprn")
self.esb_username = config.get("ESB", "username")
self.esb_password = config.get("ESB", "password")
self.influx_host = config.get("InfluxDB", "host")
self.influx_bucket = config.get("InfluxDB", "bucket")
self.influx_organisation = config.get("InfluxDB", "organisation")
self.influx_token = config.get("InfluxDB", "token")
def load_smart_meter_stats_v2(self):
# this parameter looks to be ignored by the ESB endpoint, which always returns *all* the data
today_ = datetime.today()
smart_meter_data = self.__load_esb_data(today_)
if len(smart_meter_data) == 0:
logging.info("No new data found")
return
self.__export_to_influx(smart_meter_data)
logging.info("Import completed and JSON file generated")
def __extract_xsrf_token(self, cookie_header):
cookies = cookie_header.split(',')
for cookie in cookies:
if 'XSRF-TOKEN' in cookie:
token = cookie.split('XSRF-TOKEN=')[1].split(';')[0]
return token
return None
def __load_esb_data(self, start_date):
file_url = 'https://myaccount.esbnetworks.ie/DataHub/DownloadHdfPeriodic'
logging.info("Loading ESB data for MPRN %s", self.esb_meter_mprn)
s = requests.Session()
s.headers.update({
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/111.0.0.0 Safari/537.36',
})
logging.debug("[+] calling login page. ..")
login_page = s.get("https://myaccount.esbnetworks.ie/", allow_redirects=True)
result = re.findall(r"(?<=var SETTINGS = )\S*;", str(login_page.content))
settings = json.loads(result[0][:-1])
logging.debug("[+] sending credentials ...")
s.post(
"https://login.esbnetworks.ie/esbntwkscustportalprdb2c01.onmicrosoft.com/B2C_1A_signup_signin/SelfAsserted?tx="
+ settings["transId"]
+ "&p=B2C_1A_signup_signin",
data={
"signInName": self.esb_username,
"password": self.esb_password,
"request_type": "RESPONSE",
},
headers={
"x-csrf-token": settings["csrf"],
},
allow_redirects=True,
)
logging.debug("[+] passing AUTH ...")
confirm_login = s.get(
"https://login.esbnetworks.ie/esbntwkscustportalprdb2c01.onmicrosoft.com/B2C_1A_signup_signin/api/CombinedSigninAndSignup/confirmed",
params={
"rememberMe": False,
"csrf_token": settings["csrf"],
"tx": settings["transId"],
"p": "B2C_1A_signup_signin",
},
)
# logging.debug("[+] confirm_login: ", confirm_login)
logging.debug("[+] doing some BeautifulSoup ...")
soup = BeautifulSoup(confirm_login.content, "html.parser")
form = soup.find("form", {"id": "auto"})
fff=s.post(
form['action'],
allow_redirects=True,
data={
'state': form.find('input', {'name': 'state'})['value'],
'client_info': form.find('input', {'name': 'client_info'})['value'],
'code': form.find('input', {'name': 'code'})['value'],
},
)
logging.debug("[!] Status Code: %s" %(fff.status_code))
user_welcome_soup = BeautifulSoup(fff.text,'html.parser')
user_elements = user_welcome_soup.find('h1', class_='esb-title-h1')
if user_elements.text[:2] == "We":
print("[!] Confirmed User Login: ", user_elements.text) # It should return "Welcome, Name Surname"
else:
print("[!!!] No Welcome message, User is not logged in.")
s.close()
historic_consumption_url = "https://myaccount.esbnetworks.ie/Api/HistoricConsumption"
h1_elem = s.get(historic_consumption_url, allow_redirects=True)
h1_elem_content = h1_elem.text
h1_elem_soup = BeautifulSoup(h1_elem_content, 'html.parser')
h1_elem_element = h1_elem_soup.find('h1', class_='esb-title-h1')
if h1_elem_element.text[:2] == "My":
logging.debug("[+] Jumped to page: %s" %(h1_elem_element.text)) # It should return "My energy Consumption"
else:
logging.debug("[!] ups - something went wrong.")
s.close()
x_headers={
'Host': 'myaccount.esbnetworks.ie',
'x-ReturnUrl': historic_consumption_url,
'Referer': historic_consumption_url,
}
x_down = s.get("https://myaccount.esbnetworks.ie/af/t",headers=x_headers)
set_cookie_header = x_down.headers.get('Set-Cookie', '')
xsrf_token = self.__extract_xsrf_token(set_cookie_header)
file_headers = {
'Referer': historic_consumption_url,
'content-type': 'application/json',
'x-returnurl': historic_consumption_url,
'x-xsrf-token': xsrf_token,
'Origin': "https://myaccount.esbnetworks.ie",
}
payload_data = {
"mprn": self.esb_meter_mprn,
"searchType": "intervalkw"
}
logging.debug("[+] getting CSV file for MPRN ...")
response_data_file = s.post(file_url, headers=file_headers, json=payload_data)
s.close()
magic_data = response_data_file.content.decode("utf-8")
csv_reader = csv.DictReader(magic_data.split('\n'))
return self.__csv_response_to_json(csv_reader)
def __get_dst_change_timestamp(self, year):
last_october_day = datetime(
year, 10, 31, 2, 0, 0, tzinfo=pytz.timezone("Europe/Dublin")
)
weekday = last_october_day.weekday() # 0 is Monday, 6 is Sunday
days_until_sunday = (weekday + 1) % 7
dst_change_timestamp_1am = last_october_day - timedelta(
days=days_until_sunday, hours=1
)
dst_change_timestamp_1_30am = last_october_day - timedelta(
days=days_until_sunday, hours=0, minutes=30
)
return [
dst_change_timestamp_1am.strftime("%d-%m-%Y %H:%M"),
dst_change_timestamp_1_30am.strftime("%d-%m-%Y %H:%M"),
]
def __csv_response_to_json(self, csv_reader):
logging.debug("[+] creating JSON file from CSV ...")
output_json = []
existing_data = self.__get_previous_data()
new_entries = []
existing_entries = set(
(entry["Read Date and End Time"] for entry in existing_data)
)
logging.debug("Found %s existing records in JSON file", len(existing_entries))
# List of problematic timestamps during DST change
# We get a basic timestamp in IST, but this means a duplicate hour when DST changes in autumn
# We can't use the UTC timestamp as the ESB API doesn't return the timezone
# So we just ignore the duplicate hours. It's my assumption this means 2 readings with
# the same timestamp in InfluxDB, but that's better than a missing reading
dst_change_timestamps = [
timestamp
for i in range(5)
for timestamp in self.__get_dst_change_timestamp(datetime.now().year + i)
]
for row in csv_reader:
timestamp_str = row["Read Date and End Time"]
output_json.append(row)
unique_identifier = row["Read Date and End Time"]
if unique_identifier not in existing_entries:
new_entries.append(row)
if timestamp_str not in dst_change_timestamps:
existing_entries.add(unique_identifier)
logging.info(
"Found %s new entries (%s entries downloaded)",
len(new_entries),
len(output_json),
)
with open(self.json_file_path, "w", encoding="utf-8") as jsonf:
json_out = json.dumps(output_json, indent=2)
jsonf.write(json_out)
logging.debug("[+] JSON file created")
return new_entries
def __get_previous_data(self):
existing_json_data = []
if os.path.exists(self.json_file_path):
with open(self.json_file_path, "r", encoding="utf-8") as existing_json_file:
existing_json_data = json.load(existing_json_file)
return existing_json_data
def __export_to_influx(self, data):
client = InfluxDBClient3(
token=self.influx_token,
host=self.influx_host,
org=self.influx_organisation,
database=self.influx_bucket,
)
total_entries = len(data)
for i, entry in enumerate(data, 1):
timestamp = datetime.strptime(
entry["Read Date and End Time"], "%d-%m-%Y %H:%M"
)
point = (
Point("meter_reading")
.tag("MPRN", entry["MPRN"])
.tag("MeterSerialNumber", entry["Meter Serial Number"])
.tag("ReadType", entry["Read Type"])
.field("reading", float(entry["Read Value"]))
.time(timestamp)
)
client.write(point)
sys.stdout.write(f"\rProcessing entry {i}/{total_entries}")
sys.stdout.flush()
client.close()
print()
ESBSmartMeterReader(".secrets").load_smart_meter_stats_v2()