Sharing and accessSimulated data
Compare what each team sees
Each team sees and acts on only its own part of the twin, and every Action is audited. A local demo, not a test of production security.
What this example defines: its scene and, where it has them, its simulated source and its styling. The page imports and runs this file; the page's own renderer, panels and controls are not in it. For a complete app to run on your machine, download the quickstart starter.
import { auditActions, createAccessPolicy, createMemoryAuditLog, type World } from "@klorad/access";
import { connectSource, fixtureSource } from "@klorad/api/integration";
import { createInteraction, createScene, type SceneObjectSpec } from "@klorad/api/world";
import type { StyleFn } from "@klorad/engine-three";
import { WHITE_TOWER } from "./thessaloniki";
export const people = {
ana: { id: "ana", kind: "person", roles: ["admin"] },
kostas: { id: "kostas", kind: "person", roles: ["crew"], teams: ["crew-north"] },
maria: { id: "maria", kind: "person", roles: ["crew"], teams: ["crew-south"] },
} as const;
const worlds: World[] = [
{ id: "city", name: "City operations", scope: { all: true }, capabilities: { actions: "*" }, principals: [{ kind: "role", id: "admin" }] },
{ id: "north", name: "North crew", scope: { properties: { district: "north" } }, capabilities: { actions: ["mark-emptied"] }, principals: [{ kind: "team", id: "crew-north" }] },
{ id: "south", name: "South crew", scope: { properties: { district: "south" } }, capabilities: { actions: ["mark-emptied"] }, principals: [{ kind: "team", id: "crew-south" }] },
];
const bins: [string, "north" | "south", number, number][] = [
["n1", "north", -30, 70], ["n2", "north", 10, 95], ["n3", "north", 45, 75],
["s1", "south", -25, -80], ["s2", "south", 30, -95],
];
const specs: SceneObjectSpec[] = [
// The two districts are areas of the twin; a crew's World holds its own.
{ id: "district-north", name: "North district", position: { east: 0, north: 0 }, geometry: { kind: "polygon", footprint: [[-70, 30], [80, 30], [80, 125], [-70, 125]] }, properties: { district: "north", kind: "district" } },
{ id: "district-south", name: "South district", position: { east: 0, north: 0 }, geometry: { kind: "polygon", footprint: [[-70, -125], [80, -125], [80, -40], [-70, -40]] }, properties: { district: "south", kind: "district" } },
{ id: "depot", name: "Depot", position: { east: 0, north: -5 }, geometry: { kind: "extrusion", footprint: [[-15, -9], [15, -9], [15, 9], [-15, 9]], height: 6 }, properties: { kind: "depot" } },
...bins.map(([id, district, east, north]): SceneObjectSpec => ({
id,
name: `Bin ${id.toUpperCase()}`,
position: { east, north },
geometry: { kind: "cylinder", radius: 3, height: 6 },
correspondence: "shadow",
binding: { source: "bins", entity: id },
properties: { district, kind: "bin" },
})),
];
/** One twin of a city's smart bins, three Worlds over it, an audit log of every Action. */
export function createWorldsDemo() {
const scene = createScene({ name: "Bins", coordinateSystem: { origin: WHITE_TOWER } });
specs.forEach((s) => scene.add(s));
const audit = createMemoryAuditLog();
const policy = createAccessPolicy({ scene, roles: { admin: "*", crew: ["mark-emptied"] }, worlds, audit });
const interaction = createInteraction(scene, {
entitlement: policy,
actions: [{ id: "mark-emptied", label: "Mark emptied", appliesTo: (o) => o.correspondence === "shadow", behaviour: "record-emptied" }],
behaviours: [{ id: "record-emptied", run: ({ actor, update, animate }) => (update({ properties: { emptiedBy: actor.id } }), animate({ kind: "pulse" })) }],
});
const source = fixtureSource({
id: "bins",
intervalMs: 1500,
channels: bins.map(([id], i) => ({ entity: id, quantity: "fill", unit: "%", value: (t: number) => (20 + i * 13 + t * 3) % 100 })),
});
const connect = () => {
const c = connectSource(scene, source);
const stopAudit = auditActions(scene, audit);
return () => (c.stop(), stopAudit());
};
return { scene, policy, interaction, audit, connect };
}
/** A bin stands as tall as it is full and turns amber when it needs emptying. */
export const binStyle: StyleFn = ({ object, readings, stale, palette }) => {
const kind = object.properties.kind;
if (kind === "district") return { color: object.properties.district === "north" ? "#27cee7" : "#a78bfa", opacity: 0.14, label: object.name };
if (kind === "depot") return { color: palette.object, opacity: 0.5, label: object.name };
const fill = readings.fill?.value;
if (stale || typeof fill !== "number") return { color: palette.stale, label: `${object.name} · no reading` };
return { color: fill > 80 ? "#e8a33d" : palette.shadow, heightScale: Math.max(0.12, fill / 100), glow: fill > 80 ? 0.35 : 0, label: `${object.name} · ${fill} %` };
};Anchor40.62637° N 22.94838° E
East, north+0.0 m +0.0 m
Bin, as tall as it is fullNeeds emptyingNorth districtSouth districtNo reading