Exemple #1
0
async def create_multiple_measurements(
    measurementsToAdd: Schemas.MultipleMeasurements,
    db: Session = Depends(get_database)):
    existingDevice = Crud.get_device_by_name(db, measurementsToAdd.deviceName)
    if not existingDevice:
        existingDevice = Crud.create_device(
            db, Schemas.DeviceCreate(name=measurementsToAdd.deviceName))

    for sensor in measurementsToAdd.sensors:
        existingSensor = Crud.get_sensor_by_name_and_device_id(
            db, sensor.name, existingDevice.id)
        if not existingSensor:
            existingSensor = Crud.create_sensor(
                db,
                Schemas.SensorCreate(name=sensor.name,
                                     type=sensor.type,
                                     device_id=existingDevice.id))
        Crud.create_measurement(
            db,
            Schemas.MeasurementCreate(value=sensor.value,
                                      sensor_id=existingSensor.id))

    return Status(message=f'Success')
Exemple #2
0
async def create_device(device: Schemas.DeviceCreate, db: Session = Depends(get_database)):
    existingDevice = Crud.get_device_by_name(db, device.name)
    if existingDevice:
        raise HTTPException(status_code=400, detail='Device with this name already exists')
    return Crud.create_device(db=db, device=device)