@avantics/node-red-home-automation 4.0.2 → 5.0.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/README.md +126 -4
- package/contract-examples/switch-gestures.mjs +1 -1
- package/contract-examples/volume-ownership.mjs +6 -5
- package/dist/lib/activity-lighting-controller.d.ts +2 -0
- package/dist/lib/activity-lighting-controller.js +46 -7
- package/dist/lib/adopted-request.d.ts +17 -0
- package/dist/lib/adopted-request.js +61 -7
- package/dist/lib/avr-controller.d.ts +9 -3
- package/dist/lib/avr-controller.js +76 -24
- package/dist/lib/avr-publishing.d.ts +3 -3
- package/dist/lib/avr-publishing.js +11 -5
- package/dist/lib/backlighting-controller.d.ts +2 -0
- package/dist/lib/backlighting-controller.js +37 -8
- package/dist/lib/cover-publishing.js +1 -1
- package/dist/lib/denon-protocol.d.ts +5 -1
- package/dist/lib/denon-protocol.js +26 -11
- package/dist/lib/display-mode-controller.d.ts +12 -5
- package/dist/lib/display-mode-controller.js +181 -87
- package/dist/lib/house-schedule-publishing.js +1 -1
- package/dist/lib/light-styling.d.ts +2 -2
- package/dist/lib/light-styling.js +5 -4
- package/dist/lib/motion-publishing.js +1 -1
- package/dist/lib/occupancy-publishing.js +1 -1
- package/dist/lib/power-reading.d.ts +5 -0
- package/dist/lib/power-reading.js +13 -0
- package/dist/lib/room-controller.d.ts +2 -3
- package/dist/lib/room-controller.js +18 -2
- package/dist/lib/room-lighting-discovery.js +1 -1
- package/dist/lib/room-publishing.d.ts +3 -0
- package/dist/lib/shelly-cover.d.ts +46 -0
- package/dist/lib/shelly-cover.js +193 -0
- package/dist/lib/source-capability-controller.d.ts +2 -0
- package/dist/lib/source-capability-controller.js +27 -6
- package/dist/lib/switch-policy.d.ts +1 -1
- package/dist/lib/switch-policy.js +1 -1
- package/dist/lib/theatre-requests.d.ts +29 -0
- package/dist/lib/theatre-requests.js +89 -0
- package/dist/lib/theatre-state-contract.d.ts +5 -22
- package/dist/lib/theatre-state-contract.js +18 -73
- package/dist/lib/toggle-power.d.ts +41 -0
- package/dist/lib/toggle-power.js +74 -0
- package/dist/lib/topic-names.d.ts +1 -0
- package/dist/lib/topic-names.js +7 -0
- package/dist/lib/{display-transport.d.ts → transports/display-transport.d.ts} +1 -1
- package/dist/lib/{display-transport.js → transports/display-transport.js} +32 -7
- package/dist/lib/z2m-light.d.ts +8 -0
- package/dist/lib/z2m-light.js +64 -3
- package/dist/nodes/activity-lighting-controller.html +8 -0
- package/dist/nodes/avr-controller.html +28 -6
- package/dist/nodes/avr-controller.js +16 -6
- package/dist/nodes/display-mode-controller.html +61 -11
- package/dist/nodes/display-mode-controller.js +16 -6
- package/dist/nodes/room-controller.js +14 -0
- package/dist/nodes/shelly-cover.d.ts +3 -0
- package/dist/nodes/shelly-cover.html +65 -0
- package/dist/nodes/shelly-cover.js +34 -0
- package/examples/Home theatre activity.json +5 -5
- package/examples/Home theatre equipment.json +4 -4
- package/examples/Shelly cover.json +252 -0
- package/package.json +3 -2
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import type { TraceEntry } from "./bounded-trace";
|
|
2
|
+
import type { CoverCommand, CoverEvent } from "./cover-controller";
|
|
3
|
+
export interface ShellyCoverConfig {
|
|
4
|
+
topicPrefix: string;
|
|
5
|
+
channel: number;
|
|
6
|
+
}
|
|
7
|
+
export interface ShellyCoverState {
|
|
8
|
+
status: Record<string, unknown> | null;
|
|
9
|
+
complete: boolean;
|
|
10
|
+
command: {
|
|
11
|
+
operation: string;
|
|
12
|
+
generation: number;
|
|
13
|
+
} | null;
|
|
14
|
+
}
|
|
15
|
+
export interface ShellyCoverMessage {
|
|
16
|
+
topic?: unknown;
|
|
17
|
+
payload?: unknown;
|
|
18
|
+
cover_command?: CoverCommand;
|
|
19
|
+
}
|
|
20
|
+
export interface MqttPublication {
|
|
21
|
+
topic: string;
|
|
22
|
+
payload: string;
|
|
23
|
+
}
|
|
24
|
+
export declare function normaliseShellyCoverConfig(config: {
|
|
25
|
+
topicPrefix?: unknown;
|
|
26
|
+
channel?: unknown;
|
|
27
|
+
}): ShellyCoverConfig;
|
|
28
|
+
export declare function shellyCoverTopics(config: ShellyCoverConfig): {
|
|
29
|
+
command: string;
|
|
30
|
+
status: string;
|
|
31
|
+
events: string;
|
|
32
|
+
online: string;
|
|
33
|
+
component: string;
|
|
34
|
+
};
|
|
35
|
+
export declare function transitionShellyCover(saved: Partial<ShellyCoverState> | undefined, msg: ShellyCoverMessage, config: ShellyCoverConfig, now: Date): {
|
|
36
|
+
state: ShellyCoverState;
|
|
37
|
+
publications: MqttPublication[];
|
|
38
|
+
readings: {
|
|
39
|
+
cover: CoverEvent;
|
|
40
|
+
}[];
|
|
41
|
+
traces: TraceEntry<Record<string, unknown>>[];
|
|
42
|
+
status: {
|
|
43
|
+
text: string;
|
|
44
|
+
calibrated: boolean;
|
|
45
|
+
};
|
|
46
|
+
};
|
|
@@ -0,0 +1,193 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.normaliseShellyCoverConfig = normaliseShellyCoverConfig;
|
|
4
|
+
exports.shellyCoverTopics = shellyCoverTopics;
|
|
5
|
+
exports.transitionShellyCover = transitionShellyCover;
|
|
6
|
+
// Settings may be editor strings. The prefix names the user's own device,
|
|
7
|
+
// so it has no default.
|
|
8
|
+
function normaliseShellyCoverConfig(config) {
|
|
9
|
+
const topicPrefix = String(config.topicPrefix ?? "").trim().replace(/^\/+|\/+$/g, "");
|
|
10
|
+
if (!topicPrefix)
|
|
11
|
+
throw new Error("the device's MQTT topic prefix is required");
|
|
12
|
+
if (/[+#]/.test(topicPrefix))
|
|
13
|
+
throw new Error("the MQTT topic prefix can't contain + or #");
|
|
14
|
+
const channel = config.channel === undefined || config.channel === "" ? 0 : Number(config.channel);
|
|
15
|
+
if (!Number.isInteger(channel) || channel < 0)
|
|
16
|
+
throw new Error("the cover ID must be a whole number from 0");
|
|
17
|
+
return { topicPrefix, channel };
|
|
18
|
+
}
|
|
19
|
+
function shellyCoverTopics(config) {
|
|
20
|
+
const component = `cover:${config.channel}`;
|
|
21
|
+
return {
|
|
22
|
+
command: `${config.topicPrefix}/command/${component}`,
|
|
23
|
+
status: `${config.topicPrefix}/status/${component}`,
|
|
24
|
+
events: `${config.topicPrefix}/events/rpc`,
|
|
25
|
+
online: `${config.topicPrefix}/online`,
|
|
26
|
+
component
|
|
27
|
+
};
|
|
28
|
+
}
|
|
29
|
+
const MOTIONS = {
|
|
30
|
+
opening: "opening",
|
|
31
|
+
closing: "closing",
|
|
32
|
+
open: "stopped",
|
|
33
|
+
closed: "stopped",
|
|
34
|
+
stopped: "stopped"
|
|
35
|
+
};
|
|
36
|
+
// An MQTT in node may hand over a parsed object, a string or a Buffer.
|
|
37
|
+
function parsed(payload) {
|
|
38
|
+
const text = Buffer.isBuffer(payload) ? payload.toString("utf8") : payload;
|
|
39
|
+
if (typeof text !== "string")
|
|
40
|
+
return text;
|
|
41
|
+
try {
|
|
42
|
+
return JSON.parse(text);
|
|
43
|
+
}
|
|
44
|
+
catch {
|
|
45
|
+
return text;
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
function isObject(value) {
|
|
49
|
+
return Boolean(value) && typeof value === "object" && !Array.isArray(value);
|
|
50
|
+
}
|
|
51
|
+
function transitionShellyCover(saved, msg, config, now) {
|
|
52
|
+
const state = {
|
|
53
|
+
status: isObject(saved?.status) ? { ...saved.status } : null,
|
|
54
|
+
complete: saved?.complete === true && isObject(saved?.status),
|
|
55
|
+
command: saved?.command && Number.isInteger(saved.command.generation) ? { ...saved.command } : null
|
|
56
|
+
};
|
|
57
|
+
const topics = shellyCoverTopics(config);
|
|
58
|
+
const publications = [];
|
|
59
|
+
const readings = [];
|
|
60
|
+
const traces = [];
|
|
61
|
+
const trace = (event, outcome, reason = null, details = {}) => {
|
|
62
|
+
traces.push({ event, outcome, reason, details });
|
|
63
|
+
};
|
|
64
|
+
// Asks the device for its full cover status.
|
|
65
|
+
function requestStatus(reason) {
|
|
66
|
+
publications.push({ topic: topics.command, payload: "status_update" });
|
|
67
|
+
trace("status", "requested", reason);
|
|
68
|
+
}
|
|
69
|
+
// What the status says: a position reading, or why there is none.
|
|
70
|
+
function read(observedAt, reply) {
|
|
71
|
+
const status = state.status ?? {};
|
|
72
|
+
if (status.state === "calibrating") {
|
|
73
|
+
trace("status", "ignored", "calibrating");
|
|
74
|
+
return;
|
|
75
|
+
}
|
|
76
|
+
if (status.pos_control !== false && !Object.hasOwn(status, "current_pos") && !state.complete) {
|
|
77
|
+
// Only changes so far, none of them the position: ask for the rest.
|
|
78
|
+
return requestStatus("no-full-status");
|
|
79
|
+
}
|
|
80
|
+
const reason = status.pos_control === false || !Object.hasOwn(status, "current_pos") ? "not-calibrated"
|
|
81
|
+
: typeof status.current_pos !== "number" || !Number.isFinite(status.current_pos) ? "position-unknown"
|
|
82
|
+
: null;
|
|
83
|
+
if (reason) {
|
|
84
|
+
// Never a made-up position: the command in hand fails with the reason,
|
|
85
|
+
// which the controller retries and then reports.
|
|
86
|
+
trace("status", "no-position", reason);
|
|
87
|
+
if (state.command) {
|
|
88
|
+
readings.push({ cover: { kind: "command_result", generation: state.command.generation, ok: false, reason } });
|
|
89
|
+
state.command = null;
|
|
90
|
+
}
|
|
91
|
+
return;
|
|
92
|
+
}
|
|
93
|
+
const motion = typeof status.state === "string" ? MOTIONS[status.state] : undefined;
|
|
94
|
+
readings.push({ cover: {
|
|
95
|
+
kind: "observed",
|
|
96
|
+
position: Math.max(0, Math.min(100, status.current_pos)),
|
|
97
|
+
...(motion ? { motion } : {}),
|
|
98
|
+
observedAt
|
|
99
|
+
} });
|
|
100
|
+
trace("status", reply ? "read" : "changed", null, { position: status.current_pos, state: status.state ?? null });
|
|
101
|
+
}
|
|
102
|
+
const command = msg.cover_command;
|
|
103
|
+
if (isObject(command)) {
|
|
104
|
+
const operation = String(command.operation);
|
|
105
|
+
const generation = Number(command.generation);
|
|
106
|
+
if (operation === "set_position") {
|
|
107
|
+
const position = Number(command.position);
|
|
108
|
+
if (!Number.isFinite(position))
|
|
109
|
+
throw new Error("set_position needs a position");
|
|
110
|
+
state.command = { operation, generation };
|
|
111
|
+
if (state.status && (state.status.pos_control === false || (state.complete && !Object.hasOwn(state.status, "current_pos")))) {
|
|
112
|
+
// The device refuses a position on an uncalibrated cover, so say so
|
|
113
|
+
// now rather than send it.
|
|
114
|
+
readings.push({ cover: { kind: "command_result", generation, ok: false, reason: "not-calibrated" } });
|
|
115
|
+
state.command = null;
|
|
116
|
+
trace("command", "refused", "not-calibrated", { position });
|
|
117
|
+
}
|
|
118
|
+
else {
|
|
119
|
+
publications.push({ topic: topics.command, payload: `pos,${Math.round(Math.max(0, Math.min(100, position)))}` });
|
|
120
|
+
trace("command", "sent", null, { operation, position });
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
else if (operation === "stop" || operation === "observe") {
|
|
124
|
+
state.command = { operation, generation };
|
|
125
|
+
// observe asks for the device's full status, so the reading is fresh.
|
|
126
|
+
publications.push({ topic: topics.command, payload: operation === "stop" ? "stop" : "status_update" });
|
|
127
|
+
trace("command", "sent", null, { operation });
|
|
128
|
+
}
|
|
129
|
+
else {
|
|
130
|
+
trace("command", "ignored", "unknown-operation", { operation });
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
else if (msg.topic === topics.status) {
|
|
134
|
+
const status = parsed(msg.payload);
|
|
135
|
+
if (!isObject(status))
|
|
136
|
+
throw new Error(`${topics.status} must carry the cover's status object`);
|
|
137
|
+
state.status = { ...status };
|
|
138
|
+
state.complete = true;
|
|
139
|
+
read(now.toISOString(), true);
|
|
140
|
+
}
|
|
141
|
+
else if (msg.topic === topics.events) {
|
|
142
|
+
const received = parsed(msg.payload);
|
|
143
|
+
const frame = isObject(received) && (received.method === "NotifyStatus" || received.method === "NotifyFullStatus") &&
|
|
144
|
+
isObject(received.params) ? { method: received.method, params: received.params } : null;
|
|
145
|
+
const change = frame?.params[topics.component];
|
|
146
|
+
if (!frame || !isObject(change)) {
|
|
147
|
+
trace("event", "ignored", "not-this-cover");
|
|
148
|
+
}
|
|
149
|
+
else {
|
|
150
|
+
// A partial status: a key reported as null has gone.
|
|
151
|
+
const status = frame.method === "NotifyFullStatus" ? {} : { ...state.status };
|
|
152
|
+
for (const [key, value] of Object.entries(change)) {
|
|
153
|
+
if (value === null && key !== "current_pos")
|
|
154
|
+
delete status[key];
|
|
155
|
+
else
|
|
156
|
+
status[key] = value;
|
|
157
|
+
}
|
|
158
|
+
state.status = status;
|
|
159
|
+
if (frame.method === "NotifyFullStatus")
|
|
160
|
+
state.complete = true;
|
|
161
|
+
// Stamped when received, as a status reply is, never with the
|
|
162
|
+
// device's own ts: a Shelly whose clock is behind (no time server, a
|
|
163
|
+
// drifting clock) would have every change dropped as older than the
|
|
164
|
+
// last reading. MQTT's delay is far below what the controller resolves.
|
|
165
|
+
const observedAt = now.toISOString();
|
|
166
|
+
// Only a change of position or movement is a new reading; a power or
|
|
167
|
+
// temperature update mid-travel isn't.
|
|
168
|
+
if (frame.method === "NotifyFullStatus" || Object.hasOwn(change, "current_pos") || Object.hasOwn(change, "state") ||
|
|
169
|
+
Object.hasOwn(change, "pos_control")) {
|
|
170
|
+
read(observedAt, false);
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
else if (msg.topic === topics.online) {
|
|
175
|
+
const online = parsed(msg.payload);
|
|
176
|
+
if (online !== true && online !== false)
|
|
177
|
+
throw new Error(`${topics.online} must be true or false`);
|
|
178
|
+
readings.push({ cover: { kind: "availability", online } });
|
|
179
|
+
trace("availability", online ? "online" : "offline");
|
|
180
|
+
// Back online, its position may have changed while it was away.
|
|
181
|
+
if (online)
|
|
182
|
+
requestStatus("online");
|
|
183
|
+
}
|
|
184
|
+
else {
|
|
185
|
+
trace("message", "ignored", "unrelated-message", { topic: typeof msg.topic === "string" ? msg.topic : null });
|
|
186
|
+
}
|
|
187
|
+
const status = state.status;
|
|
188
|
+
const text = !status ? "waiting for the device"
|
|
189
|
+
: status.pos_control === false || (state.complete && !Object.hasOwn(status, "current_pos")) ? "not calibrated"
|
|
190
|
+
: `${typeof status.state === "string" ? status.state : "unknown"} · ${status.current_pos ?? "?"}%`;
|
|
191
|
+
return { state, publications, readings, traces, status: { text, calibrated: Boolean(status) && status.pos_control !== false && (!state.complete || Object.hasOwn(status, "current_pos")) } };
|
|
192
|
+
}
|
|
193
|
+
//# sourceMappingURL=shelly-cover.js.map
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { type Carried } from "./adopted-request";
|
|
1
2
|
import type { TraceEntry } from "./bounded-trace";
|
|
2
3
|
import { type TargetStateEvent } from "./target-state";
|
|
3
4
|
export type SourceAction = "activate" | "deactivate";
|
|
@@ -55,6 +56,7 @@ export interface SourceState {
|
|
|
55
56
|
fallbackUsed: boolean;
|
|
56
57
|
refreshAsked: boolean;
|
|
57
58
|
adoptedGeneration: number | null;
|
|
59
|
+
carried?: Carried | null;
|
|
58
60
|
}
|
|
59
61
|
export type SourceEvent = {
|
|
60
62
|
kind: "intent";
|
|
@@ -112,7 +112,8 @@ function initialState(saved = {}, sources = {}) {
|
|
|
112
112
|
: 0,
|
|
113
113
|
fallbackUsed: saved.fallbackUsed === true,
|
|
114
114
|
refreshAsked: saved.refreshAsked === true,
|
|
115
|
-
adoptedGeneration: (0, adopted_request_1.savedAdoptedGeneration)(saved)
|
|
115
|
+
adoptedGeneration: (0, adopted_request_1.savedAdoptedGeneration)(saved),
|
|
116
|
+
carried: (0, adopted_request_1.savedCarried)(saved)
|
|
116
117
|
};
|
|
117
118
|
}
|
|
118
119
|
function commandFor(state, operation, attempt = 1) {
|
|
@@ -362,7 +363,7 @@ function transitionSource(saved, msg, suppliedConfig = {}, nowValue = new Date()
|
|
|
362
363
|
const parsedAt = (0, observation_time_1.parseObservedAt)(event.observedAt);
|
|
363
364
|
if (parsedAt === null || typeof event.observedAt !== "string")
|
|
364
365
|
throw new Error("source observedAt must be an ISO timestamp");
|
|
365
|
-
if (!state.desired ||
|
|
366
|
+
if (!state.desired || !(0, adopted_request_1.answersRequest)(state.carried, state.generation, event.generation) || event.sourceId !== state.desired.sourceId) {
|
|
366
367
|
trace("observation", "ignored", "stale-generation", { generation: event.generation });
|
|
367
368
|
return;
|
|
368
369
|
}
|
|
@@ -396,7 +397,7 @@ function transitionSource(saved, msg, suppliedConfig = {}, nowValue = new Date()
|
|
|
396
397
|
reconcileObservation();
|
|
397
398
|
}
|
|
398
399
|
function acceptCommandResult(event) {
|
|
399
|
-
if (!state.desired ||
|
|
400
|
+
if (!state.desired || !(0, adopted_request_1.answersRequest)(state.carried, state.generation, event.generation) ||
|
|
400
401
|
event.sourceId !== state.desired.sourceId || !state.pending) {
|
|
401
402
|
trace("command-result", "ignored", "stale-generation");
|
|
402
403
|
return;
|
|
@@ -443,14 +444,34 @@ function transitionSource(saved, msg, suppliedConfig = {}, nowValue = new Date()
|
|
|
443
444
|
// A refresh with the activity's wanted values, reaching a node with
|
|
444
445
|
// nothing desired (it started with nothing saved): adopt them, and observe
|
|
445
446
|
// below as at startup (see adopted-request.ts).
|
|
447
|
+
let replacedRequest = false;
|
|
446
448
|
const adoptable = event.kind === "refresh" ? (0, adopted_request_1.adoptableRefresh)(event, state) : null;
|
|
447
|
-
|
|
449
|
+
// The same wanted values as the work in hand: carry on under the new
|
|
450
|
+
// request rather than adopting it read-only (see carryOn).
|
|
451
|
+
const carried = event.kind === "refresh" && (0, adopted_request_1.carryOn)(state, adoptable, event.action === state.desired?.action && (event.sourceId || null) === (state.desired?.sourceId ?? null), state.classification === "manual_override" || state.phase === "failed");
|
|
452
|
+
if (carried)
|
|
453
|
+
trace("refresh", "continued", "same-request", { generation: state.generation });
|
|
454
|
+
if (!carried && adoptable && event.kind === "refresh" && typeof event.action === "string" && Object.hasOwn(ACTIONS, event.action) &&
|
|
448
455
|
((typeof event.sourceId === "string" && Object.hasOwn(config.sources, event.sourceId)) ||
|
|
449
456
|
(!event.sourceId && event.action === "deactivate"))) {
|
|
457
|
+
const reason = (0, adopted_request_1.adoptionReason)(state.desired);
|
|
458
|
+
// Replacing a request the theatre already counts as ready, it answers
|
|
459
|
+
// from its reading, not with an interim state the theatre would take as
|
|
460
|
+
// drift.
|
|
461
|
+
replacedRequest = reason === "newer-generation";
|
|
462
|
+
// A request it replaces takes its classification, failure and work with
|
|
463
|
+
// it, and the source is read afresh below rather than kept ready.
|
|
464
|
+
state.failure = null;
|
|
465
|
+
state.classification = null;
|
|
466
|
+
state.lastAttempt = 0;
|
|
467
|
+
state.fallbackUsed = false;
|
|
468
|
+
state.pending = null;
|
|
469
|
+
if (reason === "newer-generation")
|
|
470
|
+
state.phase = "idle";
|
|
450
471
|
state.desired = { ...adoptable, sourceId: event.sourceId || null, action: event.action };
|
|
451
472
|
state.generation = adoptable.generation;
|
|
452
473
|
state.adoptedGeneration = adoptable.generation;
|
|
453
|
-
trace("refresh", "adopted",
|
|
474
|
+
trace("refresh", "adopted", reason, { sourceId: event.sourceId || null, action: event.action, generation: adoptable.generation });
|
|
454
475
|
}
|
|
455
476
|
if (event.kind === "intent") {
|
|
456
477
|
acceptIntent(event);
|
|
@@ -511,7 +532,7 @@ function transitionSource(saved, msg, suppliedConfig = {}, nowValue = new Date()
|
|
|
511
532
|
state.phase = "manual_override";
|
|
512
533
|
trace("command", "withheld", adopted_request_1.ADOPTED_MISMATCH);
|
|
513
534
|
}
|
|
514
|
-
const outputEvent = event.kind === "refresh" && state.refreshAsked ? null : targetEvent(state, now, config.sources);
|
|
535
|
+
const outputEvent = (event.kind === "refresh" && state.refreshAsked) || replacedRequest ? null : targetEvent(state, now, config.sources);
|
|
515
536
|
if (outputEvent)
|
|
516
537
|
events.push(outputEvent);
|
|
517
538
|
return { state, commands, events, traces, status: statusFor(state) };
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import type { TheatreTarget } from "./theatre-profiles";
|
|
2
|
+
import type { TheatreMetrics, TheatreSnapshot } from "./theatre-state";
|
|
3
|
+
import { type TheatreSloAssessment, type TheatreSloConfig, type TheatreSloState } from "./theatre-slo";
|
|
4
|
+
export interface ContractRequest {
|
|
5
|
+
requestId: string;
|
|
6
|
+
generation: number;
|
|
7
|
+
activity: string;
|
|
8
|
+
requestedAt: string;
|
|
9
|
+
}
|
|
10
|
+
export interface TheatreRequestState {
|
|
11
|
+
generation: number;
|
|
12
|
+
lastRequest: ContractRequest | null;
|
|
13
|
+
slo: TheatreSloState;
|
|
14
|
+
}
|
|
15
|
+
export type TheatreStateSnapshot = Omit<TheatreSnapshot, "availability" | "targets" | "failure" | "degraded" | "metrics"> & {
|
|
16
|
+
availability: string;
|
|
17
|
+
targets: Partial<Record<TheatreTarget, Record<string, unknown> | null>>;
|
|
18
|
+
failure: Record<string, unknown> | null;
|
|
19
|
+
degraded: Array<Record<string, unknown>>;
|
|
20
|
+
metrics?: TheatreMetrics;
|
|
21
|
+
};
|
|
22
|
+
export declare function createTheatreRequestState(saved?: Partial<TheatreRequestState>): TheatreRequestState;
|
|
23
|
+
export declare function takeTheatreRequest(state: TheatreRequestState, request: {
|
|
24
|
+
requestId?: unknown;
|
|
25
|
+
activity?: unknown;
|
|
26
|
+
requestedAt?: unknown;
|
|
27
|
+
}, activities: string[], now: Date): "accepted" | "duplicate";
|
|
28
|
+
export declare function asTheatreSnapshot(value: unknown): TheatreStateSnapshot;
|
|
29
|
+
export declare function takeTheatreSnapshot(state: TheatreRequestState, event: TheatreStateSnapshot, sloConfig: TheatreSloConfig, now: Date): TheatreSloAssessment | null;
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.createTheatreRequestState = createTheatreRequestState;
|
|
4
|
+
exports.takeTheatreRequest = takeTheatreRequest;
|
|
5
|
+
exports.asTheatreSnapshot = asTheatreSnapshot;
|
|
6
|
+
exports.takeTheatreSnapshot = takeTheatreSnapshot;
|
|
7
|
+
const theatre_slo_1 = require("./theatre-slo");
|
|
8
|
+
const topic_names_1 = require("./topic-names");
|
|
9
|
+
function createTheatreRequestState(saved = {}) {
|
|
10
|
+
const lastRequest = saved.lastRequest && typeof saved.lastRequest === "object"
|
|
11
|
+
? { ...saved.lastRequest }
|
|
12
|
+
: null;
|
|
13
|
+
return {
|
|
14
|
+
generation: Number.isInteger(saved.generation) && (saved.generation ?? -1) >= 0
|
|
15
|
+
? saved.generation
|
|
16
|
+
: 0,
|
|
17
|
+
lastRequest,
|
|
18
|
+
slo: {
|
|
19
|
+
samples: Array.isArray(saved.slo?.samples) ? saved.slo.samples.map((sample) => ({ ...sample })) : []
|
|
20
|
+
}
|
|
21
|
+
};
|
|
22
|
+
}
|
|
23
|
+
function validTimestamp(value, fallback) {
|
|
24
|
+
const timestamp = value === undefined ? fallback : String(value);
|
|
25
|
+
if (!Number.isFinite(Date.parse(timestamp)))
|
|
26
|
+
throw new Error("requestedAt must be an ISO timestamp");
|
|
27
|
+
return timestamp;
|
|
28
|
+
}
|
|
29
|
+
// An activity request: a new one takes the next generation; the same
|
|
30
|
+
// request again is passed on as it was. An invalid one is an error.
|
|
31
|
+
function takeTheatreRequest(state, request, activities, now) {
|
|
32
|
+
const requestId = String(request.requestId || "");
|
|
33
|
+
const activity = String(request.activity || "");
|
|
34
|
+
if (!topic_names_1.REQUEST_ID.test(requestId))
|
|
35
|
+
throw new Error("requestId is invalid");
|
|
36
|
+
if (!activities.includes(activity))
|
|
37
|
+
throw new Error("unknown theatre activity");
|
|
38
|
+
const requestedAt = validTimestamp(request.requestedAt, now.toISOString());
|
|
39
|
+
if (state.lastRequest?.requestId === requestId) {
|
|
40
|
+
if (state.lastRequest.activity !== activity) {
|
|
41
|
+
throw new Error("requestId cannot be reused for another activity");
|
|
42
|
+
}
|
|
43
|
+
return "duplicate";
|
|
44
|
+
}
|
|
45
|
+
state.generation += 1;
|
|
46
|
+
state.lastRequest = { requestId, generation: state.generation, activity, requestedAt };
|
|
47
|
+
return "accepted";
|
|
48
|
+
}
|
|
49
|
+
// The activity controller's snapshot, checked for the fields the contract
|
|
50
|
+
// relies on.
|
|
51
|
+
function asTheatreSnapshot(value) {
|
|
52
|
+
if (!value || typeof value !== "object" || Array.isArray(value)) {
|
|
53
|
+
throw new Error("theatre state must be an object");
|
|
54
|
+
}
|
|
55
|
+
const event = value;
|
|
56
|
+
if (event.kind !== "state")
|
|
57
|
+
throw new Error("theatre state kind must be state");
|
|
58
|
+
if (!Number.isInteger(event.generation) || (event.generation ?? -1) < 0) {
|
|
59
|
+
throw new Error("theatre state generation must be a non-negative integer");
|
|
60
|
+
}
|
|
61
|
+
if (typeof event.currentActivity !== "string" || typeof event.phase !== "string" ||
|
|
62
|
+
typeof event.availability !== "string") {
|
|
63
|
+
throw new Error("theatre state must define currentActivity, phase and availability");
|
|
64
|
+
}
|
|
65
|
+
if (!event.targets || typeof event.targets !== "object" || Array.isArray(event.targets)) {
|
|
66
|
+
throw new Error("theatre state targets must be an object");
|
|
67
|
+
}
|
|
68
|
+
return event;
|
|
69
|
+
}
|
|
70
|
+
// A snapshot older than the request in hand is stale; a current one
|
|
71
|
+
// becomes the request in hand and a sample for the SLO, whose assessment
|
|
72
|
+
// it returns.
|
|
73
|
+
function takeTheatreSnapshot(state, event, sloConfig, now) {
|
|
74
|
+
if (event.generation < state.generation)
|
|
75
|
+
return null;
|
|
76
|
+
state.generation = event.generation;
|
|
77
|
+
if (event.requestId && event.desiredActivity) {
|
|
78
|
+
state.lastRequest = {
|
|
79
|
+
requestId: event.requestId,
|
|
80
|
+
generation: event.generation,
|
|
81
|
+
activity: event.desiredActivity,
|
|
82
|
+
requestedAt: event.requestedAt || event.transitionStartedAt || now.toISOString()
|
|
83
|
+
};
|
|
84
|
+
}
|
|
85
|
+
const slo = (0, theatre_slo_1.evaluateTheatreSlo)(state.slo, event, sloConfig, now);
|
|
86
|
+
state.slo = slo.state;
|
|
87
|
+
return slo.assessment;
|
|
88
|
+
}
|
|
89
|
+
//# sourceMappingURL=theatre-requests.js.map
|
|
@@ -1,8 +1,9 @@
|
|
|
1
|
-
import { type
|
|
2
|
-
import type
|
|
3
|
-
import { type TheatreSloConfig, type TheatreSloState } from "./theatre-slo";
|
|
1
|
+
import { type TheatreSloConfig } from "./theatre-slo";
|
|
2
|
+
import { type TheatreRequestState, type TheatreStateSnapshot } from "./theatre-requests";
|
|
4
3
|
import type { TraceEntry } from "./bounded-trace";
|
|
5
4
|
import { type IdScheme } from "./ha-discovery";
|
|
5
|
+
export type { ContractRequest, TheatreStateSnapshot } from "./theatre-requests";
|
|
6
|
+
export type TheatreStateContractState = TheatreRequestState;
|
|
6
7
|
export interface TheatreStateContractConfig {
|
|
7
8
|
nodeId?: string;
|
|
8
9
|
idScheme?: IdScheme;
|
|
@@ -14,24 +15,6 @@ export interface TheatreStateContractConfig {
|
|
|
14
15
|
slo: TheatreSloConfig;
|
|
15
16
|
discovery?: boolean;
|
|
16
17
|
}
|
|
17
|
-
export interface ContractRequest {
|
|
18
|
-
requestId: string;
|
|
19
|
-
generation: number;
|
|
20
|
-
activity: string;
|
|
21
|
-
requestedAt: string;
|
|
22
|
-
}
|
|
23
|
-
export interface TheatreStateContractState {
|
|
24
|
-
generation: number;
|
|
25
|
-
lastRequest: ContractRequest | null;
|
|
26
|
-
slo: TheatreSloState;
|
|
27
|
-
}
|
|
28
|
-
export type TheatreStateSnapshot = Omit<TheatreSnapshot, "availability" | "targets" | "failure" | "degraded" | "metrics"> & {
|
|
29
|
-
availability: string;
|
|
30
|
-
targets: Partial<Record<TheatreTarget, Record<string, unknown> | null>>;
|
|
31
|
-
failure: Record<string, unknown> | null;
|
|
32
|
-
degraded: Array<Record<string, unknown>>;
|
|
33
|
-
metrics?: TheatreMetrics;
|
|
34
|
-
};
|
|
35
18
|
export interface TheatreStateContractMessage {
|
|
36
19
|
topic?: string;
|
|
37
20
|
payload?: unknown;
|
|
@@ -60,7 +43,7 @@ export declare function theatreRetainedTopics(config: TheatreStateContractConfig
|
|
|
60
43
|
export declare function theatreStartingPublications(config: TheatreStateContractConfig): ContractPublication[];
|
|
61
44
|
export declare function theatreStoppingPublications(config: TheatreStateContractConfig): ContractPublication[];
|
|
62
45
|
export declare function transitionTheatreStateContract(saved: Partial<TheatreStateContractState> | undefined, msg: TheatreStateContractMessage, suppliedConfig: Partial<TheatreStateContractConfig>, nowValue?: Date | string | number): {
|
|
63
|
-
state:
|
|
46
|
+
state: TheatreRequestState;
|
|
64
47
|
requests: ContractRequestOutput[];
|
|
65
48
|
publications: ContractPublication[];
|
|
66
49
|
traces: TraceEntry<Record<string, unknown>>[];
|