Пример #1
0
    def post(self, request, streamId=None):

        try:
            readings = json.loads(request.body)
        except (TypeError, ValueError):
            return cors_http_response("Bad Json", 400)

        if streamId is not None:
            return add_readings(readings, streamId)
        else:
            return add_readings(readings)
Пример #2
0
def add_reading(request):
    """
    ' Adds a single reading to the database. To insert a reading we need a uuid from a sensor and its value
    ' that it wants to post.
    """
    timestamp = int(time.time())

    try:
        uuid = request.POST["uuid"]
    except KeyError:
        return cors_http_response("A uuid is required", 404)

    try:
        value = request.POST["value"]
        if value is None or value == "":
            return cors_http_response("No data was passed for insertion!", 404)
    except KeyError:
        return cors_http_response("A value is required", 404)

    try:
        sensor = Sensor.objects.get(uuid=uuid)
        claimedSensor = ClaimedSensor.objects.get(sensor=sensor)
        datastream = DataStream.objects.get(claimed_sensor=claimedSensor)
    except Sensor.DoesNotExist:
        return cors_http_response("There is no sensor for the uuid '%s'" % str(uuid), 404)
    except ClaimedSensor.DoesNotExist:
        return cors_http_response("Sensor with uuid '%s' is not claimed!" % str(uuid), 404)
    except DataStream.DoesNotExist:
        return cors_http_response("There is no datastream for the sensor with uuid %s" % str(uuid), 404)

    # Insert
    try:
        insert_reading(datastream, sensor, value, timestamp)
    except SensorReadingCollision as e:
        return cors_http_response(e, 500)
    except ValidationError as e:
        msg = "There was one or more errors while saving a sensor reading: "
        msg += ", ".join([field + ": " + error for field, error in e.message_dict.iteritems()])
        return cors_http_response(msg, 500)

    return cors_http_response("Successfully inserted record")