class TestMessageFiltering(unittest.TestCase): def setUp(self): self.bus = Bus(bustype='virtual', channel='testy') def tearDown(self): self.bus.shutdown() def test_match_all(self): # explicitly self.bus.set_filters() self.assertTrue(self.bus._matches_filters(EXAMPLE_MSG)) # implicitly self.bus.set_filters(None) self.assertTrue(self.bus._matches_filters(EXAMPLE_MSG)) def test_match_filters_is_empty(self): self.bus.set_filters([]) for msg in TEST_ALL_MESSAGES: self.assertTrue(self.bus._matches_filters(msg)) def test_match_example_message(self): self.bus.set_filters(MATCH_EXAMPLE) self.assertTrue(self.bus._matches_filters(EXAMPLE_MSG)) self.assertFalse(self.bus._matches_filters(HIGHEST_MSG)) self.bus.set_filters(MATCH_ONLY_HIGHEST) self.assertFalse(self.bus._matches_filters(EXAMPLE_MSG)) self.assertTrue(self.bus._matches_filters(HIGHEST_MSG))
class PythonCAN: """ """ def __init__(self, bustype): self.bustype = bustype self.connected = False def init(self, parent, receive_callback): self.parent = parent def connect(self): self.kwargs = OrderedDict() # Fetch driver keyword arguments. self._fetch_kwargs(False) self._fetch_kwargs(True) can_id = self.parent.can_id_master can_filter = { "can_id": can_id.id, "can_mask": can.MAX_29_BIT_IDENTIFIER if can_id.is_extended else can.MAX_11_BIT_IDENTIFIER, "extended": can_id.is_extended } self.bus = Bus(bustype=self.bustype, **self.kwargs) self.bus.set_filters([can_filter]) self.parent.logger.debug("Python-CAN driver: {} - {}]".format( self.bustype, self.bus)) self.connected = True def _fetch_kwargs(self, local): if local: base = self else: base = self.parent for param, arg in base.PARAMETER_TO_KW_ARG_MAP.items(): value = base.config.get(param) #if param == "CHANNEL": # value = self._handle_channel(value) self.kwargs[arg] = value def _handle_channel(self, value): match = NUMBER.match(value) if match: gd = match.groupdict() base = 16 if not gd["hex"] is None else 10 return int(value, base) else: return value def close(self): self.connected = False def transmit(self, payload): frame = Message(arbitration_id=self.parent.can_id_slave.id, is_extended_id=True if self.parent.can_id_slave.is_extended else False, data=payload) self.bus.send(frame) def read(self): if not self.connected: return None try: frame = self.bus.recv(5) except CanError: return None else: if frame is None: return None # Timeout condition. extended = frame.is_extended_id identifier = can.Identifier.make_identifier( frame.arbitration_id, extended) return can.Frame(id_=identifier, dlc=frame.dlc, data=frame.data, timestamp=frame.timestamp) def getTimestampResolution(self): return 10 * 1000