- 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
125 lines
4.8 KiB
Python
125 lines
4.8 KiB
Python
from datetime import datetime, timedelta
|
|
from decimal import Decimal
|
|
|
|
from gateway.model import MachineState
|
|
|
|
|
|
def _machine() -> MachineState:
|
|
return MachineState(1, "1#空压机", 0)
|
|
|
|
|
|
def test_instant_ratio():
|
|
m = _machine()
|
|
t = datetime(2026, 1, 1, 10, 0, 0)
|
|
m.apply(t, instant_flow=Decimal("10"), total_flow=Decimal("1000"))
|
|
m.apply(t, instant_power=Decimal("50"), total_energy=Decimal("5000"))
|
|
|
|
assert m.ratios["instant"] == Decimal("5")
|
|
# 周期基准刚建立,小时/天/月比值应为 0
|
|
assert m.ratios["hour"] == 0
|
|
assert m.ratios["day"] == 0
|
|
assert m.ratios["month"] == 0
|
|
|
|
|
|
def test_instant_ratio_zero_flow_is_safe():
|
|
m = _machine()
|
|
t = datetime(2026, 1, 1, 10, 0, 0)
|
|
m.apply(t, instant_flow=Decimal("0"), instant_power=Decimal("50"))
|
|
assert m.ratios["instant"] == 0
|
|
|
|
|
|
def test_hour_ratio_and_hour_rollover():
|
|
m = _machine()
|
|
t0 = datetime(2026, 1, 1, 10, 0, 0)
|
|
m.apply(t0, total_flow=Decimal("1000"), total_energy=Decimal("5000"))
|
|
|
|
m.apply(t0 + timedelta(minutes=30), total_flow=Decimal("1100"), total_energy=Decimal("5500"))
|
|
assert m.ratios["hour"] == Decimal("5")
|
|
assert m.ratios["day"] == Decimal("5")
|
|
|
|
# 11:00:01 整点清零:之前累计值成为新基准
|
|
t1 = datetime(2026, 1, 1, 11, 0, 1)
|
|
m.apply(t1, total_flow=Decimal("1120"), total_energy=Decimal("5600"))
|
|
assert m.ratios["hour"] == 0
|
|
# 天累计不受整点影响:600/120 = 5
|
|
assert m.ratios["day"] == Decimal("5")
|
|
|
|
# 本小时内有新产出后才开始累计
|
|
m.apply(t1 + timedelta(minutes=10), total_flow=Decimal("1170"), total_energy=Decimal("5850"))
|
|
assert m.ratios["hour"] == Decimal("5")
|
|
|
|
|
|
def test_day_and_month_rollover():
|
|
m = _machine()
|
|
start = datetime(2026, 1, 31, 23, 50, 0)
|
|
m.apply(start, total_flow=Decimal("1000"), total_energy=Decimal("3000"))
|
|
m.apply(start + timedelta(minutes=5), total_flow=Decimal("1100"), total_energy=Decimal("3300"))
|
|
assert m.ratios["day"] == Decimal("3")
|
|
assert m.ratios["month"] == Decimal("3")
|
|
|
|
# 跨零点、跨月
|
|
m.apply(datetime(2026, 2, 1, 0, 0, 5), total_flow=Decimal("1110"), total_energy=Decimal("3330"))
|
|
assert m.ratios["day"] == 0
|
|
assert m.ratios["month"] == 0
|
|
assert m.ratios["hour"] == 0
|
|
|
|
m.apply(datetime(2026, 2, 1, 0, 0, 35), total_flow=Decimal("1130"), total_energy=Decimal("3390"))
|
|
assert m.ratios["day"] == Decimal("3")
|
|
assert m.ratios["month"] == Decimal("3")
|
|
|
|
|
|
def test_zero_consumption_in_period_is_zero():
|
|
m = _machine()
|
|
t = datetime(2026, 1, 1, 10, 0, 0)
|
|
m.apply(t, total_flow=Decimal("1000"), total_energy=Decimal("5000"))
|
|
m.apply(t + timedelta(minutes=5), total_flow=Decimal("1000"), total_energy=Decimal("5000"))
|
|
assert m.ratios["hour"] == 0
|
|
|
|
|
|
def test_restore_same_period_keeps_accumulation():
|
|
m = _machine()
|
|
t0 = datetime(2026, 1, 1, 10, 0, 0)
|
|
m.apply(t0, total_flow=Decimal("1000"), total_energy=Decimal("5000"))
|
|
m.apply(t0 + timedelta(minutes=10), total_flow=Decimal("1100"), total_energy=Decimal("5500"))
|
|
|
|
payload = m.to_payload()
|
|
restored = MachineState(1, "1#空压机", 0)
|
|
restored.restore(payload, t0 + timedelta(minutes=20))
|
|
|
|
assert restored.ratios["day"] == Decimal("5")
|
|
# 恢复后再累加,仍以原始基准计算:700/140 = 5
|
|
restored.apply(t0 + timedelta(minutes=30), total_flow=Decimal("1140"), total_energy=Decimal("5700"))
|
|
assert restored.ratios["day"] == Decimal("5")
|
|
|
|
|
|
def test_restore_after_period_change_uses_last_totals():
|
|
m = _machine()
|
|
t0 = datetime(2026, 1, 1, 10, 0, 0)
|
|
m.apply(t0, total_flow=Decimal("1000"), total_energy=Decimal("5000"))
|
|
m.apply(t0 + timedelta(minutes=10), total_flow=Decimal("1100"), total_energy=Decimal("5500"))
|
|
payload = m.to_payload()
|
|
|
|
restored = MachineState(1, "1#空压机", 0)
|
|
restored.restore(payload, datetime(2026, 1, 2, 8, 0, 0))
|
|
assert restored.ratios["day"] == 0
|
|
# 仍在同一个月内,月累计保持连续
|
|
assert restored.ratios["month"] == Decimal("5")
|
|
|
|
# 恢复后的新数据以“最后一次累计值”为起点
|
|
restored.apply(datetime(2026, 1, 2, 8, 1, 0), total_flow=Decimal("1110"), total_energy=Decimal("5530"))
|
|
assert restored.ratios["day"] == Decimal("3")
|
|
|
|
|
|
def test_restore_payload_roundtrip_of_ratios():
|
|
m = _machine()
|
|
t0 = datetime(2026, 1, 1, 10, 0, 0)
|
|
m.apply(t0, instant_flow=Decimal("10"), total_flow=Decimal("1000"))
|
|
m.apply(t0, instant_power=Decimal("25"), total_energy=Decimal("5000"))
|
|
m.apply(t0 + timedelta(minutes=1), total_flow=Decimal("1010"), total_energy=Decimal("5050"))
|
|
|
|
payload = m.to_payload()
|
|
restored = MachineState(1, "1#空压机", 0)
|
|
restored.restore(payload, t0 + timedelta(minutes=2))
|
|
for key in ("instant", "hour", "day", "month"):
|
|
assert restored.ratios[key] == m.ratios[key]
|