131 lines
3.6 KiB
Python
131 lines
3.6 KiB
Python
|
|
#!/usr/bin/env python
|
||
|
|
# -*- coding: utf-8 -*-
|
||
|
|
"""
|
||
|
|
测试 SQL Server 写入功能
|
||
|
|
"""
|
||
|
|
from sqlserver_writer import SQLServerWriter
|
||
|
|
from datetime import datetime
|
||
|
|
|
||
|
|
def test_connection():
|
||
|
|
"""测试数据库连接"""
|
||
|
|
print("=" * 60)
|
||
|
|
print("测试 SQL Server 连接")
|
||
|
|
print("=" * 60)
|
||
|
|
|
||
|
|
# 从 work_order_db_config.json 加载配置
|
||
|
|
import json
|
||
|
|
from pathlib import Path
|
||
|
|
|
||
|
|
config_file = Path(__file__).parent / 'work_order_db_config.json'
|
||
|
|
|
||
|
|
if not config_file.exists():
|
||
|
|
print(f"❌ 配置文件不存在: {config_file}")
|
||
|
|
print("请先创建 work_order_db_config.json 文件")
|
||
|
|
return False
|
||
|
|
|
||
|
|
with open(config_file, 'r', encoding='utf-8') as f:
|
||
|
|
db_config = json.load(f)
|
||
|
|
|
||
|
|
config = {
|
||
|
|
'host': db_config.get('host', 'localhost'),
|
||
|
|
'port': db_config.get('port', 1433),
|
||
|
|
'database': db_config.get('database', ''),
|
||
|
|
'username': db_config.get('username', ''),
|
||
|
|
'password': db_config.get('password', ''),
|
||
|
|
}
|
||
|
|
|
||
|
|
print(f"使用配置: host={config['host']}, port={config['port']}, database={config['database']}")
|
||
|
|
|
||
|
|
writer = SQLServerWriter(config)
|
||
|
|
|
||
|
|
if writer.connect():
|
||
|
|
print("✅ 连接成功")
|
||
|
|
writer.disconnect()
|
||
|
|
return True
|
||
|
|
else:
|
||
|
|
print("❌ 连接失败")
|
||
|
|
return False
|
||
|
|
|
||
|
|
def test_write_data():
|
||
|
|
"""测试数据写入"""
|
||
|
|
print("\n" + "=" * 60)
|
||
|
|
print("测试数据写入")
|
||
|
|
print("=" * 60)
|
||
|
|
|
||
|
|
# 从 work_order_db_config.json 加载配置
|
||
|
|
import json
|
||
|
|
from pathlib import Path
|
||
|
|
|
||
|
|
config_file = Path(__file__).parent / 'work_order_db_config.json'
|
||
|
|
|
||
|
|
if not config_file.exists():
|
||
|
|
print(f"❌ 配置文件不存在: {config_file}")
|
||
|
|
return False
|
||
|
|
|
||
|
|
with open(config_file, 'r', encoding='utf-8') as f:
|
||
|
|
db_config = json.load(f)
|
||
|
|
|
||
|
|
config = {
|
||
|
|
'host': db_config.get('host', 'localhost'),
|
||
|
|
'port': db_config.get('port', 1433),
|
||
|
|
'database': db_config.get('database', ''),
|
||
|
|
'username': db_config.get('username', ''),
|
||
|
|
'password': db_config.get('password', ''),
|
||
|
|
}
|
||
|
|
|
||
|
|
# 准备测试数据
|
||
|
|
test_data = {
|
||
|
|
'order_no': 'TEST_' + datetime.now().strftime('%Y%m%d_%H%M%S'),
|
||
|
|
'runin_date': datetime.now().date(),
|
||
|
|
'operator_name': '测试员',
|
||
|
|
'power_end_part_no': 'TEST-001',
|
||
|
|
'motor_speed_rpm': 980,
|
||
|
|
'ambient_temp_c': 25.5,
|
||
|
|
'start_time': datetime.now(),
|
||
|
|
'end_time': datetime.now(),
|
||
|
|
'reviewer_name': '审核员',
|
||
|
|
'is_normal': 1,
|
||
|
|
'abnormal_desc': None,
|
||
|
|
'remarks': '测试数据',
|
||
|
|
# 温度数据示例
|
||
|
|
'temp_main_1_t05': 45.2,
|
||
|
|
'temp_main_1_t10': 48.5,
|
||
|
|
'temp_main_1_t15': 51.3,
|
||
|
|
'temp_main_1_t20': 53.8,
|
||
|
|
'temp_main_1_t25': 55.2,
|
||
|
|
'temp_main_1_t30': 56.1,
|
||
|
|
'temp_main_1_t35': 56.8,
|
||
|
|
}
|
||
|
|
|
||
|
|
writer = SQLServerWriter(config)
|
||
|
|
|
||
|
|
if not writer.connect():
|
||
|
|
print("❌ 无法连接到数据库")
|
||
|
|
return False
|
||
|
|
|
||
|
|
try:
|
||
|
|
success = writer.write_pump_600_data(test_data)
|
||
|
|
|
||
|
|
if success:
|
||
|
|
print(f"✅ 数据写入成功")
|
||
|
|
print(f" 工单号: {test_data['order_no']}")
|
||
|
|
return True
|
||
|
|
else:
|
||
|
|
print("❌ 数据写入失败")
|
||
|
|
return False
|
||
|
|
finally:
|
||
|
|
writer.disconnect()
|
||
|
|
|
||
|
|
if __name__ == "__main__":
|
||
|
|
print("SQL Server 写入功能测试")
|
||
|
|
print()
|
||
|
|
|
||
|
|
# 测试连接
|
||
|
|
if test_connection():
|
||
|
|
# 测试写入
|
||
|
|
test_write_data()
|
||
|
|
|
||
|
|
print("\n" + "=" * 60)
|
||
|
|
print("测试完成")
|
||
|
|
print("=" * 60)
|