star-cloud/app/Jobs/Machine/ProcessMachineEvent.php
sky121113 7579a0b5be [FEAT] 調整環境溫度上報文檔順序與機台硬體事件日誌歸類
1. 文件調整:將 config/api-docs.php 中的「機台環境溫度上報 (mqtt-ambient-temp-report)」MQTT API 區塊位置向上移至「機台硬體事件上報 (mqtt-event)」上方。
2. 日誌歸類:修改 ProcessMachineEvent.php,將機台硬體事件 (event) 日誌寫入資料庫時的 type 從 'status' (機台狀態) 改為 'ambient_temp' (環境溫度回傳),以直接顯示於「環境溫度回傳」Tab。
2026-06-04 12:00:54 +08:00

60 lines
1.7 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?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 為 'ambient_temp' 以便正確歸類顯示於「環境溫度回傳」日誌中
$machine->logs()->create([
'company_id' => $machine->company_id,
'type' => 'ambient_temp', // 歸類至環境溫度回傳
'level' => 'info',
'message' => $event, // 例如 "fanon" 或 "fanoff"
'context' => $this->payload,
]);
Log::info("ProcessMachineEvent: Machine [{$machine->serial_no}] recorded event [{$event}] to status logs");
}
}