Installation
Run composer require coffeebike/owasys-bundle in your Laravel project (note: this is a Symfony bundle, so you’ll need to bridge it via Symfony Bridge or use it in a Symfony-based Laravel app like Laravel Symfony).
Enable the Bundle
Since Laravel doesn’t use AppKernel.php, register the bundle in config/app.php under the extra.bundles key (if using Symfony components) or via a custom service provider:
// config/app.php
'extra' => [
'bundles' => [
CoffeeBike\OwasysBundle\CoffeeBikeOwasysBundle::class,
],
],
Alternative: Create a Laravel service provider to initialize the bundle:
php artisan make:provider OwasysServiceProvider
Then register it in config/app.php and initialize the bundle in the provider’s boot() method.
First Use Case: MQTT Connection
Configure the bundle via config/packages/coffee_bike_owasys.yaml (create the file if missing):
coffee_bike_owasys:
mqtt:
host: 'your-mqtt-broker'
port: 1883
username: 'your-username'
password: 'your-password'
client_id: 'owasys-laravel-client'
Trigger the MQTT connection in a controller or command:
use CoffeeBike\OwasysBundle\Service\OwasysMqttService;
public function connectOwasys(OwasysMqttService $mqttService) {
$mqttService->connect();
}
Device Communication
$mqttService->publish('owasys/device/command', json_encode(['action' => 'track']));
$mqttService->subscribe('owasys/device/telemetry', function ($payload) {
$data = json_decode($payload, true);
// Process GPS coordinates, battery status, etc.
});
Event-Driven Architecture Use Laravel’s event system to decouple MQTT message handling:
// In OwasysServiceProvider::boot()
$mqttService = $this->app->make(OwasysMqttService::class);
$mqttService->onTelemetryReceived(function ($data) {
event(new OwasysTelemetryReceived($data));
});
Listen to events in your app:
public function handle(OwasysTelemetryReceived $event) {
// Log or process telemetry (e.g., store in DB, trigger alerts)
}
Configuration Management
// config/owasys.php
return [
'mqtt' => [
'host' => env('OWASYS_MQTT_HOST', 'localhost'),
],
];
$this->app->singleton(OwasysMqttService::class, function ($app) {
return new OwasysMqttService($app['config']['owasys.mqtt']);
});
Command-Line Integration Create Artisan commands for MQTT management:
php artisan make:command OwasysConnect
public function handle() {
$mqttService = $this->app->make(OwasysMqttService::class);
$mqttService->connect();
$this->info('Connected to Owasys device!');
}
Symfony vs. Laravel Compatibility
Kernel and Container. Workarounds:
EventDispatcher) if needed:
$dispatcher = new Symfony\Component\EventDispatcher\EventDispatcher();
$mqttService = new OwasysMqttService($dispatcher, $config);
MQTT Connection Issues
coffee_bike_owasys:
mqtt:
debug: true
$mqttService->setOnConnectionLost(function () {
$this->retryConnection();
});
Message Serialization
$payload = $mqttService->getLastMessage();
if (!is_string($payload) || json_decode($payload) === null) {
throw new \RuntimeException('Invalid MQTT payload');
}
Thread Safety
dispatch(new ProcessOwasysTelemetry($data))->onQueue('owasys');
Extending Functionality
OwasysMqttService:
class ExtendedOwasysService extends OwasysMqttService {
public function sendFirmwareUpdate($filePath) {
$this->publish('owasys/firmware', base64_encode(file_get_contents($filePath)));
}
}
$mqttService->setMessageMiddleware(function ($payload, callable $next) {
$payload = str_replace('old', 'new', $payload); // Example transform
return $next($payload);
});
Testing
$mockClient = $this->createMock(MqttClient::class);
$mockClient->method('publish')->willReturn(true);
$service = new OwasysMqttService($mockClient, $config);
Performance
$batch = collect($telemetryData)->chunk(10);
foreach ($batch as $chunk) {
$mqttService->publish('owasys/batch', json_encode($chunk));
}
Security
$allowedTopics = ['owasys/device/telemetry', 'owasys/device/command'];
if (!in_array($topic, $allowedTopics)) {
throw new \RuntimeException('Invalid MQTT topic');
}
coffee_bike_owasys:
mqtt:
ssl: true
ca_file: '/path/to/ca.crt'
Monitoring
Telescope::addData([
'owasys_messages_received' => $mqttService->getMessageCount(),
]);
How can I help you explore Laravel packages today?