103 lines
3.1 KiB
PHP
103 lines
3.1 KiB
PHP
<?php
|
|
|
|
class NocDisplayAgent
|
|
{
|
|
private string $host;
|
|
private int $port;
|
|
private int $timeout;
|
|
private int $connectTimeout;
|
|
|
|
public function __construct(string $host, int $port = 5000, int $timeout = 10, int $connectTimeout = 5)
|
|
{
|
|
$this->host = $host;
|
|
$this->port = $port;
|
|
$this->timeout = $timeout;
|
|
$this->connectTimeout = $connectTimeout;
|
|
}
|
|
|
|
public static function fromDisplay(array $display): self
|
|
{
|
|
return new self($display['ip_address'], $display['agent_port'] ?? 5000);
|
|
}
|
|
|
|
private function request(string $method, string $endpoint, ?array $data = null): array
|
|
{
|
|
$ch = curl_init();
|
|
curl_setopt_array($ch, [
|
|
CURLOPT_URL => "http://{$this->host}:{$this->port}{$endpoint}",
|
|
CURLOPT_RETURNTRANSFER => true,
|
|
CURLOPT_TIMEOUT => $this->timeout,
|
|
CURLOPT_CONNECTTIMEOUT => $this->connectTimeout,
|
|
CURLOPT_FOLLOWLOCATION => true,
|
|
]);
|
|
|
|
if ($method === 'POST') {
|
|
curl_setopt($ch, CURLOPT_POST, true);
|
|
if ($data) {
|
|
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
|
|
curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/json']);
|
|
}
|
|
}
|
|
|
|
$response = curl_exec($ch);
|
|
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
|
|
$error = curl_error($ch);
|
|
curl_close($ch);
|
|
|
|
if ($error) {
|
|
return ['success' => false, 'error' => $error, 'online' => false];
|
|
}
|
|
|
|
$result = json_decode($response, true) ?? [];
|
|
$result['http_code'] = $httpCode;
|
|
$result['success'] = $httpCode >= 200 && $httpCode < 300;
|
|
$result['online'] = true;
|
|
return $result;
|
|
}
|
|
|
|
public function health(): array
|
|
{
|
|
return $this->request('GET', '/health');
|
|
}
|
|
|
|
public function status(): array
|
|
{
|
|
return $this->request('GET', '/status');
|
|
}
|
|
|
|
public function isOnline(): bool
|
|
{
|
|
$result = $this->health();
|
|
return ($result['status'] ?? '') === 'ok';
|
|
}
|
|
|
|
public function setUrl(int $port, string $url): array
|
|
{
|
|
if ($port < 0 || $port > 1) return ['success' => false, 'error' => 'Port must be 0 or 1'];
|
|
return $this->request('POST', "/display/{$port}/url", ['url' => $url]);
|
|
}
|
|
|
|
public function refresh(int $port): array
|
|
{
|
|
if ($port < 0 || $port > 1) return ['success' => false, 'error' => 'Port must be 0 or 1'];
|
|
return $this->request('POST', "/display/{$port}/refresh");
|
|
}
|
|
|
|
public function cecOn(int $port): array
|
|
{
|
|
if ($port < 0 || $port > 1) return ['success' => false, 'error' => 'Port must be 0 or 1'];
|
|
return $this->request('POST', "/cec/{$port}/on");
|
|
}
|
|
|
|
public function cecOff(int $port): array
|
|
{
|
|
if ($port < 0 || $port > 1) return ['success' => false, 'error' => 'Port must be 0 or 1'];
|
|
return $this->request('POST', "/cec/{$port}/off");
|
|
}
|
|
|
|
public function reboot(): array
|
|
{
|
|
return $this->request('POST', '/system/reboot');
|
|
}
|
|
}
|