Skip to content

Commit

Permalink
Merge pull request #20 from fer112233/master
Browse files Browse the repository at this point in the history
Añadido consulta de producción de solar.
  • Loading branch information
hectorespert authored Aug 1, 2021
2 parents ea5c320 + fca930a commit 3e876d4
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 0 deletions.
20 changes: 20 additions & 0 deletions oligo/asyncio/asynciber.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
GUARDAR_ESCENARIO_URL = "escenarioNew/confirmarMedicionOnLine/{}/1/{}"
BORRAR_ESCENARIO_URL = "escenarioNew/borrarEscenario/"
OBTENER_PERIODO_URL = "consumoNew/obtenerDatosConsumoPeriodo/fechaInicio/{}00:00:00/fechaFinal/{}00:00:00/"
OBTENER_PERIODO_GENERACION_URL = "consumoNew/obtenerDatosGeneracionPeriodo/fechaInicio/{}00:00:00/fechaFinal/{}00:00:00/"


class ResponseException(Exception):
Expand Down Expand Up @@ -175,6 +176,25 @@ async def consumption(self, start: datetime, end: datetime) -> list:
data = await self._consumption_raw(start, end)
return [float(x["valor"]) for x in data["y"]["data"][0] if x]

async def _production_raw(self, start: datetime, end: datetime) -> list:
return await self.__request(
OBTENER_PERIODO_GENERACION_URL.format(
start.strftime("%d-%m-%Y"), end.strftime("%d-%m-%Y")
)
)

# Get production data from a time period
#
# start/end: datetime.date objects indicating the time period (both inclusive)
# The supported time range seems to be (not documented) from Jan 1 in the previous year and a max
# length of one year.
#
# Returns a list of production starting a midnight on the start day until 23:00 on the last day.
# Each value is the hourly production in Wh.
async def production(self, start: datetime, end: datetime) -> list:
data = await self._production_raw(start, end)
return [float(x["valor"]) for x in data["y"]["data"][0] if x]

# Get total consumption in Wh (Watt-hour) over a time period
#
# start/end: datetime.date objects indicating the time period (both inclusive)
Expand Down
32 changes: 32 additions & 0 deletions oligo/iber.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ class Iber:
__guardar_escenario_url = __domain + "/consumidores/rest/escenarioNew/confirmarMedicionOnLine/{}/1/{}"
__borrar_escenario_url = __domain + "/consumidores/rest/escenarioNew/borrarEscenario/"
__obtener_periodo_url = __domain + "/consumidores/rest/consumoNew/obtenerDatosConsumoPeriodo/fechaInicio/{}00:00:00/fechaFinal/{}00:00:00/" # date format: 07-11-2020 - that's 7 Nov 2020
__obtener_periodo_generacion_url = __domain + "/consumidores/rest/consumoNew/obtenerDatosGeneracionPeriodo/fechaInicio/{}00:00:00/fechaFinal/{}00:00:00/" # date format: 07-11-2020 - that's 7 Nov 2020

__headers = {
'User-Agent': "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Ubuntu Chromium/77.0.3865.90 Chrome/77.0.3865.90 Safari/537.36",
Expand Down Expand Up @@ -212,6 +213,37 @@ def consumption(self, start, end):
else:
values.append(float(x['valor']))
return values

def _production_raw(self, start, end):
self.__check_session()
start_str = start.strftime('%d-%m-%Y')
end_str = end.strftime('%d-%m-%Y')

response = self.__session.request("GET", self.__obtener_periodo_generacion_url.format(start_str, end_str),
headers=self.__headers)
if response.status_code != 200:
raise ResponseException
if not response.text:
raise NoResponseException
return response.json()

# Get production data from a time period
#
# start/end: datetime.date objects indicating the time period (both inclusive)
# The supported time range seems to be (not documented) from Jan 1 in the previous year and a max
# length of one year.
#
# Returns a list of productions starting a midnight on the start day until 23:00 on the last day.
# Each value is the hourly production in Wh.
def production(self, start, end):
json = self._production_raw(start, end)
values = []
for x in json['y']['data'][0]:
if x is None:
values.append(None)
else:
values.append(float(x['valor']))
return values

# Get total consumption in Wh (Watt-hour) over a time period
#
Expand Down

0 comments on commit 3e876d4

Please sign in to comment.