Skip to content
Merged
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
13 changes: 12 additions & 1 deletion framework/python/src/api/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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",
Expand All @@ -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")
Expand Down
1 change: 1 addition & 0 deletions framework/python/src/common/device.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 9 additions & 1 deletion framework/python/src/common/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -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():
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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)

Expand Down
3 changes: 2 additions & 1 deletion framework/python/src/core/testrun.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down