@avantics/node-red-home-automation 0.0.0-stage → 2.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +292 -2
- package/contract-examples/README.md +28 -0
- package/contract-examples/_shared.mjs +11 -0
- package/contract-examples/motion-lighting.mjs +42 -0
- package/contract-examples/occupancy-fusion.mjs +35 -0
- package/contract-examples/room-lighting.mjs +58 -0
- package/contract-examples/run.mjs +16 -0
- package/contract-examples/source-readiness.mjs +64 -0
- package/contract-examples/switch-gestures.mjs +34 -0
- package/contract-examples/theatre-activity.mjs +62 -0
- package/contract-examples/volume-ownership.mjs +59 -0
- package/dist/lib/activity-lighting-controller.d.ts +129 -0
- package/dist/lib/activity-lighting-controller.js +372 -0
- package/dist/lib/avr-controller.d.ts +169 -0
- package/dist/lib/avr-controller.js +948 -0
- package/dist/lib/backlighting-controller.d.ts +108 -0
- package/dist/lib/backlighting-controller.js +269 -0
- package/dist/lib/bounded-trace.d.ts +30 -0
- package/dist/lib/bounded-trace.js +36 -0
- package/dist/lib/circadian-refresh.d.ts +7 -0
- package/dist/lib/circadian-refresh.js +11 -0
- package/dist/lib/circadian-schedule.d.ts +11 -0
- package/dist/lib/circadian-schedule.js +63 -0
- package/dist/lib/circadian.d.ts +6 -0
- package/dist/lib/circadian.js +15 -0
- package/dist/lib/cover-controller.d.ts +130 -0
- package/dist/lib/cover-controller.js +336 -0
- package/dist/lib/display-mode-controller.d.ts +148 -0
- package/dist/lib/display-mode-controller.js +587 -0
- package/dist/lib/display-transport.d.ts +41 -0
- package/dist/lib/display-transport.js +177 -0
- package/dist/lib/light-styling.d.ts +11 -0
- package/dist/lib/light-styling.js +15 -0
- package/dist/lib/motion-policy.d.ts +72 -0
- package/dist/lib/motion-policy.js +251 -0
- package/dist/lib/observation-time.d.ts +10 -0
- package/dist/lib/observation-time.js +40 -0
- package/dist/lib/occupancy-fusion.d.ts +53 -0
- package/dist/lib/occupancy-fusion.js +131 -0
- package/dist/lib/room-controller.d.ts +113 -0
- package/dist/lib/room-controller.js +431 -0
- package/dist/lib/source-capability-controller.d.ts +144 -0
- package/dist/lib/source-capability-controller.js +494 -0
- package/dist/lib/switch-policy.d.ts +53 -0
- package/dist/lib/switch-policy.js +110 -0
- package/dist/lib/theatre-activity-controller.d.ts +165 -0
- package/dist/lib/theatre-activity-controller.js +846 -0
- package/dist/lib/theatre-slo.d.ts +49 -0
- package/dist/lib/theatre-slo.js +156 -0
- package/dist/lib/theatre-state-contract.d.ts +77 -0
- package/dist/lib/theatre-state-contract.js +391 -0
- package/dist/lib/wall-input.d.ts +62 -0
- package/dist/lib/wall-input.js +198 -0
- package/dist/nodes/activity-lighting-controller.d.ts +3 -0
- package/dist/nodes/activity-lighting-controller.html +38 -0
- package/dist/nodes/activity-lighting-controller.js +116 -0
- package/dist/nodes/avr-controller.d.ts +3 -0
- package/dist/nodes/avr-controller.html +47 -0
- package/dist/nodes/avr-controller.js +130 -0
- package/dist/nodes/backlighting-controller.d.ts +3 -0
- package/dist/nodes/backlighting-controller.html +31 -0
- package/dist/nodes/backlighting-controller.js +103 -0
- package/dist/nodes/display-mode-controller.d.ts +3 -0
- package/dist/nodes/display-mode-controller.html +66 -0
- package/dist/nodes/display-mode-controller.js +132 -0
- package/dist/nodes/motion-policy.d.ts +3 -0
- package/dist/nodes/motion-policy.html +35 -0
- package/dist/nodes/motion-policy.js +194 -0
- package/dist/nodes/occupancy-fusion.d.ts +3 -0
- package/dist/nodes/occupancy-fusion.html +32 -0
- package/dist/nodes/occupancy-fusion.js +53 -0
- package/dist/nodes/room-controller.d.ts +3 -0
- package/dist/nodes/room-controller.html +82 -0
- package/dist/nodes/room-controller.js +123 -0
- package/dist/nodes/source-capability-controller.d.ts +3 -0
- package/dist/nodes/source-capability-controller.html +46 -0
- package/dist/nodes/source-capability-controller.js +114 -0
- package/dist/nodes/switch-policy.d.ts +3 -0
- package/dist/nodes/switch-policy.html +29 -0
- package/dist/nodes/switch-policy.js +129 -0
- package/dist/nodes/theatre-activity-controller.d.ts +3 -0
- package/dist/nodes/theatre-activity-controller.html +32 -0
- package/dist/nodes/theatre-activity-controller.js +105 -0
- package/dist/nodes/theatre-state-contract.d.ts +3 -0
- package/dist/nodes/theatre-state-contract.html +41 -0
- package/dist/nodes/theatre-state-contract.js +83 -0
- package/examples/Home theatre activity.json +477 -0
- package/examples/Home theatre equipment.json +329 -0
- package/examples/Room lighting.json +212 -0
- package/package.json +62 -4
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import {
|
|
2
|
+
normaliseActivityProfiles,
|
|
3
|
+
transitionTheatre
|
|
4
|
+
} from "../dist/lib/theatre-activity-controller.js";
|
|
5
|
+
import {assert, runWhenMain} from "./_shared.mjs";
|
|
6
|
+
|
|
7
|
+
export const name = "theatre-activity";
|
|
8
|
+
|
|
9
|
+
export function run() {
|
|
10
|
+
const activityProfiles = normaliseActivityProfiles({
|
|
11
|
+
movie_night: {
|
|
12
|
+
display: {mode: "projection"},
|
|
13
|
+
avr: {power: "on", input: "media"},
|
|
14
|
+
source: {action: "activate", sourceId: "streamer", readiness: "required"},
|
|
15
|
+
lighting: {profile: "dim"},
|
|
16
|
+
backlighting: {power: "off"}
|
|
17
|
+
}
|
|
18
|
+
});
|
|
19
|
+
const configuration = {area: "demo_room", freshnessMs: 30000, activityProfiles};
|
|
20
|
+
const intent = {
|
|
21
|
+
kind: "intent",
|
|
22
|
+
requestId: "movie-night-1",
|
|
23
|
+
generation: 1,
|
|
24
|
+
activity: "movie_night"
|
|
25
|
+
};
|
|
26
|
+
let result = transitionTheatre(undefined, {theatre: intent}, configuration, "2026-01-01T20:00:00.000Z");
|
|
27
|
+
const targetIntents = [...result.commands];
|
|
28
|
+
const targets = ["display", "avr", "source", "lighting", "backlighting"];
|
|
29
|
+
|
|
30
|
+
for (const [index, target] of targets.entries()) {
|
|
31
|
+
const observedAt = `2026-01-01T20:00:0${index + 1}.000Z`;
|
|
32
|
+
result = transitionTheatre(result.state, {theatre: {
|
|
33
|
+
kind: "target_state",
|
|
34
|
+
target,
|
|
35
|
+
generation: 1,
|
|
36
|
+
status: "ready",
|
|
37
|
+
observedAt
|
|
38
|
+
}}, configuration, observedAt);
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
assert.equal(targetIntents.length, 5);
|
|
42
|
+
assert.deepEqual(targetIntents.map(({target}) => target), targets);
|
|
43
|
+
assert.equal(result.state.phase, "ready");
|
|
44
|
+
assert.equal(result.state.currentActivity, "movie_night");
|
|
45
|
+
assert.equal(result.state.availability, "available");
|
|
46
|
+
|
|
47
|
+
return {
|
|
48
|
+
configuration: {activities: Object.keys(activityProfiles), freshnessMs: configuration.freshnessMs},
|
|
49
|
+
inputs: [intent, {kind: "target_state", targets, status: "ready"}],
|
|
50
|
+
outputs: {
|
|
51
|
+
targetIntents,
|
|
52
|
+
state: {
|
|
53
|
+
currentActivity: result.state.currentActivity,
|
|
54
|
+
phase: result.state.phase,
|
|
55
|
+
availability: result.state.availability
|
|
56
|
+
}
|
|
57
|
+
},
|
|
58
|
+
invariant: "The activity becomes ready only after fresh evidence from every mandatory target owner."
|
|
59
|
+
};
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
runWhenMain(import.meta.url, name, run);
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import {transitionAvr} from "../dist/lib/avr-controller.js";
|
|
2
|
+
import {assert, runWhenMain} from "./_shared.mjs";
|
|
3
|
+
|
|
4
|
+
export const name = "volume-ownership";
|
|
5
|
+
|
|
6
|
+
export function run() {
|
|
7
|
+
const configuration = {
|
|
8
|
+
area: "media-room",
|
|
9
|
+
sources: {streamer: "MEDIA"},
|
|
10
|
+
retryMs: 1000,
|
|
11
|
+
maxAttempts: 2,
|
|
12
|
+
minVolume: 10,
|
|
13
|
+
maxVolume: 45
|
|
14
|
+
};
|
|
15
|
+
const inputs = [
|
|
16
|
+
{kind: "observed", line: "PWON\rZMON\rSIMEDIA\rMV30\r"},
|
|
17
|
+
{kind: "volume", requestId: "volume-1", mode: "relative", value: 1},
|
|
18
|
+
{kind: "volume", requestId: "volume-2", mode: "relative", value: 1},
|
|
19
|
+
{kind: "observed", line: "MV32"},
|
|
20
|
+
{kind: "observed", line: "MV28"}
|
|
21
|
+
];
|
|
22
|
+
|
|
23
|
+
let result = transitionAvr(undefined, {avr: inputs[0]}, configuration, "2026-01-01T12:00:00.000Z");
|
|
24
|
+
const commands = [];
|
|
25
|
+
for (const [index, input] of inputs.slice(1).entries()) {
|
|
26
|
+
result = transitionAvr(
|
|
27
|
+
result.state,
|
|
28
|
+
{avr: input},
|
|
29
|
+
configuration,
|
|
30
|
+
`2026-01-01T12:00:0${index + 1}.000Z`
|
|
31
|
+
);
|
|
32
|
+
commands.push(...result.commands.map(({payload, avrCommand}) => ({payload, operation: avrCommand.operation})));
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
assert.deepEqual(commands, [
|
|
36
|
+
{payload: "MV31\r", operation: "volume"},
|
|
37
|
+
{payload: "MV32\r", operation: "volume"}
|
|
38
|
+
]);
|
|
39
|
+
assert.equal(result.state.observed.volume, 28);
|
|
40
|
+
assert.equal(result.state.volumeRequest, null);
|
|
41
|
+
assert.deepEqual(result.state.volumeMetrics, {
|
|
42
|
+
intents: 2,
|
|
43
|
+
commands: 2,
|
|
44
|
+
bounded: 0,
|
|
45
|
+
confirmations: 1,
|
|
46
|
+
externalChanges: 1,
|
|
47
|
+
failures: 0
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
return {
|
|
51
|
+
configuration,
|
|
52
|
+
inputs,
|
|
53
|
+
commands,
|
|
54
|
+
outputs: [{observedVolume: result.state.observed.volume, metrics: result.state.volumeMetrics}],
|
|
55
|
+
invariant: "Relative requests accumulate, confirmation releases ownership, and later external adjustment becomes the new baseline."
|
|
56
|
+
};
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
runWhenMain(import.meta.url, name, run);
|
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
import type { LightingEvent } from "./room-controller";
|
|
2
|
+
export interface ActivityLightingProfile {
|
|
3
|
+
targets: string[];
|
|
4
|
+
values: Record<string, unknown>;
|
|
5
|
+
}
|
|
6
|
+
export type ActivityLightingProfiles = Record<string, ActivityLightingProfile>;
|
|
7
|
+
export interface ActivityLightingConfig {
|
|
8
|
+
profiles: ActivityLightingProfiles;
|
|
9
|
+
owner: string;
|
|
10
|
+
confirmationTimeoutMs: number;
|
|
11
|
+
maxAttempts: number;
|
|
12
|
+
}
|
|
13
|
+
export interface ActivityLightingDesired {
|
|
14
|
+
requestId: string;
|
|
15
|
+
generation: number;
|
|
16
|
+
profile: string;
|
|
17
|
+
requestedAt: string;
|
|
18
|
+
}
|
|
19
|
+
export interface ActivityLightingObservation {
|
|
20
|
+
target: string;
|
|
21
|
+
on: boolean;
|
|
22
|
+
manual: boolean;
|
|
23
|
+
available: boolean;
|
|
24
|
+
values: Record<string, unknown>;
|
|
25
|
+
observedAt: string;
|
|
26
|
+
}
|
|
27
|
+
export interface ActivityLightingPending {
|
|
28
|
+
attempt: number;
|
|
29
|
+
deadlineAt: number;
|
|
30
|
+
}
|
|
31
|
+
export interface ActivityLightingFailure {
|
|
32
|
+
operation: "apply";
|
|
33
|
+
reason: string;
|
|
34
|
+
occurredAt: string;
|
|
35
|
+
}
|
|
36
|
+
export interface ActivityLightingApplied {
|
|
37
|
+
profile: string;
|
|
38
|
+
targets: string[] | null;
|
|
39
|
+
}
|
|
40
|
+
export interface ActivityLightingState {
|
|
41
|
+
desired: ActivityLightingDesired | null;
|
|
42
|
+
applied: ActivityLightingApplied | null;
|
|
43
|
+
generation: number;
|
|
44
|
+
phase: string;
|
|
45
|
+
observed: Record<string, ActivityLightingObservation>;
|
|
46
|
+
pending: ActivityLightingPending | null;
|
|
47
|
+
failure: ActivityLightingFailure | null;
|
|
48
|
+
blockedGeneration: number | null;
|
|
49
|
+
}
|
|
50
|
+
export type ActivityLightingEvent = {
|
|
51
|
+
kind: "intent";
|
|
52
|
+
requestId: string;
|
|
53
|
+
generation: number;
|
|
54
|
+
profile: string;
|
|
55
|
+
requestedAt?: string;
|
|
56
|
+
} | {
|
|
57
|
+
kind: "observed";
|
|
58
|
+
generation: number;
|
|
59
|
+
target: string;
|
|
60
|
+
on: boolean;
|
|
61
|
+
manual?: boolean;
|
|
62
|
+
available?: boolean;
|
|
63
|
+
values?: Record<string, unknown>;
|
|
64
|
+
observedAt: string;
|
|
65
|
+
} | {
|
|
66
|
+
kind: "command_result";
|
|
67
|
+
generation: number;
|
|
68
|
+
ok: boolean;
|
|
69
|
+
reason?: string;
|
|
70
|
+
} | {
|
|
71
|
+
kind: "tick";
|
|
72
|
+
generation: number;
|
|
73
|
+
} | {
|
|
74
|
+
kind: "startup" | "refresh";
|
|
75
|
+
};
|
|
76
|
+
export interface ActivityLightingMessage {
|
|
77
|
+
activityLighting?: ActivityLightingEvent;
|
|
78
|
+
}
|
|
79
|
+
export interface ActivityLightingCommand {
|
|
80
|
+
operation: "apply" | "observe" | "release";
|
|
81
|
+
requestId: string;
|
|
82
|
+
generation: number;
|
|
83
|
+
profile: string;
|
|
84
|
+
attempt: number;
|
|
85
|
+
lighting?: LightingEvent;
|
|
86
|
+
}
|
|
87
|
+
export interface ActivityLightingOutputEvent {
|
|
88
|
+
theatre: {
|
|
89
|
+
kind: "target_state";
|
|
90
|
+
target: "lighting";
|
|
91
|
+
requestId: string;
|
|
92
|
+
generation: number;
|
|
93
|
+
status: string;
|
|
94
|
+
observedAt: string;
|
|
95
|
+
details: {
|
|
96
|
+
profile: string;
|
|
97
|
+
matched: number;
|
|
98
|
+
preserved: number;
|
|
99
|
+
expected: number;
|
|
100
|
+
metrics: {
|
|
101
|
+
attempts: number;
|
|
102
|
+
retries: number;
|
|
103
|
+
fallbackUsed: boolean;
|
|
104
|
+
};
|
|
105
|
+
};
|
|
106
|
+
failure?: ActivityLightingFailure;
|
|
107
|
+
};
|
|
108
|
+
}
|
|
109
|
+
export declare function normaliseLightingProfiles(value: unknown): ActivityLightingProfiles;
|
|
110
|
+
export declare function normaliseActivityLightingConfig(config?: Partial<ActivityLightingConfig> & {
|
|
111
|
+
profiles?: unknown;
|
|
112
|
+
}): ActivityLightingConfig;
|
|
113
|
+
export declare function transitionActivityLighting(saved: Partial<ActivityLightingState> | undefined, msg: ActivityLightingMessage, suppliedConfig?: Partial<ActivityLightingConfig> & {
|
|
114
|
+
profiles?: unknown;
|
|
115
|
+
}, nowValue?: Date | string | number): {
|
|
116
|
+
state: ActivityLightingState;
|
|
117
|
+
commands: ActivityLightingCommand[];
|
|
118
|
+
events: ActivityLightingOutputEvent[];
|
|
119
|
+
traces: {
|
|
120
|
+
event: string;
|
|
121
|
+
outcome: string;
|
|
122
|
+
reason: string | null;
|
|
123
|
+
details: Record<string, unknown>;
|
|
124
|
+
}[];
|
|
125
|
+
status: {
|
|
126
|
+
phase: string;
|
|
127
|
+
text: string;
|
|
128
|
+
};
|
|
129
|
+
};
|
|
@@ -0,0 +1,372 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.normaliseLightingProfiles = normaliseLightingProfiles;
|
|
4
|
+
exports.normaliseActivityLightingConfig = normaliseActivityLightingConfig;
|
|
5
|
+
exports.transitionActivityLighting = transitionActivityLighting;
|
|
6
|
+
const observation_time_1 = require("./observation-time");
|
|
7
|
+
const PROFILE_NAME = /^[a-z][a-z0-9_-]*$/;
|
|
8
|
+
const TARGET_NAME = /^[a-z][a-z0-9_-]*$/;
|
|
9
|
+
function stable(value) {
|
|
10
|
+
if (Array.isArray(value))
|
|
11
|
+
return value.map(stable);
|
|
12
|
+
if (value && typeof value === "object") {
|
|
13
|
+
const record = value;
|
|
14
|
+
return Object.fromEntries(Object.keys(record).sort().map((key) => [key, stable(record[key])]));
|
|
15
|
+
}
|
|
16
|
+
return value;
|
|
17
|
+
}
|
|
18
|
+
function valuesMatch(expected, actual) {
|
|
19
|
+
return Object.entries(expected).every(([key, value]) => Object.hasOwn(actual, key) && JSON.stringify(stable(actual[key])) === JSON.stringify(stable(value)));
|
|
20
|
+
}
|
|
21
|
+
function normaliseLightingProfiles(value) {
|
|
22
|
+
if (!value || typeof value !== "object" || Array.isArray(value)) {
|
|
23
|
+
throw new Error("lighting profiles must be an object");
|
|
24
|
+
}
|
|
25
|
+
const entries = Object.entries(value);
|
|
26
|
+
if (entries.length === 0)
|
|
27
|
+
throw new Error("lighting profiles must not be empty");
|
|
28
|
+
const profiles = {};
|
|
29
|
+
for (const [name, descriptorValue] of entries) {
|
|
30
|
+
if (!PROFILE_NAME.test(name))
|
|
31
|
+
throw new Error(`invalid lighting profile name: ${name}`);
|
|
32
|
+
if (!descriptorValue || typeof descriptorValue !== "object" || Array.isArray(descriptorValue)) {
|
|
33
|
+
throw new Error(`lighting profile ${name} must be an object`);
|
|
34
|
+
}
|
|
35
|
+
const descriptor = descriptorValue;
|
|
36
|
+
if (!Array.isArray(descriptor.targets) ||
|
|
37
|
+
descriptor.targets.some((target) => typeof target !== "string" || !TARGET_NAME.test(target))) {
|
|
38
|
+
throw new Error(`lighting profile ${name} has invalid targets`);
|
|
39
|
+
}
|
|
40
|
+
if (new Set(descriptor.targets).size !== descriptor.targets.length) {
|
|
41
|
+
throw new Error(`lighting profile ${name} has duplicate targets`);
|
|
42
|
+
}
|
|
43
|
+
const values = descriptor.values ?? {};
|
|
44
|
+
if (!values || typeof values !== "object" || Array.isArray(values)) {
|
|
45
|
+
throw new Error(`lighting profile ${name} has invalid values`);
|
|
46
|
+
}
|
|
47
|
+
profiles[name] = {
|
|
48
|
+
targets: [...descriptor.targets],
|
|
49
|
+
values: { ...values }
|
|
50
|
+
};
|
|
51
|
+
}
|
|
52
|
+
return profiles;
|
|
53
|
+
}
|
|
54
|
+
function normaliseActivityLightingConfig(config = {}) {
|
|
55
|
+
const owner = typeof config.owner === "string" ? config.owner.trim() : "";
|
|
56
|
+
return {
|
|
57
|
+
profiles: normaliseLightingProfiles(config.profiles),
|
|
58
|
+
owner: owner || "activity",
|
|
59
|
+
confirmationTimeoutMs: Math.max(1000, Number(config.confirmationTimeoutMs) || 15000),
|
|
60
|
+
maxAttempts: Math.max(1, Math.floor(Number(config.maxAttempts) || 2))
|
|
61
|
+
};
|
|
62
|
+
}
|
|
63
|
+
function initialState(saved = {}, profiles = {}) {
|
|
64
|
+
const desired = saved.desired && Object.hasOwn(profiles, saved.desired.profile)
|
|
65
|
+
? { ...saved.desired }
|
|
66
|
+
: null;
|
|
67
|
+
const legacyTargets = saved.desired ? profiles[saved.desired.profile]?.targets : undefined;
|
|
68
|
+
const applied = saved.applied && typeof saved.applied === "object"
|
|
69
|
+
? { profile: saved.applied.profile, targets: Array.isArray(saved.applied.targets) ? [...saved.applied.targets] : null }
|
|
70
|
+
: saved.desired
|
|
71
|
+
? { profile: saved.desired.profile, targets: legacyTargets ? [...legacyTargets] : null }
|
|
72
|
+
: null;
|
|
73
|
+
return {
|
|
74
|
+
desired,
|
|
75
|
+
applied,
|
|
76
|
+
generation: Number.isInteger(saved.generation) && (saved.generation ?? -1) >= 0 ? saved.generation : 0,
|
|
77
|
+
phase: typeof saved.phase === "string" ? saved.phase : "idle",
|
|
78
|
+
observed: Object.fromEntries(Object.entries(saved.observed || {}).map(([target, observation]) => [target, { ...observation }])),
|
|
79
|
+
pending: saved.pending && typeof saved.pending === "object" ? { ...saved.pending } : null,
|
|
80
|
+
failure: saved.failure && typeof saved.failure === "object" ? { ...saved.failure } : null,
|
|
81
|
+
blockedGeneration: Number.isInteger(saved.blockedGeneration) ? saved.blockedGeneration : null
|
|
82
|
+
};
|
|
83
|
+
}
|
|
84
|
+
function profileFor(state, config) {
|
|
85
|
+
if (!state.desired)
|
|
86
|
+
throw new Error("activity lighting profile requires desired state");
|
|
87
|
+
const profile = config.profiles[state.desired.profile];
|
|
88
|
+
if (!profile)
|
|
89
|
+
throw new Error("activity lighting profile is missing");
|
|
90
|
+
return profile;
|
|
91
|
+
}
|
|
92
|
+
function commandFor(state, config, operation, attempt) {
|
|
93
|
+
if (!state.desired)
|
|
94
|
+
throw new Error("activity lighting command requires desired state");
|
|
95
|
+
const profile = profileFor(state, config);
|
|
96
|
+
const command = {
|
|
97
|
+
operation,
|
|
98
|
+
requestId: state.desired.requestId,
|
|
99
|
+
generation: state.generation,
|
|
100
|
+
profile: state.desired.profile,
|
|
101
|
+
attempt
|
|
102
|
+
};
|
|
103
|
+
if (operation === "apply") {
|
|
104
|
+
command.lighting = {
|
|
105
|
+
kind: "intent",
|
|
106
|
+
targets: [...profile.targets],
|
|
107
|
+
state: "ON",
|
|
108
|
+
values: { ...profile.values },
|
|
109
|
+
source: "activity",
|
|
110
|
+
owner: config.owner,
|
|
111
|
+
updateOnOnly: true,
|
|
112
|
+
preserveManual: true
|
|
113
|
+
};
|
|
114
|
+
}
|
|
115
|
+
return command;
|
|
116
|
+
}
|
|
117
|
+
function statusFor(state) {
|
|
118
|
+
return {
|
|
119
|
+
phase: state.phase,
|
|
120
|
+
text: `${state.desired?.profile || "none"}: ${state.phase}`
|
|
121
|
+
};
|
|
122
|
+
}
|
|
123
|
+
function transitionActivityLighting(saved, msg, suppliedConfig = {}, nowValue = new Date()) {
|
|
124
|
+
const config = normaliseActivityLightingConfig(suppliedConfig);
|
|
125
|
+
const now = nowValue instanceof Date ? nowValue : new Date(nowValue);
|
|
126
|
+
const nowMs = now.getTime();
|
|
127
|
+
if (!Number.isFinite(nowMs))
|
|
128
|
+
throw new Error("now must be a valid date");
|
|
129
|
+
const state = initialState(saved, config.profiles);
|
|
130
|
+
const event = msg?.activityLighting;
|
|
131
|
+
const commands = [];
|
|
132
|
+
const events = [];
|
|
133
|
+
const traces = [];
|
|
134
|
+
let counts = { matched: 0, preserved: 0, expected: 0 };
|
|
135
|
+
if (!event || typeof event.kind !== "string") {
|
|
136
|
+
throw new Error("msg.activityLighting.kind is required");
|
|
137
|
+
}
|
|
138
|
+
function trace(name, outcome, reason = null, details = {}) {
|
|
139
|
+
traces.push({ event: name, outcome, reason, details });
|
|
140
|
+
}
|
|
141
|
+
function fail(reason) {
|
|
142
|
+
state.pending = null;
|
|
143
|
+
state.phase = "failed";
|
|
144
|
+
state.blockedGeneration = state.generation;
|
|
145
|
+
state.failure = { operation: "apply", reason, occurredAt: now.toISOString() };
|
|
146
|
+
trace("lighting", "failed", reason, { profile: state.desired?.profile ?? null });
|
|
147
|
+
}
|
|
148
|
+
function issueApply(attempt) {
|
|
149
|
+
commands.push(commandFor(state, config, "apply", attempt));
|
|
150
|
+
state.pending = { attempt, deadlineAt: nowMs + config.confirmationTimeoutMs };
|
|
151
|
+
state.phase = "transitioning";
|
|
152
|
+
trace("command", "emitted", null, { operation: "apply", profile: state.desired?.profile, attempt });
|
|
153
|
+
}
|
|
154
|
+
function reconcile() {
|
|
155
|
+
if (!state.desired)
|
|
156
|
+
return;
|
|
157
|
+
const profile = profileFor(state, config);
|
|
158
|
+
counts = { matched: 0, preserved: 0, expected: profile.targets.length };
|
|
159
|
+
if (profile.targets.length === 0) {
|
|
160
|
+
state.pending = null;
|
|
161
|
+
state.failure = null;
|
|
162
|
+
state.phase = "ready";
|
|
163
|
+
trace("lighting", "ready", null, { profile: state.desired.profile, preserved: 0 });
|
|
164
|
+
return;
|
|
165
|
+
}
|
|
166
|
+
for (const target of profile.targets) {
|
|
167
|
+
const observation = state.observed[target];
|
|
168
|
+
if (!observation) {
|
|
169
|
+
state.phase = state.phase === "transitioning" ? "transitioning" : "confirming";
|
|
170
|
+
return;
|
|
171
|
+
}
|
|
172
|
+
if (!observation.available) {
|
|
173
|
+
fail("lighting-unavailable");
|
|
174
|
+
return;
|
|
175
|
+
}
|
|
176
|
+
if (observation.manual || !observation.on) {
|
|
177
|
+
counts.preserved += 1;
|
|
178
|
+
continue;
|
|
179
|
+
}
|
|
180
|
+
if (!valuesMatch(profile.values, observation.values)) {
|
|
181
|
+
state.phase = "confirming";
|
|
182
|
+
return;
|
|
183
|
+
}
|
|
184
|
+
counts.matched += 1;
|
|
185
|
+
}
|
|
186
|
+
state.pending = null;
|
|
187
|
+
state.failure = null;
|
|
188
|
+
state.blockedGeneration = null;
|
|
189
|
+
state.phase = "ready";
|
|
190
|
+
trace("lighting", "ready", null, {
|
|
191
|
+
profile: state.desired.profile,
|
|
192
|
+
matched: counts.matched,
|
|
193
|
+
preserved: counts.preserved
|
|
194
|
+
});
|
|
195
|
+
}
|
|
196
|
+
function acceptIntent(intent) {
|
|
197
|
+
if (typeof intent.requestId !== "string" || !intent.requestId) {
|
|
198
|
+
throw new Error("activity lighting requestId is required");
|
|
199
|
+
}
|
|
200
|
+
if (!Number.isInteger(intent.generation) || intent.generation < 1) {
|
|
201
|
+
throw new Error("activity lighting generation must be a positive integer");
|
|
202
|
+
}
|
|
203
|
+
if (!Object.hasOwn(config.profiles, intent.profile))
|
|
204
|
+
throw new Error("unknown activity lighting profile");
|
|
205
|
+
const requestedAt = intent.requestedAt || now.toISOString();
|
|
206
|
+
if (!Number.isFinite(Date.parse(requestedAt)))
|
|
207
|
+
throw new Error("activity lighting requestedAt must be an ISO timestamp");
|
|
208
|
+
if (intent.generation < state.generation) {
|
|
209
|
+
trace("intent", "ignored", "stale-generation");
|
|
210
|
+
return;
|
|
211
|
+
}
|
|
212
|
+
if (intent.generation === state.generation && state.desired) {
|
|
213
|
+
const duplicate = state.desired.requestId === intent.requestId && state.desired.profile === intent.profile;
|
|
214
|
+
trace("intent", duplicate ? "duplicate" : "rejected", duplicate ? null : "generation-conflict");
|
|
215
|
+
return;
|
|
216
|
+
}
|
|
217
|
+
// Hand back lights this node overrode that the new profile does not style.
|
|
218
|
+
// Unknown applied targets release every light held under this owner.
|
|
219
|
+
const nextTargets = config.profiles[intent.profile].targets;
|
|
220
|
+
const previous = state.applied;
|
|
221
|
+
const released = previous?.targets === null ? null
|
|
222
|
+
: (previous?.targets ?? []).filter((target) => !nextTargets.includes(target));
|
|
223
|
+
state.applied = { profile: intent.profile, targets: [...nextTargets] };
|
|
224
|
+
state.desired = {
|
|
225
|
+
requestId: intent.requestId,
|
|
226
|
+
generation: intent.generation,
|
|
227
|
+
profile: intent.profile,
|
|
228
|
+
requestedAt
|
|
229
|
+
};
|
|
230
|
+
if (previous && (released === null || released.length)) {
|
|
231
|
+
commands.push({
|
|
232
|
+
operation: "release",
|
|
233
|
+
requestId: intent.requestId,
|
|
234
|
+
generation: intent.generation,
|
|
235
|
+
profile: previous.profile,
|
|
236
|
+
attempt: 1,
|
|
237
|
+
lighting: released === null
|
|
238
|
+
? { kind: "release", owner: config.owner, source: "activity" }
|
|
239
|
+
: { kind: "release", owner: config.owner, targets: released, source: "activity" }
|
|
240
|
+
});
|
|
241
|
+
trace("command", "emitted", null, { operation: "release", profile: previous.profile, targets: released });
|
|
242
|
+
}
|
|
243
|
+
state.generation = intent.generation;
|
|
244
|
+
state.observed = {};
|
|
245
|
+
state.pending = null;
|
|
246
|
+
state.failure = null;
|
|
247
|
+
state.blockedGeneration = null;
|
|
248
|
+
if (profileFor(state, config).targets.length === 0)
|
|
249
|
+
reconcile();
|
|
250
|
+
else
|
|
251
|
+
issueApply(1);
|
|
252
|
+
trace("intent", "accepted", null, { profile: intent.profile, generation: intent.generation });
|
|
253
|
+
}
|
|
254
|
+
function acceptObservation(observation) {
|
|
255
|
+
if (!Number.isInteger(observation.generation) || observation.generation < 1) {
|
|
256
|
+
throw new Error("activity lighting observation generation must be a positive integer");
|
|
257
|
+
}
|
|
258
|
+
if (!TARGET_NAME.test(observation.target))
|
|
259
|
+
throw new Error("invalid activity lighting target");
|
|
260
|
+
const parsedAt = (0, observation_time_1.parseObservedAt)(observation.observedAt);
|
|
261
|
+
if (parsedAt === null || typeof observation.observedAt !== "string") {
|
|
262
|
+
throw new Error("activity lighting observedAt must be an ISO timestamp");
|
|
263
|
+
}
|
|
264
|
+
if (!state.desired || observation.generation !== state.generation) {
|
|
265
|
+
trace("observation", "ignored", "stale-generation");
|
|
266
|
+
return;
|
|
267
|
+
}
|
|
268
|
+
const profile = profileFor(state, config);
|
|
269
|
+
if (!profile.targets.includes(observation.target)) {
|
|
270
|
+
trace("observation", "ignored", "not-profile-target", { target: observation.target });
|
|
271
|
+
return;
|
|
272
|
+
}
|
|
273
|
+
const bounded = (0, observation_time_1.boundObservedAt)(observation.observedAt, parsedAt, nowMs);
|
|
274
|
+
if (bounded.skewed)
|
|
275
|
+
trace("observation", "adjusted", "clock-skew", { reported: observation.observedAt, usedAt: bounded.observedAt });
|
|
276
|
+
if (bounded.ms < Date.parse(state.desired.requestedAt)) {
|
|
277
|
+
trace("observation", "ignored", "stale-observation", { target: observation.target });
|
|
278
|
+
return;
|
|
279
|
+
}
|
|
280
|
+
if ((0, observation_time_1.isOutOfOrder)(bounded.ms, state.observed[observation.target]?.observedAt, nowMs)) {
|
|
281
|
+
trace("observation", "ignored", "out-of-order", { target: observation.target });
|
|
282
|
+
return;
|
|
283
|
+
}
|
|
284
|
+
state.observed[observation.target] = {
|
|
285
|
+
target: observation.target,
|
|
286
|
+
on: observation.on === true,
|
|
287
|
+
manual: observation.manual === true,
|
|
288
|
+
available: observation.available !== false,
|
|
289
|
+
values: observation.values && typeof observation.values === "object" ? { ...observation.values } : {},
|
|
290
|
+
observedAt: bounded.observedAt
|
|
291
|
+
};
|
|
292
|
+
trace("observation", "accepted", null, { target: observation.target });
|
|
293
|
+
reconcile();
|
|
294
|
+
}
|
|
295
|
+
if (event.kind === "intent") {
|
|
296
|
+
acceptIntent(event);
|
|
297
|
+
}
|
|
298
|
+
else if (event.kind === "observed") {
|
|
299
|
+
acceptObservation(event);
|
|
300
|
+
}
|
|
301
|
+
else if (event.kind === "command_result") {
|
|
302
|
+
if (!state.desired || event.generation !== state.generation || !state.pending) {
|
|
303
|
+
trace("command-result", "ignored", "stale-generation");
|
|
304
|
+
}
|
|
305
|
+
else if (event.ok === true) {
|
|
306
|
+
state.phase = "confirming";
|
|
307
|
+
trace("command-result", "accepted");
|
|
308
|
+
}
|
|
309
|
+
else if (state.pending.attempt < config.maxAttempts) {
|
|
310
|
+
issueApply(state.pending.attempt + 1);
|
|
311
|
+
}
|
|
312
|
+
else {
|
|
313
|
+
fail(typeof event.reason === "string" && event.reason ? event.reason : "command-failed");
|
|
314
|
+
}
|
|
315
|
+
}
|
|
316
|
+
else if (event.kind === "tick") {
|
|
317
|
+
if (!state.desired || event.generation !== state.generation || !state.pending) {
|
|
318
|
+
trace("tick", "ignored", "stale-generation");
|
|
319
|
+
}
|
|
320
|
+
else if (nowMs >= state.pending.deadlineAt) {
|
|
321
|
+
if (state.pending.attempt < config.maxAttempts)
|
|
322
|
+
issueApply(state.pending.attempt + 1);
|
|
323
|
+
else
|
|
324
|
+
fail("confirmation-timeout");
|
|
325
|
+
}
|
|
326
|
+
}
|
|
327
|
+
else if (event.kind === "startup" || event.kind === "refresh") {
|
|
328
|
+
state.observed = {};
|
|
329
|
+
state.pending = null;
|
|
330
|
+
if (state.desired) {
|
|
331
|
+
if (profileFor(state, config).targets.length === 0)
|
|
332
|
+
reconcile();
|
|
333
|
+
else {
|
|
334
|
+
commands.push(commandFor(state, config, "observe", 0));
|
|
335
|
+
state.phase = "confirming";
|
|
336
|
+
trace(event.kind, "observing", null, { profile: state.desired.profile });
|
|
337
|
+
}
|
|
338
|
+
}
|
|
339
|
+
else {
|
|
340
|
+
trace(event.kind, "idle");
|
|
341
|
+
}
|
|
342
|
+
}
|
|
343
|
+
else {
|
|
344
|
+
throw new Error("unknown activity lighting event kind");
|
|
345
|
+
}
|
|
346
|
+
if (state.desired) {
|
|
347
|
+
const theatre = {
|
|
348
|
+
kind: "target_state",
|
|
349
|
+
target: "lighting",
|
|
350
|
+
requestId: state.desired.requestId,
|
|
351
|
+
generation: state.generation,
|
|
352
|
+
status: state.phase === "failed" ? "failed" : state.phase === "ready" ? "ready" : state.phase,
|
|
353
|
+
observedAt: now.toISOString(),
|
|
354
|
+
details: {
|
|
355
|
+
profile: state.desired.profile,
|
|
356
|
+
matched: counts.matched,
|
|
357
|
+
preserved: counts.preserved,
|
|
358
|
+
expected: profileFor(state, config).targets.length,
|
|
359
|
+
metrics: {
|
|
360
|
+
attempts: state.pending?.attempt ?? 0,
|
|
361
|
+
retries: Math.max(0, (state.pending?.attempt ?? 1) - 1),
|
|
362
|
+
fallbackUsed: false
|
|
363
|
+
}
|
|
364
|
+
}
|
|
365
|
+
};
|
|
366
|
+
if (state.failure)
|
|
367
|
+
theatre.failure = { ...state.failure };
|
|
368
|
+
events.push({ theatre });
|
|
369
|
+
}
|
|
370
|
+
return { state, commands, events, traces, status: statusFor(state) };
|
|
371
|
+
}
|
|
372
|
+
//# sourceMappingURL=activity-lighting-controller.js.map
|