From c014d7431ce70fd6446ef5e085ea7be4c937ffcd Mon Sep 17 00:00:00 2001 From: sky121113 Date: Sat, 16 May 2026 21:45:18 +0800 Subject: [PATCH] =?UTF-8?q?[FIX]=20=E4=BF=AE=E6=AD=A3=E9=87=8D=E5=95=9F?= =?UTF-8?q?=E5=BE=8C=E7=94=A2=E7=94=9F=E9=87=8D=E8=A4=87=E6=A9=9F=E5=8F=B0?= =?UTF-8?q?=E6=97=A5=E8=AA=8C=E7=9A=84=E5=95=8F=E9=A1=8C=E4=B8=A6=E9=87=8D?= =?UTF-8?q?=E6=A7=8B=E9=82=8F=E8=BC=AF=E4=B8=80=E8=87=B4=E6=80=A7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 1. 修正 ProcessHeartbeat 在快取失效時會誤報韌體與溫度的問題。 2. 引入 Database Fallback 機制:當 Redis 無紀錄時自動比對資料庫現有值。 3. 統一韌體與溫度的偵測邏輯,確保兩者皆為「比對舊值 -> 異動才記 -> 補回快取」。 4. 提升系統對 CI/CD 重啟或快取清理的容錯能力。 --- app/Jobs/Machine/ProcessHeartbeat.php | 38 +++++++++++---------------- 1 file changed, 16 insertions(+), 22 deletions(-) diff --git a/app/Jobs/Machine/ProcessHeartbeat.php b/app/Jobs/Machine/ProcessHeartbeat.php index f513fdc..1ff0ca0 100644 --- a/app/Jobs/Machine/ProcessHeartbeat.php +++ b/app/Jobs/Machine/ProcessHeartbeat.php @@ -64,13 +64,17 @@ class ProcessHeartbeat implements ShouldQueue // 2. 韌體版本比對 (有改才傳,有改才記) if (isset($this->payload['firmware_version'])) { $fv = (string) $this->payload['firmware_version']; - if (!isset($oldState['firmware_version']) || (string)$oldState['firmware_version'] !== $fv) { + + // 優先從快取取舊版本,若無則從資料庫取 + $oldVersion = $oldState['firmware_version'] ?? $machine->firmware_version; + + if ((string)$oldVersion !== $fv) { \App\Jobs\Machine\ProcessStateLog::dispatch( $machine->id, $machine->company_id, "Firmware updated to :version", 'info', - ['version' => $fv, 'old' => $oldState['firmware_version'] ?? 'Unknown'] + ['version' => $fv, 'old' => $oldVersion ?? 'Unknown'] ); } $updateData['firmware_version'] = $fv; @@ -87,21 +91,11 @@ class ProcessHeartbeat implements ShouldQueue $tempLogCacheKey = "machine:{$this->serialNo}:last_temp_log"; $lastLogEntry = \Illuminate\Support\Facades\Cache::get($tempLogCacheKey); - $shouldLog = false; - if (!$lastLogEntry) { - $shouldLog = true; - } else { - $lastValue = (int) $lastLogEntry['value']; - $lastAt = \Carbon\Carbon::parse($lastLogEntry['at']); + // 統一邏輯:優先從快取取值,若無則從資料庫取 + $oldTemp = isset($lastLogEntry['value']) ? (int)$lastLogEntry['value'] : $machine->temperature; - // 條件 A: 只要溫度與上次記錄不同就記錄 (整數比對) - if ($temp !== $lastValue) { - Log::debug("ProcessHeartbeat: Temperature changed from {$lastValue} to {$temp}. Triggering log."); - $shouldLog = true; - } - } - - if ($shouldLog) { + if ($temp !== (int)$oldTemp) { + Log::debug("ProcessHeartbeat: Temperature changed from {$oldTemp} to {$temp}. Triggering log."); \App\Jobs\Machine\ProcessStateLog::dispatch( $machine->id, $machine->company_id, @@ -109,13 +103,13 @@ class ProcessHeartbeat implements ShouldQueue 'info', ['temp' => $temp] ); - - // 更新快取,保留 7 天 - \Illuminate\Support\Facades\Cache::put($tempLogCacheKey, [ - 'value' => $temp, - 'at' => now()->toDateTimeString() - ], 604800); } + + // 無論是否有變動,都更新/延長快取壽命,確保快取資料存在 + \Illuminate\Support\Facades\Cache::put($tempLogCacheKey, [ + 'value' => $temp, + 'at' => now()->toDateTimeString() + ], 604800); } // 更新快取