现场调试版同步: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,77 @@
|
||||
"""SQLite 掉电保持:保存/恢复各台机器的周期基准与最新累计值。"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import json
|
||||
import logging
|
||||
import sqlite3
|
||||
from datetime import datetime
|
||||
from pathlib import Path
|
||||
from typing import Any
|
||||
|
||||
log = logging.getLogger(__name__)
|
||||
|
||||
_SCHEMA = """
|
||||
CREATE TABLE IF NOT EXISTS machine_state (
|
||||
machine_id INTEGER PRIMARY KEY,
|
||||
payload TEXT NOT NULL,
|
||||
updated_at TEXT NOT NULL
|
||||
);
|
||||
CREATE TABLE IF NOT EXISTS meta (
|
||||
key TEXT PRIMARY KEY,
|
||||
value TEXT NOT NULL
|
||||
);
|
||||
"""
|
||||
|
||||
|
||||
class SqliteStore:
|
||||
def __init__(self, path: str) -> None:
|
||||
self._path = Path(path)
|
||||
if str(self._path.parent) not in ("", "."):
|
||||
self._path.parent.mkdir(parents=True, exist_ok=True)
|
||||
with self._connect() as conn:
|
||||
conn.executescript(_SCHEMA)
|
||||
|
||||
def _connect(self) -> sqlite3.Connection:
|
||||
return sqlite3.connect(self._path, timeout=10)
|
||||
|
||||
def save(self, payloads: dict[int, dict[str, Any]], now: datetime) -> None:
|
||||
if not payloads:
|
||||
return
|
||||
stamp = now.isoformat(timespec="seconds")
|
||||
rows = [
|
||||
(machine_id, json.dumps(payload, ensure_ascii=False), stamp)
|
||||
for machine_id, payload in payloads.items()
|
||||
]
|
||||
with self._connect() as conn:
|
||||
conn.executemany(
|
||||
"INSERT OR REPLACE INTO machine_state (machine_id, payload, updated_at) VALUES (?, ?, ?)",
|
||||
rows,
|
||||
)
|
||||
conn.execute(
|
||||
"INSERT OR REPLACE INTO meta (key, value) VALUES ('last_save', ?)",
|
||||
(stamp,),
|
||||
)
|
||||
|
||||
def load(self) -> dict[int, dict[str, Any]]:
|
||||
try:
|
||||
with self._connect() as conn:
|
||||
rows = conn.execute("SELECT machine_id, payload FROM machine_state").fetchall()
|
||||
except sqlite3.Error:
|
||||
log.exception("读取 SQLite 状态失败,将按冷启动处理")
|
||||
return {}
|
||||
result: dict[int, dict[str, Any]] = {}
|
||||
for machine_id, payload in rows:
|
||||
try:
|
||||
result[int(machine_id)] = json.loads(payload)
|
||||
except (TypeError, ValueError):
|
||||
log.warning("机器 %s 的历史状态无法解析,已忽略", machine_id)
|
||||
return result
|
||||
|
||||
def last_save_time(self) -> str | None:
|
||||
try:
|
||||
with self._connect() as conn:
|
||||
row = conn.execute("SELECT value FROM meta WHERE key='last_save'").fetchone()
|
||||
except sqlite3.Error:
|
||||
return None
|
||||
return row[0] if row else None
|
||||
Reference in new issue
Block a user