@avantics/node-red-home-automation 2.3.0 → 3.0.1
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/README.md +55 -8
- package/contract-examples/room-lighting.mjs +12 -7
- package/dist/lib/activity-lighting-controller.d.ts +13 -25
- package/dist/lib/activity-lighting-controller.js +15 -11
- package/dist/lib/avr-controller.d.ts +13 -47
- package/dist/lib/avr-controller.js +39 -90
- package/dist/lib/backlighting-controller.d.ts +12 -25
- package/dist/lib/backlighting-controller.js +11 -18
- package/dist/lib/bounded-trace.d.ts +6 -0
- package/dist/lib/command-echo.d.ts +22 -0
- package/dist/lib/command-echo.js +106 -0
- package/dist/lib/cover-controller.d.ts +15 -28
- package/dist/lib/cover-controller.js +6 -8
- package/dist/lib/denon-protocol.d.ts +29 -0
- package/dist/lib/denon-protocol.js +76 -0
- package/dist/lib/display-mode-controller.d.ts +7 -18
- package/dist/lib/display-mode-controller.js +39 -47
- package/dist/lib/motion-policy.d.ts +12 -22
- package/dist/lib/motion-policy.js +53 -56
- package/dist/lib/occupancy-fusion.d.ts +8 -12
- package/dist/lib/room-controller.d.ts +15 -34
- package/dist/lib/room-controller.js +100 -134
- package/dist/lib/room-lighting-discovery.d.ts +9 -0
- package/dist/lib/room-lighting-discovery.js +45 -0
- package/dist/lib/settings.d.ts +5 -0
- package/dist/lib/settings.js +68 -0
- package/dist/lib/source-capability-controller.d.ts +30 -35
- package/dist/lib/source-capability-controller.js +35 -36
- package/dist/lib/switch-policy.d.ts +2 -7
- package/dist/lib/target-state.d.ts +24 -0
- package/dist/lib/target-state.js +38 -0
- package/dist/lib/theatre-activity-controller.d.ts +6 -108
- package/dist/lib/theatre-activity-controller.js +67 -318
- package/dist/lib/theatre-ha-discovery.d.ts +14 -0
- package/dist/lib/theatre-ha-discovery.js +163 -0
- package/dist/lib/theatre-metrics.d.ts +4 -0
- package/dist/lib/theatre-metrics.js +66 -0
- package/dist/lib/theatre-profiles.d.ts +23 -0
- package/dist/lib/theatre-profiles.js +73 -0
- package/dist/lib/theatre-refresh.d.ts +20 -0
- package/dist/lib/theatre-refresh.js +46 -0
- package/dist/lib/theatre-slo.js +7 -9
- package/dist/lib/theatre-state-contract.d.ts +7 -19
- package/dist/lib/theatre-state-contract.js +33 -169
- package/dist/lib/theatre-state.d.ts +110 -0
- package/dist/lib/theatre-state.js +139 -0
- package/dist/lib/wall-input.d.ts +2 -7
- package/dist/lib/z2m-light.d.ts +16 -0
- package/dist/lib/z2m-light.js +36 -0
- package/dist/nodes/activity-lighting-controller.html +5 -3
- package/dist/nodes/activity-lighting-controller.js +21 -52
- package/dist/nodes/adapter.d.ts +15 -0
- package/dist/nodes/adapter.js +60 -0
- package/dist/nodes/avr-controller.html +3 -3
- package/dist/nodes/avr-controller.js +17 -56
- package/dist/nodes/backlighting-controller.html +1 -1
- package/dist/nodes/backlighting-controller.js +25 -47
- package/dist/nodes/display-mode-controller.html +2 -2
- package/dist/nodes/display-mode-controller.js +19 -57
- package/dist/nodes/motion-policy.html +23 -10
- package/dist/nodes/motion-policy.js +14 -135
- package/dist/nodes/occupancy-fusion.html +2 -2
- package/dist/nodes/occupancy-fusion.js +23 -29
- package/dist/nodes/room-controller.html +85 -26
- package/dist/nodes/room-controller.js +37 -54
- package/dist/nodes/source-capability-controller.html +4 -13
- package/dist/nodes/source-capability-controller.js +17 -52
- package/dist/nodes/switch-policy.html +3 -3
- package/dist/nodes/switch-policy.js +23 -42
- package/dist/nodes/theatre-activity-controller.html +4 -2
- package/dist/nodes/theatre-activity-controller.js +18 -45
- package/dist/nodes/theatre-state-contract.html +55 -22
- package/dist/nodes/theatre-state-contract.js +30 -41
- package/examples/Home theatre activity.json +13 -12
- package/examples/Home theatre equipment.json +4 -4
- package/examples/Room lighting.json +9 -9
- package/package.json +1 -1
- package/dist/lib/circadian-refresh.d.ts +0 -7
- package/dist/lib/circadian-refresh.js +0 -11
|
@@ -1,195 +1,28 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.TARGETS = void 0;
|
|
4
|
-
exports.normaliseActivityProfiles = normaliseActivityProfiles;
|
|
5
|
-
exports.createTheatreState = createTheatreState;
|
|
3
|
+
exports.createTheatreState = exports.normaliseActivityProfiles = exports.TARGETS = void 0;
|
|
6
4
|
exports.normaliseConfig = normaliseConfig;
|
|
7
5
|
exports.transitionTheatre = transitionTheatre;
|
|
6
|
+
const settings_1 = require("./settings");
|
|
8
7
|
const observation_time_1 = require("./observation-time");
|
|
9
|
-
const
|
|
10
|
-
|
|
11
|
-
const
|
|
12
|
-
const
|
|
13
|
-
const
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
unavailable: true
|
|
21
|
-
};
|
|
22
|
-
const TARGET_CLASSIFICATIONS = {
|
|
23
|
-
drift: true,
|
|
24
|
-
manual_override: true
|
|
25
|
-
};
|
|
26
|
-
const AVAILABILITIES = {
|
|
27
|
-
unknown: true,
|
|
28
|
-
available: true,
|
|
29
|
-
degraded: true,
|
|
30
|
-
unavailable: true
|
|
31
|
-
};
|
|
32
|
-
const SOURCE_ACTIONS = { activate: true, deactivate: true };
|
|
33
|
-
const SOURCE_READINESS = { required: true, optional: true };
|
|
34
|
-
const BACKLIGHTING_POWER = {
|
|
35
|
-
on: true,
|
|
36
|
-
off: true
|
|
37
|
-
};
|
|
38
|
-
function normaliseActivityProfiles(value) {
|
|
39
|
-
if (!value || typeof value !== "object" || Array.isArray(value)) {
|
|
40
|
-
throw new Error("activityProfiles must be an object");
|
|
41
|
-
}
|
|
42
|
-
const entries = Object.entries(value);
|
|
43
|
-
if (entries.length === 0)
|
|
44
|
-
throw new Error("activityProfiles must not be empty");
|
|
45
|
-
const profiles = {};
|
|
46
|
-
for (const [name, profileValue] of entries) {
|
|
47
|
-
if (!PROFILE_NAME.test(name))
|
|
48
|
-
throw new Error(`invalid activity profile name: ${name}`);
|
|
49
|
-
if (!profileValue || typeof profileValue !== "object" || Array.isArray(profileValue)) {
|
|
50
|
-
throw new Error(`activity profile ${name} must be an object`);
|
|
51
|
-
}
|
|
52
|
-
const profile = profileValue;
|
|
53
|
-
const targets = {};
|
|
54
|
-
for (const target of exports.TARGETS) {
|
|
55
|
-
const desired = profile[target];
|
|
56
|
-
if (!desired || typeof desired !== "object" || Array.isArray(desired)) {
|
|
57
|
-
throw new Error(`activity profile ${name} must define ${target}`);
|
|
58
|
-
}
|
|
59
|
-
targets[target] = { ...desired };
|
|
60
|
-
}
|
|
61
|
-
const source = targets.source;
|
|
62
|
-
if (!Object.hasOwn(SOURCE_ACTIONS, source.action)) {
|
|
63
|
-
throw new Error(`activity profile ${name} has an invalid source action`);
|
|
64
|
-
}
|
|
65
|
-
if (!Object.hasOwn(SOURCE_READINESS, source.readiness)) {
|
|
66
|
-
throw new Error(`activity profile ${name} has invalid source readiness`);
|
|
67
|
-
}
|
|
68
|
-
if (source.action === "activate" &&
|
|
69
|
-
(typeof source.sourceId !== "string" || !source.sourceId)) {
|
|
70
|
-
throw new Error(`activity profile ${name} must define sourceId`);
|
|
71
|
-
}
|
|
72
|
-
const lighting = targets.lighting;
|
|
73
|
-
if (typeof lighting.profile !== "string" || !PROFILE_NAME.test(lighting.profile)) {
|
|
74
|
-
throw new Error(`activity profile ${name} has invalid lighting profile`);
|
|
75
|
-
}
|
|
76
|
-
const backlighting = targets.backlighting;
|
|
77
|
-
if (!Object.hasOwn(BACKLIGHTING_POWER, backlighting.power)) {
|
|
78
|
-
throw new Error(`activity profile ${name} has invalid backlighting power`);
|
|
79
|
-
}
|
|
80
|
-
profiles[name] = targets;
|
|
81
|
-
}
|
|
82
|
-
return profiles;
|
|
83
|
-
}
|
|
84
|
-
function emptyTargets(saved = {}) {
|
|
85
|
-
return Object.fromEntries(exports.TARGETS.map((target) => [
|
|
86
|
-
target,
|
|
87
|
-
saved[target] && typeof saved[target] === "object" ? { ...saved[target] } : null
|
|
88
|
-
]));
|
|
89
|
-
}
|
|
90
|
-
// A refresh is re-sent at this many attempts at most before the target is
|
|
91
|
-
// reported as not responding.
|
|
92
|
-
const REFRESH_ATTEMPTS = 3;
|
|
93
|
-
function emptyRefreshPending(saved = {}) {
|
|
94
|
-
return Object.fromEntries(exports.TARGETS.map((target) => {
|
|
95
|
-
const entry = saved[target];
|
|
96
|
-
// Saved before retries existed: pending with no send time, so it retries.
|
|
97
|
-
if (entry === true)
|
|
98
|
-
return [target, { sentAt: 0, attempts: 1, exhausted: false }];
|
|
99
|
-
if (entry && typeof entry === "object" && Number.isFinite(entry.sentAt) && Number.isInteger(entry.attempts)) {
|
|
100
|
-
return [target, { sentAt: entry.sentAt, attempts: entry.attempts, exhausted: entry.exhausted === true }];
|
|
101
|
-
}
|
|
102
|
-
return [target, null];
|
|
103
|
-
}));
|
|
104
|
-
}
|
|
105
|
-
function count(value) {
|
|
106
|
-
const parsed = Number(value);
|
|
107
|
-
return Number.isInteger(parsed) && parsed >= 0 ? parsed : 0;
|
|
108
|
-
}
|
|
109
|
-
function nullableDuration(value) {
|
|
110
|
-
const parsed = Number(value);
|
|
111
|
-
return Number.isFinite(parsed) && parsed >= 0 ? parsed : null;
|
|
112
|
-
}
|
|
113
|
-
function targetMetrics(saved = {}) {
|
|
114
|
-
return Object.fromEntries(exports.TARGETS.map((target) => {
|
|
115
|
-
const metric = saved[target] || {};
|
|
116
|
-
const availability = typeof metric.availability === "string" &&
|
|
117
|
-
Object.hasOwn(AVAILABILITIES, metric.availability)
|
|
118
|
-
? metric.availability
|
|
119
|
-
: "unknown";
|
|
120
|
-
return [target, {
|
|
121
|
-
attempts: count(metric.attempts),
|
|
122
|
-
retries: count(metric.retries),
|
|
123
|
-
failures: count(metric.failures),
|
|
124
|
-
manualOverrides: count(metric.manualOverrides),
|
|
125
|
-
fallbackUses: count(metric.fallbackUses),
|
|
126
|
-
fallbackUsed: metric.fallbackUsed === true,
|
|
127
|
-
lastConfirmationMs: nullableDuration(metric.lastConfirmationMs),
|
|
128
|
-
observationAgeMs: nullableDuration(metric.observationAgeMs),
|
|
129
|
-
availability
|
|
130
|
-
}];
|
|
131
|
-
}));
|
|
132
|
-
}
|
|
133
|
-
function createMetrics(saved = {}) {
|
|
134
|
-
return {
|
|
135
|
-
requests: count(saved.requests),
|
|
136
|
-
ready: count(saved.ready),
|
|
137
|
-
failed: count(saved.failed),
|
|
138
|
-
superseded: count(saved.superseded),
|
|
139
|
-
manualOverrides: count(saved.manualOverrides),
|
|
140
|
-
driftEvents: count(saved.driftEvents),
|
|
141
|
-
recoveriesAdopted: count(saved.recoveriesAdopted),
|
|
142
|
-
recoveriesSafeOff: count(saved.recoveriesSafeOff),
|
|
143
|
-
lastTransitionMs: nullableDuration(saved.lastTransitionMs),
|
|
144
|
-
maxTransitionMs: nullableDuration(saved.maxTransitionMs),
|
|
145
|
-
lastOutcome: typeof saved.lastOutcome === "string" ? saved.lastOutcome : null,
|
|
146
|
-
targets: targetMetrics(saved.targets)
|
|
147
|
-
};
|
|
148
|
-
}
|
|
149
|
-
function createTheatreState(saved = {}, activityProfiles = {}) {
|
|
150
|
-
const savedDesired = saved.desired && typeof saved.desired === "object" ? saved.desired : null;
|
|
151
|
-
const desired = savedDesired && Object.hasOwn(activityProfiles, savedDesired.activity)
|
|
152
|
-
? { ...savedDesired }
|
|
153
|
-
: null;
|
|
154
|
-
return {
|
|
155
|
-
desired,
|
|
156
|
-
currentActivity: typeof saved.currentActivity === "string" &&
|
|
157
|
-
Object.hasOwn(activityProfiles, saved.currentActivity)
|
|
158
|
-
? saved.currentActivity
|
|
159
|
-
: "unknown",
|
|
160
|
-
generation: Number.isInteger(saved.generation) && (saved.generation ?? -1) >= 0 ? saved.generation : 0,
|
|
161
|
-
lastProposedGeneration: Number.isInteger(saved.lastProposedGeneration)
|
|
162
|
-
? saved.lastProposedGeneration
|
|
163
|
-
: savedDesired && !savedDesired.requestId.startsWith("recovery-") && Number.isInteger(savedDesired.generation)
|
|
164
|
-
? savedDesired.generation
|
|
165
|
-
: 0,
|
|
166
|
-
phase: typeof saved.phase === "string" ? saved.phase : "idle",
|
|
167
|
-
targets: emptyTargets(saved.targets),
|
|
168
|
-
refreshPending: emptyRefreshPending(saved.refreshPending),
|
|
169
|
-
failure: saved.failure && typeof saved.failure === "object" ? { ...saved.failure } : null,
|
|
170
|
-
blockedGeneration: Number.isInteger(saved.blockedGeneration) ? saved.blockedGeneration : null,
|
|
171
|
-
degraded: Array.isArray(saved.degraded) ? saved.degraded.map((entry) => ({ ...entry })) : [],
|
|
172
|
-
availability: saved.availability && Object.hasOwn(AVAILABILITIES, saved.availability)
|
|
173
|
-
? saved.availability
|
|
174
|
-
: "unknown",
|
|
175
|
-
transitionStartedAt: typeof saved.transitionStartedAt === "string"
|
|
176
|
-
? saved.transitionStartedAt
|
|
177
|
-
: null,
|
|
178
|
-
transitionCompletedAt: typeof saved.transitionCompletedAt === "string"
|
|
179
|
-
? saved.transitionCompletedAt
|
|
180
|
-
: null,
|
|
181
|
-
metrics: createMetrics(saved.metrics)
|
|
182
|
-
};
|
|
183
|
-
}
|
|
8
|
+
const target_state_1 = require("./target-state");
|
|
9
|
+
const theatre_profiles_1 = require("./theatre-profiles");
|
|
10
|
+
const theatre_state_1 = require("./theatre-state");
|
|
11
|
+
const theatre_metrics_1 = require("./theatre-metrics");
|
|
12
|
+
const theatre_refresh_1 = require("./theatre-refresh");
|
|
13
|
+
// Names other modules have imported from here.
|
|
14
|
+
var theatre_profiles_2 = require("./theatre-profiles");
|
|
15
|
+
Object.defineProperty(exports, "TARGETS", { enumerable: true, get: function () { return theatre_profiles_2.TARGETS; } });
|
|
16
|
+
Object.defineProperty(exports, "normaliseActivityProfiles", { enumerable: true, get: function () { return theatre_profiles_2.normaliseActivityProfiles; } });
|
|
17
|
+
var theatre_state_2 = require("./theatre-state");
|
|
18
|
+
Object.defineProperty(exports, "createTheatreState", { enumerable: true, get: function () { return theatre_state_2.createTheatreState; } });
|
|
184
19
|
function normaliseConfig(config = {}) {
|
|
185
|
-
const activityProfiles = normaliseActivityProfiles(config.activityProfiles);
|
|
186
|
-
const recoveryOffActivity =
|
|
187
|
-
config.recoveryOffActivity
|
|
188
|
-
? config.recoveryOffActivity
|
|
189
|
-
: "off";
|
|
20
|
+
const activityProfiles = (0, theatre_profiles_1.normaliseActivityProfiles)(config.activityProfiles);
|
|
21
|
+
const recoveryOffActivity = (0, settings_1.textSetting)(config.recoveryOffActivity, "recovery off activity", "off");
|
|
190
22
|
return {
|
|
191
|
-
|
|
192
|
-
|
|
23
|
+
// Optional: an empty area means none.
|
|
24
|
+
area: typeof config.area === "string" ? config.area.trim() : "",
|
|
25
|
+
freshnessMs: Math.max(1000, (0, settings_1.numberSetting)(config.freshnessMs, "freshness", 30000)),
|
|
193
26
|
activityProfiles,
|
|
194
27
|
recoveryOffActivity
|
|
195
28
|
};
|
|
@@ -210,7 +43,7 @@ function targetIntentCommands(desired, config, previousDesired) {
|
|
|
210
43
|
const previousSource = previousDesired
|
|
211
44
|
? config.activityProfiles[previousDesired.activity]?.source
|
|
212
45
|
: null;
|
|
213
|
-
return
|
|
46
|
+
return theatre_profiles_1.TARGETS.map((target) => {
|
|
214
47
|
let values = profile[target];
|
|
215
48
|
if (target === "source" && values.action === "deactivate" && !values.sourceId) {
|
|
216
49
|
values = { ...values, sourceId: previousSource?.sourceId || null };
|
|
@@ -219,10 +52,10 @@ function targetIntentCommands(desired, config, previousDesired) {
|
|
|
219
52
|
});
|
|
220
53
|
}
|
|
221
54
|
function targetRefreshCommands(desired) {
|
|
222
|
-
return
|
|
55
|
+
return theatre_profiles_1.TARGETS.map((target) => targetCommand(target, "refresh", desired));
|
|
223
56
|
}
|
|
224
57
|
function targetProbeCommands() {
|
|
225
|
-
return
|
|
58
|
+
return theatre_profiles_1.TARGETS.map((target) => targetCommand(target, "refresh", null));
|
|
226
59
|
}
|
|
227
60
|
function observationTime(targetState) {
|
|
228
61
|
return typeof targetState?.observedAt === "string" ? (0, observation_time_1.parseObservedAt)(targetState.observedAt) : null;
|
|
@@ -230,6 +63,9 @@ function observationTime(targetState) {
|
|
|
230
63
|
function isFresh(targetState, nowMs, freshnessMs) {
|
|
231
64
|
return (0, observation_time_1.isFresh)(observationTime(targetState), nowMs, freshnessMs);
|
|
232
65
|
}
|
|
66
|
+
function stateEvent(state, area) {
|
|
67
|
+
return { theatre: { ...(0, theatre_state_1.theatreSnapshot)(state, area) } };
|
|
68
|
+
}
|
|
233
69
|
function statusFor(state) {
|
|
234
70
|
const activity = state.desired?.activity || "none";
|
|
235
71
|
if (state.phase === "failed") {
|
|
@@ -237,36 +73,7 @@ function statusFor(state) {
|
|
|
237
73
|
}
|
|
238
74
|
return { phase: state.phase, text: `${activity}: ${state.phase}` };
|
|
239
75
|
}
|
|
240
|
-
function stateEvent(state, area) {
|
|
241
|
-
return {
|
|
242
|
-
theatre: {
|
|
243
|
-
kind: "state",
|
|
244
|
-
area,
|
|
245
|
-
requestId: state.desired?.requestId || null,
|
|
246
|
-
generation: state.generation,
|
|
247
|
-
desiredActivity: state.desired?.activity || null,
|
|
248
|
-
requestedAt: state.desired?.requestedAt || null,
|
|
249
|
-
currentActivity: state.currentActivity,
|
|
250
|
-
phase: state.phase,
|
|
251
|
-
availability: state.availability,
|
|
252
|
-
transitionStartedAt: state.transitionStartedAt,
|
|
253
|
-
transitionCompletedAt: state.transitionCompletedAt,
|
|
254
|
-
targets: emptyTargets(state.targets),
|
|
255
|
-
failure: state.failure ? { ...state.failure } : null,
|
|
256
|
-
degraded: state.degraded.map((entry) => ({ ...entry })),
|
|
257
|
-
metrics: createMetrics(state.metrics)
|
|
258
|
-
}
|
|
259
|
-
};
|
|
260
|
-
}
|
|
261
76
|
const RECOVERY_TARGETS = ["display", "avr", "source", "backlighting"];
|
|
262
|
-
function matchesExpected(expected, observed) {
|
|
263
|
-
if (!expected || typeof expected !== "object" || Array.isArray(expected)) {
|
|
264
|
-
return Object.is(expected, observed);
|
|
265
|
-
}
|
|
266
|
-
if (!observed || typeof observed !== "object" || Array.isArray(observed))
|
|
267
|
-
return false;
|
|
268
|
-
return Object.entries(expected).every(([key, value]) => matchesExpected(value, observed[key]));
|
|
269
|
-
}
|
|
270
77
|
function recoveryMatches(profile, observations, nowMs, freshnessMs) {
|
|
271
78
|
return RECOVERY_TARGETS.every((target) => {
|
|
272
79
|
const observation = observations[target];
|
|
@@ -275,7 +82,7 @@ function recoveryMatches(profile, observations, nowMs, freshnessMs) {
|
|
|
275
82
|
const expected = { ...profile[target] };
|
|
276
83
|
if (target === "source")
|
|
277
84
|
delete expected.readiness;
|
|
278
|
-
return matchesExpected(expected, observation.values);
|
|
85
|
+
return (0, theatre_profiles_1.matchesExpected)(expected, observation.values);
|
|
279
86
|
});
|
|
280
87
|
}
|
|
281
88
|
function transitionTheatre(saved, msg, suppliedConfig = {}, nowValue = new Date()) {
|
|
@@ -284,7 +91,7 @@ function transitionTheatre(saved, msg, suppliedConfig = {}, nowValue = new Date(
|
|
|
284
91
|
const nowMs = now.getTime();
|
|
285
92
|
if (!Number.isFinite(nowMs))
|
|
286
93
|
throw new Error("now must be a valid date");
|
|
287
|
-
const state = createTheatreState(saved, config.activityProfiles);
|
|
94
|
+
const state = (0, theatre_state_1.createTheatreState)(saved, config.activityProfiles);
|
|
288
95
|
const event = msg?.theatre;
|
|
289
96
|
const commands = [];
|
|
290
97
|
const events = [];
|
|
@@ -306,63 +113,20 @@ function transitionTheatre(saved, msg, suppliedConfig = {}, nowValue = new Date(
|
|
|
306
113
|
}
|
|
307
114
|
});
|
|
308
115
|
}
|
|
116
|
+
// Records an outcome. Only the transition's first ready or failed ends it
|
|
117
|
+
// and is timed: a later override or failure is not part of the transition.
|
|
309
118
|
function completeTransition(outcome) {
|
|
310
119
|
const startedAt = Date.parse(state.transitionStartedAt || "");
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
state.
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
state.metrics.lastOutcome = outcome;
|
|
317
|
-
if (outcome === "ready")
|
|
318
|
-
state.metrics.ready += 1;
|
|
319
|
-
if (outcome === "failed")
|
|
320
|
-
state.metrics.failed += 1;
|
|
321
|
-
}
|
|
322
|
-
function resetTransitionMetrics() {
|
|
323
|
-
for (const target of exports.TARGETS) {
|
|
324
|
-
const metric = state.metrics.targets[target];
|
|
325
|
-
metric.attempts = 0;
|
|
326
|
-
metric.retries = 0;
|
|
327
|
-
metric.fallbackUsed = false;
|
|
328
|
-
metric.lastConfirmationMs = null;
|
|
329
|
-
metric.observationAgeMs = null;
|
|
330
|
-
metric.availability = "unknown";
|
|
120
|
+
let transitionMs = null;
|
|
121
|
+
if (outcome !== "manual_override" && state.transitionCompletedAt === null) {
|
|
122
|
+
state.transitionCompletedAt = now.toISOString();
|
|
123
|
+
if (Number.isFinite(startedAt))
|
|
124
|
+
transitionMs = Math.max(0, nowMs - startedAt);
|
|
331
125
|
}
|
|
126
|
+
(0, theatre_metrics_1.recordOutcome)(state.metrics, outcome, transitionMs);
|
|
332
127
|
}
|
|
333
128
|
function updateTargetMetrics(target, targetState, previous) {
|
|
334
|
-
|
|
335
|
-
const details = targetState.details && typeof targetState.details === "object"
|
|
336
|
-
? targetState.details
|
|
337
|
-
: {};
|
|
338
|
-
const detailMetrics = details.metrics && typeof details.metrics === "object" &&
|
|
339
|
-
!Array.isArray(details.metrics)
|
|
340
|
-
? details.metrics
|
|
341
|
-
: {};
|
|
342
|
-
const attempts = count(detailMetrics.attempts ?? details.attempt);
|
|
343
|
-
metric.attempts = Math.max(metric.attempts, attempts);
|
|
344
|
-
metric.retries = Math.max(metric.retries, Math.max(0, attempts - 1));
|
|
345
|
-
const fallbackUsed = detailMetrics.fallbackUsed === true;
|
|
346
|
-
if (fallbackUsed && !metric.fallbackUsed)
|
|
347
|
-
metric.fallbackUses += 1;
|
|
348
|
-
metric.fallbackUsed ||= fallbackUsed;
|
|
349
|
-
const observedAt = observationTime(targetState);
|
|
350
|
-
metric.observationAgeMs = observedAt === null ? null : Math.max(0, nowMs - observedAt);
|
|
351
|
-
if (targetState.status === "ready") {
|
|
352
|
-
const startedAt = Date.parse(state.transitionStartedAt || "");
|
|
353
|
-
metric.lastConfirmationMs = Number.isFinite(startedAt) && observedAt !== null
|
|
354
|
-
? Math.max(0, observedAt - startedAt)
|
|
355
|
-
: null;
|
|
356
|
-
metric.availability = "available";
|
|
357
|
-
}
|
|
358
|
-
else if (targetState.status === "failed" || targetState.status === "unavailable") {
|
|
359
|
-
metric.availability = "unavailable";
|
|
360
|
-
if (previous?.status !== targetState.status)
|
|
361
|
-
metric.failures += 1;
|
|
362
|
-
}
|
|
363
|
-
else {
|
|
364
|
-
metric.availability = "degraded";
|
|
365
|
-
}
|
|
129
|
+
(0, theatre_metrics_1.recordTargetReport)(state.metrics.targets[target], targetState, previous, observationTime(targetState), state.transitionStartedAt, nowMs);
|
|
366
130
|
}
|
|
367
131
|
function fail(target, targetState) {
|
|
368
132
|
const firstFailure = state.phase !== "failed";
|
|
@@ -379,7 +143,6 @@ function transitionTheatre(saved, msg, suppliedConfig = {}, nowValue = new Date(
|
|
|
379
143
|
state.currentActivity = "unknown";
|
|
380
144
|
state.phase = "failed";
|
|
381
145
|
state.availability = "unavailable";
|
|
382
|
-
state.transitionCompletedAt = state.transitionCompletedAt || now.toISOString();
|
|
383
146
|
if (firstFailure)
|
|
384
147
|
completeTransition("failed");
|
|
385
148
|
}
|
|
@@ -404,8 +167,8 @@ function transitionTheatre(saved, msg, suppliedConfig = {}, nowValue = new Date(
|
|
|
404
167
|
if (!profile)
|
|
405
168
|
throw new Error("desired activity profile is missing");
|
|
406
169
|
const sourceProfile = profile.source;
|
|
407
|
-
for (const target of
|
|
408
|
-
const optional = OPTIONAL_TARGETS.includes(target) ||
|
|
170
|
+
for (const target of theatre_profiles_1.TARGETS) {
|
|
171
|
+
const optional = theatre_profiles_1.OPTIONAL_TARGETS.includes(target) ||
|
|
409
172
|
(target === "source" && sourceProfile.readiness === "optional");
|
|
410
173
|
if (!optional)
|
|
411
174
|
continue;
|
|
@@ -420,7 +183,7 @@ function transitionTheatre(saved, msg, suppliedConfig = {}, nowValue = new Date(
|
|
|
420
183
|
state.degraded.push({ target, reason: targetState.failure?.reason || targetState.status });
|
|
421
184
|
}
|
|
422
185
|
}
|
|
423
|
-
const manualTarget =
|
|
186
|
+
const manualTarget = theatre_profiles_1.TARGETS.find((target) => {
|
|
424
187
|
const targetState = state.targets[target];
|
|
425
188
|
return targetState?.classification === "manual_override" &&
|
|
426
189
|
isFresh(targetState, nowMs, config.freshnessMs);
|
|
@@ -436,7 +199,7 @@ function transitionTheatre(saved, msg, suppliedConfig = {}, nowValue = new Date(
|
|
|
436
199
|
}
|
|
437
200
|
return;
|
|
438
201
|
}
|
|
439
|
-
for (const target of REQUIRED_TARGETS) {
|
|
202
|
+
for (const target of theatre_profiles_1.REQUIRED_TARGETS) {
|
|
440
203
|
const targetState = state.targets[target];
|
|
441
204
|
if (targetState && isFresh(targetState, nowMs, config.freshnessMs) &&
|
|
442
205
|
(targetState.status === "failed" || targetState.status === "unavailable")) {
|
|
@@ -460,7 +223,7 @@ function transitionTheatre(saved, msg, suppliedConfig = {}, nowValue = new Date(
|
|
|
460
223
|
const unverified = (target) => {
|
|
461
224
|
state.degraded.push({ target, reason: state.refreshPending[target]?.exhausted ? "no-response" : "stale" });
|
|
462
225
|
};
|
|
463
|
-
for (const target of REQUIRED_TARGETS) {
|
|
226
|
+
for (const target of theatre_profiles_1.REQUIRED_TARGETS) {
|
|
464
227
|
const targetState = state.targets[target];
|
|
465
228
|
if (targetState && holdReady && targetState.status === "ready" &&
|
|
466
229
|
!isFresh(targetState, nowMs, config.freshnessMs)) {
|
|
@@ -509,45 +272,31 @@ function transitionTheatre(saved, msg, suppliedConfig = {}, nowValue = new Date(
|
|
|
509
272
|
if (previousPhase !== "ready" || previousCurrent !== state.currentActivity) {
|
|
510
273
|
// Only the first ready of a generation completes its transition.
|
|
511
274
|
if (state.transitionCompletedAt === null) {
|
|
512
|
-
state.transitionCompletedAt = now.toISOString();
|
|
513
275
|
completeTransition("ready");
|
|
514
276
|
}
|
|
515
277
|
emit("ready");
|
|
516
278
|
trace("activity", "ready", null, { activity: state.currentActivity });
|
|
517
279
|
}
|
|
518
280
|
}
|
|
519
|
-
//
|
|
520
|
-
// activity stays fresh, and re-send an unanswered refresh a bounded number
|
|
521
|
-
// of times before reporting the target as not responding.
|
|
281
|
+
// Send the refreshes planned for this moment (see theatre-refresh).
|
|
522
282
|
function tickRefreshes() {
|
|
523
|
-
const
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
if (
|
|
527
|
-
|
|
528
|
-
continue;
|
|
529
|
-
if (pending.attempts < REFRESH_ATTEMPTS) {
|
|
530
|
-
commands.push(targetCommand(target, "refresh", state.desired));
|
|
531
|
-
state.refreshPending[target] = { sentAt: nowMs, attempts: pending.attempts + 1, exhausted: false };
|
|
532
|
-
trace("refresh", "retried", null, { target, attempt: pending.attempts + 1 });
|
|
533
|
-
}
|
|
534
|
-
else {
|
|
535
|
-
pending.exhausted = true;
|
|
536
|
-
noResponse(target);
|
|
537
|
-
}
|
|
283
|
+
const plan = (0, theatre_refresh_1.planRefreshes)(state.refreshPending, (target) => observationTime(state.targets[target]), state.desired !== null, config.freshnessMs, nowMs);
|
|
284
|
+
state.refreshPending = plan.pending;
|
|
285
|
+
for (const action of plan.actions) {
|
|
286
|
+
if (action.kind === "give-up") {
|
|
287
|
+
noResponse(action.target);
|
|
538
288
|
continue;
|
|
539
289
|
}
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
}
|
|
290
|
+
commands.push(targetCommand(action.target, "refresh", state.desired));
|
|
291
|
+
if (action.kind === "retry")
|
|
292
|
+
trace("refresh", "retried", null, { target: action.target, attempt: action.attempt });
|
|
293
|
+
if (action.kind === "re-ask")
|
|
294
|
+
trace("refresh", "retried", "no-response", { target: action.target });
|
|
546
295
|
}
|
|
547
296
|
}
|
|
548
297
|
function noResponse(target) {
|
|
549
298
|
state.metrics.targets[target].availability = "unavailable";
|
|
550
|
-
trace("refresh", "no-response", null, { target, attempts: REFRESH_ATTEMPTS });
|
|
299
|
+
trace("refresh", "no-response", null, { target, attempts: theatre_state_1.REFRESH_ATTEMPTS });
|
|
551
300
|
// Before the activity first completes, an owner that never answers is
|
|
552
301
|
// unavailable; afterwards it shows as degraded and does not undo ready.
|
|
553
302
|
if (state.desired && state.transitionCompletedAt === null) {
|
|
@@ -623,29 +372,29 @@ function transitionTheatre(saved, msg, suppliedConfig = {}, nowValue = new Date(
|
|
|
623
372
|
state.lastProposedGeneration = Math.max(state.lastProposedGeneration, event.generation);
|
|
624
373
|
state.phase = "preparing";
|
|
625
374
|
state.currentActivity = "unknown";
|
|
626
|
-
state.targets = emptyTargets();
|
|
627
|
-
state.refreshPending = emptyRefreshPending();
|
|
375
|
+
state.targets = (0, theatre_state_1.emptyTargets)();
|
|
376
|
+
state.refreshPending = (0, theatre_state_1.emptyRefreshPending)();
|
|
628
377
|
state.failure = null;
|
|
629
378
|
state.blockedGeneration = null;
|
|
630
379
|
state.availability = "unknown";
|
|
631
380
|
state.transitionStartedAt = requestedAt;
|
|
632
381
|
state.transitionCompletedAt = null;
|
|
633
382
|
state.metrics.requests += 1;
|
|
634
|
-
resetTransitionMetrics();
|
|
383
|
+
(0, theatre_metrics_1.resetTransitionMetrics)(state.metrics);
|
|
635
384
|
commands.push(...targetIntentCommands(state.desired, config, previousDesired));
|
|
636
385
|
trace("intent", "accepted", null, { activity: event.activity, generation });
|
|
637
386
|
}
|
|
638
387
|
function acceptTargetState(event) {
|
|
639
|
-
if (!
|
|
388
|
+
if (!theatre_profiles_1.TARGETS.includes(event.target))
|
|
640
389
|
throw new Error("unknown theatre target");
|
|
641
390
|
if (!Number.isInteger(event.generation) || event.generation < 1) {
|
|
642
391
|
throw new Error("target generation must be a positive integer");
|
|
643
392
|
}
|
|
644
|
-
if (!
|
|
393
|
+
if (!(0, target_state_1.isTargetStatus)(event.status))
|
|
645
394
|
throw new Error("unknown theatre target status");
|
|
646
395
|
if (observationTime(event) === null)
|
|
647
396
|
throw new Error("target observedAt must be an ISO timestamp");
|
|
648
|
-
if (event.classification && !Object.hasOwn(TARGET_CLASSIFICATIONS, event.classification)) {
|
|
397
|
+
if (event.classification && !Object.hasOwn(theatre_state_1.TARGET_CLASSIFICATIONS, event.classification)) {
|
|
649
398
|
throw new Error("unknown theatre target classification");
|
|
650
399
|
}
|
|
651
400
|
if (!state.desired || event.generation !== state.generation) {
|
|
@@ -719,15 +468,15 @@ function transitionTheatre(saved, msg, suppliedConfig = {}, nowValue = new Date(
|
|
|
719
468
|
requestedAt
|
|
720
469
|
};
|
|
721
470
|
state.generation = generation;
|
|
722
|
-
state.targets = emptyTargets();
|
|
723
|
-
state.refreshPending = emptyRefreshPending();
|
|
471
|
+
state.targets = (0, theatre_state_1.emptyTargets)();
|
|
472
|
+
state.refreshPending = (0, theatre_state_1.emptyRefreshPending)();
|
|
724
473
|
state.failure = null;
|
|
725
474
|
state.blockedGeneration = null;
|
|
726
475
|
state.degraded = [];
|
|
727
476
|
state.transitionStartedAt = requestedAt;
|
|
728
477
|
state.transitionCompletedAt = requestedAt;
|
|
729
|
-
resetTransitionMetrics();
|
|
730
|
-
for (const target of
|
|
478
|
+
(0, theatre_metrics_1.resetTransitionMetrics)(state.metrics);
|
|
479
|
+
for (const target of theatre_profiles_1.TARGETS) {
|
|
731
480
|
const observation = event.observations[target];
|
|
732
481
|
state.targets[target] = {
|
|
733
482
|
status: "ready",
|
|
@@ -794,11 +543,11 @@ function transitionTheatre(saved, msg, suppliedConfig = {}, nowValue = new Date(
|
|
|
794
543
|
acceptRecovery(event);
|
|
795
544
|
}
|
|
796
545
|
else if (event.kind === "startup" || event.kind === "refresh") {
|
|
797
|
-
state.targets = emptyTargets();
|
|
798
|
-
state.refreshPending = emptyRefreshPending();
|
|
546
|
+
state.targets = (0, theatre_state_1.emptyTargets)();
|
|
547
|
+
state.refreshPending = (0, theatre_state_1.emptyRefreshPending)();
|
|
799
548
|
if (state.desired) {
|
|
800
549
|
commands.push(...targetRefreshCommands(state.desired));
|
|
801
|
-
for (const target of
|
|
550
|
+
for (const target of theatre_profiles_1.TARGETS)
|
|
802
551
|
state.refreshPending[target] = { sentAt: nowMs, attempts: 1, exhausted: false };
|
|
803
552
|
state.phase = state.blockedGeneration === state.generation ? "failed" : "confirming";
|
|
804
553
|
state.currentActivity = "unknown";
|
|
@@ -812,7 +561,7 @@ function transitionTheatre(saved, msg, suppliedConfig = {}, nowValue = new Date(
|
|
|
812
561
|
}
|
|
813
562
|
else {
|
|
814
563
|
commands.push(...targetProbeCommands());
|
|
815
|
-
for (const target of
|
|
564
|
+
for (const target of theatre_profiles_1.TARGETS)
|
|
816
565
|
state.refreshPending[target] = { sentAt: nowMs, attempts: 1, exhausted: false };
|
|
817
566
|
state.phase = "recovering";
|
|
818
567
|
state.availability = "unknown";
|
|
@@ -827,7 +576,7 @@ function transitionTheatre(saved, msg, suppliedConfig = {}, nowValue = new Date(
|
|
|
827
576
|
else {
|
|
828
577
|
tickRefreshes();
|
|
829
578
|
if (!state.desired && state.phase === "recovering" &&
|
|
830
|
-
|
|
579
|
+
theatre_profiles_1.TARGETS.every((target) => state.refreshPending[target]?.exhausted === true)) {
|
|
831
580
|
// Nobody supplied recovery observations: fall back as for no evidence.
|
|
832
581
|
trace("recovery", "timed-out", "no-observations");
|
|
833
582
|
acceptRecovery({ kind: "recover", reason: "startup", observations: {} });
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { type TheatreTarget } from "./theatre-profiles";
|
|
2
|
+
export type TheatreTopicName = "request" | "desired" | "current" | "phase" | "availability" | "failure" | "metrics" | "slo" | "slo/breach" | `health/${TheatreTarget}`;
|
|
3
|
+
export declare function theatreTopic(baseTopic: string, name: TheatreTopicName): string;
|
|
4
|
+
export interface TheatreDiscoveryConfig {
|
|
5
|
+
area: string;
|
|
6
|
+
displayName: string;
|
|
7
|
+
baseTopic: string;
|
|
8
|
+
discoveryPrefix: string;
|
|
9
|
+
activities: string[];
|
|
10
|
+
}
|
|
11
|
+
export declare function theatreDiscovery(config: TheatreDiscoveryConfig): Array<{
|
|
12
|
+
topic: string;
|
|
13
|
+
payload: Record<string, unknown>;
|
|
14
|
+
}>;
|