예제 #1
0
    def get(self, node_id, sensor_id, value_type):
        sensor_node = db.session.query(SensorNode).filter(SensorNode.api_id == node_id).first()

        value_type_id = value_types.get_id(value_type)
        if value_type_id is None:
            return {"message": "Not found"}, 404

        value_name = value_types.get_short_name(value_type_id)

        query = "SELECT value FROM {value_name} WHERE ni='{node_id:d}' AND si='{sensor_id:d}'".format(
            node_id=sensor_node.id,
            sensor_id=sensor_id,
            value_name=value_name
        )

        from influxdb import InfluxDBClient
        client = InfluxDBClient(
            host="localhost",
            port=8086,
            database="metrics"
        )
        result = client.query(query)
        values = []
        for p in result.get_points():
            values.append({
                "date": p["time"],
                "value": p["value"]
            })
        return {
            "values": values
        }
예제 #2
0
    def post(self, node_id):
        if "X-Sensor-Api-Key" not in request.headers:
            return {"message": "No API key in header found"}, 401

        sensor_node = db.session.query(SensorNode).filter(SensorNode.api_id == node_id).first()
        if sensor_node is None:
            return {"message": "Node not found"}, 404

        if sensor_node.api_key.hex != request.headers["X-Sensor-Api-Key"]:
            return {"message": "Invalid API key"}, 401

        utc_now = datetime.utcnow()
        sensor_node.last_seen_at = utc_now

        data = request.data.decode("utf-8")
        data = json.loads(data)

        reading_types = sensor_node.reading_types
        for reading in data:
            idx, sensor_type, value_type, value = reading
            found = False
            for reading_type in reading_types:
                if reading_type.sensor_index == idx and \
                        reading_type.sensor_type == sensor_type and \
                        reading_type.value_type == value_type:
                    reading_type.last_seen_at = utc_now
                    found = True
                    break

            if not found:
                reading_type = SensorReadingType(
                    sensor_node=sensor_node,
                    sensor_index=idx,
                    sensor_type=sensor_type,
                    value_type=value_type,
                    last_seen_at=utc_now
                )
                db.session.add(reading_type)

        """
        collection = SensorReadingCollection(sensor_node=sensor_node)
        db.session.add(collection)
        for reading in data:
            idx, sensor_type, value_type, value = reading

            sensor_reading = SensorReading(
                sensor_index=idx,
                sensor_type=sensor_type,
                value_type=value_type,
                value=value,
                collection=collection
            )
            db.session.add(sensor_reading)
        """
        db.session.commit()

        metrics = []
        for reading in data:
            idx, sensor_type, value_type, value = reading
            value_name = value_types.get_short_name(value_type)
            metrics.append({
                "measurement": value_name,
                "tags": {
                    # Node Id
                    "ni": sensor_node.id,
                    # Sensor Id
                    "si": idx
                },
                "time": utc_now.isoformat(),
                "fields": {
                    "value": value
                }
            })

        from influxdb import InfluxDBClient
        client = InfluxDBClient(
            host="localhost",
            port=8086,
            database="metrics"
        )
        client.write_points(metrics)

        return {}