Skip to content

Commit f6dd549

Browse files
committed
Fix bobjacobsen#92: Generate Node ID (prevent alias reservation loop when two python-openlcb instances are running).
1 parent 123833a commit f6dd549

4 files changed

Lines changed: 24 additions & 8 deletions

File tree

examples/examples_settings.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import sys
1010

1111
from logging import getLogger
12+
1213
if __name__ == "__main__":
1314
logger = getLogger(__file__)
1415
else:
@@ -26,10 +27,18 @@
2627
.format(repr(REPO_DIR)))
2728
CONFIGS_DIR = os.path.dirname(os.path.realpath(__file__))
2829

30+
from openlcb.conventions import generate_node_id_str # noqa: E402
31+
32+
range_prefix = "05.01.01" # 05.01.01 is *only* for Bob Jacobsen's
33+
# python-openlcb (or as otherwise assigned by OpenLCB Group which
34+
# reserves 05.* range). See
35+
# <https://registry.openlcb.org/uniqueidranges>.
36+
2937
DEFAULT_SETTINGS = {
3038
"host": "192.168.16.212",
3139
"port": 12021,
32-
"localNodeID": "05.01.01.01.03.01", # Warning: *only for openlcb*:
40+
# "localNodeID": "05.01.01.01.03.01", # Warning: *only for openlcb*:
41+
"localNodeID": generate_node_id_str(range_prefix),
3342
# See localNodeID_comment in SETTINGS_COMMENTS.
3443
"farNodeID": "02.01.57.00.04.9C", # Serialized if hardware:
3544
# See farNodeID_comment in SETTINGS_COMMENTS.
@@ -256,6 +265,11 @@ def load(self, settings_path=None, required=False):
256265
try:
257266
with open(settings_path, 'r') as stream:
258267
self._meta = json.load(stream)
268+
if 'localNodeID' in self._meta:
269+
del self._meta['localNodeID']
270+
# ^ Make sure it is unique on each run
271+
# (unless set via load_cli_args,
272+
# which isn't recommended).
259273
except json.decoder.JSONDecodeError:
260274
self._meta = copy.deepcopy(DEFAULT_SETTINGS)
261275
no_ext, dot_ext = os.path.splitext(settings_path)

openlcb/canbus/canlink.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -573,7 +573,8 @@ def handleReceivedAMD(self, frame: CanFrame):
573573
def handleGlobalAME(self, frame: CanFrame):
574574
"""If no data, clear all except self from maps
575575
even if not in Permitted state
576-
(CAN Frame Transfer Standard, 6.2.3).
576+
(CAN Frame Transfer Standard, 6.2.3
577+
Alias Map Enquiry).
577578
"""
578579
if frame.data: # not global
579580
return
@@ -589,9 +590,10 @@ def handleGlobalAME(self, frame: CanFrame):
589590
# pass # concurrent modification
590591
if otherNodeID == self.localNodeID:
591592
continue
592-
del self.nodeIdToAlias[otherNodeID]
593-
# except KeyError:
594-
# pass # concurrent modification
593+
try:
594+
del self.nodeIdToAlias[otherNodeID]
595+
except KeyError:
596+
pass # concurrent modification?
595597
# TODO: clear matching _send_frames??
596598

597599
def handleReceivedAME(self, frame: CanFrame):

openlcb/conventions.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -175,9 +175,9 @@ def generate_node_id_str(id_range_prefix) -> str:
175175
Args:
176176
id_range_prefix (str): NodeID prefix in dotted hex notation
177177
(3 to 5. 3 at most recommended to make uniqueness more
178-
likely) Warning: 05.01.01 is *only* for Bob Jacobsen's
178+
likely) Warning: "05.01.01" is *only* for Bob Jacobsen's
179179
python-openlcb (or as otherwise assigned by OpenLCB Group
180-
which reserves 05.* range) See
180+
which reserves 05.* range). See
181181
<https://registry.openlcb.org/uniqueidranges>.
182182
Returns:
183183
str: Full 48-bit node ID in dotted hex string notation (Example:

openlcb/nodeid.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ def generate_node_id(id_range_prefix):
9797
id_range_prefix (str): NodeID prefix in dotted hex notation.
9898
Warning: 05.01.01 is *only* for Bob Jacobsen's
9999
python-openlcb (or as otherwise assigned by OpenLCB Group
100-
which reserves 05.* range) See
100+
which reserves 05.* range). See
101101
<https://registry.openlcb.org/uniqueidranges>.
102102
Returns:
103103
NodeID: A NodeID that is unique (very likely...).

0 commit comments

Comments
 (0)