Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Network is mixing roles of network access and canopen services #574

Closed
@sveinse

Description

@sveinse

The class Network is an abstraction that provides access to the CAN Bus. The current implementation is also assuming additional canopen roles in the same class. The user have no way to avoid that. It instantiates NmtMaster, LssMaster, SyncProducer, TimeProducer. All of them are in the current implementation passive, as in, they do not initiate any traffic on their own. This implementation is mixing the role of being a network interface class and providing canopen master functions. As an example, if network is going to be used for passive listening or slave nodes, one would not want NmtMaster and LssMaster.

I propose moving the initializations of SyncProducer, TimeProducer, NmtMaster and LssMaster to a separate method Network.create_manager() (manager is the new official name for master in canopen) with a function argument to __init__() to prevent creation of them. With this the user can opt out of the additional services.

Something like this:

    # From class Network:
    def __init__(self, bus: Optional[can.BusABC] = None, create_manager = True):
        if create_manager:
            self.create_manager()

    def create_manager(self):
        self.sync = SyncProducer(self)
        self.time = TimeProducer(self)
        self.nmt = NmtMaster(0)
        self.nmt.network = self
        self.lss = LssMaster()
        self.lss.network = self
        self.subscribe(self.lss.LSS_RX_COBID, self.lss.on_message_received)

def __init__(self, bus: Optional[can.BusABC] = None):
"""
:param can.BusABC bus:
A python-can bus instance to re-use.
"""
#: A python-can :class:`can.BusABC` instance which is set after
#: :meth:`canopen.Network.connect` is called
self.bus = bus
#: A :class:`~canopen.network.NodeScanner` for detecting nodes
self.scanner = NodeScanner(self)
#: List of :class:`can.Listener` objects.
#: Includes at least MessageListener.
self.listeners = [MessageListener(self)]
self.notifier: Optional[can.Notifier] = None
self.nodes: Dict[int, Union[RemoteNode, LocalNode]] = {}
self.subscribers: Dict[int, List[Callback]] = {}
self.send_lock = threading.Lock()
self.sync = SyncProducer(self)
self.time = TimeProducer(self)
self.nmt = NmtMaster(0)
self.nmt.network = self
self.lss = LssMaster()
self.lss.network = self
self.subscribe(self.lss.LSS_RX_COBID, self.lss.on_message_received)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions