Files
genieacs/ui/device-page.ts
T
Zaid Humaimidi 6f3ce6f7c8 refactor: replace array-based expressions with typed class hierarchy
Replace the old Lisp-like array expression representation
(["AND", ["PARAM", ...], ...]) with a class hierarchy rooted in
an abstract Expression class (Literal, Parameter, Binary, Unary,
FunctionCall, Conditional). This gives compile-time type safety
to expression construction, evaluation, and pattern matching.

Key changes:
- New lib/common/expression.ts defines the class hierarchy with
  map/evaluate/toString methods
- Rewrite parser as hand-rolled recursive descent, removing the
  parsimmon dependency
- Extend Path with a colon separator to unify parameter identity
  and attributes into a single address space (e.g.
  "Device.WiFi.SSID.1.Name:writable"). The colon field tracks
  how many trailing segments are attribute qualifiers. PathSet
  rewritten with separate param/attr segment indexes
- Flatten device attribute representation in the UI to
  colon-delimited keys mirroring the new path syntax
2026-03-12 14:58:20 -07:00

69 lines
1.8 KiB
TypeScript

import { ClosureComponent } from "mithril";
import { m } from "./components.ts";
import { device as deviceConfig } from "./config.ts";
import * as store from "./store.ts";
import Expression from "../lib/common/expression.ts";
import Path from "../lib/common/path.ts";
export function init(
args: Record<string, unknown>,
): Promise<Record<string, unknown>> {
if (!window.authorizer.hasAccess("devices", 2)) {
return Promise.reject(
new Error("You are not authorized to view this page"),
);
}
return Promise.resolve({
deviceId: args.id,
deviceFilter: new Expression.Binary(
"=",
new Expression.Parameter(Path.parse("DeviceID.ID")),
new Expression.Literal(args.id as string),
),
});
}
interface Attrs {
deviceId: string;
deviceFilter: Expression;
}
export const component: ClosureComponent<Attrs> = () => {
return {
view: (vnode) => {
document.title = `${vnode.attrs.deviceId} - Devices - GenieACS`;
const dev = store.fetch("devices", vnode.attrs.deviceFilter);
if (!dev.value.length) {
if (!dev.fulfilling) {
return m(
"p.text-sm font-bold text-red-500",
`No such device ${vnode.attrs["deviceId"]}`,
);
}
return m(
"loading",
{ queries: [dev] },
m("div", { style: "height: 100px;" }),
);
}
const conf = deviceConfig;
const cmps = [];
for (const c of Object.values(conf)) {
cmps.push(
m.context(
{ device: dev.value[0], deviceQuery: dev },
store.evaluateExpression(c["type"], {}).value as string,
c as any,
),
);
}
return m("div.device-page", m("h1", vnode.attrs["deviceId"]), cmps);
},
};
};