fix: bound Symcon API responses below the kernel output limit
This commit is contained in:
1 parent
eff9eb83d7
commit
804f2d0494
9 files changed
+470
-110
No files matched your search
@@ -5,6 +5,7 @@
|
||||
// only ever shows and selects their object-tree path. The numeric object id stays an implementation detail.
|
||||
|
||||
import type { HassArea, HassEntity, HassEntityEntry, HomeAssistant, SymconObject } from "./types.ts";
|
||||
import { callSymcon as call } from "./symcon-api.ts";
|
||||
|
||||
interface SymconConfig {
|
||||
endpoint: string;
|
||||
@@ -54,22 +55,6 @@ function config(): SymconConfig {
|
||||
return settings;
|
||||
}
|
||||
|
||||
/** Same-origin JSON call to the instance's protected WebHook. */
|
||||
async function call<T>(settings: SymconConfig, action: string, payload: Record<string, unknown> = {}): Promise<T> {
|
||||
const response = await fetch(`${settings.endpoint}/api?token=${encodeURIComponent(settings.token)}`, {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/json", Accept: "application/json" },
|
||||
credentials: "same-origin",
|
||||
body: JSON.stringify({ action, ...payload }),
|
||||
});
|
||||
const body: unknown = await response.json().catch(() => ({ error: `HTTP ${response.status}` }));
|
||||
if (!response.ok || !body || typeof body !== "object" || "error" in body) {
|
||||
const error = body && typeof body === "object" && "error" in body ? (body as { error?: unknown }).error : `HTTP ${response.status}`;
|
||||
throw new Error(String(error));
|
||||
}
|
||||
return body as T;
|
||||
}
|
||||
|
||||
/**
|
||||
* Turns the instance WebHook into the interface used by the NextFloor UI. Polling is deliberate here: it
|
||||
* works in the designer tab, an iframe in the Tile visualization and a remote WebFront without exposing an
|
||||
@@ -83,6 +68,7 @@ export class SymconBridge {
|
||||
private stateRevision = -1;
|
||||
private buildingRevision = -1;
|
||||
private objectsSignature = "";
|
||||
private refreshing = false;
|
||||
private states: Record<string, HassEntity> = {};
|
||||
private entities: Record<string, HassEntityEntry> = {};
|
||||
private areas: Record<string, HassArea> = {};
|
||||
@@ -140,6 +126,8 @@ export class SymconBridge {
|
||||
}
|
||||
|
||||
private async refresh(): Promise<void> {
|
||||
if (this.refreshing) return;
|
||||
this.refreshing = true;
|
||||
try {
|
||||
const next = await call<VariableSnapshot>(this.settings, "variables/snapshot", { revision: this.stateRevision });
|
||||
if (next.state_revision !== this.stateRevision && next.variables) {
|
||||
@@ -153,6 +141,8 @@ export class SymconBridge {
|
||||
}
|
||||
} catch {
|
||||
// A temporary unavailable WebHook must not blank a running visualization. The next poll retries.
|
||||
} finally {
|
||||
this.refreshing = false;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user