Spaces:
Sleeping
Sleeping
from fastapi import FastAPI, Request, Depends, HTTPException, Form | |
from fastapi.responses import HTMLResponse, RedirectResponse | |
from fastapi.staticfiles import StaticFiles | |
from fastapi.templating import Jinja2Templates | |
from sqlalchemy.orm import Session | |
from pydantic import BaseModel | |
from typing import List | |
from models import Base, Cable, SessionLocal, init_db | |
app = FastAPI() | |
# Initialize the database | |
init_db() | |
# Serve static files | |
app.mount("/static", StaticFiles(directory="static"), name="static") | |
templates = Jinja2Templates(directory="templates") | |
class VoltageDropRequest(BaseModel): | |
voltage: float | |
load: float | |
length: float | |
cable_type: str | |
class MultipleCablesRequest(BaseModel): | |
cables: List[VoltageDropRequest] | |
def get_db(): | |
db = SessionLocal() | |
try: | |
yield db | |
finally: | |
db.close() | |
def calculate_voltage_drop(voltage, load, length, resistance): | |
resistance_in_ohms = resistance / 1000 # Convert mΩ/m to Ω/m | |
voltage_drop = load * 2*length * resistance_in_ohms | |
final_voltage = voltage - voltage_drop | |
if final_voltage < 0: | |
final_voltage = 0 | |
return final_voltage | |
async def read_root(request: Request, db: Session = Depends(get_db)): | |
cables = db.query(Cable).all() | |
return templates.TemplateResponse("index.html", {"request": request, "cables": cables}) | |
async def calculate(request: MultipleCablesRequest, db: Session = Depends(get_db)): | |
results = [] | |
for cable_request in request.cables: | |
cable = db.query(Cable).filter(Cable.type == cable_request.cable_type).first() | |
if not cable: | |
raise HTTPException(status_code=400, detail="Invalid cable type") | |
final_voltage = calculate_voltage_drop(cable_request.voltage, cable_request.load, cable_request.length, cable.resistance) | |
results.append({"voltage_drop": cable_request.voltage - final_voltage}) | |
return results | |
async def edit_cables(request: Request, db: Session = Depends(get_db)): | |
cables = db.query(Cable).all() | |
return templates.TemplateResponse("edit_cables.html", {"request": request, "cables": cables}) | |
async def add_cable(type: str = Form(...), resistance: float = Form(...), db: Session = Depends(get_db)): | |
cable = Cable(type=type, resistance=resistance) | |
db.add(cable) | |
db.commit() | |
return RedirectResponse(url="/edit_cables", status_code=303) | |
async def edit_cable(cable_id: int, type: str = Form(...), resistance: float = Form(...), db: Session = Depends(get_db)): | |
cable = db.query(Cable).filter(Cable.id == cable_id).first() | |
if not cable: | |
raise HTTPException(status_code=404, detail="Cable not found") | |
cable.type = type | |
cable.resistance = resistance | |
db.commit() | |
return RedirectResponse(url="/edit_cables", status_code=303) | |
async def delete_cable(cable_id: int, db: Session = Depends(get_db)): | |
cable = db.query(Cable).filter(Cable.id == cable_id).first() | |
if not cable: | |
raise HTTPException(status_code=404, detail="Cable not found") | |
db.delete(cable) | |
db.commit() | |
return RedirectResponse(url="/edit_cables", status_code=303) | |
async def cg_page(request: Request, db: Session = Depends(get_db)): | |
cables = db.query(Cable).all() | |
return templates.TemplateResponse("cg.html", {"request": request, "cables": cables}) | |
async def get_resistance(awg: str, db: Session = Depends(get_db)): | |
cable = db.query(Cable).filter(Cable.type == awg).first() | |
if not cable: | |
raise HTTPException(status_code=404, detail="Cable type not found") | |
return {"resistance": cable.resistance} | |