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

55 nrf connect #97

Open
wants to merge 11 commits into
base: main
Choose a base branch
from
4 changes: 2 additions & 2 deletions server/server_comm/device_connection/apps.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@


class DeviceConnectionConfig(AppConfig):
default_auto_field = 'django.db.models.BigAutoField'
name = 'device_connection'
default_auto_field = "django.db.models.BigAutoField"
name = "device_connection"
6 changes: 6 additions & 0 deletions server/server_comm/device_connection/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from .views import (
AndroidDeviceConnectionView,
APIConnectionView,
FlowDeviceConnectionView,
SerialDeviceConnectionView,
)

Expand All @@ -18,4 +19,9 @@
name="android-connection",
),
path("api/", APIConnectionView.as_view(), name="api-connection"),
path(
"connect_flow/",
FlowDeviceConnectionView.as_view(),
name="flow-connection",
),
]
122 changes: 61 additions & 61 deletions server/server_comm/device_connection/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,53 +23,53 @@ def check_connection(self, conn_id: str):
device_port = self.get_device_port(conn_id)
serial_device = Serial(device_port, timeout=10)

serial_device.write(b'ping\n')
serial_device.write(b"ping\n")
time.sleep(1)
res = serial_device.readline().strip()

if res:
return JsonResponse(
{
'status': 'connected',
'message': 'serial device connected',
'response': res,
"status": "connected",
"message": "serial device connected",
"response": res,
}
)
else:
return JsonResponse(
{
'status': 'no_response',
'message': 'serial device did not respond',
'response': None,
"status": "no_response",
"message": "serial device did not respond",
"response": None,
}
)
except SerialTimeoutException as e:
return JsonResponse(
{
'status': 'error',
'message': 'serial device timed out',
'response': str(e),
"status": "error",
"message": "serial device timed out",
"response": str(e),
}
)
except Exception as e:
return JsonResponse(
{
'status': 'error',
'message': 'an error occurred',
'response': str(e),
"status": "error",
"message": "an error occurred",
"response": str(e),
}
)

def get(self, request):
conn_id = request.GET.get('conn_id')
conn_id = request.GET.get("conn_id")
if conn_id:
return self.check_connection(conn_id)
else:
return JsonResponse(
{
'status': 'error',
'message': 'conn_id is required',
'response': None,
"status": "error",
"message": "conn_id is required",
"response": None,
}
)

Expand All @@ -78,35 +78,35 @@ class AndroidDeviceConnectionView(View):
def get_adb_devices(self):
try:
adb_devices = subprocess.run(
['adb', 'devices'], capture_output=True, text=True, check=True
["adb", "devices"], capture_output=True, text=True, check=True
).stdout.splitlines()[1:-1]
adb_devices = [line.split("\t")[0] for line in adb_devices]
return adb_devices

except subprocess.CalledProcessError as e:
return JsonResponse(
{
'status': 'error',
'message': 'adb command failed',
'response': str(e),
"status": "error",
"message": "adb command failed",
"response": str(e),
}
)

except FileNotFoundError as e:
return JsonResponse(
{
'status': 'error',
'message': 'adb not found',
'response': str(e),
"status": "error",
"message": "adb not found",
"response": str(e),
}
)

except Exception as e:
return JsonResponse(
{
'status': 'error',
'message': 'an error occurred',
'response': str(e),
"status": "error",
"message": "an error occurred",
"response": str(e),
}
)

Expand All @@ -116,30 +116,30 @@ def check_connection(self, conn_id: str):
if conn_id in adb_devices:
return JsonResponse(
{
'status': 'connected',
'message': 'android device connected',
'response': None,
"status": "connected",
"message": "android device connected",
"response": None,
}
)
else:
return JsonResponse(
{
'status': 'not_connected',
'message': 'android device not connected',
'response': None,
"status": "not_connected",
"message": "android device not connected",
"response": None,
}
)

def get(self, request):
conn_id = request.GET.get('conn_id')
conn_id = request.GET.get("conn_id")
if conn_id:
return self.check_connection(conn_id)
else:
return JsonResponse(
{
'status': 'error',
'message': 'conn_id is required',
'response': None,
"status": "error",
"message": "conn_id is required",
"response": None,
}
)

Expand All @@ -152,56 +152,56 @@ def check_connection(self, api_url: str):
if res.status_code == 200:
return JsonResponse(
{
'status': 'connected',
'message': 'connected to api',
'response': res.json(),
"status": "connected",
"message": "connected to api",
"response": res.json(),
}
)
else:
JsonResponse(
{
'status': 'error',
'message': (
f'unexpected api response {res.status_code}'
"status": "error",
"message": (
f"unexpected api response {res.status_code}"
),
'response': res.json(),
"response": res.json(),
}
)
except requests.exceptions.Timeout as e:
return JsonResponse(
{
'status': 'error',
'message': 'api request timed out',
'response': str(e),
"status": "error",
"message": "api request timed out",
"response": str(e),
}
)
except requests.exceptions.ConnectionError as e:
return JsonResponse(
{
'status': 'error',
'message': 'could not connect to api',
'response': str(e),
"status": "error",
"message": "could not connect to api",
"response": str(e),
}
)
except Exception as e:
return JsonResponse(
{
'status': 'error',
'message': 'an error occurred',
'response': str(e),
"status": "error",
"message": "an error occurred",
"response": str(e),
}
)

def get(self, request):
api_url = request.GET.get('api_url')
api_url = request.GET.get("api_url")
if api_url:
return self.check_connection(api_url)
else:
return JsonResponse(
{
'status': 'error',
'message': 'api_url is required',
'response': None,
"status": "error",
"message": "api_url is required",
"response": None,
}
)

Expand Down Expand Up @@ -242,14 +242,14 @@ def connect_devices(self, flow_id: str):
android_view.check_connection(conn_id)

def get(self, request):
flow_id = request.GET.get('flow_id')
flow_id = request.GET.get("flow_id")
if flow_id:
return self.connect_devices(flow_id)
else:
return JsonResponse(
{
'status': 'error',
'message': 'flow_id is required',
'response': None,
"status": "error",
"message": "flow_id is required",
"response": None,
}
)
2 changes: 1 addition & 1 deletion server/server_comm/server_comm/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@
WSGI_APPLICATION = "server_comm.wsgi.application"

CORS_ALLOWED_ORIGINS = [
'http://localhost:5173',
"http://localhost:5173",
]

# Database
Expand Down
4 changes: 2 additions & 2 deletions server/server_comm/test_runner/apps.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@


class TestRunnerConfig(AppConfig):
default_auto_field = 'django.db.models.BigAutoField'
name = 'test_runner'
default_auto_field = "django.db.models.BigAutoField"
name = "test_runner"
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?xml version="1.0" encoding="UTF-8"?>

<test-suite description="Check Characteristic UUID">
<target id="devkit" name="Kit" address="${MAC_ADDRESS}"/>

<test id="characteristic-uuid" description="Check Characteristic UUID">
<connect target="devkit" timeout="5000" />
<discover-services target="devkit" timeout="5000" />
<assert-service target="devkit" uuid="${SERVICE_UUID}">
<assert-characteristic target="devkit" uuid="${CHARACTERISTIC_UUID}" />
</assert-service>
</test>

<run-test ref="characteristic-uuid" />
</test-suite>
13 changes: 13 additions & 0 deletions server/server_comm/test_runner/nrf_scripts/check_service.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>

<test-suite description="Check Service UUID">
<target id="devkit" name="Kit" address="${MAC_ADDRESS}"/>

<test id="service-uuid" description="Check Service UUID">
<connect target="devkit" timeout="5000" />
<discover-services target="devkit" timeout="5000" />
<assert-service target="devkit" uuid="${SERVICE_UUID}" />
</test>

<run-test ref="service-uuid" />
</test-suite>
16 changes: 16 additions & 0 deletions server/server_comm/test_runner/nrf_scripts/connection.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?xml version="1.0" encoding="UTF-8"?>

<test-suite description="Connect">
<target id="devkit" name="Kit" address="${MAC_ADDRESS}"/>

<test id="connect" description="Connect">
<connect target="devkit" timeout="5000" />
<refresh target="devkit" timeout="5000" />
<read-rssi target="devkit" timeout="5000" >
<assert-value value="-100+"/>
</read-rssi>
<discover-services target="devkit" timeout="5000" />
</test>

<run-test ref="connect" />
</test-suite>
16 changes: 16 additions & 0 deletions server/server_comm/test_runner/nrf_scripts/custom_script.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?xml version="1.0" encoding="UTF-8"?>

<test-suite description="Custom script">
<target id="devkit" name="Kit" address="${MAC_ADDRESS}"/>

<test id="led-on" description="Turn LED on">
<connect target="devkit" timeout="5000" />
<discover-services target="devkit" timeout="5000" />
<write service-uuid="${SERVICE_UUID}"
characteristic-uuid="${CHARACTERISTIC_UUID}"
value="01"
target="devkit"/>
</test>

<run-test ref="led-on" />
</test-suite>
Loading
Loading