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

Sync hardware-team with master #311

Merged
merged 28 commits into from Apr 6, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
cd0926d
add radioreceiver get API
VentusXu09 Mar 19, 2020
64ac80b
add radioreceiver post API
VentusXu09 Mar 21, 2020
4c5172e
fix code format
VentusXu09 Mar 21, 2020
3d030bc
add test for radioreceiver view
VentusXu09 Mar 23, 2020
11eae60
add serial port search for all operating systems
VentusXu09 Mar 23, 2020
29f6d27
add invalid port test for radioreceiver views
VentusXu09 Mar 23, 2020
f55001a
Merge pull request #152 from gcivil-nyu-org/backend/feature/radio_rec…
iamvibhorsingh Mar 24, 2020
f6496ee
add stimulator script for radioreceiver
VentusXu09 Mar 25, 2020
39415e7
fix call_script funtion cannot be called bug
VentusXu09 Mar 25, 2020
50c7665
add json data into argumnets in the script
VentusXu09 Mar 25, 2020
e836d9e
Merge pull request #166 from gcivil-nyu-org/backend/feature/stimulato…
iamvibhorsingh Mar 25, 2020
dcc327f
add tests for handle exception in radio receiver
VentusXu09 Mar 25, 2020
98a8930
add json data type check
VentusXu09 Mar 25, 2020
dc58cbd
Merge pull request #178 from gcivil-nyu-org/backend/feature/stimulato…
VentusXu09 Mar 25, 2020
0e5b6fc
Merge branch 'backend-team' into backend/develop
iamvibhorsingh Mar 25, 2020
24ebcae
Merge pull request #173 from gcivil-nyu-org/backend/develop
iamvibhorsingh Mar 25, 2020
c61e255
update both api and tests
VentusXu09 Apr 1, 2020
f142a16
update response status code
VentusXu09 Apr 1, 2020
a256ae2
replace mercury with ag_data models
VentusXu09 Apr 1, 2020
306a963
Merge branch 'master' into backend/sync-with-master
jackxujh Apr 2, 2020
dc18951
Merge pull request #248 from gcivil-nyu-org/backend/feature/update_ra…
jackxujh Apr 2, 2020
56eabfc
Merge branch 'backend/develop' into backend/sync-with-master
jackxujh Apr 2, 2020
a7a5ad8
Merge pull request #266 from gcivil-nyu-org/backend/sync-with-master
Ziheng-Wang Apr 2, 2020
a6a5240
Merge branch 'backend-team' into backend/develop
jackxujh Apr 2, 2020
b163d4c
Merge pull request #287 from gcivil-nyu-org/backend/develop
alldne Apr 4, 2020
3665b18
Merge remote-tracking branch 'origin/master' into backend/develop
alldne Apr 4, 2020
ff0d62b
Merge pull request #297 from gcivil-nyu-org/backend/develop
alldne Apr 4, 2020
0d52bc6
Merge pull request #300 from gcivil-nyu-org/backend-team
alldne Apr 4, 2020
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
20 changes: 20 additions & 0 deletions ag_data/serializers.py
@@ -0,0 +1,20 @@
from rest_framework import serializers

from ag_data.models import AGEvent, AGSensor, AGMeasurement


class AGMeasurementSerializer(serializers.ModelSerializer):
"""
Serializer for the Model AGMeasurement.
"""

sensor_id = serializers.PrimaryKeyRelatedField(
read_only=False, queryset=AGSensor.objects.all()
)
event_uuid = serializers.PrimaryKeyRelatedField(
read_only=False, queryset=AGEvent.objects.all()
)

class Meta:
model = AGMeasurement
fields = ("timestamp", "sensor_id", "event_uuid", "value")
2 changes: 1 addition & 1 deletion mercury/grafanaAPI/grafana_api.py
Expand Up @@ -93,7 +93,7 @@ def get_dashboard_by_event_name(self, event_name):
# If there are spaces in the name, the GF API will replace them with dashes
# to generate the "slug". A slug can be used to query the API.
endpoint = os.path.join(
self.hostname, "api/dashboards/db", event_name.lower().replace(" ", "-"),
self.hostname, "api/dashboards/db", event_name.lower().replace(" ", "-")
)
response = requests.get(url=endpoint, auth=("api_key", self.api_token))

Expand Down
25 changes: 5 additions & 20 deletions mercury/tests/test_configure_sensors.py
Expand Up @@ -34,10 +34,7 @@ class TestConfigureSensorView(TestCase):
test_sensor_update_object_name = "update_sensor"
test_type_update_object_name = "update_type"

test_sensor = {
"name": "wind speed sensor",
"type_id": test_type_object_name,
}
test_sensor = {"name": "wind speed sensor", "type_id": test_type_object_name}

test_sensor_type = {
"type-name": "fuel level",
Expand All @@ -57,14 +54,8 @@ def setUp(self):
name=self.test_type_object_name,
processing_formula=0,
format={
self.field_name_1: {
"data_type": self.data_type_1,
"unit": self.unit_1,
},
self.field_name_2: {
"data_type": self.data_type_1,
"unit": self.unit_1,
},
self.field_name_1: {"data_type": self.data_type_1, "unit": self.unit_1},
self.field_name_2: {"data_type": self.data_type_1, "unit": self.unit_1},
},
)
test_type_object.save()
Expand All @@ -78,14 +69,8 @@ def setUp(self):
name=self.test_type_update_object_name,
processing_formula=0,
format={
self.field_name_1: {
"data_type": self.data_type_1,
"unit": self.unit_1,
},
self.field_name_2: {
"data_type": self.data_type_1,
"unit": self.unit_1,
},
self.field_name_1: {"data_type": self.data_type_1, "unit": self.unit_1},
self.field_name_2: {"data_type": self.data_type_1, "unit": self.unit_1},
},
)

Expand Down
4 changes: 2 additions & 2 deletions mercury/tests/test_grafana.py
Expand Up @@ -354,7 +354,7 @@ def test_create_postgres_datasource(self):

# Query new datasource
endpoint = os.path.join(
self.grafana.endpoints["datasource_name"], self.datasource_name,
self.grafana.endpoints["datasource_name"], self.datasource_name
)
headers = {"Content-Type": "application/json"}
response = requests.get(
Expand Down Expand Up @@ -388,7 +388,7 @@ def test_delete_postgres_datasource(self):

# confirm that the datasource was actually deleted by querying it
endpoint = os.path.join(
self.grafana.endpoints["datasource_name"], self.datasource_name,
self.grafana.endpoints["datasource_name"], self.datasource_name
)
headers = {"Content-Type": "application/json"}
response = requests.get(
Expand Down
85 changes: 85 additions & 0 deletions mercury/tests/test_measurement.py
@@ -0,0 +1,85 @@
import datetime

import mock
from django.test import TestCase
from rest_framework.reverse import reverse

from ag_data.models import AGEvent, AGVenue


def fake_venue():
return AGVenue(
uuid="d81cac8d-26e1-4983-a942-1922e54a943a",
name="fake venue",
description="fake venue",
latitude=111.11,
longitude=111.11,
)


def fake_event(uuid):
"""
Mock a dummy AGEvent model
"""
return AGEvent(
uuid=uuid,
name="fake event",
description="fake event",
date=datetime.datetime(2020, 2, 2, 20, 21, 22),
venue_uuid=fake_venue(),
)


def fake_valid(res, raise_exception=True):
return True


class TestMeasurement(TestCase):
def setUp(self) -> None:
self.post_url = "mercury:measurement"
self.uuid = "d81cac8d-26e1-4983-a942-1922e54a943d"
self.uuid2 = "d81cac8d-26e1-4983-a942-1922e54a943a"

def post_radio_data(self):
# POST sensor data to the measurement url
response = self.client.post(
reverse(self.post_url, args=[self.uuid]),
data={
"sensor_id": 1,
"values": {"power": "2", "speed": 1},
"date": datetime.datetime(2020, 2, 2, 20, 21, 22),
},
)
return response

def post_defect_data(self):
response = self.client.post(
reverse(self.post_url, args=[self.uuid]),
data={
"values": {"power": "2", "speed": 1},
"date": datetime.datetime(2020, 2, 2, 20, 21, 22),
},
)
return response

def test_Radio_Receiver_POST_Event_Not_Exist(self):
response = self.client.post(reverse(self.post_url, args=[self.uuid2]))
self.assertEqual(404, response.status_code)

@mock.patch("ag_data.models.AGEvent.objects.get", fake_event)
def test_Radio_Receiver_POST_Missing_Params(self):
response = self.post_defect_data()
self.assertEqual(400, response.status_code)

@mock.patch("ag_data.models.AGEvent.objects.get", fake_event)
def test_Radio_Receiver_POST_Fail_to_Save(self):
response = self.post_radio_data()
self.assertEqual(400, response.status_code)

@mock.patch("ag_data.models.AGEvent.objects.get", fake_event)
@mock.patch("ag_data.serializers.AGMeasurementSerializer.is_valid", fake_valid)
@mock.patch("ag_data.serializers.AGMeasurementSerializer.save", fake_valid)
@mock.patch("ag_data.serializers.AGMeasurementSerializer.data", "")
def test_Radio_Receiver_POST_Event_Success(self):
response = self.post_radio_data()
self.assertEqual(201, response.status_code)
132 changes: 132 additions & 0 deletions mercury/tests/test_radio_receiver.py
@@ -0,0 +1,132 @@
from django.test import TestCase
from django.urls import reverse
import datetime
import mock

from ag_data.models import AGEvent, AGVenue


def fake_venue():
return AGVenue(
uuid="d81cac8d-26e1-4983-a942-1922e54a943a",
name="fake venue",
description="fake venue",
latitude=111.11,
longitude=111.11,
)


def fake_event(uuid):
"""
Mock a dummy AGEvent model
"""
return AGEvent(
uuid=uuid,
name="fake event",
description="fake event",
date=datetime.datetime(2020, 2, 2, 20, 21, 22),
venue_uuid=fake_venue(),
)


def fake_valid(res):
return True


def fake_valid_port():
return ["dev/tty.USB"]


def fake_invalid_port():
return []


class TestRadioReceiverView(TestCase):
def setUp(self) -> None:
self.get_url = "mercury:radioreceiver"
self.post_url = "mercury:radioreceiver"
self.uuid = "d81cac8d-26e1-4983-a942-1922e54a943d"
self.uuid2 = "d81cac8d-26e1-4983-a942-1922e54a943a"

def test_Radio_Receiver_GET_No_Related_Event(self):
response = self.client.get(reverse(self.get_url, args=[self.uuid2]))
self.assertEqual(404, response.status_code)

@mock.patch("ag_data.models.AGEvent.objects.get", fake_event)
def test_Radio_Receiver_GET_Missing_Enable(self):
response = self.client.get(
reverse(self.get_url, args=[self.uuid]),
data={
"baudrate": 9000,
"bytesize": 8,
"parity": "N",
"stopbits": 1,
"timeout": 1,
},
)
self.assertEqual(400, response.status_code)

@mock.patch("ag_data.models.AGEvent.objects.get", fake_event)
@mock.patch("mercury.views.radioreceiver.serial_ports", fake_invalid_port)
def test_Radio_Receiver_GET_No_Valid_Port(self):
response = self.client.get(
reverse(self.get_url, args=[self.uuid]),
data={
"enable": 1,
"baudrate": 9000,
"bytesize": 8,
"parity": "N",
"stopbits": 1,
"timeout": 1,
},
)
self.assertEqual(503, response.status_code)

@mock.patch("ag_data.models.AGEvent.objects.get", fake_event)
@mock.patch("mercury.views.radioreceiver.serial_ports", fake_valid_port)
def test_Radio_Receiver_GET_Success(self):
response = self.client.get(
reverse(self.get_url, args=[self.uuid]),
data={
"enable": 1,
"baudrate": 9000,
"bytesize": 8,
"parity": "N",
"stopbits": 1,
"timeout": 1,
},
)
self.assertEqual(200, response.status_code)

@mock.patch("ag_data.models.AGEvent.objects.get", fake_event)
@mock.patch("mercury.views.radioreceiver.serial_ports", fake_valid_port)
@mock.patch("mercury.views.radioreceiver.check_port", fake_valid)
def test_Radio_Receiver_GET_Close_Port_Success(self):
response = self.client.get(
reverse(self.get_url, args=[self.uuid]),
data={
"enable": 0,
"baudrate": 9000,
"bytesize": 8,
"parity": "N",
"stopbits": 1,
"timeout": 1,
},
)
self.assertEqual(200, response.status_code)

@mock.patch("ag_data.models.AGEvent.objects.get", fake_event)
def test_Radio_Receiver_Fake_GET_Success(self):
response = self.client.get(
reverse(self.get_url, args=[self.uuid]),
data={
"enable": 1,
"baudrate": 9000,
"bytesize": 8,
"parity": "N",
"stopbits": 1,
"timeout": 1,
"fake": 1,
},
)
self.assertEqual(200, response.status_code)
12 changes: 12 additions & 0 deletions mercury/urls.py
Expand Up @@ -4,7 +4,9 @@
sensor,
events,
pitcrew,
radioreceiver,
gf_config,
measurement,
)

app_name = "mercury"
Expand Down Expand Up @@ -40,11 +42,21 @@
path("events/export/<uuid:event_uuid>/csv", events.export_event),
path("events/export/<uuid:event_uuid>/json", events.export_event),
path("pitcrew/", pitcrew.PitCrewView.as_view(), name="pitcrew"),
path(
"radioreceiver/<uuid:event_uuid>",
radioreceiver.RadioReceiverView.as_view(),
name="radioreceiver",
),
path("gfconfig/", gf_config.GFConfigView.as_view(), name="gfconfig"),
path(
"gfconfig/delete/<int:gf_id>", gf_config.delete_config, name="gfconfig_delete"
),
path(
"gfconfig/update/<int:gf_id>", gf_config.update_config, name="gfconfig_update"
),
path(
"measurement/<uuid:event_uuid>",
measurement.MeasurementView.as_view(),
name="measurement",
),
]
5 changes: 1 addition & 4 deletions mercury/views/events.py
Expand Up @@ -77,10 +77,7 @@ def export_event(request, event_uuid=None, file_format="CSV"):
}
measurement_info.append(temp)

data = {
"event_info": event_info,
"measurement_info": measurement_info,
}
data = {"event_info": event_info, "measurement_info": measurement_info}

response = HttpResponse(str(data), content_type="application/json")
response["Content-Disposition"] = (
Expand Down