async def delete_sensor(sensorId: int, db: Session = Depends(get_database)):
    sensor = Crud.get_sensor(db, sensorId=sensorId)
    if sensor is None:
        raise HTTPException(status_code=404, detail='Sensor not found')

    Crud.delete_sensor(db, sensor)
    return Status(message=f'Deleted sensor {sensor.id}')
async def get_sensor_measurements(sensorId: int,
                                  startDateTime: str = START_DATE_TIME,
                                  endDateTime: str = END_DATE_TIME,
                                  db: Session = Depends(get_database)):
    sensor = Crud.get_sensor(db, sensorId=sensorId)
    if sensor is None:
        raise HTTPException(status_code=404, detail='Sensor not found')
    return Crud.get_measurements_for_sensor(db, startDateTime, endDateTime,
                                            sensorId)
示例#3
0
async def create_measurement(measurement: Schemas.MeasurementCreate,
                             db: Session = Depends(get_database)):
    existingSensor = Crud.get_sensor(db, measurement.sensor_id)
    if not existingSensor:
        raise HTTPException(
            status_code=404,
            detail=f'No sensor with id "{measurement.sensor_id}" existing')

    return Crud.create_measurement(db=db, measurement=measurement)
async def update_sensor(sensorId: int,
                        sensor: Schemas.SensorUpdate,
                        db: Session = Depends(get_database)):
    sensorToUpdate = Crud.get_sensor(db, sensorId)
    if not sensorToUpdate:
        raise HTTPException(status_code=404, detail='Sensor not found')

    if sensorToUpdate.name != sensor.name:
        existingSensor = Crud.get_sensor_by_name_and_device_id(
            db, sensor.name, sensorToUpdate.device_id)
        if existingSensor:
            raise HTTPException(
                status_code=400,
                detail=f'A sensor called "{sensor.name}" already exists '
                f'(ID: {existingSensor.id}) for device {sensorToUpdate.device_id}'
            )

    return Crud.update_sensor(db=db, sensorId=sensorId, sensor=sensor)
async def read_sensor(sensorId: int, db: Session = Depends(get_database)):
    sensor = Crud.get_sensor(db, sensorId=sensorId)
    if sensor is None:
        raise HTTPException(status_code=404, detail='Sensor not found')
    return sensor
async def get_latest_measurements_for_sensor(
    sensorId: int, db: Session = Depends(get_database)):
    sensor = Crud.get_sensor(db, sensorId=sensorId)
    if sensor is None:
        raise HTTPException(status_code=404, detail='Sensor not found')
    return Crud.get_latest_measurement_for_sensor(db, sensorId)