« Back to History
machine_beam_status_monitor.php
|
20260920_164915.php
Initial Domain Snapshot
Copy Code
<?php error_reporting(E_ALL); ini_set('display_errors', 1); require_once __DIR__ . '/modules/auth/page_acl.php'; $ctx = page_require_access('machine_beam_status_monitor'); $company_id = (int)$ctx['company_id']; $pdo = $ctx['pdo']; /* ------------------------------------------------- STEP 1: LAST SYNC TIME ------------------------------------------------- */ $st = $pdo->prepare(" SELECT MAX(updated_at) FROM machine_beam_status WHERE company_id = ? "); $st->execute([$company_id]); $last_sync = $st->fetchColumn() ?: '1970-01-01 00:00:00'; /* ------------------------------------------------- STEP 2: FETCH NEW EVENTS ------------------------------------------------- */ $st = $pdo->prepare(" SELECT beam_no, machine_no, machine_type, beam_slot, event_type, created_at FROM beam_load_data WHERE company_id = ? AND created_at > ? AND event_type IN ('loaded','finished') ORDER BY created_at ASC "); $st->execute([$company_id, $last_sync]); $events = $st->fetchAll(PDO::FETCH_ASSOC); /* ------------------------------------------------- HELPERS ------------------------------------------------- */ function json_add($json, $value){ $arr = $json ? json_decode($json, true) : []; if (!in_array($value, $arr)) $arr[] = $value; return json_encode(array_values($arr)); } /* ------------------------------------------------- STEP 3: PROCESS EVENTS ------------------------------------------------- */ foreach ($events as $e) { $machine_no = $e['machine_no']; $machine_type = $e['machine_type']; $slot = $e['beam_slot']; // primary / secondary $beam_no = $e['beam_no']; $etype = $e['event_type']; /* Ensure ONE row per machine */ $st = $pdo->prepare(" SELECT * FROM machine_beam_status WHERE company_id = ? AND machine_no = ? LIMIT 1 "); $st->execute([$company_id, $machine_no]); $row = $st->fetch(PDO::FETCH_ASSOC); if (!$row) { $pdo->prepare(" INSERT INTO machine_beam_status (company_id, machine_no, machine_type, updated_at) VALUES (?,?,?,NOW()) ")->execute([$company_id, $machine_no, $machine_type]); $row = [ 'primary_running_beam' => null, 'primary_advance_beam' => null, 'primary_finished_beams' => null, 'secondary_running_beam' => null, 'secondary_advance_beam' => null, 'secondary_finished_beams'=> null, ]; } /* ---------------- LOADED ---------------- */ if ($etype === 'loaded') { if ($slot === 'primary') { if (empty($row['primary_running_beam'])) { $pdo->prepare(" UPDATE machine_beam_status SET primary_running_beam=?, updated_at=NOW() WHERE company_id=? AND machine_no=? ")->execute([$beam_no, $company_id, $machine_no]); } elseif (empty($row['primary_advance_beam'])) { $pdo->prepare(" UPDATE machine_beam_status SET primary_advance_beam=?, updated_at=NOW() WHERE company_id=? AND machine_no=? ")->execute([$beam_no, $company_id, $machine_no]); } } if ($slot === 'secondary') { if (empty($row['secondary_running_beam'])) { $pdo->prepare(" UPDATE machine_beam_status SET secondary_running_beam=?, updated_at=NOW() WHERE company_id=? AND machine_no=? ")->execute([$beam_no, $company_id, $machine_no]); } elseif (empty($row['secondary_advance_beam'])) { $pdo->prepare(" UPDATE machine_beam_status SET secondary_advance_beam=?, updated_at=NOW() WHERE company_id=? AND machine_no=? ")->execute([$beam_no, $company_id, $machine_no]); } } } /* ---------------- FINISHED ---------------- */ if ($etype === 'finished') { if ($slot === 'primary' && $row['primary_running_beam'] == $beam_no) { $finished = json_add($row['primary_finished_beams'], $beam_no); $pdo->prepare(" UPDATE machine_beam_status SET primary_running_beam = primary_advance_beam, primary_advance_beam = NULL, primary_finished_beams = ?, updated_at = NOW() WHERE company_id=? AND machine_no=? ")->execute([$finished, $company_id, $machine_no]); } if ($slot === 'secondary' && $row['secondary_running_beam'] == $beam_no) { $finished = json_add($row['secondary_finished_beams'], $beam_no); $pdo->prepare(" UPDATE machine_beam_status SET secondary_running_beam = secondary_advance_beam, secondary_advance_beam = NULL, secondary_finished_beams = ?, updated_at = NOW() WHERE company_id=? AND machine_no=? ")->execute([$finished, $company_id, $machine_no]); } } } /* ------------------------------------------------- STEP 4: VIEW SNAPSHOT ------------------------------------------------- */ $st = $pdo->prepare(" SELECT * FROM machine_beam_status WHERE company_id = ? ORDER BY machine_no "); $st->execute([$company_id]); $rows = $st->fetchAll(PDO::FETCH_ASSOC); require_once __DIR__ . '/partials/header.php'; ?> <div class="wrap"> <h2>Machine Beam Status Monitor</h2> <table class="table table--bordered"> <thead> <tr> <th>Machine</th> <th>Primary Running</th> <th>Primary Advance</th> <th>Secondary Running</th> <th>Secondary Advance</th> <th>Updated</th> </tr> </thead> <tbody> <?php foreach ($rows as $r): ?> <tr> <td><?=htmlspecialchars($r['machine_no'])?></td> <td><?=$r['primary_running_beam'] ?: '-'?></td> <td><?=$r['primary_advance_beam'] ?: '-'?></td> <td><?=$r['secondary_running_beam'] ?: '-'?></td> <td><?=$r['secondary_advance_beam'] ?: '-'?></td> <td><?=$r['updated_at']?></td> </tr> <?php endforeach; ?> </tbody> </table> </div> <?php require_once __DIR__ . '/partials/footer.php'; ?>