easye4-python 1.0.1__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- easye4/__init__.py +9 -0
- easye4/api.py +926 -0
- easye4/certificates/easyRootCertV1.crt +15 -0
- easye4/client.py +468 -0
- easye4_python-1.0.1.dist-info/METADATA +251 -0
- easye4_python-1.0.1.dist-info/RECORD +9 -0
- easye4_python-1.0.1.dist-info/WHEEL +5 -0
- easye4_python-1.0.1.dist-info/licenses/LICENSE +21 -0
- easye4_python-1.0.1.dist-info/top_level.txt +1 -0
easye4/api.py
ADDED
|
@@ -0,0 +1,926 @@
|
|
|
1
|
+
import requests
|
|
2
|
+
|
|
3
|
+
from requests.auth import HTTPBasicAuth
|
|
4
|
+
import base64
|
|
5
|
+
import struct
|
|
6
|
+
from pathlib import Path
|
|
7
|
+
import ssl
|
|
8
|
+
from requests.adapters import HTTPAdapter
|
|
9
|
+
|
|
10
|
+
class EatonTLSAdapter(HTTPAdapter):
|
|
11
|
+
|
|
12
|
+
def __init__(
|
|
13
|
+
self,
|
|
14
|
+
ca_file,
|
|
15
|
+
compatibility_mode=False,
|
|
16
|
+
**kwargs
|
|
17
|
+
):
|
|
18
|
+
self.ca_file = ca_file
|
|
19
|
+
self.compatibility_mode = compatibility_mode
|
|
20
|
+
|
|
21
|
+
super().__init__(**kwargs)
|
|
22
|
+
|
|
23
|
+
def init_poolmanager(
|
|
24
|
+
self,
|
|
25
|
+
connections,
|
|
26
|
+
maxsize,
|
|
27
|
+
block=False,
|
|
28
|
+
**pool_kwargs
|
|
29
|
+
):
|
|
30
|
+
|
|
31
|
+
context = ssl.create_default_context(
|
|
32
|
+
cafile=self.ca_file
|
|
33
|
+
)
|
|
34
|
+
|
|
35
|
+
if self.compatibility_mode:
|
|
36
|
+
context.verify_flags &= ~ssl.VERIFY_X509_STRICT
|
|
37
|
+
|
|
38
|
+
pool_kwargs["ssl_context"] = context
|
|
39
|
+
|
|
40
|
+
return super().init_poolmanager(
|
|
41
|
+
connections,
|
|
42
|
+
maxsize,
|
|
43
|
+
block=block,
|
|
44
|
+
**pool_kwargs
|
|
45
|
+
)
|
|
46
|
+
|
|
47
|
+
class EasyE4API:
|
|
48
|
+
|
|
49
|
+
def __init__(
|
|
50
|
+
self,
|
|
51
|
+
ip,
|
|
52
|
+
port=443,
|
|
53
|
+
protocol="https",
|
|
54
|
+
tls_config=None,
|
|
55
|
+
api_key=None,
|
|
56
|
+
username=None,
|
|
57
|
+
password=None,
|
|
58
|
+
timeout=5,
|
|
59
|
+
auth_method="none",
|
|
60
|
+
):
|
|
61
|
+
|
|
62
|
+
self.base_url = f"{protocol}://{ip}:{port}"
|
|
63
|
+
self.timeout = timeout
|
|
64
|
+
|
|
65
|
+
self.session = requests.Session()
|
|
66
|
+
|
|
67
|
+
# TLS certificate configuration
|
|
68
|
+
|
|
69
|
+
tls_config = tls_config or {}
|
|
70
|
+
|
|
71
|
+
ca_mode = tls_config.get("ca_mode", "system")
|
|
72
|
+
|
|
73
|
+
if ca_mode == "system":
|
|
74
|
+
|
|
75
|
+
self.session.verify = True
|
|
76
|
+
|
|
77
|
+
elif ca_mode == "custom":
|
|
78
|
+
|
|
79
|
+
ca_file = tls_config.get("ca_file")
|
|
80
|
+
|
|
81
|
+
if not ca_file:
|
|
82
|
+
raise ValueError(
|
|
83
|
+
"Custom CA mode requires a certificate file"
|
|
84
|
+
)
|
|
85
|
+
|
|
86
|
+
base_dir = Path(__file__).resolve().parent.parent
|
|
87
|
+
|
|
88
|
+
certificate_path = Path(ca_file)
|
|
89
|
+
|
|
90
|
+
if not certificate_path.is_absolute():
|
|
91
|
+
certificate_path = base_dir / certificate_path
|
|
92
|
+
|
|
93
|
+
certificate_path = certificate_path.resolve()
|
|
94
|
+
|
|
95
|
+
if not certificate_path.is_file():
|
|
96
|
+
raise FileNotFoundError(
|
|
97
|
+
f"CA certificate not found: {certificate_path}"
|
|
98
|
+
)
|
|
99
|
+
|
|
100
|
+
self.session.verify = str(certificate_path)
|
|
101
|
+
|
|
102
|
+
self.session.mount(
|
|
103
|
+
self.base_url + "/",
|
|
104
|
+
EatonTLSAdapter(
|
|
105
|
+
ca_file=str(certificate_path),
|
|
106
|
+
compatibility_mode=tls_config.get(
|
|
107
|
+
"compatibility_mode",
|
|
108
|
+
False
|
|
109
|
+
)
|
|
110
|
+
)
|
|
111
|
+
)
|
|
112
|
+
|
|
113
|
+
else:
|
|
114
|
+
|
|
115
|
+
raise ValueError(
|
|
116
|
+
f"Unsupported TLS CA mode: {ca_mode}"
|
|
117
|
+
)
|
|
118
|
+
|
|
119
|
+
# Authenticatie
|
|
120
|
+
# Authentication configuration
|
|
121
|
+
|
|
122
|
+
if auth_method == "bearer":
|
|
123
|
+
|
|
124
|
+
if not api_key:
|
|
125
|
+
raise ValueError(
|
|
126
|
+
"API key is required for Bearer authentication"
|
|
127
|
+
)
|
|
128
|
+
|
|
129
|
+
self.session.headers.update({
|
|
130
|
+
"Authorization": f"Bearer {api_key}"
|
|
131
|
+
})
|
|
132
|
+
|
|
133
|
+
elif auth_method == "basic":
|
|
134
|
+
|
|
135
|
+
if not username or not password:
|
|
136
|
+
raise ValueError(
|
|
137
|
+
"Username and password are required"
|
|
138
|
+
)
|
|
139
|
+
|
|
140
|
+
self.session.auth = HTTPBasicAuth(
|
|
141
|
+
username,
|
|
142
|
+
password
|
|
143
|
+
)
|
|
144
|
+
|
|
145
|
+
elif auth_method != "none":
|
|
146
|
+
|
|
147
|
+
raise ValueError(
|
|
148
|
+
f"Unsupported authentication method: {auth_method}"
|
|
149
|
+
)
|
|
150
|
+
|
|
151
|
+
def close(self):
|
|
152
|
+
self.session.close()
|
|
153
|
+
|
|
154
|
+
def __enter__(self):
|
|
155
|
+
return self
|
|
156
|
+
|
|
157
|
+
def __exit__(self, exc_type, exc_value, traceback):
|
|
158
|
+
self.close()
|
|
159
|
+
return False
|
|
160
|
+
|
|
161
|
+
def _get_json(self, params):
|
|
162
|
+
|
|
163
|
+
url = f"{self.base_url}/api/get/data"
|
|
164
|
+
|
|
165
|
+
try:
|
|
166
|
+
response = self.session.get(
|
|
167
|
+
url,
|
|
168
|
+
params=params,
|
|
169
|
+
timeout=self.timeout
|
|
170
|
+
)
|
|
171
|
+
|
|
172
|
+
response.raise_for_status()
|
|
173
|
+
|
|
174
|
+
|
|
175
|
+
|
|
176
|
+
except requests.exceptions.SSLError:
|
|
177
|
+
|
|
178
|
+
raise ConnectionError(
|
|
179
|
+
|
|
180
|
+
"TLS certificate verification failed. "
|
|
181
|
+
|
|
182
|
+
"Check the Eaton Root Certificate."
|
|
183
|
+
|
|
184
|
+
) from None
|
|
185
|
+
|
|
186
|
+
except requests.exceptions.Timeout:
|
|
187
|
+
raise ConnectionError(
|
|
188
|
+
"Connection timeout. "
|
|
189
|
+
"The easyE4 did not respond in time."
|
|
190
|
+
) from None
|
|
191
|
+
|
|
192
|
+
except requests.exceptions.HTTPError as error:
|
|
193
|
+
|
|
194
|
+
status = (
|
|
195
|
+
error.response.status_code
|
|
196
|
+
if error.response is not None
|
|
197
|
+
else None
|
|
198
|
+
)
|
|
199
|
+
|
|
200
|
+
if status == 401:
|
|
201
|
+
raise ConnectionError(
|
|
202
|
+
"Authentication failed. "
|
|
203
|
+
"Check API key or login credentials."
|
|
204
|
+
) from None
|
|
205
|
+
|
|
206
|
+
if status == 403:
|
|
207
|
+
raise ConnectionError(
|
|
208
|
+
"Access denied. "
|
|
209
|
+
"The user has insufficient permissions."
|
|
210
|
+
) from None
|
|
211
|
+
|
|
212
|
+
if status == 404:
|
|
213
|
+
raise ConnectionError(
|
|
214
|
+
"API endpoint not found. "
|
|
215
|
+
"Check easyE4 firmware and API configuration."
|
|
216
|
+
) from None
|
|
217
|
+
|
|
218
|
+
raise ConnectionError(
|
|
219
|
+
f"easyE4 HTTP error: {status}"
|
|
220
|
+
) from None
|
|
221
|
+
|
|
222
|
+
except requests.exceptions.ConnectionError:
|
|
223
|
+
raise ConnectionError(
|
|
224
|
+
"Unable to connect to easyE4. "
|
|
225
|
+
"Check IP address, port and network."
|
|
226
|
+
) from None
|
|
227
|
+
|
|
228
|
+
except requests.exceptions.RequestException:
|
|
229
|
+
raise ConnectionError(
|
|
230
|
+
"Unexpected HTTP communication error."
|
|
231
|
+
) from None
|
|
232
|
+
|
|
233
|
+
try:
|
|
234
|
+
return response.json()
|
|
235
|
+
|
|
236
|
+
except ValueError:
|
|
237
|
+
raise ValueError(
|
|
238
|
+
"Invalid JSON response received from easyE4."
|
|
239
|
+
) from None
|
|
240
|
+
|
|
241
|
+
def read_operand(self, operand_type, index):
|
|
242
|
+
|
|
243
|
+
operand_type = operand_type.upper()
|
|
244
|
+
|
|
245
|
+
supported_types = {
|
|
246
|
+
"AI", "M", "I", "O",
|
|
247
|
+
"AO", "MB", "MW", "MD"
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
if operand_type == "M":
|
|
251
|
+
return self.read_markers([index])[f"M{index}"]
|
|
252
|
+
|
|
253
|
+
if operand_type not in supported_types:
|
|
254
|
+
raise ValueError(
|
|
255
|
+
f"Unsupported operand type: {operand_type}"
|
|
256
|
+
)
|
|
257
|
+
|
|
258
|
+
if not isinstance(index, int) or index < 1:
|
|
259
|
+
raise ValueError(
|
|
260
|
+
"Operant index must be a positive and whole number"
|
|
261
|
+
)
|
|
262
|
+
|
|
263
|
+
params = {
|
|
264
|
+
"elm": f"{operand_type}({index})"
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
result = self._get_json(params)
|
|
268
|
+
|
|
269
|
+
# JSON-antwoord verwerken
|
|
270
|
+
try:
|
|
271
|
+
operands = result["OPERANDS"]
|
|
272
|
+
|
|
273
|
+
key = f"{operand_type}SINGLE"
|
|
274
|
+
|
|
275
|
+
values = operands[key]
|
|
276
|
+
|
|
277
|
+
item = next(
|
|
278
|
+
value for value in values
|
|
279
|
+
if int(value["INDEX"]) == index
|
|
280
|
+
)
|
|
281
|
+
|
|
282
|
+
if "ERROR" in item:
|
|
283
|
+
raise RuntimeError(
|
|
284
|
+
f"easyE4 API-fault: {item['ERROR']}"
|
|
285
|
+
)
|
|
286
|
+
|
|
287
|
+
raw_value = item["V"]
|
|
288
|
+
|
|
289
|
+
if isinstance(raw_value, bool):
|
|
290
|
+
raise ValueError(
|
|
291
|
+
"Invalid operandvalue: recieved boolean"
|
|
292
|
+
)
|
|
293
|
+
|
|
294
|
+
if isinstance(raw_value, int):
|
|
295
|
+
value = raw_value
|
|
296
|
+
|
|
297
|
+
elif isinstance(raw_value, str):
|
|
298
|
+
try:
|
|
299
|
+
value = int(raw_value)
|
|
300
|
+
except ValueError:
|
|
301
|
+
raise ValueError(
|
|
302
|
+
"Recieved non-numeric value. "
|
|
303
|
+
"The Easy possibly uses Bases64-coding."
|
|
304
|
+
) from None
|
|
305
|
+
|
|
306
|
+
else:
|
|
307
|
+
raise ValueError(
|
|
308
|
+
f"Unsupported operandtype: "
|
|
309
|
+
f"{type(raw_value).__name__}"
|
|
310
|
+
)
|
|
311
|
+
|
|
312
|
+
# Digitale operanden valideren
|
|
313
|
+
if operand_type in ("M", "I", "O"):
|
|
314
|
+
|
|
315
|
+
if value not in (0, 1):
|
|
316
|
+
raise ValueError(
|
|
317
|
+
f"Invalid digital value for "
|
|
318
|
+
f"{operand_type}{index}: {value}"
|
|
319
|
+
)
|
|
320
|
+
|
|
321
|
+
return value
|
|
322
|
+
|
|
323
|
+
except (KeyError, IndexError, StopIteration, TypeError) as error:
|
|
324
|
+
raise ValueError(
|
|
325
|
+
f"Unexpected API answer for "
|
|
326
|
+
f"{operand_type}{index}: {result}"
|
|
327
|
+
) from error
|
|
328
|
+
|
|
329
|
+
def _read_marker_memory(self):
|
|
330
|
+
|
|
331
|
+
result = self._get_json({"elm": "M"})
|
|
332
|
+
|
|
333
|
+
try:
|
|
334
|
+
encoded = result["OPERANDS"]["MALL"]
|
|
335
|
+
|
|
336
|
+
raw = base64.b64decode(
|
|
337
|
+
encoded,
|
|
338
|
+
validate=True
|
|
339
|
+
)
|
|
340
|
+
|
|
341
|
+
except (KeyError, TypeError, ValueError) as error:
|
|
342
|
+
raise ValueError(
|
|
343
|
+
"Invalid MALL response"
|
|
344
|
+
) from error
|
|
345
|
+
|
|
346
|
+
if len(raw) != 128:
|
|
347
|
+
raise ValueError(
|
|
348
|
+
f"Invalid MALL length: {len(raw)}"
|
|
349
|
+
)
|
|
350
|
+
|
|
351
|
+
return raw
|
|
352
|
+
|
|
353
|
+
def read_marker_memory(self, markers=None, words=None, dwords=None):
|
|
354
|
+
|
|
355
|
+
markers = [] if markers is None else list(markers)
|
|
356
|
+
words = [] if words is None else list(words)
|
|
357
|
+
dwords = [] if dwords is None else list(dwords)
|
|
358
|
+
|
|
359
|
+
if not markers and not words and not dwords:
|
|
360
|
+
return {}
|
|
361
|
+
|
|
362
|
+
def validate_indices(indices, maximum, operand_type):
|
|
363
|
+
|
|
364
|
+
for index in indices:
|
|
365
|
+
|
|
366
|
+
if (
|
|
367
|
+
isinstance(index, bool)
|
|
368
|
+
or not isinstance(index, int)
|
|
369
|
+
or not 1 <= index <= maximum
|
|
370
|
+
):
|
|
371
|
+
raise ValueError(
|
|
372
|
+
f"Invalid {operand_type} index: {index}"
|
|
373
|
+
)
|
|
374
|
+
|
|
375
|
+
validate_indices(markers, 1024, "M")
|
|
376
|
+
validate_indices(words, 64, "MW")
|
|
377
|
+
validate_indices(dwords, 32, "MD")
|
|
378
|
+
|
|
379
|
+
# Slechts één HTTP-aanvraag
|
|
380
|
+
raw = self._read_marker_memory()
|
|
381
|
+
|
|
382
|
+
results = {}
|
|
383
|
+
|
|
384
|
+
# Digitale merkers
|
|
385
|
+
for index in markers:
|
|
386
|
+
|
|
387
|
+
position = index - 1
|
|
388
|
+
|
|
389
|
+
byte_index = position // 8
|
|
390
|
+
bit_index = position % 8
|
|
391
|
+
|
|
392
|
+
results[f"M{index}"] = (
|
|
393
|
+
raw[byte_index] >> bit_index
|
|
394
|
+
) & 1
|
|
395
|
+
|
|
396
|
+
# Merkerwoorden: unsigned 16-bit
|
|
397
|
+
for index in words:
|
|
398
|
+
|
|
399
|
+
offset = (index - 1) * 2
|
|
400
|
+
|
|
401
|
+
results[f"MW{index}"] = struct.unpack_from(
|
|
402
|
+
"<H",
|
|
403
|
+
raw,
|
|
404
|
+
offset
|
|
405
|
+
)[0]
|
|
406
|
+
|
|
407
|
+
# Merkerdubbelwoorden: unsigned 32-bit
|
|
408
|
+
for index in dwords:
|
|
409
|
+
|
|
410
|
+
offset = (index - 1) * 4
|
|
411
|
+
|
|
412
|
+
results[f"MD{index}"] = struct.unpack_from(
|
|
413
|
+
"<I",
|
|
414
|
+
raw,
|
|
415
|
+
offset
|
|
416
|
+
)[0]
|
|
417
|
+
|
|
418
|
+
return results
|
|
419
|
+
|
|
420
|
+
def read_marker_word(self, index):
|
|
421
|
+
|
|
422
|
+
return self.read_marker_memory(
|
|
423
|
+
words=[index]
|
|
424
|
+
)[f"MW{index}"]
|
|
425
|
+
|
|
426
|
+
def read_marker_dword(self, index):
|
|
427
|
+
|
|
428
|
+
return self.read_marker_memory(
|
|
429
|
+
dwords=[index]
|
|
430
|
+
)[f"MD{index}"]
|
|
431
|
+
|
|
432
|
+
def read_markers(self, indices):
|
|
433
|
+
|
|
434
|
+
if not indices:
|
|
435
|
+
return {}
|
|
436
|
+
|
|
437
|
+
return self.read_marker_memory(
|
|
438
|
+
markers=indices
|
|
439
|
+
)
|
|
440
|
+
|
|
441
|
+
def read_digital_inputs(self, indices):
|
|
442
|
+
|
|
443
|
+
if not indices:
|
|
444
|
+
return {}
|
|
445
|
+
|
|
446
|
+
for index in indices:
|
|
447
|
+
if (
|
|
448
|
+
isinstance(index, bool)
|
|
449
|
+
or not isinstance(index, int)
|
|
450
|
+
or not 1 <= index <= 128
|
|
451
|
+
):
|
|
452
|
+
raise ValueError(
|
|
453
|
+
f"Invalid digital input index: {index}"
|
|
454
|
+
)
|
|
455
|
+
|
|
456
|
+
result = self._get_json({"elm": "I"})
|
|
457
|
+
|
|
458
|
+
try:
|
|
459
|
+
encoded = result["OPERANDS"]["IALL"]
|
|
460
|
+
|
|
461
|
+
raw = base64.b64decode(
|
|
462
|
+
encoded,
|
|
463
|
+
validate=True
|
|
464
|
+
)
|
|
465
|
+
|
|
466
|
+
except (KeyError, TypeError, ValueError) as error:
|
|
467
|
+
raise ValueError(
|
|
468
|
+
"Invalid IALL response"
|
|
469
|
+
) from error
|
|
470
|
+
|
|
471
|
+
if len(raw) != 16:
|
|
472
|
+
raise ValueError(
|
|
473
|
+
f"Invalid IALL length: {len(raw)}"
|
|
474
|
+
)
|
|
475
|
+
|
|
476
|
+
inputs = {}
|
|
477
|
+
|
|
478
|
+
for index in indices:
|
|
479
|
+
|
|
480
|
+
bit_position = index - 1
|
|
481
|
+
byte_index = bit_position // 8
|
|
482
|
+
bit_index = bit_position % 8
|
|
483
|
+
|
|
484
|
+
value = (
|
|
485
|
+
raw[byte_index] >> bit_index
|
|
486
|
+
) & 1
|
|
487
|
+
|
|
488
|
+
inputs[f"I{index}"] = value
|
|
489
|
+
|
|
490
|
+
return inputs
|
|
491
|
+
|
|
492
|
+
def read_digital_outputs(self, indices):
|
|
493
|
+
|
|
494
|
+
if not indices:
|
|
495
|
+
return {}
|
|
496
|
+
|
|
497
|
+
for index in indices:
|
|
498
|
+
if (
|
|
499
|
+
isinstance(index, bool)
|
|
500
|
+
or not isinstance(index, int)
|
|
501
|
+
or not 1 <= index <= 128
|
|
502
|
+
):
|
|
503
|
+
raise ValueError(
|
|
504
|
+
f"Invalid digital output index: {index}"
|
|
505
|
+
)
|
|
506
|
+
|
|
507
|
+
result = self._get_json({"elm": "O"})
|
|
508
|
+
|
|
509
|
+
try:
|
|
510
|
+
encoded = result["OPERANDS"]["OALL"]
|
|
511
|
+
|
|
512
|
+
raw = base64.b64decode(
|
|
513
|
+
encoded,
|
|
514
|
+
validate=True
|
|
515
|
+
)
|
|
516
|
+
|
|
517
|
+
except (KeyError, TypeError, ValueError) as error:
|
|
518
|
+
raise ValueError(
|
|
519
|
+
"Invalid OALL response"
|
|
520
|
+
) from error
|
|
521
|
+
|
|
522
|
+
if len(raw) != 16:
|
|
523
|
+
raise ValueError(
|
|
524
|
+
f"Invalid OALL length: {len(raw)}"
|
|
525
|
+
)
|
|
526
|
+
|
|
527
|
+
outputs = {}
|
|
528
|
+
|
|
529
|
+
for index in indices:
|
|
530
|
+
|
|
531
|
+
bit_position = index - 1
|
|
532
|
+
byte_index = bit_position // 8
|
|
533
|
+
bit_index = bit_position % 8
|
|
534
|
+
|
|
535
|
+
value = (
|
|
536
|
+
raw[byte_index] >> bit_index
|
|
537
|
+
) & 1
|
|
538
|
+
|
|
539
|
+
outputs[f"O{index}"] = value
|
|
540
|
+
|
|
541
|
+
return outputs
|
|
542
|
+
|
|
543
|
+
def read_analog_inputs(self, indices):
|
|
544
|
+
|
|
545
|
+
if not indices:
|
|
546
|
+
return {}
|
|
547
|
+
|
|
548
|
+
for index in indices:
|
|
549
|
+
if (
|
|
550
|
+
isinstance(index, bool)
|
|
551
|
+
or not isinstance(index, int)
|
|
552
|
+
or not 1 <= index <= 512
|
|
553
|
+
):
|
|
554
|
+
raise ValueError(
|
|
555
|
+
f"Invalid analog input index: {index}"
|
|
556
|
+
)
|
|
557
|
+
|
|
558
|
+
result = self._get_json({"elm": "AI"})
|
|
559
|
+
|
|
560
|
+
try:
|
|
561
|
+
encoded = result["OPERANDS"]["AIALL"]
|
|
562
|
+
|
|
563
|
+
raw = base64.b64decode(
|
|
564
|
+
encoded,
|
|
565
|
+
validate=True
|
|
566
|
+
)
|
|
567
|
+
|
|
568
|
+
except (KeyError, TypeError, ValueError) as error:
|
|
569
|
+
raise ValueError(
|
|
570
|
+
"Invalid AIALL response"
|
|
571
|
+
) from error
|
|
572
|
+
|
|
573
|
+
if len(raw) != 2048:
|
|
574
|
+
raise ValueError(
|
|
575
|
+
f"Invalid AIALL length: {len(raw)}"
|
|
576
|
+
)
|
|
577
|
+
|
|
578
|
+
values = struct.unpack("<512i", raw)
|
|
579
|
+
|
|
580
|
+
return {
|
|
581
|
+
f"AI{index}": values[index - 1]
|
|
582
|
+
for index in indices
|
|
583
|
+
}
|
|
584
|
+
|
|
585
|
+
def read_analog_outputs(self, indices):
|
|
586
|
+
|
|
587
|
+
if not indices:
|
|
588
|
+
return {}
|
|
589
|
+
|
|
590
|
+
for index in indices:
|
|
591
|
+
if (
|
|
592
|
+
isinstance(index, bool)
|
|
593
|
+
or not isinstance(index, int)
|
|
594
|
+
or not 1 <= index <= 128
|
|
595
|
+
):
|
|
596
|
+
raise ValueError(
|
|
597
|
+
f"Invalid analog output index: {index}"
|
|
598
|
+
)
|
|
599
|
+
|
|
600
|
+
result = self._get_json({"elm": "AO"})
|
|
601
|
+
|
|
602
|
+
try:
|
|
603
|
+
encoded = result["OPERANDS"]["AOALL"]
|
|
604
|
+
|
|
605
|
+
raw = base64.b64decode(
|
|
606
|
+
encoded,
|
|
607
|
+
validate=True
|
|
608
|
+
)
|
|
609
|
+
|
|
610
|
+
except (KeyError, TypeError, ValueError) as error:
|
|
611
|
+
raise ValueError(
|
|
612
|
+
"Invalid AOALL response"
|
|
613
|
+
) from error
|
|
614
|
+
|
|
615
|
+
if len(raw) != 512:
|
|
616
|
+
raise ValueError(
|
|
617
|
+
f"Invalid AOALL length: {len(raw)}"
|
|
618
|
+
)
|
|
619
|
+
|
|
620
|
+
values = struct.unpack("<128i", raw)
|
|
621
|
+
|
|
622
|
+
return {
|
|
623
|
+
f"AO{index}": values[index - 1]
|
|
624
|
+
for index in indices
|
|
625
|
+
}
|
|
626
|
+
|
|
627
|
+
def read_operands(self, operands):
|
|
628
|
+
|
|
629
|
+
if not operands:
|
|
630
|
+
return {}
|
|
631
|
+
|
|
632
|
+
normalized = []
|
|
633
|
+
|
|
634
|
+
for operand_type, index in operands:
|
|
635
|
+
|
|
636
|
+
operand_type = operand_type.upper()
|
|
637
|
+
|
|
638
|
+
if operand_type not in ("AI", "M"):
|
|
639
|
+
raise ValueError(
|
|
640
|
+
f"Unsupported operandtype: {operand_type}"
|
|
641
|
+
)
|
|
642
|
+
|
|
643
|
+
if not isinstance(index, int) or index < 1:
|
|
644
|
+
raise ValueError(
|
|
645
|
+
f"Invalid operand index: {index}"
|
|
646
|
+
)
|
|
647
|
+
|
|
648
|
+
normalized.append((operand_type, index))
|
|
649
|
+
|
|
650
|
+
results = {}
|
|
651
|
+
|
|
652
|
+
# Analoge ingangen verzamelen
|
|
653
|
+
analog_indices = [
|
|
654
|
+
index
|
|
655
|
+
for operand_type, index in normalized
|
|
656
|
+
if operand_type == "AI"
|
|
657
|
+
]
|
|
658
|
+
|
|
659
|
+
if analog_indices:
|
|
660
|
+
|
|
661
|
+
start = min(analog_indices)
|
|
662
|
+
end = max(analog_indices)
|
|
663
|
+
|
|
664
|
+
if start == end:
|
|
665
|
+
|
|
666
|
+
results[f"AI{start}"] = self.read_operand(
|
|
667
|
+
"AI",
|
|
668
|
+
start
|
|
669
|
+
)
|
|
670
|
+
|
|
671
|
+
else:
|
|
672
|
+
|
|
673
|
+
analog_values = self.read_analog_range(
|
|
674
|
+
start,
|
|
675
|
+
end
|
|
676
|
+
)
|
|
677
|
+
|
|
678
|
+
for index in analog_indices:
|
|
679
|
+
results[f"AI{index}"] = analog_values[
|
|
680
|
+
f"AI{index}"
|
|
681
|
+
]
|
|
682
|
+
|
|
683
|
+
# Digitale merkers gezamenlijk uitlezen
|
|
684
|
+
marker_indices = [
|
|
685
|
+
index
|
|
686
|
+
for operand_type, index in normalized
|
|
687
|
+
if operand_type == "M"
|
|
688
|
+
]
|
|
689
|
+
|
|
690
|
+
if marker_indices:
|
|
691
|
+
results.update(
|
|
692
|
+
self.read_markers(marker_indices)
|
|
693
|
+
)
|
|
694
|
+
|
|
695
|
+
return results
|
|
696
|
+
|
|
697
|
+
def read_analog_range(self, start, end):
|
|
698
|
+
|
|
699
|
+
if not isinstance(start, int) or not isinstance(end, int):
|
|
700
|
+
raise ValueError("Start and end must be whole numbers")
|
|
701
|
+
|
|
702
|
+
if start < 1 or end < start:
|
|
703
|
+
raise ValueError("Invalid analog range")
|
|
704
|
+
|
|
705
|
+
params = {
|
|
706
|
+
"elm": f"AI({start},{end})"
|
|
707
|
+
}
|
|
708
|
+
|
|
709
|
+
result = self._get_json(params)
|
|
710
|
+
|
|
711
|
+
try:
|
|
712
|
+
item = result["OPERANDS"]["AIRANGE"][0]
|
|
713
|
+
|
|
714
|
+
if "ERROR" in item:
|
|
715
|
+
raise RuntimeError(
|
|
716
|
+
f"easyE4 API-fault: {item['ERROR']}"
|
|
717
|
+
)
|
|
718
|
+
|
|
719
|
+
if int(item["START"]) != start or int(item["END"]) != end:
|
|
720
|
+
raise ValueError(
|
|
721
|
+
"The recieved range is different from the requested range"
|
|
722
|
+
)
|
|
723
|
+
|
|
724
|
+
encoded = item["V"]
|
|
725
|
+
|
|
726
|
+
raw_bytes = base64.b64decode(
|
|
727
|
+
encoded,
|
|
728
|
+
validate=True
|
|
729
|
+
)
|
|
730
|
+
|
|
731
|
+
count = end - start + 1
|
|
732
|
+
|
|
733
|
+
if len(raw_bytes) != count * 4:
|
|
734
|
+
raise ValueError(
|
|
735
|
+
"Unexpected length of analog data"
|
|
736
|
+
)
|
|
737
|
+
|
|
738
|
+
values = struct.unpack(
|
|
739
|
+
f"<{count}i",
|
|
740
|
+
raw_bytes
|
|
741
|
+
)
|
|
742
|
+
|
|
743
|
+
return {
|
|
744
|
+
f"AI{start + i}": value
|
|
745
|
+
for i, value in enumerate(values)
|
|
746
|
+
}
|
|
747
|
+
|
|
748
|
+
except (KeyError, IndexError, TypeError) as error:
|
|
749
|
+
raise ValueError(
|
|
750
|
+
f"Unexpected API answer: {result}"
|
|
751
|
+
) from error
|
|
752
|
+
|
|
753
|
+
def _write_operand(self, operand_type, index, value):
|
|
754
|
+
|
|
755
|
+
url = f"{self.base_url}/api/set/op"
|
|
756
|
+
|
|
757
|
+
try:
|
|
758
|
+
response = self.session.get(
|
|
759
|
+
url,
|
|
760
|
+
params={
|
|
761
|
+
"op": operand_type,
|
|
762
|
+
"index": index,
|
|
763
|
+
"val": value
|
|
764
|
+
},
|
|
765
|
+
timeout=self.timeout
|
|
766
|
+
)
|
|
767
|
+
|
|
768
|
+
response.raise_for_status()
|
|
769
|
+
|
|
770
|
+
except requests.exceptions.SSLError:
|
|
771
|
+
raise ConnectionError(
|
|
772
|
+
"TLS certificate verification failed"
|
|
773
|
+
) from None
|
|
774
|
+
|
|
775
|
+
except requests.exceptions.Timeout:
|
|
776
|
+
raise ConnectionError(
|
|
777
|
+
"Connection timeout"
|
|
778
|
+
) from None
|
|
779
|
+
|
|
780
|
+
except requests.exceptions.HTTPError as error:
|
|
781
|
+
|
|
782
|
+
status = (
|
|
783
|
+
error.response.status_code
|
|
784
|
+
if error.response is not None
|
|
785
|
+
else None
|
|
786
|
+
)
|
|
787
|
+
|
|
788
|
+
if status == 400:
|
|
789
|
+
raise ValueError(
|
|
790
|
+
"Write request rejected by easyE4"
|
|
791
|
+
) from None
|
|
792
|
+
|
|
793
|
+
if status == 401:
|
|
794
|
+
raise PermissionError(
|
|
795
|
+
"Authentication failed or write access denied"
|
|
796
|
+
) from None
|
|
797
|
+
|
|
798
|
+
if status == 403:
|
|
799
|
+
raise PermissionError(
|
|
800
|
+
"Insufficient write permissions"
|
|
801
|
+
) from None
|
|
802
|
+
|
|
803
|
+
raise ConnectionError(
|
|
804
|
+
f"easyE4 HTTP error: {status}"
|
|
805
|
+
) from None
|
|
806
|
+
|
|
807
|
+
except requests.exceptions.ConnectionError:
|
|
808
|
+
raise ConnectionError(
|
|
809
|
+
"Unable to connect to easyE4"
|
|
810
|
+
) from None
|
|
811
|
+
|
|
812
|
+
except requests.exceptions.RequestException:
|
|
813
|
+
raise ConnectionError(
|
|
814
|
+
"Unexpected HTTP communication error"
|
|
815
|
+
) from None
|
|
816
|
+
|
|
817
|
+
return response.ok
|
|
818
|
+
|
|
819
|
+
def write_marker(self, index, value):
|
|
820
|
+
|
|
821
|
+
if (
|
|
822
|
+
isinstance(index, bool)
|
|
823
|
+
or not isinstance(index, int)
|
|
824
|
+
or not 1 <= index <= 512
|
|
825
|
+
):
|
|
826
|
+
raise ValueError(
|
|
827
|
+
"Marker index must be between 1 and 512"
|
|
828
|
+
)
|
|
829
|
+
|
|
830
|
+
if (
|
|
831
|
+
isinstance(value, bool)
|
|
832
|
+
or not isinstance(value, int)
|
|
833
|
+
or value not in (0, 1)
|
|
834
|
+
):
|
|
835
|
+
raise ValueError(
|
|
836
|
+
"Marker value must be 0 or 1"
|
|
837
|
+
)
|
|
838
|
+
|
|
839
|
+
return self._write_operand(
|
|
840
|
+
"M",
|
|
841
|
+
index,
|
|
842
|
+
value
|
|
843
|
+
)
|
|
844
|
+
|
|
845
|
+
def write_marker_word(self, index, value):
|
|
846
|
+
|
|
847
|
+
if (
|
|
848
|
+
isinstance(index, bool)
|
|
849
|
+
or not isinstance(index, int)
|
|
850
|
+
or not 1 <= index <= 64
|
|
851
|
+
):
|
|
852
|
+
raise ValueError(
|
|
853
|
+
"Marker word index must be between 1 and 64"
|
|
854
|
+
)
|
|
855
|
+
|
|
856
|
+
if (
|
|
857
|
+
isinstance(value, bool)
|
|
858
|
+
or not isinstance(value, int)
|
|
859
|
+
or not 0 <= value <= 65535
|
|
860
|
+
):
|
|
861
|
+
raise ValueError(
|
|
862
|
+
"Marker word value must be between 0 and 65535"
|
|
863
|
+
)
|
|
864
|
+
|
|
865
|
+
return self._write_operand(
|
|
866
|
+
"MW",
|
|
867
|
+
index,
|
|
868
|
+
value
|
|
869
|
+
)
|
|
870
|
+
|
|
871
|
+
def get_system_info(self, item):
|
|
872
|
+
|
|
873
|
+
supported_items = {
|
|
874
|
+
"VERSION",
|
|
875
|
+
"STATE",
|
|
876
|
+
"DEVNAME",
|
|
877
|
+
"PROGNAME"
|
|
878
|
+
}
|
|
879
|
+
|
|
880
|
+
item = item.upper()
|
|
881
|
+
|
|
882
|
+
if item not in supported_items:
|
|
883
|
+
raise ValueError(
|
|
884
|
+
f"Unsupported system information: {item}"
|
|
885
|
+
)
|
|
886
|
+
|
|
887
|
+
result = self._get_json({
|
|
888
|
+
"elm": item
|
|
889
|
+
})
|
|
890
|
+
|
|
891
|
+
try:
|
|
892
|
+
return result["SYSINFO"]
|
|
893
|
+
|
|
894
|
+
except (KeyError, TypeError) as error:
|
|
895
|
+
raise ValueError(
|
|
896
|
+
f"Invalid system information response: {item}"
|
|
897
|
+
) from error
|
|
898
|
+
|
|
899
|
+
def test_connection(self):
|
|
900
|
+
|
|
901
|
+
try:
|
|
902
|
+
|
|
903
|
+
info = self.get_system_info("VERSION")
|
|
904
|
+
|
|
905
|
+
version = info.get("VERS")
|
|
906
|
+
build = info.get("BUILD")
|
|
907
|
+
|
|
908
|
+
if not version or not build:
|
|
909
|
+
raise ValueError(
|
|
910
|
+
"Firmware information is incomplete"
|
|
911
|
+
)
|
|
912
|
+
|
|
913
|
+
return {
|
|
914
|
+
"connected": True,
|
|
915
|
+
"message": "easyE4 connection successful",
|
|
916
|
+
"firmware": version,
|
|
917
|
+
"build": build
|
|
918
|
+
}
|
|
919
|
+
|
|
920
|
+
except Exception as error:
|
|
921
|
+
|
|
922
|
+
return {
|
|
923
|
+
"connected": False,
|
|
924
|
+
"message": "easyE4 connection failed",
|
|
925
|
+
"error": str(error)
|
|
926
|
+
}
|