Skip to content
Merged
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
35 changes: 16 additions & 19 deletions app/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -293,35 +293,32 @@ def storeData(request):
macAddress = data['macAddress']
measure = data["measure"]


# Device verification
if not Device.objects.all().filter(api_token=apiToken).exists():
return Response({'message': 'invalid api token.'}, status=status.HTTP_401_UNAUTHORIZED)

if not Device.objects.get(api_token=apiToken).is_authorized == 2:
return Response({'message': 'device not authorized.'}, status=status.HTTP_401_UNAUTHORIZED)

if apiToken and measure is not None:
for i in measure:
device = Device.objects.get(api_token=apiToken)

if device.mac_address == macAddress:
try:
total = Data.objects.all().filter(device=device, type=i["type"]).order_by('id').reverse()

if total:
storeData = Data(device=device, type=i["type"], last_collection=float(i["value"]), total=(float(total[0].total) + float(i["value"])))
storeData.save()
else:
storeData = Data(device=device, type=i["type"], last_collection=float(i["value"]), total=float(i["value"]))
storeData.save()

return Response({'message': 'data stored.'}, status=status.HTTP_200_OK)
except:
return Response({'message': 'something went wrong.'}, status=status.HTTP_400_BAD_REQUEST)
else:
return Response({'message': 'api token and mac address does not match.'}, status=status.HTTP_400_BAD_REQUEST)

try:
total = Data.objects.all().filter(device=device, type=i["type"]).order_by('id').reverse()

if total:
storeData = Data(device=device, type=i["type"], last_collection=float(i["value"]), total=(float(total[0].total) + float(i["value"])))
storeData.save()
else:
storeData = Data(device=device, type=i["type"], last_collection=float(i["value"]), total=float(i["value"]))
storeData.save()

except:
return Response({'message': 'something went wrong.'}, status=status.HTTP_400_BAD_REQUEST)

return Response({'message': 'data stored.'}, status=status.HTTP_200_OK)

else:
return Response({'message': 'data not received.'}, status=status.HTTP_400_BAD_REQUEST)

Expand Down