1. [FEAT] 新增 App/Jobs/Machine/ProcessMachineEvent.php 用以非同步處理 machine/{serial_no}/event 事件,將收到的風扇狀態(fanon/fanoff)記錄在機台狀態日誌中。
2. [FEAT] 修改 ListenMqttQueue.php 支援 event 類型的 MQTT 事件分派路由。
3. [FEAT] 更新 config/api-docs.php 規格文件,將原本在 status 主題的風扇說明移去,並新增獨立的 event (機台硬體事件上報) 主題文件。
4. [FIX] 更新多語系資源檔,新增 fanon 與 fanoff 扁平鍵對譯並進行 ksort 排序與去逸出美化。
60 lines
1.7 KiB
PHP
60 lines
1.7 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 ProcessMachineEvent implements ShouldQueue
|
||
{
|
||
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
|
||
|
||
protected $serialNo;
|
||
protected $payload;
|
||
|
||
/**
|
||
* Create a new job instance.
|
||
*/
|
||
public function __construct($serialNo, $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("ProcessMachineEvent: Machine not found [{$this->serialNo}]");
|
||
return;
|
||
}
|
||
|
||
$event = $this->payload['event'] ?? null;
|
||
|
||
if (!$event) {
|
||
Log::warning("ProcessMachineEvent: Missing event in payload", ['payload' => $this->payload]);
|
||
return;
|
||
}
|
||
|
||
// 寫入機台狀態日誌,type 為 'status' 以便正確歸類顯示於「機台狀態」日誌中
|
||
$machine->logs()->create([
|
||
'company_id' => $machine->company_id,
|
||
'type' => 'status', // 忠實呈現於機台狀態中
|
||
'level' => 'info',
|
||
'message' => $event, // 例如 "fanon" 或 "fanoff"
|
||
'context' => $this->payload,
|
||
]);
|
||
|
||
Log::info("ProcessMachineEvent: Machine [{$machine->serial_no}] recorded event [{$event}] to status logs");
|
||
}
|
||
}
|