시놀로지 원격 로그 뷰어 추가

This commit is contained in:
seo
2026-07-25 19:52:38 +09:00
parent 3aff2748e8
commit 756464643c
5 changed files with 254 additions and 5 deletions
+63 -1
View File
@@ -16,11 +16,12 @@ require_once __DIR__ . '/../public/api.php';
final class ControlWebSocket implements MessageComponentInterface
{
/** @var SplObjectStorage<ConnectionInterface, array{dmesg: bool, ip: string}> */
/** @var SplObjectStorage<ConnectionInterface, array{dmesg: bool, synology_log: bool, ip: string}> */
private SplObjectStorage $clients;
private bool $statusBusy = false;
private bool $dmesgBusy = false;
private bool $synologyLogBusy = false;
public function __construct()
{
@@ -47,6 +48,7 @@ final class ControlWebSocket implements MessageComponentInterface
$this->clients[$conn] = [
'dmesg' => false,
'synology_log' => false,
'ip' => $ip,
];
$this->sendStatus($conn);
@@ -61,6 +63,7 @@ final class ControlWebSocket implements MessageComponentInterface
$state = $this->clients[$from] ?? [
'dmesg' => false,
'synology_log' => false,
'ip' => '127.0.0.1',
];
@@ -73,6 +76,15 @@ final class ControlWebSocket implements MessageComponentInterface
return;
}
if (($data['type'] ?? '') === 'synology_log') {
$state['synology_log'] = !empty($data['open']);
$this->clients[$from] = $state;
if ($state['synology_log']) {
$this->sendSynologyLog($from);
}
return;
}
if (($data['type'] ?? '') === 'status_refresh') {
$this->sendStatus($from);
}
@@ -147,6 +159,40 @@ final class ControlWebSocket implements MessageComponentInterface
}
}
public function broadcastSynologyLog(): void
{
if ($this->clients->count() === 0 || $this->synologyLogBusy) {
return;
}
$targets = [];
foreach ($this->clients as $client) {
$state = $this->clients[$client];
if (!empty($state['synology_log'])) {
$targets[] = $client;
}
}
if ($targets === []) {
return;
}
$this->synologyLogBusy = true;
try {
$payload = $this->json([
'type' => 'synology_log',
'data' => synology_remote_log(),
]);
foreach ($targets as $client) {
$client->send($payload);
}
} catch (Throwable $e) {
$this->broadcastError($e);
} finally {
$this->synologyLogBusy = false;
}
}
private function sendStatus(ConnectionInterface $conn): void
{
try {
@@ -174,6 +220,21 @@ final class ControlWebSocket implements MessageComponentInterface
}
}
private function sendSynologyLog(ConnectionInterface $conn): void
{
try {
$conn->send($this->json([
'type' => 'synology_log',
'data' => synology_remote_log(),
]));
} catch (Throwable $e) {
$conn->send($this->json([
'type' => 'error',
'message' => $e->getMessage(),
]));
}
}
private function statusPayload(): string
{
try {
@@ -280,6 +341,7 @@ $startedSignature = $watchSignature($watchFiles);
$loop->addPeriodicTimer(1.0, static fn() => $app->broadcastStatus());
$loop->addPeriodicTimer(1.0, static fn() => $app->broadcastDmesg());
$loop->addPeriodicTimer(1.0, static fn() => $app->broadcastSynologyLog());
$loop->addPeriodicTimer(15.0, static function () use ($watchFiles, $watchSignature, $startedSignature): void {
if ($watchSignature($watchFiles) === $startedSignature) {
return;