Mochinov

Untitled

Dec 26th, 2020 (edited)
294
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.50 KB | None | 0 0
  1.  
  2. @csrf_exempt
  3. def save_measurement(request):
  4.     if request.method == "POST":
  5.         response = {}
  6.         json_data = json.loads(request.body)
  7.         auth_token = json_data.get("auth_token")
  8.         if not auth_token:
  9.             return HttpResponse('Authentication token required')
  10.         try:
  11.             auth_token = Token.objects.get(key=auth_token)
  12.             user = Patient.objects.get(user=auth_token.user)
  13.         except Exception as e:
  14.             return HttpResponse('Invalid authentication token')
  15.         android_data = json_data.get("android_client")
  16.         device_data = json_data.get("health_device")
  17.         measurement_data = json_data.get("measurements")
  18.  
  19.         # MAC validation?
  20.         try:
  21.             android = Android.objects.get(mac_adress=android_data["mac"])
  22.         except ObjectDoesNotExist:
  23.         #if not android:
  24.             android = Android(mac_adress=android_data["mac"],
  25.                 last_connect=datetime.now(),
  26.                 battery_charge=android_data["battery_charge"],
  27.                 last_user=user)
  28.             android.save()
  29.         except Exception as e:
  30.             print('Android MAC error: %s' % e)
  31.             #response['message'] = 'Invalid Android MAC'
  32.             return HttpResponse('Invalid Android MAC')
  33.         else:
  34.             android.last_connect = datetime.now()
  35.             android.battery_charge = android_data["battery_charge"]
  36.             android.last_user = user
  37.             android.save()
  38.  
  39.         device_type = DeviceTypes.objects.get(type=device_data["type"])
  40.         try:
  41.             device = HealthDevice.objects.get(mac_adress=device_data["mac"])
  42.         except Exception as e:
  43.         #if not device:
  44.             device = HealthDevice(mac_adress=device_data["mac"],
  45.                 last_connect=datetime.now(),
  46.                 battery_charge=device_data["battery_charge"],
  47.                 device_type_id=device_type.id,
  48.                 device_species_id=40, # !!!
  49.                 last_user=user)
  50.             device.save()
  51.         except Exception as e:
  52.             print('Device MAC error: %s' % e)
  53.             #response['message'] = 'Invalid device MAC'
  54.             return HttpResponse('Invalid device MAC')
  55.         else:
  56.             device.last_connect = datetime.now()
  57.             device.battery_charge = device_data["battery_charge"]
  58.             device.last_user = user
  59.             device.save()
  60.  
  61.         sync_dt = datetime.strptime(measurement_data['datetime'], '%Y-%m-%d-%H-%M-%S')
  62.  
  63.         if device_data["type"] == "tonometer": # or (device_data["type"] == "wristband" and 'systolic_rate' in measurement_data)
  64.             dev = Tonometer(user=user,
  65.                 datetime=datetime.now(),    
  66.                 sync_datetime=sync_dt,
  67.                 android_mac = android,
  68.                 HR=measurement_data['pulse_rate'],
  69.                 HP=measurement_data['systolic_pressure'],
  70.                 LP=measurement_data['diastolic_pressure'],
  71.                 AFTER_MED=measurement_data.get('after_medicine', False)) #?
  72.             dev.save()
  73.         elif device_data["type"] == "oximeter": # or (device_data["type"] == "wristband" and 'blood_oxygen' in measurement_data)
  74.             dev = Oximeter(user=user,
  75.                 datetime=datetime.now(),
  76.                 sync_datetime=sync_dt,
  77.                 android_mac = android,
  78.                 HR=measurement_data['pulse_rate'],
  79.                 BO=measurement_data['blood_oxygen'])
  80.         elif device_data["type"] == "thermometer": # or (device_data["type"] == "wristband" and 'temperature' in measurement_data)
  81.             dev = Thermometer(user=user,
  82.                 datetime=datetime.now(),
  83.                 sync_datetime=sync_dt,
  84.                 android_mac = android,
  85.                 TM=measurement_data['temperature'])
  86.         elif device_data["type"] == "glucometer": #  or (device_data["type"] == "wristband" and 'blood_glucose' in measurement_data)
  87.             dev = Glucometer(user=user,
  88.                 datetime=datetime.now(),
  89.                 sync_datetime=sync_dt,
  90.                 android_mac = android,
  91.                 BG=measurement_data['blood_glucose'],
  92.                 AFTER_EAT=measurement_data.get('after_meal', True))
  93.         elif device_data["type"] == "wristband":
  94.             if 'pulse_rate' in measurement_data:
  95.                 dev = WristbandHR(user=user,
  96.                     datetime=datetime.now(),
  97.                     sync_datetime=sync_dt,
  98.                     android_mac = android,
  99.                     HR=measurement_data['pulse_rate'])
  100.             elif 'blood_oxygen' in measurement_data:
  101.                 dev = WristbandBO(user=user,
  102.                     datetime=datetime.now(),
  103.                     sync_datetime=sync_dt,
  104.                     android_mac = android,
  105.                     BO=measurement_data['blood_oxygen'])
  106.             elif 'systolic_rate' in measurement_data:
  107.                 dev = Wristband(user=user,
  108.                     datetime=datetime.now(),
  109.                     sync_datetime=sync_dt,
  110.                     android_mac = android,
  111.                     HP=measurement_data['systolic_rate'],
  112.                     LP=measurement_data['diastolic_rate'])
  113.             else:
  114.                 HttpResponse('Invalid health device type')
  115.         else:
  116.             HttpResponse('Invalid health device type')
  117.        
  118.         dev.save()
  119.  
  120.         response['message'] = 'Measurement data saved'
  121.         return HttpResponse(json.dumps(response), content_type='application/json')
  122.     else:
  123.         return HttpResponse('POST required')
  124.  
Add Comment
Please, Sign In to add comment