From 6c3690b8cf98e9859bdc87f16ebaa7f4c944f06d Mon Sep 17 00:00:00 2001 From: EliHaugu Date: Tue, 5 Nov 2024 01:24:00 +0100 Subject: [PATCH] Add nRF connection to test runner --- server/server_comm/test_runner/views.py | 54 +++++++++++++++++++++++-- 1 file changed, 51 insertions(+), 3 deletions(-) diff --git a/server/server_comm/test_runner/views.py b/server/server_comm/test_runner/views.py index 1fc76fd..9377e08 100644 --- a/server/server_comm/test_runner/views.py +++ b/server/server_comm/test_runner/views.py @@ -5,6 +5,7 @@ from django.shortcuts import get_object_or_404 from django.urls import reverse from flow_parser import FlowParser +from test_runner.nrf_scripts.nrf_connect import run_check_connection from rest_framework import status from rest_framework.response import Response from rest_framework.views import APIView @@ -91,15 +92,61 @@ def check_device_connections(self, flow_id): "message": "Failed to check device connections", } + def get_nrf_devices(self, devices): + """ + TODO: Placeholder function, logic for deciding what is an nrf device + """ + return devices + + def connect_ble_nrf_devices(self, flow_id): + """ + Connect select device(s) in command nodes to nrf kit + """ + # TODO: does the user need to make category "Android" for this to work + # what if they wanna categorize differently? + # not all BLE/android devices should be connected, no? who should be? + # TODO: how do we check UUIDs? need to know if service/characteristic + + protocol = 'https' if self.request.is_secure() else 'http' + base_url = f"{protocol}://{self.request.get_host()}" + get_devices_url = f""" + {base_url}{reverse('devices')} + """ + response = requests.get(get_devices_url) + + if response.status_code == 200: + devices = response.json() + else: + return { + "status": "error", + "message": "Failed to get devices", + "response": response, + } + + nrf_devices = self.get_nrf_devices(devices) + + for device in nrf_devices: + dev_id = device.device_id + mac = device.communication_ids.get("mac_address") + res = run_check_connection(dev_id, mac) + + if res.get("status") == "error": + raise RuntimeError( + f"{res.get("message")}\n{res.get("response")}" + ) + return { + "status": "success", + "message": "Connected all nRF devices", + "response": "", + } + def test_setup(self, flow_id): """ Set up and assert device connections for the test flow. """ self.check_device_connections(flow_id) - # TODO Connect android device(s) in command nodes to nrf kit (LIL-90) - - # TODO Assert that connection is setup (LIL-90) + self.connect_ble_nrf_devices(flow_id) def run_node(self, node): """ @@ -112,6 +159,7 @@ def run_node(self, node): result = True elif node.node_type == Node.ACTION: try: + # TODO: HOW DO WE RUN NRF SCRIPTS? exec(node.function) result = True except Exception as e: