forked from RWTH-EBC/FiLiP
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added models and starte refactoring iot_agent
For RWTH-EBC#45
- Loading branch information
Showing
16 changed files
with
306 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
from .settings import settings | ||
from .models import FiwareHeader |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
from pydantic import BaseSettings, Field, AnyHttpUrl | ||
|
||
class _Settings(BaseSettings): | ||
OCB_URL: AnyHttpUrl = Field(default="http://localhost:1026", | ||
env=['ORION_URL', 'OCB_URL']) | ||
IOTA_URL: AnyHttpUrl = Field(default="http://localhost:4041", | ||
env='IOTA_URL') | ||
QL_URL: AnyHttpUrl = Field(default="http://localhost:8668", | ||
env=['QUANTUMLEAP_URL', 'QL_URL']) | ||
|
||
class Config: | ||
env_file = '../../.env' | ||
env_file_encoding = 'utf-8' | ||
case_sensitive = False | ||
|
||
|
||
settings = _Settings() |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
import requests | ||
import logging | ||
import json | ||
from typing import List, Union | ||
from urllib.parse import urljoin | ||
from core import settings, FiwareHeader | ||
from .models import Device, Protocol | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
class IoTAClient(): | ||
def __init__(self, fiware_header: FiwareHeader = None): | ||
self.headers = dict() | ||
if not fiware_header: | ||
fiware_header = FiwareHeader() | ||
self.headers.update(fiware_header.dict(by_alias=True)) | ||
|
||
def get_version(self): | ||
url = urljoin(settings.IOTA_URL, 'version') | ||
url = 'http://github.com/' | ||
try: | ||
res = requests.get(url=url, headers=self.headers) | ||
res.raise_for_status() | ||
except requests.exceptions.RequestException as e: | ||
logger.error(e) | ||
|
||
def get_devices(self, *, limit: int = 20, offset: int = 20, | ||
detailed: bool = None, entity: str = None, | ||
protocol: Protocol = None) -> List[Device]: | ||
""" | ||
Returns a list of all the devices in the device registry with all | ||
its data. | ||
Args: | ||
limit: if present, limits the number of devices returned in the list. | ||
offset: if present, skip that number of devices from the original query. | ||
detailed: | ||
entity: | ||
protocol: | ||
Returns: | ||
""" | ||
url = urljoin(settings.IOTA_URL, 'iot/devices') | ||
headers = self.headers | ||
params = {param for param in locals().items() if param is not None} | ||
try: | ||
res = requests.get(url=url, headers=headers, params=params) | ||
if res.ok: | ||
return [Device(**device) for device in res.json()['devices']] | ||
else: | ||
res.raise_for_status() | ||
except requests.exceptions.RequestException as e: | ||
logger.error(e) | ||
|
||
def post_devices(self): | ||
url = urljoin(settings.IOTA_URL, 'version') | ||
|
||
def get_device(self, *, device_id) -> Device: | ||
""" | ||
Returns all the information about a particular device. | ||
Args: | ||
device_id: | ||
Returns: | ||
Device | ||
""" | ||
url = urljoin(settings.IOTA_URL, 'iot/devices', device_id) | ||
headers = self.headers | ||
try: | ||
res = requests.get(url=url, headers=headers) | ||
if res.ok: | ||
return Device.parse_raw(res.json()) | ||
else: | ||
res.raise_for_status() | ||
except requests.exceptions.RequestException as e: | ||
logger.error(e) | ||
|
||
def update_device(self, *, device_id): | ||
pass | ||
|
||
def delete_device(self, *, device_id) -> None: | ||
""" | ||
Remove a device from the device registry. No payload is required | ||
or received. | ||
Args: | ||
device_id: | ||
Returns: | ||
None | ||
""" | ||
url = urljoin(settings.IOTA_URL, 'iot/devices', device_id) | ||
headers = self.headers | ||
try: | ||
res = requests.delete(url=url, headers=headers) | ||
if res.ok: | ||
logger.info(f"Device {device_id} successfully deleted!") | ||
else: | ||
res.raise_for_status() | ||
except requests.exceptions.RequestException as e: | ||
logger.error(e) | ||
logger.error(f"Device {device_id} was not deleted!") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
IOTA_URL="http://myHost:4041" | ||
OCB_URL="http://myHost:1026" | ||
QL_URL="http://myHost:8668" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import unittest | ||
# Third-party imports... | ||
import requests | ||
from unittest.mock import Mock, patch | ||
from iota import IoTAClient, models | ||
|
||
class TestClient(unittest.TestCase): | ||
def setUp(self) -> None: | ||
self.device = { | ||
"device_id": "saf", | ||
"service": None, | ||
"service_path": "/", | ||
"entity_name": "saf", | ||
"entity_type": "all", | ||
"timezone": 'Europe/Berlin', | ||
"timestamp": None, | ||
"apikey": "1234", | ||
"endpoint": None, | ||
"protocol": None, | ||
"transport": None, | ||
"lazy": None, | ||
"commands": None, | ||
"attributes": None, | ||
"static_attributes": None, | ||
"internal_attributes": None, | ||
"expressionLanguage": None, | ||
"explicitAttrs": None, | ||
"ngsiVersion": None | ||
} | ||
self.client = IoTAClient() | ||
|
||
def test_device_model(self): | ||
device = models.Device(**self.device) | ||
self.assertEqual(device.dict(), self.device) | ||
|
||
def test_get_version(self): | ||
res = self.client.get_version() | ||
if res is not None: | ||
print(type(res.json())) | ||
|
||
def test_device_endpoints(self): | ||
self.client.post_devices() | ||
|
||
def test_get_device(self): | ||
res = requests.get('http://jsonplaceholder.typicode.com/todos') | ||
self.assertTrue(res.ok) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import unittest | ||
from core.settings import _Settings | ||
|
||
|
||
class TestSettings(unittest.TestCase): | ||
def setUp(self) -> None: | ||
self.settings_parsing = _Settings(_env_file='test.env') | ||
self.settings_dotenv = _Settings(_env_file='../../example.env') | ||
|
||
def test_load_dotenv(self): | ||
self.assertEqual(self.settings_parsing.IOTA_URL, "http://myHost:4041") | ||
self.assertEqual(self.settings_parsing.OCB_URL, "http://myHost:1026") | ||
self.assertEqual(self.settings_parsing.QL_URL, "http://myHost:8668") | ||
|
||
def test_example_dotenv(self): | ||
self.assertEqual(self.settings_parsing, self.settings_dotenv) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.