Files
lanhongguangandClaude Sonnet 4.6 f60135a67e feat: 新增 SIM 卡管理页面及设备详情面板
- 新增 SimCards 页面(/sim-cards):以表格形式展示所有 SIM 卡的
  网络状态、信号强度、网络制式、注册状态、运营商、上/下行流量、
  在线时长、短信发送/接收/今日统计,顶部汇总卡片
- 新增 SimDetail 页面(/modems/:id):单卡详情,含别名编辑、手动刷新
- 后端新增 access_technologies、registration_state、tx_bytes、rx_bytes、
  connection_duration 字段,通过 mmcli Bearer 接口采集流量数据
- 新增 GET /api/modems/{id}/detail 接口,返回设备详情及短信统计
- 修复 WebSocket 断开时客户端列表重复移除的问题
- 修复 modem unique key 从 device_path 改为 mm_object_path
- 修复入站短信去重逻辑,改用 mm_sms_index
- 修复 mmcli send_sms 内容含逗号时被截断的问题
- 修复 sms_scheduler 调度后立即写入 next_run_at
- 新增 migrate.py 数据库迁移脚本
- 新增 docs/sim-cards-mockup.html 页面设计图
- 更新 README 补充新页面、接口及迁移说明

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-06-24 14:56:28 +08:00

91 lines
3.0 KiB
Python

"""
One-time migration script for schema changes:
- modems: unique key moves from device_path to mm_object_path
- sms_messages: add mm_sms_index column for inbound dedup
Run once: python migrate.py
"""
import sqlite3
import os
DB_PATH = os.environ.get("DB_PATH", "./sim_manager.db")
if not os.path.exists(DB_PATH):
print(f"Database {DB_PATH} not found — nothing to migrate.")
exit(0)
conn = sqlite3.connect(DB_PATH)
cur = conn.cursor()
# Check if mm_sms_index already exists
cur.execute("PRAGMA table_info(sms_messages)")
cols = {row[1] for row in cur.fetchall()}
if "mm_sms_index" not in cols:
cur.execute("ALTER TABLE sms_messages ADD COLUMN mm_sms_index TEXT")
print("Added sms_messages.mm_sms_index")
else:
print("sms_messages.mm_sms_index already exists, skipping")
# SQLite doesn't support DROP CONSTRAINT; rebuild modems table to fix unique key
cur.execute("PRAGMA table_info(modems)")
modem_cols = {row[1] for row in cur.fetchall()}
cur.execute("SELECT sql FROM sqlite_master WHERE type='table' AND name='modems'")
row = cur.fetchone()
ddl = row[0] if row else ""
# Only rebuild if device_path is still marked UNIQUE and mm_object_path is not
if "device_path" in ddl and 'mm_object_path' not in ddl.replace("mm_object_path", ""):
print("Rebuilding modems table to move unique constraint...")
cur.executescript("""
BEGIN;
ALTER TABLE modems RENAME TO _modems_old;
CREATE TABLE modems (
id INTEGER PRIMARY KEY,
device_path VARCHAR(100),
mm_object_path VARCHAR(200) UNIQUE NOT NULL,
imei VARCHAR(20) UNIQUE,
manufacturer VARCHAR(100),
model VARCHAR(100),
phone_number VARCHAR(30),
operator VARCHAR(100),
signal_quality INTEGER DEFAULT 0,
status VARCHAR(20) DEFAULT 'unknown',
alias VARCHAR(100),
is_active BOOLEAN DEFAULT 1,
last_seen DATETIME,
created_at DATETIME
);
INSERT INTO modems SELECT
id, device_path, mm_object_path, imei, manufacturer, model,
phone_number, operator, signal_quality, status, alias,
is_active, last_seen, created_at
FROM _modems_old;
DROP TABLE _modems_old;
COMMIT;
""")
print("modems table rebuilt")
else:
print("modems table already correct, skipping")
# Add new extended stats columns to modems if missing
cur.execute("PRAGMA table_info(modems)")
modem_cols2 = {row[1] for row in cur.fetchall()}
new_modem_cols = {
"access_technologies": "TEXT",
"registration_state": "TEXT",
"tx_bytes": "INTEGER DEFAULT 0",
"rx_bytes": "INTEGER DEFAULT 0",
"connection_duration": "INTEGER DEFAULT 0",
}
for col, col_type in new_modem_cols.items():
if col not in modem_cols2:
cur.execute(f"ALTER TABLE modems ADD COLUMN {col} {col_type}")
print(f"Added modems.{col}")
else:
print(f"modems.{col} already exists, skipping")
conn.commit()
conn.close()
print("Migration complete.")