Spaces:
Sleeping
Sleeping
File size: 3,898 Bytes
deeb85c 2004aa8 deeb85c 2004aa8 deeb85c ea8c1bf 2004aa8 a4231fd 2004aa8 deeb85c 2004aa8 a4231fd 2004aa8 deeb85c 0b9e56f |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 |
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
@app.get("/", response_class=HTMLResponse)
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})
@app.post("/calculate")
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
@app.get("/edit_cables", response_class=HTMLResponse)
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})
@app.post("/add_cable")
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)
@app.post("/edit_cable/{cable_id}")
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)
@app.post("/delete_cable/{cable_id}")
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)
@app.get("/cg", response_class=HTMLResponse)
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})
@app.get("/get_resistance/{awg}", response_model=dict)
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}
|