feat: release Symcon 0.2.2 with object tree selection and hook recovery

This commit is contained in:
jhartworks committed 2026-11-09 12:50:45 +01:00
1 parent 4a875aa8ea
commit eff9eb83d7
19 files changed
+2273 -1064

No files matched your search

+128 -12
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">'
@@ -239,7 +322,8 @@ class NextFloor extends IPSModuleStrict
'state_revision' => $stateRevision,
'building_revision' => $this->ReadAttributeInteger('BuildingRevision'),
'variables' => $objects['variables'],
'areas' => $objects['areas']
'areas' => $objects['areas'],
'objects' => $objects['objects']
];
}
@@ -254,24 +338,40 @@ class NextFloor extends IPSModuleStrict
if ($stateRevision !== $knownRevision) {
$result['variables'] = $objects['variables'];
$result['areas'] = $objects['areas'];
$result['objects'] = $objects['objects'];
}
return $result;
}
/** Returns every selectable variable and every category that can serve as a room area. */
/** Export real object nodes, not paths reconstructed from variable names. */
private function objectTree(): array
{
$variables = [];
$areas = [];
$nodes = [];
foreach (IPS_GetChildrenIDs(0) as $child) {
$this->collectObjects($child, [], null, $variables, $areas);
$this->collectObjects($child, [], null, $variables, $areas, $nodes);
}
$byVariable = [];
foreach ($variables as $variable) {
$byVariable[$variable['attributes']['symcon_id']] = $variable['entity_id'];
}
// A link stays at its original position in the tree and selects its target variable.
foreach ($nodes as &$node) {
if ($node['object_type'] === 6) {
$target = (int) IPS_GetLink($node['object_id'])['TargetID'];
if (isset($byVariable[$target])) {
$node['entity_id'] = $byVariable[$target];
}
}
}
unset($node);
usort($variables, static fn(array $a, array $b): int => strnatcasecmp($a['path'], $b['path']));
usort($areas, static fn(array $a, array $b): int => strnatcasecmp($a['name'], $b['name']));
return ['variables' => $variables, 'areas' => $areas];
return ['variables' => $variables, 'areas' => $areas, 'objects' => $nodes];
}
private function collectObjects(int $objectID, array $parents, ?int $nearestCategory, array &$variables, array &$areas): void
private function collectObjects(int $objectID, array $parents, ?int $nearestCategory, array &$variables, array &$areas, array &$nodes): void
{
if (!IPS_ObjectExists($objectID)) {
return;
@@ -280,15 +380,31 @@ class NextFloor extends IPSModuleStrict
$name = IPS_GetName($objectID);
$path = [...$parents, $name];
$type = (int) $object['ObjectType'];
$node = [
'object_id' => $objectID,
'parent_id' => (int) $object['ParentID'],
'name' => $name,
'path' => implode(' / ', $path),
'object_type' => $type,
'position' => (int) $object['ObjectPosition'],
'hidden' => (bool) $object['ObjectIsHidden'],
'icon' => (string) $object['ObjectIcon']
];
if ($type === 1) { // OBJECTTYPE_INSTANCE: the logical console view separates system modules.
$node['module_type'] = (int) IPS_GetInstance($objectID)['ModuleInfo']['ModuleType'];
}
if ($type === 0) { // OBJECTTYPE_CATEGORY
$nearestCategory = $objectID;
$areas[] = ['area_id' => 'symcon-category-' . $objectID, 'name' => implode(' / ', $path)];
}
if ($type === 2) { // OBJECTTYPE_VARIABLE
$variables[] = $this->describeVariable($objectID, $name, implode(' / ', $path), $nearestCategory);
$variable = $this->describeVariable($objectID, $name, implode(' / ', $path), $nearestCategory);
$variables[] = $variable;
$node['entity_id'] = $variable['entity_id'];
}
$nodes[] = $node;
foreach (IPS_GetChildrenIDs($objectID) as $child) {
$this->collectObjects($child, $path, $nearestCategory, $variables, $areas);
$this->collectObjects($child, $path, $nearestCategory, $variables, $areas, $nodes);
}
}