@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,587 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.classifyProjectorPower = classifyProjectorPower;
|
|
4
|
+
exports.normaliseConfig = normaliseConfig;
|
|
5
|
+
exports.transitionDisplay = transitionDisplay;
|
|
6
|
+
const observation_time_1 = require("./observation-time");
|
|
7
|
+
const MODES = { off: true, tv: true, projector: true };
|
|
8
|
+
const DEVICES = ["tv", "screen", "projector"];
|
|
9
|
+
function iso(now) {
|
|
10
|
+
return now.toISOString();
|
|
11
|
+
}
|
|
12
|
+
function initialState(saved = {}) {
|
|
13
|
+
return {
|
|
14
|
+
desired: saved.desired || null,
|
|
15
|
+
generation: Number.isInteger(saved.generation) ? saved.generation : 0,
|
|
16
|
+
phase: saved.phase || "idle",
|
|
17
|
+
observed: saved.observed || { tv: null, screen: null, projector: null },
|
|
18
|
+
seen: saved.seen || { tv: false, screen: false, projector: false },
|
|
19
|
+
querying: saved.querying || { tv: false, screen: false, projector: false },
|
|
20
|
+
pending: saved.pending || null,
|
|
21
|
+
failure: saved.failure || null,
|
|
22
|
+
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
|
+
fallbackUsed: saved.fallbackUsed === true
|
|
28
|
+
};
|
|
29
|
+
}
|
|
30
|
+
function classifyProjectorPower(watts, previous = "unknown", config = {}) {
|
|
31
|
+
const onWatts = Number(config.projectorOnWatts ?? 20);
|
|
32
|
+
const offWatts = Number(config.projectorOffWatts ?? 10);
|
|
33
|
+
if (!Number.isFinite(watts) || watts < 0)
|
|
34
|
+
return "unknown";
|
|
35
|
+
if (watts >= onWatts)
|
|
36
|
+
return "on";
|
|
37
|
+
if (watts <= offWatts)
|
|
38
|
+
return "standby";
|
|
39
|
+
return previous === "on" || previous === "standby" ? previous : "transitioning";
|
|
40
|
+
}
|
|
41
|
+
function statusFor(state) {
|
|
42
|
+
const mode = state.desired?.mode || "none";
|
|
43
|
+
const text = state.phase === "failed"
|
|
44
|
+
? `${mode}: ${state.failure?.reason || "failed"}`
|
|
45
|
+
: `${mode}: ${state.phase}`;
|
|
46
|
+
return { phase: state.phase, text };
|
|
47
|
+
}
|
|
48
|
+
function displayEvent(kind, state, details = {}) {
|
|
49
|
+
return {
|
|
50
|
+
display: {
|
|
51
|
+
kind,
|
|
52
|
+
requestId: state.desired?.requestId || null,
|
|
53
|
+
generation: state.desired?.generation || state.generation,
|
|
54
|
+
mode: state.desired?.mode || null,
|
|
55
|
+
...details
|
|
56
|
+
}
|
|
57
|
+
};
|
|
58
|
+
}
|
|
59
|
+
function commandFor(state, device, operation, values = {}, attempt = 1) {
|
|
60
|
+
return {
|
|
61
|
+
device,
|
|
62
|
+
operation,
|
|
63
|
+
requestId: state.desired?.requestId || null,
|
|
64
|
+
generation: state.desired?.generation || state.generation,
|
|
65
|
+
attempt,
|
|
66
|
+
...values
|
|
67
|
+
};
|
|
68
|
+
}
|
|
69
|
+
function queryFor(state, device) {
|
|
70
|
+
const operation = device === "screen" ? "query_position" : "query_power";
|
|
71
|
+
return commandFor(state, device, operation);
|
|
72
|
+
}
|
|
73
|
+
function allSeen(state) {
|
|
74
|
+
return DEVICES.every((device) => state.seen[device]);
|
|
75
|
+
}
|
|
76
|
+
function screenAtPosition(observed, target, tolerance) {
|
|
77
|
+
if (!observed || !Number.isFinite(observed.position))
|
|
78
|
+
return false;
|
|
79
|
+
if (observed.state === "opening" || observed.state === "closing")
|
|
80
|
+
return false;
|
|
81
|
+
return Math.abs(observed.position - target) <= tolerance;
|
|
82
|
+
}
|
|
83
|
+
function operationSatisfied(operation, observed, config) {
|
|
84
|
+
switch (operation) {
|
|
85
|
+
case "tv_on": return observed.tv?.power === "on";
|
|
86
|
+
case "tv_standby": return observed.tv?.power === "standby";
|
|
87
|
+
case "screen_open": return screenAtPosition(observed.screen, config.screenOpenPosition, config.screenTolerance);
|
|
88
|
+
case "screen_close": return screenAtPosition(observed.screen, 0, config.screenTolerance);
|
|
89
|
+
case "projector_start": return observed.projector?.power === "on";
|
|
90
|
+
case "projector_settle": return observed.projector?.power === "on" || observed.projector?.power === "standby";
|
|
91
|
+
case "projector_shutdown_observe":
|
|
92
|
+
case "projector_shutdown_confirm":
|
|
93
|
+
case "projector_cooldown": return observed.projector?.power === "standby";
|
|
94
|
+
default: return false;
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
// A finite number, or the default for missing or unusable input.
|
|
98
|
+
function setting(value, fallback) {
|
|
99
|
+
const parsed = Number(value ?? fallback);
|
|
100
|
+
return Number.isFinite(parsed) ? parsed : fallback;
|
|
101
|
+
}
|
|
102
|
+
function normaliseConfig(config = {}) {
|
|
103
|
+
const projectorWarmupMs = Math.max(1000, setting(config.projectorWarmupMs, 60000));
|
|
104
|
+
return {
|
|
105
|
+
screenOpenPosition: setting(config.screenOpenPosition, 82),
|
|
106
|
+
screenTolerance: Math.max(0, setting(config.screenTolerance, 2)),
|
|
107
|
+
pollMs: Math.max(250, setting(config.pollMs, 2000)),
|
|
108
|
+
queryTimeoutMs: Math.max(1000, setting(config.queryTimeoutMs, 5000)),
|
|
109
|
+
tvTimeoutMs: Math.max(1000, setting(config.tvTimeoutMs, 30000)),
|
|
110
|
+
screenTimeoutMs: Math.max(1000, setting(config.screenTimeoutMs, 65000)),
|
|
111
|
+
projectorWarmupMs,
|
|
112
|
+
projectorMinimumWarmupMs: Math.min(projectorWarmupMs, Math.max(0, setting(config.projectorMinimumWarmupMs, 15000))),
|
|
113
|
+
projectorShutdownConfirmMs: Math.max(1000, setting(config.projectorShutdownConfirmMs, 5000)),
|
|
114
|
+
projectorCooldownMs: Math.max(1000, setting(config.projectorCooldownMs, 120000)),
|
|
115
|
+
maxAttempts: Math.max(1, Math.floor(setting(config.maxAttempts, 2))),
|
|
116
|
+
projectorOnWatts: setting(config.projectorOnWatts, 20),
|
|
117
|
+
projectorOffWatts: setting(config.projectorOffWatts, 10),
|
|
118
|
+
projectorPowerFallback: config.projectorPowerFallback === true
|
|
119
|
+
};
|
|
120
|
+
}
|
|
121
|
+
function modeSatisfied(state, config) {
|
|
122
|
+
if (!state.desired || !allSeen(state))
|
|
123
|
+
return false;
|
|
124
|
+
if (state.desired.mode === "tv") {
|
|
125
|
+
return state.observed.tv?.power === "on" &&
|
|
126
|
+
state.observed.projector?.power === "standby" &&
|
|
127
|
+
screenAtPosition(state.observed.screen, 0, config.screenTolerance);
|
|
128
|
+
}
|
|
129
|
+
if (state.desired.mode === "projector") {
|
|
130
|
+
return state.observed.tv?.power === "standby" &&
|
|
131
|
+
state.observed.projector?.power === "on" &&
|
|
132
|
+
screenAtPosition(state.observed.screen, config.screenOpenPosition, config.screenTolerance);
|
|
133
|
+
}
|
|
134
|
+
return state.observed.tv?.power === "standby" &&
|
|
135
|
+
state.observed.projector?.power === "standby" &&
|
|
136
|
+
screenAtPosition(state.observed.screen, 0, config.screenTolerance);
|
|
137
|
+
}
|
|
138
|
+
function transitionDisplay(saved, msg, suppliedConfig = {}, now = new Date()) {
|
|
139
|
+
const config = normaliseConfig(suppliedConfig);
|
|
140
|
+
const state = initialState(saved);
|
|
141
|
+
const previousPhase = state.phase;
|
|
142
|
+
// Set when a device reading is stored: only then may a settled display
|
|
143
|
+
// report itself ready again.
|
|
144
|
+
let observationAccepted = false;
|
|
145
|
+
const event = msg?.display;
|
|
146
|
+
const commands = [];
|
|
147
|
+
const events = [];
|
|
148
|
+
const targetStates = [];
|
|
149
|
+
const traces = [];
|
|
150
|
+
function trace(name, outcome, reason = null, details = {}) {
|
|
151
|
+
traces.push({ event: name, outcome, reason, details });
|
|
152
|
+
}
|
|
153
|
+
function fail(device, operation, reason) {
|
|
154
|
+
state.pending = null;
|
|
155
|
+
state.phase = "failed";
|
|
156
|
+
state.blockedGeneration = state.desired?.generation || state.generation;
|
|
157
|
+
state.failure = { device, operation, reason, occurredAt: iso(now) };
|
|
158
|
+
events.push(displayEvent("failed", state, { failure: state.failure }));
|
|
159
|
+
trace("transition", "failed", reason, { device, operation });
|
|
160
|
+
}
|
|
161
|
+
function issue(device, operation, command, timeoutMs, retryable = true) {
|
|
162
|
+
const attempt = command.attempt || 1;
|
|
163
|
+
state.pending = {
|
|
164
|
+
device,
|
|
165
|
+
operation,
|
|
166
|
+
command,
|
|
167
|
+
attempt,
|
|
168
|
+
retryable,
|
|
169
|
+
deadlineAt: now.getTime() + timeoutMs,
|
|
170
|
+
nextAt: now.getTime() + config.pollMs
|
|
171
|
+
};
|
|
172
|
+
state.phase = "transitioning";
|
|
173
|
+
commands.push(command);
|
|
174
|
+
trace("command", "emitted", null, { device, operation, attempt });
|
|
175
|
+
state.lastAttempt = Math.max(state.lastAttempt, attempt);
|
|
176
|
+
if (device === "projector" && operation.startsWith("projector_") &&
|
|
177
|
+
config.projectorPowerFallback) {
|
|
178
|
+
state.fallbackUsed = true;
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
function issueTv(power) {
|
|
182
|
+
const operation = power === "on" ? "tv_on" : "tv_standby";
|
|
183
|
+
issue("tv", operation, commandFor(state, "tv", "set_power", { power }), config.tvTimeoutMs);
|
|
184
|
+
}
|
|
185
|
+
function issueScreen(position) {
|
|
186
|
+
const operation = position === 0 ? "screen_close" : "screen_open";
|
|
187
|
+
issue("screen", operation, commandFor(state, "screen", "set_position", { position }), config.screenTimeoutMs);
|
|
188
|
+
}
|
|
189
|
+
function issueProjectorStart() {
|
|
190
|
+
issue("projector", "projector_start", commandFor(state, "projector", "power_toggle", { reason: "start" }), config.projectorWarmupMs, false);
|
|
191
|
+
if (!state.pending)
|
|
192
|
+
throw new Error("projector start did not create pending state");
|
|
193
|
+
state.pending.readyAt = now.getTime() + config.projectorMinimumWarmupMs;
|
|
194
|
+
}
|
|
195
|
+
function issueProjectorShutdown() {
|
|
196
|
+
const command = commandFor(state, "projector", "power_toggle", { reason: "shutdown-request" });
|
|
197
|
+
state.pending = {
|
|
198
|
+
device: "projector",
|
|
199
|
+
operation: "projector_shutdown_observe",
|
|
200
|
+
command,
|
|
201
|
+
attempt: 1,
|
|
202
|
+
retryable: false,
|
|
203
|
+
deadlineAt: now.getTime() + config.projectorShutdownConfirmMs + config.queryTimeoutMs,
|
|
204
|
+
nextAt: now.getTime() + config.projectorShutdownConfirmMs
|
|
205
|
+
};
|
|
206
|
+
state.phase = "transitioning";
|
|
207
|
+
commands.push(command);
|
|
208
|
+
trace("command", "emitted", null, { device: "projector", operation: "shutdown-request", attempt: 1 });
|
|
209
|
+
state.lastAttempt = Math.max(state.lastAttempt, 1);
|
|
210
|
+
if (config.projectorPowerFallback)
|
|
211
|
+
state.fallbackUsed = true;
|
|
212
|
+
}
|
|
213
|
+
function beginDiscovery() {
|
|
214
|
+
state.phase = "discovering";
|
|
215
|
+
for (const device of DEVICES) {
|
|
216
|
+
if (!state.seen[device] && !state.querying[device]) {
|
|
217
|
+
state.querying[device] = true;
|
|
218
|
+
commands.push(queryFor(state, device));
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
// A reading between projectorOffWatts and projectorOnWatts is a lamp warming
|
|
223
|
+
// up or cooling down: re-query at pollMs until it settles, never toggle on it.
|
|
224
|
+
// Either phase may legitimately last up to its own timeout.
|
|
225
|
+
function awaitProjectorPower() {
|
|
226
|
+
state.pending = {
|
|
227
|
+
device: "projector",
|
|
228
|
+
operation: "projector_settle",
|
|
229
|
+
command: queryFor(state, "projector"),
|
|
230
|
+
attempt: 1,
|
|
231
|
+
retryable: false,
|
|
232
|
+
deadlineAt: now.getTime() + Math.max(config.projectorWarmupMs, config.projectorCooldownMs),
|
|
233
|
+
nextAt: now.getTime() + config.pollMs
|
|
234
|
+
};
|
|
235
|
+
state.phase = "transitioning";
|
|
236
|
+
trace("projector", "settling", null, { watts: state.observed.projector?.watts ?? null });
|
|
237
|
+
}
|
|
238
|
+
function markReady() {
|
|
239
|
+
if (state.phase !== "ready") {
|
|
240
|
+
state.phase = "ready";
|
|
241
|
+
state.failure = null;
|
|
242
|
+
events.push(displayEvent("ready", state));
|
|
243
|
+
trace("transition", "ready");
|
|
244
|
+
}
|
|
245
|
+
state.classification = null;
|
|
246
|
+
}
|
|
247
|
+
function reconcile() {
|
|
248
|
+
if (!state.desired) {
|
|
249
|
+
state.phase = allSeen(state) ? "idle" : state.phase;
|
|
250
|
+
return;
|
|
251
|
+
}
|
|
252
|
+
if (state.blockedGeneration === state.desired.generation) {
|
|
253
|
+
state.phase = "failed";
|
|
254
|
+
return;
|
|
255
|
+
}
|
|
256
|
+
if (state.classification === "manual_override") {
|
|
257
|
+
state.phase = "manual_override";
|
|
258
|
+
return;
|
|
259
|
+
}
|
|
260
|
+
if (state.pending)
|
|
261
|
+
return;
|
|
262
|
+
if (!allSeen(state)) {
|
|
263
|
+
beginDiscovery();
|
|
264
|
+
return;
|
|
265
|
+
}
|
|
266
|
+
const mode = state.desired.mode;
|
|
267
|
+
if (mode === "tv") {
|
|
268
|
+
if (state.observed.tv?.power !== "on")
|
|
269
|
+
return issueTv("on");
|
|
270
|
+
if (state.observed.projector?.power !== "standby") {
|
|
271
|
+
if (state.observed.projector?.power === "on")
|
|
272
|
+
return issueProjectorShutdown();
|
|
273
|
+
return awaitProjectorPower();
|
|
274
|
+
}
|
|
275
|
+
if (!screenAtPosition(state.observed.screen, 0, config.screenTolerance))
|
|
276
|
+
return issueScreen(0);
|
|
277
|
+
return markReady();
|
|
278
|
+
}
|
|
279
|
+
if (mode === "projector") {
|
|
280
|
+
if (state.observed.tv?.power !== "standby")
|
|
281
|
+
return issueTv("standby");
|
|
282
|
+
if (!screenAtPosition(state.observed.screen, config.screenOpenPosition, config.screenTolerance)) {
|
|
283
|
+
return issueScreen(config.screenOpenPosition);
|
|
284
|
+
}
|
|
285
|
+
if (state.observed.projector?.power !== "on") {
|
|
286
|
+
if (state.observed.projector?.power === "standby")
|
|
287
|
+
return issueProjectorStart();
|
|
288
|
+
return awaitProjectorPower();
|
|
289
|
+
}
|
|
290
|
+
return markReady();
|
|
291
|
+
}
|
|
292
|
+
if (state.observed.tv?.power !== "standby")
|
|
293
|
+
return issueTv("standby");
|
|
294
|
+
if (state.observed.projector?.power !== "standby") {
|
|
295
|
+
if (state.observed.projector?.power === "on")
|
|
296
|
+
return issueProjectorShutdown();
|
|
297
|
+
return awaitProjectorPower();
|
|
298
|
+
}
|
|
299
|
+
if (!screenAtPosition(state.observed.screen, 0, config.screenTolerance))
|
|
300
|
+
return issueScreen(0);
|
|
301
|
+
return markReady();
|
|
302
|
+
}
|
|
303
|
+
function acceptObservation(event) {
|
|
304
|
+
const device = event.device;
|
|
305
|
+
if (!DEVICES.includes(device))
|
|
306
|
+
throw new Error("unknown display observation device");
|
|
307
|
+
if (Number.isInteger(event.generation) && state.desired && event.generation !== state.desired.generation) {
|
|
308
|
+
trace("observation", "ignored", "stale-generation", { device, generation: event.generation });
|
|
309
|
+
return;
|
|
310
|
+
}
|
|
311
|
+
// A future time counts as now; a reading older than the stored one (an
|
|
312
|
+
// overlapping poll answering late) is ignored.
|
|
313
|
+
const reportedMs = event.observedAt ? (0, observation_time_1.parseObservedAt)(event.observedAt) : null;
|
|
314
|
+
const bounded = reportedMs === null ? null : (0, observation_time_1.boundObservedAt)(event.observedAt, reportedMs, now.getTime());
|
|
315
|
+
if (bounded?.skewed) {
|
|
316
|
+
trace("observation", "adjusted", "clock-skew", { device, reported: event.observedAt, usedAt: bounded.observedAt });
|
|
317
|
+
}
|
|
318
|
+
const observedAt = bounded ? bounded.observedAt : event.observedAt || iso(now);
|
|
319
|
+
if ((0, observation_time_1.isOutOfOrder)((0, observation_time_1.parseObservedAt)(observedAt) ?? now.getTime(), state.observed[device]?.observedAt, now.getTime())) {
|
|
320
|
+
trace("observation", "ignored", "out-of-order", { device });
|
|
321
|
+
return;
|
|
322
|
+
}
|
|
323
|
+
const source = event.source || "unknown";
|
|
324
|
+
if (device === "tv") {
|
|
325
|
+
if (event.power !== "on" && event.power !== "standby")
|
|
326
|
+
throw new Error("invalid TV power observation");
|
|
327
|
+
state.observed.tv = { power: event.power, observedAt, source };
|
|
328
|
+
}
|
|
329
|
+
else if (device === "screen") {
|
|
330
|
+
const position = Number(event.position);
|
|
331
|
+
if (!Number.isFinite(position) || position < 0 || position > 100)
|
|
332
|
+
throw new Error("invalid screen position");
|
|
333
|
+
state.observed.screen = { position, state: event.state || "unknown", observedAt, source };
|
|
334
|
+
}
|
|
335
|
+
else {
|
|
336
|
+
const watts = Number(event.watts);
|
|
337
|
+
if (!Number.isFinite(watts) || watts < 0)
|
|
338
|
+
throw new Error("invalid projector power observation");
|
|
339
|
+
const previous = state.observed.projector?.power || "unknown";
|
|
340
|
+
state.observed.projector = {
|
|
341
|
+
watts,
|
|
342
|
+
power: classifyProjectorPower(watts, previous, config),
|
|
343
|
+
observedAt,
|
|
344
|
+
source
|
|
345
|
+
};
|
|
346
|
+
}
|
|
347
|
+
state.seen[device] = true;
|
|
348
|
+
observationAccepted = true;
|
|
349
|
+
state.querying[device] = false;
|
|
350
|
+
trace("observation", "accepted", null, { device });
|
|
351
|
+
if (state.phase === "ready" && !state.pending && state.desired &&
|
|
352
|
+
allSeen(state) && !modeSatisfied(state, config)) {
|
|
353
|
+
state.phase = "manual_override";
|
|
354
|
+
state.classification = "manual_override";
|
|
355
|
+
events.push(displayEvent("manual_override", state, { device }));
|
|
356
|
+
trace("observation", "manual_override", null, { device });
|
|
357
|
+
return;
|
|
358
|
+
}
|
|
359
|
+
if (!state.pending || state.pending.device !== device)
|
|
360
|
+
return reconcile();
|
|
361
|
+
if (state.pending.operation === "projector_start" &&
|
|
362
|
+
state.pending.readyAt !== undefined &&
|
|
363
|
+
state.observed.projector?.power === "on" && now.getTime() < state.pending.readyAt) {
|
|
364
|
+
state.pending.nextAt = Math.min(state.pending.deadlineAt, state.pending.readyAt);
|
|
365
|
+
return;
|
|
366
|
+
}
|
|
367
|
+
if (operationSatisfied(state.pending.operation, state.observed, config)) {
|
|
368
|
+
state.pending = null;
|
|
369
|
+
return reconcile();
|
|
370
|
+
}
|
|
371
|
+
if (state.pending.operation === "projector_shutdown_confirm" && device === "projector") {
|
|
372
|
+
if (state.observed.projector?.power === "on") {
|
|
373
|
+
const command = commandFor(state, "projector", "power_toggle", { reason: "shutdown-confirm" });
|
|
374
|
+
commands.push(command);
|
|
375
|
+
state.pending = {
|
|
376
|
+
device: "projector",
|
|
377
|
+
operation: "projector_cooldown",
|
|
378
|
+
command,
|
|
379
|
+
attempt: 1,
|
|
380
|
+
retryable: false,
|
|
381
|
+
deadlineAt: now.getTime() + config.projectorCooldownMs,
|
|
382
|
+
nextAt: now.getTime() + config.pollMs
|
|
383
|
+
};
|
|
384
|
+
trace("command", "emitted", null, { device: "projector", operation: "shutdown-confirm", attempt: 1 });
|
|
385
|
+
state.lastAttempt = Math.max(state.lastAttempt, 1);
|
|
386
|
+
if (config.projectorPowerFallback)
|
|
387
|
+
state.fallbackUsed = true;
|
|
388
|
+
}
|
|
389
|
+
}
|
|
390
|
+
}
|
|
391
|
+
function handleTick(event) {
|
|
392
|
+
const pending = state.pending;
|
|
393
|
+
if (!pending)
|
|
394
|
+
return reconcile();
|
|
395
|
+
if (Number.isInteger(event.generation) && state.desired && event.generation !== state.desired.generation) {
|
|
396
|
+
trace("timer", "ignored", "stale-generation", { generation: event.generation });
|
|
397
|
+
return;
|
|
398
|
+
}
|
|
399
|
+
if (now.getTime() < pending.nextAt)
|
|
400
|
+
return;
|
|
401
|
+
if (pending.operation === "projector_shutdown_observe") {
|
|
402
|
+
commands.push(queryFor(state, "projector"));
|
|
403
|
+
pending.operation = "projector_shutdown_confirm";
|
|
404
|
+
pending.nextAt = now.getTime() + config.queryTimeoutMs;
|
|
405
|
+
pending.deadlineAt = pending.nextAt;
|
|
406
|
+
trace("command", "emitted", null, { device: "projector", operation: "query-before-confirm", attempt: 1 });
|
|
407
|
+
return;
|
|
408
|
+
}
|
|
409
|
+
if (now.getTime() >= pending.deadlineAt) {
|
|
410
|
+
if (pending.retryable && pending.attempt < config.maxAttempts) {
|
|
411
|
+
const attempt = pending.attempt + 1;
|
|
412
|
+
const retried = { ...pending.command, attempt };
|
|
413
|
+
commands.push(retried);
|
|
414
|
+
pending.command = retried;
|
|
415
|
+
pending.attempt = attempt;
|
|
416
|
+
const timeout = pending.device === "tv" ? config.tvTimeoutMs : config.screenTimeoutMs;
|
|
417
|
+
pending.deadlineAt = now.getTime() + timeout;
|
|
418
|
+
pending.nextAt = now.getTime() + config.pollMs;
|
|
419
|
+
trace("command", "retried", null, { device: pending.device, operation: pending.operation, attempt });
|
|
420
|
+
state.lastAttempt = Math.max(state.lastAttempt, attempt);
|
|
421
|
+
return;
|
|
422
|
+
}
|
|
423
|
+
return fail(pending.device, pending.operation, pending.operation === "projector_settle" ? "projector-power-unsettled" : "confirmation-timeout");
|
|
424
|
+
}
|
|
425
|
+
commands.push(queryFor(state, pending.device));
|
|
426
|
+
pending.nextAt = Math.min(pending.deadlineAt, now.getTime() + config.pollMs);
|
|
427
|
+
trace("command", "emitted", null, { device: pending.device, operation: "confirmation-query" });
|
|
428
|
+
}
|
|
429
|
+
if (!event || typeof event.kind !== "string")
|
|
430
|
+
throw new Error("msg.display.kind is required");
|
|
431
|
+
if (event.kind === "startup") {
|
|
432
|
+
// A projector power operation in flight cannot be repeated safely (a
|
|
433
|
+
// second toggle can switch a warming lamp off), so it survives startup
|
|
434
|
+
// and is only re-observed; everything else is rediscovered.
|
|
435
|
+
const inFlight = state.pending && !state.pending.retryable ? state.pending : null;
|
|
436
|
+
state.pending = null;
|
|
437
|
+
state.seen = { tv: false, screen: false, projector: false };
|
|
438
|
+
state.querying = { tv: false, screen: false, projector: false };
|
|
439
|
+
beginDiscovery();
|
|
440
|
+
if (inFlight) {
|
|
441
|
+
const deadlineAt = Math.max(inFlight.deadlineAt, now.getTime() + config.queryTimeoutMs);
|
|
442
|
+
state.pending = { ...inFlight, deadlineAt, nextAt: Math.min(deadlineAt, now.getTime() + config.pollMs) };
|
|
443
|
+
state.phase = "transitioning";
|
|
444
|
+
}
|
|
445
|
+
trace("startup", "querying", inFlight ? "projector-operation-kept" : null);
|
|
446
|
+
}
|
|
447
|
+
else if (event.kind === "intent") {
|
|
448
|
+
if (!Object.hasOwn(MODES, event.mode))
|
|
449
|
+
throw new Error("display mode must be off, tv or projector");
|
|
450
|
+
if (typeof event.requestId !== "string" || !event.requestId)
|
|
451
|
+
throw new Error("display requestId is required");
|
|
452
|
+
if (!Number.isInteger(event.generation) || event.generation < 1)
|
|
453
|
+
throw new Error("display generation must be a positive integer");
|
|
454
|
+
if (event.generation < state.generation) {
|
|
455
|
+
events.push(displayEvent("rejected", state, { reason: "stale-generation" }));
|
|
456
|
+
trace("intent", "rejected", "stale-generation");
|
|
457
|
+
}
|
|
458
|
+
else if (event.generation === state.generation && state.desired) {
|
|
459
|
+
const same = state.desired.requestId === event.requestId && state.desired.mode === event.mode;
|
|
460
|
+
if (!same) {
|
|
461
|
+
events.push(displayEvent("rejected", state, { reason: "request-conflict" }));
|
|
462
|
+
trace("intent", "rejected", "request-conflict");
|
|
463
|
+
}
|
|
464
|
+
}
|
|
465
|
+
else {
|
|
466
|
+
if (state.desired && state.phase !== "ready") {
|
|
467
|
+
events.push(displayEvent("superseded", state, { supersededBy: event.requestId }));
|
|
468
|
+
}
|
|
469
|
+
state.desired = { requestId: event.requestId, generation: event.generation, mode: event.mode };
|
|
470
|
+
state.generation = event.generation;
|
|
471
|
+
state.failure = null;
|
|
472
|
+
state.classification = null;
|
|
473
|
+
state.lastAttempt = 0;
|
|
474
|
+
state.fallbackUsed = false;
|
|
475
|
+
state.blockedGeneration = null;
|
|
476
|
+
state.pending = null;
|
|
477
|
+
state.seen = { tv: false, screen: false, projector: false };
|
|
478
|
+
state.querying = { tv: false, screen: false, projector: false };
|
|
479
|
+
beginDiscovery();
|
|
480
|
+
trace("intent", "accepted", null, { mode: event.mode });
|
|
481
|
+
}
|
|
482
|
+
}
|
|
483
|
+
else if (event.kind === "observed") {
|
|
484
|
+
acceptObservation(event);
|
|
485
|
+
}
|
|
486
|
+
else if (event.kind === "transport_failed") {
|
|
487
|
+
if (Number.isInteger(event.generation) && state.desired && event.generation !== state.desired.generation) {
|
|
488
|
+
trace("transport", "ignored", "stale-generation", { device: event.device });
|
|
489
|
+
}
|
|
490
|
+
else if (state.querying[event.device]) {
|
|
491
|
+
state.querying[event.device] = false;
|
|
492
|
+
fail(event.device, event.operation, event.reason || "transport-failed");
|
|
493
|
+
}
|
|
494
|
+
else if (state.pending && state.pending.device === event.device &&
|
|
495
|
+
state.pending.command.operation === event.operation) {
|
|
496
|
+
if (state.pending.retryable && state.pending.attempt < config.maxAttempts) {
|
|
497
|
+
const attempt = state.pending.attempt + 1;
|
|
498
|
+
const retried = { ...state.pending.command, attempt };
|
|
499
|
+
state.pending.command = retried;
|
|
500
|
+
state.pending.attempt = attempt;
|
|
501
|
+
commands.push(retried);
|
|
502
|
+
trace("command", "retried", event.reason || "transport-failed", {
|
|
503
|
+
device: event.device, operation: state.pending.operation, attempt
|
|
504
|
+
});
|
|
505
|
+
state.lastAttempt = Math.max(state.lastAttempt, attempt);
|
|
506
|
+
}
|
|
507
|
+
else {
|
|
508
|
+
fail(event.device, state.pending.operation, event.reason || "transport-failed");
|
|
509
|
+
}
|
|
510
|
+
}
|
|
511
|
+
else {
|
|
512
|
+
trace("transport", "ignored", "confirmation-query-failed", { device: event.device });
|
|
513
|
+
}
|
|
514
|
+
}
|
|
515
|
+
else if (event.kind === "tick") {
|
|
516
|
+
handleTick(event);
|
|
517
|
+
}
|
|
518
|
+
else if (event.kind === "refresh") {
|
|
519
|
+
// Re-confirm a settled display from fresh readings; its target state
|
|
520
|
+
// follows them. These queries are not discovery, so a failed one is
|
|
521
|
+
// ignored rather than failing the request.
|
|
522
|
+
if (state.desired && state.phase === "ready" && !state.pending) {
|
|
523
|
+
for (const device of DEVICES)
|
|
524
|
+
commands.push(queryFor(state, device));
|
|
525
|
+
trace("refresh", "querying");
|
|
526
|
+
}
|
|
527
|
+
else {
|
|
528
|
+
trace("refresh", "reported", null, { phase: state.phase });
|
|
529
|
+
}
|
|
530
|
+
}
|
|
531
|
+
else {
|
|
532
|
+
throw new Error("unknown display event kind");
|
|
533
|
+
}
|
|
534
|
+
// The theatre activity controller's view of this target. A settled display
|
|
535
|
+
// reports ready only with a reading taken in this event, stamped with the
|
|
536
|
+
// oldest of the device readings every mode relies on, so it is only as
|
|
537
|
+
// fresh as its least recent confirmation.
|
|
538
|
+
if (state.desired) {
|
|
539
|
+
const readings = DEVICES.map((device) => (0, observation_time_1.parseObservedAt)(state.observed[device]?.observedAt));
|
|
540
|
+
const confirmedMs = readings.every((ms) => ms !== null) ? Math.min(...readings) : null;
|
|
541
|
+
const status = state.phase === "manual_override"
|
|
542
|
+
? "transitioning"
|
|
543
|
+
: state.phase === "idle" ? "confirming" : state.phase;
|
|
544
|
+
if (!(status === "ready" && previousPhase === "ready" && !observationAccepted)) {
|
|
545
|
+
const theatre = {
|
|
546
|
+
kind: "target_state",
|
|
547
|
+
target: "display",
|
|
548
|
+
requestId: state.desired.requestId,
|
|
549
|
+
generation: state.desired.generation,
|
|
550
|
+
status,
|
|
551
|
+
observedAt: status === "ready" && confirmedMs !== null ? new Date(confirmedMs).toISOString() : now.toISOString(),
|
|
552
|
+
details: {
|
|
553
|
+
mode: state.desired.mode,
|
|
554
|
+
observed: {
|
|
555
|
+
tv: state.observed.tv?.power ?? null,
|
|
556
|
+
screen: state.observed.screen?.position ?? null,
|
|
557
|
+
projector: state.observed.projector?.power ?? null
|
|
558
|
+
},
|
|
559
|
+
recovery: { mode: state.phase === "ready" ? state.desired.mode : null },
|
|
560
|
+
metrics: {
|
|
561
|
+
attempts: state.lastAttempt,
|
|
562
|
+
retries: Math.max(0, state.lastAttempt - 1),
|
|
563
|
+
fallbackUsed: state.fallbackUsed
|
|
564
|
+
}
|
|
565
|
+
}
|
|
566
|
+
};
|
|
567
|
+
if (state.classification)
|
|
568
|
+
theatre.classification = state.classification;
|
|
569
|
+
if (state.failure)
|
|
570
|
+
theatre.failure = { ...state.failure };
|
|
571
|
+
targetStates.push({ theatre });
|
|
572
|
+
}
|
|
573
|
+
}
|
|
574
|
+
events.unshift(displayEvent("state", state, {
|
|
575
|
+
phase: state.phase,
|
|
576
|
+
observed: state.observed,
|
|
577
|
+
failure: state.failure,
|
|
578
|
+
classification: state.classification,
|
|
579
|
+
metrics: {
|
|
580
|
+
attempts: state.lastAttempt,
|
|
581
|
+
retries: Math.max(0, state.lastAttempt - 1),
|
|
582
|
+
fallbackUsed: state.fallbackUsed
|
|
583
|
+
}
|
|
584
|
+
}));
|
|
585
|
+
return { state, commands, events, targetStates, traces, status: statusFor(state) };
|
|
586
|
+
}
|
|
587
|
+
//# sourceMappingURL=display-mode-controller.js.map
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import type { DisplayDevice, DisplayMessage } from "./display-mode-controller";
|
|
2
|
+
interface DeviceResponse {
|
|
3
|
+
error?: unknown;
|
|
4
|
+
result?: Array<{
|
|
5
|
+
status?: unknown;
|
|
6
|
+
}>;
|
|
7
|
+
current_pos?: unknown;
|
|
8
|
+
state?: unknown;
|
|
9
|
+
apower?: unknown;
|
|
10
|
+
}
|
|
11
|
+
export interface DisplayTransportOptions {
|
|
12
|
+
tvHost: string;
|
|
13
|
+
tvPsk: string;
|
|
14
|
+
screenHost: string;
|
|
15
|
+
projectorMeterHost: string;
|
|
16
|
+
timeoutMs?: number;
|
|
17
|
+
fetch?: typeof globalThis.fetch;
|
|
18
|
+
}
|
|
19
|
+
export interface DisplayTransportCommand {
|
|
20
|
+
device: DisplayDevice;
|
|
21
|
+
operation: string;
|
|
22
|
+
generation: number;
|
|
23
|
+
power?: string;
|
|
24
|
+
position?: number;
|
|
25
|
+
}
|
|
26
|
+
export declare function safeReason(error: unknown): string;
|
|
27
|
+
export declare class DisplayTransport {
|
|
28
|
+
readonly tvHost: string;
|
|
29
|
+
readonly tvPsk: string;
|
|
30
|
+
readonly screenHost: string;
|
|
31
|
+
readonly projectorMeterHost: string;
|
|
32
|
+
readonly timeoutMs: number;
|
|
33
|
+
readonly fetch: typeof globalThis.fetch;
|
|
34
|
+
constructor(options: DisplayTransportOptions);
|
|
35
|
+
signal(): AbortSignal;
|
|
36
|
+
bravia(method: string, params: unknown[]): Promise<DeviceResponse>;
|
|
37
|
+
shellyGet(deviceHost: string, component: string): Promise<DeviceResponse>;
|
|
38
|
+
shellyPost(deviceHost: string, component: string, params: Record<string, unknown>): Promise<void>;
|
|
39
|
+
execute(command: DisplayTransportCommand, now?: Date): Promise<DisplayMessage | null>;
|
|
40
|
+
}
|
|
41
|
+
export {};
|