Finish database, API, Compare/Detail/Compare Views
This commit is contained in:
BIN
backend/__pycache__/database.cpython-313.pyc
Normal file
BIN
backend/__pycache__/database.cpython-313.pyc
Normal file
Binary file not shown.
BIN
backend/__pycache__/main.cpython-313.pyc
Normal file
BIN
backend/__pycache__/main.cpython-313.pyc
Normal file
Binary file not shown.
140
backend/admin.py
Normal file
140
backend/admin.py
Normal file
@@ -0,0 +1,140 @@
|
||||
from database import init_db, get_all_mcus, get_mcu, add_mcu, update_mcu, delete_mcu
|
||||
|
||||
# 定义所有 MCU 字段(除了 id)
|
||||
MCU_FIELDS = [
|
||||
"name", "core_num", "core_type", "instruction_set", "coremark", "coremark_per_mhz",
|
||||
"frequency", "flash", "rom", "ram", "sram", "sram_in_rtc", "bus_width",
|
||||
"cache_type", "l1_cache_size", "l2_cache_size", "l3_cache_size",
|
||||
"pipeline_depth", "simd_support", "fpu", "package", "gpios", "uarts",
|
||||
"i2cs", "i2ses", "spis", "spi_protocols", "systems", "manufacturer", "link"
|
||||
]
|
||||
|
||||
FIELD_LABELS = {
|
||||
"name": "Name",
|
||||
"core_num": "# of Cores",
|
||||
"core_type": "Core Architecture",
|
||||
"instruction_set": "Instruction Set",
|
||||
"coremark": "CoreMark Score",
|
||||
"coremark_per_mhz": "CoreMark/MHz",
|
||||
"frequency": "Frequency (MHz)",
|
||||
"flash": "Flash (KB)",
|
||||
"rom": "ROM (KB)",
|
||||
"ram": "RAM (KB)",
|
||||
"sram": "SRAM (KB)",
|
||||
"sram_in_rtc": "RTC SRAM (KB)",
|
||||
"bus_width": "Bus Width (bit)",
|
||||
"cache_type": "Cache Type",
|
||||
"l1_cache_size": "L1 Cache (KB)",
|
||||
"l2_cache_size": "L2 Cache (KB)",
|
||||
"l3_cache_size": "L3 Cache (KB)",
|
||||
"pipeline_depth": "Pipeline Depth",
|
||||
"simd_support": "SIMD support (y=1, n=0)",
|
||||
"fpu": "FPU support (Single/Double Precision)",
|
||||
"package": "Package",
|
||||
"gpios": "Number of GPIO ",
|
||||
"uarts": "Number of UARTs",
|
||||
"i2cs": "Number of I2C",
|
||||
"i2ses": "Number of I2S",
|
||||
"spis": "Number of SPI",
|
||||
"spi_protocols": "SPI Protocols",
|
||||
"systems": "Systems",
|
||||
"manufacturer": "Manufacturer",
|
||||
"link": "Link"
|
||||
}
|
||||
|
||||
def list_mcus():
|
||||
mcus = get_all_mcus()
|
||||
if not mcus:
|
||||
print("⚠️ MCU 数据库为空。")
|
||||
else:
|
||||
print("\n=== MCU 列表 ===")
|
||||
for m in mcus:
|
||||
print(f"ID: {m['id']} | 名称: {m['name']} | 核心: {m['core_num']} x {m['core_type']} | "
|
||||
f"频率: {m['frequency']} MHz | 厂商: {m.get('manufacturer','')}")
|
||||
print("=================\n")
|
||||
|
||||
def input_fields(existing=None):
|
||||
"""动态输入所有字段(友好名称提示),existing 用于编辑时提供默认值"""
|
||||
data = {}
|
||||
for field in MCU_FIELDS:
|
||||
old_val = existing.get(field) if existing else ""
|
||||
display_name = FIELD_LABELS.get(field, field) # ✅ 使用中文提示
|
||||
prompt = f"{display_name} ({old_val if old_val is not None else ''}): "
|
||||
val = input(prompt).strip()
|
||||
if val == "" and existing: # 编辑时保留原值
|
||||
continue
|
||||
if val == "": # 新增时空值存 None
|
||||
data[field] = None
|
||||
else:
|
||||
# ✅ 类型转换
|
||||
if field in ["pipeline_depth"]:
|
||||
try:
|
||||
data[field] = int(val)
|
||||
except ValueError:
|
||||
data[field] = None
|
||||
elif field in ["coremark_per_mhz"]:
|
||||
try:
|
||||
data[field] = float(val)
|
||||
except ValueError:
|
||||
data[field] = None
|
||||
elif field == "simd_support":
|
||||
data[field] = val.lower() in ["1", "true", "yes", "y"]
|
||||
else:
|
||||
data[field] = val
|
||||
return data
|
||||
|
||||
def create_mcu():
|
||||
print("\n=== 添加 MCU ===")
|
||||
fields = input_fields()
|
||||
add_mcu(**fields)
|
||||
print("✅ MCU 添加成功!")
|
||||
|
||||
def edit_mcu():
|
||||
mcu_id = int(input("输入要修改的 MCU ID: "))
|
||||
existing = get_mcu(mcu_id)
|
||||
if not existing:
|
||||
print("❌ MCU ID 不存在")
|
||||
return
|
||||
print("\n=== 编辑 MCU(直接回车保留原值) ===")
|
||||
fields = input_fields(existing)
|
||||
if fields:
|
||||
update_mcu(mcu_id, **fields)
|
||||
print("✅ MCU 更新成功!")
|
||||
else:
|
||||
print("⚠️ 未修改任何字段。")
|
||||
|
||||
def remove_mcu():
|
||||
mcu_id = int(input("输入要删除的 MCU ID: "))
|
||||
if not get_mcu(mcu_id):
|
||||
print("❌ MCU ID 不存在")
|
||||
return
|
||||
delete_mcu(mcu_id)
|
||||
print("✅ MCU 删除成功!")
|
||||
|
||||
def menu():
|
||||
while True:
|
||||
print("\n=== MCU 管理菜单 ===")
|
||||
print("1. 查看所有 MCU")
|
||||
print("2. 添加 MCU")
|
||||
print("3. 修改 MCU")
|
||||
print("4. 删除 MCU")
|
||||
print("5. 退出")
|
||||
choice = input("选择操作 (1-5): ")
|
||||
|
||||
if choice == "1":
|
||||
list_mcus()
|
||||
elif choice == "2":
|
||||
create_mcu()
|
||||
elif choice == "3":
|
||||
edit_mcu()
|
||||
elif choice == "4":
|
||||
remove_mcu()
|
||||
elif choice == "5":
|
||||
print("👋 退出 MCU 管理工具")
|
||||
break
|
||||
else:
|
||||
print("❌ 无效选择,请重新输入。")
|
||||
|
||||
if __name__ == "__main__":
|
||||
init_db()
|
||||
menu()
|
||||
98
backend/database.py
Normal file
98
backend/database.py
Normal file
@@ -0,0 +1,98 @@
|
||||
import sqlite3
|
||||
from pathlib import Path
|
||||
|
||||
DB_PATH = Path(__file__).parent / "mcu.db"
|
||||
|
||||
def init_db():
|
||||
conn = sqlite3.connect(DB_PATH)
|
||||
cur = conn.cursor()
|
||||
cur.execute("""
|
||||
CREATE TABLE IF NOT EXISTS mcu (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT, -- ID
|
||||
name TEXT NOT NULL, -- 名称
|
||||
core_num TEXT NOT NULL, -- 核心数量
|
||||
core_type TEXT NOT NULL, -- 核心架构
|
||||
instruction_set TEXT, -- 指令集架构
|
||||
coremark TEXT, -- CoreMark 分数
|
||||
coremark_per_mhz REAL, -- CoreMark/MHz
|
||||
frequency TEXT NOT NULL, -- 主频 MHz
|
||||
flash TEXT, -- Flash KB
|
||||
rom TEXT, -- ROM KB
|
||||
ram TEXT, -- RAM KB
|
||||
sram TEXT, -- SRAM KB
|
||||
sram_in_rtc TEXT, -- RTC SRAM KB
|
||||
bus_width TEXT, -- 总线宽度 bit
|
||||
cache_type TEXT, -- Cache 类型 (L1/L2)
|
||||
l1_cache_size TEXT, -- L1 Cache 大小 KB
|
||||
l2_cache_size TEXT, -- L2 Cache 大小 KB
|
||||
l3_cache_size TEXT, -- L3 Cache 大小 KB
|
||||
pipeline_depth TEXT, -- 流水线深度
|
||||
simd_support BOOLEAN, -- SIMD 支持
|
||||
fpu TEXT, -- FPU 支持 (Single/Double Precision)
|
||||
package TEXT, -- 封装
|
||||
gpios TEXT, -- GPIO 引脚数量
|
||||
uarts TEXT, -- UART 数量
|
||||
i2cs TEXT, -- I2C 数量
|
||||
i2ses TEXT, -- I2S 数量
|
||||
spis TEXT, -- SPI 数量
|
||||
spi_protocols TEXT, -- SPI 协议
|
||||
systems TEXT, -- 系统
|
||||
manufacturer TEXT, -- 厂商
|
||||
link TEXT -- 链接
|
||||
)
|
||||
""")
|
||||
conn.commit()
|
||||
conn.close()
|
||||
|
||||
# ==================== 查询所有 MCU(关键字段) ====================
|
||||
def get_all_mcus():
|
||||
conn = sqlite3.connect(DB_PATH)
|
||||
cur = conn.cursor()
|
||||
cur.execute("SELECT id, name, core_num, core_type, rom, ram, sram, flash, frequency, manufacturer FROM mcu")
|
||||
rows = cur.fetchall()
|
||||
conn.close()
|
||||
return [{"id": i, "name": n, "core_num": cn, "core_type": ct, "rom": ro, "ram": ra, "sram": sra, "flash":fl, "frequency": f, "manufacturer": m}
|
||||
for i, n, cn, ct, ro, ra, sra, fl, f, m in rows]
|
||||
|
||||
# ==================== 查询单个 MCU(完整字段) ====================
|
||||
def get_mcu(mcu_id):
|
||||
conn = sqlite3.connect(DB_PATH)
|
||||
conn.row_factory = sqlite3.Row # 允许返回 dict
|
||||
cur = conn.cursor()
|
||||
cur.execute("SELECT * FROM mcu WHERE id=?", (mcu_id,))
|
||||
row = cur.fetchone()
|
||||
conn.close()
|
||||
return dict(row) if row else None
|
||||
|
||||
# ==================== 插入 MCU(支持所有字段) ====================
|
||||
def add_mcu(**fields):
|
||||
"""动态插入 MCU(支持所有字段)"""
|
||||
keys = ",".join(fields.keys())
|
||||
placeholders = ",".join("?" for _ in fields)
|
||||
values = tuple(fields.values())
|
||||
|
||||
conn = sqlite3.connect(DB_PATH)
|
||||
cur = conn.cursor()
|
||||
cur.execute(f"INSERT INTO mcu ({keys}) VALUES ({placeholders})", values)
|
||||
conn.commit()
|
||||
conn.close()
|
||||
|
||||
# ==================== 更新 MCU(支持所有字段) ====================
|
||||
def update_mcu(mcu_id, **fields):
|
||||
"""动态更新 MCU(支持所有字段)"""
|
||||
set_clause = ",".join(f"{k}=?" for k in fields.keys())
|
||||
values = tuple(fields.values()) + (mcu_id,)
|
||||
|
||||
conn = sqlite3.connect(DB_PATH)
|
||||
cur = conn.cursor()
|
||||
cur.execute(f"UPDATE mcu SET {set_clause} WHERE id=?", values)
|
||||
conn.commit()
|
||||
conn.close()
|
||||
|
||||
# ==================== 删除 MCU ====================
|
||||
def delete_mcu(mcu_id):
|
||||
conn = sqlite3.connect(DB_PATH)
|
||||
cur = conn.cursor()
|
||||
cur.execute("DELETE FROM mcu WHERE id=?", (mcu_id,))
|
||||
conn.commit()
|
||||
conn.close()
|
||||
59
backend/main.py
Normal file
59
backend/main.py
Normal file
@@ -0,0 +1,59 @@
|
||||
from fastapi import FastAPI, HTTPException
|
||||
from fastapi.middleware.cors import CORSMiddleware
|
||||
from pydantic import BaseModel
|
||||
from database import init_db, get_all_mcus, get_mcu, add_mcu, update_mcu, delete_mcu
|
||||
|
||||
# Pydantic 模型(用于请求体验证)
|
||||
class MCU(BaseModel):
|
||||
name: str
|
||||
core: str
|
||||
frequency: int
|
||||
|
||||
# 初始化 FastAPI
|
||||
app = FastAPI(title="MCU Compare API", version="1.0")
|
||||
|
||||
# 允许跨域
|
||||
app.add_middleware(
|
||||
CORSMiddleware,
|
||||
allow_origins=["*"],
|
||||
allow_methods=["*"],
|
||||
allow_headers=["*"],
|
||||
)
|
||||
|
||||
# 初始化数据库
|
||||
init_db()
|
||||
|
||||
# ✅ 1. 获取所有 MCU
|
||||
@app.get("/api/mcus")
|
||||
def read_mcus():
|
||||
return get_all_mcus()
|
||||
|
||||
# ✅ 2. 获取单个 MCU
|
||||
@app.get("/api/mcus/{mcu_id}")
|
||||
def read_mcu(mcu_id: int):
|
||||
mcu = get_mcu(mcu_id)
|
||||
if not mcu:
|
||||
raise HTTPException(status_code=404, detail="MCU not found")
|
||||
return mcu
|
||||
|
||||
# ✅ 3. 创建 MCU
|
||||
@app.post("/api/mcus")
|
||||
def create_mcu(mcu: MCU):
|
||||
add_mcu(mcu.name, mcu.core, mcu.frequency)
|
||||
return {"message": "MCU added successfully"}
|
||||
|
||||
# ✅ 4. 更新 MCU
|
||||
@app.put("/api/mcus/{mcu_id}")
|
||||
def modify_mcu(mcu_id: int, mcu: MCU):
|
||||
if not get_mcu(mcu_id):
|
||||
raise HTTPException(status_code=404, detail="MCU not found")
|
||||
update_mcu(mcu_id, mcu.name, mcu.core, mcu.frequency)
|
||||
return {"message": "MCU updated successfully"}
|
||||
|
||||
# ✅ 5. 删除 MCU
|
||||
@app.delete("/api/mcus/{mcu_id}")
|
||||
def remove_mcu(mcu_id: int):
|
||||
if not get_mcu(mcu_id):
|
||||
raise HTTPException(status_code=404, detail="MCU not found")
|
||||
delete_mcu(mcu_id)
|
||||
return {"message": "MCU deleted successfully"}
|
||||
Reference in New Issue
Block a user