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 指令碼以供開發與測試使用。
36 lines
918 B
PHP
36 lines
918 B
PHP
<?php
|
|
|
|
namespace App\Services\Machine;
|
|
|
|
use Illuminate\Support\Facades\Redis;
|
|
use Illuminate\Support\Str;
|
|
|
|
class MqttCommandService
|
|
{
|
|
/**
|
|
* 發送指令至機台
|
|
*
|
|
* @param string $serialNo 機台序號
|
|
* @param string $command 指令名稱 (例如 dispense, reboot)
|
|
* @param array $payload 指令參數
|
|
* @return string Message ID
|
|
*/
|
|
public function sendCommand(string $serialNo, string $command, array $payload = []): string
|
|
{
|
|
$messageId = 'MSG_' . Str::random(16);
|
|
$queueKey = config('mqtt.outgoing_queue', 'mqtt_outgoing_commands');
|
|
|
|
$data = [
|
|
'target' => $serialNo,
|
|
'command' => $command,
|
|
'payload' => $payload,
|
|
'message_id' => $messageId,
|
|
'timestamp' => now()->toIso8601String(),
|
|
];
|
|
|
|
Redis::rpush($queueKey, json_encode($data));
|
|
|
|
return $messageId;
|
|
}
|
|
}
|