Repository navigation
Expand file tree
/
Copy pathsmartess_client.py
More file actions
260 lines (232 loc) · 9.12 KB
/
Copy pathsmartess_client.py
File metadata and controls
260 lines (232 loc) · 9.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
#!/usr/bin/env python3
# Copyright (C) 2025 Tomasz Jablonowski - distributed under the GNU GPL v3 (see LICENSE).
"""
smartess_client.py - klient do odczytu/zapisu rejestrow z dataloggera SmartESS.
Uzycie:
python smartess_client.py <IP_DATALOGGERA> <REGISTER> [--count N] [--localip X.X.X.X] [--tcp-port 502] [--udp-port 58899] [--debug]
python smartess_client.py <IP_DATALOGGERA> <REGISTER> --set "100,200" [--localip X.X.X.X] [--tcp-port 502] [--udp-port 58899] [--debug]
"""
import argparse
import socket
import sys
import time
import json
from typing import Optional, Union, List
from smartess_protocol import (
now_iso,
resolve_local_ip,
send_udp_notify,
wait_for_datalogger_connection,
build_tunneled_request,
build_tunneled_write,
read_one_modbus_tcp_frame,
parse_tunneled_response,
)
__version__ = "1.2.0"
# ----- konfiguracja TID (jak w data_logger_v2) -----
NEXT_TID = 0x8000
def next_tid() -> int:
global NEXT_TID
tid = NEXT_TID
NEXT_TID = (NEXT_TID + 1) & 0xFFFF
if NEXT_TID == 0:
NEXT_TID = 1
return tid
class SmartESSSession:
"""Sesja TCP z dataloggerem utrzymywana do jawnego close()."""
def __init__(
self,
ip: str,
localip: Optional[str] = None,
tcp_port: int = 502,
udp_port: int = 58899,
debug: bool = False,
connect_timeout: int = 30,
io_timeout: float = 3.0,
):
self.ip = ip
self.localip = resolve_local_ip(localip)
self.tcp_port = tcp_port
self.udp_port = udp_port
self.debug = debug
self.connect_timeout = connect_timeout
self.io_timeout = io_timeout
self.conn: Optional[socket.socket] = None
@property
def is_connected(self) -> bool:
return self.conn is not None
def connect(self) -> None:
if self.conn is not None:
return
send_udp_notify(self.ip, self.localip, udp_port=self.udp_port, tcp_port=self.tcp_port, debug=self.debug)
conn = wait_for_datalogger_connection("0.0.0.0", tcp_port=self.tcp_port, timeout=self.connect_timeout, debug=self.debug)
if not conn:
raise RuntimeError("Timeout oczekiwania na polaczenie TCP od dataloggera")
self.conn = conn
time.sleep(0.2)
def close(self) -> None:
if self.conn is None:
return
try:
self.conn.close()
finally:
self.conn = None
def _send_and_parse(self, frame: bytes, tid_used: int) -> Union[List[int], tuple]:
if self.conn is None:
raise RuntimeError("Sesja nie jest polaczona")
try:
self.conn.sendall(frame)
rx = read_one_modbus_tcp_frame(self.conn, timeout=self.io_timeout, expect_tid=tid_used, debug=self.debug)
if not rx:
raise RuntimeError("Brak odpowiedzi od dataloggera")
parsed = parse_tunneled_response(rx, debug=self.debug)
if not parsed:
raise RuntimeError("Nie udalo sie sparsowac odpowiedzi")
_, payload = parsed
return payload
except Exception:
self.close()
raise
def read(self, register: int, count: int = 1, tid: Optional[int] = None) -> str:
self.connect()
tid_used = tid if tid is not None else next_tid()
frame = build_tunneled_request(tid_used, register, count, debug=self.debug)
if self.debug:
print(f"{now_iso()} DEBUG: Sending frame: TID=0x{tid_used:04x}, base={register}, qty={count}")
pdu = frame[7:]
print(f"{now_iso()} DEBUG: PDU={pdu.hex()}, len={len(pdu)}, pdu[0]=0x{pdu[0]:02x}, pdu[1]=0x{pdu[1]:02x}")
if len(pdu) >= 3 and pdu[0] == 0x04 and pdu[1] == 0x01:
print(f"{now_iso()} DEBUG: Matched tunneled RTU")
print(f"{now_iso()} DEBUG: Tunneled RTU fn=0x{pdu[2]:02x}")
regs = self._send_and_parse(frame, tid_used)
if not isinstance(regs, list):
raise RuntimeError("Otrzymano nieoczekiwana odpowiedz read")
ts = now_iso()
out = {"ts": ts}
out.update({str(register + i): regs[i] for i in range(len(regs))})
return json.dumps(out, ensure_ascii=False)
def write(self, register: int, values: List[int], tid: Optional[int] = None) -> str:
self.connect()
tid_used = tid if tid is not None else next_tid()
frame = build_tunneled_write(tid_used, register, values, debug=self.debug)
if self.debug:
print(f"{now_iso()} DEBUG: Sending WRITE frame: TID=0x{tid_used:04x}, base={register}, values={values}")
info = self._send_and_parse(frame, tid_used)
if not (isinstance(info, tuple) and info[0] == "write"):
raise RuntimeError("Otrzymano nieoczekiwana odpowiedz write")
_, addr, qty = info
ts = now_iso()
out = {"ts": ts, "written_base": addr, "written_qty": qty, "values": values}
return json.dumps(out, ensure_ascii=False)
def __enter__(self):
self.connect()
return self
def __exit__(self, exc_type, exc, tb):
self.close()
return False
def smartess_open_session(
ip: str,
localip: Optional[str] = None,
tcp_port: int = 502,
udp_port: int = 58899,
debug: bool = False,
connect_timeout: int = 30,
io_timeout: float = 3.0,
) -> SmartESSSession:
"""Utworz i polacz sesje utrzymywana do jawnego close()."""
session = SmartESSSession(
ip=ip,
localip=localip,
tcp_port=tcp_port,
udp_port=udp_port,
debug=debug,
connect_timeout=connect_timeout,
io_timeout=io_timeout,
)
session.connect()
return session
def smartess_read(
ip: str,
register: int,
count: int = 1,
localip: Optional[str] = None,
tcp_port: int = 502,
udp_port: int = 58899,
debug: bool = False,
tid: Optional[int] = None,
session: Optional[SmartESSSession] = None,
) -> str:
"""Backward-compatible read. Opcjonalnie uzywa utrzymywanej sesji."""
if session is not None:
return session.read(register=register, count=count, tid=tid)
with SmartESSSession(ip=ip, localip=localip, tcp_port=tcp_port, udp_port=udp_port, debug=debug) as sess:
return sess.read(register=register, count=count, tid=tid)
def smartess_write(
ip: str,
register: int,
values: List[int],
localip: Optional[str] = None,
tcp_port: int = 502,
udp_port: int = 58899,
debug: bool = False,
tid: Optional[int] = None,
session: Optional[SmartESSSession] = None,
) -> str:
"""Backward-compatible write. Opcjonalnie uzywa utrzymywanej sesji."""
if session is not None:
return session.write(register=register, values=values, tid=tid)
with SmartESSSession(ip=ip, localip=localip, tcp_port=tcp_port, udp_port=udp_port, debug=debug) as sess:
return sess.write(register=register, values=values, tid=tid)
# ----- CLI -----
def main():
ap = argparse.ArgumentParser(description="Minimalny klient SmartESS (tunel Modbus w TCP).")
ap.add_argument("ip", help="Adres IP dataloggera SmartESS")
ap.add_argument("register", type=int, help="Adres startowy rejestru")
ap.add_argument("--count", type=int, default=1, help="Liczba rejestrow (domyslnie 1)")
ap.add_argument("--localip", help="Lokalny IP (autodetekcja jesli brak)")
ap.add_argument("--tcp-port", type=int, default=502, help="Port TCP Anenji (domyslnie 502)")
ap.add_argument("--udp-port", type=int, default=58899, help="Port UDP Anenji (domyslnie 58899)")
ap.add_argument("--debug", action="store_true", help="Debug")
ap.add_argument("--set", metavar="VALUES", help='Wykonaj zapis (Write Multiple Registers). Wartosci do zapisu przecinkami, np. "100,200"')
ap.add_argument("--version", action="version", version=f"%(prog)s {__version__}")
args = ap.parse_args()
try:
if args.set is not None:
if args.set.strip() == "":
ap.error('Przy zapisie wymagane wartosci, np. --set "100,200"')
vals = []
for s in args.set.split(","):
s = s.strip()
if not s:
continue
if s.startswith("0x") or s.startswith("0X"):
vals.append(int(s, 16))
else:
vals.append(int(s, 10))
if not vals:
ap.error('Przy zapisie wymagane wartosci, np. --set "100,200"')
res_json = smartess_write(
ip=args.ip,
register=args.register,
values=vals,
localip=args.localip,
tcp_port=args.tcp_port,
udp_port=args.udp_port,
debug=args.debug,
)
else:
res_json = smartess_read(
ip=args.ip,
register=args.register,
count=args.count,
localip=args.localip,
tcp_port=args.tcp_port,
udp_port=args.udp_port,
debug=args.debug,
)
print(res_json)
except Exception as e:
print(f"Blad: {e}", file=sys.stderr)
sys.exit(1)
if __name__ == "__main__":
main()