Files
mcu-benchmark/backend/admin.py

141 lines
4.5 KiB
Python
Raw Permalink Normal View History

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()