Files
Specific-Power/tests/test_store.py
T
yangpengyu 9eaf87ab7d 现场调试版同步:S331 点表修正(CDAB/scale 60/int32) + SafeRTUFramer 多点485 + 哨兵过滤
- config.yaml: 恢复现场 IP(10.50.253.165/.181/.182)、s331_2 端口 502
- config.yaml: S331 全部 32 位量 word_order 改 CDAB,瞬时流量 scale=60 (m3/min -> m3/h)
- config.yaml: 11# 电表功率/电能类型改 int32;第二路 RTU 从站 slave_id 改 10
- poller.py: 过滤 S331 无数据哨兵 -9999 与非有限值,避免污染周期基准
- rtu.py: 新增 SafeRTUFramer 支持多点半总线,忽略其它从站请求
- 新增 config.debug.yaml、README.md、wheelhouse 离线依赖
- tests: 适配新配置(ttyS7/address_offset);新增 pytest.ini 限定 testpaths
2026-11-19 10:59:27 +08:00

114 lines
3.3 KiB
Python

from datetime import datetime
from decimal import Decimal
from gateway.config import CompressorCfg, Point
from gateway.registers import decode_point
from gateway.store import StateStore
def _cfg(machine_id: int, register_index: int) -> CompressorCfg:
point = Point(address=0)
return CompressorCfg(
id=machine_id,
name=f"{machine_id}#空压机",
s331="s331_1",
meter="meter_410s",
register_index=register_index,
flow_instant=point,
flow_total=point,
power_instant=point,
energy_total=point,
)
def _ratio_words(store: StateStore, base: int) -> list[Decimal]:
point = Point(address=0, type="float32")
words = store.read_registers(base, 8)
assert words is not None
return [decode_point(words[i : i + 2], point) for i in range(0, 8, 2)]
def test_register_image_layout():
store = StateStore([_cfg(1, 0), _cfg(2, 1)])
assert store.register_count == 16
now = datetime(2026, 1, 1, 10, 0, 0)
store.update(
{2: {"instant_flow": Decimal("10"), "instant_power": Decimal("30")}},
now=now,
)
machine1 = _ratio_words(store, 0)
machine2 = _ratio_words(store, 8)
assert machine1 == [Decimal(0)] * 4
assert machine2[0] == Decimal("3")
def test_read_out_of_range_returns_none():
store = StateStore([_cfg(1, 0)])
assert store.read_registers(0, 8) is not None
assert store.read_registers(4, 8) is None
assert store.read_registers(-1, 2) is None
def test_concurrent_updates_never_tear_registers():
"""写线程成组更新 4 个比值,读线程必须始终读到同一代数据。"""
import threading
store = StateStore([_cfg(1, 0)])
now = datetime(2026, 1, 1, 10, 0, 0)
# 建立 (0, 0) 基准,使四种比值完全由后续累计值决定
store.update({1: {"total_flow": Decimal(0), "total_energy": Decimal(0)}}, now=now)
stop = threading.Event()
read_count = 0
bad_reads: list[list[Decimal]] = []
def writer() -> None:
for index in range(1, 4001):
factor = 3 if index % 2 == 0 else 6
store.update(
{
1: {
"instant_flow": Decimal(10),
"instant_power": Decimal(10 * factor),
"total_flow": Decimal(100),
"total_energy": Decimal(100 * factor),
}
},
now=now,
)
stop.set()
thread = threading.Thread(target=writer, daemon=True)
thread.start()
while not stop.is_set():
values = _ratio_words(store, 0)
read_count += 1
if len(set(values)) != 1:
bad_reads.append(values)
thread.join()
assert read_count > 100
assert bad_reads == []
def test_persist_and_restore_roundtrip():
store = StateStore([_cfg(1, 0)])
now = datetime(2026, 1, 1, 10, 0, 0)
store.update(
{
1: {
"instant_flow": Decimal("10"),
"instant_power": Decimal("30"),
"total_flow": Decimal("1000"),
"total_energy": Decimal("3000"),
}
},
now=now,
)
payload = store.persist_payloads()
fresh = StateStore([_cfg(1, 0)])
assert fresh.restore(payload, now=now) == 1
assert _ratio_words(fresh, 0)[0] == Decimal("3")