Skip to content

Commit

Permalink
Change data source for South Australia battery (electricitymaps#905)
Browse files Browse the repository at this point in the history
* Modify SA battery
Use nemlog api rather than global-roam.

* Add session
  • Loading branch information
systemcatch authored and corradio committed Dec 12, 2017
1 parent 083c98c commit 0560367
Showing 1 changed file with 28 additions and 18 deletions.
46 changes: 28 additions & 18 deletions parsers/lib/AU_battery.py
Original file line number Diff line number Diff line change
@@ -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

Expand Down

0 comments on commit 0560367

Please sign in to comment.