96 lines
4.2 KiB
Python
96 lines
4.2 KiB
Python
"""Optional c104/lib60870-C master checks against the real scan fixture.
|
|
|
|
Usage: python periodic_scan_interop_test.py server.exe lib60870_client.exe
|
|
The c104 package is a test-only dependency; no production source imports it.
|
|
|
|
可选互操作编排:分别使用c104与原生lib60870-C主站验证Type20/21、COT1/2和SQ=0/1。
|
|
外部主站独立解析本库子站的报文;普通C专项和Node回环不依赖这两个客户端。
|
|
"""
|
|
import importlib.metadata
|
|
import pathlib
|
|
import subprocess
|
|
import sys
|
|
import time
|
|
|
|
import c104
|
|
|
|
|
|
def c104_client(port: int, sequence: bool) -> None:
|
|
errors = []
|
|
seen = set()
|
|
client = c104.Client(tick_rate_ms=50)
|
|
connection = client.add_connection(ip="127.0.0.1", port=port, init=c104.Init.NONE)
|
|
station = connection.add_station(common_address=1)
|
|
|
|
def receive(point: c104.Point, previous_info: c104.Information,
|
|
message: c104.IncomingMessage) -> c104.ResponseState:
|
|
try:
|
|
typ = int(point.type)
|
|
cot = int(message.cot)
|
|
assert message.common_address == 1 and message.originator_address == 0
|
|
assert message.is_sequence == sequence
|
|
assert message.number_of_objects == 2
|
|
assert not message.is_test and not message.is_negative
|
|
assert (typ == 20 and cot == 2) or (typ == 21 and cot in (1, 2))
|
|
index = point.io_address - (100 if typ == 20 else 200)
|
|
assert index in (0, 1)
|
|
if typ == 20:
|
|
assert isinstance(point.info, c104.StatusAndChanged)
|
|
assert point.info.status.value == 0x1234 + index, repr(point.info)
|
|
assert point.info.changed.value == 0x40 + index, repr(point.info)
|
|
assert point.quality == c104.Quality.Invalid
|
|
else:
|
|
assert abs(float(point.value) - (0.5 if index == 0 else -0.25)) < 0.0001
|
|
assert point.quality.is_good
|
|
seen.add((typ, cot, point.io_address))
|
|
except Exception as exc:
|
|
errors.append(f"{type(exc).__name__}: {exc}; type={point.type}, info={point.info}")
|
|
return c104.ResponseState.SUCCESS
|
|
|
|
for typ, addresses in ((c104.Type.M_PS_NA_1, (100, 101)), (c104.Type.M_ME_ND_1, (200, 201))):
|
|
for ioa in addresses:
|
|
station.add_point(io_address=ioa, type=typ).on_receive(callable=receive)
|
|
client.start()
|
|
try:
|
|
end = time.monotonic() + 5
|
|
while len(seen) < 6 and not errors and time.monotonic() < end:
|
|
time.sleep(0.02)
|
|
assert not errors, errors
|
|
assert len(seen) == 6, f"Missing parsed points: {seen}"
|
|
finally:
|
|
client.stop()
|
|
print(f"c104 {importlib.metadata.version('c104')} SQ={int(sequence)}: PASS Type20/21 COT1/2 IOA/value/quality")
|
|
|
|
|
|
def run(server: str, external: str, sequence: bool, use_c104: bool) -> None:
|
|
process = subprocess.Popen([server, str(int(sequence)), "6500"],
|
|
stdout=subprocess.PIPE, stderr=subprocess.PIPE,
|
|
text=True, creationflags=subprocess.CREATE_NO_WINDOW)
|
|
try:
|
|
line = process.stdout.readline().strip()
|
|
assert line.startswith("PORT "), line
|
|
port = int(line.split()[1])
|
|
if use_c104:
|
|
c104_client(port, sequence)
|
|
else:
|
|
result = subprocess.run([external, str(port), str(int(sequence))],
|
|
capture_output=True, text=True, timeout=8,
|
|
creationflags=subprocess.CREATE_NO_WINDOW)
|
|
assert result.returncode == 0, result.stdout + result.stderr
|
|
print(result.stdout.strip())
|
|
stdout, stderr = process.communicate(timeout=8)
|
|
assert process.returncode == 0 and not stderr, stdout + stderr
|
|
finally:
|
|
if process.poll() is None:
|
|
process.kill()
|
|
process.communicate()
|
|
|
|
|
|
if __name__ == "__main__":
|
|
if len(sys.argv) != 3:
|
|
raise SystemExit("Usage: python periodic_scan_interop_test.py server.exe lib60870_client.exe")
|
|
server_path, client_path = (str(pathlib.Path(p).resolve()) for p in sys.argv[1:])
|
|
for seq in (False, True):
|
|
for python_client in (False, True):
|
|
run(server_path, client_path, seq, python_client)
|