some fixes

This commit is contained in:
jhartworks committed 2026-11-10 12:55:21 +01:00
1 parent 0ea3a3d818
commit d2c0f5b111
6 files changed
+109 -34

No files matched your search

+35 -13
View File
@@ -18,6 +18,8 @@ class NextFloor extends IPSModuleStrict
private const TRANSFER_CHUNK_BYTES = 128 * 1024;
private const MAX_TRANSFER_BYTES = 64 * 1024 * 1024;
private const TRANSFER_TTL = 120;
private const HOOK_RETRY_MS = 5000;
private const HOOK_MAINTENANCE_MS = 30000;
public function Create(): void
{
@@ -34,16 +36,19 @@ class NextFloor extends IPSModuleStrict
$this->RegisterAttributeInteger('BuildingRevision', 0);
$this->RegisterAttributeInteger('StateRevision', 0);
$this->RegisterMessage(0, IPS_KERNELSTARTED);
$this->RegisterMessage(0, KR_READY);
$this->RegisterTimer('WebHookRetry', 0, 'NFSC_RetryWebHook($_IPS[\'TARGET\']);');
$this->ensureAccessToken();
$this->SetVisualizationType(1);
$this->ensureWebHook();
$this->ensureWebHook(true);
}
public function ApplyChanges(): void
{
parent::ApplyChanges();
$this->RegisterMessage(0, IPS_KERNELSTARTED);
$this->RegisterMessage(0, KR_READY);
$this->SetVisualizationType(1);
$this->ensureWebHook();
}
@@ -51,12 +56,12 @@ class NextFloor extends IPSModuleStrict
public function MessageSink(int $TimeStamp, int $SenderID, int $Message, array $Data): void
{
parent::MessageSink($TimeStamp, $SenderID, $Message, $Data);
if ($SenderID === 0 && $Message === IPS_KERNELSTARTED) {
if ($SenderID === 0 && ($Message === KR_READY || $Message === IPS_KERNELSTARTED)) {
$this->ensureWebHook();
}
}
/** Timer callback; stops itself as soon as the volatile hook has been restored. */
/** Keep the volatile route registered, including resets after successful startup or module reload. */
public function RetryWebHook(): void
{
$this->ensureWebHook();
@@ -95,8 +100,9 @@ class NextFloor extends IPSModuleStrict
. htmlspecialchars($this->hookErrorMessage(), ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8') . '</div>';
}
$src = htmlspecialchars($this->designerUrl('tile'), ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8');
$title = htmlspecialchars($this->viewTitle(), ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8');
return '<style>html,body{margin:0;width:100%;height:100%;overflow:hidden}iframe{display:block;width:100%;height:100%;min-height:260px;border:0;background:#111}</style>'
. '<iframe title="NextFloor" src="' . $src . '" loading="eager" allow="fullscreen"></iframe>';
. '<iframe title="' . $title . '" src="' . $src . '" loading="eager" allow="fullscreen"></iframe>';
}
/** IP-Symcon calls this for /hook/nextfloor/<instance id>/... */
@@ -140,13 +146,14 @@ class NextFloor extends IPSModuleStrict
// --------------------------------------------------------------------- WebHook pages
private function ensureWebHook(): bool
private function ensureWebHook(bool $duringCreate = false): bool
{
// Native hooks are volatile. Create/ApplyChanges can run before WebHook Control is ready;
// register again after the kernel has started and whenever a designer/tile link is requested.
if (IPS_GetKernelRunlevel() !== KR_READY) {
// Register in Create as documented, even while Symcon initializes. Re-register after
// startup/update and keep maintaining the route: WebHook Control can reset later too.
$ready = IPS_GetKernelRunlevel() === KR_READY;
if (!$ready && !$duringCreate) {
$this->SetStatus(104);
$this->SetTimerInterval('WebHookRetry', 5000);
$this->SetTimerInterval('WebHookRetry', self::HOOK_RETRY_MS);
return false;
}
try {
@@ -156,17 +163,27 @@ class NextFloor extends IPSModuleStrict
throw new RuntimeException('Symcon hat die Registrierung von /hook/' . $this->hookPath() . ' abgelehnt.');
}
} catch (Throwable $error) {
if (!$ready) {
$this->SetStatus(104);
$this->SetTimerInterval('WebHookRetry', self::HOOK_RETRY_MS);
return false;
}
$message = $error->getMessage();
if ($message !== $this->ReadAttributeString('HookError')) {
$this->LogMessage('WebHook-Registrierung fehlgeschlagen: ' . $message, KL_ERROR);
}
$this->WriteAttributeString('HookError', $message);
$this->SetStatus(200);
$this->SetTimerInterval('WebHookRetry', 5000);
$this->SetTimerInterval('WebHookRetry', self::HOOK_RETRY_MS);
return false;
}
if (!$ready) {
$this->SetStatus(104);
$this->SetTimerInterval('WebHookRetry', self::HOOK_RETRY_MS);
return false;
}
$this->WriteAttributeString('HookError', '');
$this->SetTimerInterval('WebHookRetry', 0);
$this->SetTimerInterval('WebHookRetry', self::HOOK_MAINTENANCE_MS);
$this->SetStatus(102);
return true;
}
@@ -192,6 +209,11 @@ class NextFloor extends IPSModuleStrict
return 'nextfloor/' . $this->InstanceID;
}
private function viewTitle(): string
{
return trim($this->ReadPropertyString('Title')) ?: 'NextFloor';
}
private function designerUrl(string $mode): string
{
return '/hook/' . $this->hookPath() . '/designer?mode=' . rawurlencode($mode) . '&token=' . rawurlencode($this->ensureAccessToken());
@@ -243,7 +265,7 @@ class NextFloor extends IPSModuleStrict
'endpoint' => '/hook/' . $this->hookPath(),
'token' => $this->ensureAccessToken(),
'language' => 'de',
'title' => $this->ReadPropertyString('Title') ?: 'NextFloor'
'title' => $this->viewTitle()
];
$asset = '/hook/' . $this->hookPath() . '/assets/nextfloor-designer.js?v=' . rawurlencode($this->moduleVersion())
. '&token=' . rawurlencode($this->ensureAccessToken());
@@ -253,7 +275,7 @@ class NextFloor extends IPSModuleStrict
. '<title>' . htmlspecialchars($config['title'], ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8') . '</title>'
. '<style>html,body{width:100%;height:100%;margin:0;background:#111;color:#e1e1e1}body>nextfloor-panel,body>nextfloor-card{display:block;width:100%;height:100%}</style>'
. '</head><body><p style="padding:1rem;font-family:system-ui">NextFloor wird geladen …</p>'
. '<script>window.__NEXTFLOOR_SYMCON__=' . json_encode($config, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE) . ';</script>'
. '<script>window.__NEXTFLOOR_SYMCON__=' . json_encode($config, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE | JSON_HEX_TAG | JSON_HEX_AMP | JSON_HEX_APOS | JSON_HEX_QUOT) . ';</script>'
. '<script type="module" src="' . htmlspecialchars($asset, ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8') . '"></script></body></html>';
}