Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- @csrf_exempt
- def save_measurement(request):
- if request.method == "POST":
- response = {}
- json_data = json.loads(request.body)
- auth_token = json_data.get("auth_token")
- if not auth_token:
- return HttpResponse('Authentication token required')
- try:
- auth_token = Token.objects.get(key=auth_token)
- user = Patient.objects.get(user=auth_token.user)
- except Exception as e:
- return HttpResponse('Invalid authentication token')
- android_data = json_data.get("android_client")
- device_data = json_data.get("health_device")
- measurement_data = json_data.get("measurements")
- # MAC validation?
- try:
- android = Android.objects.get(mac_adress=android_data["mac"])
- except ObjectDoesNotExist:
- #if not android:
- android = Android(mac_adress=android_data["mac"],
- last_connect=datetime.now(),
- battery_charge=android_data["battery_charge"],
- last_user=user)
- android.save()
- except Exception as e:
- print('Android MAC error: %s' % e)
- #response['message'] = 'Invalid Android MAC'
- return HttpResponse('Invalid Android MAC')
- else:
- android.last_connect = datetime.now()
- android.battery_charge = android_data["battery_charge"]
- android.last_user = user
- android.save()
- device_type = DeviceTypes.objects.get(type=device_data["type"])
- try:
- device = HealthDevice.objects.get(mac_adress=device_data["mac"])
- except Exception as e:
- #if not device:
- device = HealthDevice(mac_adress=device_data["mac"],
- last_connect=datetime.now(),
- battery_charge=device_data["battery_charge"],
- device_type_id=device_type.id,
- device_species_id=40, # !!!
- last_user=user)
- device.save()
- except Exception as e:
- print('Device MAC error: %s' % e)
- #response['message'] = 'Invalid device MAC'
- return HttpResponse('Invalid device MAC')
- else:
- device.last_connect = datetime.now()
- device.battery_charge = device_data["battery_charge"]
- device.last_user = user
- device.save()
- sync_dt = datetime.strptime(measurement_data['datetime'], '%Y-%m-%d-%H-%M-%S')
- if device_data["type"] == "tonometer": # or (device_data["type"] == "wristband" and 'systolic_rate' in measurement_data)
- dev = Tonometer(user=user,
- datetime=datetime.now(),
- sync_datetime=sync_dt,
- android_mac = android,
- HR=measurement_data['pulse_rate'],
- HP=measurement_data['systolic_pressure'],
- LP=measurement_data['diastolic_pressure'],
- AFTER_MED=measurement_data.get('after_medicine', False)) #?
- dev.save()
- elif device_data["type"] == "oximeter": # or (device_data["type"] == "wristband" and 'blood_oxygen' in measurement_data)
- dev = Oximeter(user=user,
- datetime=datetime.now(),
- sync_datetime=sync_dt,
- android_mac = android,
- HR=measurement_data['pulse_rate'],
- BO=measurement_data['blood_oxygen'])
- elif device_data["type"] == "thermometer": # or (device_data["type"] == "wristband" and 'temperature' in measurement_data)
- dev = Thermometer(user=user,
- datetime=datetime.now(),
- sync_datetime=sync_dt,
- android_mac = android,
- TM=measurement_data['temperature'])
- elif device_data["type"] == "glucometer": # or (device_data["type"] == "wristband" and 'blood_glucose' in measurement_data)
- dev = Glucometer(user=user,
- datetime=datetime.now(),
- sync_datetime=sync_dt,
- android_mac = android,
- BG=measurement_data['blood_glucose'],
- AFTER_EAT=measurement_data.get('after_meal', True))
- elif device_data["type"] == "wristband":
- if 'pulse_rate' in measurement_data:
- dev = WristbandHR(user=user,
- datetime=datetime.now(),
- sync_datetime=sync_dt,
- android_mac = android,
- HR=measurement_data['pulse_rate'])
- elif 'blood_oxygen' in measurement_data:
- dev = WristbandBO(user=user,
- datetime=datetime.now(),
- sync_datetime=sync_dt,
- android_mac = android,
- BO=measurement_data['blood_oxygen'])
- elif 'systolic_rate' in measurement_data:
- dev = Wristband(user=user,
- datetime=datetime.now(),
- sync_datetime=sync_dt,
- android_mac = android,
- HP=measurement_data['systolic_rate'],
- LP=measurement_data['diastolic_rate'])
- else:
- HttpResponse('Invalid health device type')
- else:
- HttpResponse('Invalid health device type')
- dev.save()
- response['message'] = 'Measurement data saved'
- return HttpResponse(json.dumps(response), content_type='application/json')
- else:
- return HttpResponse('POST required')
Add Comment
Please, Sign In to add comment