|
12 | 12 |
|
13 | 13 | # third-party |
14 | 14 | from vcorelib import DEFAULT_ENCODING |
| 15 | +from vcorelib.dict import GenericStrDict |
15 | 16 | from vcorelib.io import ARBITER, JsonObject |
| 17 | +from vcorelib.io.bus import BUS |
16 | 18 | from vcorelib.logging import DEFAULT_TIME_FORMAT, LoggerMixin |
17 | 19 | from vcorelib.math import default_time_ns, nano_str |
18 | 20 | from vcorelib.names import name_search |
|
25 | 27 | CommandHook, |
26 | 28 | EnvironmentMap, |
27 | 29 | ) |
| 30 | +from runtimepy.channel.environment.command.result import CommandResult |
28 | 31 | from runtimepy.channel.registry import ParsedEvent |
29 | 32 | from runtimepy.mapping import DEFAULT_PATTERN |
30 | 33 |
|
@@ -243,6 +246,38 @@ def register(self, name: str, env: ChannelCommandProcessor) -> None: |
243 | 246 | ENVIRONMENTS = GLOBAL |
244 | 247 |
|
245 | 248 |
|
| 249 | +def global_command(env: str, value: str) -> Optional[CommandResult]: |
| 250 | + """Handle a global command.""" |
| 251 | + |
| 252 | + result = None |
| 253 | + if env in GLOBAL: |
| 254 | + result = GLOBAL[env].command(value) |
| 255 | + else: |
| 256 | + GLOBAL.logger.error( |
| 257 | + "Couldn't run command env='%s' value='%s'.", env, value |
| 258 | + ) |
| 259 | + return result |
| 260 | + |
| 261 | + |
| 262 | +def global_commands(*cmds: tuple[str, str]) -> None: |
| 263 | + """Handle a global command.""" |
| 264 | + for env, value in cmds: |
| 265 | + global_command(env, value) |
| 266 | + |
| 267 | + |
| 268 | +async def global_command_bus(payload: GenericStrDict) -> None: |
| 269 | + """Handle a bus message.""" |
| 270 | + |
| 271 | + if "env" in payload and "value" in payload: |
| 272 | + global_command(payload["env"], payload["value"]) |
| 273 | + elif "cmds" in payload: |
| 274 | + global_commands(*payload["cmds"]) |
| 275 | + |
| 276 | + |
| 277 | +BUS.register_ro("command", global_command_bus) |
| 278 | +BUS.register_ro("cmd", global_command_bus) |
| 279 | + |
| 280 | + |
246 | 281 | def clear_env() -> None: |
247 | 282 | """Reset the global environment mapping.""" |
248 | 283 | GLOBAL.clear() |
|
0 commit comments