Advanced SDK behaviourA simulated device
Send a command, wait for the device
Only an entitled person can open the barrier, and the twin shows it open only once the device confirms.
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 { createAccessPolicy } from "@klorad/access";
import { connectSource, fixtureDevice } from "@klorad/api/integration";
import { createInteraction, createScene, type ActionDefinition, type BehaviourDefinition } from "@klorad/api/world";
import type { StyleFn } from "@klorad/engine-three";
import { WHITE_TOWER } from "./thessaloniki";
export const SITE = "site";
export const actors = {
operator: { id: "operator", kind: "person", roles: ["operator"] },
visitor: { id: "visitor", kind: "person", roles: ["visitor"] },
} as const;
const actions: ActionDefinition[] = [
{
id: "dim",
label: "Dim or brighten lamp",
appliesTo: (o) => o.properties.kind === "lamp",
behaviour: "set-level",
},
{
id: "open",
label: "Open barrier",
appliesTo: (o) => o.correspondence === "twin",
behaviour: "request",
command: () => ({ name: "open" }),
},
{
id: "close",
label: "Close barrier",
appliesTo: (o) => o.correspondence === "twin",
behaviour: "request",
command: () => ({ name: "close" }),
},
];
const behaviours: BehaviourDefinition[] = [
{
id: "set-level",
run: ({ object, update, animate }) => {
update({ properties: { level: object.properties.level === 30 ? 100 : 30 } });
animate({ kind: "pulse" });
},
},
{
id: "request",
// why: the twin records what was asked; what the barrier really does arrives as an observation
run: ({ update, animate }) => {
update({ properties: { requestedAt: new Date().toISOString() } });
animate({ kind: "highlight" });
},
},
];
/** The entrance of a site: a road, a booth, a street lamp (a Digital Object) and a barrier (the Digital Twin of a simulated PLC). */
export function createActionsDemo() {
const scene = createScene({ name: "Site entrance", coordinateSystem: { origin: WHITE_TOWER } });
scene.add({ id: "road", name: "Access road", position: { east: 0, north: 0 }, geometry: { kind: "polyline", path: [[0, -30], [0, 30]], width: 7 }, properties: { kind: "road" } });
scene.add({ id: "booth", name: "Guard booth", position: { east: -7.5, north: 1 }, geometry: { kind: "extrusion", footprint: [[-1.6, -2], [1.6, -2], [1.6, 2], [-1.6, 2]], height: 2.8 }, properties: { kind: "booth" } });
scene.add({ id: "post", name: "Barrier post", position: { east: -4.4, north: -2 }, geometry: { kind: "box", width: 0.5, depth: 0.5, height: 1.1 }, properties: { kind: "post" } });
scene.add({ id: "lamp", name: "Street lamp", position: { east: 5.2, north: 5 }, orientation: { heading: 270 }, geometry: { kind: "model", uri: "/models/street-lamp.glb" }, properties: { kind: "lamp", level: 100 } });
scene.add({
id: "barrier",
name: "Entrance barrier",
// The arm starts at its hinge on the post and points east across the road: pitching it up lifts it.
position: { east: -4.4, north: -2, up: 1 },
orientation: { heading: 90 },
geometry: { kind: "extrusion", footprint: [[-0.1, 0], [0.1, 0], [0.1, 8], [-0.1, 8]], height: 0.2 },
correspondence: "twin",
binding: { source: "plc", entity: "barrier-1" },
});
const plc = fixtureDevice({
id: "plc",
latencyMs: 1200,
entities: { "barrier-1": { state: "closed" } },
respond: (cmd, s) => {
const target = cmd.name === "open" ? "open" : "closed";
return s.state === target ? `already ${target}` : { state: target };
},
});
const policy = createAccessPolicy({
scene,
roles: { operator: "*", visitor: [] },
worlds: [{ id: SITE, name: "Site", scope: { all: true }, capabilities: { actions: "*" }, principals: [{ kind: "role", id: "operator" }, { kind: "role", id: "visitor" }] }],
});
const interaction = createInteraction(scene, { entitlement: policy, actions, behaviours, actuators: [plc] });
// why: connect in an effect, so React's strict mode can stop and restart it safely
return { scene, interaction, connect: () => connectSource(scene, plc).stop };
}
/** The lamp glows as bright as its level; the barrier lifts only when the device reports that it opened. */
export const actionsStyle: StyleFn = ({ object, readings, palette }) => {
const kind = object.properties.kind;
if (kind === "road") return { color: "#3b5568", opacity: 0.9 };
if (kind === "booth" || kind === "post") return { color: palette.object, opacity: kind === "post" ? 1 : 0.5 };
if (kind === "lamp") return { glow: Number(object.properties.level) / 100, label: `Lamp ${object.properties.level} %` };
const state = readings.state?.value;
return state === "open" ? { color: "#3ccf91", pose: { pitch: 80 }, label: "Barrier open" } : { color: palette.twin, label: `Barrier ${state ?? "unknown"}` };
};Anchor40.62637° N 22.94838° E
East, north+0.0 m +0.0 m
Digital ObjectDigital ShadowDigital TwinStale