Skip to content

Commit bd749cd

Browse files
committed
Fixup the canopen connect mechanism for async
* Move loop init to the Network * Remove unused callbacks in LocalNode
1 parent 59a7643 commit bd749cd

File tree

2 files changed

+16
-24
lines changed

2 files changed

+16
-24
lines changed

canopen/network.py

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@ class Network(MutableMapping):
3333
NOTIFIER_CYCLE: float = 1.0 #: Maximum waiting time for one notifier iteration.
3434
NOTIFIER_SHUTDOWN_TIMEOUT: float = 5.0 #: Maximum waiting time to stop notifiers.
3535

36-
def __init__(self, bus: Optional[can.BusABC] = None, loop: Optional[AbstractEventLoop] = None):
36+
def __init__(self, bus: Optional[can.BusABC] = None, notifier: Optional[can.Notifier] = None,
37+
loop: Optional[AbstractEventLoop] = None):
3738
"""
3839
:param can.BusABC bus:
3940
A python-can bus instance to re-use.
@@ -47,7 +48,7 @@ def __init__(self, bus: Optional[can.BusABC] = None, loop: Optional[AbstractEven
4748
#: List of :class:`can.Listener` objects.
4849
#: Includes at least MessageListener.
4950
self.listeners = [MessageListener(self)]
50-
self.notifier: Optional[can.Notifier] = None
51+
self.notifier: Optional[can.Notifier] = notifier
5152
self.nodes: Dict[int, Union[RemoteNode, LocalNode]] = {}
5253
self.subscribers: Dict[int, List[Callback]] = {}
5354
self.send_lock = threading.Lock()
@@ -59,6 +60,11 @@ def __init__(self, bus: Optional[can.BusABC] = None, loop: Optional[AbstractEven
5960
self.lss = LssMaster()
6061
self.lss.network = self
6162

63+
# Register this function as the means to check if canopen is run in
64+
# async mode. This enables the @ensure_not_async() decorator to
65+
# work. See async_guard.py
66+
set_async_sentinel(self.is_async)
67+
6268
if self.is_async():
6369
self.subscribe(self.lss.LSS_RX_COBID, self.lss.aon_message_received)
6470
else:
@@ -117,19 +123,13 @@ def connect(self, *args, **kwargs) -> Network:
117123
if node.object_dictionary.bitrate:
118124
kwargs["bitrate"] = node.object_dictionary.bitrate
119125
break
120-
# The optional loop parameter goes to can.Notifier()
121-
kwargs_notifier = {}
122-
if "loop" in kwargs:
123-
self.loop = kwargs.pop("loop")
124-
kwargs_notifier["loop"] = self.loop
125-
# Register this function as the means to check if canopen is run in
126-
# async mode. This enables the @ensure_not_async() decorator to
127-
# work. See async_guard.py
128-
set_async_sentinel(self.is_async)
129126
if self.bus is None:
130127
self.bus = can.Bus(*args, **kwargs)
131128
logger.info("Connected to '%s'", self.bus.channel_info)
132-
self.notifier = can.Notifier(self.bus, self.listeners, self.NOTIFIER_CYCLE, **kwargs_notifier)
129+
if self.notifier is None:
130+
self.notifier = can.Notifier(self.bus, [], self.NOTIFIER_CYCLE, loop=self.loop)
131+
for listener in self.listeners:
132+
self.notifier.add_listener(listener)
133133
return self
134134

135135
def disconnect(self) -> None:

canopen/node/local.py

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -45,21 +45,13 @@ def associate_network(self, network: Network):
4545
self.rpdo.network = network
4646
self.nmt.network = network
4747
self.emcy.network = network
48-
if network.is_async():
49-
network.subscribe(self.sdo.rx_cobid, self.sdo.aon_request)
50-
network.subscribe(0, self.nmt.aon_command)
51-
else:
52-
network.subscribe(self.sdo.rx_cobid, self.sdo.on_request)
53-
network.subscribe(0, self.nmt.on_command)
48+
network.subscribe(self.sdo.rx_cobid, self.sdo.on_request)
49+
network.subscribe(0, self.nmt.on_command)
5450

5551
def remove_network(self):
5652
network = self.network
57-
if network.is_async():
58-
network.unsubscribe(self.sdo.rx_cobid, self.sdo.aon_request)
59-
network.unsubscribe(0, self.nmt.aon_command)
60-
else:
61-
network.unsubscribe(self.sdo.rx_cobid, self.sdo.on_request)
62-
network.unsubscribe(0, self.nmt.on_command)
53+
network.unsubscribe(self.sdo.rx_cobid, self.sdo.on_request)
54+
network.unsubscribe(0, self.nmt.on_command)
6355
self.network = None
6456
self.sdo.network = None
6557
self.tpdo.network = None

0 commit comments

Comments
 (0)