Controller API

Complete API reference for the RouteMQ Controller base class and controller development patterns.

Controller Class

The Controller class is the base class that all application controllers should extend. It provides common functionality and logging capabilities for handling MQTT messages.

Import

from core.controller import Controller

Constructor

class MyController(Controller):
    pass

Controllers inherit from the base Controller class and implement static or instance methods to handle MQTT messages.

Properties

logger

Class-level logger instance for all controllers.

Type: logging.Logger Name: "RouteMQ.Controller"

Example:

Controller Development Patterns

Handler Method Signatures

Controller methods that handle MQTT messages should follow this signature pattern:

Static methods are the recommended approach for handlers as they don't require class instantiation:

Instance Methods

Instance methods can be used when you need to maintain state or inject dependencies:

Common Controller Patterns

Parameter Validation

Always validate route parameters and payload data:

Error Handling

Implement robust error handling in your controllers:

Database Integration

Controllers can integrate with database models:

Redis Integration

Use Redis for caching and temporary data:

Response Publishing

Controllers can publish responses back to MQTT:

Controller Organization

Single Responsibility

Each controller should handle a specific domain:

Method Naming Conventions

Use descriptive method names that indicate the action:

Testing Controllers

Unit Testing

Test controller methods independently:

Integration Testing

Test with actual MQTT client and database:

Best Practices

1. Keep Controllers Thin

Controllers should orchestrate, not implement business logic:

2. Use Dependency Injection

Make controllers testable by injecting dependencies:

3. Consistent Error Response Format

Use consistent error response formats across controllers:

4. Use Type Hints

Add type hints for better code documentation and IDE support:

Last updated