Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Nueva clase IoT. #69

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions doc/best_practices.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ Las funciones que actualmente soporta la librería son:
- modulo `cb`: Módulo que incluye funciones relacionadas con la comunicación con el Context Broker.
- `send_batch`: Función que envía un lote de entidades al Context Broker. Recibe un listado con todos los tokens por subservicio y usa el correspondiente para realizar la llamada al Context Broker. Si no se dispone de token o ha caducado, se solicita o renueva el token según el caso y luego envía los datos.
- `get_entities_page`: Función que permite la recogida de datos del Context Broker. Permite el uso de ciertos parámetros como offset, limit, orderBy y type para filtrar la recogida de datos.
- modulo `iot`: Módulo que incluye funciones relacionadas con la comunicación del IoT Agent.
- `send_json`: Función que envía un JSON al IoT Agent. Recibe el nombre del sensor, su API key correspondiente, la URL del servidor y los datos a enviar.
- `send_batch`: Función que envía una lista de diccionarios al IoT Agent. Los envía uno por uno. Recibe el nombre del sensor, su API key correspondiente, la URL del servidor, el tiempo de espera en segundos entre envío y envío y los datos a enviar.

Se puede encontrar más detalles de la librería en la documentación de esta. [Ref.](../python-lib/tc_etl_lib/README.md)

Expand Down
21 changes: 21 additions & 0 deletions python-lib/tc_etl_lib/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,27 @@ La librería está creada con diferentes clases dependiendo de la funcionalidad
- Reemplaza todos los espacios en blanco consecutivos por el carácter de reemplazo.
- NOTA: Esta función no recorta la longitud de la cadena devuelta a 256 caracteres, porque el llamante puede querer conservar la cadena entera para por ejemplo guardarla en algún otro atributo, antes de truncarla.

- Clase `IoT`: En esta clase están las funciones relacionadas con el IoT Agent.

- `__init__`: constructor de objetos de la clase.
- `send_json`: Función que envía un archivo en formato JSON al IoT Agent.
- :param obligatorio `sensor_name`: El nombre del sensor.
- :param obligatorio `api_key`: La API key correspondiente al sensor.
- :param obligatorio `req_url`: La URL del servicio al que se le quiere enviar los datos.
- :param obligatorio: `data`: Datos a enviar. La estructura debe tener pares de elementos clave-valor (diccionario).
- :raises ValueError: Se lanza cuando los datos a enviar son distintos a un único diccionario.
- :raises Excepction: Se lanza una excepción ConnectionError cuando no puede conectarse al servidor. Se lanza una excepción FetchError cuando se produce un error en en la solicitud HTTP.
- :return: True si el envío de datos es exitoso.
- `send_batch`: Función que envía un archivo en formato JSON al IoT Agent.
- :param obligatorio `sensor_name`: El nombre del sensor.
- :param obligatorio `api_key`: La API key correspondiente al sensor.
- :param obligatorio `req_url`: La URL del servicio al que se le quiere enviar los datos.
- :param obligatorio: `time_sleep`: Es el tiempo de espera entre cada envío de datos en segundos.
- :param obligatorio: `data`: Datos a enviar. La estructura debe tener pares de elementos clave-valor (diccionario).t
- :raises ValueError: Se lanza cuando el tipo de los datos a enviar es incorrecto.
- :raises Excepction: Se lanza una excepción ConnectionError cuando no puede conectarse al servidor. Se lanza una excepción FetchError cuando se produce un error en en la solicitud HTTP.
- :return: True si el envío de datos es exitoso.

Algunos ejemplos de uso de `normalizer`:

```
Expand Down
4 changes: 3 additions & 1 deletion python-lib/tc_etl_lib/tc_etl_lib/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
#

from .auth import authManager
from .cb import FetchError, cbManager
from .cb import cbManager
from .exceptions import FetchError
from .iot import IoT
from .store import Store, orionStore, sqlFileStore
from .normalizer import normalizer
29 changes: 2 additions & 27 deletions python-lib/tc_etl_lib/tc_etl_lib/cb.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,39 +34,14 @@
import time
import json

from . import authManager
from . import authManager, exceptions

# control urllib3 post and get verify in false
import urllib3, urllib3.exceptions
urllib3.disable_warnings(category=urllib3.exceptions.InsecureRequestWarning)

logger = logging.getLogger(__name__)

class FetchError(Exception):
"""
FetchError encapsulates all parameters of an HTTP request and the erroneous response
"""

response: requests.Response
method: str
url: str
params: Optional[Any] = None
headers: Optional[Any] = None
body: Optional[Any] = None

def __init__(self, response: requests.Response, method: str, url: str, params: Optional[Any] = None, headers: Optional[Any] = None, body: Optional[Any] = None):
"""Constructor for FetchError class"""
self.response = response
self.method = method
self.url = url
self.params = params
self.headers = headers
self.body = body

def __str__(self) -> str:
return f"Failed to {self.method} {self.url} (headers: {self.headers}, params: {self.params}, body: {self.body}): [{self.response.status_code}] {self.response.text}"


class cbManager:
"""ContextBroker Manager

Expand Down Expand Up @@ -260,7 +235,7 @@ def get_entities_page(self, *, service: Optional[str] = None, subservice: Option
respjson = resp.json()
logger.error(f'{respjson["name"]}: {respjson["message"]}')
if resp.status_code < 200 or resp.status_code > 204:
raise FetchError(response=resp, method="GET", url=req_url, params=params, headers=headers)
raise exceptions.FetchError(response=resp, method="GET", url=req_url, params=params, headers=headers)

return resp.json()

Expand Down
50 changes: 50 additions & 0 deletions python-lib/tc_etl_lib/tc_etl_lib/exceptions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# Copyright 2022 Telefónica Soluciones de Informática y Comunicaciones de España, S.A.U.
#
# This file is part of tc_etl_lib
#
# tc_etl_lib is free software: you can redistribute it and/or
# modify it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# tc_etl_lib is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero
# General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with IoT orchestrator. If not, see http://www.gnu.org/licenses/.

"""
Exceptions handling for Python.
"""

import requests
from typing import Any, Optional

class FetchError(Exception):
"""
FetchError encapsulates all parameters of an HTTP request and the erroneous response.
"""

response: requests.Response
method: str
url: str
params: Optional[Any] = None
headers: Optional[Any] = None
body: Optional[Any] = None

def __init__(self, response: requests.Response, method: str, url: str, params: Optional[Any] = None, headers: Optional[Any] = None, body: Optional[Any] = None):
"""Constructor for FetchError class."""
self.response = response
self.method = method
self.url = url
self.params = params
self.headers = headers
self.body = body

def __str__(self) -> str:
return f"Failed to {self.method} {self.url} (headers: {self.headers}, params: {self.params}, body: {self.body}): [{self.response.status_code}] {self.response.text}"
102 changes: 102 additions & 0 deletions python-lib/tc_etl_lib/tc_etl_lib/iot.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# Copyright 2022 Telefónica Soluciones de Informática y Comunicaciones de España, S.A.U.
#
# This file is part of tc_etl_lib
#
# tc_etl_lib is free software: you can redistribute it and/or
# modify it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# tc_etl_lib is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero
# General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with IoT orchestrator. If not, see http://www.gnu.org/licenses/.

'''
IoT routines for Python:
- iot.send_json
- iot.send_batch
'''

from . import exceptions
import requests
import tc_etl_lib as tc
import time
from typing import Any, Iterable


class IoT:
"""IoT is a class that allows us to communicate with the IoT Agent."""

def __init__(self):
pass

def send_json(self,
sensor_name: str,
api_key: str,
req_url: str,
data: Any) -> None:
params = {
'i': sensor_name,
'k': api_key
}
headers = {
"Content-Type": "application/json"
}

try:
# Verify if data is a single dictionary.
if isinstance(data, dict):
resp = requests.post(url=req_url, json=data,
params=params, headers=headers)
if resp.status_code == 200:
return True
else:
raise exceptions.FetchError(
response=resp,
method="POST",
url=req_url,
params=params,
headers=headers)
else:
raise ValueError(
"The parameter 'data' should be a single dictionary {}.")
except requests.exceptions.ConnectionError as e:
raise e

def send_batch(self,
sensor_name: str,
api_key: str,
req_url: str,
time_sleep: float,
data: Iterable[Any]) -> None:
params = {
'i': sensor_name,
'k': api_key
}
headers = {
"Content-Type": "application/json"
}

try:
for i in range(0, len(data)):
resp = requests.post(
url=req_url, json=data[i], params=params, headers=headers)

if resp.status_code == 200:
time.sleep(time_sleep)
return True
else:
raise exceptions.FetchError(response=resp,
method="POST",
url=req_url,
params=params,
headers=headers)
except requests.exceptions.ConnectionError as e:
raise e
Loading