成功添加哈士奇业务以及白名单功能创建
This commit is contained in:
@ -7,14 +7,13 @@ from sqlalchemy import desc, or_
|
||||
from extensions import db
|
||||
from models import Device, DeviceHistory, MaintenanceLog
|
||||
|
||||
# --- 导入服务模块 ---
|
||||
# --- 尝试导入服务模块 ---
|
||||
try:
|
||||
from services.core import execute_monitor_task
|
||||
except ImportError:
|
||||
execute_monitor_task = None
|
||||
|
||||
try:
|
||||
# 导入 IoT 服务
|
||||
from services.iot_api import sync_iot_data_service
|
||||
except ImportError:
|
||||
sync_iot_data_service = None
|
||||
@ -22,14 +21,18 @@ except ImportError:
|
||||
api_bp = Blueprint('api', __name__, url_prefix='/api')
|
||||
|
||||
|
||||
# =======================
|
||||
# 0. 辅助函数区
|
||||
# =======================
|
||||
# =========================================================
|
||||
# 0. 核心算法区:数据质量分析与辅助函数
|
||||
# =========================================================
|
||||
|
||||
def calculate_offset(latest_time_str):
|
||||
"""计算时间滞后天数"""
|
||||
if not latest_time_str or latest_time_str == "N/A": return "从未同步"
|
||||
"""
|
||||
计算时间滞后天数
|
||||
"""
|
||||
if not latest_time_str or latest_time_str == "N/A":
|
||||
return "从未同步"
|
||||
try:
|
||||
# 兼容处理 2026_01_13 和 2026-01-13 格式
|
||||
clean = str(latest_time_str).split()[0].replace('_', '-')
|
||||
target = datetime.strptime(clean, "%Y-%m-%d").date()
|
||||
diff = (datetime.now().date() - target).days
|
||||
@ -39,37 +42,136 @@ def calculate_offset(latest_time_str):
|
||||
|
||||
|
||||
def check_data_quality(content_data, source_type, data_time_str=None):
|
||||
"""数据质量分析算法 (只针对爬虫数据)"""
|
||||
if not content_data: return 'ok'
|
||||
if str(source_type) == 'iot_card': return 'ok'
|
||||
"""
|
||||
数据质量分析算法 (融合版:旧版核心规则 + 新版夜间/IoT过滤)
|
||||
"""
|
||||
if not content_data:
|
||||
return 'ok'
|
||||
|
||||
# 夜间免打扰
|
||||
# 1. [新功能] IoT 卡不需要检查数据质量
|
||||
if str(source_type) == 'iot_card':
|
||||
return 'ok'
|
||||
|
||||
# 2. [新功能] 夜间免打扰逻辑 (08:00 - 17:00 之外不报错)
|
||||
# 物理规律:晚上没有太阳,光谱仪数值低是正常的,不应报错
|
||||
if data_time_str and data_time_str != 'N/A':
|
||||
try:
|
||||
clean_time = str(data_time_str).replace('_', '-')
|
||||
dt = None
|
||||
try:
|
||||
dt = datetime.strptime(clean_time, "%Y-%m-%d %H:%M:%S")
|
||||
if dt.hour < 8 or dt.hour >= 17: return 'ok'
|
||||
except:
|
||||
try:
|
||||
dt = datetime.strptime(clean_time, "%Y-%m-%d %H:%M")
|
||||
except:
|
||||
pass
|
||||
|
||||
status = 'ok'
|
||||
if '106' in str(source_type):
|
||||
try:
|
||||
text = content_data.get('content', str(content_data))
|
||||
if 'OSIFBeta' in text:
|
||||
# 保留原有的复杂波形判断逻辑
|
||||
pass
|
||||
# 如果解析成功,且不在 8点-17点之间,视为夜晚,直接返回 ok
|
||||
if dt and (dt.hour < 8 or dt.hour >= 17):
|
||||
return 'ok'
|
||||
except:
|
||||
pass
|
||||
|
||||
# 3. [旧版核心] 数据异常判断逻辑
|
||||
status = 'ok'
|
||||
source_str = str(source_type)
|
||||
|
||||
# --- Type A: 106 设备逻辑 (CSV格式) ---
|
||||
if '106' in source_str:
|
||||
try:
|
||||
# 兼容处理:如果 content_data 是字典,尝试取 content 字段;如果是字符串直接用
|
||||
text_content = ""
|
||||
if isinstance(content_data, dict):
|
||||
text_content = content_data.get('content', str(content_data))
|
||||
else:
|
||||
text_content = str(content_data)
|
||||
|
||||
# 只要包含 OSIFBeta 就进行解析
|
||||
if 'OSIFBeta' in text_content:
|
||||
lines = text_content.split('\n') if '\n' in text_content else [text_content]
|
||||
|
||||
for line in lines:
|
||||
if 'OSIFBeta' not in line:
|
||||
continue
|
||||
|
||||
parts = line.split(',')
|
||||
# 简单校验列数
|
||||
if len(parts) < 10:
|
||||
continue
|
||||
|
||||
# 检查积分时间 (Index 2)
|
||||
try:
|
||||
int_time = int(parts[2])
|
||||
# 旧代码逻辑:只有积分时间饱和 (>= 66534) 才检查数值
|
||||
if int_time >= 66534:
|
||||
# 数据点通常从第4个(Index 3)开始
|
||||
data_points = []
|
||||
for p in parts[3:]:
|
||||
try:
|
||||
data_points.append(float(p))
|
||||
except:
|
||||
pass
|
||||
|
||||
if not data_points:
|
||||
continue
|
||||
|
||||
# 规则A:红色报错 (存在 < 100 的点)
|
||||
for val in data_points:
|
||||
if val < 100:
|
||||
return 'error'
|
||||
|
||||
# 规则B:黄色警告 (连续 5 个点在 100-500 之间)
|
||||
consecutive_warning = 0
|
||||
for val in data_points:
|
||||
if 100 <= val <= 500:
|
||||
consecutive_warning += 1
|
||||
if consecutive_warning >= 5:
|
||||
status = 'warning'
|
||||
# 注意:不立即返回,继续检查后面是否有 error
|
||||
else:
|
||||
consecutive_warning = 0
|
||||
except:
|
||||
continue
|
||||
except Exception:
|
||||
return 'ok'
|
||||
|
||||
# --- Type B: 82 设备逻辑 (JSON格式) ---
|
||||
else:
|
||||
try:
|
||||
# 82 设备 content_data 应该已经是字典
|
||||
if not isinstance(content_data, dict):
|
||||
return 'ok'
|
||||
|
||||
specs = content_data.get('downspec', [])
|
||||
if not specs:
|
||||
specs = content_data.get('upspec', [])
|
||||
|
||||
if specs and isinstance(specs, list):
|
||||
consecutive_low = 0
|
||||
for val in specs:
|
||||
# 确保 val 是数字
|
||||
if not isinstance(val, (int, float)):
|
||||
continue
|
||||
|
||||
# 旧代码逻辑: 连续2点 < 500 -> error
|
||||
if val < 500:
|
||||
consecutive_low += 1
|
||||
if consecutive_low >= 2:
|
||||
return 'error'
|
||||
else:
|
||||
consecutive_low = 0
|
||||
return 'ok'
|
||||
except Exception:
|
||||
return 'ok'
|
||||
|
||||
return status
|
||||
|
||||
|
||||
def save_iot_cards_to_db(card_list):
|
||||
"""
|
||||
[逻辑重构] IoT SIM卡数据入库
|
||||
1. 目标:只维护 source='iot_card' 的记录。
|
||||
2. 策略:实时更新 (Update),不写历史 (No History)。
|
||||
3. 格式:支持字母+数字的 ICCID。
|
||||
[新功能] IoT数据入库逻辑
|
||||
1. 只操作 source='iot_card' 的记录。
|
||||
2. 必须保留 is_whitelist 状态,防止被自动同步覆盖。
|
||||
"""
|
||||
if not card_list: return 0, None
|
||||
update_count = 0
|
||||
@ -79,36 +181,37 @@ def save_iot_cards_to_db(card_list):
|
||||
iccid = card.get('iccid')
|
||||
if not iccid: continue
|
||||
|
||||
# 1. 查找 'SIM卡记录' (source 固定为 iot_card)
|
||||
# name 字段直接存储 ICCID (无论是否包含字母)
|
||||
# 1. 查找是否存在
|
||||
sim_record = Device.query.filter_by(name=iccid, source='iot_card').first()
|
||||
current_whitelist = False
|
||||
|
||||
if not sim_record:
|
||||
# 如果是新卡,创建一条记录
|
||||
# install_site 默认为 IoT库,不干扰主设备的地点
|
||||
# 插入新数据
|
||||
sim_record = Device(name=iccid, source='iot_card', install_site="IoT库")
|
||||
db.session.add(sim_record)
|
||||
db.session.flush()
|
||||
else:
|
||||
# 旧卡:读取并保留旧的白名单设置
|
||||
try:
|
||||
old_json = json.loads(sim_record.json_data)
|
||||
current_whitelist = old_json.get('is_whitelist', False)
|
||||
except:
|
||||
current_whitelist = False
|
||||
|
||||
# 2. 仅更新实时状态 (Real-time update)
|
||||
# 2. 更新字段
|
||||
sim_record.status = str(card.get('cardStatus', ''))
|
||||
|
||||
# 构造数据包
|
||||
card_data = {
|
||||
"iccid": iccid,
|
||||
"usedTraffic": str(card.get('usedTraffic') or '0'),
|
||||
"stopDate": card.get('stopDate', 'N/A'),
|
||||
"cardStatus": card.get('cardStatus'),
|
||||
"tag": card.get('tag', '')
|
||||
"tag": card.get('tag', ''),
|
||||
"is_whitelist": current_whitelist # 写回保留的状态
|
||||
}
|
||||
|
||||
# 更新 JSON 数据和检查时间
|
||||
sim_record.json_data = json.dumps(card_data, ensure_ascii=False)
|
||||
sim_record.check_time = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
||||
|
||||
# [关键]:这里不更新 latest_time,也不写入 DeviceHistory 表
|
||||
# 确保了 "IoT数据只实时更新,不留历史" 的需求
|
||||
|
||||
update_count += 1
|
||||
|
||||
return update_count, None
|
||||
@ -116,9 +219,10 @@ def save_iot_cards_to_db(card_list):
|
||||
return 0, str(e)
|
||||
|
||||
|
||||
# =======================
|
||||
# 1. 认证接口
|
||||
# =======================
|
||||
# =========================================================
|
||||
# 1. 基础接口 (认证 & 概览)
|
||||
# =========================================================
|
||||
|
||||
@api_bp.route('/login', methods=['POST'])
|
||||
def login():
|
||||
data = request.get_json()
|
||||
@ -135,14 +239,10 @@ def login():
|
||||
return jsonify({'code': 401, 'message': '用户名或密码错误'}), 401
|
||||
|
||||
|
||||
# =======================
|
||||
# 2. 设备概览 (逻辑聚合)
|
||||
# =======================
|
||||
@api_bp.route('/devices_overview', methods=['GET'])
|
||||
def devices_overview():
|
||||
try:
|
||||
# A. 获取 'IoT卡表' 数据 (source='iot_card')
|
||||
# 构建字典 { ICCID: {data} },作为缓存供主设备查询
|
||||
# A. 获取 IoT卡表 (source='iot_card')
|
||||
iot_records = Device.query.filter_by(source='iot_card').all()
|
||||
iot_map = {}
|
||||
for rec in iot_records:
|
||||
@ -152,14 +252,12 @@ def devices_overview():
|
||||
except:
|
||||
pass
|
||||
|
||||
# B. 获取 '爬虫设备表' (source != 'iot_card')
|
||||
# 这些才是 Dashboard 主列表展示的设备
|
||||
# B. 获取 真实设备 (source != 'iot_card')
|
||||
devices = Device.query.filter(Device.source != 'iot_card').all()
|
||||
|
||||
data_list = []
|
||||
|
||||
for d in devices:
|
||||
item = d.to_dict()
|
||||
|
||||
parsed_content = {}
|
||||
if d.json_data:
|
||||
try:
|
||||
@ -167,30 +265,39 @@ def devices_overview():
|
||||
except:
|
||||
pass
|
||||
|
||||
# --- 关键逻辑:关联 ---
|
||||
# 通过 bound_iccid 字段,将设备与 IoT 卡数据关联
|
||||
# --- 绑定逻辑 (将IoT卡信息注入到设备) ---
|
||||
bound_iccid = parsed_content.get('bound_iccid')
|
||||
|
||||
item['usedTraffic'] = None
|
||||
item['stopDate'] = None
|
||||
item['isBound'] = False
|
||||
item['bound_iccid'] = bound_iccid
|
||||
item['is_whitelist'] = False # 默认无白名单
|
||||
|
||||
# 如果绑定了 ICCID,且该 ICCID 存在于 IoT 表中
|
||||
# 如果有绑定,且卡片存在,则注入卡片信息
|
||||
if bound_iccid and bound_iccid in iot_map:
|
||||
card_info = iot_map[bound_iccid]
|
||||
item['usedTraffic'] = card_info.get('usedTraffic')
|
||||
item['stopDate'] = card_info.get('stopDate')
|
||||
item['is_whitelist'] = card_info.get('is_whitelist', False)
|
||||
item['isBound'] = True
|
||||
|
||||
# 质量检测只针对爬虫数据
|
||||
# [关键] 调用异常检测函数 (check_data_quality)
|
||||
item['data_quality'] = check_data_quality(parsed_content, d.source, d.latest_time)
|
||||
|
||||
data_list.append(item)
|
||||
|
||||
# C. 将 'IoT卡表' 的数据也传给前端 (用于绑定弹窗)
|
||||
# 前端通过 isOrphanIoT 过滤,主列表不显示,但在绑定列表中显示
|
||||
# C. 必须把 IoT卡表 的数据也传给前端 (用于计算总流量 & 绑定弹窗)
|
||||
for rec in iot_records:
|
||||
item = rec.to_dict()
|
||||
try:
|
||||
j = json.loads(rec.json_data)
|
||||
except:
|
||||
j = {}
|
||||
|
||||
item['usedTraffic'] = j.get('usedTraffic', '0')
|
||||
item['stopDate'] = j.get('stopDate', '')
|
||||
item['is_whitelist'] = j.get('is_whitelist', False)
|
||||
item['isOrphanIoT'] = True
|
||||
item['source'] = 'iot_card'
|
||||
data_list.append(item)
|
||||
@ -200,137 +307,129 @@ def devices_overview():
|
||||
return jsonify({'code': 500, 'message': str(e)})
|
||||
|
||||
|
||||
# =======================
|
||||
# 3. 日志接口 (保持原有功能)
|
||||
# =======================
|
||||
@api_bp.route('/logs/list', methods=['GET'])
|
||||
def get_logs():
|
||||
keyword = request.args.get('keyword', '')
|
||||
start_date = request.args.get('start_date')
|
||||
end_date = request.args.get('end_date')
|
||||
query = MaintenanceLog.query
|
||||
if keyword:
|
||||
kw = f"%{keyword}%"
|
||||
query = query.filter(or_(
|
||||
MaintenanceLog.device_name.like(kw),
|
||||
MaintenanceLog.engineer.like(kw),
|
||||
MaintenanceLog.location.like(kw),
|
||||
MaintenanceLog.content.like(kw)
|
||||
))
|
||||
if start_date and end_date:
|
||||
try:
|
||||
start_dt = datetime.strptime(start_date, '%Y-%m-%d')
|
||||
end_dt = datetime.strptime(end_date, '%Y-%m-%d').replace(hour=23, minute=59, second=59)
|
||||
query = query.filter(MaintenanceLog.timestamp.between(start_dt, end_dt))
|
||||
except ValueError:
|
||||
pass
|
||||
logs = query.order_by(MaintenanceLog.timestamp.desc()).all()
|
||||
return jsonify({'code': 200, 'data': [l.to_dict() for l in logs]})
|
||||
# =========================================================
|
||||
# 2. 历史数据接口
|
||||
# =========================================================
|
||||
|
||||
@api_bp.route('/device_data_by_date', methods=['GET'])
|
||||
def device_data_by_date():
|
||||
name = request.args.get('name')
|
||||
date_str = request.args.get('date') # 格式: 2026_01_13 或 2026-01-13
|
||||
|
||||
if not name or not date_str:
|
||||
return jsonify({'code': 400, 'message': 'Missing name or date'}), 400
|
||||
|
||||
device = Device.query.filter_by(name=name).first()
|
||||
if not device:
|
||||
return jsonify({'code': 404, 'message': 'Device not found'}), 404
|
||||
|
||||
content = None
|
||||
# 统一将下划线格式转换为横杠格式进行查询
|
||||
query_date = date_str.replace('_', '-')
|
||||
|
||||
# 1. 尝试从历史记录表中查找
|
||||
history_record = DeviceHistory.query.filter(
|
||||
DeviceHistory.device_id == device.id,
|
||||
DeviceHistory.data_time.like(f"{query_date}%")
|
||||
).order_by(desc(DeviceHistory.id)).first()
|
||||
|
||||
if history_record:
|
||||
content = history_record.json_data
|
||||
# 2. 如果历史表中没有,查当前 Device 表
|
||||
elif device.latest_time and device.latest_time.startswith(query_date):
|
||||
content = device.json_data
|
||||
|
||||
if content:
|
||||
return jsonify({
|
||||
'code': 200,
|
||||
'name': device.name,
|
||||
'source': device.source,
|
||||
'content': content
|
||||
})
|
||||
return jsonify({'code': 404, 'message': 'No data for this date'}), 404
|
||||
|
||||
|
||||
@api_bp.route('/logs/add', methods=['POST'])
|
||||
def add_log():
|
||||
data = request.get_json()
|
||||
try:
|
||||
new_log = MaintenanceLog(
|
||||
device_name=data.get('device_name', '未知设备'),
|
||||
engineer=data.get('engineer', ''),
|
||||
location=data.get('location', ''),
|
||||
content=data.get('content', '')
|
||||
)
|
||||
db.session.add(new_log)
|
||||
db.session.commit()
|
||||
return jsonify({'code': 200, 'message': 'Log saved'})
|
||||
except Exception as e:
|
||||
db.session.rollback()
|
||||
return jsonify({'code': 500, 'message': str(e)})
|
||||
# 兼容旧调用的 stub
|
||||
@api_bp.route('/device_data_by_date_stub', methods=['GET'])
|
||||
def device_data_by_date_stub():
|
||||
return device_data_by_date()
|
||||
|
||||
|
||||
@api_bp.route('/logs/update', methods=['POST'])
|
||||
def update_log():
|
||||
data = request.get_json()
|
||||
log = MaintenanceLog.query.get(data.get('id'))
|
||||
if not log: return jsonify({'code': 404, 'message': 'Not found'}), 404
|
||||
try:
|
||||
log.device_name = data.get('device_name', log.device_name)
|
||||
log.engineer = data.get('engineer', log.engineer)
|
||||
log.location = data.get('location', log.location)
|
||||
log.content = data.get('content', log.content)
|
||||
db.session.commit()
|
||||
return jsonify({'code': 200, 'message': 'Log updated'})
|
||||
except Exception as e:
|
||||
db.session.rollback()
|
||||
return jsonify({'code': 500, 'message': str(e)})
|
||||
# =========================================================
|
||||
# 3. 核心控制接口 (检测 & 写入)
|
||||
# =========================================================
|
||||
|
||||
|
||||
@api_bp.route('/logs/delete', methods=['POST'])
|
||||
def delete_log():
|
||||
data = request.get_json()
|
||||
log = MaintenanceLog.query.get(data.get('id'))
|
||||
if log:
|
||||
db.session.delete(log)
|
||||
db.session.commit()
|
||||
return jsonify({'code': 200, 'message': 'Deleted'})
|
||||
return jsonify({'code': 404, 'message': 'Not found'}), 404
|
||||
|
||||
|
||||
# =======================
|
||||
# 4. 一键检测 (双路并行,逻辑隔离)
|
||||
# =======================
|
||||
@api_bp.route('/run_monitor', methods=['POST'])
|
||||
def run_monitor():
|
||||
msg_list = []
|
||||
|
||||
try:
|
||||
# A. 爬虫任务 (写入历史)
|
||||
# 这部分负责更新 106/82 设备,并记录 DeviceHistory
|
||||
# --- A. 执行爬虫并入库 ---
|
||||
if execute_monitor_task:
|
||||
task_result = execute_monitor_task()
|
||||
if task_result:
|
||||
scraped_list = task_result.get('device_list', [])
|
||||
current_time = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
||||
count_crawler = 0
|
||||
|
||||
count_crawler = 0
|
||||
for item in scraped_list:
|
||||
d_name = item.get('name')
|
||||
if not d_name: continue
|
||||
|
||||
# 提取原始信息
|
||||
d_raw = item.get('raw_json', {})
|
||||
source = item.get('source', '')
|
||||
target_time = item.get('target_time') # 默认时间
|
||||
|
||||
# [旧代码逻辑保留] 针对 106 设备,从路径中强制解析正确的时间格式
|
||||
if '106' in str(source):
|
||||
try:
|
||||
path_str = d_raw.get('path', '')
|
||||
# 匹配形如 /Data/2026_01_13/xxx_15_30_00.csv
|
||||
match = re.search(r'/Data/(\d{4}_\d{2}_\d{2})/\w+_(\d{2}_\d{2}_\d{2})\.csv', path_str)
|
||||
if match:
|
||||
date_part = match.group(1).replace('_', '-') # 2026-01-13
|
||||
time_part = match.group(2).replace('_', ':') # 15:30:00
|
||||
target_time = f"{date_part} {time_part}"
|
||||
except:
|
||||
pass
|
||||
|
||||
# 查找或创建设备
|
||||
device = Device.query.filter_by(name=d_name).first()
|
||||
if not device:
|
||||
device = Device(name=d_name, source=item.get('source'), install_site="")
|
||||
device = Device(name=d_name, source=source, install_site="")
|
||||
db.session.add(device)
|
||||
db.session.flush()
|
||||
|
||||
# 纠正 source (防止爬虫设备被误标为 iot_card)
|
||||
if device.source == 'iot_card':
|
||||
device.source = item.get('source')
|
||||
device.source = source
|
||||
|
||||
# 更新主数据
|
||||
# 更新字段
|
||||
device.status = item.get('status')
|
||||
device.current_value = item.get('value')
|
||||
device.latest_time = item.get('target_time')
|
||||
device.latest_time = target_time
|
||||
device.check_time = current_time
|
||||
|
||||
# 更新 JSON
|
||||
# [新代码逻辑] 合并模式 (update),防止覆盖掉 bound_iccid
|
||||
old_json = {}
|
||||
try:
|
||||
old_json = json.loads(device.json_data)
|
||||
except:
|
||||
pass
|
||||
|
||||
new_json = item.get('raw_json', {})
|
||||
new_json = d_raw if isinstance(d_raw, dict) else item.get('raw_json', {})
|
||||
if isinstance(new_json, dict):
|
||||
old_json.update(new_json)
|
||||
|
||||
device.json_data = json.dumps(old_json, ensure_ascii=False)
|
||||
device.offset = calculate_offset(device.latest_time)
|
||||
|
||||
# 写入历史 (History Table)
|
||||
# 写入历史记录
|
||||
new_history = DeviceHistory(
|
||||
device_id=device.id,
|
||||
status=item.get('status'),
|
||||
result_data=item.get('value'),
|
||||
data_time=item.get('target_time'),
|
||||
data_time=target_time,
|
||||
json_data=device.json_data
|
||||
)
|
||||
db.session.add(new_history)
|
||||
@ -340,15 +439,14 @@ def run_monitor():
|
||||
else:
|
||||
msg_list.append("爬虫无数据")
|
||||
|
||||
# B. IoT 任务 (只更新 IoT 实时状态)
|
||||
# 这部分只更新 source='iot_card' 的记录,不产生历史
|
||||
# --- B. 执行 IoT 同步 (写入数据库) ---
|
||||
if sync_iot_data_service:
|
||||
iot_list = sync_iot_data_service()
|
||||
c, e = save_iot_cards_to_db(iot_list)
|
||||
if e:
|
||||
msg_list.append(f"IoT错: {e}")
|
||||
else:
|
||||
msg_list.append(f"IoT实时更新: {c}")
|
||||
msg_list.append(f"IoT更新: {c}")
|
||||
|
||||
db.session.commit()
|
||||
return jsonify({'code': 200, 'message': " | ".join(msg_list)})
|
||||
@ -358,15 +456,34 @@ def run_monitor():
|
||||
return jsonify({'code': 500, 'message': str(e)})
|
||||
|
||||
|
||||
# =======================
|
||||
# 5. 绑定与其他接口
|
||||
# =======================
|
||||
# =========================================================
|
||||
# 4. 白名单、绑定与设备管理 (新功能)
|
||||
# =========================================================
|
||||
|
||||
@api_bp.route('/toggle_whitelist', methods=['POST'])
|
||||
def toggle_whitelist():
|
||||
data = request.get_json()
|
||||
iccid = data.get('iccid')
|
||||
is_whitelist = data.get('is_whitelist')
|
||||
|
||||
sim_record = Device.query.filter_by(name=iccid, source='iot_card').first()
|
||||
if not sim_record:
|
||||
return jsonify({'code': 404, 'message': '未找到该卡片'})
|
||||
|
||||
try:
|
||||
j = json.loads(sim_record.json_data)
|
||||
j['is_whitelist'] = is_whitelist
|
||||
sim_record.json_data = json.dumps(j, ensure_ascii=False)
|
||||
db.session.commit()
|
||||
return jsonify({'code': 200, 'message': '设置成功'})
|
||||
except Exception as e:
|
||||
db.session.rollback()
|
||||
return jsonify({'code': 500, 'message': str(e)})
|
||||
|
||||
|
||||
@api_bp.route('/sync_iot_cards', methods=['POST'])
|
||||
def sync_iot_cards():
|
||||
"""单独同步 IoT (只更新 IoT 表)"""
|
||||
if not sync_iot_data_service:
|
||||
return jsonify({'code': 500, 'message': '服务缺失'}), 500
|
||||
if not sync_iot_data_service: return jsonify({'code': 500, 'message': '服务缺失'}), 500
|
||||
try:
|
||||
iot_list = sync_iot_data_service()
|
||||
c, e = save_iot_cards_to_db(iot_list)
|
||||
@ -379,32 +496,20 @@ def sync_iot_cards():
|
||||
|
||||
@api_bp.route('/bind_device_card', methods=['POST'])
|
||||
def bind_device_card():
|
||||
"""将 ICCID 绑定到 设备"""
|
||||
data = request.get_json()
|
||||
device_name = data.get('device_name')
|
||||
iccid = data.get('iccid')
|
||||
|
||||
target = Device.query.filter_by(name=device_name).first()
|
||||
target = Device.query.filter_by(name=data.get('device_name')).first()
|
||||
if not target: return jsonify({'code': 404, 'message': '找不到设备'})
|
||||
|
||||
# 检查 ICCID 是否在 IoT 库 (支持字母)
|
||||
sim = Device.query.filter_by(name=iccid, source='iot_card').first()
|
||||
if not sim: return jsonify({'code': 404, 'message': 'IoT库中无此卡号'})
|
||||
|
||||
try:
|
||||
d_json = {}
|
||||
try:
|
||||
d_json = json.loads(target.json_data)
|
||||
except:
|
||||
pass
|
||||
|
||||
d_json['bound_iccid'] = iccid
|
||||
d_json['bound_iccid'] = data.get('iccid')
|
||||
target.json_data = json.dumps(d_json, ensure_ascii=False)
|
||||
|
||||
db.session.commit()
|
||||
return jsonify({'code': 200, 'message': '绑定成功'})
|
||||
except Exception as e:
|
||||
db.session.rollback()
|
||||
return jsonify({'code': 500, 'message': str(e)})
|
||||
|
||||
|
||||
@ -417,11 +522,10 @@ def add_device():
|
||||
install_site=data.get('site', ''),
|
||||
source='manual',
|
||||
status='offline',
|
||||
current_value='0',
|
||||
latest_time='N/A',
|
||||
check_time=datetime.now().strftime("%Y-%m-%d %H:%M:%S"),
|
||||
json_data='{}',
|
||||
is_hidden=0, is_maintaining=0
|
||||
is_hidden=0,
|
||||
is_maintaining=0
|
||||
)
|
||||
db.session.add(new_device)
|
||||
db.session.commit()
|
||||
@ -437,7 +541,7 @@ def update_site():
|
||||
d.install_site = request.json.get('site')
|
||||
db.session.commit()
|
||||
return jsonify({'code': 200})
|
||||
return jsonify({'code': 404}), 404
|
||||
return jsonify({'code': 404})
|
||||
|
||||
|
||||
@api_bp.route('/toggle_maintenance', methods=['POST'])
|
||||
@ -447,7 +551,7 @@ def toggle_maintenance():
|
||||
d.is_maintaining = request.json.get('is_maintaining')
|
||||
db.session.commit()
|
||||
return jsonify({'code': 200})
|
||||
return jsonify({'code': 404}), 404
|
||||
return jsonify({'code': 404})
|
||||
|
||||
|
||||
@api_bp.route('/toggle_hidden', methods=['POST'])
|
||||
@ -457,9 +561,67 @@ def toggle_hidden():
|
||||
d.is_hidden = request.json.get('is_hidden')
|
||||
db.session.commit()
|
||||
return jsonify({'code': 200})
|
||||
return jsonify({'code': 404}), 404
|
||||
return jsonify({'code': 404})
|
||||
|
||||
|
||||
@api_bp.route('/device_data_by_date', methods=['GET'])
|
||||
def device_data_by_date_stub():
|
||||
return device_data_by_date()
|
||||
# =========================================================
|
||||
# 5. 日志管理接口 (CRUD)
|
||||
# =========================================================
|
||||
|
||||
@api_bp.route('/logs/list', methods=['GET'])
|
||||
def get_logs_list():
|
||||
keyword = request.args.get('keyword', '')
|
||||
query = MaintenanceLog.query
|
||||
if keyword:
|
||||
kw = f"%{keyword}%"
|
||||
query = query.filter(or_(
|
||||
MaintenanceLog.device_name.like(kw),
|
||||
MaintenanceLog.content.like(kw),
|
||||
MaintenanceLog.engineer.like(kw)
|
||||
))
|
||||
logs = query.order_by(MaintenanceLog.timestamp.desc()).all()
|
||||
return jsonify({'code': 200, 'data': [l.to_dict() for l in logs]})
|
||||
|
||||
|
||||
@api_bp.route('/logs/add', methods=['POST'])
|
||||
def add_log_entry():
|
||||
data = request.get_json()
|
||||
try:
|
||||
new_log = MaintenanceLog(
|
||||
device_name=data.get('device_name', ''),
|
||||
engineer=data.get('engineer', ''),
|
||||
location=data.get('location', ''),
|
||||
content=data.get('content', '')
|
||||
)
|
||||
db.session.add(new_log)
|
||||
db.session.commit()
|
||||
return jsonify({'code': 200})
|
||||
except Exception as e:
|
||||
return jsonify({'code': 500, 'message': str(e)})
|
||||
|
||||
|
||||
@api_bp.route('/logs/update', methods=['POST'])
|
||||
def update_log_entry():
|
||||
data = request.get_json()
|
||||
log = MaintenanceLog.query.get(data.get('id'))
|
||||
if not log: return jsonify({'code': 404})
|
||||
try:
|
||||
log.device_name = data.get('device_name', log.device_name)
|
||||
log.content = data.get('content', log.content)
|
||||
log.engineer = data.get('engineer', log.engineer)
|
||||
log.location = data.get('location', log.location)
|
||||
db.session.commit()
|
||||
return jsonify({'code': 200})
|
||||
except Exception as e:
|
||||
return jsonify({'code': 500})
|
||||
|
||||
|
||||
@api_bp.route('/logs/delete', methods=['POST'])
|
||||
def delete_log_entry():
|
||||
data = request.get_json()
|
||||
log = MaintenanceLog.query.get(data.get('id'))
|
||||
if log:
|
||||
db.session.delete(log)
|
||||
db.session.commit()
|
||||
return jsonify({'code': 200})
|
||||
return jsonify({'code': 404})
|
||||
@ -27,9 +27,9 @@
|
||||
<div class="status-summary">
|
||||
<el-tag color="#409EFF" effect="dark" class="legend-tag">修</el-tag>
|
||||
<el-tag color="#F56C6C" effect="dark" class="legend-tag">离线 / 严重滞后</el-tag>
|
||||
<el-tag color="#E6A23C" effect="dark" class="legend-tag">滞后 (>24h)</el-tag>
|
||||
<el-tag color="#E6A23C" effect="dark" class="legend-tag">滞后 / 流量超标</el-tag>
|
||||
<el-tag color="#FAC858" effect="dark" class="legend-tag" style="color: #333">数据异常 / 昨日</el-tag>
|
||||
<el-tag color="#67C23A" effect="dark" class="legend-tag">正常 (当天)</el-tag>
|
||||
<el-tag color="#67C23A" effect="dark" class="legend-tag">正常</el-tag>
|
||||
</div>
|
||||
|
||||
<div class="toolbar">
|
||||
@ -122,16 +122,30 @@
|
||||
</template>
|
||||
</el-table-column>
|
||||
|
||||
<el-table-column label="使用量" width="120" prop="trafficNum" sortable>
|
||||
<el-table-column label="本月流量" width="130" prop="trafficNum" sortable>
|
||||
<template #default="{ row }">
|
||||
<span v-if="row.isBound && row.trafficNum >= 0" style="font-weight: 600; color: #409EFF;">{{ row.trafficNum }} M</span>
|
||||
<div v-if="row.isBound">
|
||||
<span :style="{ fontWeight: '600', color: row.trafficWarning ? '#E6A23C' : '#606266' }">
|
||||
{{ row.trafficNum }} M
|
||||
</span>
|
||||
<el-tooltip v-if="row.trafficWarning" content="流量超标 (>=500M)" placement="top">
|
||||
<el-icon color="#E6A23C" style="margin-left: 4px; cursor: help;"><Warning /></el-icon>
|
||||
</el-tooltip>
|
||||
</div>
|
||||
<span v-else style="color: #ccc;">--</span>
|
||||
</template>
|
||||
</el-table-column>
|
||||
|
||||
<el-table-column label="截止时间" width="150">
|
||||
<el-table-column label="服务截止" width="140">
|
||||
<template #default="{ row }">
|
||||
<span v-if="row.isBound && row.stopDate" style="font-size: 13px;">{{ row.stopDate }}</span>
|
||||
<div v-if="row.isBound && row.stopDate">
|
||||
<span :style="{ color: row.expireWarning ? '#E6A23C' : '#606266', fontWeight: row.expireWarning ? 'bold' : 'normal' }">
|
||||
{{ row.stopDate }}
|
||||
</span>
|
||||
<el-tooltip v-if="row.expireWarning" content="即将过期 (<30天)" placement="top">
|
||||
<el-icon color="#E6A23C" style="margin-left: 4px; cursor: help;"><Warning /></el-icon>
|
||||
</el-tooltip>
|
||||
</div>
|
||||
<span v-else style="color: #ccc;">--</span>
|
||||
</template>
|
||||
</el-table-column>
|
||||
@ -139,7 +153,7 @@
|
||||
<el-table-column label="数据时效与质量" width="260" prop="sortWeight" sortable>
|
||||
<template #default="{ row }">
|
||||
<div style="font-size: 13px; display:flex; align-items:center; gap:5px; color: #606266; margin-bottom: 4px;">
|
||||
<el-icon><Clock /></el-icon> {{ row.latest_time || 'N/A' }}
|
||||
<el-icon><Clock /></el-icon> {{ row.latest_time || '尚未同步' }}
|
||||
</div>
|
||||
|
||||
<div v-if="!row.is_maintaining && !row.is_hidden">
|
||||
@ -153,17 +167,17 @@
|
||||
⚠️ {{ row.statusReason }}
|
||||
</div>
|
||||
<div v-else class="status-text success-text">
|
||||
✅ 时效最新
|
||||
✅ 状态正常
|
||||
</div>
|
||||
|
||||
<div v-if="row.statusType !== 'error' && row.statusType !== 'warning'" style="margin-top: 4px;">
|
||||
<div v-if="row.statusType !== 'error'" style="margin-top: 4px;">
|
||||
<el-tag v-if="row.data_quality === 'error'" type="danger" size="small" effect="dark">
|
||||
<el-icon><Warning /></el-icon> 数据严重异常
|
||||
</el-tag>
|
||||
<el-tag v-else-if="row.data_quality === 'warning'" type="warning" size="small" effect="dark">
|
||||
<el-icon><WarningFilled /></el-icon> 数值警告
|
||||
</el-tag>
|
||||
<el-tag v-else type="success" size="small" effect="plain">
|
||||
<el-tag v-else-if="row.statusType !== 'warning' && row.statusType !== 'slight-warning'" type="success" size="small" effect="plain">
|
||||
数值正常
|
||||
</el-tag>
|
||||
</div>
|
||||
@ -275,8 +289,9 @@ const fetchData = async () => {
|
||||
const isHidden = item.is_hidden === true || item.is_hidden === 1
|
||||
const isBound = !!item.isBound
|
||||
const isOrphanIoT = (item.source === 'iot_card')
|
||||
const isWhitelist = !!item.is_whitelist
|
||||
|
||||
// 1. 数据时效
|
||||
// 1. 数据时效处理
|
||||
let diffDays = 0, diffHours = 0, isToday = false, validTime = false
|
||||
let timeStr = item.latest_time
|
||||
|
||||
@ -292,20 +307,51 @@ const fetchData = async () => {
|
||||
}
|
||||
}
|
||||
|
||||
// 2. 状态判定与排序 (sortWeight: 越大越靠前)
|
||||
// 2. [恢复旧逻辑] 解析监测数值 (用于排序,虽然不显示但保留逻辑以免报错)
|
||||
let currentValueNum = 0
|
||||
if (item.current_value) {
|
||||
// 尝试提取数字,例如 "1024.5 M" -> 1024.5
|
||||
const match = String(item.current_value).match(/(\d+(\.\d+)?)/)
|
||||
if (match) {
|
||||
currentValueNum = parseFloat(match[0])
|
||||
}
|
||||
}
|
||||
|
||||
// 3. 流量与过期计算
|
||||
let trafficNum = 0
|
||||
let rawTraffic = item.usedTraffic
|
||||
if ((rawTraffic === undefined || rawTraffic === null) && item.json_data) {
|
||||
try {
|
||||
const j = JSON.parse(item.json_data)
|
||||
rawTraffic = j.usedTraffic
|
||||
} catch(e) {}
|
||||
}
|
||||
if (rawTraffic) {
|
||||
trafficNum = parseFloat(rawTraffic)
|
||||
if (isNaN(trafficNum)) trafficNum = 0
|
||||
}
|
||||
|
||||
const trafficWarning = (trafficNum >= 500 && !isWhitelist)
|
||||
let expireWarning = false
|
||||
if (item.stopDate && item.stopDate !== 'N/A') {
|
||||
const stopD = new Date(item.stopDate.replace(/_/g, '-'))
|
||||
if (!isNaN(stopD.getTime())) {
|
||||
const daysLeft = (stopD - now) / (1000 * 3600 * 24)
|
||||
if (daysLeft < 30) expireWarning = true
|
||||
}
|
||||
}
|
||||
|
||||
// 4. 状态判定与权重排序 (融合逻辑)
|
||||
let statusColor = '#67C23A', statusLabel = '正常', statusType = 'normal', statusLabelColor = '#fff'
|
||||
let statusReason = ''
|
||||
let sortWeight = diffHours
|
||||
let sortWeight = diffHours // 基础权重为滞后小时数
|
||||
|
||||
if (item.is_maintaining) {
|
||||
statusColor = '#409EFF'; statusLabel = '维修中'; statusType = 'maintenance';
|
||||
// [修复] 维修中置顶:使用最大安全整数
|
||||
sortWeight = Number.MAX_SAFE_INTEGER;
|
||||
} else if (!validTime) {
|
||||
statusLabel = '未知'; statusColor = '#909399'; statusType = 'slight-warning'; statusReason = '从未同步';
|
||||
sortWeight = 90000000;
|
||||
} else if (item.status === 'offline') {
|
||||
statusLabel = '离线'; statusColor = '#F56C6C'; statusType = 'error'; statusReason = '设备离线';
|
||||
} else if (!validTime || item.status === 'offline') {
|
||||
statusLabel = '离线'; statusColor = '#F56C6C'; statusType = 'error';
|
||||
statusReason = validTime ? '设备离线' : '暂无数据(离线)';
|
||||
sortWeight = 80000000;
|
||||
} else if (diffDays > 7) {
|
||||
statusLabel = '严重滞后'; statusColor = '#F56C6C'; statusType = 'error';
|
||||
@ -313,6 +359,14 @@ const fetchData = async () => {
|
||||
} else if (diffHours > 24) {
|
||||
statusLabel = '滞后'; statusColor = '#E6A23C'; statusType = 'warning';
|
||||
statusReason = `滞后 ${Math.floor(diffDays)} 天`;
|
||||
} else if (trafficWarning) {
|
||||
statusLabel = '流量警告'; statusColor = '#E6A23C'; statusType = 'warning';
|
||||
statusReason = `流量超标`;
|
||||
sortWeight = 500;
|
||||
} else if (expireWarning) {
|
||||
statusLabel = '即将过期'; statusColor = '#E6A23C'; statusType = 'warning';
|
||||
statusReason = `卡片即将过期`;
|
||||
sortWeight = 400;
|
||||
} else if (!isToday) {
|
||||
statusLabel = '昨日数据'; statusColor = '#FAC858'; statusType = 'slight-warning'; statusLabelColor = '#333';
|
||||
statusReason = '非今日数据';
|
||||
@ -320,32 +374,22 @@ const fetchData = async () => {
|
||||
sortWeight = 0;
|
||||
}
|
||||
|
||||
// 3. 流量值解析 (防御性:确保是数字)
|
||||
let trafficNum = 0
|
||||
// 尝试直接取
|
||||
if (item.usedTraffic) {
|
||||
trafficNum = parseFloat(item.usedTraffic)
|
||||
} else if (item.json_data) {
|
||||
// 尝试从 json 中取
|
||||
try {
|
||||
const j = JSON.parse(item.json_data)
|
||||
if (j.usedTraffic) trafficNum = parseFloat(j.usedTraffic)
|
||||
} catch(e) {}
|
||||
}
|
||||
if (isNaN(trafficNum)) trafficNum = 0
|
||||
|
||||
return {
|
||||
...item,
|
||||
is_hidden: isHidden,
|
||||
isOrphanIoT,
|
||||
isBound,
|
||||
isWhitelist,
|
||||
diffDays, diffHours, sortWeight, isToday,
|
||||
statusColor, statusLabel, statusType, statusLabelColor, statusReason,
|
||||
isEditingSite: false, tempSite: '',
|
||||
data_quality: item.data_quality || 'ok',
|
||||
trafficNum
|
||||
currentValueNum,
|
||||
trafficNum,
|
||||
trafficWarning,
|
||||
expireWarning
|
||||
}
|
||||
}).sort((a, b) => b.sortWeight - a.sortWeight) // 按权重降序
|
||||
}).sort((a, b) => b.sortWeight - a.sortWeight)
|
||||
|
||||
lastCheckTime.value = new Date().toLocaleString()
|
||||
} catch (e) {
|
||||
@ -369,20 +413,21 @@ const summary = computed(() => {
|
||||
|
||||
const filteredData = computed(() => {
|
||||
return rawData.value.filter(item => {
|
||||
// 隐藏孤儿卡
|
||||
if (item.isOrphanIoT) return false
|
||||
|
||||
if (filters.status === 'hidden') return item.is_hidden
|
||||
if (item.is_hidden) return false
|
||||
|
||||
if (filters.status === 'abnormal') return ['error', 'warning', 'slight-warning'].includes(item.statusType)
|
||||
if (filters.status === 'data_error') return ['error', 'warning'].includes(item.data_quality)
|
||||
return true
|
||||
}).filter(item => !filters.keyword || item.name.toLowerCase().includes(filters.keyword.toLowerCase()))
|
||||
})
|
||||
|
||||
// === [重要修复] 卡池总用量计算 ===
|
||||
// === 卡池总用量 ===
|
||||
const totalUsageSum = computed(() => {
|
||||
return rawData.value.reduce((sum, item) => {
|
||||
// 1. 只统计 source='iot_card' (代表SIM卡)
|
||||
// 2. 累加 item.trafficNum (这是我们在 fetchData 里解析好的数字)
|
||||
if (item.source === 'iot_card') {
|
||||
return sum + (item.trafficNum || 0)
|
||||
}
|
||||
@ -390,7 +435,7 @@ const totalUsageSum = computed(() => {
|
||||
}, 0)
|
||||
})
|
||||
|
||||
// ... 交互函数保持不变 ...
|
||||
// === 交互函数 ===
|
||||
const handleAddDeviceSubmit = async () => { if (!newDeviceForm.name) return; await axios.post(`${API_BASE}/api/add_device`, newDeviceForm); showAddDialog.value=false; fetchData() }
|
||||
const handleDeviceClick = (row) => { if (!row.is_hidden && dataMonitorRef.value) dataMonitorRef.value.open(row) }
|
||||
const openLogCenter = (row) => { if (maintenanceLogsRef.value) maintenanceLogsRef.value.open(row ? { deviceName: row.name } : null) }
|
||||
@ -402,20 +447,24 @@ const handleMaintenanceBeforeChange = (row) => { return new Promise(r => { axios
|
||||
const toggleHidden = async (row, val) => { await axios.post(`${API_BASE}/api/toggle_hidden`, {name:row.name, is_hidden:val}); row.is_hidden=val; fetchData() }
|
||||
const handleLogout = () => { localStorage.removeItem('token'); router.push('/') }
|
||||
const formatDisplayName = (name) => name ? name.toUpperCase().replace(/_/g, ' ') : ''
|
||||
|
||||
// 行高亮逻辑 (融合了数据异常的高亮)
|
||||
const tableRowClassName = ({ row }) => {
|
||||
if (row.is_hidden) return 'hidden-row'
|
||||
if (row.data_quality === 'error') return 'data-error-row' // 优先显示数值严重错误
|
||||
if (row.statusType === 'error') return 'error-row'
|
||||
if (row.data_quality === 'warning') return 'data-warning-row' // 数值警告
|
||||
if (row.statusType === 'warning') return 'warning-row'
|
||||
if (row.statusType === 'maintenance') return 'maintenance-row'
|
||||
return ''
|
||||
}
|
||||
|
||||
const updateDimensions = () => { windowHeight.value = window.innerHeight; windowWidth.value = window.innerWidth }
|
||||
onMounted(() => { fetchData(); window.addEventListener('resize', updateDimensions) })
|
||||
onBeforeUnmount(() => window.removeEventListener('resize', updateDimensions))
|
||||
</script>
|
||||
|
||||
<style scoped>
|
||||
/* 样式部分保持原样 */
|
||||
.dashboard-container { padding: 10px; background: #f5f7fa; min-height: 100vh; box-sizing: border-box; }
|
||||
.main-card { border-radius: 8px; }
|
||||
.header-row { display: flex; justify-content: space-between; align-items: center; flex-wrap: wrap; gap: 10px; }
|
||||
@ -450,8 +499,10 @@ onBeforeUnmount(() => window.removeEventListener('resize', updateDimensions))
|
||||
:deep(.warning-row) { background-color: #fdf6ec !important; }
|
||||
:deep(.maintenance-row) { background-color: #f0f9ff !important; }
|
||||
:deep(.hidden-row) { background-color: #f4f4f5 !important; color: #909399; }
|
||||
/* 增加原有代码的数据异常背景色 */
|
||||
:deep(.data-error-row) { background-color: #ffe6e6 !important; }
|
||||
:deep(.data-warning-row) { background-color: #fffbe6 !important; }
|
||||
|
||||
@media screen and (max-width: 768px) {
|
||||
.dashboard-container { padding: 5px; }
|
||||
.left-panel, .header-actions { width: 100%; justify-content: space-between; margin-bottom: 5px; }
|
||||
|
||||
@ -3,7 +3,7 @@
|
||||
<el-dialog
|
||||
v-model="visible"
|
||||
title="🔗 IoT 卡片管理与绑定"
|
||||
width="950px"
|
||||
width="1000px"
|
||||
top="8vh"
|
||||
destroy-on-close
|
||||
append-to-body
|
||||
@ -12,16 +12,11 @@
|
||||
<div class="binder-container">
|
||||
|
||||
<div class="tips-alert">
|
||||
<el-alert
|
||||
title="功能说明"
|
||||
type="info"
|
||||
show-icon
|
||||
:closable="false"
|
||||
>
|
||||
<el-alert title="功能说明" type="info" show-icon :closable="false">
|
||||
<template #default>
|
||||
<div>1. 此处展示所有 IoT 卡片 (包括已绑定和未绑定的)。</div>
|
||||
<div>2. <b>绑定要求:</b> 目标设备必须是系统中已存在的设备,不能随意输入不存在的名称。</div>
|
||||
<div>3. 绑定后,该设备的流量数据将由对应的 ICCID 卡片提供。</div>
|
||||
<div>1. <b>白名单置顶:</b> 开启白名单的卡片会自动排在列表最上方。</div>
|
||||
<div>2. <b>绑定要求:</b> 目标设备必须是系统中已存在的设备。</div>
|
||||
<div>3. <b>流量警告:</b> 白名单卡片即使流量超过 500M 也不会触发黄色警告。</div>
|
||||
</template>
|
||||
</el-alert>
|
||||
</div>
|
||||
@ -61,16 +56,15 @@
|
||||
</template>
|
||||
</el-table-column>
|
||||
|
||||
<el-table-column label="关联设备状态" min-width="320">
|
||||
<el-table-column label="关联设备状态" min-width="300">
|
||||
<template #default="{ row }">
|
||||
|
||||
<div v-if="row.isEditing" class="edit-cell">
|
||||
<el-autocomplete
|
||||
v-model="row.targetDeviceName"
|
||||
:fetch-suggestions="querySearchDevice"
|
||||
placeholder="请输入并选择设备..."
|
||||
size="small"
|
||||
style="width: 200px;"
|
||||
style="width: 180px;"
|
||||
@select="handleSelectDevice"
|
||||
@keyup.enter="saveBinding(row)"
|
||||
ref="nameInputRef"
|
||||
@ -84,9 +78,8 @@
|
||||
</div>
|
||||
</template>
|
||||
</el-autocomplete>
|
||||
|
||||
<el-button type="success" size="small" icon="Check" circle @click="saveBinding(row)" :loading="row.saving" title="确认绑定" />
|
||||
<el-button type="info" size="small" icon="Close" circle @click="cancelEdit(row)" title="取消" />
|
||||
<el-button type="success" size="small" icon="Check" circle @click="saveBinding(row)" :loading="row.saving" />
|
||||
<el-button type="info" size="small" icon="Close" circle @click="cancelEdit(row)" />
|
||||
</div>
|
||||
|
||||
<div v-else-if="row.boundDeviceName" class="bound-cell">
|
||||
@ -97,24 +90,30 @@
|
||||
</div>
|
||||
|
||||
<div v-else class="unbound-cell" @click="startEdit(row)">
|
||||
<span class="placeholder-text">🔴 尚未关联设备,点击绑定...</span>
|
||||
<span class="placeholder-text">🔴 尚未关联,点击绑定...</span>
|
||||
<el-icon class="edit-icon"><EditPen /></el-icon>
|
||||
</div>
|
||||
|
||||
</template>
|
||||
</el-table-column>
|
||||
|
||||
<el-table-column label="卡状态" width="100" align="center">
|
||||
<el-table-column label="当月用量" width="120" align="right" sortable prop="usedTrafficNum">
|
||||
<template #default="{ row }">
|
||||
<el-tag :type="row.status === 'online' ? 'success' : 'info'" effect="dark" size="small">
|
||||
{{ row.status === 'online' ? '在线' : '离线' }}
|
||||
</el-tag>
|
||||
<span :style="{ fontWeight: row.usedTrafficNum >= 500 && !row.isWhitelist ? 'bold' : 'normal', color: row.usedTrafficNum >= 500 && !row.isWhitelist ? '#E6A23C' : '#606266' }">
|
||||
{{ row.usedTraffic }} M
|
||||
</span>
|
||||
</template>
|
||||
</el-table-column>
|
||||
|
||||
<el-table-column label="当月用量" width="120" align="right">
|
||||
<el-table-column label="白名单" width="100" align="center">
|
||||
<template #default="{ row }">
|
||||
<span>{{ row.usedTraffic }} M</span>
|
||||
<el-switch
|
||||
v-model="row.isWhitelist"
|
||||
active-text="是"
|
||||
inactive-text="否"
|
||||
inline-prompt
|
||||
:loading="row.whitelistLoading"
|
||||
:before-change="() => toggleWhitelist(row)"
|
||||
/>
|
||||
</template>
|
||||
</el-table-column>
|
||||
|
||||
@ -141,46 +140,52 @@ import zhCn from 'element-plus/es/locale/lang/zh-cn'
|
||||
const API_BASE = import.meta.env.DEV ? 'http://127.0.0.1:5000' : ''
|
||||
const visible = ref(false)
|
||||
const loading = ref(false)
|
||||
|
||||
// 数据源
|
||||
const fullSimList = ref([]) // 所有的 SIM 卡列表
|
||||
const displayList = ref([]) // 表格展示列表
|
||||
const allDeviceNames = ref([]) // 所有的真实设备 (用于自动补全和验证)
|
||||
|
||||
// 筛选条件
|
||||
const fullSimList = ref([])
|
||||
const displayList = ref([])
|
||||
const allDeviceNames = ref([])
|
||||
const keyword = ref('')
|
||||
const filterStatus = ref('unbound') // 默认看未绑定的
|
||||
const filterStatus = ref('all')
|
||||
|
||||
const emit = defineEmits(['update-success'])
|
||||
|
||||
// 1. 打开弹窗
|
||||
const open = () => {
|
||||
visible.value = true
|
||||
filterStatus.value = 'unbound' // 每次打开默认只看未绑定的,方便操作
|
||||
filterStatus.value = 'all'
|
||||
fetchIoTDevices()
|
||||
}
|
||||
|
||||
// 2. 获取数据 (逻辑升级:聚合所有卡片信息)
|
||||
// 本地排序逻辑
|
||||
const applySort = () => {
|
||||
fullSimList.value.sort((a, b) => {
|
||||
// 1. 白名单 (True 在前)
|
||||
if (a.isWhitelist !== b.isWhitelist) {
|
||||
return a.isWhitelist ? -1 : 1
|
||||
}
|
||||
// 2. 绑定状态
|
||||
const aBound = !!a.boundDeviceName
|
||||
const bBound = !!b.boundDeviceName
|
||||
if (aBound !== bBound) {
|
||||
return aBound ? -1 : 1
|
||||
}
|
||||
// 3. 默认顺序
|
||||
return a.iccid.localeCompare(b.iccid)
|
||||
})
|
||||
// 重新执行过滤以应用排序到显示列表
|
||||
filterList()
|
||||
}
|
||||
|
||||
const fetchIoTDevices = async () => {
|
||||
loading.value = true
|
||||
try {
|
||||
const res = await axios.get(`${API_BASE}/api/devices_overview`)
|
||||
const allData = res.data.data || []
|
||||
|
||||
// 1. 构建 [ICCID -> 设备名] 的查找表 (核心修复点)
|
||||
const iccidToDeviceMap = {}
|
||||
|
||||
// 提取真实设备用于下拉建议
|
||||
const realDevices = []
|
||||
|
||||
allData.forEach(d => {
|
||||
// 如果是真实设备
|
||||
if (d.source !== 'iot_card') {
|
||||
realDevices.push({
|
||||
value: d.name,
|
||||
site: d.install_site || '未填地点'
|
||||
})
|
||||
// 如果它绑定了卡,记录到映射表
|
||||
realDevices.push({ value: d.name, site: d.install_site || '未填地点' })
|
||||
if (d.bound_iccid) {
|
||||
iccidToDeviceMap[d.bound_iccid] = d.name
|
||||
}
|
||||
@ -188,202 +193,154 @@ const fetchIoTDevices = async () => {
|
||||
})
|
||||
allDeviceNames.value = realDevices
|
||||
|
||||
// 2. 提取所有 SIM 卡记录 (source='iot_card')
|
||||
// 这些就是所有的物理卡片
|
||||
const cards = allData.filter(d => d.source === 'iot_card')
|
||||
|
||||
const tempList = cards.map(c => {
|
||||
const iccid = c.name // 这里的 name 就是 ICCID
|
||||
|
||||
// 尝试解析 JSON
|
||||
const iccid = c.name
|
||||
let j = {}
|
||||
try { j = JSON.parse(c.json_data || '{}') } catch(e){}
|
||||
|
||||
// **核心修复**:直接从映射表里查,这张卡被谁绑定了
|
||||
// 如果 iccidToDeviceMap[iccid] 有值,说明它被绑定了
|
||||
const ownerDevice = iccidToDeviceMap[iccid] || ''
|
||||
|
||||
// 流量解析
|
||||
let traffic = c.usedTraffic || j.usedTraffic || '0'
|
||||
let trafficNum = parseFloat(traffic) || 0
|
||||
|
||||
let isW = false
|
||||
if (j.is_whitelist !== undefined) isW = j.is_whitelist
|
||||
if (c.is_whitelist !== undefined) isW = c.is_whitelist
|
||||
|
||||
return {
|
||||
iccid: iccid,
|
||||
tag: j.tag || '',
|
||||
usedTraffic: traffic,
|
||||
boundDeviceName: ownerDevice, // 这样刷新后绑定关系绝对不会丢
|
||||
usedTrafficNum: trafficNum,
|
||||
boundDeviceName: ownerDevice,
|
||||
targetDeviceName: ownerDevice,
|
||||
status: c.status || 'offline',
|
||||
isWhitelist: !!isW,
|
||||
isEditing: false,
|
||||
saving: false
|
||||
saving: false,
|
||||
whitelistLoading: false
|
||||
}
|
||||
})
|
||||
|
||||
fullSimList.value = tempList
|
||||
filterList()
|
||||
applySort()
|
||||
|
||||
} catch (e) {
|
||||
ElMessage.error('加载列表失败')
|
||||
console.error(e)
|
||||
ElMessage.error('加载失败')
|
||||
} finally {
|
||||
loading.value = false
|
||||
}
|
||||
}
|
||||
|
||||
// 3. 列表筛选
|
||||
const filterList = () => {
|
||||
let list = fullSimList.value
|
||||
|
||||
// 状态筛选
|
||||
if (filterStatus.value === 'bound') {
|
||||
list = list.filter(item => item.boundDeviceName)
|
||||
} else if (filterStatus.value === 'unbound') {
|
||||
list = list.filter(item => !item.boundDeviceName)
|
||||
}
|
||||
|
||||
// 关键词筛选
|
||||
if (filterStatus.value === 'bound') list = list.filter(i => i.boundDeviceName)
|
||||
if (filterStatus.value === 'unbound') list = list.filter(i => !i.boundDeviceName)
|
||||
if (keyword.value) {
|
||||
const k = keyword.value.toLowerCase()
|
||||
list = list.filter(item =>
|
||||
item.iccid.toLowerCase().includes(k) ||
|
||||
(item.boundDeviceName && item.boundDeviceName.toLowerCase().includes(k))
|
||||
)
|
||||
list = list.filter(i => i.iccid.toLowerCase().includes(k) || (i.boundDeviceName && i.boundDeviceName.toLowerCase().includes(k)))
|
||||
}
|
||||
|
||||
displayList.value = list
|
||||
}
|
||||
|
||||
// 4. 开始编辑/绑定
|
||||
const startEdit = (row) => {
|
||||
// 取消其他行的编辑状态
|
||||
displayList.value.forEach(item => { if(item !== row) cancelEdit(item) })
|
||||
|
||||
// 设置输入框初始值:如果是修改,填入旧名字;如果是新绑定,为空
|
||||
displayList.value.forEach(i => i.isEditing = false)
|
||||
row.targetDeviceName = row.boundDeviceName || ''
|
||||
row.isEditing = true
|
||||
|
||||
nextTick(() => {
|
||||
// 自动聚焦
|
||||
const el = document.querySelector('.edit-cell input')
|
||||
if (el) el.focus()
|
||||
})
|
||||
nextTick(() => document.querySelector('.edit-cell input')?.focus())
|
||||
}
|
||||
|
||||
const cancelEdit = (row) => {
|
||||
row.isEditing = false
|
||||
// 恢复原值 (如果没保存)
|
||||
row.targetDeviceName = row.boundDeviceName
|
||||
}
|
||||
|
||||
// 5. 自动补全逻辑 (支持模糊搜索)
|
||||
const querySearchDevice = (queryString, cb) => {
|
||||
const results = queryString
|
||||
? allDeviceNames.value.filter(createFilter(queryString))
|
||||
: allDeviceNames.value
|
||||
cb(results)
|
||||
const querySearchDevice = (qs, cb) => {
|
||||
const res = qs ? allDeviceNames.value.filter(i => i.value.toLowerCase().indexOf(qs.toLowerCase()) > -1) : allDeviceNames.value
|
||||
cb(res)
|
||||
}
|
||||
const handleSelectDevice = (item) => {}
|
||||
|
||||
// 模糊匹配逻辑:包含即可
|
||||
const createFilter = (queryString) => {
|
||||
return (item) => {
|
||||
return (item.value.toLowerCase().indexOf(queryString.toLowerCase()) > -1)
|
||||
}
|
||||
}
|
||||
|
||||
const handleSelectDevice = (item) => {
|
||||
// 选中后自动触发保存逻辑可以在这里写,但为了安全,还是让用户按回车或点对号
|
||||
}
|
||||
|
||||
// 6. 保存绑定 (核心逻辑 + 强制验证)
|
||||
const saveBinding = async (row) => {
|
||||
const targetName = row.targetDeviceName
|
||||
if (!targetName) return ElMessage.warning('请输入目标设备名称')
|
||||
|
||||
// [新增] 强制验证:输入的名字必须存在于 allDeviceNames 列表中
|
||||
const isValidDevice = allDeviceNames.value.some(d => d.value === targetName)
|
||||
if (!isValidDevice) {
|
||||
ElMessage.error(`设备 "${targetName}" 不存在!请先在主界面新增该设备。`)
|
||||
return
|
||||
}
|
||||
const target = row.targetDeviceName
|
||||
if (!target) return ElMessage.warning('请输入设备名')
|
||||
const exists = allDeviceNames.value.some(d => d.value === target)
|
||||
if (!exists) return ElMessage.error('设备不存在,请先新建设备')
|
||||
|
||||
row.saving = true
|
||||
try {
|
||||
// 调用后端绑定接口
|
||||
await axios.post(`${API_BASE}/api/bind_device_card`, {
|
||||
iccid: row.iccid,
|
||||
device_name: targetName
|
||||
})
|
||||
|
||||
ElMessage.success(`绑定成功: ${row.iccid} -> ${targetName}`)
|
||||
|
||||
// 更新本地状态,避免必须刷新全量
|
||||
row.boundDeviceName = targetName
|
||||
await axios.post(`${API_BASE}/api/bind_device_card`, { iccid: row.iccid, device_name: target })
|
||||
ElMessage.success('绑定成功')
|
||||
row.boundDeviceName = target
|
||||
row.isEditing = false
|
||||
|
||||
// 重新触发筛选逻辑 (因为绑定状态变了)
|
||||
filterList()
|
||||
|
||||
// 通知父组件刷新主列表
|
||||
emit('update-success')
|
||||
|
||||
fetchIoTDevices()
|
||||
} catch (e) {
|
||||
const msg = e.response?.data?.message || '绑定失败'
|
||||
ElMessage.error(msg)
|
||||
ElMessage.error(e.response?.data?.message || '失败')
|
||||
} finally {
|
||||
row.saving = false
|
||||
}
|
||||
}
|
||||
|
||||
const handleClose = () => {
|
||||
visible.value = false
|
||||
// ---------------------------------------------------------
|
||||
// [核心修复] 白名单切换逻辑
|
||||
// ---------------------------------------------------------
|
||||
const toggleWhitelist = (row) => {
|
||||
// 设置 Loading,防止重复点击
|
||||
row.whitelistLoading = true
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
// 预期的新状态 (当前状态取反)
|
||||
const targetVal = !row.isWhitelist
|
||||
|
||||
axios.post(`${API_BASE}/api/toggle_whitelist`, {
|
||||
iccid: row.iccid,
|
||||
is_whitelist: targetVal
|
||||
}).then(() => {
|
||||
// 1. API 成功
|
||||
ElMessage.success(targetVal ? '已加入白名单' : '已移出白名单')
|
||||
|
||||
// 2. 触发父组件更新 (Dashboard 计数等)
|
||||
emit('update-success')
|
||||
|
||||
// 3. 关键:resolve(true) 会告诉 el-switch 组件可以切换视觉状态了
|
||||
// 此时 Vue 会自动更新 v-model (即 row.isWhitelist) 的值
|
||||
// 我们不需要在这里手动写 row.isWhitelist = targetVal
|
||||
resolve(true)
|
||||
|
||||
row.whitelistLoading = false
|
||||
|
||||
// 4. 延迟触发排序
|
||||
// 为什么要延迟?
|
||||
// A. 等待 el-switch 动画播放
|
||||
// B. 确保 v-model 的值已经确实更新到了 row 对象上
|
||||
setTimeout(() => {
|
||||
applySort()
|
||||
}, 300)
|
||||
|
||||
}).catch(() => {
|
||||
// 失败:El-switch 保持原状
|
||||
ElMessage.error('操作失败')
|
||||
row.whitelistLoading = false
|
||||
reject(new Error('Failed'))
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
const handleClose = () => { visible.value = false }
|
||||
defineExpose({ open })
|
||||
</script>
|
||||
|
||||
<style scoped>
|
||||
.binder-container { padding: 5px; }
|
||||
.tips-alert { margin-bottom: 15px; }
|
||||
.toolbar { display: flex; align-items: center; margin-bottom: 15px; }
|
||||
|
||||
.iccid-text { font-family: monospace; font-weight: bold; color: #606266; font-size: 13px; }
|
||||
|
||||
/* 绑定状态单元格 */
|
||||
.iccid-text { font-family: monospace; font-weight: bold; color: #606266; }
|
||||
.bound-cell { display: flex; align-items: center; justify-content: space-between; }
|
||||
.bound-tag { font-size: 13px; padding: 0 10px; height: 28px; line-height: 26px; }
|
||||
|
||||
.unbound-cell {
|
||||
cursor: pointer;
|
||||
color: #909399;
|
||||
font-style: italic;
|
||||
font-size: 13px;
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: space-between;
|
||||
padding: 4px 8px;
|
||||
border: 1px dashed transparent;
|
||||
border-radius: 4px;
|
||||
}
|
||||
.unbound-cell:hover {
|
||||
background-color: #f0f9ff;
|
||||
border-color: #a0cfff;
|
||||
color: #409EFF;
|
||||
}
|
||||
|
||||
.unbound-cell { cursor: pointer; color: #909399; font-style: italic; font-size: 13px; display:flex; align-items:center; justify-content:space-between; padding:4px 8px; border:1px dashed transparent; border-radius:4px; }
|
||||
.unbound-cell:hover { background-color: #f0f9ff; border-color: #a0cfff; color: #409EFF; }
|
||||
.edit-cell { display: flex; align-items: center; gap: 5px; }
|
||||
|
||||
/* 自动补全下拉项样式 */
|
||||
.suggestion-item {
|
||||
display: flex;
|
||||
justify-content: space-between;
|
||||
align-items: center;
|
||||
width: 100%;
|
||||
}
|
||||
.device-name-highlight { font-weight: bold; color: #333; }
|
||||
.suggestion-site { color: #999; font-size: 12px; margin-left: 10px; }
|
||||
|
||||
.empty-tip {
|
||||
text-align: center;
|
||||
color: #909399;
|
||||
padding: 40px;
|
||||
}
|
||||
.suggestion-item { display: flex; justify-content: space-between; width: 100%; }
|
||||
.suggestion-site { color: #999; font-size: 12px; }
|
||||
.empty-tip { text-align: center; color: #909399; padding: 40px; }
|
||||
</style>
|
||||
|
||||
Reference in New Issue
Block a user