@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,130 @@
|
|
|
1
|
+
export type CoverMotion = "opening" | "closing" | "stopped";
|
|
2
|
+
export type CoverOperation = "set_position" | "stop" | "observe";
|
|
3
|
+
export interface CoverConfig {
|
|
4
|
+
positionTolerance: number;
|
|
5
|
+
invert: boolean;
|
|
6
|
+
travelTimeoutMs: number;
|
|
7
|
+
maxAttempts: number;
|
|
8
|
+
pollMs: number;
|
|
9
|
+
settleMs: number;
|
|
10
|
+
}
|
|
11
|
+
export interface CoverDesired {
|
|
12
|
+
requestId: string;
|
|
13
|
+
generation: number;
|
|
14
|
+
operation: "set_position" | "stop";
|
|
15
|
+
position: number | null;
|
|
16
|
+
source: string | null;
|
|
17
|
+
requestedAt: string;
|
|
18
|
+
}
|
|
19
|
+
export interface CoverObservation {
|
|
20
|
+
position: number;
|
|
21
|
+
motion: CoverMotion | null;
|
|
22
|
+
observedAt: string;
|
|
23
|
+
}
|
|
24
|
+
export interface CoverPending {
|
|
25
|
+
operation: CoverOperation;
|
|
26
|
+
attempt: number;
|
|
27
|
+
issuedAt: number;
|
|
28
|
+
deadlineAt: number;
|
|
29
|
+
nextAt: number;
|
|
30
|
+
stablePosition: number | null;
|
|
31
|
+
stableSince: number | null;
|
|
32
|
+
startPosition: number | null;
|
|
33
|
+
moved: boolean;
|
|
34
|
+
verifying: boolean;
|
|
35
|
+
}
|
|
36
|
+
export interface CoverFailure {
|
|
37
|
+
operation: CoverOperation;
|
|
38
|
+
reason: string;
|
|
39
|
+
occurredAt: string;
|
|
40
|
+
}
|
|
41
|
+
export interface CoverState {
|
|
42
|
+
desired: CoverDesired | null;
|
|
43
|
+
generation: number;
|
|
44
|
+
phase: string;
|
|
45
|
+
observed: CoverObservation | null;
|
|
46
|
+
pending: CoverPending | null;
|
|
47
|
+
failure: CoverFailure | null;
|
|
48
|
+
blockedGeneration: number | null;
|
|
49
|
+
classification: "manual_override" | null;
|
|
50
|
+
lastAttempt: number;
|
|
51
|
+
}
|
|
52
|
+
interface IntentBase {
|
|
53
|
+
requestId: string;
|
|
54
|
+
generation: number;
|
|
55
|
+
source?: string;
|
|
56
|
+
requestedAt?: string;
|
|
57
|
+
}
|
|
58
|
+
export type CoverEvent = (IntentBase & {
|
|
59
|
+
kind: "set_position";
|
|
60
|
+
position: number;
|
|
61
|
+
}) | (IntentBase & {
|
|
62
|
+
kind: "open" | "close" | "stop";
|
|
63
|
+
}) | {
|
|
64
|
+
kind: "observed";
|
|
65
|
+
position: number;
|
|
66
|
+
motion?: CoverMotion;
|
|
67
|
+
observedAt: string;
|
|
68
|
+
} | {
|
|
69
|
+
kind: "command_result";
|
|
70
|
+
generation: number;
|
|
71
|
+
ok: boolean;
|
|
72
|
+
reason?: string;
|
|
73
|
+
} | {
|
|
74
|
+
kind: "tick";
|
|
75
|
+
generation: number;
|
|
76
|
+
} | {
|
|
77
|
+
kind: "startup" | "refresh";
|
|
78
|
+
};
|
|
79
|
+
export interface CoverMessage {
|
|
80
|
+
cover?: CoverEvent;
|
|
81
|
+
}
|
|
82
|
+
export interface CoverCommand {
|
|
83
|
+
operation: CoverOperation;
|
|
84
|
+
requestId: string;
|
|
85
|
+
generation: number;
|
|
86
|
+
position?: number;
|
|
87
|
+
attempt: number;
|
|
88
|
+
}
|
|
89
|
+
export interface CoverOutputEvent {
|
|
90
|
+
theatre: {
|
|
91
|
+
kind: "target_state";
|
|
92
|
+
target: "cover";
|
|
93
|
+
requestId: string;
|
|
94
|
+
generation: number;
|
|
95
|
+
status: string;
|
|
96
|
+
observedAt: string;
|
|
97
|
+
details: {
|
|
98
|
+
operation: "set_position" | "stop";
|
|
99
|
+
position: number | null;
|
|
100
|
+
observedPosition: number | null;
|
|
101
|
+
motion: CoverMotion | null;
|
|
102
|
+
source: string | null;
|
|
103
|
+
recovery: Record<string, unknown>;
|
|
104
|
+
metrics: {
|
|
105
|
+
attempts: number;
|
|
106
|
+
retries: number;
|
|
107
|
+
fallbackUsed: boolean;
|
|
108
|
+
};
|
|
109
|
+
};
|
|
110
|
+
classification?: "manual_override";
|
|
111
|
+
failure?: CoverFailure;
|
|
112
|
+
};
|
|
113
|
+
}
|
|
114
|
+
export declare function normaliseCoverConfig(config?: Partial<CoverConfig>): CoverConfig;
|
|
115
|
+
export declare function transitionCover(saved: Partial<CoverState> | undefined, msg: CoverMessage, suppliedConfig?: Partial<CoverConfig>, nowValue?: Date | string | number): {
|
|
116
|
+
state: CoverState;
|
|
117
|
+
commands: CoverCommand[];
|
|
118
|
+
events: CoverOutputEvent[];
|
|
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
|
+
};
|
|
130
|
+
export {};
|
|
@@ -0,0 +1,336 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.normaliseCoverConfig = normaliseCoverConfig;
|
|
4
|
+
exports.transitionCover = transitionCover;
|
|
5
|
+
const observation_time_1 = require("./observation-time");
|
|
6
|
+
const MOTIONS = { opening: true, closing: true, stopped: true };
|
|
7
|
+
function normaliseCoverConfig(config = {}) {
|
|
8
|
+
const tolerance = Number(config.positionTolerance);
|
|
9
|
+
const pollMs = Math.max(250, Number(config.pollMs) || 5000);
|
|
10
|
+
return {
|
|
11
|
+
positionTolerance: Number.isFinite(tolerance) && tolerance >= 0 ? Math.min(tolerance, 50) : 2,
|
|
12
|
+
invert: config.invert === true,
|
|
13
|
+
travelTimeoutMs: Math.max(1000, Number(config.travelTimeoutMs) || 90000),
|
|
14
|
+
maxAttempts: Math.max(1, Math.floor(Number(config.maxAttempts) || 2)),
|
|
15
|
+
pollMs,
|
|
16
|
+
settleMs: Math.max(250, Number(config.settleMs) || pollMs)
|
|
17
|
+
};
|
|
18
|
+
}
|
|
19
|
+
function initialState(saved = {}) {
|
|
20
|
+
return {
|
|
21
|
+
desired: saved.desired && typeof saved.desired === "object" ? { ...saved.desired } : null,
|
|
22
|
+
generation: Number.isInteger(saved.generation) && (saved.generation ?? -1) >= 0 ? saved.generation : 0,
|
|
23
|
+
phase: typeof saved.phase === "string" ? saved.phase : "idle",
|
|
24
|
+
observed: saved.observed && typeof saved.observed === "object" ? { ...saved.observed } : null,
|
|
25
|
+
pending: saved.pending && typeof saved.pending === "object" ? { ...saved.pending } : null,
|
|
26
|
+
failure: saved.failure && typeof saved.failure === "object" ? { ...saved.failure } : null,
|
|
27
|
+
blockedGeneration: Number.isInteger(saved.blockedGeneration) ? saved.blockedGeneration : null,
|
|
28
|
+
classification: saved.classification === "manual_override" ? "manual_override" : null,
|
|
29
|
+
lastAttempt: Number.isInteger(saved.lastAttempt) && (saved.lastAttempt ?? -1) >= 0 ? saved.lastAttempt : 0
|
|
30
|
+
};
|
|
31
|
+
}
|
|
32
|
+
function validPosition(value, label) {
|
|
33
|
+
if (typeof value !== "number" || !Number.isFinite(value) || value < 0 || value > 100) {
|
|
34
|
+
throw new Error(`${label} must be a number from 0 to 100`);
|
|
35
|
+
}
|
|
36
|
+
return value;
|
|
37
|
+
}
|
|
38
|
+
function statusFor(state) {
|
|
39
|
+
const target = state.desired?.operation === "stop" && state.desired.position === null
|
|
40
|
+
? "stop"
|
|
41
|
+
: state.desired?.position ?? "none";
|
|
42
|
+
return { phase: state.phase, text: `${target}: ${state.phase}` };
|
|
43
|
+
}
|
|
44
|
+
function transitionCover(saved, msg, suppliedConfig = {}, nowValue = new Date()) {
|
|
45
|
+
const config = normaliseCoverConfig(suppliedConfig);
|
|
46
|
+
const now = nowValue instanceof Date ? nowValue : new Date(nowValue);
|
|
47
|
+
const nowMs = now.getTime();
|
|
48
|
+
if (!Number.isFinite(nowMs))
|
|
49
|
+
throw new Error("now must be a valid date");
|
|
50
|
+
const state = initialState(saved);
|
|
51
|
+
const event = msg?.cover;
|
|
52
|
+
const commands = [];
|
|
53
|
+
const events = [];
|
|
54
|
+
const traces = [];
|
|
55
|
+
if (!event || typeof event.kind !== "string")
|
|
56
|
+
throw new Error("msg.cover.kind is required");
|
|
57
|
+
function trace(name, outcome, reason = null, details = {}) {
|
|
58
|
+
traces.push({ event: name, outcome, reason, details });
|
|
59
|
+
}
|
|
60
|
+
function emit(operation, attempt) {
|
|
61
|
+
if (!state.desired)
|
|
62
|
+
throw new Error("cover command requires desired state");
|
|
63
|
+
const command = {
|
|
64
|
+
operation,
|
|
65
|
+
requestId: state.desired.requestId,
|
|
66
|
+
generation: state.generation,
|
|
67
|
+
attempt
|
|
68
|
+
};
|
|
69
|
+
if (operation === "set_position" && state.desired.position !== null) {
|
|
70
|
+
command.position = config.invert ? 100 - state.desired.position : state.desired.position;
|
|
71
|
+
}
|
|
72
|
+
commands.push(command);
|
|
73
|
+
trace("command", "emitted", null, { operation, position: command.position ?? null, attempt });
|
|
74
|
+
}
|
|
75
|
+
function issue(operation, attempt, verifying = false) {
|
|
76
|
+
emit(operation, attempt);
|
|
77
|
+
state.pending = {
|
|
78
|
+
operation,
|
|
79
|
+
attempt,
|
|
80
|
+
issuedAt: nowMs,
|
|
81
|
+
deadlineAt: nowMs + config.travelTimeoutMs,
|
|
82
|
+
nextAt: nowMs + config.pollMs,
|
|
83
|
+
stablePosition: null,
|
|
84
|
+
stableSince: null,
|
|
85
|
+
startPosition: state.observed?.position ?? null,
|
|
86
|
+
moved: false,
|
|
87
|
+
verifying
|
|
88
|
+
};
|
|
89
|
+
state.phase = operation === "set_position" ? "moving" : operation === "stop" ? "stopping" : "confirming";
|
|
90
|
+
if (operation !== "observe")
|
|
91
|
+
state.lastAttempt = Math.max(state.lastAttempt, attempt);
|
|
92
|
+
}
|
|
93
|
+
function retryOrFail(reason) {
|
|
94
|
+
const pending = state.pending;
|
|
95
|
+
if (pending.operation !== "observe" && pending.attempt < config.maxAttempts) {
|
|
96
|
+
issue(pending.operation, pending.attempt + 1);
|
|
97
|
+
return;
|
|
98
|
+
}
|
|
99
|
+
state.pending = null;
|
|
100
|
+
state.phase = "failed";
|
|
101
|
+
state.blockedGeneration = state.generation;
|
|
102
|
+
state.failure = { operation: pending.operation, reason, occurredAt: now.toISOString() };
|
|
103
|
+
trace("cover", "failed", reason, { operation: pending.operation, position: state.desired?.position ?? null });
|
|
104
|
+
}
|
|
105
|
+
function ready(position) {
|
|
106
|
+
state.desired.position = position;
|
|
107
|
+
state.pending = null;
|
|
108
|
+
state.failure = null;
|
|
109
|
+
state.blockedGeneration = null;
|
|
110
|
+
state.classification = null;
|
|
111
|
+
state.phase = "ready";
|
|
112
|
+
trace("observation", "ready", null, { position });
|
|
113
|
+
}
|
|
114
|
+
function acceptIntent(intent) {
|
|
115
|
+
if (typeof intent.requestId !== "string" || !intent.requestId)
|
|
116
|
+
throw new Error("cover requestId is required");
|
|
117
|
+
if (!Number.isInteger(intent.generation) || intent.generation < 1) {
|
|
118
|
+
throw new Error("cover generation must be a positive integer");
|
|
119
|
+
}
|
|
120
|
+
const position = intent.kind === "set_position"
|
|
121
|
+
? validPosition(intent.position, "cover position")
|
|
122
|
+
: intent.kind === "open" ? 100 : intent.kind === "close" ? 0 : null;
|
|
123
|
+
const requestedAt = intent.requestedAt || now.toISOString();
|
|
124
|
+
if (!Number.isFinite(Date.parse(requestedAt)))
|
|
125
|
+
throw new Error("cover requestedAt must be an ISO timestamp");
|
|
126
|
+
if (intent.generation < state.generation) {
|
|
127
|
+
trace("intent", "ignored", "stale-generation");
|
|
128
|
+
return;
|
|
129
|
+
}
|
|
130
|
+
if (intent.generation === state.generation && state.desired) {
|
|
131
|
+
const duplicate = state.desired.requestId === intent.requestId;
|
|
132
|
+
trace("intent", duplicate ? "duplicate" : "rejected", duplicate ? null : "generation-conflict");
|
|
133
|
+
return;
|
|
134
|
+
}
|
|
135
|
+
state.desired = {
|
|
136
|
+
requestId: intent.requestId,
|
|
137
|
+
generation: intent.generation,
|
|
138
|
+
operation: position === null ? "stop" : "set_position",
|
|
139
|
+
position,
|
|
140
|
+
source: typeof intent.source === "string" && intent.source ? intent.source : null,
|
|
141
|
+
requestedAt
|
|
142
|
+
};
|
|
143
|
+
state.generation = intent.generation;
|
|
144
|
+
state.failure = null;
|
|
145
|
+
state.blockedGeneration = null;
|
|
146
|
+
state.classification = null;
|
|
147
|
+
state.lastAttempt = 0;
|
|
148
|
+
issue(state.desired.operation, 1);
|
|
149
|
+
trace("intent", "accepted", null, { kind: intent.kind, position, generation: intent.generation });
|
|
150
|
+
}
|
|
151
|
+
function acceptObservation(observation) {
|
|
152
|
+
const devicePosition = validPosition(observation.position, "observed cover position");
|
|
153
|
+
if (observation.motion !== undefined && !Object.hasOwn(MOTIONS, observation.motion)) {
|
|
154
|
+
throw new Error("unknown observed cover motion");
|
|
155
|
+
}
|
|
156
|
+
const parsedAt = (0, observation_time_1.parseObservedAt)(observation.observedAt);
|
|
157
|
+
if (parsedAt === null || typeof observation.observedAt !== "string") {
|
|
158
|
+
throw new Error("cover observedAt must be an ISO timestamp");
|
|
159
|
+
}
|
|
160
|
+
const bounded = (0, observation_time_1.boundObservedAt)(observation.observedAt, parsedAt, nowMs);
|
|
161
|
+
if (bounded.skewed)
|
|
162
|
+
trace("observation", "adjusted", "clock-skew", { reported: observation.observedAt, usedAt: bounded.observedAt });
|
|
163
|
+
if ((0, observation_time_1.isOutOfOrder)(bounded.ms, state.observed?.observedAt, nowMs)) {
|
|
164
|
+
trace("observation", "ignored", "out-of-order");
|
|
165
|
+
return;
|
|
166
|
+
}
|
|
167
|
+
const observedAt = bounded.ms;
|
|
168
|
+
const position = config.invert ? 100 - devicePosition : devicePosition;
|
|
169
|
+
const motion = observation.motion ?? null;
|
|
170
|
+
const previous = state.observed;
|
|
171
|
+
state.observed = { position, motion, observedAt: bounded.observedAt };
|
|
172
|
+
if (!state.desired)
|
|
173
|
+
return;
|
|
174
|
+
const pending = state.pending;
|
|
175
|
+
const moving = motion === "opening" || motion === "closing";
|
|
176
|
+
if (pending) {
|
|
177
|
+
if (observedAt < pending.issuedAt) {
|
|
178
|
+
trace("observation", "ignored", "stale-observation");
|
|
179
|
+
return;
|
|
180
|
+
}
|
|
181
|
+
const changed = pending.stablePosition !== null && pending.stablePosition !== position;
|
|
182
|
+
if (pending.stablePosition !== position) {
|
|
183
|
+
pending.stablePosition = position;
|
|
184
|
+
pending.stableSince = observedAt;
|
|
185
|
+
}
|
|
186
|
+
// A duplicate report mid-travel is not a stop: without motion reports,
|
|
187
|
+
// the same position must hold for settleMs.
|
|
188
|
+
const settled = motion === "stopped"
|
|
189
|
+
|| (motion === null && observedAt - (pending.stableSince ?? observedAt) >= config.settleMs);
|
|
190
|
+
if (pending.operation === "stop" || (pending.operation === "observe" && state.desired.position === null)) {
|
|
191
|
+
if (settled) {
|
|
192
|
+
ready(position);
|
|
193
|
+
}
|
|
194
|
+
else {
|
|
195
|
+
trace("observation", "accepted", "still-moving", { position, motion });
|
|
196
|
+
}
|
|
197
|
+
return;
|
|
198
|
+
}
|
|
199
|
+
const target = state.desired.position;
|
|
200
|
+
const within = Math.abs(position - target) <= config.positionTolerance;
|
|
201
|
+
if (moving || changed
|
|
202
|
+
|| (pending.startPosition !== null && Math.abs(position - pending.startPosition) > config.positionTolerance)) {
|
|
203
|
+
pending.moved = true;
|
|
204
|
+
}
|
|
205
|
+
if (within && !moving) {
|
|
206
|
+
ready(target);
|
|
207
|
+
}
|
|
208
|
+
else if (pending.verifying && !moving) {
|
|
209
|
+
latchManual("moved-while-offline", target, position);
|
|
210
|
+
}
|
|
211
|
+
else if (settled && pending.operation === "observe") {
|
|
212
|
+
// Restart mid-travel and the cover has stopped off target: it answered,
|
|
213
|
+
// so this is not a confirmation timeout.
|
|
214
|
+
latchManual("stopped-while-offline", target, position);
|
|
215
|
+
}
|
|
216
|
+
else if (settled && pending.operation === "set_position" && pending.moved) {
|
|
217
|
+
// Stopped short after moving: a person or an obstruction stopped it.
|
|
218
|
+
// Never retry against that; a newer intent resumes automation.
|
|
219
|
+
latchManual("stopped-short", target, position);
|
|
220
|
+
}
|
|
221
|
+
else {
|
|
222
|
+
trace("observation", "accepted", "position-mismatch", { expected: target, observed: position, motion });
|
|
223
|
+
}
|
|
224
|
+
return;
|
|
225
|
+
}
|
|
226
|
+
if (state.desired.position === null)
|
|
227
|
+
return;
|
|
228
|
+
const within = Math.abs(position - state.desired.position) <= config.positionTolerance;
|
|
229
|
+
if (state.phase === "ready" && !within) {
|
|
230
|
+
latchManual(null, state.desired.position, position);
|
|
231
|
+
}
|
|
232
|
+
else if (state.phase === "manual_override" && within) {
|
|
233
|
+
// Settled back at this generation's target (or a stopped-short latch that
|
|
234
|
+
// was really still travelling): nothing to own, so the cover is ready
|
|
235
|
+
// again. A hand move passing through the target is not settled.
|
|
236
|
+
const settled = motion === "stopped" || (motion === null && previous !== null
|
|
237
|
+
&& previous.position === position && observedAt - Date.parse(previous.observedAt) >= config.settleMs);
|
|
238
|
+
if (settled)
|
|
239
|
+
ready(state.desired.position);
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
function latchManual(reason, expected, observed) {
|
|
243
|
+
state.pending = null;
|
|
244
|
+
state.classification = "manual_override";
|
|
245
|
+
state.phase = "manual_override";
|
|
246
|
+
trace("observation", "manual_override", reason, { expected, observed });
|
|
247
|
+
}
|
|
248
|
+
if (event.kind === "set_position" || event.kind === "open" || event.kind === "close" || event.kind === "stop") {
|
|
249
|
+
acceptIntent(event);
|
|
250
|
+
}
|
|
251
|
+
else if (event.kind === "observed") {
|
|
252
|
+
acceptObservation(event);
|
|
253
|
+
}
|
|
254
|
+
else if (event.kind === "command_result") {
|
|
255
|
+
if (!state.desired || event.generation !== state.generation || !state.pending) {
|
|
256
|
+
trace("command-result", "ignored", "stale-generation");
|
|
257
|
+
}
|
|
258
|
+
else if (event.ok === true) {
|
|
259
|
+
state.pending.nextAt = nowMs;
|
|
260
|
+
trace("command-result", "accepted");
|
|
261
|
+
}
|
|
262
|
+
else {
|
|
263
|
+
retryOrFail(typeof event.reason === "string" && event.reason ? event.reason : "command-failed");
|
|
264
|
+
}
|
|
265
|
+
}
|
|
266
|
+
else if (event.kind === "tick") {
|
|
267
|
+
if (!state.desired || event.generation !== state.generation || !state.pending) {
|
|
268
|
+
trace("tick", "ignored", "stale-generation");
|
|
269
|
+
}
|
|
270
|
+
else if (nowMs >= state.pending.deadlineAt) {
|
|
271
|
+
retryOrFail(state.pending.operation === "observe" ? "confirmation-timeout" : "travel-timeout");
|
|
272
|
+
}
|
|
273
|
+
else if (nowMs >= state.pending.nextAt) {
|
|
274
|
+
emit("observe", state.pending.attempt);
|
|
275
|
+
state.pending.nextAt = nowMs + config.pollMs;
|
|
276
|
+
}
|
|
277
|
+
}
|
|
278
|
+
else if (event.kind === "startup" || event.kind === "refresh") {
|
|
279
|
+
state.observed = null;
|
|
280
|
+
if (!state.desired) {
|
|
281
|
+
state.pending = null;
|
|
282
|
+
trace(event.kind, "idle");
|
|
283
|
+
}
|
|
284
|
+
else if (state.phase === "failed") {
|
|
285
|
+
state.pending = null;
|
|
286
|
+
trace(event.kind, "blocked", "failed");
|
|
287
|
+
}
|
|
288
|
+
else if (state.classification === "manual_override") {
|
|
289
|
+
state.pending = null;
|
|
290
|
+
emit("observe", 0);
|
|
291
|
+
trace(event.kind, "observing", "manual_override");
|
|
292
|
+
}
|
|
293
|
+
else {
|
|
294
|
+
const verifying = state.phase === "ready" && !state.pending;
|
|
295
|
+
issue("observe", 0, verifying);
|
|
296
|
+
trace(event.kind, "observing", null, { position: state.desired.position, verifying });
|
|
297
|
+
}
|
|
298
|
+
}
|
|
299
|
+
else {
|
|
300
|
+
throw new Error("unknown cover event kind");
|
|
301
|
+
}
|
|
302
|
+
if (state.desired) {
|
|
303
|
+
const theatre = {
|
|
304
|
+
kind: "target_state",
|
|
305
|
+
target: "cover",
|
|
306
|
+
requestId: state.desired.requestId,
|
|
307
|
+
generation: state.generation,
|
|
308
|
+
// Theatre target statuses: cover phases moving/stopping/manual_override
|
|
309
|
+
// are all "transitioning" there.
|
|
310
|
+
status: state.phase === "failed" || state.phase === "ready" || state.phase === "confirming"
|
|
311
|
+
? state.phase
|
|
312
|
+
: "transitioning",
|
|
313
|
+
observedAt: now.toISOString(),
|
|
314
|
+
details: {
|
|
315
|
+
operation: state.desired.operation,
|
|
316
|
+
position: state.desired.position,
|
|
317
|
+
observedPosition: state.observed?.position ?? null,
|
|
318
|
+
motion: state.observed?.motion ?? null,
|
|
319
|
+
source: state.desired.source,
|
|
320
|
+
recovery: state.observed ? { position: state.observed.position } : {},
|
|
321
|
+
metrics: {
|
|
322
|
+
attempts: state.lastAttempt,
|
|
323
|
+
retries: Math.max(0, state.lastAttempt - 1),
|
|
324
|
+
fallbackUsed: false
|
|
325
|
+
}
|
|
326
|
+
}
|
|
327
|
+
};
|
|
328
|
+
if (state.classification)
|
|
329
|
+
theatre.classification = state.classification;
|
|
330
|
+
if (state.failure)
|
|
331
|
+
theatre.failure = { ...state.failure };
|
|
332
|
+
events.push({ theatre });
|
|
333
|
+
}
|
|
334
|
+
return { state, commands, events, traces, status: statusFor(state) };
|
|
335
|
+
}
|
|
336
|
+
//# sourceMappingURL=cover-controller.js.map
|
|
@@ -0,0 +1,148 @@
|
|
|
1
|
+
export type DisplayMode = "off" | "tv" | "projector";
|
|
2
|
+
export type DisplayDevice = "tv" | "screen" | "projector";
|
|
3
|
+
export interface DisplayConfig {
|
|
4
|
+
screenOpenPosition: number;
|
|
5
|
+
screenTolerance: number;
|
|
6
|
+
pollMs: number;
|
|
7
|
+
queryTimeoutMs: number;
|
|
8
|
+
tvTimeoutMs: number;
|
|
9
|
+
screenTimeoutMs: number;
|
|
10
|
+
projectorWarmupMs: number;
|
|
11
|
+
projectorMinimumWarmupMs: number;
|
|
12
|
+
projectorShutdownConfirmMs: number;
|
|
13
|
+
projectorCooldownMs: number;
|
|
14
|
+
maxAttempts: number;
|
|
15
|
+
projectorOnWatts: number;
|
|
16
|
+
projectorOffWatts: number;
|
|
17
|
+
projectorPowerFallback?: boolean;
|
|
18
|
+
}
|
|
19
|
+
interface DisplayDesired {
|
|
20
|
+
requestId: string;
|
|
21
|
+
generation: number;
|
|
22
|
+
mode: DisplayMode;
|
|
23
|
+
}
|
|
24
|
+
interface DeviceObservation {
|
|
25
|
+
observedAt: string;
|
|
26
|
+
source: string;
|
|
27
|
+
}
|
|
28
|
+
interface TvObservation extends DeviceObservation {
|
|
29
|
+
power: "on" | "standby";
|
|
30
|
+
}
|
|
31
|
+
interface ScreenObservation extends DeviceObservation {
|
|
32
|
+
position: number;
|
|
33
|
+
state: string;
|
|
34
|
+
}
|
|
35
|
+
interface ProjectorObservation extends DeviceObservation {
|
|
36
|
+
watts: number;
|
|
37
|
+
power: "unknown" | "on" | "standby" | "transitioning";
|
|
38
|
+
}
|
|
39
|
+
interface DisplayObserved {
|
|
40
|
+
tv: TvObservation | null;
|
|
41
|
+
screen: ScreenObservation | null;
|
|
42
|
+
projector: ProjectorObservation | null;
|
|
43
|
+
}
|
|
44
|
+
export interface DisplayCommand {
|
|
45
|
+
device: DisplayDevice;
|
|
46
|
+
operation: string;
|
|
47
|
+
requestId: string | null;
|
|
48
|
+
generation: number;
|
|
49
|
+
attempt: number;
|
|
50
|
+
[key: string]: unknown;
|
|
51
|
+
}
|
|
52
|
+
interface DisplayPending {
|
|
53
|
+
device: DisplayDevice;
|
|
54
|
+
operation: string;
|
|
55
|
+
command: DisplayCommand;
|
|
56
|
+
attempt: number;
|
|
57
|
+
retryable: boolean;
|
|
58
|
+
deadlineAt: number;
|
|
59
|
+
nextAt: number;
|
|
60
|
+
readyAt?: number;
|
|
61
|
+
}
|
|
62
|
+
interface DisplayFailure {
|
|
63
|
+
device: DisplayDevice;
|
|
64
|
+
operation: string;
|
|
65
|
+
reason: string;
|
|
66
|
+
occurredAt: string;
|
|
67
|
+
}
|
|
68
|
+
export interface DisplayState {
|
|
69
|
+
desired: DisplayDesired | null;
|
|
70
|
+
generation: number;
|
|
71
|
+
phase: string;
|
|
72
|
+
observed: DisplayObserved;
|
|
73
|
+
seen: Record<DisplayDevice, boolean>;
|
|
74
|
+
querying: Record<DisplayDevice, boolean>;
|
|
75
|
+
pending: DisplayPending | null;
|
|
76
|
+
failure: DisplayFailure | null;
|
|
77
|
+
blockedGeneration: number | null;
|
|
78
|
+
classification: "manual_override" | null;
|
|
79
|
+
lastAttempt: number;
|
|
80
|
+
fallbackUsed: boolean;
|
|
81
|
+
}
|
|
82
|
+
export type DisplayInputEvent = {
|
|
83
|
+
kind: "startup";
|
|
84
|
+
} | {
|
|
85
|
+
kind: "intent";
|
|
86
|
+
requestId: string;
|
|
87
|
+
generation: number;
|
|
88
|
+
mode: DisplayMode;
|
|
89
|
+
} | {
|
|
90
|
+
kind: "observed";
|
|
91
|
+
device: DisplayDevice;
|
|
92
|
+
generation?: number;
|
|
93
|
+
observedAt?: string;
|
|
94
|
+
source?: string;
|
|
95
|
+
power?: string;
|
|
96
|
+
position?: number;
|
|
97
|
+
state?: string;
|
|
98
|
+
watts?: number;
|
|
99
|
+
} | {
|
|
100
|
+
kind: "transport_failed";
|
|
101
|
+
device: DisplayDevice;
|
|
102
|
+
operation: string;
|
|
103
|
+
generation?: number;
|
|
104
|
+
reason?: string;
|
|
105
|
+
} | {
|
|
106
|
+
kind: "tick";
|
|
107
|
+
generation?: number;
|
|
108
|
+
} | {
|
|
109
|
+
kind: "refresh";
|
|
110
|
+
};
|
|
111
|
+
export interface DisplayMessage {
|
|
112
|
+
display?: DisplayInputEvent;
|
|
113
|
+
}
|
|
114
|
+
export interface DisplayTargetState {
|
|
115
|
+
kind: "target_state";
|
|
116
|
+
target: "display";
|
|
117
|
+
requestId: string;
|
|
118
|
+
generation: number;
|
|
119
|
+
status: string;
|
|
120
|
+
observedAt: string;
|
|
121
|
+
details: Record<string, unknown>;
|
|
122
|
+
classification?: "manual_override";
|
|
123
|
+
failure?: DisplayFailure;
|
|
124
|
+
}
|
|
125
|
+
export interface DisplayOutputEvent {
|
|
126
|
+
display: Record<string, unknown>;
|
|
127
|
+
}
|
|
128
|
+
export declare function classifyProjectorPower(watts: number, previous?: ProjectorObservation["power"], config?: Partial<DisplayConfig>): ProjectorObservation["power"];
|
|
129
|
+
export declare function normaliseConfig(config?: Partial<DisplayConfig>): DisplayConfig;
|
|
130
|
+
export declare function transitionDisplay(saved: Partial<DisplayState> | undefined, msg: DisplayMessage, suppliedConfig?: Partial<DisplayConfig>, now?: Date): {
|
|
131
|
+
state: DisplayState;
|
|
132
|
+
commands: DisplayCommand[];
|
|
133
|
+
events: DisplayOutputEvent[];
|
|
134
|
+
targetStates: {
|
|
135
|
+
theatre: DisplayTargetState;
|
|
136
|
+
}[];
|
|
137
|
+
traces: {
|
|
138
|
+
event: string;
|
|
139
|
+
outcome: string;
|
|
140
|
+
reason: string | null;
|
|
141
|
+
details: Record<string, unknown>;
|
|
142
|
+
}[];
|
|
143
|
+
status: {
|
|
144
|
+
phase: string;
|
|
145
|
+
text: string;
|
|
146
|
+
};
|
|
147
|
+
};
|
|
148
|
+
export {};
|