fixed deleted hook

This commit is contained in:
jhartworks committed 2026-11-09 12:30:47 +01:00
1 parent 662763d5a4
commit 1de0a60bd0
6 files changed
+263 -9

No files matched your search

+88 -5
View File
@@ -26,25 +26,51 @@ class NextFloor extends IPSModuleStrict
$this->RegisterAttributeString('History', '[]');
$this->RegisterAttributeString('AccessToken', '');
$this->RegisterAttributeString('StateHash', '');
$this->RegisterAttributeString('HookError', '');
$this->RegisterAttributeInteger('BuildingRevision', 0);
$this->RegisterAttributeInteger('StateRevision', 0);
$this->RegisterMessage(0, IPS_KERNELSTARTED);
$this->RegisterTimer('WebHookRetry', 0, 'NFSC_RetryWebHook($_IPS[\'TARGET\']);');
$this->ensureAccessToken();
$this->SetVisualizationType(1);
$this->RegisterHook($this->hookPath());
$this->ensureWebHook();
}
public function ApplyChanges(): void
{
parent::ApplyChanges();
$this->SetVisualizationType(1);
$this->RegisterHook($this->hookPath());
$this->SetStatus(102);
$this->ensureWebHook();
}
public function MessageSink(int $TimeStamp, int $SenderID, int $Message, array $Data): void
{
parent::MessageSink($TimeStamp, $SenderID, $Message, $Data);
if ($SenderID === 0 && $Message === IPS_KERNELSTARTED) {
$this->ensureWebHook();
}
}
/** Timer callback; stops itself as soon as the volatile hook has been restored. */
public function RetryWebHook(): void
{
$this->ensureWebHook();
}
/** Explicit recovery from the console without recreating the instance or changing its project. */
public function RepairWebHook(): string
{
if (!$this->ensureWebHook()) {
return $this->hookErrorMessage();
}
return 'WebHook registriert: /hook/' . $this->hookPath() . '. Jetzt den Designer öffnen.';
}
/** URL returned to the console form. It opens in a console tab, as IPSView does. */
public function OpenDesigner(): string
{
$this->requireWebHook();
// `link:` is the documented Form-Button marker for a relative Symcon WebHook URL.
return 'link:' . $this->designerUrl('designer');
}
@@ -52,6 +78,7 @@ class NextFloor extends IPSModuleStrict
/** Invalidates links from copied browser history and returns a fresh designer URL. */
public function RotateAccessToken(): string
{
$this->requireWebHook();
$this->WriteAttributeString('AccessToken', $this->newToken());
return 'link:' . $this->designerUrl('designer');
}
@@ -59,6 +86,10 @@ class NextFloor extends IPSModuleStrict
/** The custom tile is deliberately an iframe: the full 3D viewer can lazy-load its assets via the same protected hook. */
public function GetVisualizationTile(): string
{
if (!$this->ensureWebHook()) {
return '<div style="padding:16px;font-family:system-ui">'
. htmlspecialchars($this->hookErrorMessage(), ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8') . '</div>';
}
$src = htmlspecialchars($this->designerUrl('tile'), 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>';
@@ -67,7 +98,11 @@ class NextFloor extends IPSModuleStrict
/** IP-Symcon calls this for /hook/nextfloor/<instance id>/... */
protected function ProcessHookData(): void
{
$path = (string) (parse_url((string) ($_SERVER['REQUEST_URI'] ?? ''), PHP_URL_PATH) ?? '');
$requestPath = (string) ($_SERVER['REQUEST_URI'] ?? '');
if ($requestPath === '') {
$requestPath = (string) ($_SERVER['SCRIPT_NAME'] ?? '');
}
$path = (string) (parse_url($requestPath, PHP_URL_PATH) ?? '');
$prefix = '/hook/' . $this->hookPath();
if (!str_starts_with($path, $prefix)) {
$this->sendError(404, 'not_found');
@@ -101,6 +136,53 @@ class NextFloor extends IPSModuleStrict
// --------------------------------------------------------------------- WebHook pages
private function ensureWebHook(): 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) {
$this->SetStatus(104);
$this->SetTimerInterval('WebHookRetry', 5000);
return false;
}
try {
$registered = $this->RegisterHook($this->hookPath());
// Older native implementations return void; an explicit false reports rejection.
if ($registered === false) {
throw new RuntimeException('Symcon hat die Registrierung von /hook/' . $this->hookPath() . ' abgelehnt.');
}
} catch (Throwable $error) {
$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);
return false;
}
$this->WriteAttributeString('HookError', '');
$this->SetTimerInterval('WebHookRetry', 0);
$this->SetStatus(102);
return true;
}
private function requireWebHook(): void
{
if (!$this->ensureWebHook()) {
throw new RuntimeException($this->hookErrorMessage());
}
}
private function hookErrorMessage(): string
{
if (IPS_GetKernelRunlevel() !== KR_READY) {
return 'IP-Symcon startet noch. Der NextFloor-WebHook wird nach dem Start automatisch registriert.';
}
return 'NextFloor-WebHook nicht verfügbar: ' . $this->ReadAttributeString('HookError')
. ' Bitte in der NextFloor-Instanz „WebHook reparieren“ ausführen.';
}
private function hookPath(): string
{
return 'nextfloor/' . $this->InstanceID;
@@ -159,7 +241,8 @@ class NextFloor extends IPSModuleStrict
'language' => 'de',
'title' => $this->ReadPropertyString('Title') ?: 'NextFloor'
];
$asset = '/hook/' . $this->hookPath() . '/assets/nextfloor-designer.js?token=' . rawurlencode($this->ensureAccessToken());
$asset = '/hook/' . $this->hookPath() . '/assets/nextfloor-designer.js?v=' . rawurlencode($this->moduleVersion())
. '&token=' . rawurlencode($this->ensureAccessToken());
header('Content-Type: text/html; charset=utf-8');
header('Cache-Control: no-store');
echo '<!doctype html><html lang="de"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1">'