2026-05-27 16:38:40 +08:00
|
|
|
|
import asyncio
|
|
|
|
|
|
from contextlib import asynccontextmanager
|
|
|
|
|
|
from fastapi import FastAPI
|
|
|
|
|
|
from fastapi.middleware.cors import CORSMiddleware
|
|
|
|
|
|
from loguru import logger
|
|
|
|
|
|
|
|
|
|
|
|
from app.config import settings
|
|
|
|
|
|
from app.database import init_db
|
|
|
|
|
|
from app.api import router
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@asynccontextmanager
|
|
|
|
|
|
async def lifespan(app: FastAPI):
|
|
|
|
|
|
logger.info("启动推拉酸洗线二级系统...")
|
|
|
|
|
|
await init_db()
|
|
|
|
|
|
await _create_default_admin()
|
2026-06-29 13:57:59 +08:00
|
|
|
|
await _ensure_line_state()
|
2026-05-27 16:38:40 +08:00
|
|
|
|
|
|
|
|
|
|
# 启动L1报文接收服务(UDP)
|
|
|
|
|
|
from app.services.message_parser import l1_server
|
|
|
|
|
|
import app.services.material_service # noqa: 注册报文处理器
|
|
|
|
|
|
await l1_server.start()
|
|
|
|
|
|
|
2026-06-29 13:57:59 +08:00
|
|
|
|
# 启动产线联动引擎(计划上线/上卷鞍座/实绩/停机自动检测)
|
|
|
|
|
|
from app.services.line_service import run_engine_loop
|
|
|
|
|
|
engine_task = asyncio.create_task(run_engine_loop())
|
|
|
|
|
|
|
2026-05-27 16:38:40 +08:00
|
|
|
|
logger.info("系统启动完成")
|
|
|
|
|
|
yield
|
|
|
|
|
|
|
2026-06-29 13:57:59 +08:00
|
|
|
|
engine_task.cancel()
|
2026-05-27 16:38:40 +08:00
|
|
|
|
l1_server.stop()
|
|
|
|
|
|
logger.info("系统已停止")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async def _create_default_admin():
|
|
|
|
|
|
"""首次启动时创建默认管理员"""
|
|
|
|
|
|
from app.database import AsyncSessionLocal
|
|
|
|
|
|
from sqlalchemy import select
|
|
|
|
|
|
from app.models.user import User
|
|
|
|
|
|
from app.services.auth_service import hash_password
|
|
|
|
|
|
|
|
|
|
|
|
async with AsyncSessionLocal() as db:
|
|
|
|
|
|
result = await db.execute(select(User).where(User.username == "admin"))
|
|
|
|
|
|
if not result.scalar_one_or_none():
|
|
|
|
|
|
admin = User(
|
|
|
|
|
|
username="admin",
|
|
|
|
|
|
full_name="系统管理员",
|
|
|
|
|
|
hashed_password=hash_password("admin123"),
|
|
|
|
|
|
role="admin",
|
|
|
|
|
|
)
|
|
|
|
|
|
db.add(admin)
|
|
|
|
|
|
await db.commit()
|
|
|
|
|
|
logger.info("默认管理员已创建: admin / admin123")
|
|
|
|
|
|
|
|
|
|
|
|
|
2026-06-29 13:57:59 +08:00
|
|
|
|
async def _ensure_line_state():
|
|
|
|
|
|
"""确保产线状态单例行存在(避免并发首建竞争)。"""
|
|
|
|
|
|
from app.database import AsyncSessionLocal
|
|
|
|
|
|
from app.models.line_state import LineState
|
|
|
|
|
|
|
|
|
|
|
|
async with AsyncSessionLocal() as db:
|
|
|
|
|
|
if not await db.get(LineState, 1):
|
|
|
|
|
|
db.add(LineState(id=1, speed=0))
|
|
|
|
|
|
await db.commit()
|
|
|
|
|
|
|
|
|
|
|
|
|
2026-05-27 16:38:40 +08:00
|
|
|
|
app = FastAPI(
|
|
|
|
|
|
title="推拉酸洗线二级系统",
|
|
|
|
|
|
description="Pickling Line Level-2 MES System",
|
|
|
|
|
|
version="1.0.0",
|
|
|
|
|
|
lifespan=lifespan,
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
app.add_middleware(
|
|
|
|
|
|
CORSMiddleware,
|
|
|
|
|
|
allow_origins=settings.CORS_ORIGINS,
|
|
|
|
|
|
allow_credentials=True,
|
|
|
|
|
|
allow_methods=["*"],
|
|
|
|
|
|
allow_headers=["*"],
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
app.include_router(router, prefix="/api")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@app.get("/health")
|
|
|
|
|
|
async def health():
|
|
|
|
|
|
return {"status": "ok", "service": "pickling-mes"}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
|
|
import uvicorn
|
|
|
|
|
|
uvicorn.run("app.main:app", host=settings.APP_HOST, port=settings.APP_PORT, reload=settings.DEBUG)
|