feat: add ProcessErrorLog job to handle B013 mqtt errors

This commit is contained in:
sky121113 2026-04-22 16:36:56 +08:00
parent f49c1d3058
commit 680d922e1f
2 changed files with 62 additions and 2 deletions

View File

@ -0,0 +1,60 @@
<?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 ProcessErrorLog 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 ErrorLog: Machine not found", ['serial_no' => $this->serialNo]);
return;
}
$errorCode = $this->payload['error_code'] ?? 'Unknown';
$tid = $this->payload['tid'] ?? 'N/A';
// 這裡可以整合 i18n 翻譯,或是直接記錄
// 依照 SKILL.md 定義error_code 會被翻譯
ProcessStateLog::dispatch(
$machine->id,
$machine->company_id,
"Hardware Error reported: :code on slot :tid",
'error',
[
'code' => $errorCode,
'tid' => $tid,
'raw_payload' => $this->payload
],
'error'
);
}
}

View File

@ -4,7 +4,7 @@ return [
'title' => 'Star Cloud IoT 通訊協議',
'version' => 'v1.0.0',
'description' => '此文件提供 Star Cloud 智能販賣機 IoT 端點通訊協議說明,包含主動拉取 (HTTP REST) 與被動接收 (MQTT) 雙軌通訊機制,供硬體端與前端開發者調研與串接使用。',
'http_apis' => [
[
'name' => '機台核心通訊 (IoT Core)',
@ -438,7 +438,7 @@ return [
],
],
[
'name' => '機台異常上報 (B013 Error)',
'name' => '機台異常上報 (Error)',
'slug' => 'mqtt-error',
'action' => 'PUB',
'topic' => 'machine/{serial_no}/error',