def ifAuthenticatedAddEntry(user_id, raw_password): try: node = SensorNode.objects.get(sensor_id=user_id) except SensorNode.DoesNotExist: return HttpResponse("Unable to find node") except SensorNode.MultipleObjectsReturned: return HttpResponse("Should never occur!") if securelayer.checkPassword(raw_password): new_entry = SensorReading(node_id=user_id, timestamp=str(datetime.datetime.now())) new_entry.save() else: return "sorry wrong password"
def put_reading(request, node_id): # Valid values for type: { light, temperature, humidity, wind_speed, atmospheric_pressure } # Valid units: { cd/m^2, C, %, m/s, hPa / bar } try: username = request.POST.get('username') password = request.POST.get('password') SensorNode.objects.get(sensor_id=node_id) authenticated = auth.ifAuthenticatedAddEntry(username, password) except SensorNode.DoesNotExist: return JsonResponse({"message": "Unable to find node"}) if authenticated is True: light = request.POST.get('light') temp = request.POST.get('temp') pressure = request.POST.get('pressure') humidity = request.POST.get('humidity') wind_speed = request.POST.get('wind_speed') time = request.POST.get('time') lat = request.POST.get('lat') lon = request.POST.get('lon') altitude = request.POST.get('altitude') sealevel_pressure = request.POST.get('sealevel_pressure') reading = SensorReading( # node=node, node_id=request.POST.get('username'), timestamp= time, #str(datetime.datetime.now()), # Should *maybe* be time # it at least changes some assumptions on the data. temp=temp, pressure=pressure, humidity=humidity, wind_speed=wind_speed, lat=lat, lon=lon, light=light, altitude=altitude, sealevel_pressure=sealevel_pressure) print request.POST.get('username', 'NOT_AUTHENTICATED') reading.save() response_data = {} response_data['refresh_interval'] = REFRESH_RATE return JsonResponse(response_data) else: return JsonResponse( {"message": "Login error. Provide valid username & password!."})
def put_reading(request, node_id): # Valid values for type: { light, temperature, humidity, wind_speed, atmospheric_pressure } # Valid units: { cd/m^2, C, %, m/s, hPa / bar } try: username = request.POST.get('username') password = request.POST.get('password') SensorNode.objects.get(sensor_id=node_id) authenticated = auth.ifAuthenticatedAddEntry(username, password) except SensorNode.DoesNotExist: return JsonResponse({"message": "Unable to find node"}) if authenticated is True: light = request.POST.get('light') temp = request.POST.get('temp') pressure = request.POST.get('pressure') humidity = request.POST.get('humidity') wind_speed = request.POST.get('wind_speed') time = request.POST.get('time') lat = request.POST.get('lat') lon = request.POST.get('lon') altitude = request.POST.get('altitude') sealevel_pressure = request.POST.get('sealevel_pressure') reading = SensorReading( # node=node, node_id=request.POST.get('username'), timestamp=time, #str(datetime.datetime.now()), # Should *maybe* be time # it at least changes some assumptions on the data. temp=temp, pressure=pressure, humidity=humidity, wind_speed=wind_speed, lat=lat, lon=lon, light=light, altitude=altitude, sealevel_pressure=sealevel_pressure ) print request.POST.get('username', 'NOT_AUTHENTICATED') reading.save() response_data = {} response_data['refresh_interval'] = REFRESH_RATE return JsonResponse(response_data) else: return JsonResponse({"message": "Login error. Provide valid username & password!."})