1. 導入 Go 語言撰寫的 MQTT Gateway (mqtt-gateway/),負責訊息接收並透過 Redis List 轉發至 Laravel。 2. 更新 compose.yaml 以包含 EMQX Broker 與 mqtt-gateway 服務,並配置相關環境變數。 3. 新增 ListenMqttQueue 指令與對應的異步處理 Job (ProcessHeartbeat, ProcessTransaction, ProcessMachineError)。 4. 實作 MqttSyncAuth 指令與 Machine Model Observer,支援自動化的機台認證資訊同步至 Redis。 5. 新增 MqttCommandService 用於處理雲端至機台的即時指令下發。 6. 更新專案規範文件 (framework.md 與 Skills) 以反映新的 MQTT 通訊機制與安全性規範。 7. 提供 simulate-heartbeat.sh 指令碼以供開發與測試使用。
60 lines
1.6 KiB
PHP
60 lines
1.6 KiB
PHP
<?php
|
|
|
|
namespace App\Jobs\Machine;
|
|
|
|
use App\Models\Machine\Machine;
|
|
use Illuminate\Bus\Queueable;
|
|
use Illuminate\Contracts\Queue\ShouldQueue;
|
|
use Illuminate\Foundation\Bus\Dispatchable;
|
|
use Illuminate\Queue\InteractsWithQueue;
|
|
use Illuminate\Queue\SerializesModels;
|
|
use Illuminate\Support\Facades\Log;
|
|
|
|
class ProcessHeartbeat implements ShouldQueue
|
|
{
|
|
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
|
|
|
|
protected $serialNo;
|
|
protected $payload;
|
|
|
|
/**
|
|
* Create a new job instance.
|
|
*/
|
|
public function __construct(string $serialNo, array $payload)
|
|
{
|
|
$this->serialNo = $serialNo;
|
|
$this->payload = $payload;
|
|
}
|
|
|
|
/**
|
|
* Execute the job.
|
|
*/
|
|
public function handle(): void
|
|
{
|
|
$machine = Machine::withoutGlobalScopes()->where('serial_no', $this->serialNo)->first();
|
|
|
|
if (!$machine) {
|
|
Log::warning("MQTT Heartbeat: Machine not found", ['serial_no' => $this->serialNo]);
|
|
return;
|
|
}
|
|
|
|
// 更新機台狀態
|
|
$updateData = [
|
|
'last_heartbeat_at' => now(),
|
|
];
|
|
|
|
// 根據 payload 動態更新欄位
|
|
if (isset($this->payload['current_page'])) {
|
|
$updateData['current_page'] = $this->payload['current_page'];
|
|
}
|
|
if (isset($this->payload['temperature'])) {
|
|
$updateData['temperature'] = $this->payload['temperature'];
|
|
}
|
|
if (isset($this->payload['firmware_version'])) {
|
|
$updateData['firmware_version'] = $this->payload['firmware_version'];
|
|
}
|
|
|
|
$machine->update($updateData);
|
|
}
|
|
}
|