Skip to content

Commit

Permalink
Rename to _UninitializedNetwork, use singleton instance.
Browse files Browse the repository at this point in the history
  • Loading branch information
acolomb committed Aug 12, 2024
1 parent 378835e commit 24560d0
Show file tree
Hide file tree
Showing 9 changed files with 24 additions and 20 deletions.
2 changes: 1 addition & 1 deletion canopen/emcy.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ def wait(
class EmcyProducer:

def __init__(self, cob_id: int):
self.network: canopen.network.Network = canopen.network._DummyNetwork()
self.network: canopen.network.Network = canopen.network._UNINITIALIZED_NETWORK
self.cob_id = cob_id

def send(self, code: int, register: int = 0, data: bytes = b""):
Expand Down
2 changes: 1 addition & 1 deletion canopen/lss.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ class LssMaster:
RESPONSE_TIMEOUT = 0.5

def __init__(self) -> None:
self.network: canopen.network.Network = canopen.network._DummyNetwork()
self.network: canopen.network.Network = canopen.network._UNINITIALIZED_NETWORK
self._node_id = 0
self._data = None
self.responses = queue.Queue()
Expand Down
6 changes: 5 additions & 1 deletion canopen/network.py
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@ def __len__(self) -> int:
return len(self.nodes)


class _DummyNetwork(Network):
class _UninitializedNetwork(Network):
"""Empty network implementation as a placeholder before actual initialization."""

def __init__(self, bus: Optional[can.BusABC] = None):
Expand All @@ -290,6 +290,10 @@ def __getattribute__(self, name):
"try associating to a real network first.")


#: Singleton instance
_UNINITIALIZED_NETWORK = _UninitializedNetwork()


class PeriodicMessageTask:
"""
Task object to transmit a message periodically using python-can's
Expand Down
2 changes: 1 addition & 1 deletion canopen/nmt.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ class NmtBase:

def __init__(self, node_id: int):
self.id = node_id
self.network: canopen.network.Network = canopen.network._DummyNetwork()
self.network: canopen.network.Network = canopen.network._UNINITIALIZED_NETWORK
self._state = 0

def on_command(self, can_id, data, timestamp):
Expand Down
4 changes: 2 additions & 2 deletions canopen/node/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def __init__(
node_id: int,
object_dictionary: Union[ObjectDictionary, str, TextIO],
):
self.network: canopen.network.Network = canopen.network._DummyNetwork()
self.network: canopen.network.Network = canopen.network._UNINITIALIZED_NETWORK

if not isinstance(object_dictionary, ObjectDictionary):
object_dictionary = import_od(object_dictionary, node_id)
Expand All @@ -29,4 +29,4 @@ def __init__(

def has_network(self) -> bool:
"""Check whether the node has been associated to a network."""
return not isinstance(self.network, canopen.network._DummyNetwork)
return not isinstance(self.network, canopen.network._UninitializedNetwork)
12 changes: 6 additions & 6 deletions canopen/node/local.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,12 @@ def associate_network(self, network: canopen.network.Network):
def remove_network(self) -> None:
self.network.unsubscribe(self.sdo.rx_cobid, self.sdo.on_request)
self.network.unsubscribe(0, self.nmt.on_command)
self.network = canopen.network._DummyNetwork()
self.sdo.network = self.network
self.tpdo.network = self.network
self.rpdo.network = self.network
self.nmt.network = self.network
self.emcy.network = self.network
self.network = canopen.network._UNINITIALIZED_NETWORK
self.sdo.network = canopen.network._UNINITIALIZED_NETWORK
self.tpdo.network = canopen.network._UNINITIALIZED_NETWORK
self.rpdo.network = canopen.network._UNINITIALIZED_NETWORK
self.nmt.network = canopen.network._UNINITIALIZED_NETWORK
self.emcy.network = canopen.network._UNINITIALIZED_NETWORK

def add_read_callback(self, callback):
self._read_callbacks.append(callback)
Expand Down
12 changes: 6 additions & 6 deletions canopen/node/remote.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,12 @@ def remove_network(self) -> None:
self.network.unsubscribe(0x700 + self.id, self.nmt.on_heartbeat)
self.network.unsubscribe(0x80 + self.id, self.emcy.on_emcy)
self.network.unsubscribe(0, self.nmt.on_command)
self.network = canopen.network._DummyNetwork()
self.sdo.network = self.network
self.pdo.network = self.network
self.tpdo.network = self.network
self.rpdo.network = self.network
self.nmt.network = self.network
self.network = canopen.network._UNINITIALIZED_NETWORK
self.sdo.network = canopen.network._UNINITIALIZED_NETWORK
self.pdo.network = canopen.network._UNINITIALIZED_NETWORK
self.tpdo.network = canopen.network._UNINITIALIZED_NETWORK
self.rpdo.network = canopen.network._UNINITIALIZED_NETWORK
self.nmt.network = canopen.network._UNINITIALIZED_NETWORK

def add_sdo(self, rx_cobid, tx_cobid):
"""Add an additional SDO channel.
Expand Down
2 changes: 1 addition & 1 deletion canopen/pdo/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class PdoBase(Mapping):
"""

def __init__(self, node: Union[LocalNode, RemoteNode]):
self.network: canopen.network.Network = canopen.network._DummyNetwork()
self.network: canopen.network.Network = canopen.network._UNINITIALIZED_NETWORK
self.map: Optional[PdoMaps] = None
self.node: Union[LocalNode, RemoteNode] = node

Expand Down
2 changes: 1 addition & 1 deletion canopen/sdo/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ def __init__(
"""
self.rx_cobid = rx_cobid
self.tx_cobid = tx_cobid
self.network: canopen.network.Network = canopen.network._DummyNetwork()
self.network: canopen.network.Network = canopen.network._UNINITIALIZED_NETWORK
self.od = od

def __getitem__(
Expand Down

0 comments on commit 24560d0

Please sign in to comment.