Coverage for abb_rws_client_python_rw6/highlevel/modules.py: 100%
25 statements
« prev ^ index » next coverage.py v7.15.4, created at 2026-08-13 22:17 +0000
« prev ^ index » next coverage.py v7.15.4, created at 2026-08-13 22:17 +0000
1# abb_rws_client/highlevel/modules.py
2"""High-level RAPID module load/unload for ABB RWS RobotWare 6.
4Author: Clement RACINET
6Composed operations built exclusively from atomic ``rws/`` functions.
7No HTTP calls are made directly in this module.
9All functions are async and require an open ``RWSClient`` instance.
10"""
12from __future__ import annotations
14from abb_rws_client_python_rw6.core.client import RWSClient
15from abb_rws_client_python_rw6.core.exceptions import RWSHTTPError
16from abb_rws_client_python_rw6.core.logger import get_logger
17from abb_rws_client_python_rw6.rws.mastership import (
18 post_mastership_domain_release,
19 post_mastership_domain_request,
20)
21from abb_rws_client_python_rw6.rws.rapid.execution import (
22 reset_rapid_program_pointer_to_main,
23)
24from abb_rws_client_python_rw6.rws.rapid.tasks import (
25 load_rapid_module_into_rapid_task,
26 post_unload_module_from_rapid_task,
27)
29logger = get_logger(__name__)
32async def load_module_safe(
33 client: RWSClient,
34 *,
35 task: str,
36 module_path: str,
37 module_name: str,
38 domain: str = "rapid",
39) -> None:
40 """Unload (if present) then load a RAPID module, with mastership.
42 Composes:
44 1. ``reset_rapid_program_pointer_to_main(action="resetpp")`` — resets
45 the program pointer so the controller accepts module load/unload
46 operations (ABB requires PP reset before any structural change).
47 2. ``post_mastership_domain_request(domain, action="request")``
48 3. ``post_unload_module_from_rapid_task(task, action="unloadmod",
49 module=module_name)`` — errors are logged but not re-raised
50 (module may not be loaded yet).
51 4. ``load_rapid_module_into_rapid_task(task, action="loadmod",
52 modulepath=module_path)``
53 5. ``post_mastership_domain_release(domain, action="release")``
55 Mastership is always released in a ``finally`` block.
57 Route (delegated):
58 - ``POST /rw/rapid/execution`` (action=resetpp)
59 - ``POST /rw/mastership/{domain}`` (action=request)
60 - ``POST /rw/rapid/tasks/{task}`` (action=unloadmod)
61 - ``POST /rw/rapid/tasks/{task}`` (action=loadmod)
62 - ``POST /rw/mastership/{domain}`` (action=release)
64 ABB constraints:
65 - ``resetpp`` must be called before any module load/unload
66 operation, even when RAPID is already stopped. Omitting it
67 causes HTTP 400 (SYS_CTRL_E_EXEC_STATE).
68 - ``module_path`` is a path on the **controller filesystem**,
69 e.g. ``"$HOME/my_module.mod"``.
70 - ``module_name`` is the RAPID module name (without extension),
71 e.g. ``"my_module"``.
72 - RAPID mastership required for both load and unload.
73 - ``domain`` must be one of: ``"rapid"``, ``"cfg"``, ``"motion"``.
75 Args:
76 client: Open ``RWSClient`` instance.
77 task: RAPID task name, e.g. ``"T_ROB1"``.
78 module_path: Full path on the controller filesystem, e.g.
79 ``"$HOME/my_module.mod"``.
80 module_name: RAPID module name (no extension), e.g.
81 ``"my_module"``.
82 domain: Mastership domain. Defaults to ``"rapid"``.
84 Returns:
85 None. Expects HTTP 204 on the load call.
87 Raises:
88 RWSAuthenticationError: On HTTP 401.
89 RWSNotFoundError: On HTTP 404.
90 RWSHTTPError: On any other HTTP >= 400 (load step only).
92 Example:
93 ```python
94 async with RWSClient(host="192.168.125.1") as client:
95 await load_module_safe(
96 client,
97 task="T_ROB1",
98 module_path="$HOME/my_module.mod",
99 module_name="my_module",
100 )
101 ```
102 """
103 # Step 1 — Reset program pointer BEFORE taking mastership.
104 # ABB RW6 rejects loadmod/unloadmod with HTTP 400 (SYS_CTRL_E_EXEC_STATE)
105 # if the PP has not been reset, even when RAPID is stopped.
106 logger.debug("Resetting program pointer before module load …")
107 try:
108 await reset_rapid_program_pointer_to_main(client, action="resetpp")
109 logger.debug("Program pointer reset OK.")
110 except RWSHTTPError as exc:
111 # resetpp can fail if no program is loaded yet (first load).
112 # This is acceptable — log and continue.
113 logger.debug("resetpp skipped (no program loaded yet: %s)", exc)
115 # Step 2 — Acquire mastership on the specified domain
116 await post_mastership_domain_request(client, domain=domain, action="request")
117 try:
118 # Step 3 — Attempt unload — silently ignore if module is not loaded
119 try:
120 await post_unload_module_from_rapid_task(
121 client,
122 task=task,
123 action="unloadmod",
124 module=module_name,
125 )
126 logger.debug("Module %r unloaded from task %r.", module_name, task)
127 except RWSHTTPError as exc:
128 logger.debug(
129 "Unload of %r skipped (not loaded or error: %s).",
130 module_name,
131 exc,
132 )
134 # Step 4 — Load the module
135 await load_rapid_module_into_rapid_task(
136 client,
137 task=task,
138 action="loadmod",
139 modulepath=module_path,
140 )
141 logger.debug(
142 "Module %r loaded into task %r from %r.",
143 module_name,
144 task,
145 module_path,
146 )
147 finally:
148 # Step 5 — Always release mastership, even on exception
149 await post_mastership_domain_release(client, domain=domain, action="release")