diff --git a/framework/python/src/api/api.py b/framework/python/src/api/api.py index f63f1825a..379652574 100644 --- a/framework/python/src/api/api.py +++ b/framework/python/src/api/api.py @@ -13,6 +13,7 @@ # limitations under the License. from fastapi import FastAPI, APIRouter, Response, Request, status +from fastapi.middleware.cors import CORSMiddleware import json from json import JSONDecodeError import psutil @@ -52,8 +53,18 @@ def __init__(self, test_run): self._router.add_api_route("/devices", self.get_devices) self._router.add_api_route("/device", self.save_device, methods=["POST"]) + # TODO: Make this configurable in system.json + origins = ["http://localhost:4200"] + self._app = FastAPI() self._app.include_router(self._router) + self._app.add_middleware( + CORSMiddleware, + allow_origins=origins, + allow_credentials=True, + allow_methods=["*"], + allow_headers=["*"], + ) self._api_thread = threading.Thread(target=self._start, name="Test Run API", @@ -65,7 +76,7 @@ def start(self): LOGGER.info("API waiting for requests") def _start(self): - uvicorn.run(self._app, log_config=None) + uvicorn.run(self._app, log_config=None, port=self._session.get_api_port()) def stop(self): LOGGER.info("Stopping API") diff --git a/framework/python/src/common/device.py b/framework/python/src/common/device.py index e2552d75a..bd5a8562f 100644 --- a/framework/python/src/common/device.py +++ b/framework/python/src/common/device.py @@ -20,6 +20,7 @@ class Device(): """Represents a physical device and it's configuration.""" + folder_url: str = None mac_addr: str = None manufacturer: str = None model: str = None diff --git a/framework/python/src/common/session.py b/framework/python/src/common/session.py index 13e4b09fb..1eef8b98d 100644 --- a/framework/python/src/common/session.py +++ b/framework/python/src/common/session.py @@ -25,6 +25,7 @@ MONITOR_PERIOD_KEY = 'monitor_period' STARTUP_TIMEOUT_KEY = 'startup_timeout' LOG_LEVEL_KEY = 'log_level' +API_PORT_KEY = 'api_port' MAX_DEVICE_REPORTS_KEY = 'max_device_reports' class TestRunSession(): @@ -68,7 +69,8 @@ def _get_default_config(self): 'startup_timeout': 60, 'monitor_period': 30, 'runtime': 120, - 'max_device_reports': 5 + 'max_device_reports': 5, + 'api_port': 8000 } def get_config(self): @@ -101,6 +103,9 @@ def _load_config(self): if LOG_LEVEL_KEY in config_file_json: self._config[LOG_LEVEL_KEY] = config_file_json.get(LOG_LEVEL_KEY) + if API_PORT_KEY in config_file_json: + self._config[API_PORT_KEY] = config_file_json.get(API_PORT_KEY) + if MAX_DEVICE_REPORTS_KEY in config_file_json: self._config[MAX_DEVICE_REPORTS_KEY] = config_file_json.get(MAX_DEVICE_REPORTS_KEY) @@ -132,6 +137,9 @@ def get_monitor_period(self): def get_startup_timeout(self): return self._config.get(STARTUP_TIMEOUT_KEY) + def get_api_port(self): + return self._config.get(API_PORT_KEY) + def get_max_device_reports(self): return self._config.get(MAX_DEVICE_REPORTS_KEY) diff --git a/framework/python/src/core/testrun.py b/framework/python/src/core/testrun.py index 6e3a6da5d..5875c8e44 100644 --- a/framework/python/src/core/testrun.py +++ b/framework/python/src/core/testrun.py @@ -146,7 +146,8 @@ def _load_devices(self, device_dir): if 'max_device_reports' in device_config_json: max_device_reports = device_config_json.get(MAX_DEVICE_REPORTS_KEY) - device = Device(manufacturer=device_manufacturer, + device = Device(folder_url=os.path.join(device_dir, device_folder), + manufacturer=device_manufacturer, model=device_model, mac_addr=mac_addr, test_modules=test_modules,