@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,948 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.QUERY_COMMAND = void 0;
|
|
4
|
+
exports.createAvrState = createAvrState;
|
|
5
|
+
exports.formatVolumeCommand = formatVolumeCommand;
|
|
6
|
+
exports.parseAvrLine = parseAvrLine;
|
|
7
|
+
exports.normaliseAvrConfig = normaliseAvrConfig;
|
|
8
|
+
exports.transitionAvr = transitionAvr;
|
|
9
|
+
exports.QUERY_COMMAND = "PW?\rZM?\rSI?\rMV?\r";
|
|
10
|
+
function parseSources(value) {
|
|
11
|
+
const sources = value && typeof value === "object" && !Array.isArray(value)
|
|
12
|
+
? value
|
|
13
|
+
: {};
|
|
14
|
+
const entries = Object.entries(sources);
|
|
15
|
+
if (entries.some(([name, code]) => !name ||
|
|
16
|
+
!code || typeof code !== "string" ||
|
|
17
|
+
!/^[A-Z0-9][A-Z0-9 /._-]*$/.test(code))) {
|
|
18
|
+
throw new Error("sources must map names to Denon source codes");
|
|
19
|
+
}
|
|
20
|
+
const typedEntries = entries;
|
|
21
|
+
if (new Set(typedEntries.map(([, code]) => code)).size !== typedEntries.length) {
|
|
22
|
+
throw new Error("Denon source codes must be unique");
|
|
23
|
+
}
|
|
24
|
+
return Object.fromEntries(typedEntries);
|
|
25
|
+
}
|
|
26
|
+
function observedTime(value) {
|
|
27
|
+
return typeof value === "string" && Number.isFinite(Date.parse(value)) ? value : null;
|
|
28
|
+
}
|
|
29
|
+
function createAvrState(_config, saved = {}) {
|
|
30
|
+
const observed = saved.observed || {};
|
|
31
|
+
const seen = saved.seen || {};
|
|
32
|
+
const volumeRequest = saved.volumeRequest && typeof saved.volumeRequest === "object"
|
|
33
|
+
? saved.volumeRequest
|
|
34
|
+
: null;
|
|
35
|
+
const volumeMetrics = saved.volumeMetrics || {};
|
|
36
|
+
return {
|
|
37
|
+
observed: {
|
|
38
|
+
power: observed.power === "on" || observed.power === "standby" ? observed.power : null,
|
|
39
|
+
mainZone: observed.mainZone === "on" || observed.mainZone === "off" ? observed.mainZone : null,
|
|
40
|
+
source: typeof observed.source === "string" ? observed.source : null,
|
|
41
|
+
sourceCode: typeof observed.sourceCode === "string" ? observed.sourceCode : null,
|
|
42
|
+
volume: Number.isFinite(observed.volume) ? observed.volume : null,
|
|
43
|
+
at: typeof observed.at === "string" ? observed.at : null
|
|
44
|
+
},
|
|
45
|
+
fieldObservedAt: {
|
|
46
|
+
power: observedTime(saved.fieldObservedAt?.power),
|
|
47
|
+
mainZone: observedTime(saved.fieldObservedAt?.mainZone),
|
|
48
|
+
source: observedTime(saved.fieldObservedAt?.source),
|
|
49
|
+
volume: observedTime(saved.fieldObservedAt?.volume)
|
|
50
|
+
},
|
|
51
|
+
seen: {
|
|
52
|
+
power: seen.power === true,
|
|
53
|
+
mainZone: seen.mainZone === true,
|
|
54
|
+
source: seen.source === true,
|
|
55
|
+
volume: seen.volume === true
|
|
56
|
+
},
|
|
57
|
+
desired: saved.desired && typeof saved.desired === "object" ? { ...saved.desired } : null,
|
|
58
|
+
generation: Number.isInteger(saved.generation) && (saved.generation ?? -1) >= 0 ? saved.generation : 0,
|
|
59
|
+
pending: saved.pending && typeof saved.pending === "object" ? { ...saved.pending } : null,
|
|
60
|
+
phase: typeof saved.phase === "string" ? saved.phase : "unknown",
|
|
61
|
+
failure: saved.failure && typeof saved.failure === "object" ? { ...saved.failure } : null,
|
|
62
|
+
blockedGeneration: Number.isInteger(saved.blockedGeneration) ? saved.blockedGeneration : null,
|
|
63
|
+
querySentAt: Number.isFinite(saved.querySentAt) ? saved.querySentAt : null,
|
|
64
|
+
classification: saved.classification === "manual_override" ? "manual_override" : null,
|
|
65
|
+
volumeRequest: volumeRequest && typeof volumeRequest.requestId === "string" &&
|
|
66
|
+
(volumeRequest.mode === "absolute" || volumeRequest.mode === "relative")
|
|
67
|
+
? {
|
|
68
|
+
requestId: volumeRequest.requestId,
|
|
69
|
+
mode: volumeRequest.mode,
|
|
70
|
+
requested: Number(volumeRequest.requested) || 0,
|
|
71
|
+
target: Number.isFinite(volumeRequest.target) ? volumeRequest.target : null,
|
|
72
|
+
bounded: volumeRequest.bounded === true
|
|
73
|
+
}
|
|
74
|
+
: null,
|
|
75
|
+
recentVolumeRequestIds: Array.isArray(saved.recentVolumeRequestIds)
|
|
76
|
+
? saved.recentVolumeRequestIds.filter((value) => typeof value === "string").slice(-32)
|
|
77
|
+
: [],
|
|
78
|
+
volumeMetrics: {
|
|
79
|
+
intents: Number(volumeMetrics.intents) || 0,
|
|
80
|
+
commands: Number(volumeMetrics.commands) || 0,
|
|
81
|
+
bounded: Number(volumeMetrics.bounded) || 0,
|
|
82
|
+
confirmations: Number(volumeMetrics.confirmations) || 0,
|
|
83
|
+
externalChanges: Number(volumeMetrics.externalChanges) || 0,
|
|
84
|
+
failures: Number(volumeMetrics.failures) || 0
|
|
85
|
+
},
|
|
86
|
+
lastAttempt: Number.isInteger(saved.lastAttempt) && (saved.lastAttempt ?? -1) >= 0
|
|
87
|
+
? saved.lastAttempt
|
|
88
|
+
: 0
|
|
89
|
+
};
|
|
90
|
+
}
|
|
91
|
+
function parseVolume(raw) {
|
|
92
|
+
if (!/^\d{2,3}$/.test(raw))
|
|
93
|
+
return null;
|
|
94
|
+
if (raw.length === 2)
|
|
95
|
+
return Number(raw);
|
|
96
|
+
return Number(raw.slice(0, 2)) + Number(raw.slice(2)) / 10;
|
|
97
|
+
}
|
|
98
|
+
function roundVolume(value) {
|
|
99
|
+
return Math.round(value * 2) / 2;
|
|
100
|
+
}
|
|
101
|
+
function sameVolume(left, right) {
|
|
102
|
+
return left !== null && right !== null && Math.abs(left - right) < 0.001;
|
|
103
|
+
}
|
|
104
|
+
function formatVolumeCommand(value) {
|
|
105
|
+
const rounded = roundVolume(value);
|
|
106
|
+
if (!Number.isFinite(value) || rounded < 0 || rounded > 98 || Math.abs(value - rounded) > 0.001) {
|
|
107
|
+
throw new Error("volume must be a half-step between 0 and 98");
|
|
108
|
+
}
|
|
109
|
+
const whole = Math.floor(rounded).toString().padStart(2, "0");
|
|
110
|
+
return "MV" + whole + (Number.isInteger(rounded) ? "" : "5") + "\r";
|
|
111
|
+
}
|
|
112
|
+
function parseAvrLine(value, sources) {
|
|
113
|
+
const line = String(value ?? "").trim();
|
|
114
|
+
if (line === "PWON")
|
|
115
|
+
return { field: "power", value: "on", line };
|
|
116
|
+
if (line === "PWOFF" || line === "PWSTANDBY") {
|
|
117
|
+
return { field: "power", value: "standby", line };
|
|
118
|
+
}
|
|
119
|
+
if (line === "ZMON")
|
|
120
|
+
return { field: "mainZone", value: "on", line };
|
|
121
|
+
if (line === "ZMOFF")
|
|
122
|
+
return { field: "mainZone", value: "off", line };
|
|
123
|
+
if (/^SI.+/.test(line)) {
|
|
124
|
+
const sourceCode = line.slice(2);
|
|
125
|
+
const source = Object.entries(sources).find(([, code]) => code === sourceCode)?.[0] || null;
|
|
126
|
+
return { field: "source", value: source, sourceCode, line };
|
|
127
|
+
}
|
|
128
|
+
if (/^MV\d{2,3}$/.test(line)) {
|
|
129
|
+
return { field: "volume", value: parseVolume(line.slice(2)), line };
|
|
130
|
+
}
|
|
131
|
+
return null;
|
|
132
|
+
}
|
|
133
|
+
// Validates AVR configuration and fills its defaults. The Node-RED adapter
|
|
134
|
+
// calls this at deploy, so a bad configuration shows on the node instead of
|
|
135
|
+
// failing every message.
|
|
136
|
+
function normaliseAvrConfig(rawConfig) {
|
|
137
|
+
const rawMinVolume = Number(rawConfig.minVolume);
|
|
138
|
+
const rawMaxVolume = Number(rawConfig.maxVolume);
|
|
139
|
+
const minVolume = roundVolume(rawMinVolume);
|
|
140
|
+
const maxVolume = roundVolume(rawMaxVolume);
|
|
141
|
+
if (!Number.isFinite(rawMinVolume) || !Number.isFinite(rawMaxVolume) ||
|
|
142
|
+
Math.abs(rawMinVolume - minVolume) > 0.001 || Math.abs(rawMaxVolume - maxVolume) > 0.001 ||
|
|
143
|
+
minVolume < 0 || maxVolume > 98 || minVolume >= maxVolume) {
|
|
144
|
+
throw new Error("volume bounds must be half-step values between 0 and 98 with minimum below maximum");
|
|
145
|
+
}
|
|
146
|
+
return {
|
|
147
|
+
area: rawConfig.area || "room",
|
|
148
|
+
sources: parseSources(rawConfig.sources),
|
|
149
|
+
retryMs: Math.max(250, Number(rawConfig.retryMs) || 2000),
|
|
150
|
+
maxAttempts: Math.max(1, Math.floor(Number(rawConfig.maxAttempts) || 3)),
|
|
151
|
+
minVolume,
|
|
152
|
+
maxVolume
|
|
153
|
+
};
|
|
154
|
+
}
|
|
155
|
+
function transitionAvr(saved, msg, rawConfig, nowValue = new Date()) {
|
|
156
|
+
const config = normaliseAvrConfig(rawConfig);
|
|
157
|
+
const now = nowValue instanceof Date ? nowValue : new Date(nowValue);
|
|
158
|
+
const nowMs = now.getTime();
|
|
159
|
+
const state = createAvrState(config, saved);
|
|
160
|
+
const commands = [];
|
|
161
|
+
const events = [];
|
|
162
|
+
const targetStates = [];
|
|
163
|
+
const traces = [];
|
|
164
|
+
const event = msg.avr || { kind: "unknown" };
|
|
165
|
+
const previousPhase = state.phase;
|
|
166
|
+
// Fields the receiver reported in this event.
|
|
167
|
+
const reportedFields = new Set();
|
|
168
|
+
const before = JSON.stringify({
|
|
169
|
+
observed: state.observed,
|
|
170
|
+
seen: state.seen,
|
|
171
|
+
fieldObservedAt: state.fieldObservedAt,
|
|
172
|
+
desired: state.desired,
|
|
173
|
+
generation: state.generation,
|
|
174
|
+
pending: state.pending,
|
|
175
|
+
phase: state.phase,
|
|
176
|
+
failure: state.failure,
|
|
177
|
+
blockedGeneration: state.blockedGeneration,
|
|
178
|
+
classification: state.classification,
|
|
179
|
+
volumeRequest: state.volumeRequest,
|
|
180
|
+
recentVolumeRequestIds: state.recentVolumeRequestIds,
|
|
181
|
+
volumeMetrics: state.volumeMetrics,
|
|
182
|
+
lastAttempt: state.lastAttempt
|
|
183
|
+
});
|
|
184
|
+
let forceStateEvent = false;
|
|
185
|
+
function trace(name, outcome, reason = null, details = {}) {
|
|
186
|
+
traces.push({ event: name, outcome, reason, details });
|
|
187
|
+
}
|
|
188
|
+
function requiredFields() {
|
|
189
|
+
if (!state.desired)
|
|
190
|
+
return [];
|
|
191
|
+
if (state.desired.power === "standby")
|
|
192
|
+
return ["power"];
|
|
193
|
+
const fields = ["power", "mainZone"];
|
|
194
|
+
if (state.desired.source)
|
|
195
|
+
fields.push("source");
|
|
196
|
+
return fields;
|
|
197
|
+
}
|
|
198
|
+
function hasFreshTargetState() {
|
|
199
|
+
return requiredFields().every((field) => state.seen[field] === true);
|
|
200
|
+
}
|
|
201
|
+
function desiredSourceCode() {
|
|
202
|
+
return state.desired?.source ? config.sources[state.desired.source] : null;
|
|
203
|
+
}
|
|
204
|
+
function targetSatisfied() {
|
|
205
|
+
if (!state.desired || !hasFreshTargetState())
|
|
206
|
+
return false;
|
|
207
|
+
if (state.desired.power === "standby")
|
|
208
|
+
return state.observed.power === "standby";
|
|
209
|
+
return state.observed.power === "on" &&
|
|
210
|
+
state.observed.mainZone === "on" &&
|
|
211
|
+
(!state.desired.source || state.observed.sourceCode === desiredSourceCode());
|
|
212
|
+
}
|
|
213
|
+
function operationSatisfied(operation) {
|
|
214
|
+
if (operation === "query")
|
|
215
|
+
return hasFreshTargetState();
|
|
216
|
+
if (operation === "volume-query") {
|
|
217
|
+
return state.seen.power && state.seen.mainZone && state.seen.volume;
|
|
218
|
+
}
|
|
219
|
+
if (operation === "volume") {
|
|
220
|
+
return state.seen.volume &&
|
|
221
|
+
sameVolume(state.observed.volume, state.volumeRequest?.target ?? null);
|
|
222
|
+
}
|
|
223
|
+
if (operation === "power-on")
|
|
224
|
+
return state.seen.power && state.observed.power === "on";
|
|
225
|
+
if (operation === "main-zone-on")
|
|
226
|
+
return state.seen.mainZone && state.observed.mainZone === "on";
|
|
227
|
+
if (operation === "source") {
|
|
228
|
+
return state.seen.source && state.observed.sourceCode === desiredSourceCode();
|
|
229
|
+
}
|
|
230
|
+
if (operation === "standby")
|
|
231
|
+
return state.seen.power && state.observed.power === "standby";
|
|
232
|
+
return false;
|
|
233
|
+
}
|
|
234
|
+
function isVolumeOperation(operation) {
|
|
235
|
+
return operation === "volume" || operation === "volume-query";
|
|
236
|
+
}
|
|
237
|
+
function emitCommand(operation, payload, attempt) {
|
|
238
|
+
const volumeOperation = isVolumeOperation(operation);
|
|
239
|
+
commands.push({
|
|
240
|
+
payload,
|
|
241
|
+
avrCommand: {
|
|
242
|
+
area: config.area,
|
|
243
|
+
operation,
|
|
244
|
+
requestId: volumeOperation
|
|
245
|
+
? state.volumeRequest?.requestId || null
|
|
246
|
+
: state.desired?.requestId || null,
|
|
247
|
+
generation: state.desired?.generation ?? state.generation,
|
|
248
|
+
attempt
|
|
249
|
+
}
|
|
250
|
+
});
|
|
251
|
+
state.pending = {
|
|
252
|
+
operation,
|
|
253
|
+
payload,
|
|
254
|
+
attempt,
|
|
255
|
+
deadlineAt: nowMs + config.retryMs
|
|
256
|
+
};
|
|
257
|
+
state.phase = volumeOperation && blocked()
|
|
258
|
+
? "failed"
|
|
259
|
+
: operation === "query" || operation === "volume-query"
|
|
260
|
+
? "discovering"
|
|
261
|
+
: "transitioning";
|
|
262
|
+
if (operation === "volume")
|
|
263
|
+
state.volumeMetrics.commands += 1;
|
|
264
|
+
trace("command", "emitted", null, { operation, attempt });
|
|
265
|
+
state.lastAttempt = Math.max(state.lastAttempt, attempt);
|
|
266
|
+
}
|
|
267
|
+
function ready() {
|
|
268
|
+
const desired = state.desired;
|
|
269
|
+
if (!desired)
|
|
270
|
+
throw new Error("AVR cannot become ready without desired state");
|
|
271
|
+
const changed = state.phase !== "ready";
|
|
272
|
+
state.pending = null;
|
|
273
|
+
state.phase = "ready";
|
|
274
|
+
state.failure = null;
|
|
275
|
+
state.blockedGeneration = null;
|
|
276
|
+
if (changed) {
|
|
277
|
+
events.push({
|
|
278
|
+
avr: {
|
|
279
|
+
kind: "ready",
|
|
280
|
+
area: config.area,
|
|
281
|
+
requestId: desired.requestId,
|
|
282
|
+
generation: desired.generation
|
|
283
|
+
}
|
|
284
|
+
});
|
|
285
|
+
trace("request", "ready", null, {
|
|
286
|
+
requestId: desired.requestId,
|
|
287
|
+
generation: desired.generation
|
|
288
|
+
});
|
|
289
|
+
}
|
|
290
|
+
}
|
|
291
|
+
function clampVolume(value) {
|
|
292
|
+
const rounded = roundVolume(value);
|
|
293
|
+
const bounded = Math.max(config.minVolume, Math.min(config.maxVolume, rounded));
|
|
294
|
+
return { value: bounded, bounded: Math.abs(bounded - value) > 0.001 };
|
|
295
|
+
}
|
|
296
|
+
// The phase once no volume work is in flight; a failed request stays failed.
|
|
297
|
+
function blocked() {
|
|
298
|
+
return state.desired !== null && state.blockedGeneration === state.desired.generation;
|
|
299
|
+
}
|
|
300
|
+
function restingPhase() {
|
|
301
|
+
if (state.classification === "manual_override")
|
|
302
|
+
return "manual_override";
|
|
303
|
+
if (blocked())
|
|
304
|
+
return "failed";
|
|
305
|
+
return state.desired && targetSatisfied() ? "ready" : "idle";
|
|
306
|
+
}
|
|
307
|
+
// Every failed volume request is reported in one shape; operation and
|
|
308
|
+
// attempts are null when no command was involved (a restart).
|
|
309
|
+
function volumeFailed(requestId, reason, operation, attempts) {
|
|
310
|
+
state.volumeMetrics.failures += 1;
|
|
311
|
+
events.push({ avr: { kind: "volume_failed", area: config.area, requestId, reason, operation, attempts } });
|
|
312
|
+
trace("volume", "failed", reason, { requestId, operation, attempts });
|
|
313
|
+
}
|
|
314
|
+
function completeVolume() {
|
|
315
|
+
const request = state.volumeRequest;
|
|
316
|
+
if (!request || request.target === null)
|
|
317
|
+
return;
|
|
318
|
+
state.volumeMetrics.confirmations += 1;
|
|
319
|
+
if (state.pending && isVolumeOperation(state.pending.operation))
|
|
320
|
+
state.pending = null;
|
|
321
|
+
events.push({
|
|
322
|
+
avr: {
|
|
323
|
+
kind: "volume_ready",
|
|
324
|
+
area: config.area,
|
|
325
|
+
requestId: request.requestId,
|
|
326
|
+
volume: request.target,
|
|
327
|
+
bounded: request.bounded
|
|
328
|
+
}
|
|
329
|
+
});
|
|
330
|
+
trace("volume", "ready", null, {
|
|
331
|
+
requestId: request.requestId,
|
|
332
|
+
volume: request.target,
|
|
333
|
+
bounded: request.bounded
|
|
334
|
+
});
|
|
335
|
+
state.volumeRequest = null;
|
|
336
|
+
state.phase = restingPhase();
|
|
337
|
+
}
|
|
338
|
+
function abandonVolume(reason, observed = state.observed.volume) {
|
|
339
|
+
const request = state.volumeRequest;
|
|
340
|
+
if (!request)
|
|
341
|
+
return;
|
|
342
|
+
if (state.pending && isVolumeOperation(state.pending.operation))
|
|
343
|
+
state.pending = null;
|
|
344
|
+
events.push({
|
|
345
|
+
avr: {
|
|
346
|
+
kind: "volume_manual_override",
|
|
347
|
+
area: config.area,
|
|
348
|
+
requestId: request.requestId,
|
|
349
|
+
reason,
|
|
350
|
+
observed,
|
|
351
|
+
target: request.target
|
|
352
|
+
}
|
|
353
|
+
});
|
|
354
|
+
trace("volume", "manual_override", reason, {
|
|
355
|
+
requestId: request.requestId,
|
|
356
|
+
observed,
|
|
357
|
+
target: request.target
|
|
358
|
+
});
|
|
359
|
+
state.volumeRequest = null;
|
|
360
|
+
state.phase = restingPhase();
|
|
361
|
+
}
|
|
362
|
+
function resolveRelativeVolume() {
|
|
363
|
+
const request = state.volumeRequest;
|
|
364
|
+
if (!request || request.mode !== "relative" || request.target !== null ||
|
|
365
|
+
!state.seen.volume || state.observed.volume === null)
|
|
366
|
+
return;
|
|
367
|
+
const resolved = clampVolume(state.observed.volume + request.requested);
|
|
368
|
+
request.target = resolved.value;
|
|
369
|
+
request.bounded ||= resolved.bounded;
|
|
370
|
+
if (resolved.bounded)
|
|
371
|
+
state.volumeMetrics.bounded += 1;
|
|
372
|
+
}
|
|
373
|
+
function reconcileVolume(allowQuery) {
|
|
374
|
+
if (!state.volumeRequest)
|
|
375
|
+
return false;
|
|
376
|
+
if (state.pending)
|
|
377
|
+
return true;
|
|
378
|
+
if (!state.seen.power || !state.seen.mainZone || !state.seen.volume) {
|
|
379
|
+
if (allowQuery)
|
|
380
|
+
emitCommand("volume-query", exports.QUERY_COMMAND, 1);
|
|
381
|
+
return true;
|
|
382
|
+
}
|
|
383
|
+
if (state.observed.power !== "on" || state.observed.mainZone !== "on") {
|
|
384
|
+
const request = state.volumeRequest;
|
|
385
|
+
events.push({
|
|
386
|
+
avr: {
|
|
387
|
+
kind: "volume_rejected",
|
|
388
|
+
area: config.area,
|
|
389
|
+
requestId: request.requestId,
|
|
390
|
+
reason: "receiver-not-ready"
|
|
391
|
+
}
|
|
392
|
+
});
|
|
393
|
+
trace("volume", "rejected", "receiver-not-ready", {
|
|
394
|
+
requestId: request.requestId
|
|
395
|
+
});
|
|
396
|
+
state.volumeRequest = null;
|
|
397
|
+
return false;
|
|
398
|
+
}
|
|
399
|
+
resolveRelativeVolume();
|
|
400
|
+
if (state.volumeRequest.target !== null &&
|
|
401
|
+
sameVolume(state.observed.volume, state.volumeRequest.target)) {
|
|
402
|
+
completeVolume();
|
|
403
|
+
return false;
|
|
404
|
+
}
|
|
405
|
+
if (state.volumeRequest.target === null)
|
|
406
|
+
return true;
|
|
407
|
+
emitCommand("volume", formatVolumeCommand(state.volumeRequest.target), 1);
|
|
408
|
+
return true;
|
|
409
|
+
}
|
|
410
|
+
function reconcile(allowQuery = true) {
|
|
411
|
+
if (state.pending && operationSatisfied(state.pending.operation)) {
|
|
412
|
+
if (state.pending.operation === "volume")
|
|
413
|
+
completeVolume();
|
|
414
|
+
else
|
|
415
|
+
state.pending = null;
|
|
416
|
+
}
|
|
417
|
+
if (state.pending)
|
|
418
|
+
return;
|
|
419
|
+
if (state.desired && state.classification !== "manual_override" &&
|
|
420
|
+
state.phase === "ready" && !state.volumeRequest &&
|
|
421
|
+
hasFreshTargetState() && !targetSatisfied()) {
|
|
422
|
+
state.classification = "manual_override";
|
|
423
|
+
state.phase = "manual_override";
|
|
424
|
+
events.push({
|
|
425
|
+
avr: {
|
|
426
|
+
kind: "manual_override",
|
|
427
|
+
area: config.area,
|
|
428
|
+
requestId: state.desired.requestId,
|
|
429
|
+
generation: state.desired.generation,
|
|
430
|
+
observed: { ...state.observed }
|
|
431
|
+
}
|
|
432
|
+
});
|
|
433
|
+
trace("observation", "manual_override");
|
|
434
|
+
}
|
|
435
|
+
if (state.desired && state.classification !== "manual_override" && !targetSatisfied()) {
|
|
436
|
+
// A failed request still answers volume: sent if the receiver is on,
|
|
437
|
+
// rejected otherwise.
|
|
438
|
+
if (blocked()) {
|
|
439
|
+
reconcileVolume(allowQuery);
|
|
440
|
+
return;
|
|
441
|
+
}
|
|
442
|
+
if (!hasFreshTargetState()) {
|
|
443
|
+
state.phase = "discovering";
|
|
444
|
+
if (allowQuery)
|
|
445
|
+
emitCommand("query", exports.QUERY_COMMAND, 1);
|
|
446
|
+
return;
|
|
447
|
+
}
|
|
448
|
+
if (state.desired.power === "standby") {
|
|
449
|
+
emitCommand("standby", "PWSTANDBY\r", 1);
|
|
450
|
+
}
|
|
451
|
+
else if (state.observed.power !== "on") {
|
|
452
|
+
emitCommand("power-on", "PWON\r", 1);
|
|
453
|
+
}
|
|
454
|
+
else if (state.observed.mainZone !== "on") {
|
|
455
|
+
emitCommand("main-zone-on", "ZMON\r", 1);
|
|
456
|
+
}
|
|
457
|
+
else if (state.desired.source && state.observed.sourceCode !== desiredSourceCode()) {
|
|
458
|
+
emitCommand("source", "SI" + desiredSourceCode() + "\r", 1);
|
|
459
|
+
}
|
|
460
|
+
return;
|
|
461
|
+
}
|
|
462
|
+
if (reconcileVolume(allowQuery))
|
|
463
|
+
return;
|
|
464
|
+
if (state.desired) {
|
|
465
|
+
if (state.classification === "manual_override") {
|
|
466
|
+
state.phase = "manual_override";
|
|
467
|
+
}
|
|
468
|
+
else {
|
|
469
|
+
ready();
|
|
470
|
+
}
|
|
471
|
+
}
|
|
472
|
+
else {
|
|
473
|
+
state.phase = Object.values(state.seen).some(Boolean) ? "idle" : "discovering";
|
|
474
|
+
}
|
|
475
|
+
}
|
|
476
|
+
function validateIntent(intent) {
|
|
477
|
+
if (!intent.requestId || typeof intent.requestId !== "string")
|
|
478
|
+
return "missing-request-id";
|
|
479
|
+
if (!Number.isInteger(intent.generation) || intent.generation < 1)
|
|
480
|
+
return "invalid-generation";
|
|
481
|
+
if (intent.power !== "on" && intent.power !== "standby")
|
|
482
|
+
return "invalid-power";
|
|
483
|
+
if (intent.power === "standby" && intent.source)
|
|
484
|
+
return "source-with-standby";
|
|
485
|
+
if (intent.source && !Object.hasOwn(config.sources, intent.source))
|
|
486
|
+
return "unknown-source";
|
|
487
|
+
return null;
|
|
488
|
+
}
|
|
489
|
+
function validateVolumeIntent(intent) {
|
|
490
|
+
if (!intent.requestId || typeof intent.requestId !== "string")
|
|
491
|
+
return "missing-request-id";
|
|
492
|
+
if (intent.mode !== "absolute" && intent.mode !== "relative")
|
|
493
|
+
return "invalid-volume-mode";
|
|
494
|
+
if (!Number.isFinite(intent.value))
|
|
495
|
+
return "invalid-volume-value";
|
|
496
|
+
if (intent.mode === "relative" && intent.value === 0)
|
|
497
|
+
return "zero-volume-delta";
|
|
498
|
+
return null;
|
|
499
|
+
}
|
|
500
|
+
if (event.kind === "refresh") {
|
|
501
|
+
// Re-read the receiver without touching work in flight: desired state,
|
|
502
|
+
// pending commands, the volume request and the phase all carry on.
|
|
503
|
+
commands.push({
|
|
504
|
+
payload: exports.QUERY_COMMAND,
|
|
505
|
+
avrCommand: {
|
|
506
|
+
area: config.area,
|
|
507
|
+
operation: "query",
|
|
508
|
+
requestId: state.desired?.requestId || null,
|
|
509
|
+
generation: state.desired?.generation ?? state.generation,
|
|
510
|
+
attempt: 1
|
|
511
|
+
}
|
|
512
|
+
});
|
|
513
|
+
trace("refresh", "querying", null, { pending: state.pending?.operation ?? null });
|
|
514
|
+
forceStateEvent = true;
|
|
515
|
+
}
|
|
516
|
+
else if (event.kind === "startup") {
|
|
517
|
+
const requestBlocked = state.desired &&
|
|
518
|
+
state.blockedGeneration === state.desired.generation;
|
|
519
|
+
const abandonedVolumeRequest = state.volumeRequest?.requestId || null;
|
|
520
|
+
// Not replayed after a restart: the caller hears it failed.
|
|
521
|
+
if (abandonedVolumeRequest)
|
|
522
|
+
volumeFailed(abandonedVolumeRequest, "restart", null, null);
|
|
523
|
+
state.seen = { power: false, mainZone: false, source: false, volume: false };
|
|
524
|
+
state.pending = null;
|
|
525
|
+
state.volumeRequest = null;
|
|
526
|
+
state.phase = requestBlocked ? "failed" : "discovering";
|
|
527
|
+
state.querySentAt = nowMs;
|
|
528
|
+
commands.push({
|
|
529
|
+
payload: exports.QUERY_COMMAND,
|
|
530
|
+
avrCommand: {
|
|
531
|
+
area: config.area,
|
|
532
|
+
operation: "query",
|
|
533
|
+
requestId: state.desired?.requestId || null,
|
|
534
|
+
generation: state.desired?.generation ?? state.generation,
|
|
535
|
+
attempt: 1
|
|
536
|
+
}
|
|
537
|
+
});
|
|
538
|
+
state.lastAttempt = Math.max(state.lastAttempt, 1);
|
|
539
|
+
if (state.desired && !requestBlocked && state.classification !== "manual_override") {
|
|
540
|
+
state.pending = {
|
|
541
|
+
operation: "query",
|
|
542
|
+
payload: exports.QUERY_COMMAND,
|
|
543
|
+
attempt: 1,
|
|
544
|
+
deadlineAt: nowMs + config.retryMs
|
|
545
|
+
};
|
|
546
|
+
}
|
|
547
|
+
trace(event.kind, "querying", requestBlocked ? "request-remains-blocked" : null, {
|
|
548
|
+
abandonedVolumeRequest
|
|
549
|
+
});
|
|
550
|
+
forceStateEvent = true;
|
|
551
|
+
}
|
|
552
|
+
else if (event.kind === "observed") {
|
|
553
|
+
const value = event.line ?? msg.payload;
|
|
554
|
+
const lines = String(value ?? "").split(/[\r\n]+/).filter(Boolean);
|
|
555
|
+
let recognised = 0;
|
|
556
|
+
for (const line of lines) {
|
|
557
|
+
const parsed = parseAvrLine(line, config.sources);
|
|
558
|
+
if (!parsed)
|
|
559
|
+
continue;
|
|
560
|
+
recognised += 1;
|
|
561
|
+
let previous;
|
|
562
|
+
let current;
|
|
563
|
+
const wasFresh = state.seen[parsed.field];
|
|
564
|
+
state.seen[parsed.field] = true;
|
|
565
|
+
state.fieldObservedAt[parsed.field] = now.toISOString();
|
|
566
|
+
reportedFields.add(parsed.field);
|
|
567
|
+
if (parsed.field === "source") {
|
|
568
|
+
previous = state.observed.sourceCode;
|
|
569
|
+
state.observed.source = parsed.value;
|
|
570
|
+
state.observed.sourceCode = parsed.sourceCode;
|
|
571
|
+
current = parsed.sourceCode;
|
|
572
|
+
}
|
|
573
|
+
else if (parsed.field === "power") {
|
|
574
|
+
previous = state.observed.power;
|
|
575
|
+
state.observed.power = parsed.value;
|
|
576
|
+
current = parsed.value;
|
|
577
|
+
}
|
|
578
|
+
else if (parsed.field === "mainZone") {
|
|
579
|
+
previous = state.observed.mainZone;
|
|
580
|
+
state.observed.mainZone = parsed.value;
|
|
581
|
+
current = parsed.value;
|
|
582
|
+
}
|
|
583
|
+
else {
|
|
584
|
+
previous = state.observed.volume;
|
|
585
|
+
state.observed.volume = parsed.value;
|
|
586
|
+
current = parsed.value;
|
|
587
|
+
if (wasFresh && previous !== current) {
|
|
588
|
+
if (state.volumeRequest && state.pending?.operation === "volume" &&
|
|
589
|
+
!sameVolume(parsed.value, state.volumeRequest.target)) {
|
|
590
|
+
state.volumeMetrics.externalChanges += 1;
|
|
591
|
+
abandonVolume("unexpected-observation", parsed.value);
|
|
592
|
+
}
|
|
593
|
+
else if (!state.volumeRequest) {
|
|
594
|
+
state.volumeMetrics.externalChanges += 1;
|
|
595
|
+
events.push({
|
|
596
|
+
avr: {
|
|
597
|
+
kind: "volume_external",
|
|
598
|
+
area: config.area,
|
|
599
|
+
previous,
|
|
600
|
+
observed: parsed.value
|
|
601
|
+
}
|
|
602
|
+
});
|
|
603
|
+
trace("volume", "external", null, { previous, observed: parsed.value });
|
|
604
|
+
}
|
|
605
|
+
else if (state.volumeRequest.mode === "relative" &&
|
|
606
|
+
state.pending?.operation !== "volume") {
|
|
607
|
+
state.volumeRequest.target = null;
|
|
608
|
+
}
|
|
609
|
+
}
|
|
610
|
+
}
|
|
611
|
+
state.observed.at = now.toISOString();
|
|
612
|
+
trace("observed", previous === current ? "unchanged" : "changed", null, {
|
|
613
|
+
field: parsed.field,
|
|
614
|
+
value: current
|
|
615
|
+
});
|
|
616
|
+
}
|
|
617
|
+
if (recognised) {
|
|
618
|
+
reconcile();
|
|
619
|
+
forceStateEvent = true;
|
|
620
|
+
}
|
|
621
|
+
}
|
|
622
|
+
else if (event.kind === "intent") {
|
|
623
|
+
const reason = validateIntent(event);
|
|
624
|
+
if (reason) {
|
|
625
|
+
events.push({ avr: { kind: "rejected", area: config.area, reason } });
|
|
626
|
+
trace("intent", "rejected", reason);
|
|
627
|
+
}
|
|
628
|
+
else if (event.generation < state.generation) {
|
|
629
|
+
events.push({
|
|
630
|
+
avr: {
|
|
631
|
+
kind: "rejected",
|
|
632
|
+
area: config.area,
|
|
633
|
+
requestId: event.requestId,
|
|
634
|
+
generation: event.generation,
|
|
635
|
+
reason: "stale-generation"
|
|
636
|
+
}
|
|
637
|
+
});
|
|
638
|
+
trace("intent", "rejected", "stale-generation", {
|
|
639
|
+
generation: event.generation,
|
|
640
|
+
currentGeneration: state.generation
|
|
641
|
+
});
|
|
642
|
+
}
|
|
643
|
+
else if (event.generation === state.generation && state.desired &&
|
|
644
|
+
(state.desired.requestId !== event.requestId ||
|
|
645
|
+
state.desired.power !== event.power ||
|
|
646
|
+
state.desired.source !== (event.source || null))) {
|
|
647
|
+
const conflict = state.desired.requestId === event.requestId
|
|
648
|
+
? "request-conflict"
|
|
649
|
+
: "generation-conflict";
|
|
650
|
+
events.push({
|
|
651
|
+
avr: {
|
|
652
|
+
kind: "rejected",
|
|
653
|
+
area: config.area,
|
|
654
|
+
requestId: event.requestId,
|
|
655
|
+
generation: event.generation,
|
|
656
|
+
reason: conflict
|
|
657
|
+
}
|
|
658
|
+
});
|
|
659
|
+
trace("intent", "rejected", conflict);
|
|
660
|
+
}
|
|
661
|
+
else if (event.generation === state.generation &&
|
|
662
|
+
state.desired && state.desired.requestId === event.requestId) {
|
|
663
|
+
trace("intent", "duplicate", null, { requestId: event.requestId, generation: event.generation });
|
|
664
|
+
reconcile();
|
|
665
|
+
}
|
|
666
|
+
else {
|
|
667
|
+
if (state.desired) {
|
|
668
|
+
events.push({
|
|
669
|
+
avr: {
|
|
670
|
+
kind: "superseded",
|
|
671
|
+
area: config.area,
|
|
672
|
+
requestId: state.desired.requestId,
|
|
673
|
+
generation: state.desired.generation,
|
|
674
|
+
supersededBy: event.requestId
|
|
675
|
+
}
|
|
676
|
+
});
|
|
677
|
+
}
|
|
678
|
+
state.generation = event.generation;
|
|
679
|
+
state.desired = {
|
|
680
|
+
requestId: event.requestId,
|
|
681
|
+
generation: event.generation,
|
|
682
|
+
power: event.power,
|
|
683
|
+
source: event.source || null
|
|
684
|
+
};
|
|
685
|
+
state.pending = null;
|
|
686
|
+
state.failure = null;
|
|
687
|
+
state.blockedGeneration = null;
|
|
688
|
+
state.classification = null;
|
|
689
|
+
state.lastAttempt = 0;
|
|
690
|
+
// Ready belonged to the previous request; left as it was, the drift
|
|
691
|
+
// check would read this request's unmet target as a manual change.
|
|
692
|
+
if (state.phase === "ready" || state.phase === "manual_override")
|
|
693
|
+
state.phase = "idle";
|
|
694
|
+
trace("intent", "accepted", null, {
|
|
695
|
+
requestId: event.requestId,
|
|
696
|
+
generation: event.generation,
|
|
697
|
+
power: event.power,
|
|
698
|
+
source: event.source || null
|
|
699
|
+
});
|
|
700
|
+
reconcile();
|
|
701
|
+
}
|
|
702
|
+
forceStateEvent = true;
|
|
703
|
+
}
|
|
704
|
+
else if (event.kind === "volume") {
|
|
705
|
+
const reason = validateVolumeIntent(event);
|
|
706
|
+
if (reason) {
|
|
707
|
+
events.push({
|
|
708
|
+
avr: {
|
|
709
|
+
kind: "volume_rejected",
|
|
710
|
+
area: config.area,
|
|
711
|
+
requestId: event.requestId,
|
|
712
|
+
reason
|
|
713
|
+
}
|
|
714
|
+
});
|
|
715
|
+
trace("volume", "rejected", reason, { requestId: event.requestId });
|
|
716
|
+
}
|
|
717
|
+
else if (state.recentVolumeRequestIds.includes(event.requestId)) {
|
|
718
|
+
trace("volume", "duplicate", null, { requestId: event.requestId });
|
|
719
|
+
reconcile();
|
|
720
|
+
}
|
|
721
|
+
else {
|
|
722
|
+
const existing = state.volumeRequest;
|
|
723
|
+
let mode = event.mode;
|
|
724
|
+
let requested = event.value;
|
|
725
|
+
let target = null;
|
|
726
|
+
let bounded = false;
|
|
727
|
+
let boundedThisIntent = false;
|
|
728
|
+
if (event.mode === "absolute") {
|
|
729
|
+
const resolved = clampVolume(event.value);
|
|
730
|
+
target = resolved.value;
|
|
731
|
+
bounded = resolved.bounded;
|
|
732
|
+
boundedThisIntent = resolved.bounded;
|
|
733
|
+
}
|
|
734
|
+
else if (existing?.target !== null && existing?.target !== undefined) {
|
|
735
|
+
const unresolvedTarget = existing.target + event.value;
|
|
736
|
+
const resolved = clampVolume(unresolvedTarget);
|
|
737
|
+
mode = existing.mode;
|
|
738
|
+
requested = existing.mode === "relative"
|
|
739
|
+
? existing.requested + event.value
|
|
740
|
+
: unresolvedTarget;
|
|
741
|
+
target = resolved.value;
|
|
742
|
+
bounded = existing.bounded || resolved.bounded;
|
|
743
|
+
boundedThisIntent = resolved.bounded;
|
|
744
|
+
}
|
|
745
|
+
else if (existing?.mode === "relative") {
|
|
746
|
+
requested = existing.requested + event.value;
|
|
747
|
+
}
|
|
748
|
+
else if (state.seen.volume && state.observed.volume !== null) {
|
|
749
|
+
const resolved = clampVolume(state.observed.volume + event.value);
|
|
750
|
+
target = resolved.value;
|
|
751
|
+
bounded = resolved.bounded;
|
|
752
|
+
boundedThisIntent = resolved.bounded;
|
|
753
|
+
}
|
|
754
|
+
state.volumeRequest = {
|
|
755
|
+
requestId: event.requestId,
|
|
756
|
+
mode,
|
|
757
|
+
requested,
|
|
758
|
+
target,
|
|
759
|
+
bounded
|
|
760
|
+
};
|
|
761
|
+
state.recentVolumeRequestIds = [...state.recentVolumeRequestIds, event.requestId].slice(-32);
|
|
762
|
+
state.volumeMetrics.intents += 1;
|
|
763
|
+
if (boundedThisIntent)
|
|
764
|
+
state.volumeMetrics.bounded += 1;
|
|
765
|
+
if (state.pending?.operation === "volume") {
|
|
766
|
+
state.pending = null;
|
|
767
|
+
// The last reading predates the command in flight, so it cannot
|
|
768
|
+
// confirm the new target: re-read before deciding.
|
|
769
|
+
if (target !== null && sameVolume(state.observed.volume, target))
|
|
770
|
+
state.seen.volume = false;
|
|
771
|
+
}
|
|
772
|
+
trace("volume", "accepted", null, {
|
|
773
|
+
requestId: event.requestId,
|
|
774
|
+
mode: event.mode,
|
|
775
|
+
value: event.value,
|
|
776
|
+
target,
|
|
777
|
+
bounded
|
|
778
|
+
});
|
|
779
|
+
reconcile();
|
|
780
|
+
}
|
|
781
|
+
forceStateEvent = true;
|
|
782
|
+
}
|
|
783
|
+
else if (event.kind === "tick") {
|
|
784
|
+
if (!state.pending) {
|
|
785
|
+
reconcile();
|
|
786
|
+
}
|
|
787
|
+
else if (nowMs >= state.pending.deadlineAt) {
|
|
788
|
+
if (operationSatisfied(state.pending.operation)) {
|
|
789
|
+
if (state.pending.operation === "volume")
|
|
790
|
+
completeVolume();
|
|
791
|
+
else
|
|
792
|
+
state.pending = null;
|
|
793
|
+
reconcile();
|
|
794
|
+
}
|
|
795
|
+
else if (state.pending.attempt >= config.maxAttempts) {
|
|
796
|
+
const failed = state.pending;
|
|
797
|
+
state.pending = null;
|
|
798
|
+
if (isVolumeOperation(failed.operation)) {
|
|
799
|
+
const requestId = state.volumeRequest?.requestId || null;
|
|
800
|
+
state.volumeRequest = null;
|
|
801
|
+
state.phase = restingPhase();
|
|
802
|
+
volumeFailed(requestId, "attempts-exhausted", failed.operation, failed.attempt);
|
|
803
|
+
reconcile(false);
|
|
804
|
+
}
|
|
805
|
+
else {
|
|
806
|
+
state.phase = "failed";
|
|
807
|
+
state.failure = {
|
|
808
|
+
operation: failed.operation,
|
|
809
|
+
attempts: failed.attempt,
|
|
810
|
+
at: now.toISOString()
|
|
811
|
+
};
|
|
812
|
+
state.blockedGeneration = state.desired?.generation ?? null;
|
|
813
|
+
events.push({
|
|
814
|
+
avr: {
|
|
815
|
+
kind: "failed",
|
|
816
|
+
area: config.area,
|
|
817
|
+
requestId: state.desired?.requestId || null,
|
|
818
|
+
generation: state.desired?.generation ?? state.generation,
|
|
819
|
+
operation: failed.operation,
|
|
820
|
+
attempts: failed.attempt
|
|
821
|
+
}
|
|
822
|
+
});
|
|
823
|
+
trace("request", "failed", "attempts-exhausted", {
|
|
824
|
+
operation: failed.operation,
|
|
825
|
+
attempts: failed.attempt
|
|
826
|
+
});
|
|
827
|
+
}
|
|
828
|
+
}
|
|
829
|
+
else {
|
|
830
|
+
emitCommand(state.pending.operation, state.pending.payload, state.pending.attempt + 1);
|
|
831
|
+
}
|
|
832
|
+
}
|
|
833
|
+
forceStateEvent = true;
|
|
834
|
+
}
|
|
835
|
+
const after = JSON.stringify({
|
|
836
|
+
observed: state.observed,
|
|
837
|
+
seen: state.seen,
|
|
838
|
+
fieldObservedAt: state.fieldObservedAt,
|
|
839
|
+
desired: state.desired,
|
|
840
|
+
generation: state.generation,
|
|
841
|
+
pending: state.pending,
|
|
842
|
+
phase: state.phase,
|
|
843
|
+
failure: state.failure,
|
|
844
|
+
blockedGeneration: state.blockedGeneration,
|
|
845
|
+
classification: state.classification,
|
|
846
|
+
volumeRequest: state.volumeRequest,
|
|
847
|
+
recentVolumeRequestIds: state.recentVolumeRequestIds,
|
|
848
|
+
volumeMetrics: state.volumeMetrics,
|
|
849
|
+
lastAttempt: state.lastAttempt
|
|
850
|
+
});
|
|
851
|
+
if (forceStateEvent || before !== after) {
|
|
852
|
+
const payload = {
|
|
853
|
+
area: config.area,
|
|
854
|
+
phase: state.phase,
|
|
855
|
+
generation: state.generation,
|
|
856
|
+
observed: { ...state.observed },
|
|
857
|
+
fresh: { ...state.seen },
|
|
858
|
+
desired: state.desired ? { ...state.desired } : null,
|
|
859
|
+
pending: state.pending ? {
|
|
860
|
+
operation: state.pending.operation,
|
|
861
|
+
attempt: state.pending.attempt,
|
|
862
|
+
deadlineAt: state.pending.deadlineAt
|
|
863
|
+
} : null,
|
|
864
|
+
failure: state.failure,
|
|
865
|
+
classification: state.classification,
|
|
866
|
+
volume: {
|
|
867
|
+
request: state.volumeRequest ? { ...state.volumeRequest } : null,
|
|
868
|
+
bounds: { minimum: config.minVolume, maximum: config.maxVolume },
|
|
869
|
+
metrics: { ...state.volumeMetrics }
|
|
870
|
+
},
|
|
871
|
+
metrics: {
|
|
872
|
+
attempts: state.lastAttempt,
|
|
873
|
+
retries: Math.max(0, state.lastAttempt - 1),
|
|
874
|
+
fallbackUsed: false,
|
|
875
|
+
volume: { ...state.volumeMetrics }
|
|
876
|
+
}
|
|
877
|
+
};
|
|
878
|
+
events.unshift({ payload, avr: { kind: "state", ...payload } });
|
|
879
|
+
}
|
|
880
|
+
const source = state.observed.source || state.observed.sourceCode || "unknown";
|
|
881
|
+
const status = state.phase === "failed"
|
|
882
|
+
? { phase: "failed", text: "failed · " + (state.failure?.operation || "unknown") }
|
|
883
|
+
: state.phase === "transitioning"
|
|
884
|
+
? {
|
|
885
|
+
phase: "transitioning",
|
|
886
|
+
text: (state.pending?.operation || "unknown") + " · " +
|
|
887
|
+
(state.pending?.attempt || 0) + "/" + config.maxAttempts
|
|
888
|
+
}
|
|
889
|
+
: state.phase === "manual_override"
|
|
890
|
+
? { phase: "manual_override", text: "manual control" }
|
|
891
|
+
: state.phase === "discovering"
|
|
892
|
+
? { phase: "discovering", text: "discovering" }
|
|
893
|
+
: state.phase === "ready"
|
|
894
|
+
? { phase: "ready", text: (state.observed.power || "unknown") + " · " + source }
|
|
895
|
+
: { phase: "idle", text: (state.observed.power || "unknown") + " · " + source };
|
|
896
|
+
// The theatre activity controller's view of this target. Ready is stamped
|
|
897
|
+
// with the oldest report among the fields the request needs, so it is only
|
|
898
|
+
// as fresh as its least recent confirmation, and is re-sent only when the
|
|
899
|
+
// receiver reports one of those fields.
|
|
900
|
+
if (state.desired) {
|
|
901
|
+
const fields = requiredFields();
|
|
902
|
+
const status = state.phase === "manual_override"
|
|
903
|
+
? "transitioning"
|
|
904
|
+
: state.phase === "idle" || state.phase === "unknown" ? "confirming" : state.phase;
|
|
905
|
+
const confirmedAt = fields
|
|
906
|
+
.map((field) => state.fieldObservedAt[field])
|
|
907
|
+
.reduce((oldest, at) => at === null || oldest === null ? null : Date.parse(at) < Date.parse(oldest) ? at : oldest, now.toISOString());
|
|
908
|
+
const reported = fields.some((field) => reportedFields.has(field));
|
|
909
|
+
if (status !== "ready" || ((previousPhase !== "ready" || reported) && confirmedAt !== null)) {
|
|
910
|
+
const theatre = {
|
|
911
|
+
kind: "target_state",
|
|
912
|
+
target: "avr",
|
|
913
|
+
requestId: state.desired.requestId,
|
|
914
|
+
generation: state.desired.generation,
|
|
915
|
+
status,
|
|
916
|
+
observedAt: status === "ready" && confirmedAt !== null ? confirmedAt : now.toISOString(),
|
|
917
|
+
details: {
|
|
918
|
+
power: state.desired.power,
|
|
919
|
+
source: state.desired.source,
|
|
920
|
+
observed: {
|
|
921
|
+
power: state.observed.power,
|
|
922
|
+
mainZone: state.observed.mainZone,
|
|
923
|
+
source: state.observed.source
|
|
924
|
+
},
|
|
925
|
+
recovery: { power: state.observed.power, source: state.observed.source },
|
|
926
|
+
metrics: {
|
|
927
|
+
attempts: state.lastAttempt,
|
|
928
|
+
retries: Math.max(0, state.lastAttempt - 1),
|
|
929
|
+
fallbackUsed: false
|
|
930
|
+
}
|
|
931
|
+
}
|
|
932
|
+
};
|
|
933
|
+
if (state.classification)
|
|
934
|
+
theatre.classification = state.classification;
|
|
935
|
+
if (state.failure) {
|
|
936
|
+
theatre.failure = {
|
|
937
|
+
operation: state.failure.operation,
|
|
938
|
+
reason: "attempts-exhausted",
|
|
939
|
+
attempts: state.failure.attempts,
|
|
940
|
+
occurredAt: state.failure.at
|
|
941
|
+
};
|
|
942
|
+
}
|
|
943
|
+
targetStates.push({ theatre });
|
|
944
|
+
}
|
|
945
|
+
}
|
|
946
|
+
return { state, commands, events, targetStates, traces, status };
|
|
947
|
+
}
|
|
948
|
+
//# sourceMappingURL=avr-controller.js.map
|