Documentation

Workers

Workers are background processes that handle sensor polling, control logic, and event processing.

How Workers Operate

When MudPi starts, it loads workers defined in your configuration. Each worker runs in its own thread, independently handling its assigned tasks like reading sensors or managing relay schedules.

Worker Lifecycle

1

Initialize — Worker loads its configuration and validates settings

2

Connect — Worker establishes connections to Redis and hardware

3

Run — Worker enters its main loop, performing tasks at defined intervals

4

Shutdown — Worker cleans up resources on system stop

Worker Types

Sensor Workers

Poll sensors at defined intervals and publish readings to Redis. Each sensor worker manages one or more sensors and handles GPIO communication.

Control Workers

Manage relay states and respond to commands. Control workers listen for events on Redis pub/sub channels and toggle GPIO pins accordingly.

Trigger Workers

Evaluate conditions and fire events when thresholds are met. Trigger workers subscribe to sensor data channels and compare readings against configured rules.

Schedule Workers

Execute time-based actions on a cron-like schedule. Schedule workers use the system clock to trigger controls at predetermined times.

Custom Workers

You can create custom workers to extend MudPi's functionality. Custom workers follow the same lifecycle pattern and can interact with all system components through Redis events.

Python
class CustomWorker(Worker):
    def __init__(self, config):
        super().__init__(config)
        self.name = config.get("name", "Custom Worker")

    def run(self):
        while self.running:
            # Your custom logic here
            self.sleep(self.poll_interval)