DeviceStatusPolicy.php 810 B

1234567891011121314151617181920212223242526272829
  1. <?php
  2. declare(strict_types=1);
  3. namespace plugin\saiadmin\app\service\device;
  4. /** One mutually exclusive display policy for counters, filters, cards and exports. */
  5. final class DeviceStatusPolicy
  6. {
  7. public const LABELS = [
  8. 'online' => '在线',
  9. 'offline' => '离线',
  10. 'fault' => '告警',
  11. 'unconnected' => '未接入',
  12. ];
  13. public function __construct(private readonly \DateTimeImmutable $now)
  14. {
  15. }
  16. public function expression(): string
  17. {
  18. $cutoff = $this->now->modify('-15 minutes')->format('Y-m-d H:i:s');
  19. return "CASE
  20. WHEN TRIM(COALESCE(d.fault, '')) <> '' THEN 'fault'
  21. WHEN d.last_seen IS NULL THEN 'unconnected'
  22. WHEN d.last_seen < '$cutoff' THEN 'offline'
  23. ELSE 'online' END";
  24. }
  25. }