diff --git a/.idea/ZDXX.iml b/.idea/ZDXX.iml new file mode 100644 index 0000000..fa47c6d --- /dev/null +++ b/.idea/ZDXX.iml @@ -0,0 +1,12 @@ + + + + + + + + + + \ No newline at end of file diff --git a/2.1版本/instance/binary/TowerIS2_012_data_141656.db b/2.1版本/instance/binary/TowerIS2_012_data_141656.db deleted file mode 100644 index d5235a5..0000000 Binary files a/2.1版本/instance/binary/TowerIS2_012_data_141656.db and /dev/null differ diff --git a/2.1版本/instance/devices.db b/2.1版本/instance/devices.db deleted file mode 100644 index f04cc77..0000000 Binary files a/2.1版本/instance/devices.db and /dev/null differ diff --git a/2.1版本/models.py b/2.1版本/models.py index a19d783..f20fc5d 100644 --- a/2.1版本/models.py +++ b/2.1版本/models.py @@ -1,7 +1,7 @@ # models.py from datetime import datetime import json -from extensions import db # ✅ 必须从 extensions 导入 +from extensions import db class Device(db.Model): __tablename__ = 'devices' @@ -13,38 +13,29 @@ class Device(db.Model): # 快照字段 status = db.Column(db.String(50)) current_value = db.Column(db.String(200)) - latest_time = db.Column(db.String(50)) - json_data = db.Column(db.Text) + latest_time = db.Column(db.String(50)) # 原始抓取时间字符串 + json_data = db.Column(db.Text) # 完整数据 - check_time = db.Column(db.String(50)) + check_time = db.Column(db.String(50)) # 系统检查时间 reason = db.Column(db.String(255)) - offset = db.Column(db.String(50)) + offset = db.Column(db.String(50)) # 滞后描述 install_site = db.Column(db.String(100), default="") is_maintaining = db.Column(db.Boolean, default=False) is_hidden = db.Column(db.Boolean, default=False) - history = db.relationship('DeviceHistory', backref='device', lazy='dynamic', cascade='all, delete-orphan') - def to_dict(self): # 兼容处理 API 返回 api_status = 'offline' if self.status in ['离线', '异常', '已离线'] else 'online' - raw_obj = None - if self.json_data: - try: - raw_obj = json.loads(self.json_data) - except: - raw_obj = self.json_data - + # 注意:列表接口不返回 json_data 以提高性能 return { 'id': self.id, 'name': self.name, 'source': self.source, 'latest_time': self.latest_time, - 'status': api_status, # 前端用这个判断颜色 - 'status_text': self.status, # 显示文本 + 'status': api_status, + 'status_text': self.status, 'value': self.current_value, - 'raw_json': raw_obj, 'reason': self.reason, 'install_site': self.install_site, 'is_maintaining': self.is_maintaining, @@ -52,7 +43,6 @@ class Device(db.Model): 'offset': self.offset } - class DeviceHistory(db.Model): __tablename__ = 'device_history' @@ -67,18 +57,21 @@ class DeviceHistory(db.Model): recorded_at = db.Column(db.DateTime, default=datetime.now) - class MaintenanceLog(db.Model): - __tablename__ = 'maintenance_log' + __tablename__ = 'maintenance_logs' id = db.Column(db.Integer, primary_key=True) - device_name = db.Column(db.String(100)) - content = db.Column(db.Text) - timestamp = db.Column(db.DateTime, default=datetime.now) + device_name = db.Column(db.String(100), nullable=False) + engineer = db.Column(db.String(50)) # 工程师 + location = db.Column(db.String(100)) # 地点 + content = db.Column(db.Text) # 事件内容 + timestamp = db.Column(db.DateTime, default=datetime.now) # 自动记录时间 def to_dict(self): return { 'id': self.id, 'device_name': self.device_name, + 'engineer': self.engineer or '', + 'location': self.location or '', 'content': self.content, - 'timestamp': self.timestamp.strftime('%Y-%m-%d %H:%M:%S') if self.timestamp else "" + 'timestamp': self.timestamp.strftime('%Y-%m-%d %H:%M:%S') } \ No newline at end of file diff --git a/2.1版本/routes/api.py b/2.1版本/routes/api.py index 842ef6a..1346f3b 100644 --- a/2.1版本/routes/api.py +++ b/2.1版本/routes/api.py @@ -1,26 +1,170 @@ -# routes/api.py import os import shutil import json -from flask import Blueprint, jsonify, request +import re # [新增] 引入正则模块用于解析路径 from datetime import datetime -from extensions import db # ✅ 从 extensions 导入 db 用于提交事务 +from flask import Blueprint, jsonify, request +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 -# ✅ 定义蓝图,设置统一前缀 /api api_bp = Blueprint('api', __name__, url_prefix='/api') +# ======================= +# 1. 设备概览与详情接口 +# ======================= + +@api_bp.route('/devices_overview', methods=['GET']) +def devices_overview(): + try: + devices = Device.query.all() + data_list = [d.to_dict() for d in devices] + return jsonify({'code': 200, 'data': data_list}) + except Exception as e: + return jsonify({'code': 500, 'message': str(e)}) + + +@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') # 前端传来的格式通常是 YYYY-MM-DD + + 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 + + # 1. 查历史表 + # 注意:如果数据是通过 run_monitor 经过处理保存的,data_time 应该是标准的 YYYY-MM-DD 格式 + history_record = DeviceHistory.query.filter( + DeviceHistory.device_id == device.id, + DeviceHistory.data_time.like(f"{date_str}%") + ).order_by(desc(DeviceHistory.id)).first() + + if history_record: + content = history_record.json_data + + # 2. 查当前状态 (如果历史表没查到,且当前状态的时间也匹配) + elif device.latest_time and device.latest_time.startswith(date_str): + content = device.json_data + + if content: + return jsonify({ + 'code': 200, + 'name': device.name, + 'source': device.source, + 'content': content + }) + else: + return jsonify({'code': 404, 'message': 'No data for this date'}), 404 + + +# ======================= +# 2. 维修日志接口 +# ======================= + +@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]}) + + +@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)}) + + +@api_bp.route('/logs/update', methods=['POST']) +def update_log(): + data = request.get_json() + log_id = data.get('id') + + if not log_id: + return jsonify({'code': 400, 'message': 'Missing Log ID'}), 400 + + log = MaintenanceLog.query.get(log_id) + if not log: + return jsonify({'code': 404, 'message': 'Log 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)}) + + +@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 + + +# ======================= +# 3. 辅助与控制接口 (核心修改逻辑在 run_monitor) +# ======================= + def calculate_offset(latest_time_str): - """计算时间差的辅助函数""" if not latest_time_str or latest_time_str == "N/A": return "从未同步" try: + # 处理可能包含 _ 或 - 的日期 clean = str(latest_time_str).split()[0].replace('_', '-') if len(clean) < 8: return latest_time_str target = datetime.strptime(clean, "%Y-%m-%d").date() @@ -30,186 +174,110 @@ def calculate_offset(latest_time_str): return "时间解析失败" -# 👇👇👇 修复核心:补全 devices_overview 接口 👇👇👇 -@api_bp.route('/devices_overview', methods=['GET']) -def devices_overview(): - try: - # 获取所有设备 - devices = Device.query.all() - # 转换为字典列表 - data_list = [d.to_dict() for d in devices] - return jsonify({'code': 200, 'data': data_list}) - except Exception as e: - print(f"Error in devices_overview: {e}") - return jsonify({'code': 500, 'message': str(e)}) - - @api_bp.route('/run_monitor', methods=['POST']) def run_monitor(): try: if not execute_monitor_task: return jsonify({'code': 500, 'message': 'Core module missing'}) - # 1. 准备归档目录 - base_dir = os.path.abspath(os.path.dirname(os.path.dirname(__file__))) - binary_dir = os.path.join(base_dir, 'instance', 'binary') - if not os.path.exists(binary_dir): os.makedirs(binary_dir) - - # 2. 获取数据 (列表) task_result = execute_monitor_task() - if not task_result: return jsonify({'code': 200, 'message': '任务跳过(正在运行)'}) + if not task_result: return jsonify({'code': 200, 'message': '任务跳过'}) scraped_list = task_result.get('device_list', []) current_check_time = datetime.now().strftime("%Y-%m-%d %H:%M:%S") - # 3. 处理每一条数据 + count = 0 for item in scraped_list: d_name = item.get('name') - d_source = item.get('source') - d_status = item.get('status') - d_value = item.get('value') - d_time = item.get('target_time') - d_raw_json = item.get('raw_json', {}) - d_temp_file = item.get('temp_file') + if not d_name: continue - # --- 文件归档 --- - final_db_rel_path = "" - if d_temp_file and os.path.exists(d_temp_file): - # 移动文件: instance/temp -> instance/binary - fname = os.path.basename(d_temp_file) - dest = os.path.join(binary_dir, fname) + d_raw = item.get('raw_json', {}) + source = item.get('source', '') + target_time = item.get('target_time') + + # ================= [核心修改部分 START] ================= + # 针对 106 网站的数据,从 path 中解析标准时间 + # 格式示例: "path": "/Data/2026_01_08/xiwuzhu_16_29_28.csv" + # 目标格式: "2026-01-08 16:29:28" + if '106' in str(source): try: - shutil.move(d_temp_file, dest) - final_db_rel_path = f"binary/{fname}" - except Exception as e: - print(f"File move error: {e}") + path_str = d_raw.get('path', '') + # 正则匹配日期 (YYYY_MM_DD) 和 时间 (HH_MM_SS) + # 解释:/Data/(年_月_日)/任意字符_(时_分_秒).csv + match = re.search(r'/Data/(\d{4}_\d{2}_\d{2})/\w+_(\d{2}_\d{2}_\d{2})\.csv', path_str) - # --- 序列化 Raw JSON --- - json_str = json.dumps(d_raw_json, ensure_ascii=False) + if match: + date_part = match.group(1).replace('_', '-') # 2026_01_08 -> 2026-01-08 + time_part = match.group(2).replace('_', ':') # 16_29_28 -> 16:29:28 + extracted_time = f"{date_part} {time_part}" + + # 覆盖爬虫原本获取的可能不准确的时间 + target_time = extracted_time + item['target_time'] = extracted_time + except Exception as parse_err: + print(f"Error parsing 106 path: {parse_err}") + # ================= [核心修改部分 END] ================= + + json_str = json.dumps(d_raw, ensure_ascii=False) if isinstance(d_raw, (dict, list)) else str(d_raw) - # --- A. 更新 Device (快照) --- device = Device.query.filter_by(name=d_name).first() if not device: - device = Device(name=d_name, source=d_source) + device = Device(name=d_name, source=source) db.session.add(device) - db.session.flush() # 获取 ID + db.session.flush() # 获取ID - device.status = d_status - device.current_value = d_value - device.latest_time = d_time + device.status = item.get('status') + device.current_value = item.get('value') + device.latest_time = target_time # 使用解析后的时间 device.check_time = current_check_time device.json_data = json_str - device.offset = calculate_offset(d_time) + device.offset = calculate_offset(target_time) - # --- B. 插入 History (日志) --- new_history = DeviceHistory( device_id=device.id, - status=d_status, - result_data=d_value, - data_time=d_time, - json_data=json_str, - file_path=final_db_rel_path + status=item.get('status'), + result_data=item.get('value'), + data_time=target_time, # 存入标准时间,方便查询 + json_data=json_str ) db.session.add(new_history) + count += 1 db.session.commit() - return jsonify({'code': 200, 'message': f'更新 {len(scraped_list)} 台设备'}) - + return jsonify({'code': 200, 'message': f'更新 {count} 台设备'}) except Exception as e: db.session.rollback() return jsonify({'code': 500, 'message': str(e)}) -@api_bp.route('/device_history', methods=['GET']) -def get_device_history(): - try: - name = request.args.get('name') - device = Device.query.filter_by(name=name).first() - if not device: return jsonify({'error': 'Not found'}), 404 - - history = DeviceHistory.query.filter_by(device_id=device.id) \ - .order_by(DeviceHistory.recorded_at.desc()).limit(100).all() - - data = [] - for h in history: - raw = None - if h.json_data: - try: - raw = json.loads(h.json_data) - except: - raw = h.json_data - - data.append({ - 'recorded_at': h.recorded_at.strftime('%Y-%m-%d %H:%M:%S'), - 'data_time': h.data_time, - 'status': h.status, - 'value': h.result_data, - 'file_path': h.file_path, - 'raw_data': raw - }) - return jsonify({'device': device.name, 'history': data}) - except Exception as e: - return jsonify({'error': str(e)}), 500 - - -# 👇👇👇 以下是 Vue 前端用到的其他接口,之前缺失的 👇👇👇 - @api_bp.route('/update_site', methods=['POST']) def update_site(): - """更新安装地点""" data = request.get_json() - name = data.get('name') - site = data.get('site') - - device = Device.query.filter_by(name=name).first() + device = Device.query.filter_by(name=data.get('name')).first() if device: - device.install_site = site + device.install_site = data.get('site') db.session.commit() - return jsonify({'code': 200, 'message': 'Updated'}) - return jsonify({'code': 404, 'message': 'Device not found'}), 404 + return jsonify({'code': 200}) + return jsonify({'code': 404}), 404 @api_bp.route('/toggle_maintenance', methods=['POST']) def toggle_maintenance(): - """切换维修状态""" data = request.get_json() - name = data.get('name') - is_maintaining = data.get('is_maintaining') - - device = Device.query.filter_by(name=name).first() + device = Device.query.filter_by(name=data.get('name')).first() if device: - device.is_maintaining = is_maintaining + device.is_maintaining = data.get('is_maintaining') db.session.commit() - return jsonify({'code': 200, 'message': 'Updated'}) - return jsonify({'code': 404, 'message': 'Device not found'}), 404 + return jsonify({'code': 200}) + return jsonify({'code': 404}), 404 @api_bp.route('/toggle_hidden', methods=['POST']) def toggle_hidden(): - """切换隐藏/回收站状态""" data = request.get_json() - name = data.get('name') - is_hidden = data.get('is_hidden') - - device = Device.query.filter_by(name=name).first() + device = Device.query.filter_by(name=data.get('name')).first() if device: - device.is_hidden = is_hidden + device.is_hidden = data.get('is_hidden') db.session.commit() - return jsonify({'code': 200, 'message': 'Updated'}) - return jsonify({'code': 404, 'message': 'Device not found'}), 404 - - -@api_bp.route('/logs/add', methods=['POST']) -def add_log(): - """添加维修日志""" - data = request.get_json() - device_name = data.get('device_name') - content = data.get('content') - - if not device_name or not content: - return jsonify({'code': 400, 'message': 'Missing data'}), 400 - - new_log = MaintenanceLog(device_name=device_name, content=content) - db.session.add(new_log) - db.session.commit() - return jsonify({'code': 200, 'message': 'Log added'}) \ No newline at end of file + return jsonify({'code': 200}) + return jsonify({'code': 404}), 404 \ No newline at end of file diff --git a/zhandianxinxi/.idea/inspectionProfiles/Project_Default.xml b/zhandianxinxi/.idea/inspectionProfiles/Project_Default.xml new file mode 100644 index 0000000..cd83845 --- /dev/null +++ b/zhandianxinxi/.idea/inspectionProfiles/Project_Default.xml @@ -0,0 +1,6 @@ + + + + \ No newline at end of file diff --git a/zhandianxinxi/光谱数据监控/src/App.vue b/zhandianxinxi/光谱数据监控/src/App.vue index ddae293..9ba6e9f 100644 --- a/zhandianxinxi/光谱数据监控/src/App.vue +++ b/zhandianxinxi/光谱数据监控/src/App.vue @@ -1,3 +1,4 @@ + diff --git a/zhandianxinxi/光谱数据监控/src/views/Dashboard.vue b/zhandianxinxi/光谱数据监控/src/views/Dashboard.vue index 8692b16..284f6b3 100644 --- a/zhandianxinxi/光谱数据监控/src/views/Dashboard.vue +++ b/zhandianxinxi/光谱数据监控/src/views/Dashboard.vue @@ -1,7 +1,6 @@