Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a dummy object to designate "uninitialized" networks (fixes #511) #525

Open
wants to merge 16 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 13 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion canopen/emcy.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
import time
from typing import Callable, List, Optional

import canopen.network


# Error code, error register, vendor specific data
EMCY_STRUCT = struct.Struct("<HB5s")

Expand Down Expand Up @@ -82,7 +85,7 @@ def wait(
class EmcyProducer:

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

def send(self, code: int, register: int = 0, data: bytes = b""):
Expand Down
7 changes: 5 additions & 2 deletions canopen/lss.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
import struct
import queue

import canopen.network


logger = logging.getLogger(__name__)

# Command Specifier (CS)
Expand Down Expand Up @@ -78,8 +81,8 @@ class LssMaster:
#: Max time in seconds to wait for response from server
RESPONSE_TIMEOUT = 0.5

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


class _DummyNetwork(Network):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure about the naming. I'd prefer if the naming more closely reflected it's purpose:

  • _NetworkPlaceholder
  • _UninitializedNetwork
  • _EmptyNetwork

"""Empty network implementation as a placeholder before actual initialization."""

def __init__(self, bus: Optional[can.BusABC] = None):
"""Do not initialize attributes, by skipping the parent constructor."""

def __getattribute__(self, name):
raise RuntimeError("No actual Network object was assigned, "
"try associating to a real network first.")


class PeriodicMessageTask:
"""
Task object to transmit a message periodically using python-can's
Expand Down
10 changes: 8 additions & 2 deletions canopen/nmt.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,13 @@
import logging
import struct
import time
from typing import Callable, Optional
from typing import Callable, Optional, TYPE_CHECKING

import canopen.network

if TYPE_CHECKING:
from canopen.network import PeriodicMessageTask


logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -45,7 +51,7 @@ class NmtBase:

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

def on_command(self, can_id, data, timestamp):
Expand Down
8 changes: 7 additions & 1 deletion canopen/node/base.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
from typing import TextIO, Union

import canopen.network
from canopen.objectdictionary import ObjectDictionary, import_od


Expand All @@ -17,10 +19,14 @@ def __init__(
node_id: int,
object_dictionary: Union[ObjectDictionary, str, TextIO],
):
self.network = None
self.network: canopen.network.Network = canopen.network._DummyNetwork()

if not isinstance(object_dictionary, ObjectDictionary):
object_dictionary = import_od(object_dictionary, node_id)
self.object_dictionary = object_dictionary

self.id = node_id or self.object_dictionary.node_id

def has_network(self) -> bool:
"""Check whether the node has been associated to a network."""
return not isinstance(self.network, canopen.network._DummyNetwork)
19 changes: 11 additions & 8 deletions canopen/node/local.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
from __future__ import annotations

import logging
from typing import Dict, Union

import canopen.network
from canopen.node.base import BaseNode
from canopen.sdo import SdoServer, SdoAbortedError
from canopen.pdo import PDO, TPDO, RPDO
Expand Down Expand Up @@ -34,7 +37,7 @@ def __init__(
self.add_write_callback(self.nmt.on_write)
self.emcy = EmcyProducer(0x80 + self.id)

def associate_network(self, network):
def associate_network(self, network: canopen.network.Network):
self.network = network
self.sdo.network = network
self.tpdo.network = network
Expand All @@ -44,15 +47,15 @@ def associate_network(self, network):
network.subscribe(self.sdo.rx_cobid, self.sdo.on_request)
network.subscribe(0, self.nmt.on_command)

def remove_network(self):
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 = None
self.sdo.network = None
self.tpdo.network = None
self.rpdo.network = None
self.nmt.network = None
self.emcy.network = None
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
acolomb marked this conversation as resolved.
Show resolved Hide resolved

def add_read_callback(self, callback):
self._read_callbacks.append(callback)
Expand Down
21 changes: 12 additions & 9 deletions canopen/node/remote.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
from __future__ import annotations

import logging
from typing import Union, TextIO

import canopen.network
from canopen.sdo import SdoClient, SdoCommunicationError, SdoAbortedError
from canopen.nmt import NmtMaster
from canopen.emcy import EmcyConsumer
Expand Down Expand Up @@ -46,7 +49,7 @@ def __init__(
if load_od:
self.load_configuration()

def associate_network(self, network):
def associate_network(self, network: canopen.network.Network):
self.network = network
self.sdo.network = network
self.pdo.network = network
Expand All @@ -59,18 +62,18 @@ def associate_network(self, network):
network.subscribe(0x80 + self.id, self.emcy.on_emcy)
network.subscribe(0, self.nmt.on_command)

def remove_network(self):
def remove_network(self) -> None:
for sdo in self.sdo_channels:
self.network.unsubscribe(sdo.tx_cobid, sdo.on_response)
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 = None
self.sdo.network = None
self.pdo.network = None
self.tpdo.network = None
self.rpdo.network = None
self.nmt.network = None
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

def add_sdo(self, rx_cobid, tx_cobid):
"""Add an additional SDO channel.
Expand All @@ -87,7 +90,7 @@ def add_sdo(self, rx_cobid, tx_cobid):
"""
client = SdoClient(rx_cobid, tx_cobid, self.object_dictionary)
self.sdo_channels.append(client)
if self.network is not None:
if self.has_network():
self.network.subscribe(client.tx_cobid, client.on_response)
return client

Expand Down
4 changes: 2 additions & 2 deletions canopen/pdo/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@
import logging
import binascii

import canopen.network
from canopen.sdo import SdoAbortedError
from canopen import objectdictionary
from canopen import variable

if TYPE_CHECKING:
from canopen.network import Network
from canopen import LocalNode, RemoteNode
from canopen.pdo import RPDO, TPDO
from canopen.sdo import SdoRecord
Expand All @@ -30,7 +30,7 @@ class PdoBase(Mapping):
"""

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

Expand Down
3 changes: 2 additions & 1 deletion canopen/sdo/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from typing import Iterator, Optional, Union
from collections.abc import Mapping

import canopen.network
from canopen import objectdictionary
from canopen import variable
from canopen.utils import pretty_index
Expand Down Expand Up @@ -43,7 +44,7 @@ def __init__(
"""
self.rx_cobid = rx_cobid
self.tx_cobid = tx_cobid
self.network = None
self.network: canopen.network.Network = canopen.network._DummyNetwork()
self.od = od

def __getitem__(
Expand Down
Loading