|
| 1 | +# ************************************************************************************** |
| 2 | + |
| 3 | +# @package samps |
| 4 | +# @license MIT License Copyright (c) 2025 Michael J. Roberts |
| 5 | + |
| 6 | +# ************************************************************************************** |
| 7 | + |
| 8 | + |
| 9 | +from time import monotonic |
| 10 | +from typing import Optional |
| 11 | + |
| 12 | +# ************************************************************************************** |
| 13 | + |
| 14 | + |
| 15 | +class ReadTimeoutHandler: |
| 16 | + """ |
| 17 | + A handler for managing read timeouts in serial communication. |
| 18 | +
|
| 19 | + Tracks a timeout period in milliseconds, provides methods to check |
| 20 | + whether the timeout has expired, retrieve the remaining time, |
| 21 | + start or reset the timer, and a repr string. |
| 22 | + """ |
| 23 | + |
| 24 | + def __init__(self, timeout: Optional[float]) -> None: |
| 25 | + """ |
| 26 | + Initialize with a timeout value in milliseconds. |
| 27 | +
|
| 28 | + Args: |
| 29 | + timeout: timeout duration in milliseconds, or None to disable. |
| 30 | + """ |
| 31 | + self._timeout = timeout |
| 32 | + |
| 33 | + def start(self) -> None: |
| 34 | + """ |
| 35 | + Restart the timeout countdown from the current moment. |
| 36 | + """ |
| 37 | + self._start = monotonic() |
| 38 | + |
| 39 | + def has_expired(self) -> bool: |
| 40 | + """ |
| 41 | + Return True if the elapsed time since start() has reached |
| 42 | + or exceeded the timeout value. |
| 43 | + """ |
| 44 | + # If no timeout is set, return None: |
| 45 | + if self._timeout is None: |
| 46 | + return False |
| 47 | + |
| 48 | + # If the timer hasn't started, we can't calculate remaining time: |
| 49 | + if self._start is None: |
| 50 | + raise RuntimeError("Timeout not started. Call start() first.") |
| 51 | + |
| 52 | + return ((monotonic() - self._start) * 1000) >= self._timeout |
| 53 | + |
| 54 | + def remaining(self) -> Optional[float]: |
| 55 | + """ |
| 56 | + Return the number of milliseconds left before expiration, |
| 57 | + never negative. |
| 58 | +
|
| 59 | + Returns: |
| 60 | + The remaining time in milliseconds, or None if timeouts are disabled. |
| 61 | + """ |
| 62 | + # If no timeout is set, return None: |
| 63 | + if self._timeout is None: |
| 64 | + return None |
| 65 | + |
| 66 | + # If the timer hasn't started, we can't calculate remaining time: |
| 67 | + if self._start is None: |
| 68 | + raise RuntimeError("Timeout not started. Call start() first.") |
| 69 | + |
| 70 | + remaining = self._timeout - (monotonic() - self._start) * 1000 |
| 71 | + return remaining if remaining > 0 else 0.0 |
| 72 | + |
| 73 | + def reset(self) -> None: |
| 74 | + """ |
| 75 | + Alias for start(): restart the timeout countdown. |
| 76 | + """ |
| 77 | + self.start() |
| 78 | + |
| 79 | + def __repr__(self) -> str: |
| 80 | + # If the timeout is None, we can't calculate remaining time: |
| 81 | + if self._timeout is None: |
| 82 | + return "<ReadTimeoutHandler timeout=None>" |
| 83 | + |
| 84 | + # If the timer hasn't started, we can't calculate remaining time: |
| 85 | + if self._start is None: |
| 86 | + return f"<ReadTimeoutHandler timeout={self._timeout:.0f}ms, remaining=NotStarted>" |
| 87 | + |
| 88 | + # Otherwise, return back the remaining time: |
| 89 | + return ( |
| 90 | + f"<ReadTimeoutHandler timeout={self._timeout:.0f}ms, " |
| 91 | + f"remaining={self.remaining():.0f}ms>" |
| 92 | + ) |
| 93 | + |
| 94 | + |
| 95 | +# ************************************************************************************** |
0 commit comments