Skip to content

Commit e037080

Browse files
Fixup NMT
1 parent 473f267 commit e037080

File tree

1 file changed

+18
-7
lines changed

1 file changed

+18
-7
lines changed

canopen/nmt.py

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,17 @@
22
import logging
33
import struct
44
import time
5-
from typing import Callable, Optional
5+
from collections.abc import Callable
6+
from typing import Dict, Final, List, Optional, TYPE_CHECKING
7+
8+
9+
if TYPE_CHECKING:
10+
from canopen import Network
11+
612

713
logger = logging.getLogger(__name__)
814

9-
NMT_STATES = {
15+
NMT_STATES: Final[Dict[int, str]] = {
1016
0: 'INITIALISING',
1117
4: 'STOPPED',
1218
5: 'OPERATIONAL',
@@ -15,7 +21,7 @@
1521
127: 'PRE-OPERATIONAL'
1622
}
1723

18-
NMT_COMMANDS = {
24+
NMT_COMMANDS: Final[Dict[str, int]] = {
1925
'OPERATIONAL': 1,
2026
'STOPPED': 2,
2127
'SLEEP': 80,
@@ -26,7 +32,7 @@
2632
'RESET COMMUNICATION': 130
2733
}
2834

29-
COMMAND_TO_STATE = {
35+
COMMAND_TO_STATE: Final[Dict[int, int]] = {
3036
1: 5,
3137
2: 4,
3238
80: 80,
@@ -45,7 +51,7 @@ class NmtBase:
4551

4652
def __init__(self, node_id: int):
4753
self.id = node_id
48-
self.network = None
54+
self.network: Optional[Network] = None
4955
self._state = 0
5056

5157
def on_command(self, can_id, data, timestamp):
@@ -111,7 +117,7 @@ def __init__(self, node_id: int):
111117
#: Timestamp of last heartbeat message
112118
self.timestamp: Optional[float] = None
113119
self.state_update = threading.Condition()
114-
self._callbacks = []
120+
self._callbacks: List[Callable[[int], None]] = []
115121

116122
def on_heartbeat(self, can_id, data, timestamp):
117123
with self.state_update:
@@ -139,6 +145,7 @@ def send_command(self, code: int):
139145
super(NmtMaster, self).send_command(code)
140146
logger.info(
141147
"Sending NMT command 0x%X to node %d", code, self.id)
148+
assert self.network is not None
142149
self.network.send_message(0, [code, self.id])
143150

144151
def wait_for_heartbeat(self, timeout: float = 10):
@@ -180,7 +187,9 @@ def start_node_guarding(self, period: float):
180187
:param period:
181188
Period (in seconds) at which the node guarding should be advertised to the slave node.
182189
"""
183-
if self._node_guarding_producer : self.stop_node_guarding()
190+
if self._node_guarding_producer:
191+
self.stop_node_guarding()
192+
assert self.network is not None
184193
self._node_guarding_producer = self.network.send_periodic(0x700 + self.id, None, period, True)
185194

186195
def stop_node_guarding(self):
@@ -216,6 +225,7 @@ def send_command(self, code: int) -> None:
216225

217226
if self._state == 0:
218227
logger.info("Sending boot-up message")
228+
assert self.network is not None
219229
self.network.send_message(0x700 + self.id, [0])
220230

221231
# The heartbeat service should start on the transition
@@ -246,6 +256,7 @@ def start_heartbeat(self, heartbeat_time_ms: int):
246256
self.stop_heartbeat()
247257
if heartbeat_time_ms > 0:
248258
logger.info("Start the heartbeat timer, interval is %d ms", self._heartbeat_time_ms)
259+
assert self.network is not None
249260
self._send_task = self.network.send_periodic(
250261
0x700 + self.id, [self._state], heartbeat_time_ms / 1000.0)
251262

0 commit comments

Comments
 (0)