OpenSMI-ROS2 0.1.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- opensmi/ros2/__init__.py +89 -0
- opensmi/ros2/__main__.py +11 -0
- opensmi/ros2/cli.py +143 -0
- opensmi/ros2/py.typed +0 -0
- opensmi/ros2/runtime/__init__.py +67 -0
- opensmi/ros2/runtime/_decode.py +101 -0
- opensmi/ros2/runtime/_encode.py +102 -0
- opensmi/ros2/runtime/_parser.py +147 -0
- opensmi/ros2/runtime/errors.py +141 -0
- opensmi/ros2/runtime/goal.py +232 -0
- opensmi/ros2/runtime/graph.py +288 -0
- opensmi/ros2/runtime/guard.py +213 -0
- opensmi/ros2/runtime/interface.py +140 -0
- opensmi/ros2/runtime/messages/__init__.py +503 -0
- opensmi/ros2/runtime/messages/industrial_msgs.py +62 -0
- opensmi/ros2/runtime/messages/industrial_msgs.pyi +159 -0
- opensmi/ros2/runtime/messages/motoros2_interfaces.py +79 -0
- opensmi/ros2/runtime/messages/motoros2_interfaces.pyi +307 -0
- opensmi/ros2/runtime/messages/neo_msgs2.py +55 -0
- opensmi/ros2/runtime/messages/neo_msgs2.pyi +138 -0
- opensmi/ros2/runtime/messages/neo_srvs2.py +72 -0
- opensmi/ros2/runtime/messages/neo_srvs2.pyi +224 -0
- opensmi/ros2/runtime/outcomes.py +103 -0
- opensmi/ros2/runtime/parameters.py +723 -0
- opensmi/ros2/runtime/publisher.py +125 -0
- opensmi/ros2/runtime/qos.py +394 -0
- opensmi/ros2/runtime/runtime.py +1171 -0
- opensmi/ros2/runtime/selection.py +150 -0
- opensmi/ros2/runtime/stamp.py +100 -0
- opensmi/ros2/runtime/subscription.py +354 -0
- opensmi/ros2/runtime/tf.py +714 -0
- opensmi/ros2/server/__init__.py +148 -0
- opensmi/ros2/server/access.py +109 -0
- opensmi/ros2/server/bindings/__init__.py +92 -0
- opensmi/ros2/server/bindings/_base.py +154 -0
- opensmi/ros2/server/bindings/action.py +133 -0
- opensmi/ros2/server/bindings/keyed.py +127 -0
- opensmi/ros2/server/bindings/lifecycle.py +131 -0
- opensmi/ros2/server/bindings/method.py +130 -0
- opensmi/ros2/server/bindings/monitoring.py +109 -0
- opensmi/ros2/server/bindings/parameter.py +133 -0
- opensmi/ros2/server/bindings/publisher.py +192 -0
- opensmi/ros2/server/bindings/registry.py +95 -0
- opensmi/ros2/server/bindings/tf.py +256 -0
- opensmi/ros2/server/browse_names.py +94 -0
- opensmi/ros2/server/component.py +334 -0
- opensmi/ros2/server/config.py +493 -0
- opensmi/ros2/server/containers.py +191 -0
- opensmi/ros2/server/drivers.py +421 -0
- opensmi/ros2/server/errors.py +159 -0
- opensmi/ros2/server/events.py +406 -0
- opensmi/ros2/server/introspection.py +850 -0
- opensmi/ros2/server/keyed.py +749 -0
- opensmi/ros2/server/lifecycle.py +238 -0
- opensmi/ros2/server/limits.py +66 -0
- opensmi/ros2/server/loader.py +977 -0
- opensmi/ros2/server/machine.py +225 -0
- opensmi/ros2/server/method.py +307 -0
- opensmi/ros2/server/mirror.py +582 -0
- opensmi/ros2/server/mutation.py +992 -0
- opensmi/ros2/server/nodesets/Opc.Ua.SmartFactory.ROS2.NodeSet2.xml +85 -0
- opensmi/ros2/server/nodesets/__init__.py +18 -0
- opensmi/ros2/server/nodesets/smart_factory_ros2.py +26 -0
- opensmi/ros2/server/parameters.py +704 -0
- opensmi/ros2/server/projection.py +1176 -0
- opensmi/ros2/server/projections/__init__.py +41 -0
- opensmi/ros2/server/projections/scope.py +222 -0
- opensmi/ros2/server/provenance.py +121 -0
- opensmi/ros2/server/publisher.py +781 -0
- opensmi/ros2/server/recipes.py +514 -0
- opensmi/ros2/server/required.py +633 -0
- opensmi/ros2/server/skill.py +363 -0
- opensmi/ros2/server/spatial.py +283 -0
- opensmi/ros2/server/startup.py +626 -0
- opensmi/ros2/server/status.py +107 -0
- opensmi/ros2/server/structures.py +135 -0
- opensmi/ros2/server/transforms/__init__.py +39 -0
- opensmi/ros2/server/transforms/conversions.py +58 -0
- opensmi/ros2/server/transforms/filing.py +340 -0
- opensmi/ros2/server/transforms/mirror.py +462 -0
- opensmi/ros2/server/transforms/outward.py +180 -0
- opensmi/ros2/server/transforms/records.py +195 -0
- opensmi/ros2/server/transforms/spatial.py +154 -0
- opensmi_ros2-0.1.0.dist-info/METADATA +183 -0
- opensmi_ros2-0.1.0.dist-info/RECORD +89 -0
- opensmi_ros2-0.1.0.dist-info/WHEEL +5 -0
- opensmi_ros2-0.1.0.dist-info/entry_points.txt +2 -0
- opensmi_ros2-0.1.0.dist-info/licenses/LICENSES/MIT.txt +18 -0
- opensmi_ros2-0.1.0.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,406 @@
|
|
|
1
|
+
# SPDX-FileCopyrightText: 2026 Technologie-Initiative SmartFactory KL e.V.
|
|
2
|
+
#
|
|
3
|
+
# SPDX-License-Identifier: MIT
|
|
4
|
+
|
|
5
|
+
"""The project's namespace, and the one event it raises about itself.
|
|
6
|
+
|
|
7
|
+
A BindingEvent is the only OPC UA event this project defines. Its subject is always a Binding: a
|
|
8
|
+
diagnostic changing level, a QoS mismatch, a failed publish, a reached cap, a key collision, a
|
|
9
|
+
lifecycle node moved by something other than us. It carries the Binding name, the ROS2 path and a
|
|
10
|
+
code. A fault with a state worth reading has a variable for it; the rest are moments, and a moment
|
|
11
|
+
is an event.
|
|
12
|
+
|
|
13
|
+
The event type subtypes the base event type, not the house ``AlertType``, which would need the OPC
|
|
14
|
+
UA condition lifecycle that the stack underneath hardly implements. The code goes in its
|
|
15
|
+
own ``Code`` field, like the house alert's ``ErrorCode``. The event is emitted by the item's
|
|
16
|
+
Notification node with ``SourceNode`` and ``SourceName`` set to the item itself.
|
|
17
|
+
|
|
18
|
+
The namespace is shipped as XML and registered through opensmi.server's nodeset handling; NodeIds
|
|
19
|
+
are read off :class:`~opensmi.ros2.server.nodesets.SmartFactoryROS2NodeIds`. Events are raised on
|
|
20
|
+
asyncua through the server's ``ua_server``. :class:`ROS2EventsMixin` puts the BindingEvent on the
|
|
21
|
+
item's own Notification node, where opensmi fires its ``ua_log_*`` events.
|
|
22
|
+
:class:`ROS2ModelChangeMixin` puts OPC UA's ``GeneralModelChangeEvent`` on the spatial-object list,
|
|
23
|
+
the node OPC 10000-210 names for it.
|
|
24
|
+
|
|
25
|
+
A client has to subscribe to the emitting node itself. asyncua does not propagate events up the
|
|
26
|
+
``HasNotifier`` hierarchy, so a subscription to the Server object receives none of them, and the
|
|
27
|
+
failure is silence.
|
|
28
|
+
"""
|
|
29
|
+
|
|
30
|
+
from __future__ import annotations
|
|
31
|
+
|
|
32
|
+
import asyncio
|
|
33
|
+
import datetime as dt
|
|
34
|
+
from collections import deque
|
|
35
|
+
from collections.abc import AsyncGenerator
|
|
36
|
+
from dataclasses import dataclass
|
|
37
|
+
from enum import IntEnum
|
|
38
|
+
from pathlib import Path
|
|
39
|
+
from typing import TYPE_CHECKING, Any, Final
|
|
40
|
+
|
|
41
|
+
from asyncua import ua
|
|
42
|
+
from asyncua.ua.object_ids import ObjectIds
|
|
43
|
+
from asyncua.ua.uaerrors import BadNoMatch
|
|
44
|
+
from opensmi.core.lifecycle_mixin import lifecycle
|
|
45
|
+
from opensmi.server.base_machinery_item import BaseMachineryItem
|
|
46
|
+
from opensmi.server.mixins import MonitoringMixin, NotificationMixin, SpatialObjectListMixin
|
|
47
|
+
|
|
48
|
+
from opensmi.ros2.server.errors import DeclarationError
|
|
49
|
+
from opensmi.ros2.server.nodesets import PACKAGE, SmartFactoryROS2NodeIds
|
|
50
|
+
|
|
51
|
+
if TYPE_CHECKING:
|
|
52
|
+
from collections.abc import Sequence
|
|
53
|
+
|
|
54
|
+
from asyncua.common.node import Node
|
|
55
|
+
from asyncua.server.event_generator import EventGenerator
|
|
56
|
+
from opensmi.server import Server
|
|
57
|
+
|
|
58
|
+
__all__ = [
|
|
59
|
+
"BINDING_EVENT_TYPE",
|
|
60
|
+
"NAMESPACE_URI",
|
|
61
|
+
"NODESET_PATH",
|
|
62
|
+
"RECENT_EVENTS",
|
|
63
|
+
"SIGNED_BYTE_STRING_TYPE",
|
|
64
|
+
"BindingEventRecord",
|
|
65
|
+
"ROS2EventsMixin",
|
|
66
|
+
"ROS2ModelChangeMixin",
|
|
67
|
+
"Severity",
|
|
68
|
+
"import_nodeset",
|
|
69
|
+
"root_of",
|
|
70
|
+
]
|
|
71
|
+
|
|
72
|
+
_NODE_VERSION: Final = "NodeVersion"
|
|
73
|
+
"""The Property OPC UA Part 3 defines and OPC 10000-210 makes mandatory on the list.
|
|
74
|
+
|
|
75
|
+
It is in namespace zero, hence the explicit index ``0`` in the lookup.
|
|
76
|
+
"""
|
|
77
|
+
|
|
78
|
+
|
|
79
|
+
NAMESPACE_URI: Final = SmartFactoryROS2NodeIds.URI
|
|
80
|
+
"""This project's own namespace URI.
|
|
81
|
+
|
|
82
|
+
It names the information model, not the distribution that ships it.
|
|
83
|
+
"""
|
|
84
|
+
|
|
85
|
+
|
|
86
|
+
NODESET_PATH: Final = Path(__file__).parent / "nodesets" / SmartFactoryROS2NodeIds.FILE_NAME
|
|
87
|
+
"""Where the nodeset XML that defines everything in :data:`NAMESPACE_URI` is installed."""
|
|
88
|
+
|
|
89
|
+
BINDING_EVENT_TYPE: Final = SmartFactoryROS2NodeIds.BindingEventType
|
|
90
|
+
"""``BindingEventType`` in this project's namespace, resolved per server with ``ua_get_node_id``."""
|
|
91
|
+
|
|
92
|
+
SIGNED_BYTE_STRING_TYPE: Final = SmartFactoryROS2NodeIds.SignedByteString
|
|
93
|
+
"""``SignedByteString`` in this project's namespace, resolved per server with ``ua_get_node_id``.
|
|
94
|
+
|
|
95
|
+
A ``ByteString`` subtype for bulk payloads whose elements are signed (an occupancy grid's
|
|
96
|
+
``int8[]``), which a client would otherwise read as large positive values. Unsigned payloads keep
|
|
97
|
+
the plain ``ByteString``.
|
|
98
|
+
"""
|
|
99
|
+
|
|
100
|
+
|
|
101
|
+
RECENT_EVENTS: Final = 200
|
|
102
|
+
"""How many BindingEvents one Machine keeps, so that they can be asked for after the fact.
|
|
103
|
+
|
|
104
|
+
A client that was not subscribed when an event fired cannot learn of it, so the introspection
|
|
105
|
+
Component hands back the last N from this ring. It is a ring so that memory does not grow with
|
|
106
|
+
every diagnostic transition; 200 covers a boot and the minutes after it.
|
|
107
|
+
"""
|
|
108
|
+
|
|
109
|
+
|
|
110
|
+
class Severity(IntEnum):
|
|
111
|
+
"""The house severity bands, which are the only ones this project uses.
|
|
112
|
+
|
|
113
|
+
opensmi writes ``0`` for information, ``555`` for a warning and ``999`` for an error. ``STALE``
|
|
114
|
+
is an alias of ``ERROR``: ROS2 ranks neither level, so a separate band would be this package's
|
|
115
|
+
opinion presented as the robot's.
|
|
116
|
+
"""
|
|
117
|
+
|
|
118
|
+
INFO = 0
|
|
119
|
+
WARNING = 555
|
|
120
|
+
ERROR = 999
|
|
121
|
+
STALE = 999
|
|
122
|
+
|
|
123
|
+
|
|
124
|
+
@dataclass(frozen=True, slots=True, kw_only=True)
|
|
125
|
+
class BindingEventRecord:
|
|
126
|
+
"""One BindingEvent, kept so that it can be read after the moment it was.
|
|
127
|
+
|
|
128
|
+
Holds the event's own fields plus when and where, so the introspection Component shows what a
|
|
129
|
+
subscribed client would have received.
|
|
130
|
+
|
|
131
|
+
:ivar at: When it fired, in UTC (wall-clock, to correlate with a robot's log).
|
|
132
|
+
:ivar item: The path of the machinery item that fired it.
|
|
133
|
+
:ivar binding: The Binding it is about.
|
|
134
|
+
:ivar path: The ROS2 topic, service, action or node path that Binding names.
|
|
135
|
+
:ivar code: What happened, as the stable identifier a client branches on.
|
|
136
|
+
:ivar severity: The house band it was fired at.
|
|
137
|
+
:ivar text: The human-readable detail that went into the event's ``Message``.
|
|
138
|
+
"""
|
|
139
|
+
|
|
140
|
+
at: dt.datetime
|
|
141
|
+
item: str
|
|
142
|
+
binding: str
|
|
143
|
+
path: str
|
|
144
|
+
code: str
|
|
145
|
+
severity: Severity
|
|
146
|
+
text: str
|
|
147
|
+
|
|
148
|
+
def __str__(self) -> str:
|
|
149
|
+
"""Return the one line an operator reads: when, where, what, and why."""
|
|
150
|
+
when = self.at.isoformat(timespec="seconds")
|
|
151
|
+
return f"{when} {self.item} {self.binding} ({self.path}) {self.code}: {self.text}"
|
|
152
|
+
|
|
153
|
+
|
|
154
|
+
async def import_nodeset(server: Server) -> int:
|
|
155
|
+
"""Register this project's nodeset with a server, import it once, and return its namespace index.
|
|
156
|
+
|
|
157
|
+
The import is idempotent, so a second Machine in the same process reuses the first one's.
|
|
158
|
+
|
|
159
|
+
:param server: The opensmi server to import into.
|
|
160
|
+
"""
|
|
161
|
+
server.ua_register_nodeset_xml(namespace=SmartFactoryROS2NodeIds, package=PACKAGE)
|
|
162
|
+
return await server.ua_import_nodeset(SmartFactoryROS2NodeIds)
|
|
163
|
+
|
|
164
|
+
|
|
165
|
+
def root_of(item: object) -> Any:
|
|
166
|
+
"""Return the Machine a machinery item reports to, which for a Machine is itself."""
|
|
167
|
+
current = item
|
|
168
|
+
while (parent := getattr(current, "parent", None)) is not None:
|
|
169
|
+
current = parent
|
|
170
|
+
return current
|
|
171
|
+
|
|
172
|
+
|
|
173
|
+
class ROS2EventsMixin(NotificationMixin):
|
|
174
|
+
"""Gives a machinery item the BindingEvent, on its own Notification node.
|
|
175
|
+
|
|
176
|
+
Brings opensmi's :class:`~opensmi.server.mixins.NotificationMixin`, so a Component that raises
|
|
177
|
+
BindingEvents has a Notification of its own. The namespace is imported and the generator built
|
|
178
|
+
in a lifecycle step after the Notification exists and before the item's ``_init``.
|
|
179
|
+
"""
|
|
180
|
+
|
|
181
|
+
def __init__(self, *args: Any, **kwargs: Any) -> None:
|
|
182
|
+
"""Start out with no event generator, and pass every argument to opensmi."""
|
|
183
|
+
super().__init__(*args, **kwargs)
|
|
184
|
+
self._ros2_namespace_index: int | None = None
|
|
185
|
+
self._binding_events: EventGenerator | None = None
|
|
186
|
+
# Every BindingEvent fired on this Machine, oldest first. Only the Machine's ring is written.
|
|
187
|
+
self._recent_binding_events: deque[BindingEventRecord] = deque(maxlen=RECENT_EVENTS)
|
|
188
|
+
# The generator keeps its fields between triggers, so concurrent emits would interleave.
|
|
189
|
+
self._binding_event_lock = asyncio.Lock()
|
|
190
|
+
|
|
191
|
+
@lifecycle(
|
|
192
|
+
after=NotificationMixin.lifecycle_notification,
|
|
193
|
+
before=BaseMachineryItem.lifecycle_machinery_item,
|
|
194
|
+
)
|
|
195
|
+
async def lifecycle_ros2_binding_events(self) -> AsyncGenerator[None]:
|
|
196
|
+
"""Import this project's namespace and build the BindingEvent generator on the Notification.
|
|
197
|
+
|
|
198
|
+
opensmi's generator keeps the Binding, ROS2Path and Code fields in the Notification's event history.
|
|
199
|
+
"""
|
|
200
|
+
self._ros2_namespace_index = await import_nodeset(self.server)
|
|
201
|
+
self._house_notification_node() # refuses an item without a Notification
|
|
202
|
+
self._binding_events = await self.notification.add_event_generator(
|
|
203
|
+
self.server.ua_get_node_id(BINDING_EVENT_TYPE)
|
|
204
|
+
)
|
|
205
|
+
yield
|
|
206
|
+
# no shutdown: the generator lives as long as the node it fires on
|
|
207
|
+
|
|
208
|
+
@property
|
|
209
|
+
def ros2_root_machine(self) -> Any:
|
|
210
|
+
"""Return the Machine this item reports to, which for a Machine is itself."""
|
|
211
|
+
return root_of(self)
|
|
212
|
+
|
|
213
|
+
@property
|
|
214
|
+
def ros2_recent_events(self) -> tuple[BindingEventRecord, ...]:
|
|
215
|
+
"""Return the BindingEvents the Machine and its Components fired, oldest first, at most RECENT_EVENTS."""
|
|
216
|
+
return tuple(self._recent_binding_events)
|
|
217
|
+
|
|
218
|
+
@property
|
|
219
|
+
def ros2_namespace_index(self) -> int:
|
|
220
|
+
"""Return the index this server imported :data:`NAMESPACE_URI` at.
|
|
221
|
+
|
|
222
|
+
:raises DeclarationError: Before the item's BindingEvents are initialised, which is
|
|
223
|
+
where the import happens.
|
|
224
|
+
"""
|
|
225
|
+
if self._ros2_namespace_index is None:
|
|
226
|
+
message = (
|
|
227
|
+
f"{type(self).__name__} has no namespace index yet; {NAMESPACE_URI} is imported "
|
|
228
|
+
f"while the machinery item's Notification is initialised"
|
|
229
|
+
)
|
|
230
|
+
raise DeclarationError(message)
|
|
231
|
+
return self._ros2_namespace_index
|
|
232
|
+
|
|
233
|
+
def _house_notification_node(self) -> ua.NodeId:
|
|
234
|
+
"""Return the node opensmi fires its own machinery-item messages on.
|
|
235
|
+
|
|
236
|
+
:raises DeclarationError: When this machinery item has no Notification at all.
|
|
237
|
+
"""
|
|
238
|
+
try:
|
|
239
|
+
node_id: ua.NodeId = self.notification.ua_node.nodeid
|
|
240
|
+
except (AssertionError, RuntimeError) as error:
|
|
241
|
+
message = (
|
|
242
|
+
f"{type(self).__name__} has no Notification object, so a BindingEvent would have "
|
|
243
|
+
f"nowhere to be emitted from"
|
|
244
|
+
)
|
|
245
|
+
raise DeclarationError(message) from error
|
|
246
|
+
return node_id
|
|
247
|
+
|
|
248
|
+
async def emit_binding_event(
|
|
249
|
+
self,
|
|
250
|
+
*,
|
|
251
|
+
binding: str,
|
|
252
|
+
path: str,
|
|
253
|
+
code: str,
|
|
254
|
+
severity: Severity,
|
|
255
|
+
text: str | None = None,
|
|
256
|
+
) -> None:
|
|
257
|
+
"""Fire one BindingEvent from this machinery item.
|
|
258
|
+
|
|
259
|
+
:param binding: The name of the Binding this is about.
|
|
260
|
+
:param path: The ROS2 topic, service, action or node path that Binding names.
|
|
261
|
+
:param code: What happened, as a stable upper-snake-case identifier a client can branch on.
|
|
262
|
+
:param severity: One of the house bands.
|
|
263
|
+
:param text: A human-readable detail for the event's ``Message``, such as a node's own
|
|
264
|
+
refusal text. Defaults to a summary of the other three and never carries the code.
|
|
265
|
+
:raises DeclarationError: When this machinery item's BindingEvents were never
|
|
266
|
+
initialised, so there is no generator to fire from.
|
|
267
|
+
"""
|
|
268
|
+
if self._binding_events is None:
|
|
269
|
+
message = f"{type(self).__name__} has no BindingEvent generator; its Notification was never initialised"
|
|
270
|
+
raise DeclarationError(message)
|
|
271
|
+
# The fields come from the nodeset, so they are set by name on asyncua's event object.
|
|
272
|
+
message = text if text is not None else f"{code} on {binding} ({path})"
|
|
273
|
+
event: Any = self._binding_events.event
|
|
274
|
+
async with self._binding_event_lock:
|
|
275
|
+
event.SourceNode = self.ua_node.nodeid
|
|
276
|
+
event.SourceName = self.name
|
|
277
|
+
event.Binding = binding
|
|
278
|
+
event.ROS2Path = path
|
|
279
|
+
event.Code = code
|
|
280
|
+
event.Severity = int(severity)
|
|
281
|
+
await self._binding_events.trigger(message=message)
|
|
282
|
+
self._keep(binding=binding, path=path, code=code, severity=severity, text=message)
|
|
283
|
+
self.logger.info("Fired a BindingEvent", binding=binding, path=path, code=code, severity=int(severity))
|
|
284
|
+
|
|
285
|
+
def _keep(self, *, binding: str, path: str, code: str, severity: Severity, text: str) -> None:
|
|
286
|
+
"""Record one fired BindingEvent on this Machine's ring, after the trigger succeeded."""
|
|
287
|
+
record = BindingEventRecord(
|
|
288
|
+
at=dt.datetime.now(dt.UTC),
|
|
289
|
+
item=str(self.path),
|
|
290
|
+
binding=binding,
|
|
291
|
+
path=path,
|
|
292
|
+
code=code,
|
|
293
|
+
severity=severity,
|
|
294
|
+
text=text,
|
|
295
|
+
)
|
|
296
|
+
self.ros2_root_machine.note_binding_event(record)
|
|
297
|
+
|
|
298
|
+
def note_binding_event(self, record: BindingEventRecord) -> None:
|
|
299
|
+
"""Add one fired BindingEvent to this Machine's ring. Called by every item on it."""
|
|
300
|
+
self._recent_binding_events.append(record)
|
|
301
|
+
|
|
302
|
+
|
|
303
|
+
class ROS2ModelChangeMixin(SpatialObjectListMixin, MonitoringMixin, ROS2EventsMixin):
|
|
304
|
+
"""Adds the standard model-change event, on the node the companion specification names.
|
|
305
|
+
|
|
306
|
+
For the Machine only: OPC 10000-210 marks the spatial-object list with
|
|
307
|
+
``GeneratesEvent GeneralModelChangeEventType``, and there is one per Machine. It brings opensmi's
|
|
308
|
+
spatial-object list and Monitoring container and builds the list as the Machine comes up.
|
|
309
|
+
"""
|
|
310
|
+
|
|
311
|
+
def __init__(self, *args: Any, **kwargs: Any) -> None:
|
|
312
|
+
"""Start out with no model-change generator, and pass everything to opensmi."""
|
|
313
|
+
super().__init__(*args, **kwargs)
|
|
314
|
+
self._model_changes: EventGenerator | None = None
|
|
315
|
+
self._model_change_lock = asyncio.Lock()
|
|
316
|
+
self._node_version: Node | None = None
|
|
317
|
+
self._node_versions = 0
|
|
318
|
+
|
|
319
|
+
@lifecycle(
|
|
320
|
+
after=MonitoringMixin.lifecycle_monitoring,
|
|
321
|
+
before=BaseMachineryItem.lifecycle_machinery_item,
|
|
322
|
+
)
|
|
323
|
+
async def lifecycle_ros2_model_change(self) -> AsyncGenerator[None]:
|
|
324
|
+
"""Build the spatial-object list, and generate model-change events on it."""
|
|
325
|
+
# Named after the Machine, to tell its list from another server's when a client aggregates.
|
|
326
|
+
await self._init_spatial_object_list(name=f"{self.name}SpatialObjectList")
|
|
327
|
+
self._model_changes = await self.server.ua_server.get_event_generator(
|
|
328
|
+
ua.NodeId(ua.Int32(ObjectIds.GeneralModelChangeEventType)),
|
|
329
|
+
self._specified_model_change_node(),
|
|
330
|
+
)
|
|
331
|
+
# asyncua types `Changes` with a DataType NodeId, which `ua.Variant` refuses; a structure
|
|
332
|
+
# travels as an ExtensionObject.
|
|
333
|
+
self._model_changes.event.data_types["Changes"] = ua.VariantType.ExtensionObject
|
|
334
|
+
self._node_version = await self._specified_node_version()
|
|
335
|
+
yield
|
|
336
|
+
# no shutdown: the generator lives as long as the node it fires on
|
|
337
|
+
|
|
338
|
+
async def _specified_node_version(self) -> Node | None:
|
|
339
|
+
"""Return the ``NodeVersion`` OPC 10000-210 makes mandatory on the spatial-object list.
|
|
340
|
+
|
|
341
|
+
An event says the address space moved; the node version is what a client that was not
|
|
342
|
+
listening compares against. ``None`` (with a warning) when the nodeset
|
|
343
|
+
has none, since the model change is the notification that matters.
|
|
344
|
+
"""
|
|
345
|
+
node = self.spatial_object_list.ua_node
|
|
346
|
+
try:
|
|
347
|
+
return await node.get_child(ua.QualifiedName(_NODE_VERSION, 0))
|
|
348
|
+
except BadNoMatch:
|
|
349
|
+
self.logger.warning(
|
|
350
|
+
"The spatial object list carries no NodeVersion; model changes are announced without one",
|
|
351
|
+
node=node.nodeid.to_string(),
|
|
352
|
+
)
|
|
353
|
+
return None
|
|
354
|
+
|
|
355
|
+
def _specified_model_change_node(self) -> ua.NodeId:
|
|
356
|
+
"""Return the node OPC 10000-210 names as the source of ``GeneralModelChangeEvent``.
|
|
357
|
+
|
|
358
|
+
:raises DeclarationError: When the Machine has no spatial-object list.
|
|
359
|
+
"""
|
|
360
|
+
try:
|
|
361
|
+
node_id: ua.NodeId = self.spatial_object_list.ua_node.nodeid
|
|
362
|
+
except Exception as error:
|
|
363
|
+
message = (
|
|
364
|
+
f"{type(self).__name__} has no spatial object list, which is the node OPC "
|
|
365
|
+
f"10000-210 names as the source of GeneralModelChangeEvent"
|
|
366
|
+
)
|
|
367
|
+
raise DeclarationError(message) from error
|
|
368
|
+
return node_id
|
|
369
|
+
|
|
370
|
+
async def emit_model_change(self, changes: Sequence[ua.ModelChangeStructureDataType]) -> None:
|
|
371
|
+
"""Announce that the address space moved, with every change of one mutation batched.
|
|
372
|
+
|
|
373
|
+
One event per call and none at init. An empty batch is not announced.
|
|
374
|
+
|
|
375
|
+
:param changes: What moved, one entry per node, as OPC UA models a model change.
|
|
376
|
+
:raises DeclarationError: When this Machine's model-change generator was never
|
|
377
|
+
built, so there is nothing to fire from.
|
|
378
|
+
"""
|
|
379
|
+
if self._model_changes is None:
|
|
380
|
+
message = f"{type(self).__name__} has no model-change generator; its spatial object list was never built"
|
|
381
|
+
raise DeclarationError(message)
|
|
382
|
+
if not changes:
|
|
383
|
+
return
|
|
384
|
+
event: Any = self._model_changes.event
|
|
385
|
+
async with self._model_change_lock:
|
|
386
|
+
await self._bump_node_version()
|
|
387
|
+
event.Changes = list(changes)
|
|
388
|
+
await self._model_changes.trigger()
|
|
389
|
+
|
|
390
|
+
async def _bump_node_version(self) -> None:
|
|
391
|
+
"""Write the next node version, before the event that says why it moved.
|
|
392
|
+
|
|
393
|
+
Written inside the same lock, so a client woken by the event reads a version that already
|
|
394
|
+
counts it. The value is an opaque counter as text (OPC UA Part 3); only its changing means
|
|
395
|
+
anything. It moves for every runtime mutation on the Machine, not only those under the
|
|
396
|
+
list: a client re-browses once too often but never misses a change.
|
|
397
|
+
"""
|
|
398
|
+
if self._node_version is None:
|
|
399
|
+
return
|
|
400
|
+
self._node_versions += 1
|
|
401
|
+
await self._node_version.write_value(ua.Variant(str(self._node_versions), ua.VariantType.String))
|
|
402
|
+
|
|
403
|
+
@property
|
|
404
|
+
def node_version(self) -> int:
|
|
405
|
+
"""Return how many times this Machine's spatial-object list has been re-versioned."""
|
|
406
|
+
return self._node_versions
|