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.
@@ -0,0 +1,15 @@
1
+ -----BEGIN CERTIFICATE-----
2
+ MIICVDCCAdugAwIBAgIUamSYpvEI2ZmJcDUP9oRrLCzZJEwwCgYIKoZIzj0EAwIw
3
+ dzEeMBwGA1UECgwVRWF0b24gSW5kdXN0cmllcyBHbWJIMSwwKgYDVQQLDCNDb3B5
4
+ cmlnaHQgKEMpIEVhdG9uIEluZHVzdHJpZXMgMjAyMjEaMBgGA1UEAwwRZWFzeSBS
5
+ b290IENBIFYxLjAxCzAJBgNVBAYTAkRFMCAXDTIyMDUxMTE1MDUxNFoYDzIwNzIw
6
+ NTA5MTUwNTE0WjB3MR4wHAYDVQQKDBVFYXRvbiBJbmR1c3RyaWVzIEdtYkgxLDAq
7
+ BgNVBAsMI0NvcHlyaWdodCAoQykgRWF0b24gSW5kdXN0cmllcyAyMDIyMRowGAYD
8
+ VQQDDBFlYXN5IFJvb3QgQ0EgVjEuMDELMAkGA1UEBhMCREUwdjAQBgcqhkjOPQIB
9
+ BgUrgQQAIgNiAAQTSBTDZhK4t7eGibWjayp5Ho2b+Qh5HabZg2Cs6fJjRn8HXH/M
10
+ CORBwedZJiGOO/Fmyno0Ap194Mzz/CzW2YQP8dm9QdsMiAvXoxf9MOU1OgwoFqCG
11
+ +bSBZJKldRs1v1WjJjAkMBIGA1UdEwEB/wQIMAYBAf8CAQIwDgYDVR0PAQH/BAQD
12
+ AgEGMAoGCCqGSM49BAMCA2cAMGQCMEteMKdKI61kbOzqHabU3vW+NImuU/Vw4wY4
13
+ 8calrx7W2EzcWNdqjrsifgP8HkkG7AIwR85rlrlhxdLeVyhUJeYEeAltBWJRhiYm
14
+ avsKIiX/NnAKQJPlBDHm01XC4NUu7c7a
15
+ -----END CERTIFICATE-----
easye4/client.py ADDED
@@ -0,0 +1,468 @@
1
+ import os
2
+ from pathlib import Path
3
+
4
+ from dotenv import load_dotenv
5
+
6
+ from .api import EasyE4API
7
+
8
+
9
+ class EasyE4:
10
+ """
11
+ Public interface for the Eaton easyE4 JSON API.
12
+
13
+ Provides connection management, system information,
14
+ marker memory reading and marker writing.
15
+ """
16
+
17
+ def __init__(
18
+ self,
19
+ ip,
20
+ port=443,
21
+ protocol="https",
22
+ timeout=5,
23
+ auth_method="none",
24
+ api_key=None,
25
+ username=None,
26
+ password=None,
27
+ tls_config=None
28
+ ):
29
+
30
+ self.api = EasyE4API(
31
+ ip=ip,
32
+ port=port,
33
+ protocol=protocol,
34
+ timeout=timeout,
35
+ auth_method=auth_method,
36
+ api_key=api_key,
37
+ username=username,
38
+ password=password,
39
+ tls_config=tls_config
40
+ )
41
+
42
+ # ==========================================
43
+ # CONNECTION MANAGEMENT
44
+ # ==========================================
45
+
46
+ def connect(self):
47
+ """
48
+ Test connection and authentication.
49
+ """
50
+
51
+ result = self.api.test_connection()
52
+
53
+ if not result["connected"]:
54
+ raise ConnectionError(
55
+ result.get(
56
+ "error",
57
+ "Unable to connect to easyE4"
58
+ )
59
+ )
60
+
61
+ return result
62
+
63
+ def disconnect(self):
64
+ """
65
+ Close the HTTP session.
66
+ """
67
+
68
+ self.api.close()
69
+
70
+ def is_connected(self):
71
+ """
72
+ Check whether the easyE4 responds.
73
+ """
74
+
75
+ result = self.api.test_connection()
76
+
77
+ return result["connected"]
78
+
79
+ # ==========================================
80
+ # SYSTEM INFORMATION
81
+ # ==========================================
82
+
83
+ def get_firmware(self):
84
+
85
+ info = self.api.get_system_info("VERSION")
86
+
87
+ return {
88
+ "version": info.get("VERS"),
89
+ "build": info.get("BUILD")
90
+ }
91
+
92
+ def get_state(self):
93
+
94
+ info = self.api.get_system_info("STATE")
95
+
96
+ return info.get("STATE")
97
+
98
+ def get_device_name(self):
99
+
100
+ info = self.api.get_system_info("DEVNAME")
101
+
102
+ return info.get("DEVNAME")
103
+
104
+ def get_program_name(self):
105
+
106
+ info = self.api.get_system_info("PROGNAME")
107
+
108
+ return info.get("PROGNAME")
109
+
110
+ # ==========================================
111
+ # DIGITAL INPUTS
112
+ # ==========================================
113
+
114
+ def read_digital_input(self, index):
115
+
116
+ return self.api.read_digital_inputs(
117
+ [index]
118
+ )[f"I{index}"]
119
+
120
+ def read_digital_inputs(self, indices):
121
+
122
+ return self.api.read_digital_inputs(indices)
123
+
124
+ # ==========================================
125
+ # DIGITAL OUTPUTS
126
+ # ==========================================
127
+
128
+ def read_digital_output(self, index):
129
+
130
+ return self.api.read_digital_outputs(
131
+ [index]
132
+ )[f"O{index}"]
133
+
134
+ def read_digital_outputs(self, indices):
135
+
136
+ return self.api.read_digital_outputs(indices)
137
+
138
+ # ==========================================
139
+ # ANALOG INPUTS
140
+ # ==========================================
141
+
142
+ def read_analog_input(self, index):
143
+
144
+ return self.api.read_analog_inputs(
145
+ [index]
146
+ )[f"AI{index}"]
147
+
148
+ def read_analog_inputs(self, indices):
149
+
150
+ return self.api.read_analog_inputs(indices)
151
+
152
+ # ==========================================
153
+ # ANALOG OUTPUTS
154
+ # ==========================================
155
+
156
+ def read_analog_output(self, index):
157
+
158
+ return self.api.read_analog_outputs(
159
+ [index]
160
+ )[f"AO{index}"]
161
+
162
+ def read_analog_outputs(self, indices):
163
+
164
+ return self.api.read_analog_outputs(indices)
165
+
166
+ # ==========================================
167
+ # COMBINED DIGITAL AND ANALOG I/O
168
+ # ==========================================
169
+
170
+ def read_io(
171
+ self,
172
+ digital_inputs=None,
173
+ digital_outputs=None,
174
+ analog_inputs=None,
175
+ analog_outputs=None
176
+ ):
177
+ """
178
+ Read selected digital and analog I/O operands.
179
+
180
+ Each requested operand category is retrieved
181
+ using one JSON API request.
182
+
183
+ Supported categories:
184
+ - I: Digital inputs
185
+ - O: Digital outputs
186
+ - AI: Analog inputs
187
+ - AO: Analog outputs
188
+
189
+ Returns a dictionary containing raw values.
190
+
191
+ No scaling factors are applied.
192
+ """
193
+
194
+ digital_inputs = (
195
+ [] if digital_inputs is None
196
+ else list(digital_inputs)
197
+ )
198
+
199
+ digital_outputs = (
200
+ [] if digital_outputs is None
201
+ else list(digital_outputs)
202
+ )
203
+
204
+ analog_inputs = (
205
+ [] if analog_inputs is None
206
+ else list(analog_inputs)
207
+ )
208
+
209
+ analog_outputs = (
210
+ [] if analog_outputs is None
211
+ else list(analog_outputs)
212
+ )
213
+
214
+ results = {}
215
+
216
+ if digital_inputs:
217
+ results.update(
218
+ self.read_digital_inputs(digital_inputs)
219
+ )
220
+
221
+ if digital_outputs:
222
+ results.update(
223
+ self.read_digital_outputs(digital_outputs)
224
+ )
225
+
226
+ if analog_inputs:
227
+ results.update(
228
+ self.read_analog_inputs(analog_inputs)
229
+ )
230
+
231
+ if analog_outputs:
232
+ results.update(
233
+ self.read_analog_outputs(analog_outputs)
234
+ )
235
+
236
+ return results
237
+
238
+ # ==========================================
239
+ # COMPLETE PLC SNAPSHOT
240
+ # ==========================================
241
+
242
+ def read_snapshot(
243
+ self,
244
+ digital_inputs=None,
245
+ digital_outputs=None,
246
+ analog_inputs=None,
247
+ analog_outputs=None,
248
+ markers=None,
249
+ words=None,
250
+ dwords=None
251
+ ):
252
+ """
253
+ Read selected I/O and marker memory operands.
254
+
255
+ Combines read_io() and read_memory() into one
256
+ convenient method.
257
+
258
+ Each requested operand category is retrieved
259
+ using one JSON API request.
260
+
261
+ Maximum: 5 requests (I, O, AI, AO, M).
262
+
263
+ Returns raw operand values without scaling.
264
+
265
+ Note:
266
+ The values are read sequentially and do not
267
+ represent an atomic PLC snapshot.
268
+ """
269
+
270
+ io_values = self.read_io(
271
+ digital_inputs=digital_inputs,
272
+ digital_outputs=digital_outputs,
273
+ analog_inputs=analog_inputs,
274
+ analog_outputs=analog_outputs
275
+ )
276
+
277
+ memory_values = self.read_memory(
278
+ markers=markers,
279
+ words=words,
280
+ dwords=dwords
281
+ )
282
+
283
+ return {
284
+ **io_values,
285
+ **memory_values
286
+ }
287
+
288
+ # ==========================================
289
+ # DIGITAL MARKERS
290
+ # ==========================================
291
+
292
+ def read_marker(self, index):
293
+
294
+ return self.api.read_markers(
295
+ [index]
296
+ )[f"M{index}"]
297
+
298
+ def read_markers(self, indices):
299
+
300
+ return self.api.read_markers(indices)
301
+
302
+ def write_marker(self, index, value):
303
+
304
+ return self.api.write_marker(
305
+ index,
306
+ value
307
+ )
308
+
309
+ # ==========================================
310
+ # MARKER WORDS
311
+ # ==========================================
312
+
313
+ def read_word(self, index):
314
+
315
+ return self.api.read_marker_word(index)
316
+
317
+ def write_word(self, index, value):
318
+
319
+ return self.api.write_marker_word(
320
+ index,
321
+ value
322
+ )
323
+
324
+ # ==========================================
325
+ # MARKER DOUBLE WORDS
326
+ # ==========================================
327
+
328
+ def read_dword(self, index):
329
+
330
+ return self.api.read_marker_dword(index)
331
+
332
+ # ==========================================
333
+ # COMBINED MEMORY READING
334
+ # ==========================================
335
+
336
+ def read_memory(
337
+ self,
338
+ markers=None,
339
+ words=None,
340
+ dwords=None
341
+ ):
342
+
343
+ return self.api.read_marker_memory(
344
+ markers=markers,
345
+ words=words,
346
+ dwords=dwords
347
+ )
348
+
349
+ # ==========================================
350
+ # ENVIRONMENT CONFIGURATION
351
+ # ==========================================
352
+
353
+ @classmethod
354
+ def from_env(cls, env_file=None):
355
+ """
356
+ Create an EasyE4 instance using environment
357
+ variables or a .env configuration file.
358
+ """
359
+
360
+ if env_file is None:
361
+ env_file = Path(__file__).resolve().parent.parent / ".env"
362
+
363
+ load_dotenv(
364
+ dotenv_path=env_file,
365
+ override=False
366
+ )
367
+
368
+ ip = os.getenv("EASYE4_IP")
369
+
370
+ if not ip:
371
+ raise ValueError(
372
+ "EASYE4_IP is not configured"
373
+ )
374
+
375
+ port = int(
376
+ os.getenv("EASYE4_PORT", "443")
377
+ )
378
+
379
+ protocol = os.getenv(
380
+ "EASYE4_PROTOCOL",
381
+ "https"
382
+ )
383
+
384
+ timeout = float(
385
+ os.getenv("EASYE4_TIMEOUT", "5")
386
+ )
387
+
388
+ auth_method = os.getenv(
389
+ "EASYE4_AUTH_METHOD",
390
+ "none"
391
+ ).lower()
392
+
393
+ api_key = os.getenv("EASYE4_API_KEY")
394
+ username = os.getenv("EASYE4_USERNAME")
395
+ password = os.getenv("EASYE4_PASSWORD")
396
+
397
+ ca_file = os.getenv("EASYE4_CA_FILE", "").strip()
398
+
399
+ # Use bundled Eaton certificate when no custom CA is specified
400
+ if not ca_file:
401
+ bundled_certificate = (
402
+ Path(__file__).resolve().parent
403
+ / "certificates"
404
+ / "easyRootCertV1.crt"
405
+ )
406
+
407
+ if bundled_certificate.is_file():
408
+ ca_file = str(bundled_certificate)
409
+
410
+ compatibility_mode = (
411
+ os.getenv(
412
+ "EASYE4_TLS_COMPATIBILITY",
413
+ "false"
414
+ ).lower() == "true"
415
+ )
416
+
417
+ tls_config = {
418
+ "ca_mode": "system",
419
+ "ca_file": "",
420
+ "compatibility_mode": False
421
+ }
422
+
423
+ if ca_file:
424
+
425
+ certificate_path = Path(ca_file)
426
+
427
+ if not certificate_path.is_absolute():
428
+ certificate_path = (
429
+ Path(env_file).resolve().parent
430
+ / certificate_path
431
+ )
432
+
433
+ tls_config = {
434
+ "ca_mode": "custom",
435
+ "ca_file": str(certificate_path),
436
+ "compatibility_mode": compatibility_mode
437
+ }
438
+
439
+ return cls(
440
+ ip=ip,
441
+ port=port,
442
+ protocol=protocol,
443
+ timeout=timeout,
444
+ auth_method=auth_method,
445
+ api_key=api_key,
446
+ username=username,
447
+ password=password,
448
+ tls_config=tls_config
449
+ )
450
+
451
+ # ==========================================
452
+ # CONTEXT MANAGER
453
+ # ==========================================
454
+
455
+ def __enter__(self):
456
+
457
+ return self
458
+
459
+ def __exit__(
460
+ self,
461
+ exc_type,
462
+ exc_value,
463
+ traceback
464
+ ):
465
+
466
+ self.disconnect()
467
+
468
+ return False