|
1 | 1 | import random |
| 2 | +import selectors |
2 | 3 | import socket |
3 | 4 | import struct |
4 | 5 |
|
|
16 | 17 |
|
17 | 18 | class PTC: |
18 | 19 | def __init__(self, ip_address, port=502, timeout=10, fake_errors=False): |
19 | | - self.ip_address = ip_address |
20 | | - self.port = int(port) |
21 | 20 | self.fake_errors = fake_errors |
22 | 21 |
|
23 | 22 | self.model = None |
24 | 23 | self.serial = None |
25 | 24 | self.software_revision = None |
26 | 25 |
|
27 | | - self.comm = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
28 | | - self.comm.connect((self.ip_address, self.port)) # connects to the PTC |
29 | | - self.comm.settimeout(timeout) |
| 26 | + self.ip_address = ip_address |
| 27 | + self.port = int(port) |
| 28 | + self.timeout = timeout |
| 29 | + self.comm = self._connect((self.ip_address, self.port)) |
| 30 | + |
| 31 | + def _connect(self, address): |
| 32 | + sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
| 33 | + sock.settimeout(self.timeout) |
| 34 | + try: |
| 35 | + sock.connect(address) |
| 36 | + except TimeoutError: |
| 37 | + print(f"Connection not established within {self.timeout}.") |
| 38 | + return |
| 39 | + except OSError as e: |
| 40 | + print(f"Unable to connect. {e}") |
| 41 | + return |
| 42 | + except Exception as e: |
| 43 | + print(f"Caught unexpected {type(e).__name__} while connecting:") |
| 44 | + print(f" {e}") |
| 45 | + return |
| 46 | + return sock |
| 47 | + |
| 48 | + def reset(self): |
| 49 | + print("Resetting the connection to the compressor.") |
| 50 | + self.comm = self._connect((self.ip_address, self.port)) |
| 51 | + |
| 52 | + def _write(self, msg): |
| 53 | + if self.comm is None: |
| 54 | + print("Connection not established. Unable to send command.") |
| 55 | + self.reset() |
| 56 | + return |
| 57 | + |
| 58 | + try: |
| 59 | + self.comm.sendall(msg) |
| 60 | + return |
| 61 | + except (BrokenPipeError, ConnectionResetError) as e: |
| 62 | + print(f"Connection error: {e}") |
| 63 | + self.reset() |
| 64 | + except TimeoutError as e: |
| 65 | + print(f"Timeout error while writing: {e}") |
| 66 | + self.reset() |
| 67 | + except Exception as e: |
| 68 | + print(f"Caught unexpected {type(e).__name__} during write:") |
| 69 | + print(f" {e}") |
| 70 | + self.reset() |
| 71 | + |
| 72 | + # Try a second time before giving up |
| 73 | + try: |
| 74 | + self.comm.sendall(msg) |
| 75 | + except (BrokenPipeError, ConnectionResetError) as e: |
| 76 | + print(f"Connection error: {e}") |
| 77 | + raise ConnectionError |
| 78 | + except TimeoutError as e: |
| 79 | + print(f"Timeout error while writing: {e}") |
| 80 | + raise ConnectionError |
| 81 | + except AttributeError: |
| 82 | + raise ConnectionError("Unable to reset connection.") |
| 83 | + except Exception as e: |
| 84 | + print(f"Caught unexpected {type(e).__name__} during write:") |
| 85 | + print(f" {e}") |
| 86 | + raise ConnectionError |
| 87 | + |
| 88 | + def _check_ready(self): |
| 89 | + """Check socket is ready to read from.""" |
| 90 | + if self.comm is None: |
| 91 | + raise ConnectionError("Connection not established, not ready to read.") |
| 92 | + |
| 93 | + sel = selectors.DefaultSelector() |
| 94 | + sel.register(self.comm, selectors.EVENT_READ) |
| 95 | + if not sel.select(self.timeout): |
| 96 | + raise ConnectionError |
| 97 | + |
| 98 | + def _read(self): |
| 99 | + self._check_ready() |
| 100 | + data = self.comm.recv(1024) |
| 101 | + return data |
30 | 102 |
|
31 | 103 | def get_data(self): |
32 | 104 | """ |
33 | 105 | Gets the raw data from the ptc and returns it in a usable format. |
34 | 106 | """ |
35 | | - self.comm.sendall(self.buildRegistersQuery()) |
36 | | - data = self.comm.recv(1024) |
| 107 | + self._write(self.buildRegistersQuery()) |
| 108 | + data = self._read() |
37 | 109 | data_flag, brd = self.breakdownReplyData(data) |
38 | 110 |
|
39 | 111 | return data_flag, brd |
|
0 commit comments