179 lines
5.4 KiB
Python
179 lines
5.4 KiB
Python
|
|
#!/usr/bin/env python3
|
||
|
|
"""
|
||
|
|
测试整数值的写入和查询
|
||
|
|
"""
|
||
|
|
|
||
|
|
import json
|
||
|
|
import os
|
||
|
|
from influxdb_client import InfluxDBClient, Point
|
||
|
|
from influxdb_client.client.write_api import SYNCHRONOUS
|
||
|
|
import datetime
|
||
|
|
|
||
|
|
def load_config():
|
||
|
|
"""从default.json加载配置"""
|
||
|
|
config_path = os.path.join(os.path.dirname(__file__), "default.json")
|
||
|
|
try:
|
||
|
|
with open(config_path, 'r', encoding='utf-8') as f:
|
||
|
|
config = json.load(f)
|
||
|
|
return config.get('influx', {})
|
||
|
|
except Exception as e:
|
||
|
|
print(f"加载配置文件失败: {e}")
|
||
|
|
return {}
|
||
|
|
|
||
|
|
def test_integer_write():
|
||
|
|
"""测试写入整数值"""
|
||
|
|
print("测试写入整数值")
|
||
|
|
print("=" * 40)
|
||
|
|
|
||
|
|
influx_config = load_config()
|
||
|
|
|
||
|
|
try:
|
||
|
|
client = InfluxDBClient(
|
||
|
|
url=influx_config.get('url', 'http://127.0.0.1:8086'),
|
||
|
|
token=influx_config.get('token', ''),
|
||
|
|
org=influx_config.get('org', 'MEASCON')
|
||
|
|
)
|
||
|
|
write_api = client.write_api(write_options=SYNCHRONOUS)
|
||
|
|
|
||
|
|
measurement = influx_config.get('measurement', 'PCM_Measurement')
|
||
|
|
bucket = influx_config.get('bucket', 'PCM')
|
||
|
|
|
||
|
|
now = datetime.datetime.now(datetime.timezone.utc)
|
||
|
|
|
||
|
|
# 写入整数值 1
|
||
|
|
point1 = Point(measurement) \
|
||
|
|
.tag("data_type", "Breaker") \
|
||
|
|
.field("load_status", 1) \
|
||
|
|
.time(now)
|
||
|
|
|
||
|
|
write_api.write(bucket=bucket, record=point1)
|
||
|
|
print("OK 写入整数值 1")
|
||
|
|
|
||
|
|
# 等待一秒
|
||
|
|
import time
|
||
|
|
time.sleep(1)
|
||
|
|
|
||
|
|
# 写入整数值 0
|
||
|
|
point2 = Point(measurement) \
|
||
|
|
.tag("data_type", "Breaker") \
|
||
|
|
.field("load_status", 0) \
|
||
|
|
.time(datetime.datetime.now(datetime.timezone.utc))
|
||
|
|
|
||
|
|
write_api.write(bucket=bucket, record=point2)
|
||
|
|
print("OK 写入整数值 0")
|
||
|
|
|
||
|
|
client.close()
|
||
|
|
|
||
|
|
except Exception as e:
|
||
|
|
print(f"ERROR 写入失败: {e}")
|
||
|
|
|
||
|
|
def test_integer_query():
|
||
|
|
"""测试查询整数值"""
|
||
|
|
print("\n测试查询整数值")
|
||
|
|
print("=" * 40)
|
||
|
|
|
||
|
|
influx_config = load_config()
|
||
|
|
|
||
|
|
try:
|
||
|
|
client = InfluxDBClient(
|
||
|
|
url=influx_config.get('url', 'http://127.0.0.1:8086'),
|
||
|
|
token=influx_config.get('token', ''),
|
||
|
|
org=influx_config.get('org', 'MEASCON')
|
||
|
|
)
|
||
|
|
|
||
|
|
measurement = influx_config.get('measurement', 'PCM_Measurement')
|
||
|
|
bucket = influx_config.get('bucket', 'PCM')
|
||
|
|
|
||
|
|
# 查询最近的数据
|
||
|
|
query = f'''
|
||
|
|
from(bucket: "{bucket}")
|
||
|
|
|> range(start: -10m)
|
||
|
|
|> filter(fn: (r) => r["_measurement"] == "{measurement}")
|
||
|
|
|> filter(fn: (r) => r["data_type"] == "Breaker")
|
||
|
|
|> filter(fn: (r) => r["_field"] == "load_status")
|
||
|
|
|> sort(columns: ["_time"], desc: true)
|
||
|
|
|> limit(n: 5)
|
||
|
|
'''
|
||
|
|
|
||
|
|
result = client.query_api().query(query)
|
||
|
|
|
||
|
|
if result:
|
||
|
|
print("最近的load_status数据:")
|
||
|
|
for table in result:
|
||
|
|
for record in table.records:
|
||
|
|
value = record.get_value()
|
||
|
|
value_type = type(value).__name__
|
||
|
|
print(f" 时间: {record.get_time()}")
|
||
|
|
print(f" 值: {value} (类型: {value_type})")
|
||
|
|
|
||
|
|
# 检查值类型
|
||
|
|
if isinstance(value, int):
|
||
|
|
print(" ✅ 值是整数类型")
|
||
|
|
elif isinstance(value, str):
|
||
|
|
print(" ❌ 值是字符串类型")
|
||
|
|
else:
|
||
|
|
print(f" ⚠️ 值是其他类型: {value_type}")
|
||
|
|
print("-" * 20)
|
||
|
|
else:
|
||
|
|
print("没有查询到数据")
|
||
|
|
|
||
|
|
client.close()
|
||
|
|
|
||
|
|
except Exception as e:
|
||
|
|
print(f"ERROR 查询失败: {e}")
|
||
|
|
|
||
|
|
def test_monitor_logic():
|
||
|
|
"""测试监控器逻辑(整数比较)"""
|
||
|
|
print("\n测试监控器逻辑")
|
||
|
|
print("=" * 40)
|
||
|
|
|
||
|
|
# 模拟监控器的状态比较逻辑
|
||
|
|
test_values = [1, 0, "1", "0", 1.0, 0.0]
|
||
|
|
|
||
|
|
for value in test_values:
|
||
|
|
print(f"测试值: {value} (类型: {type(value).__name__})")
|
||
|
|
|
||
|
|
# 监控器配置的整数值
|
||
|
|
start_value = 1
|
||
|
|
end_value = 0
|
||
|
|
|
||
|
|
# 比较逻辑
|
||
|
|
if value == start_value:
|
||
|
|
print(" ✅ 匹配开始值 (实验开始)")
|
||
|
|
elif value == end_value:
|
||
|
|
print(" ✅ 匹配结束值 (实验结束)")
|
||
|
|
else:
|
||
|
|
print(" ❌ 不匹配任何状态值")
|
||
|
|
|
||
|
|
print("-" * 20)
|
||
|
|
|
||
|
|
def main():
|
||
|
|
print("整数值测试")
|
||
|
|
print("=" * 50)
|
||
|
|
print("目标: 确保load_status字段使用整数值 1 和 0")
|
||
|
|
print("=" * 50)
|
||
|
|
|
||
|
|
# 1. 测试写入整数值
|
||
|
|
test_integer_write()
|
||
|
|
|
||
|
|
# 2. 测试查询整数值
|
||
|
|
test_integer_query()
|
||
|
|
|
||
|
|
# 3. 测试监控器逻辑
|
||
|
|
test_monitor_logic()
|
||
|
|
|
||
|
|
print("\n" + "=" * 50)
|
||
|
|
print("修改总结:")
|
||
|
|
print("1. ui_main.py: status_values 使用整数 1 和 0")
|
||
|
|
print("2. quick_test_data.py: field() 使用整数值")
|
||
|
|
print("3. 监控器将正确比较整数值")
|
||
|
|
|
||
|
|
print("\n现在可以测试:")
|
||
|
|
print("1. 在UI中创建工单并进入等待状态")
|
||
|
|
print("2. 执行: echo 3 | python quick_test_data.py")
|
||
|
|
print("3. 执行: echo 2 | python quick_test_data.py")
|
||
|
|
print("4. 观察监控器是否检测到整数值变化")
|
||
|
|
|
||
|
|
if __name__ == "__main__":
|
||
|
|
main()
|