现场调试版同步: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
This commit is contained in:
commit
9eaf87ab7d
43 files changed
+4514
No files matched your search
@@ -0,0 +1,187 @@
|
||||
"""单台空压机的数据对象与气电比计算。
|
||||
|
||||
计算口径:
|
||||
瞬时气电比 = 瞬时功率 / 瞬时流量
|
||||
小时气电比 = (本小时末累计电量 - 本小时起点累计电量) / (本小时末累计气量 - 本小时起点累计气量)
|
||||
天气电比 / 月气电比 同理,仅在整点 / 零点 / 月初切换基准。
|
||||
|
||||
全部使用 Decimal 计算,避免 float 累积误差;周期切换通过“基准值复位”实现。
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from datetime import datetime
|
||||
from decimal import Decimal
|
||||
from typing import Any, Optional
|
||||
|
||||
PERIODS = ("hour", "day", "month")
|
||||
RATIO_KEYS = ("instant", "hour", "day", "month")
|
||||
ZERO = Decimal(0)
|
||||
_UNSET = object()
|
||||
|
||||
|
||||
def period_keys(now: datetime) -> dict[str, str]:
|
||||
"""返回当前时刻所属的小时/天/月标识,用于判断是否跨周期。"""
|
||||
return {
|
||||
"hour": now.strftime("%Y%m%d%H"),
|
||||
"day": now.strftime("%Y%m%d"),
|
||||
"month": now.strftime("%Y%m"),
|
||||
}
|
||||
|
||||
|
||||
def _ratio(numerator: Optional[Decimal], denominator: Optional[Decimal]) -> Decimal:
|
||||
if numerator is None or denominator is None:
|
||||
return ZERO
|
||||
if denominator <= 0 or numerator < 0:
|
||||
return ZERO
|
||||
return numerator / denominator
|
||||
|
||||
|
||||
def _dec(value: Any) -> Optional[Decimal]:
|
||||
if value is None:
|
||||
return None
|
||||
return value if isinstance(value, Decimal) else Decimal(str(value))
|
||||
|
||||
|
||||
def _s(value: Optional[Decimal]) -> Optional[str]:
|
||||
return None if value is None else str(value)
|
||||
|
||||
|
||||
class MachineState:
|
||||
"""一台空压机的实时值、周期基准与四种气电比。"""
|
||||
|
||||
def __init__(self, machine_id: int, name: str, register_index: int) -> None:
|
||||
self.machine_id = machine_id
|
||||
self.name = name
|
||||
self.register_index = register_index
|
||||
|
||||
self.instant_flow: Optional[Decimal] = None
|
||||
self.total_flow: Optional[Decimal] = None
|
||||
self.instant_power: Optional[Decimal] = None
|
||||
self.total_energy: Optional[Decimal] = None
|
||||
|
||||
self.ratios: dict[str, Decimal] = {key: ZERO for key in RATIO_KEYS}
|
||||
self.flow_time: Optional[datetime] = None
|
||||
self.power_time: Optional[datetime] = None
|
||||
self.ratios_time: Optional[datetime] = None
|
||||
|
||||
self._keys: dict[str, str] = {p: "" for p in PERIODS}
|
||||
# 周期基准:(累计气量, 累计电量)
|
||||
self._bases: dict[str, Optional[tuple[Decimal, Decimal]]] = {p: None for p in PERIODS}
|
||||
|
||||
# ------------------------------------------------------------------ 更新
|
||||
def apply(
|
||||
self,
|
||||
now: datetime,
|
||||
*,
|
||||
instant_flow: Any = _UNSET,
|
||||
total_flow: Any = _UNSET,
|
||||
instant_power: Any = _UNSET,
|
||||
total_energy: Any = _UNSET,
|
||||
) -> None:
|
||||
"""写入新采样值(缺省字段表示本次未更新),随后重算比值。"""
|
||||
if instant_flow is not _UNSET:
|
||||
self.instant_flow = _dec(instant_flow)
|
||||
self.flow_time = now
|
||||
if total_flow is not _UNSET:
|
||||
self.total_flow = _dec(total_flow)
|
||||
self.flow_time = now
|
||||
if instant_power is not _UNSET:
|
||||
self.instant_power = _dec(instant_power)
|
||||
self.power_time = now
|
||||
if total_energy is not _UNSET:
|
||||
self.total_energy = _dec(total_energy)
|
||||
self.power_time = now
|
||||
self._refresh(now)
|
||||
|
||||
def _refresh(self, now: datetime) -> None:
|
||||
keys = period_keys(now)
|
||||
totals_ready = self.total_flow is not None and self.total_energy is not None
|
||||
|
||||
for period in PERIODS:
|
||||
if self._keys[period] != keys[period]:
|
||||
# 跨周期:基准清零,使用新周期内的第一次累计值作为起点
|
||||
self._keys[period] = keys[period]
|
||||
self._bases[period] = None
|
||||
if self._bases[period] is None and totals_ready:
|
||||
self._bases[period] = (self.total_flow, self.total_energy)
|
||||
|
||||
self.ratios["instant"] = _ratio(self.instant_power, self.instant_flow)
|
||||
for period in PERIODS:
|
||||
base = self._bases[period]
|
||||
if base is None or self.total_flow is None or self.total_energy is None:
|
||||
self.ratios[period] = ZERO
|
||||
continue
|
||||
delta_flow = self.total_flow - base[0]
|
||||
delta_energy = self.total_energy - base[1]
|
||||
self.ratios[period] = _ratio(delta_energy, delta_flow)
|
||||
self.ratios_time = now
|
||||
|
||||
# ------------------------------------------------------------------ 持久化
|
||||
def to_payload(self) -> dict[str, Any]:
|
||||
return {
|
||||
"name": self.name,
|
||||
"register_index": self.register_index,
|
||||
"instant_flow": _s(self.instant_flow),
|
||||
"total_flow": _s(self.total_flow),
|
||||
"instant_power": _s(self.instant_power),
|
||||
"total_energy": _s(self.total_energy),
|
||||
"ratios": {key: str(value) for key, value in self.ratios.items()},
|
||||
"period_keys": dict(self._keys),
|
||||
"bases": {
|
||||
period: (
|
||||
[_s(base[0]), _s(base[1])] if base is not None else None
|
||||
)
|
||||
for period, base in self._bases.items()
|
||||
},
|
||||
"flow_time": self.flow_time.isoformat() if self.flow_time else None,
|
||||
"power_time": self.power_time.isoformat() if self.power_time else None,
|
||||
}
|
||||
|
||||
def restore(self, payload: dict[str, Any], now: datetime) -> None:
|
||||
"""从 SQLite 恢复。
|
||||
|
||||
周期未变化时沿用原基准,做到重启后累计值连续;
|
||||
周期已经跨越(例如停机跨天)时,用最后一次累计值作为新基准。
|
||||
"""
|
||||
self.instant_flow = _dec(payload.get("instant_flow"))
|
||||
self.total_flow = _dec(payload.get("total_flow"))
|
||||
self.instant_power = _dec(payload.get("instant_power"))
|
||||
self.total_energy = _dec(payload.get("total_energy"))
|
||||
|
||||
self.flow_time = _parse_dt(payload.get("flow_time"))
|
||||
self.power_time = _parse_dt(payload.get("power_time"))
|
||||
|
||||
keys = period_keys(now)
|
||||
saved_keys = payload.get("period_keys") or {}
|
||||
saved_bases = payload.get("bases") or {}
|
||||
both_totals = self.total_flow is not None and self.total_energy is not None
|
||||
|
||||
for period in PERIODS:
|
||||
base = saved_bases.get(period)
|
||||
same_period = saved_keys.get(period) == keys[period]
|
||||
if same_period and base:
|
||||
self._bases[period] = (Decimal(str(base[0])), Decimal(str(base[1])))
|
||||
elif both_totals:
|
||||
self._bases[period] = (self.total_flow, self.total_energy)
|
||||
else:
|
||||
self._bases[period] = None
|
||||
self._keys[period] = keys[period]
|
||||
|
||||
self._refresh(now)
|
||||
|
||||
def age_s(self, now: datetime) -> Optional[float]:
|
||||
"""距离最近一次气量/电表更新的秒数(取较旧者)。"""
|
||||
times = [t for t in (self.flow_time, self.power_time) if t is not None]
|
||||
if not times:
|
||||
return None
|
||||
return (now - min(times)).total_seconds()
|
||||
|
||||
|
||||
def _parse_dt(value: Any) -> Optional[datetime]:
|
||||
if not value:
|
||||
return None
|
||||
try:
|
||||
return datetime.fromisoformat(str(value))
|
||||
except ValueError:
|
||||
return None
|
||||
Reference in new issue
Block a user