|
| 1 | +''' |
| 2 | +This Driver was tested on a FL SWITCH 2303-8SP1 with FW-version 3.27.01 BETA |
| 3 | +file phoenixcontact_fl_2300.py |
| 4 | +author Raffael Krakau |
| 5 | +date 2023-08-24 |
| 6 | +
|
| 7 | +Copyright 2023 JUMO GmbH & Co. KG |
| 8 | +''' |
| 9 | +import pexpect |
| 10 | + |
| 11 | +PORT = 23 |
| 12 | + |
| 13 | + |
| 14 | +def __login_telnet(tn): |
| 15 | + """ |
| 16 | + Login user with set credentials |
| 17 | +
|
| 18 | + @param tn : pyexpect-telnet-object |
| 19 | + """ |
| 20 | + username = "admin" |
| 21 | + password = "private" |
| 22 | + |
| 23 | + # login user with password |
| 24 | + tn.expect(b'User: ') |
| 25 | + tn.send(bytes(f'{username}\r\n', "utf-8")) |
| 26 | + tn.expect(b'Password: ') |
| 27 | + tn.send(bytes(f'{password}\r\n', "utf-8")) |
| 28 | + |
| 29 | + |
| 30 | +def power_set(host, port, index: int, value: bool): |
| 31 | + """ |
| 32 | + Set power state by socket port number (e.g. 1 - 8) and an value {'enable', 'disable'}. |
| 33 | +
|
| 34 | + - values: |
| 35 | + - disable(False): Turn OFF, |
| 36 | + - enable(True): Turn ON |
| 37 | + """ |
| 38 | + action = "enable" if value else "disable" |
| 39 | + |
| 40 | + with pexpect.spawn(f"telnet {host} {port}", timeout=1) as tn: |
| 41 | + # login user with password |
| 42 | + __login_telnet(tn) |
| 43 | + |
| 44 | + # set value |
| 45 | + tn.send(f'pse port {index} power {action}\r\n'.encode()) |
| 46 | + |
| 47 | + tn.expect(b'OK') |
| 48 | + |
| 49 | + tn.send(b"quit\r\n") |
| 50 | + tn.expect(pexpect.EOF) |
| 51 | + |
| 52 | + |
| 53 | +def power_get(host, port, index: int) -> bool: |
| 54 | + """ |
| 55 | + Get current state of a given socket number. |
| 56 | + - host: spe-switch-device adress |
| 57 | + - port: standard is 23 |
| 58 | + - index: depends on spe-switch-device 1-n (n is the number of spe-switch-ports) |
| 59 | + """ |
| 60 | + status = None |
| 61 | + |
| 62 | + with pexpect.spawn(f"telnet {host} {port}", timeout=1) as tn: |
| 63 | + # login user with password |
| 64 | + __login_telnet(tn) |
| 65 | + |
| 66 | + # get value |
| 67 | + tn.send(bytes(f'show pse port port-no {index}\r\n', "utf-8")) |
| 68 | + |
| 69 | + status = tn.expect(['disable', 'enable']) |
| 70 | + |
| 71 | + tn.send(b"quit\r\n") |
| 72 | + tn.expect(pexpect.EOF) |
| 73 | + |
| 74 | + return True if status == 1 else False |
0 commit comments