# HG changeset patch # User drewp@bigasterisk.com # Date 2022-05-24 08:00:40 # Node ID 46a8f5a8a4ddf1f0caeb7e879da4a0d3160cec62 # Parent 33f65e2d0e592dcecf501d4dc22c03ffa29af174 fix collector display to show real values from py diff --git a/light9/collector/web/Light9CollectorUi.ts b/light9/collector/web/Light9CollectorUi.ts --- a/light9/collector/web/Light9CollectorUi.ts +++ b/light9/collector/web/Light9CollectorUi.ts @@ -2,13 +2,14 @@ import debug from "debug"; import { html, LitElement } from "lit"; import { customElement, property } from "lit/decorators.js"; import { NamedNode } from "n3"; +import ReconnectingWebSocket from "reconnectingwebsocket"; import { sortBy, uniq } from "underscore"; +import { Light9CollectorDevice } from "../../web/collector/Light9CollectorDevice"; import { Patch } from "../../web/patch"; import { getTopGraph } from "../../web/RdfdbSyncedGraph"; import { SyncedGraph } from "../../web/SyncedGraph"; - -export { Light9CollectorDevice } from "../../web/collector/Light9CollectorDevice"; export { RdfdbSyncedGraph } from "../../web/RdfdbSyncedGraph"; +export { Light9CollectorDevice }; debug.enable("*"); const log = debug("collector"); @@ -21,7 +22,7 @@ export class Light9CollectorUi extends L

Collector [metrics]

Devices

-
${this.devices.map((d) => html``)}
`; +
${this.devices.map((d) => html``)}
`; } @property() devices: NamedNode[] = []; @@ -32,12 +33,21 @@ export class Light9CollectorUi extends L this.graph = g; this.graph.runHandler(this.findDevices.bind(this), "findDevices"); }); + + const ws = new ReconnectingWebSocket(location.href.replace("http", "ws") + "api/updates"); + ws.addEventListener("message", (ev: any) => { + const outputAttrsSet = JSON.parse(ev.data).outputAttrsSet; + if (outputAttrsSet) { + this.updateDev(outputAttrsSet.dev, outputAttrsSet.attrs); + } + }); } findDevices(patch?: Patch) { const U = this.graph.U(); this.devices = []; + this.clearDeviceChildElementCache(); let classes = this.graph.subjects(U("rdf:type"), U(":DeviceClass")); uniq(sortBy(classes, "value"), true).forEach((dc) => { sortBy(this.graph.subjects(U("rdf:type"), dc), "value").forEach((dev) => { @@ -45,4 +55,37 @@ export class Light9CollectorUi extends L }); }); } + + deviceElements: Map = new Map(); + + clearDeviceChildElementCache() { + this.deviceElements = new Map(); + } + + findDeviceChildElement(uri: string): Light9CollectorDevice | undefined { + const known = this.deviceElements.get(uri); + if (known) { + return known; + } + + for (const el of this.shadowRoot!.querySelectorAll("light9-collector-device")) { + const eld = el as Light9CollectorDevice; + if (eld.uri.value == uri) { + this.deviceElements.set(uri, eld); + return eld; + } + } + + return undefined; + } + + updateDev(uri: string, attrs: { attr: string; chan: string; val: string; valClass: string }[]) { + const el = this.findDeviceChildElement(uri); + if (!el) { + // unresolved race: updates come in before we have device elements to display them + setTimeout(() => this.updateDev(uri, attrs), 300); + return; + } + el.setAttrs(attrs); + } } diff --git a/light9/web/collector/Light9CollectorDevice.ts b/light9/web/collector/Light9CollectorDevice.ts --- a/light9/web/collector/Light9CollectorDevice.ts +++ b/light9/web/collector/Light9CollectorDevice.ts @@ -4,7 +4,6 @@ import { customElement, property } from import { NamedNode } from "n3"; export { ResourceDisplay } from "../ResourceDisplay"; -debug.enable("*"); const log = debug("device-el"); @customElement("light9-collector-device") @@ -58,26 +57,19 @@ export class Light9CollectorDevice exten `; } @property({ - // todo don't rebuild uri; pass it right - converter: (s: string | null) => new NamedNode(s || ""), + converter: acceptStringOrUri(), }) uri: NamedNode = new NamedNode(""); @property() attrs: Array<{ attr: string; valClass: string; val: string; chan: string }> = []; - constructor() { - super(); + setAttrs(attrs: any) { + this.attrs = attrs; + this.attrs.forEach(function (row: any) { + row.valClass = row.val == 255 ? "full" : row.val ? "nonzero" : ""; + }); } - // observers: [ - // "initUpdates(updates)", - // ], - // initUpdates(updates) { - // updates.addListener(function (msg) { - // if (msg.outputAttrsSet && msg.outputAttrsSet.dev == this.uri.value) { - // this.set("attrs", msg.outputAttrsSet.attrs); - // this.attrs.forEach(function (row) { - // row.valClass = row.val == 255 ? "full" : row.val ? "nonzero" : ""; - // }); - // } - // }); - // } } + +function acceptStringOrUri() { + return (s: string | null) => new NamedNode(s || ""); +}