|
8 | 8 | * https://github.com/intrepidcs/python_ics
|
9 | 9 | """
|
10 | 10 |
|
| 11 | +import functools |
11 | 12 | import logging
|
12 | 13 | import os
|
13 | 14 | import tempfile
|
@@ -129,6 +130,27 @@ class ICSOperationError(ICSApiError, CanOperationError):
|
129 | 130 | pass
|
130 | 131 |
|
131 | 132 |
|
| 133 | +def check_if_bus_open(func): |
| 134 | + """ |
| 135 | + Decorator that checks if the bus is open before executing the function. |
| 136 | +
|
| 137 | + If the bus is not open, it raises a CanOperationError. |
| 138 | + """ |
| 139 | + |
| 140 | + @functools.wraps(func) |
| 141 | + def wrapper(self, *args, **kwargs): |
| 142 | + """ |
| 143 | + Wrapper function that checks if the bus is open before executing the function. |
| 144 | +
|
| 145 | + :raises CanOperationError: If the bus is not open. |
| 146 | + """ |
| 147 | + if self._is_shutdown: |
| 148 | + raise CanOperationError("Cannot operate on a closed bus") |
| 149 | + return func(self, *args, **kwargs) |
| 150 | + |
| 151 | + return wrapper |
| 152 | + |
| 153 | + |
132 | 154 | class NeoViBus(BusABC):
|
133 | 155 | """
|
134 | 156 | The CAN Bus implemented for the python_ics interface
|
@@ -312,6 +334,7 @@ def _find_device(self, type_filter=None, serial=None):
|
312 | 334 | msg.append("found.")
|
313 | 335 | raise CanInitializationError(" ".join(msg))
|
314 | 336 |
|
| 337 | + @check_if_bus_open |
315 | 338 | def _process_msg_queue(self, timeout=0.1):
|
316 | 339 | try:
|
317 | 340 | messages, errors = ics.get_messages(self.dev, False, timeout)
|
@@ -409,6 +432,7 @@ def _recv_internal(self, timeout=0.1):
|
409 | 432 | return None, False
|
410 | 433 | return msg, False
|
411 | 434 |
|
| 435 | + @check_if_bus_open |
412 | 436 | def send(self, msg, timeout=0):
|
413 | 437 | """Transmit a message to the CAN bus.
|
414 | 438 |
|
|
0 commit comments