219 lines
6.9 KiB
Python
219 lines
6.9 KiB
Python
#!/usr/bin/env python3
|
|
"""
|
|
调试实验状态问题
|
|
检查为什么实验结束后状态没有更新
|
|
"""
|
|
|
|
import sqlite3
|
|
import json
|
|
import os
|
|
from pathlib import Path
|
|
from influxdb_client import InfluxDBClient
|
|
import datetime
|
|
|
|
def check_database_status():
|
|
"""检查数据库中的实验状态"""
|
|
print("检查数据库中的实验状态")
|
|
print("=" * 40)
|
|
|
|
try:
|
|
db_path = Path(__file__).parent / "experiments.db"
|
|
db = sqlite3.connect(str(db_path))
|
|
cur = db.cursor()
|
|
|
|
# 查询最近的实验记录
|
|
cur.execute("""
|
|
SELECT id, work_order_no, start_ts, end_ts, created_at
|
|
FROM experiments
|
|
WHERE work_order_no = '112233'
|
|
ORDER BY created_at DESC
|
|
LIMIT 5
|
|
""")
|
|
|
|
results = cur.fetchall()
|
|
|
|
if results:
|
|
print("工单 112233 的实验记录:")
|
|
for row in results:
|
|
exp_id, work_order, start_ts, end_ts, created_at = row
|
|
print(f" ID: {exp_id}")
|
|
print(f" 工单号: {work_order}")
|
|
print(f" 创建时间: {created_at}")
|
|
print(f" 开始时间: {start_ts or '未开始'}")
|
|
print(f" 结束时间: {end_ts or '未结束'}")
|
|
|
|
# 判断状态
|
|
if start_ts and end_ts:
|
|
status = "已完成"
|
|
elif start_ts:
|
|
status = "进行中"
|
|
else:
|
|
status = "等待开始"
|
|
print(f" 状态: {status}")
|
|
print("-" * 30)
|
|
else:
|
|
print("没有找到工单 112233 的记录")
|
|
|
|
db.close()
|
|
|
|
except Exception as e:
|
|
print(f"ERROR 检查数据库失败: {e}")
|
|
|
|
def check_influx_data():
|
|
"""检查InfluxDB中的最新数据"""
|
|
print("\n检查InfluxDB中的最新数据")
|
|
print("=" * 40)
|
|
|
|
try:
|
|
# 加载配置
|
|
with open("default.json", 'r', encoding='utf-8') as f:
|
|
config = json.load(f)
|
|
|
|
influx_config = config.get('influx', {})
|
|
|
|
client = InfluxDBClient(
|
|
url=influx_config.get('url', 'http://127.0.0.1:8086'),
|
|
token=influx_config.get('token', ''),
|
|
org=influx_config.get('org', 'MEASCON')
|
|
)
|
|
|
|
# 查询最近的load_status数据
|
|
query = '''
|
|
from(bucket: "PCM")
|
|
|> range(start: -1h)
|
|
|> filter(fn: (r) => r["_measurement"] == "PCM_Measurement")
|
|
|> filter(fn: (r) => r["data_type"] == "Breaker")
|
|
|> filter(fn: (r) => r["_field"] == "load_status")
|
|
|> sort(columns: ["_time"], desc: true)
|
|
|> limit(n: 10)
|
|
'''
|
|
|
|
result = client.query_api().query(query)
|
|
|
|
if result:
|
|
print("最近的load_status数据:")
|
|
for table in result:
|
|
for record in table.records:
|
|
print(f" 时间: {record.get_time()}")
|
|
print(f" 值: {record.get_value()}")
|
|
print(f" 字段: {record.get_field()}")
|
|
print("-" * 20)
|
|
else:
|
|
print("没有找到load_status数据")
|
|
|
|
client.close()
|
|
|
|
except Exception as e:
|
|
print(f"ERROR 检查InfluxDB失败: {e}")
|
|
|
|
def simulate_monitor_logic():
|
|
"""模拟监控器逻辑"""
|
|
print("\n模拟监控器逻辑")
|
|
print("=" * 40)
|
|
|
|
try:
|
|
# 加载配置
|
|
with open("default.json", 'r', encoding='utf-8') as f:
|
|
config = json.load(f)
|
|
|
|
influx_config = config.get('influx', {})
|
|
|
|
client = InfluxDBClient(
|
|
url=influx_config.get('url', 'http://127.0.0.1:8086'),
|
|
token=influx_config.get('token', ''),
|
|
org=influx_config.get('org', 'MEASCON')
|
|
)
|
|
|
|
# 模拟监控器查询
|
|
query = '''
|
|
from(bucket: "PCM")
|
|
|> range(start: -1h)
|
|
|> filter(fn: (r) => r["_measurement"] == "PCM_Measurement")
|
|
|> filter(fn: (r) => r["data_type"] == "Breaker")
|
|
|> filter(fn: (r) => r["_field"] == "load_status")
|
|
|> last()
|
|
'''
|
|
|
|
result = client.query_api().query(query)
|
|
|
|
if result:
|
|
for table in result:
|
|
for record in table.records:
|
|
current_value = record.get_value()
|
|
print(f"当前状态值: {current_value}")
|
|
|
|
# 模拟状态检测逻辑
|
|
if str(current_value) == "1":
|
|
print("检测到: 实验开始状态 (load_status = 1)")
|
|
elif str(current_value) == "0":
|
|
print("检测到: 实验结束状态 (load_status = 0)")
|
|
else:
|
|
print(f"未知状态值: {current_value}")
|
|
else:
|
|
print("监控器查询无结果")
|
|
|
|
client.close()
|
|
|
|
except Exception as e:
|
|
print(f"ERROR 模拟监控器失败: {e}")
|
|
|
|
def check_monitor_process():
|
|
"""检查监控器进程是否在运行"""
|
|
print("\n检查监控器进程")
|
|
print("=" * 40)
|
|
|
|
import psutil
|
|
|
|
try:
|
|
# 查找包含main.py的Python进程
|
|
for proc in psutil.process_iter(['pid', 'name', 'cmdline']):
|
|
try:
|
|
if proc.info['name'] and 'python' in proc.info['name'].lower():
|
|
cmdline = proc.info['cmdline']
|
|
if cmdline and any('main.py' in arg for arg in cmdline):
|
|
print(f"找到主程序进程: PID {proc.info['pid']}")
|
|
print(f"命令行: {' '.join(cmdline)}")
|
|
return True
|
|
except (psutil.NoSuchProcess, psutil.AccessDenied):
|
|
continue
|
|
|
|
print("没有找到运行中的main.py进程")
|
|
return False
|
|
|
|
except Exception as e:
|
|
print(f"ERROR 检查进程失败: {e}")
|
|
return False
|
|
|
|
def main():
|
|
print("实验状态调试工具")
|
|
print("检查为什么实验结束后状态没有更新")
|
|
print("=" * 50)
|
|
|
|
# 1. 检查数据库状态
|
|
check_database_status()
|
|
|
|
# 2. 检查InfluxDB数据
|
|
check_influx_data()
|
|
|
|
# 3. 模拟监控器逻辑
|
|
simulate_monitor_logic()
|
|
|
|
# 4. 检查监控器进程
|
|
is_running = check_monitor_process()
|
|
|
|
print("\n" + "=" * 50)
|
|
print("调试总结:")
|
|
print("1. 检查数据库中实验记录的start_ts和end_ts是否已更新")
|
|
print("2. 检查InfluxDB中是否有状态变化数据")
|
|
print("3. 检查监控器逻辑是否能正确检测状态")
|
|
print(f"4. 主程序是否在运行: {'是' if is_running else '否'}")
|
|
|
|
if not is_running:
|
|
print("\n建议:")
|
|
print("1. 重启主程序: python main.py")
|
|
print("2. 创建工单并进入等待状态")
|
|
print("3. 观察监控器日志输出")
|
|
|
|
if __name__ == "__main__":
|
|
main()
|