Connected dataStart here: 4 of 4Simulated data
Watch readings arrive, late and stale
A reading that arrives late or twice never overwrites a newer one, and a quiet sensor shows as stale.
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 { fixtureSource } from "@klorad/api/integration";
import type { SceneObjectSpec } from "@klorad/api/world";
import type { StyleFn } from "@klorad/engine-three";
export const SOURCE_ID = "fixture";
const bound = (entity: string) => ({ correspondence: "shadow" as const, binding: { source: SOURCE_ID, entity } });
/** An exhibition hall and the three sensors that report from it. Positions are metres east and north of the scene's origin. */
export const shadowObjects: SceneObjectSpec[] = [
{ id: "forecourt", name: "Forecourt", position: { east: 0, north: 0 }, geometry: { kind: "polygon", footprint: [[-20, -22], [22, -22], [22, 14], [-20, 14]] } },
{ id: "hall", name: "Exhibition hall", position: { east: 0, north: 0 }, geometry: { kind: "extrusion", footprint: [[-14, -8], [14, -8], [14, 3], [5, 3], [5, 9], [-14, 9]], height: 7 } },
// A counter at the entrance: the renderer draws it as tall as the hall is full.
{ id: "entrance", name: "Entrance counter", position: { east: 9, north: -11 }, geometry: { kind: "box", width: 2, depth: 2, height: 1 }, ...bound("entrance-01") },
// A sensor hanging under the roof.
{ id: "climate", name: "Hall climate", position: { east: -6, north: 2, up: 5 }, geometry: { kind: "sphere", radius: 0.9 }, ...bound("climate-01") },
// A door leaf hinged at its position: the footprint starts at the hinge, so a turn swings it open.
{ id: "door", name: "Fire door", position: { east: -14, north: -5 }, geometry: { kind: "extrusion", footprint: [[-0.15, 0], [0.15, 0], [0.15, 2.4], [-0.15, 2.4]], height: 2.6 }, ...bound("door-07") },
];
export interface FixtureOptions {
/** Milliseconds between readings. Default 1000. */
readonly intervalMs?: number;
/** How late the climate sensor's every fourth reading arrives, in milliseconds. Default 2500. */
readonly lateMs?: number;
}
/**
* One reading per channel per interval. The climate sensor's every fourth reading arrives
* late, so it lands behind newer ones: the Time Spectrum must not let it win.
*/
export function createFixture({ intervalMs = 1000, lateMs = 2500 }: FixtureOptions = {}) {
return fixtureSource({
id: SOURCE_ID,
intervalMs,
channels: [
{ entity: "entrance-01", quantity: "occupancy", unit: "people", value: (t) => 20 + Math.round(15 * Math.sin(t / 6)) + (t % 3) },
{ entity: "climate-01", quantity: "temperature", unit: "°C", value: (t) => Math.round((22 + Math.sin(t / 10)) * 10) / 10, lagMs: (t) => (t % 4 === 3 ? lateMs : 0) },
{ entity: "door-07", quantity: "door", value: (t) => (Math.floor(t / 7) % 2 === 0 ? "closed" : "open") },
],
});
}
/** Domain state as looks: the counter rises with occupancy, an open door swings out and turns amber, everything greys when its readings go quiet. */
export const shadowStyle: StyleFn = ({ object, readings, stale, palette }) => {
if (object.id === "forecourt") return { color: "#3b5568", opacity: 0.35 };
if (object.correspondence === "object") return { color: palette.object, opacity: 0.4 };
const reading = Object.values(readings)[0];
const label = reading ? `${reading.value}${reading.unit ? ` ${reading.unit}` : ""}` : "no reading";
if (stale) return { color: palette.stale, opacity: 0.7, label: `${label} · stale` };
const occupancy = readings.occupancy?.value;
if (typeof occupancy === "number") return { color: palette.shadow, heightScale: Math.max(0.4, occupancy / 8), label };
if (readings.door?.value === "open") return { color: "#e8a33d", pose: { heading: -75 }, glow: 0.4, label };
return { color: palette.shadow, label };
};Anchor40.62637° N 22.94838° E
East, north+0.0 m +0.0 m
Digital ObjectDigital ShadowDigital TwinStale