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

Backend team #329

Merged
merged 10 commits into from Apr 9, 2020
12 changes: 12 additions & 0 deletions mercury/tests/test_measurement.py
Expand Up @@ -37,9 +37,21 @@ def fake_valid(res, raise_exception=True):
class TestMeasurement(TestCase):
def setUp(self) -> None:
self.post_url = "mercury:measurement"
self.post_url2 = "mercury:measurementWO"
self.uuid = "d81cac8d-26e1-4983-a942-1922e54a943d"
self.uuid2 = "d81cac8d-26e1-4983-a942-1922e54a943a"

def post_radio_data_wo_event(self):
response = self.client.post(
reverse(self.post_url2),
data={
"sensor_id": 1,
"values": {"power": "2", "speed": 1},
"date": datetime.datetime(2020, 2, 2, 20, 21, 22),
},
)
return response

def post_radio_data(self):
# POST sensor data to the measurement url
response = self.client.post(
Expand Down
5 changes: 5 additions & 0 deletions mercury/urls.py
Expand Up @@ -76,4 +76,9 @@
measurement.MeasurementView.as_view(),
name="measurement",
),
path(
"measurement/",
measurement.MeasurementWithoutEvent.as_view(),
name="measurementWO",
),
]
65 changes: 44 additions & 21 deletions mercury/views/measurement.py
Expand Up @@ -12,12 +12,42 @@ def build_error(str):
return json.dumps({"error": str})


def add_measurement(request, event):
json_data = request.data
if isinstance(json_data, str):
json_data = json.loads(json_data)

res = {"event_uuid": event.uuid}
key_map = {
"timestamp": "date",
"sensor_id": "sensor_id",
"value": "values",
}

for key, json_key in key_map.items():
if json_key not in json_data:
return Response(
build_error("Missing required params " + json_key),
status=status.HTTP_400_BAD_REQUEST,
)
res[key] = json_data[json_key]

serializer = AGMeasurementSerializer(data=res)
try:
serializer.is_valid(raise_exception=True)
serializer.save()
except serializers.ValidationError:
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
else:
return Response(serializer.data, status=status.HTTP_201_CREATED)


class MeasurementView(APIView):
def post(self, request, event_uuid=None):
"""
The post receives sensor data through internet
Url example:
http://localhost:8000/radioreceiver/d81cac8d-26e1-4983-a942-1922e54a943d
http://localhost:8000/measurement/d81cac8d-26e1-4983-a942-1922e54a943d
Post Json Data Example
{
"sensor_id": 1,
Expand All @@ -38,26 +68,19 @@ def post(self, request, event_uuid=None):
build_error("Event uuid not found"), status=status.HTTP_404_NOT_FOUND
)

json_data = request.data
if isinstance(json_data, str):
json_data = json.loads(json_data)
return add_measurement(request, event)

res = {"event_uuid": event_uuid}
dic = {"timestamp": "date", "sensor_id": "sensor_id", "value": "values"}

for d in dic:
if json_data.get(dic[d]) is None:
return Response(
build_error("Missing required params " + dic[d]),
status=status.HTTP_400_BAD_REQUEST,
)
res[d] = json_data[dic[d]]

serializer = AGMeasurementSerializer(data=res)
class MeasurementWithoutEvent(APIView):
def post(self, request):
"""
TODO: fetch the active event
Now we use the first event in the db
"""
try:
serializer.is_valid(raise_exception=True)
serializer.save()
except serializers.ValidationError:
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
else:
return Response(serializer.data, status=status.HTTP_201_CREATED)
events = AGEvent.objects.all()
event = events.first()
except AGEvent.DoesNotExist:
event = False

return add_measurement(request, event)