From 0560367ffd13eb0110890e794340b70351260131 Mon Sep 17 00:00:00 2001 From: systemcatch <30196510+systemcatch@users.noreply.github.com> Date: Tue, 12 Dec 2017 12:22:26 +0000 Subject: [PATCH] Change data source for South Australia battery (#905) * Modify SA battery Use nemlog api rather than global-roam. * Add session --- parsers/lib/AU_battery.py | 46 ++++++++++++++++++++++++--------------- 1 file changed, 28 insertions(+), 18 deletions(-) diff --git a/parsers/lib/AU_battery.py b/parsers/lib/AU_battery.py index 66e486c8e8..7a6ae98106 100644 --- a/parsers/lib/AU_battery.py +++ b/parsers/lib/AU_battery.py @@ -1,31 +1,41 @@ #!/usr/bin/python -import requests +"""Parser for South Australia's 129MWh battery built by Tesla.""" -#Parser for South Australia's 129MWh battery built by Tesla. -#base_url gets generation status in 5 min intervals. +import arrow +import json +import requests -base_url = 'https://ausrealtimefueltype.global-roam.com/api/SeriesSnapshot?time=' +#nemlog_url gets generation status in 5 min intervals. -def fetch_SA_battery(): +def fetch_SA_battery(session = None): """ - Makes a request to the NemWatch widget data source. - Finds dictionaries related to the South Australia battery. - Returns a float. + Makes a request to the nemlog api for South Australia battery data. + Returns a float or None. """ - #Verify set to false to prevent SSL error due to domain mismatch. - req = requests.get(base_url, verify = False) - json_content = req.json() - dicts = json_content["seriesCollection"] - discharge_id = (item for item in dicts if item["id"] == "41").next() - storage_id = (item for item in dicts if item["id"] == "57").next() + today = arrow.now('Australia/Adelaide') + current = today.format('YYYYMMDD') + old = today.shift(days=-2).format('YYYYMMDD') + nemlog_url = 'http://nemlog.com.au/api/unit/HPRL1/{}/{}/json'.format(old, current) + + s = session or requests.Session() + req = s.get(nemlog_url) + + data = [] + for line in req.iter_lines(): + data.append(line) + + try: + latest = json.loads(data[-1]) + except IndexError: + #No data available. + return None - discharge = -1*float(discharge_id["value"]) - storage = float(storage_id["value"]) + state = float(latest["SCADAVALUE"]) - #one of these should always be zero - battery_status = discharge + storage + #Source classifies charge/discharge opposite to EM. + battery_status = -1*state return battery_status