@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,108 @@
|
|
|
1
|
+
export type BacklightingPower = "on" | "off";
|
|
2
|
+
export interface BacklightingConfig {
|
|
3
|
+
pollMs: number;
|
|
4
|
+
confirmationTimeoutMs: number;
|
|
5
|
+
maxAttempts: number;
|
|
6
|
+
}
|
|
7
|
+
export interface BacklightingDesired {
|
|
8
|
+
requestId: string;
|
|
9
|
+
generation: number;
|
|
10
|
+
power: BacklightingPower;
|
|
11
|
+
requestedAt: string;
|
|
12
|
+
}
|
|
13
|
+
export interface BacklightingObservation {
|
|
14
|
+
power: BacklightingPower;
|
|
15
|
+
observedAt: string;
|
|
16
|
+
}
|
|
17
|
+
export interface BacklightingPending {
|
|
18
|
+
operation: "set_power" | "observe";
|
|
19
|
+
attempt: number;
|
|
20
|
+
deadlineAt: number;
|
|
21
|
+
nextAt: number;
|
|
22
|
+
}
|
|
23
|
+
export interface BacklightingFailure {
|
|
24
|
+
operation: "set_power" | "observe";
|
|
25
|
+
reason: string;
|
|
26
|
+
occurredAt: string;
|
|
27
|
+
}
|
|
28
|
+
export interface BacklightingState {
|
|
29
|
+
desired: BacklightingDesired | null;
|
|
30
|
+
generation: number;
|
|
31
|
+
phase: string;
|
|
32
|
+
observed: BacklightingObservation | null;
|
|
33
|
+
pending: BacklightingPending | null;
|
|
34
|
+
failure: BacklightingFailure | null;
|
|
35
|
+
blockedGeneration: number | null;
|
|
36
|
+
classification: "manual_override" | null;
|
|
37
|
+
lastAttempt: number;
|
|
38
|
+
}
|
|
39
|
+
export type BacklightingEvent = {
|
|
40
|
+
kind: "intent";
|
|
41
|
+
requestId: string;
|
|
42
|
+
generation: number;
|
|
43
|
+
power: BacklightingPower;
|
|
44
|
+
requestedAt?: string;
|
|
45
|
+
} | {
|
|
46
|
+
kind: "observed";
|
|
47
|
+
generation: number;
|
|
48
|
+
power: BacklightingPower;
|
|
49
|
+
observedAt: string;
|
|
50
|
+
} | {
|
|
51
|
+
kind: "command_result";
|
|
52
|
+
generation: number;
|
|
53
|
+
ok: boolean;
|
|
54
|
+
reason?: string;
|
|
55
|
+
} | {
|
|
56
|
+
kind: "tick";
|
|
57
|
+
generation: number;
|
|
58
|
+
} | {
|
|
59
|
+
kind: "startup" | "refresh";
|
|
60
|
+
};
|
|
61
|
+
export interface BacklightingMessage {
|
|
62
|
+
backlighting?: BacklightingEvent;
|
|
63
|
+
}
|
|
64
|
+
export interface BacklightingCommand {
|
|
65
|
+
operation: "set_power" | "observe";
|
|
66
|
+
requestId: string;
|
|
67
|
+
generation: number;
|
|
68
|
+
power: BacklightingPower;
|
|
69
|
+
attempt: number;
|
|
70
|
+
}
|
|
71
|
+
export interface BacklightingOutputEvent {
|
|
72
|
+
theatre: {
|
|
73
|
+
kind: "target_state";
|
|
74
|
+
target: "backlighting";
|
|
75
|
+
requestId: string;
|
|
76
|
+
generation: number;
|
|
77
|
+
status: string;
|
|
78
|
+
observedAt: string;
|
|
79
|
+
details: {
|
|
80
|
+
power: BacklightingPower;
|
|
81
|
+
observedPower: BacklightingPower | null;
|
|
82
|
+
recovery: Record<string, unknown>;
|
|
83
|
+
metrics: {
|
|
84
|
+
attempts: number;
|
|
85
|
+
retries: number;
|
|
86
|
+
fallbackUsed: boolean;
|
|
87
|
+
};
|
|
88
|
+
};
|
|
89
|
+
classification?: "manual_override";
|
|
90
|
+
failure?: BacklightingFailure;
|
|
91
|
+
};
|
|
92
|
+
}
|
|
93
|
+
export declare function normaliseBacklightingConfig(config?: Partial<BacklightingConfig>): BacklightingConfig;
|
|
94
|
+
export declare function transitionBacklighting(saved: Partial<BacklightingState> | undefined, msg: BacklightingMessage, suppliedConfig?: Partial<BacklightingConfig>, nowValue?: Date | string | number): {
|
|
95
|
+
state: BacklightingState;
|
|
96
|
+
commands: BacklightingCommand[];
|
|
97
|
+
events: BacklightingOutputEvent[];
|
|
98
|
+
traces: {
|
|
99
|
+
event: string;
|
|
100
|
+
outcome: string;
|
|
101
|
+
reason: string | null;
|
|
102
|
+
details: Record<string, unknown>;
|
|
103
|
+
}[];
|
|
104
|
+
status: {
|
|
105
|
+
phase: string;
|
|
106
|
+
text: string;
|
|
107
|
+
};
|
|
108
|
+
};
|
|
@@ -0,0 +1,269 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.normaliseBacklightingConfig = normaliseBacklightingConfig;
|
|
4
|
+
exports.transitionBacklighting = transitionBacklighting;
|
|
5
|
+
const observation_time_1 = require("./observation-time");
|
|
6
|
+
const POWERS = { on: true, off: true };
|
|
7
|
+
function normaliseBacklightingConfig(config = {}) {
|
|
8
|
+
return {
|
|
9
|
+
pollMs: Math.max(250, Number(config.pollMs) || 2000),
|
|
10
|
+
confirmationTimeoutMs: Math.max(1000, Number(config.confirmationTimeoutMs) || 15000),
|
|
11
|
+
maxAttempts: Math.max(1, Math.floor(Number(config.maxAttempts) || 2))
|
|
12
|
+
};
|
|
13
|
+
}
|
|
14
|
+
function initialState(saved = {}) {
|
|
15
|
+
return {
|
|
16
|
+
desired: saved.desired && typeof saved.desired === "object" ? { ...saved.desired } : null,
|
|
17
|
+
generation: Number.isInteger(saved.generation) && (saved.generation ?? -1) >= 0 ? saved.generation : 0,
|
|
18
|
+
phase: typeof saved.phase === "string" ? saved.phase : "idle",
|
|
19
|
+
observed: saved.observed && typeof saved.observed === "object" ? { ...saved.observed } : null,
|
|
20
|
+
pending: saved.pending && typeof saved.pending === "object" ? { ...saved.pending } : null,
|
|
21
|
+
failure: saved.failure && typeof saved.failure === "object" ? { ...saved.failure } : null,
|
|
22
|
+
blockedGeneration: Number.isInteger(saved.blockedGeneration) ? saved.blockedGeneration : null,
|
|
23
|
+
classification: saved.classification === "manual_override" ? "manual_override" : null,
|
|
24
|
+
lastAttempt: Number.isInteger(saved.lastAttempt) && (saved.lastAttempt ?? -1) >= 0
|
|
25
|
+
? saved.lastAttempt
|
|
26
|
+
: 0
|
|
27
|
+
};
|
|
28
|
+
}
|
|
29
|
+
function commandFor(state, operation, attempt) {
|
|
30
|
+
if (!state.desired)
|
|
31
|
+
throw new Error("backlighting command requires desired state");
|
|
32
|
+
return {
|
|
33
|
+
operation,
|
|
34
|
+
requestId: state.desired.requestId,
|
|
35
|
+
generation: state.generation,
|
|
36
|
+
power: state.desired.power,
|
|
37
|
+
attempt
|
|
38
|
+
};
|
|
39
|
+
}
|
|
40
|
+
function statusFor(state) {
|
|
41
|
+
return {
|
|
42
|
+
phase: state.phase,
|
|
43
|
+
text: `${state.desired?.power || "none"}: ${state.phase}`
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
function transitionBacklighting(saved, msg, suppliedConfig = {}, nowValue = new Date()) {
|
|
47
|
+
const config = normaliseBacklightingConfig(suppliedConfig);
|
|
48
|
+
const now = nowValue instanceof Date ? nowValue : new Date(nowValue);
|
|
49
|
+
const nowMs = now.getTime();
|
|
50
|
+
if (!Number.isFinite(nowMs))
|
|
51
|
+
throw new Error("now must be a valid date");
|
|
52
|
+
const state = initialState(saved);
|
|
53
|
+
const event = msg?.backlighting;
|
|
54
|
+
const commands = [];
|
|
55
|
+
const events = [];
|
|
56
|
+
const traces = [];
|
|
57
|
+
if (!event || typeof event.kind !== "string")
|
|
58
|
+
throw new Error("msg.backlighting.kind is required");
|
|
59
|
+
function trace(name, outcome, reason = null, details = {}) {
|
|
60
|
+
traces.push({ event: name, outcome, reason, details });
|
|
61
|
+
}
|
|
62
|
+
function fail(operation, reason) {
|
|
63
|
+
state.pending = null;
|
|
64
|
+
state.phase = "failed";
|
|
65
|
+
state.blockedGeneration = state.generation;
|
|
66
|
+
state.failure = { operation, reason, occurredAt: now.toISOString() };
|
|
67
|
+
trace("backlighting", "failed", reason, { operation, power: state.desired?.power ?? null });
|
|
68
|
+
}
|
|
69
|
+
function issue(operation, attempt) {
|
|
70
|
+
commands.push(commandFor(state, operation, attempt));
|
|
71
|
+
state.pending = {
|
|
72
|
+
operation,
|
|
73
|
+
attempt,
|
|
74
|
+
deadlineAt: nowMs + config.confirmationTimeoutMs,
|
|
75
|
+
nextAt: nowMs + config.pollMs
|
|
76
|
+
};
|
|
77
|
+
state.phase = operation === "set_power" ? "transitioning" : "confirming";
|
|
78
|
+
trace("command", "emitted", null, { operation, power: state.desired?.power, attempt });
|
|
79
|
+
state.lastAttempt = Math.max(state.lastAttempt, attempt);
|
|
80
|
+
}
|
|
81
|
+
function acceptIntent(intent) {
|
|
82
|
+
if (typeof intent.requestId !== "string" || !intent.requestId) {
|
|
83
|
+
throw new Error("backlighting requestId is required");
|
|
84
|
+
}
|
|
85
|
+
if (!Number.isInteger(intent.generation) || intent.generation < 1) {
|
|
86
|
+
throw new Error("backlighting generation must be a positive integer");
|
|
87
|
+
}
|
|
88
|
+
if (!Object.hasOwn(POWERS, intent.power))
|
|
89
|
+
throw new Error("unknown backlighting power");
|
|
90
|
+
const requestedAt = intent.requestedAt || now.toISOString();
|
|
91
|
+
if (!Number.isFinite(Date.parse(requestedAt)))
|
|
92
|
+
throw new Error("backlighting requestedAt must be an ISO timestamp");
|
|
93
|
+
if (intent.generation < state.generation) {
|
|
94
|
+
trace("intent", "ignored", "stale-generation");
|
|
95
|
+
return;
|
|
96
|
+
}
|
|
97
|
+
if (intent.generation === state.generation && state.desired) {
|
|
98
|
+
const duplicate = state.desired.requestId === intent.requestId && state.desired.power === intent.power;
|
|
99
|
+
trace("intent", duplicate ? "duplicate" : "rejected", duplicate ? null : "generation-conflict");
|
|
100
|
+
return;
|
|
101
|
+
}
|
|
102
|
+
state.desired = {
|
|
103
|
+
requestId: intent.requestId,
|
|
104
|
+
generation: intent.generation,
|
|
105
|
+
power: intent.power,
|
|
106
|
+
requestedAt
|
|
107
|
+
};
|
|
108
|
+
state.generation = intent.generation;
|
|
109
|
+
state.observed = null;
|
|
110
|
+
state.pending = null;
|
|
111
|
+
state.failure = null;
|
|
112
|
+
state.blockedGeneration = null;
|
|
113
|
+
state.classification = null;
|
|
114
|
+
state.lastAttempt = 0;
|
|
115
|
+
issue("set_power", 1);
|
|
116
|
+
trace("intent", "accepted", null, { power: intent.power, generation: intent.generation });
|
|
117
|
+
}
|
|
118
|
+
function acceptObservation(observation) {
|
|
119
|
+
if (!Number.isInteger(observation.generation) || observation.generation < 1) {
|
|
120
|
+
throw new Error("backlighting observation generation must be a positive integer");
|
|
121
|
+
}
|
|
122
|
+
if (!Object.hasOwn(POWERS, observation.power))
|
|
123
|
+
throw new Error("unknown observed backlighting power");
|
|
124
|
+
const parsedAt = (0, observation_time_1.parseObservedAt)(observation.observedAt);
|
|
125
|
+
if (parsedAt === null || typeof observation.observedAt !== "string") {
|
|
126
|
+
throw new Error("backlighting observedAt must be an ISO timestamp");
|
|
127
|
+
}
|
|
128
|
+
if (!state.desired || observation.generation !== state.generation) {
|
|
129
|
+
trace("observation", "ignored", "stale-generation");
|
|
130
|
+
return;
|
|
131
|
+
}
|
|
132
|
+
const bounded = (0, observation_time_1.boundObservedAt)(observation.observedAt, parsedAt, nowMs);
|
|
133
|
+
if (bounded.skewed)
|
|
134
|
+
trace("observation", "adjusted", "clock-skew", { reported: observation.observedAt, usedAt: bounded.observedAt });
|
|
135
|
+
if (bounded.ms < Date.parse(state.desired.requestedAt)) {
|
|
136
|
+
trace("observation", "ignored", "stale-observation");
|
|
137
|
+
return;
|
|
138
|
+
}
|
|
139
|
+
if ((0, observation_time_1.isOutOfOrder)(bounded.ms, state.observed?.observedAt, nowMs)) {
|
|
140
|
+
trace("observation", "ignored", "out-of-order");
|
|
141
|
+
return;
|
|
142
|
+
}
|
|
143
|
+
const wasReady = state.phase === "ready" && !state.pending;
|
|
144
|
+
state.observed = { power: observation.power, observedAt: bounded.observedAt };
|
|
145
|
+
if (observation.power === state.desired.power) {
|
|
146
|
+
state.pending = null;
|
|
147
|
+
state.failure = null;
|
|
148
|
+
state.blockedGeneration = null;
|
|
149
|
+
state.classification = null;
|
|
150
|
+
state.phase = "ready";
|
|
151
|
+
trace("observation", "ready", null, { power: observation.power });
|
|
152
|
+
}
|
|
153
|
+
else if (wasReady) {
|
|
154
|
+
state.pending = null;
|
|
155
|
+
state.classification = "manual_override";
|
|
156
|
+
state.phase = "manual_override";
|
|
157
|
+
trace("observation", "manual_override", null, {
|
|
158
|
+
expected: state.desired.power,
|
|
159
|
+
observed: observation.power
|
|
160
|
+
});
|
|
161
|
+
}
|
|
162
|
+
else if (state.classification === "manual_override") {
|
|
163
|
+
state.pending = null;
|
|
164
|
+
state.phase = "manual_override";
|
|
165
|
+
}
|
|
166
|
+
else {
|
|
167
|
+
state.phase = "confirming";
|
|
168
|
+
trace("observation", "accepted", "power-mismatch", {
|
|
169
|
+
expected: state.desired.power,
|
|
170
|
+
observed: observation.power
|
|
171
|
+
});
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
if (event.kind === "intent") {
|
|
175
|
+
acceptIntent(event);
|
|
176
|
+
}
|
|
177
|
+
else if (event.kind === "observed") {
|
|
178
|
+
acceptObservation(event);
|
|
179
|
+
}
|
|
180
|
+
else if (event.kind === "command_result") {
|
|
181
|
+
if (!state.desired || event.generation !== state.generation || !state.pending) {
|
|
182
|
+
trace("command-result", "ignored", "stale-generation");
|
|
183
|
+
}
|
|
184
|
+
else if (event.ok === true) {
|
|
185
|
+
state.phase = "confirming";
|
|
186
|
+
state.pending.nextAt = nowMs;
|
|
187
|
+
trace("command-result", "accepted");
|
|
188
|
+
}
|
|
189
|
+
else if (state.pending.operation === "set_power" && state.pending.attempt < config.maxAttempts) {
|
|
190
|
+
issue("set_power", state.pending.attempt + 1);
|
|
191
|
+
}
|
|
192
|
+
else {
|
|
193
|
+
fail(state.pending.operation, typeof event.reason === "string" && event.reason ? event.reason : "command-failed");
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
else if (event.kind === "tick") {
|
|
197
|
+
if (!state.desired || event.generation !== state.generation || !state.pending) {
|
|
198
|
+
trace("tick", "ignored", "stale-generation");
|
|
199
|
+
}
|
|
200
|
+
else if (nowMs >= state.pending.deadlineAt) {
|
|
201
|
+
if (state.pending.operation === "set_power" && state.pending.attempt < config.maxAttempts) {
|
|
202
|
+
issue("set_power", state.pending.attempt + 1);
|
|
203
|
+
}
|
|
204
|
+
else {
|
|
205
|
+
fail(state.pending.operation, "confirmation-timeout");
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
else if (nowMs >= state.pending.nextAt) {
|
|
209
|
+
commands.push(commandFor(state, "observe", state.pending.attempt));
|
|
210
|
+
state.pending.nextAt = nowMs + config.pollMs;
|
|
211
|
+
state.phase = "confirming";
|
|
212
|
+
trace("command", "emitted", null, { operation: "observe", power: state.desired.power });
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
else if (event.kind === "startup" || event.kind === "refresh") {
|
|
216
|
+
state.observed = null;
|
|
217
|
+
state.pending = null;
|
|
218
|
+
if (state.desired) {
|
|
219
|
+
if (state.classification === "manual_override") {
|
|
220
|
+
commands.push(commandFor(state, "observe", 0));
|
|
221
|
+
state.pending = null;
|
|
222
|
+
state.phase = "manual_override";
|
|
223
|
+
}
|
|
224
|
+
else {
|
|
225
|
+
issue("observe", 0);
|
|
226
|
+
}
|
|
227
|
+
trace(event.kind, "observing", null, { power: state.desired.power });
|
|
228
|
+
}
|
|
229
|
+
else {
|
|
230
|
+
trace(event.kind, "idle");
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
else {
|
|
234
|
+
throw new Error("unknown backlighting event kind");
|
|
235
|
+
}
|
|
236
|
+
if (state.desired) {
|
|
237
|
+
const theatre = {
|
|
238
|
+
kind: "target_state",
|
|
239
|
+
target: "backlighting",
|
|
240
|
+
requestId: state.desired.requestId,
|
|
241
|
+
generation: state.generation,
|
|
242
|
+
status: state.phase === "failed"
|
|
243
|
+
? "failed"
|
|
244
|
+
: state.phase === "ready"
|
|
245
|
+
? "ready"
|
|
246
|
+
: state.phase === "manual_override"
|
|
247
|
+
? "transitioning"
|
|
248
|
+
: state.phase,
|
|
249
|
+
observedAt: now.toISOString(),
|
|
250
|
+
details: {
|
|
251
|
+
power: state.desired.power,
|
|
252
|
+
observedPower: state.observed?.power ?? null,
|
|
253
|
+
recovery: state.observed ? { power: state.observed.power } : {},
|
|
254
|
+
metrics: {
|
|
255
|
+
attempts: state.lastAttempt,
|
|
256
|
+
retries: Math.max(0, state.lastAttempt - 1),
|
|
257
|
+
fallbackUsed: false
|
|
258
|
+
}
|
|
259
|
+
}
|
|
260
|
+
};
|
|
261
|
+
if (state.classification)
|
|
262
|
+
theatre.classification = state.classification;
|
|
263
|
+
if (state.failure)
|
|
264
|
+
theatre.failure = { ...state.failure };
|
|
265
|
+
events.push({ theatre });
|
|
266
|
+
}
|
|
267
|
+
return { state, commands, events, traces, status: statusFor(state) };
|
|
268
|
+
}
|
|
269
|
+
//# sourceMappingURL=backlighting-controller.js.map
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
export declare const TRACE_SCHEMA = 1;
|
|
2
|
+
export declare const DEFAULT_TRACE_LIMIT = 200;
|
|
3
|
+
export interface TraceContext {
|
|
4
|
+
get(key: string): unknown;
|
|
5
|
+
set(key: string, value: unknown): void;
|
|
6
|
+
}
|
|
7
|
+
export interface TraceMetadata {
|
|
8
|
+
area: string;
|
|
9
|
+
component: string;
|
|
10
|
+
limit?: number;
|
|
11
|
+
}
|
|
12
|
+
export interface TraceInput {
|
|
13
|
+
at?: string;
|
|
14
|
+
event: string;
|
|
15
|
+
outcome: string;
|
|
16
|
+
reason?: string | null;
|
|
17
|
+
details?: Record<string, unknown>;
|
|
18
|
+
}
|
|
19
|
+
export interface TraceRecord {
|
|
20
|
+
schema: number;
|
|
21
|
+
sequence: number;
|
|
22
|
+
at: string;
|
|
23
|
+
area: string;
|
|
24
|
+
component: string;
|
|
25
|
+
event: string;
|
|
26
|
+
outcome: string;
|
|
27
|
+
reason: string | null;
|
|
28
|
+
details: Record<string, unknown>;
|
|
29
|
+
}
|
|
30
|
+
export declare function appendTrace(context: TraceContext, metadata: TraceMetadata, entries: TraceInput[], now?: Date | string | number): TraceRecord[];
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.DEFAULT_TRACE_LIMIT = exports.TRACE_SCHEMA = void 0;
|
|
4
|
+
exports.appendTrace = appendTrace;
|
|
5
|
+
exports.TRACE_SCHEMA = 1;
|
|
6
|
+
exports.DEFAULT_TRACE_LIMIT = 200;
|
|
7
|
+
function appendTrace(context, metadata, entries, now = new Date()) {
|
|
8
|
+
if (!Array.isArray(entries) || entries.length === 0)
|
|
9
|
+
return [];
|
|
10
|
+
const limit = Number.isInteger(metadata.limit) && (metadata.limit ?? 0) > 0
|
|
11
|
+
? metadata.limit
|
|
12
|
+
: exports.DEFAULT_TRACE_LIMIT;
|
|
13
|
+
const savedHistory = context.get("trace");
|
|
14
|
+
const history = Array.isArray(savedHistory) ? savedHistory : [];
|
|
15
|
+
let sequence = Number(context.get("traceSequence")) || 0;
|
|
16
|
+
const at = now instanceof Date ? now.toISOString() : new Date(now).toISOString();
|
|
17
|
+
const appended = entries.map((entry) => {
|
|
18
|
+
sequence += 1;
|
|
19
|
+
return {
|
|
20
|
+
schema: exports.TRACE_SCHEMA,
|
|
21
|
+
sequence,
|
|
22
|
+
at: entry.at || at,
|
|
23
|
+
area: metadata.area,
|
|
24
|
+
component: metadata.component,
|
|
25
|
+
event: entry.event,
|
|
26
|
+
outcome: entry.outcome,
|
|
27
|
+
reason: entry.reason ?? null,
|
|
28
|
+
details: entry.details || {}
|
|
29
|
+
};
|
|
30
|
+
});
|
|
31
|
+
history.push(...appended);
|
|
32
|
+
context.set("trace", history.slice(-limit));
|
|
33
|
+
context.set("traceSequence", sequence);
|
|
34
|
+
return appended;
|
|
35
|
+
}
|
|
36
|
+
//# sourceMappingURL=bounded-trace.js.map
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.alreadyStyled = alreadyStyled;
|
|
4
|
+
// Whether a light already shows a style, by its own latest report. A bulb
|
|
5
|
+
// that missed a command reports something else, so it is not "styled".
|
|
6
|
+
function alreadyStyled(style, observed) {
|
|
7
|
+
return observed?.on === true &&
|
|
8
|
+
Number(observed.brightness) === style.brightness &&
|
|
9
|
+
Number(observed.color_temp) === style.color_temp;
|
|
10
|
+
}
|
|
11
|
+
//# sourceMappingURL=circadian-refresh.js.map
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
export declare const WAKE_RISE_MINUTES = 60;
|
|
2
|
+
export declare const WIND_DOWN_MINUTES = 180;
|
|
3
|
+
export declare const DEFAULT_WAKE_TIME = "07:00";
|
|
4
|
+
export declare const DEFAULT_BED_TIME = "23:00";
|
|
5
|
+
export interface DaySchedule {
|
|
6
|
+
wakeTime: string;
|
|
7
|
+
bedTime: string;
|
|
8
|
+
}
|
|
9
|
+
export declare function clockMinutes(value: unknown): number | null;
|
|
10
|
+
export declare function resolveSchedule(wakeTime?: unknown, bedTime?: unknown): DaySchedule;
|
|
11
|
+
export declare function scheduleLevel(now: Date, wakeTime: string, bedTime: string): number;
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.DEFAULT_BED_TIME = exports.DEFAULT_WAKE_TIME = exports.WIND_DOWN_MINUTES = exports.WAKE_RISE_MINUTES = void 0;
|
|
4
|
+
exports.clockMinutes = clockMinutes;
|
|
5
|
+
exports.resolveSchedule = resolveSchedule;
|
|
6
|
+
exports.scheduleLevel = scheduleLevel;
|
|
7
|
+
// A daily wake and bed schedule, and how far into the day a given moment is.
|
|
8
|
+
// Pure: it knows nothing about lights, and takes the time it is asked about.
|
|
9
|
+
// After waking, the day level rises over this long; before bed it winds down
|
|
10
|
+
// over the three hours that evidence on evening light exposure points to
|
|
11
|
+
// (Brown et al. 2022, PLOS Biology).
|
|
12
|
+
exports.WAKE_RISE_MINUTES = 60;
|
|
13
|
+
exports.WIND_DOWN_MINUTES = 180;
|
|
14
|
+
// A conventional day, used until a room is given its own times.
|
|
15
|
+
exports.DEFAULT_WAKE_TIME = "07:00";
|
|
16
|
+
exports.DEFAULT_BED_TIME = "23:00";
|
|
17
|
+
const MINUTES_PER_DAY = 24 * 60;
|
|
18
|
+
const CLOCK_TIME = /^([01]\d|2[0-3]):([0-5]\d)$/;
|
|
19
|
+
// Minutes after midnight for an "HH:MM" clock time, or null if it is not one.
|
|
20
|
+
function clockMinutes(value) {
|
|
21
|
+
const match = typeof value === "string" ? CLOCK_TIME.exec(value.trim()) : null;
|
|
22
|
+
return match ? Number(match[1]) * 60 + Number(match[2]) : null;
|
|
23
|
+
}
|
|
24
|
+
// A usable schedule from configured times: a blank time takes its default;
|
|
25
|
+
// each must be "HH:MM" and they must differ.
|
|
26
|
+
function resolveSchedule(wakeTime, bedTime) {
|
|
27
|
+
const wake = (typeof wakeTime === "string" ? wakeTime.trim() : "") || exports.DEFAULT_WAKE_TIME;
|
|
28
|
+
const bed = (typeof bedTime === "string" ? bedTime.trim() : "") || exports.DEFAULT_BED_TIME;
|
|
29
|
+
const wakeMinutes = clockMinutes(wake);
|
|
30
|
+
const bedMinutes = clockMinutes(bed);
|
|
31
|
+
if (wakeMinutes === null || bedMinutes === null)
|
|
32
|
+
throw new Error("wake and bed times must both be HH:MM (24-hour)");
|
|
33
|
+
if (wakeMinutes === bedMinutes)
|
|
34
|
+
throw new Error("wake and bed times must differ");
|
|
35
|
+
return { wakeTime: wake, bedTime: bed };
|
|
36
|
+
}
|
|
37
|
+
// Ease in and out, so neither end of a ramp is a visible step.
|
|
38
|
+
function ease(fraction) {
|
|
39
|
+
return fraction * fraction * (3 - 2 * fraction);
|
|
40
|
+
}
|
|
41
|
+
// The day level, from 0 (asleep) to 1 (full day), for the given local time.
|
|
42
|
+
// Times wrap at midnight, so a bed time after midnight or a wake time later
|
|
43
|
+
// than the bed time both work. A waking day shorter than the rise and the
|
|
44
|
+
// wind-down together shortens both in proportion.
|
|
45
|
+
function scheduleLevel(now, wakeTime, bedTime) {
|
|
46
|
+
const wake = clockMinutes(wakeTime);
|
|
47
|
+
const bed = clockMinutes(bedTime);
|
|
48
|
+
if (wake === null || bed === null || wake === bed)
|
|
49
|
+
throw new Error("wake and bed times must be different HH:MM times");
|
|
50
|
+
const awakeMinutes = (bed - wake + MINUTES_PER_DAY) % MINUTES_PER_DAY;
|
|
51
|
+
const minuteOfDay = now.getHours() * 60 + now.getMinutes() + now.getSeconds() / 60;
|
|
52
|
+
const sinceWake = (minuteOfDay - wake + MINUTES_PER_DAY) % MINUTES_PER_DAY;
|
|
53
|
+
if (sinceWake >= awakeMinutes)
|
|
54
|
+
return 0;
|
|
55
|
+
const shrink = Math.min(1, awakeMinutes / (exports.WAKE_RISE_MINUTES + exports.WIND_DOWN_MINUTES));
|
|
56
|
+
const rise = exports.WAKE_RISE_MINUTES * shrink;
|
|
57
|
+
const windDown = exports.WIND_DOWN_MINUTES * shrink;
|
|
58
|
+
if (sinceWake < rise)
|
|
59
|
+
return ease(sinceWake / rise);
|
|
60
|
+
const untilBed = awakeMinutes - sinceWake;
|
|
61
|
+
return untilBed < windDown ? ease(untilBed / windDown) : 1;
|
|
62
|
+
}
|
|
63
|
+
//# sourceMappingURL=circadian-schedule.js.map
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { type DaySchedule } from "./circadian-schedule";
|
|
2
|
+
import { type LightLevels, type LightStyle } from "./light-styling";
|
|
3
|
+
export interface CircadianLighting {
|
|
4
|
+
styleAt(now: Date, sleeping: boolean): LightStyle;
|
|
5
|
+
}
|
|
6
|
+
export declare function scheduledLighting(schedule: DaySchedule, levels: LightLevels): CircadianLighting;
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.scheduledLighting = scheduledLighting;
|
|
4
|
+
const circadian_schedule_1 = require("./circadian-schedule");
|
|
5
|
+
const light_styling_1 = require("./light-styling");
|
|
6
|
+
// Circadian lighting that follows a wake and bed schedule; sleep mode holds
|
|
7
|
+
// the sleep settings.
|
|
8
|
+
function scheduledLighting(schedule, levels) {
|
|
9
|
+
return {
|
|
10
|
+
styleAt(now, sleeping) {
|
|
11
|
+
return (0, light_styling_1.styleAtLevel)(sleeping ? 0 : (0, circadian_schedule_1.scheduleLevel)(now, schedule.wakeTime, schedule.bedTime), levels);
|
|
12
|
+
}
|
|
13
|
+
};
|
|
14
|
+
}
|
|
15
|
+
//# sourceMappingURL=circadian.js.map
|