forked from linjmeyer/tilt-pitch
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c56c436
commit 56400ad
Showing
27 changed files
with
321 additions
and
67 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
FROM python:3.11-slim |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
default: help | ||
@>&2 echo "Choose an action." | ||
|
||
help: | ||
@echo "Please use 'make <target>' where <target> is one of:" | ||
@echo "" | ||
@echo " clean remove temporary files" | ||
@echo " install tinstalls dependencies and pre-commit hooks" | ||
@echo "" | ||
|
||
clean: | ||
@find . | grep -E "(__pycache__|\.pyc$$|\.pyo$$|dist|.egg-info$$|\build/*$$ )" | xargs rm -rf | ||
|
||
install: | ||
@python3.11 -m venv venv | ||
@venv/bin/python3 -m pip install -U pre-commit | ||
@venv/bin/python3 -m pip install -e .[dev] | ||
@venv/bin/pre-commit install | ||
|
||
dist: clean | ||
@python3 -m build |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
from functools import lru_cache | ||
|
||
from pydantic_settings import BaseSettings, SettingsConfigDict | ||
|
||
|
||
class Settings(BaseSettings): | ||
app_name: str = "Tilt Pitch API" | ||
sqlalchemy_database_url: str | ||
model_config = SettingsConfigDict(env_file=".env") | ||
|
||
|
||
@lru_cache() | ||
def get_settings(): | ||
return Settings() | ||
|
||
|
||
settings = Settings() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
from sqlalchemy.orm import Session | ||
|
||
from . import database, schemas | ||
|
||
|
||
def get_sensor(db: Session, sensor_id: int): | ||
return db.query(database.Sensor).filter(database.Sensor.id == sensor_id).first() | ||
|
||
|
||
def get_sensor_by_colour(db: Session, colour: str): | ||
return db.query(database.Sensor).filter(database.Sensor.colour == colour).first() | ||
|
||
|
||
def get_sensors(db: Session, skip: int = 0, limit: int = 100): | ||
return db.query(database.Sensor).offset(skip).limit(limit).all() | ||
|
||
|
||
def create_sensor(db: Session, sensor: schemas.SensorCreate): | ||
db_sensor = database.Sensor(colour=sensor.colour, is_active=sensor.is_active) | ||
db.add(db_sensor) | ||
db.commit() | ||
db.refresh(db_sensor) | ||
return db_sensor | ||
|
||
|
||
|
||
def get_brews(db: Session, skip: int = 0, limit: int = 100): | ||
return db.query(database.Brew).offset(skip).limit(limit).all() | ||
|
||
|
||
def create_brew(db: Session, brew: schemas.BrewCreate, sensor_id: int): | ||
db_brew = database.Brew(**brew.model_dump(), sensor_id=sensor_id) | ||
db.add(db_brew) | ||
db.commit() | ||
db.refresh(db_brew) | ||
return db_brew |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
from sqlalchemy import (Boolean, Column, Engine, Float, ForeignKey, Integer, | ||
String, create_engine) | ||
from sqlalchemy.ext.declarative import declarative_base | ||
from sqlalchemy.orm import relationship, sessionmaker | ||
|
||
from .config import get_settings | ||
|
||
settings = get_settings() | ||
SQLALCHEMY_DATABASE_URL = settings.sqlalchemy_database_url | ||
engine: Engine | ||
|
||
if SQLALCHEMY_DATABASE_URL.startswith("sqlite"): | ||
engine = create_engine(SQLALCHEMY_DATABASE_URL, connect_args={"check_same_thread": False}) | ||
else: | ||
engine = create_engine(SQLALCHEMY_DATABASE_URL) | ||
|
||
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine) | ||
|
||
Base = declarative_base() | ||
|
||
|
||
class Sensor(Base): | ||
__tablename__ = "sensors" | ||
id = Column(Integer, primary_key=True, index=True) | ||
colour = Column(String) | ||
is_active = Column(Boolean) | ||
brews = relationship("Brew", back_populates="sensor") | ||
|
||
|
||
class Brew(Base): | ||
__tablename__ = "brews" | ||
id = Column(Integer, primary_key=True, index=True) | ||
name = Column(String, nullable=False) | ||
starting_gravity = Column(Float, nullable=False) | ||
sensor = relationship("Sensor", back_populates="brews") | ||
sensor_id = Column(Integer, ForeignKey("sensors.id")) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
import threading | ||
from typing import List | ||
|
||
from fastapi import Depends, FastAPI, HTTPException | ||
from sqlalchemy.orm import Session | ||
|
||
from api.sensors import sensor_router | ||
|
||
from . import config, crud, database, schemas | ||
|
||
fast_api_app = FastAPI() | ||
# fast_api_app.include_router(sensor_router) | ||
|
||
database.Base.metadata.create_all(bind=database.engine) | ||
|
||
|
||
def get_db(): | ||
db = database.SessionLocal() | ||
try: | ||
yield db | ||
finally: | ||
db.close() | ||
|
||
|
||
@fast_api_app.get("/") | ||
def get_processes() -> list[str]: | ||
threads: List[threading.Thread] = threading.enumerate() | ||
thread_names: List[str] = [] | ||
for thread in threads: | ||
thread_names.append(thread.name) | ||
return thread_names | ||
|
||
|
||
@fast_api_app.post("/sensors/", response_model=schemas.Sensor) | ||
def create_sensor(sensor: schemas.SensorCreate, db: Session = Depends(get_db)): | ||
db_sensor = crud.get_sensor_by_colour(db, colour=sensor.colour) | ||
if db_sensor: | ||
raise HTTPException(status_code=400, detail="Colour already registered") | ||
return crud.create_sensor(db=db, sensor=sensor) | ||
|
||
|
||
@fast_api_app.get("/sensors/", response_model=list[schemas.Sensor]) | ||
def read_sensors(skip: int = 0, limit: int = 100, db: Session = Depends(get_db)): | ||
sensors = crud.get_sensors(db, skip=skip, limit=limit) | ||
return sensors | ||
|
||
|
||
@fast_api_app.get("/sensors/{sensor_id}", response_model=schemas.Sensor) | ||
def read_sensor(sensor_id: int, db: Session = Depends(get_db)): | ||
db_sensor = crud.get_sensor(db, sensor_id=sensor_id) | ||
if db_sensor is None: | ||
raise HTTPException(status_code=404, detail="Sensor not found") | ||
return db_sensor | ||
|
||
|
||
@fast_api_app.post("/sensors/{sensor_id}/brews/", response_model=schemas.Brew) | ||
def create_brew_for_sensor( | ||
sensor_id: int, brew: schemas.BrewCreate, db: Session = Depends(get_db) | ||
): | ||
return crud.create_brew(db=db, brew=brew, sensor_id=sensor_id) | ||
|
||
|
||
@fast_api_app.get("/brews/", response_model=list[schemas.Brew]) | ||
def read_brews(skip: int = 0, limit: int = 100, db: Session = Depends(get_db)): | ||
brews = crud.get_brews(db, skip=skip, limit=limit) | ||
return brews |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
from pydantic import BaseModel | ||
|
||
|
||
class BrewBase(BaseModel): | ||
name: str | ||
starting_gravity: float | ||
sensor_id: int | ||
|
||
|
||
class BrewCreate(BrewBase): | ||
pass | ||
|
||
|
||
class Brew(BrewBase): | ||
id: int | ||
|
||
class Config: | ||
from_attributes = True | ||
|
||
|
||
class SensorBase(BaseModel): | ||
is_active: bool | ||
colour: str | ||
|
||
|
||
class SensorCreate(SensorBase): | ||
pass | ||
|
||
|
||
class Sensor(SensorBase): | ||
|
||
class Config: | ||
from_attributes = True |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
from typing import List | ||
|
||
from fastapi import APIRouter | ||
|
||
sensor_router = APIRouter( | ||
prefix="/sensors", | ||
tags=["sensors"], | ||
responses={404: {"description": "Not found"}}, | ||
) | ||
|
||
|
||
@sensor_router.get("/") | ||
async def get_sensors() -> List[str]: | ||
return ["yellow", "purple"] | ||
|
||
|
||
@sensor_router.get("/{sensor_id}") | ||
async def get_sensor(sensor_id: str) -> dict: | ||
return {"sensor_id": sensor_id} | ||
|
||
|
||
@sensor_router.post("/") | ||
async def add_sensor(): | ||
return {} | ||
|
||
|
||
@sensor_router.put("/{sensor_id}") | ||
async def update_sensor(): | ||
return {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.