45 lines
1.5 KiB
Python
45 lines
1.5 KiB
Python
# -*- coding: utf-8 -*-
|
||||
|
|
"""能源表驱动 —— 电表/水表/气表等能源计量设备参数化适配。
|
|||
|
|
|
|||
|
|
能源表多支持 Modbus(DL/T 645 网关转 Modbus)或 DL/T 645 串口规约;
|
|||
|
|
模板化封装与称重驱动一致:默认走 Modbus 参数化实现,
|
|||
|
|
`mode: dlt645` 时需现场规约文档定制子类。
|
|||
|
|
"""
|
|||
|
|
from __future__ import annotations
|
|||
|
|
|
|||
|
|
from typing import Dict, List, Optional
|
|||
|
|
|
|||
|
|
from point_dict.loader import Point
|
|||
|
|
from .base import Driver, SampleValue
|
|||
|
|
from .modbus_driver import ModbusDriver
|
|||
|
|
|
|||
|
|
|
|||
|
|
class EnergyDriver(Driver):
|
|||
|
|
"""能源表只读采集驱动(参数化适配)。"""
|
|||
|
|
|
|||
|
|
protocol = "energy"
|
|||
|
|
|
|||
|
|
def __init__(self, config: Optional[dict] = None):
|
|||
|
|
super().__init__(config)
|
|||
|
|
self.mode: str = self.config.get("mode", "modbus")
|
|||
|
|
self._delegate: Optional[Driver] = None
|
|||
|
|
|
|||
|
|
def connect(self) -> None:
|
|||
|
|
if self.mode == "modbus":
|
|||
|
|
self._delegate = ModbusDriver(self.config)
|
|||
|
|
else:
|
|||
|
|
raise ConnectionError(
|
|||
|
|
"能源驱动非 modbus 模式(如 DL/T 645)需要现场规约文档支持,请实现子类"
|
|||
|
|
)
|
|||
|
|
self._delegate.connect()
|
|||
|
|
|
|||
|
|
def read_points(self, points: List[Point]) -> Dict[str, SampleValue]:
|
|||
|
|
if self._delegate is None:
|
|||
|
|
raise ConnectionError("能源驱动未连接,请先 connect()")
|
|||
|
|
return self._delegate.read_points(points)
|
|||
|
|
|
|||
|
|
def close(self) -> None:
|
|||
|
|
if self._delegate is not None:
|
|||
|
|
self._delegate.close()
|
|||
|
|
self._delegate = None
|