Tutorial: OS EventBus, Background Services, Policy & App Store

PylotOS provides a decoupled OS application model so foreground UI dashboards, background sensor/network daemons, per-device configs, and runtime security policies cooperate cleanly without global-variable spaghetti or resource leaks across app restarts.


1. Typed OS PubSub EventBus (pylotos.events)

Both the PylotOS kernel and user applications publish and subscribe to named topics on the singleton events bus:

Built-In System Topics

from pylotos.events import TOPIC_WIFI_CHANGED, events

# Subscribe with automatic Subscription handle:
sub = events.subscribe("sensor.co2", lambda topic, data: print(topic, data))

# Read sticky last-known payload for any topic immediately on app startup:
wifi = events.last(TOPIC_WIFI_CHANGED, default={"connected": False})

# Publish custom application or service telemetry:
events.publish("sensor.co2", {"ppm": 615, "room": "Lab"})

# Cooperatively await the next occurrence of a topic in async tasks:
payload = await events.wait_for("sensor.co2", timeout_ms=5000)

# Clean up when done (app_runner also clears app-owned tasks & modules on stop!):
sub.unsubscribe()

2. Declarative Background Services (/apps/services/*.py)

Unlike foreground UI apps (which own the display and stop when the user switches apps), Background Services run continuously alongside any active UI app or the OS Launcher.

Drop a Python file into /apps/services/co2_sampler.py:

"""Background CO2 publisher daemon (`/apps/services/co2_sampler.py`)."""

import asyncio
from pylotos.events import events

SERVICE_MANIFEST = {
    "name": "co2_sampler",
    "version": "1.0.0",
    "autostart": True,
    "caps": ["i2c"],
    "restart_on_crash": True,
}


async def run_service(stop_event):
    ppm = 420
    while not stop_event.is_set():
        ppm = 420 + ((ppm + 7) % 300)
        events.publish("sensor.co2", {"ppm": ppm})
        await asyncio.sleep(5)

Manage services programmatically or via the pylot> CLI:

from pylotos.services import services

await services.start("co2_sampler")
print(services.status("co2_sampler"))
await services.stop("co2_sampler")

3. Isolated App Lifecycle & Soft-Reset Hygiene

When an app exits or crashes, app_runner.py guarantees deterministic isolation:

  1. Cancels all asyncio.Task coroutines spawned during the app session.
  2. Evicts app-imported modules from sys.modules so code edits reload cleanly.
  3. Calls hal.wireless_reset_app_handles() in C — putting any app-opened hal.rf, hal.nfc, hal.ir, hal.espnow, or attach() GPIO pins back into safe standby while keeping HAL_OWNER_OS Wi-Fi, :8266 Bridge, and :80/:443 Web UI alive!
  4. Triggers gc.collect() (and supports optional boot_app_soft_reset VM soft-reset mode).

4. Three-Layer Per-Device app_config & Runtime policy

Keep shared code identical across a 20-board fleet while customizing room names, thresholds, or bus addresses per board:

from pylotos import app_config, policy

# Merges defaults <- /apps/<app>.default.json <- /apps/.config/<app>.json
cfg = app_config.load(defaults={"room": "Lab", "limit_ppm": 1000})

# Runtime capability & secret protection gate (`ALLOW`, `ASK`, `DENY`):
decision, reason = policy.evaluate("nvs.get", target="wifi_pass", source="lan")
assert decision == policy.DENY  # Secret keys are hard-denied over LAN/MCP even if session is approved!

5. Signed App Store Catalog & .pylot-app Bundles (pylotos.app_store)

PylotOS supports offline and remote apps-index.json catalogs with capability filtering (board_caps, chip), VFS storage quotas (check_quota()), and atomic SHA-256 + ECDSA-verified .pylot-app package installation:

from pylotos import app_store

# Check remaining LittleFS/FAT VFS quota before installing:
quota = app_store.check_quota(extra_bytes=16384, quota_kb=512)

# Filter a catalog index to only apps compatible with the current board's capabilities:
compatible = app_store.filter_catalog(index_doc, only_compatible=True)