@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,62 @@
|
|
|
1
|
+
export interface WallInputConfig {
|
|
2
|
+
switchWindowMs: number;
|
|
3
|
+
switchFreshnessMs: number;
|
|
4
|
+
}
|
|
5
|
+
export interface WallInputState {
|
|
6
|
+
baselineKnown: boolean;
|
|
7
|
+
value: boolean;
|
|
8
|
+
observedAt: number | null;
|
|
9
|
+
sessionObserved: boolean;
|
|
10
|
+
available: boolean | null;
|
|
11
|
+
rebaselineRequired: boolean;
|
|
12
|
+
generation: number;
|
|
13
|
+
edgeAt: number;
|
|
14
|
+
timerDueAt: number | null;
|
|
15
|
+
}
|
|
16
|
+
export type WallInputInput = {
|
|
17
|
+
kind: "startup";
|
|
18
|
+
} | {
|
|
19
|
+
kind: "observation";
|
|
20
|
+
payload?: unknown;
|
|
21
|
+
retain?: boolean;
|
|
22
|
+
baseline?: boolean;
|
|
23
|
+
observedAt?: unknown;
|
|
24
|
+
} | {
|
|
25
|
+
kind: "availability";
|
|
26
|
+
payload?: unknown;
|
|
27
|
+
} | {
|
|
28
|
+
kind: "timer";
|
|
29
|
+
generation?: number;
|
|
30
|
+
edgeAt?: number;
|
|
31
|
+
};
|
|
32
|
+
export type WallInputEffect = {
|
|
33
|
+
kind: "cancel-switch-timer";
|
|
34
|
+
} | {
|
|
35
|
+
kind: "schedule-switch-timer";
|
|
36
|
+
delayMs: number;
|
|
37
|
+
generation: number;
|
|
38
|
+
switchAt: number;
|
|
39
|
+
};
|
|
40
|
+
export interface WallInputTrace {
|
|
41
|
+
event: string;
|
|
42
|
+
outcome: string;
|
|
43
|
+
reason: string | null;
|
|
44
|
+
details: Record<string, unknown>;
|
|
45
|
+
}
|
|
46
|
+
export interface WallInputStatus {
|
|
47
|
+
phase: "awaiting-baseline" | "unavailable" | "stale" | "fresh";
|
|
48
|
+
reason: "no-baseline" | "reported-unavailable" | "startup-unconfirmed" | "timestamp-unknown" | "expired" | null;
|
|
49
|
+
baselineKnown: boolean;
|
|
50
|
+
available: boolean | null;
|
|
51
|
+
observedAt: number | null;
|
|
52
|
+
ageMs: number | null;
|
|
53
|
+
}
|
|
54
|
+
export declare function createWallInputState(saved?: Partial<WallInputState>): WallInputState;
|
|
55
|
+
export declare function wallInputStatus(saved: Partial<WallInputState> | undefined, freshnessMs: number, now?: number): WallInputStatus;
|
|
56
|
+
export declare function transitionWallInput(saved: Partial<WallInputState> | undefined, input: WallInputInput, config: WallInputConfig, now?: number): {
|
|
57
|
+
state: WallInputState;
|
|
58
|
+
effects: WallInputEffect[];
|
|
59
|
+
traces: WallInputTrace[];
|
|
60
|
+
gesture: "double" | "single" | null;
|
|
61
|
+
status: WallInputStatus;
|
|
62
|
+
};
|
|
@@ -0,0 +1,198 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.createWallInputState = createWallInputState;
|
|
4
|
+
exports.wallInputStatus = wallInputStatus;
|
|
5
|
+
exports.transitionWallInput = transitionWallInput;
|
|
6
|
+
const observation_time_1 = require("./observation-time");
|
|
7
|
+
function createWallInputState(saved = {}) {
|
|
8
|
+
return {
|
|
9
|
+
baselineKnown: saved.baselineKnown === true,
|
|
10
|
+
value: saved.value === true,
|
|
11
|
+
observedAt: typeof saved.observedAt === "number" && Number.isFinite(saved.observedAt)
|
|
12
|
+
? saved.observedAt
|
|
13
|
+
: null,
|
|
14
|
+
sessionObserved: saved.sessionObserved === true,
|
|
15
|
+
available: typeof saved.available === "boolean" ? saved.available : null,
|
|
16
|
+
rebaselineRequired: saved.rebaselineRequired === true,
|
|
17
|
+
generation: Number(saved.generation) || 0,
|
|
18
|
+
edgeAt: Number(saved.edgeAt) || 0,
|
|
19
|
+
timerDueAt: typeof saved.timerDueAt === "number" ? saved.timerDueAt : null
|
|
20
|
+
};
|
|
21
|
+
}
|
|
22
|
+
function observationTimestamp(input, now) {
|
|
23
|
+
const payloadTimestamp = input.payload && typeof input.payload === "object"
|
|
24
|
+
? input.payload.observedAt
|
|
25
|
+
: undefined;
|
|
26
|
+
const parsed = (0, observation_time_1.parseObservedAt)(input.observedAt ?? payloadTimestamp);
|
|
27
|
+
if (parsed !== null)
|
|
28
|
+
return parsed;
|
|
29
|
+
return input.retain === true ? null : now;
|
|
30
|
+
}
|
|
31
|
+
function availability(value) {
|
|
32
|
+
if (value === true || value === "online" || value === "available" || value === "on")
|
|
33
|
+
return true;
|
|
34
|
+
if (value === false || value === "offline" || value === "unavailable" || value === "off")
|
|
35
|
+
return false;
|
|
36
|
+
return null;
|
|
37
|
+
}
|
|
38
|
+
function wallInputStatus(saved, freshnessMs, now = Date.now()) {
|
|
39
|
+
const state = createWallInputState(saved);
|
|
40
|
+
const ageMs = state.observedAt === null ? null : Math.max(0, now - state.observedAt);
|
|
41
|
+
let phase;
|
|
42
|
+
let reason;
|
|
43
|
+
if (state.available === false) {
|
|
44
|
+
phase = "unavailable";
|
|
45
|
+
reason = "reported-unavailable";
|
|
46
|
+
}
|
|
47
|
+
else if (!state.baselineKnown) {
|
|
48
|
+
phase = "awaiting-baseline";
|
|
49
|
+
reason = "no-baseline";
|
|
50
|
+
}
|
|
51
|
+
else if (!state.sessionObserved) {
|
|
52
|
+
phase = "stale";
|
|
53
|
+
reason = "startup-unconfirmed";
|
|
54
|
+
}
|
|
55
|
+
else if (ageMs === null) {
|
|
56
|
+
phase = "stale";
|
|
57
|
+
reason = "timestamp-unknown";
|
|
58
|
+
}
|
|
59
|
+
else if (ageMs > freshnessMs) {
|
|
60
|
+
phase = "stale";
|
|
61
|
+
reason = "expired";
|
|
62
|
+
}
|
|
63
|
+
else {
|
|
64
|
+
phase = "fresh";
|
|
65
|
+
reason = null;
|
|
66
|
+
}
|
|
67
|
+
return {
|
|
68
|
+
phase,
|
|
69
|
+
reason,
|
|
70
|
+
baselineKnown: state.baselineKnown,
|
|
71
|
+
available: state.available,
|
|
72
|
+
observedAt: state.observedAt,
|
|
73
|
+
ageMs
|
|
74
|
+
};
|
|
75
|
+
}
|
|
76
|
+
function transitionWallInput(saved, input, config, now = Date.now()) {
|
|
77
|
+
const state = createWallInputState(saved);
|
|
78
|
+
const effects = [];
|
|
79
|
+
const traces = [];
|
|
80
|
+
let gesture = null;
|
|
81
|
+
function trace(event, outcome, reason, details = {}) {
|
|
82
|
+
traces.push({ event, outcome, reason, details });
|
|
83
|
+
}
|
|
84
|
+
function cancelTimer(reason) {
|
|
85
|
+
const wasActive = Number(state.timerDueAt) > now;
|
|
86
|
+
state.generation += 1;
|
|
87
|
+
state.timerDueAt = null;
|
|
88
|
+
effects.push({ kind: "cancel-switch-timer" });
|
|
89
|
+
if (wasActive && reason)
|
|
90
|
+
trace("switch-timer", "cancelled", reason);
|
|
91
|
+
}
|
|
92
|
+
if (input.kind === "startup") {
|
|
93
|
+
state.sessionObserved = false;
|
|
94
|
+
state.available = null;
|
|
95
|
+
state.edgeAt = 0;
|
|
96
|
+
cancelTimer("startup");
|
|
97
|
+
trace("switch-startup", state.baselineKnown ? "baseline-preserved" : "awaiting-baseline", null, {
|
|
98
|
+
observedAt: state.observedAt
|
|
99
|
+
});
|
|
100
|
+
}
|
|
101
|
+
else if (input.kind === "availability") {
|
|
102
|
+
const nextAvailability = availability(input.payload);
|
|
103
|
+
if (nextAvailability === null) {
|
|
104
|
+
trace("switch-availability", "ignored", "invalid-availability");
|
|
105
|
+
}
|
|
106
|
+
else {
|
|
107
|
+
const changed = state.available !== nextAvailability;
|
|
108
|
+
state.available = nextAvailability;
|
|
109
|
+
if (!nextAvailability) {
|
|
110
|
+
state.baselineKnown = false;
|
|
111
|
+
state.sessionObserved = false;
|
|
112
|
+
state.rebaselineRequired = true;
|
|
113
|
+
state.edgeAt = 0;
|
|
114
|
+
cancelTimer("input-unavailable");
|
|
115
|
+
}
|
|
116
|
+
trace("switch-availability", changed ? "changed" : "unchanged", null, { available: nextAvailability });
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
else if (input.kind === "observation") {
|
|
120
|
+
const nextState = input.payload && typeof input.payload === "object"
|
|
121
|
+
? input.payload.state
|
|
122
|
+
: undefined;
|
|
123
|
+
if (typeof nextState !== "boolean") {
|
|
124
|
+
trace("switch", "ignored", "invalid-state");
|
|
125
|
+
}
|
|
126
|
+
else {
|
|
127
|
+
const firstState = !state.baselineKnown;
|
|
128
|
+
const changed = firstState || nextState !== state.value;
|
|
129
|
+
const shouldBaseline = input.retain === true ||
|
|
130
|
+
input.baseline === true ||
|
|
131
|
+
state.rebaselineRequired;
|
|
132
|
+
state.available = true;
|
|
133
|
+
state.sessionObserved = true;
|
|
134
|
+
const reportedAt = observationTimestamp(input, now);
|
|
135
|
+
// A time beyond the skew limit counts as observed now.
|
|
136
|
+
state.observedAt = reportedAt !== null && reportedAt > now + observation_time_1.CLOCK_SKEW_MS ? now : reportedAt;
|
|
137
|
+
if (state.observedAt !== reportedAt) {
|
|
138
|
+
trace("switch", "adjusted", "clock-skew", { reported: reportedAt, usedAt: now });
|
|
139
|
+
}
|
|
140
|
+
state.baselineKnown = true;
|
|
141
|
+
state.rebaselineRequired = false;
|
|
142
|
+
state.value = nextState;
|
|
143
|
+
if (shouldBaseline) {
|
|
144
|
+
if (state.timerDueAt !== null || state.edgeAt) {
|
|
145
|
+
state.edgeAt = 0;
|
|
146
|
+
cancelTimer(input.retain === true ? "retained-baseline" : "explicit-baseline");
|
|
147
|
+
}
|
|
148
|
+
trace("switch", "baseline", input.retain === true ? "retained-observation" : "explicit-observation", {
|
|
149
|
+
changed,
|
|
150
|
+
observedAt: state.observedAt
|
|
151
|
+
});
|
|
152
|
+
}
|
|
153
|
+
else if (!changed) {
|
|
154
|
+
trace("switch", "refreshed", "unchanged", { observedAt: state.observedAt });
|
|
155
|
+
}
|
|
156
|
+
else if (state.edgeAt && now - state.edgeAt <= config.switchWindowMs) {
|
|
157
|
+
state.edgeAt = 0;
|
|
158
|
+
cancelTimer("double-edge");
|
|
159
|
+
gesture = "double";
|
|
160
|
+
trace("switch", "gesture", null, { gesture });
|
|
161
|
+
}
|
|
162
|
+
else {
|
|
163
|
+
cancelTimer("new-edge");
|
|
164
|
+
state.edgeAt = now;
|
|
165
|
+
state.timerDueAt = now + config.switchWindowMs;
|
|
166
|
+
effects.push({
|
|
167
|
+
kind: "schedule-switch-timer",
|
|
168
|
+
delayMs: config.switchWindowMs,
|
|
169
|
+
generation: state.generation,
|
|
170
|
+
switchAt: now
|
|
171
|
+
});
|
|
172
|
+
trace("switch-timer", "scheduled", null, { dueAt: new Date(state.timerDueAt).toISOString() });
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
else {
|
|
177
|
+
const current = input.generation === state.generation &&
|
|
178
|
+
input.edgeAt === state.edgeAt &&
|
|
179
|
+
state.timerDueAt !== null;
|
|
180
|
+
if (!current) {
|
|
181
|
+
trace("switch-timer", "ignored", "stale-generation", { generation: input.generation });
|
|
182
|
+
}
|
|
183
|
+
else {
|
|
184
|
+
state.edgeAt = 0;
|
|
185
|
+
state.timerDueAt = null;
|
|
186
|
+
gesture = "single";
|
|
187
|
+
trace("switch", "gesture", null, { gesture });
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
return {
|
|
191
|
+
state,
|
|
192
|
+
effects,
|
|
193
|
+
traces,
|
|
194
|
+
gesture,
|
|
195
|
+
status: wallInputStatus(state, config.switchFreshnessMs, now)
|
|
196
|
+
};
|
|
197
|
+
}
|
|
198
|
+
//# sourceMappingURL=wall-input.js.map
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
<script type="text/javascript">
|
|
2
|
+
RED.nodes.registerType("home-activity-lighting-controller", {
|
|
3
|
+
category: "home automation",
|
|
4
|
+
color: "#c7d9f1",
|
|
5
|
+
defaults: {
|
|
6
|
+
name: {value: ""},
|
|
7
|
+
area: {value: "lounge", required: true},
|
|
8
|
+
profiles: {value: "{}", required: true},
|
|
9
|
+
owner: {value: "activity"},
|
|
10
|
+
confirmationTimeoutMs: {value: 15000, required: true, validate: RED.validators.number()},
|
|
11
|
+
maxAttempts: {value: 2, required: true, validate: RED.validators.number()}
|
|
12
|
+
},
|
|
13
|
+
inputs: 1,
|
|
14
|
+
outputs: 2,
|
|
15
|
+
outputLabels: ["room-lighting commands", "theatre target state"],
|
|
16
|
+
icon: "font-awesome/fa-lightbulb-o",
|
|
17
|
+
label: function() { return this.name || "activity lighting"; }
|
|
18
|
+
});
|
|
19
|
+
</script>
|
|
20
|
+
|
|
21
|
+
<script type="text/html" data-template-name="home-activity-lighting-controller">
|
|
22
|
+
<div class="form-row"><label for="node-input-name">Name</label><input type="text" id="node-input-name"></div>
|
|
23
|
+
<div class="form-row"><label for="node-input-area">Area</label><input type="text" id="node-input-area"></div>
|
|
24
|
+
<div class="form-row"><label for="node-input-profiles">Profiles JSON</label><input type="text" id="node-input-profiles"></div>
|
|
25
|
+
<div class="form-row"><label for="node-input-owner">Owner</label><input type="text" id="node-input-owner" placeholder="activity"></div>
|
|
26
|
+
<div class="form-row"><label for="node-input-confirmationTimeoutMs">Confirm ms</label><input type="number" id="node-input-confirmationTimeoutMs"></div>
|
|
27
|
+
<div class="form-row"><label for="node-input-maxAttempts">Attempts</label><input type="number" id="node-input-maxAttempts"></div>
|
|
28
|
+
</script>
|
|
29
|
+
|
|
30
|
+
<script type="text/html" data-help-name="home-activity-lighting-controller">
|
|
31
|
+
<p>Applies configuration-owned activity styles only to room lights that are already on and not manually owned.</p>
|
|
32
|
+
<p>Input <code>msg.activityLighting</code> carries generation-scoped intent, observation, refresh, result or tick events. Output 1 emits standard <code>msg.lighting</code> intents for a room controller. Output 2 emits theatre <code>target_state</code> evidence. Room-light failure is optional degradation, not activity success.</p>
|
|
33
|
+
<p>While a profile is active, its lights are overridden in the room controller under <b>Owner</b>
|
|
34
|
+
(default <code>activity</code>), so circadian refresh leaves the activity style alone. When a new profile
|
|
35
|
+
no longer includes a light, including a switch to an off profile with no targets, the node emits
|
|
36
|
+
<code>{kind: "release", owner, targets}</code> for those lights and the room controller restyles them.
|
|
37
|
+
Give each activity-lighting node that feeds the same room controller its own owner.</p>
|
|
38
|
+
</script>
|
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
const bounded_trace_1 = require("../lib/bounded-trace");
|
|
3
|
+
const activity_lighting_controller_1 = require("../lib/activity-lighting-controller");
|
|
4
|
+
function number(value, fallback) {
|
|
5
|
+
const parsed = Number(value);
|
|
6
|
+
return Number.isFinite(parsed) ? parsed : fallback;
|
|
7
|
+
}
|
|
8
|
+
const registerActivityLightingController = (RED) => {
|
|
9
|
+
function HomeActivityLightingController(config) {
|
|
10
|
+
RED.nodes.createNode(this, config);
|
|
11
|
+
const node = this;
|
|
12
|
+
const context = node.context();
|
|
13
|
+
let timer;
|
|
14
|
+
let profiles;
|
|
15
|
+
try {
|
|
16
|
+
profiles = (0, activity_lighting_controller_1.normaliseLightingProfiles)(JSON.parse(config.profiles || "{}"));
|
|
17
|
+
}
|
|
18
|
+
catch (error) {
|
|
19
|
+
node.status({ fill: "red", shape: "ring", text: "invalid profile JSON" });
|
|
20
|
+
node.error(error);
|
|
21
|
+
return;
|
|
22
|
+
}
|
|
23
|
+
const controllerConfig = {
|
|
24
|
+
profiles,
|
|
25
|
+
owner: config.owner || "activity",
|
|
26
|
+
confirmationTimeoutMs: number(config.confirmationTimeoutMs, 15000),
|
|
27
|
+
maxAttempts: number(config.maxAttempts, 2)
|
|
28
|
+
};
|
|
29
|
+
function updateStatus(status) {
|
|
30
|
+
const appearances = {
|
|
31
|
+
idle: { fill: "grey", shape: "dot" },
|
|
32
|
+
transitioning: { fill: "blue", shape: "dot" },
|
|
33
|
+
confirming: { fill: "blue", shape: "ring" },
|
|
34
|
+
ready: { fill: "green", shape: "dot" },
|
|
35
|
+
failed: { fill: "yellow", shape: "ring" }
|
|
36
|
+
};
|
|
37
|
+
const appearance = appearances[status.phase] ?? { fill: "grey", shape: "ring" };
|
|
38
|
+
node.status({ ...appearance, text: status.text });
|
|
39
|
+
}
|
|
40
|
+
function schedule(state) {
|
|
41
|
+
clearTimeout(timer);
|
|
42
|
+
timer = undefined;
|
|
43
|
+
if (!state.pending?.deadlineAt)
|
|
44
|
+
return;
|
|
45
|
+
const delay = Math.max(0, state.pending.deadlineAt - Date.now());
|
|
46
|
+
timer = setTimeout(() => {
|
|
47
|
+
try {
|
|
48
|
+
process({ activityLighting: { kind: "tick", generation: state.generation } }, node.send.bind(node));
|
|
49
|
+
}
|
|
50
|
+
catch (error) {
|
|
51
|
+
node.error(error);
|
|
52
|
+
}
|
|
53
|
+
}, delay);
|
|
54
|
+
}
|
|
55
|
+
function process(msg, send) {
|
|
56
|
+
const now = new Date();
|
|
57
|
+
const saved = context.get("state");
|
|
58
|
+
const result = (0, activity_lighting_controller_1.transitionActivityLighting)(saved, msg, controllerConfig, now);
|
|
59
|
+
context.set("state", result.state);
|
|
60
|
+
(0, bounded_trace_1.appendTrace)(context, {
|
|
61
|
+
area: config.area || "lounge",
|
|
62
|
+
component: "activity-lighting-controller"
|
|
63
|
+
}, result.traces, now);
|
|
64
|
+
if (result.commands.length) {
|
|
65
|
+
send([
|
|
66
|
+
result.commands.map((activityLightingCommand) => ({
|
|
67
|
+
payload: undefined,
|
|
68
|
+
activityLightingCommand,
|
|
69
|
+
...(activityLightingCommand.lighting ? { lighting: activityLightingCommand.lighting } : {})
|
|
70
|
+
})),
|
|
71
|
+
null
|
|
72
|
+
]);
|
|
73
|
+
}
|
|
74
|
+
if (result.events.length) {
|
|
75
|
+
send([null, result.events.map((event) => ({ payload: undefined, ...event }))]);
|
|
76
|
+
}
|
|
77
|
+
updateStatus(result.status);
|
|
78
|
+
schedule(result.state);
|
|
79
|
+
return result;
|
|
80
|
+
}
|
|
81
|
+
// Context survives a redeploy but timers do not. Recover a pending
|
|
82
|
+
// transition with the reducer's own startup (re-observe, never replay)
|
|
83
|
+
// rather than ticking old state; a later startup from the flow repeats it.
|
|
84
|
+
if (context.get("state")?.pending) {
|
|
85
|
+
timer = setTimeout(() => {
|
|
86
|
+
try {
|
|
87
|
+
process({ activityLighting: { kind: "startup" } }, node.send.bind(node));
|
|
88
|
+
}
|
|
89
|
+
catch (error) {
|
|
90
|
+
node.error(error);
|
|
91
|
+
}
|
|
92
|
+
}, 0);
|
|
93
|
+
}
|
|
94
|
+
node.on("input", (msg, send, done) => {
|
|
95
|
+
try {
|
|
96
|
+
process({
|
|
97
|
+
activityLighting: msg.activityLighting && typeof msg.activityLighting === "object"
|
|
98
|
+
? msg.activityLighting
|
|
99
|
+
: undefined
|
|
100
|
+
}, send);
|
|
101
|
+
done();
|
|
102
|
+
}
|
|
103
|
+
catch (error) {
|
|
104
|
+
node.error(error, msg);
|
|
105
|
+
done(error instanceof Error ? error : new Error(String(error)));
|
|
106
|
+
}
|
|
107
|
+
});
|
|
108
|
+
node.on("close", (_removed, done) => {
|
|
109
|
+
clearTimeout(timer);
|
|
110
|
+
done();
|
|
111
|
+
});
|
|
112
|
+
}
|
|
113
|
+
RED.nodes.registerType("home-activity-lighting-controller", HomeActivityLightingController);
|
|
114
|
+
};
|
|
115
|
+
module.exports = registerActivityLightingController;
|
|
116
|
+
//# sourceMappingURL=activity-lighting-controller.js.map
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
<script type="text/javascript">
|
|
2
|
+
RED.nodes.registerType("home-avr-controller", {
|
|
3
|
+
category: "home automation",
|
|
4
|
+
color: "#c7d9f1",
|
|
5
|
+
defaults: {
|
|
6
|
+
name: {value: ""},
|
|
7
|
+
area: {value: "lounge", required: true},
|
|
8
|
+
sources: {value: "{\"shield\":\"SAT/CBL\",\"playstation\":\"GAME\"}", required: true},
|
|
9
|
+
retryMs: {value: 2000, required: true, validate: RED.validators.number()},
|
|
10
|
+
maxAttempts: {value: 3, required: true, validate: RED.validators.number()},
|
|
11
|
+
minVolume: {value: 0, required: true, validate: RED.validators.number()},
|
|
12
|
+
maxVolume: {value: 60, required: true, validate: RED.validators.number()}
|
|
13
|
+
},
|
|
14
|
+
inputs: 1,
|
|
15
|
+
outputs: 2,
|
|
16
|
+
outputLabels: ["Denon commands", "state and results"],
|
|
17
|
+
icon: "font-awesome/fa-volume-up",
|
|
18
|
+
label: function () { return this.name || this.area + " AVR controller"; }
|
|
19
|
+
});
|
|
20
|
+
</script>
|
|
21
|
+
|
|
22
|
+
<script type="text/html" data-template-name="home-avr-controller">
|
|
23
|
+
<div class="form-row"><label for="node-input-name">Name</label><input type="text" id="node-input-name"></div>
|
|
24
|
+
<div class="form-row"><label for="node-input-area">Area</label><input type="text" id="node-input-area"></div>
|
|
25
|
+
<div class="form-row"><label for="node-input-sources">Sources</label><input type="text" id="node-input-sources"></div>
|
|
26
|
+
<div class="form-row"><label for="node-input-retryMs">Retry milliseconds</label><input type="number" id="node-input-retryMs"></div>
|
|
27
|
+
<div class="form-row"><label for="node-input-maxAttempts">Maximum attempts</label><input type="number" id="node-input-maxAttempts"></div>
|
|
28
|
+
<div class="form-row"><label for="node-input-minVolume">Minimum volume</label><input type="number" step="0.5" id="node-input-minVolume"></div>
|
|
29
|
+
<div class="form-row"><label for="node-input-maxVolume">Maximum volume</label><input type="number" step="0.5" id="node-input-maxVolume"></div>
|
|
30
|
+
</script>
|
|
31
|
+
|
|
32
|
+
<script type="text/html" data-help-name="home-avr-controller">
|
|
33
|
+
<p>Owns explicit power, main-zone, source and bounded volume commands for a Denon AVR.</p>
|
|
34
|
+
<p>Send protocol observations as <code>msg.avr = {kind: "observed", line: msg.payload}</code>.</p>
|
|
35
|
+
<p>Send activity requests as <code>msg.avr = {kind: "intent", requestId, generation, power: "on", source: "shield"}</code>. Omit <code>source</code> to preserve the current input, or use <code>power: "standby"</code> to stop the AVR.</p>
|
|
36
|
+
<p>Send volume requests as <code>msg.avr = {kind: "volume", requestId, mode: "absolute", value: 30}</code> or use <code>mode: "relative"</code> with a positive or negative delta. The controller applies configured bounds, accumulates rapid relative requests, confirms fresh <code>MV</code> evidence and releases ownership so external adjustments are preserved.</p>
|
|
37
|
+
<p>Send <code>kind: "startup"</code> after deployment to discard stale observations and query the receiver; a volume request still pending then is reported as failed with reason <code>restart</code>. Send <code>kind: "refresh"</code> to re-read the receiver without disturbing work in flight. Output 1 connects to a TCP output node; output 2 emits state plus ready, failed, rejected, superseded and volume results.</p>
|
|
38
|
+
<p>While it has a request, output 2 also carries <code>msg.theatre = {kind: "target_state", target: "avr", requestId, generation, status, observedAt, details}</code>, so it can be wired straight back to a theatre activity controller. A ready state is stamped with the oldest receiver reading the request relies on, and a <code>refresh</code> is answered once the receiver reports again.</p>
|
|
39
|
+
<h3>Volume results</h3>
|
|
40
|
+
<dl class="message-properties">
|
|
41
|
+
<dt>volume_ready</dt><dd><code>{kind, area, requestId, volume, bounded}</code>: confirmed by a fresh reading.</dd>
|
|
42
|
+
<dt>volume_rejected</dt><dd><code>{kind, area, requestId, reason}</code>: not sent, for example <code>receiver-not-ready</code> or an invalid request.</dd>
|
|
43
|
+
<dt>volume_failed</dt><dd><code>{kind, area, requestId, reason, operation, attempts}</code>: <code>reason</code> is <code>attempts-exhausted</code> (with the operation and attempts) or <code>restart</code> (operation and attempts are <code>null</code>).</dd>
|
|
44
|
+
<dt>volume_manual_override</dt><dd><code>{kind, area, requestId, reason, observed, target}</code>: the receiver moved elsewhere while the request was in flight.</dd>
|
|
45
|
+
<dt>volume_external</dt><dd><code>{kind, area, previous, observed}</code>: a change made outside any request.</dd>
|
|
46
|
+
</dl>
|
|
47
|
+
</script>
|
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
const bounded_trace_1 = require("../lib/bounded-trace");
|
|
3
|
+
const avr_controller_1 = require("../lib/avr-controller");
|
|
4
|
+
function number(value, fallback) {
|
|
5
|
+
const parsed = Number(value);
|
|
6
|
+
return Number.isFinite(parsed) ? parsed : fallback;
|
|
7
|
+
}
|
|
8
|
+
function parseSources(value) {
|
|
9
|
+
const sources = JSON.parse(value || "{}");
|
|
10
|
+
if (!sources || Array.isArray(sources) || typeof sources !== "object") {
|
|
11
|
+
throw new Error("sources must be a JSON object");
|
|
12
|
+
}
|
|
13
|
+
return sources;
|
|
14
|
+
}
|
|
15
|
+
const registerAvrController = (RED) => {
|
|
16
|
+
function HomeAvrController(config) {
|
|
17
|
+
RED.nodes.createNode(this, config);
|
|
18
|
+
const node = this;
|
|
19
|
+
const context = node.context();
|
|
20
|
+
let retryTimer;
|
|
21
|
+
let controllerConfig;
|
|
22
|
+
try {
|
|
23
|
+
// The same validation the reducer applies, so a bad configuration
|
|
24
|
+
// fails here at deploy rather than on every message.
|
|
25
|
+
controllerConfig = (0, avr_controller_1.normaliseAvrConfig)({
|
|
26
|
+
area: config.area || "room",
|
|
27
|
+
sources: parseSources(config.sources),
|
|
28
|
+
retryMs: Math.max(250, number(config.retryMs, 2000)),
|
|
29
|
+
maxAttempts: Math.max(1, Math.floor(number(config.maxAttempts, 3))),
|
|
30
|
+
minVolume: number(config.minVolume, 0),
|
|
31
|
+
maxVolume: number(config.maxVolume, 60)
|
|
32
|
+
});
|
|
33
|
+
}
|
|
34
|
+
catch (error) {
|
|
35
|
+
// A short badge; the full reason goes to the log and debug sidebar.
|
|
36
|
+
const reason = error instanceof Error ? error.message : String(error);
|
|
37
|
+
node.status({ fill: "red", shape: "ring", text: "invalid configuration" });
|
|
38
|
+
node.error("invalid configuration: " + reason);
|
|
39
|
+
return;
|
|
40
|
+
}
|
|
41
|
+
function updateStatus(status) {
|
|
42
|
+
const appearances = {
|
|
43
|
+
discovering: { fill: "grey", shape: "ring" },
|
|
44
|
+
idle: { fill: "grey", shape: "dot" },
|
|
45
|
+
transitioning: { fill: "blue", shape: "dot" },
|
|
46
|
+
ready: { fill: "green", shape: "dot" },
|
|
47
|
+
manual_override: { fill: "yellow", shape: "ring" },
|
|
48
|
+
failed: { fill: "red", shape: "ring" }
|
|
49
|
+
};
|
|
50
|
+
const appearance = appearances[status.phase] ?? { fill: "grey", shape: "ring" };
|
|
51
|
+
node.status({ ...appearance, text: status.text });
|
|
52
|
+
}
|
|
53
|
+
function scheduleRetry(state) {
|
|
54
|
+
clearTimeout(retryTimer);
|
|
55
|
+
retryTimer = undefined;
|
|
56
|
+
if (!state.pending?.deadlineAt)
|
|
57
|
+
return;
|
|
58
|
+
const delay = Math.max(0, state.pending.deadlineAt - Date.now());
|
|
59
|
+
retryTimer = setTimeout(() => {
|
|
60
|
+
try {
|
|
61
|
+
process({ avr: { kind: "tick" } }, node.send.bind(node));
|
|
62
|
+
}
|
|
63
|
+
catch (error) {
|
|
64
|
+
node.error(error);
|
|
65
|
+
}
|
|
66
|
+
}, delay);
|
|
67
|
+
}
|
|
68
|
+
function process(msg, send) {
|
|
69
|
+
const now = new Date();
|
|
70
|
+
const saved = context.get("state");
|
|
71
|
+
const result = (0, avr_controller_1.transitionAvr)(saved, msg, controllerConfig, now);
|
|
72
|
+
context.set("state", result.state);
|
|
73
|
+
(0, bounded_trace_1.appendTrace)(context, {
|
|
74
|
+
area: controllerConfig.area,
|
|
75
|
+
component: "avr-controller"
|
|
76
|
+
}, result.traces, now);
|
|
77
|
+
const commands = result.commands.map((command) => ({
|
|
78
|
+
payload: Buffer.from(command.payload, "utf8"),
|
|
79
|
+
avr_command: command.avrCommand
|
|
80
|
+
}));
|
|
81
|
+
const outputs = [...result.events, ...result.targetStates];
|
|
82
|
+
if (commands.length || outputs.length) {
|
|
83
|
+
send([
|
|
84
|
+
commands.length ? commands : null,
|
|
85
|
+
outputs.length
|
|
86
|
+
? outputs.map((event) => ({ payload: undefined, ...event }))
|
|
87
|
+
: null
|
|
88
|
+
]);
|
|
89
|
+
}
|
|
90
|
+
updateStatus(result.status);
|
|
91
|
+
scheduleRetry(result.state);
|
|
92
|
+
return result;
|
|
93
|
+
}
|
|
94
|
+
// Context survives a redeploy but timers do not. Recover a pending
|
|
95
|
+
// transition with the reducer's own startup (re-observe, never replay)
|
|
96
|
+
// rather than ticking old state; a later startup from the flow repeats it.
|
|
97
|
+
if (context.get("state")?.pending) {
|
|
98
|
+
retryTimer = setTimeout(() => {
|
|
99
|
+
try {
|
|
100
|
+
process({ avr: { kind: "startup" } }, node.send.bind(node));
|
|
101
|
+
}
|
|
102
|
+
catch (error) {
|
|
103
|
+
node.error(error);
|
|
104
|
+
}
|
|
105
|
+
}, 0);
|
|
106
|
+
}
|
|
107
|
+
node.on("input", (msg, send, done) => {
|
|
108
|
+
try {
|
|
109
|
+
process({
|
|
110
|
+
avr: msg.avr && typeof msg.avr === "object"
|
|
111
|
+
? msg.avr
|
|
112
|
+
: undefined,
|
|
113
|
+
payload: msg.payload
|
|
114
|
+
}, send);
|
|
115
|
+
done();
|
|
116
|
+
}
|
|
117
|
+
catch (error) {
|
|
118
|
+
node.error(error, msg);
|
|
119
|
+
done(error instanceof Error ? error : new Error(String(error)));
|
|
120
|
+
}
|
|
121
|
+
});
|
|
122
|
+
node.on("close", (_removed, done) => {
|
|
123
|
+
clearTimeout(retryTimer);
|
|
124
|
+
done();
|
|
125
|
+
});
|
|
126
|
+
}
|
|
127
|
+
RED.nodes.registerType("home-avr-controller", HomeAvrController);
|
|
128
|
+
};
|
|
129
|
+
module.exports = registerAvrController;
|
|
130
|
+
//# sourceMappingURL=avr-controller.js.map
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
<script type="text/javascript">
|
|
2
|
+
RED.nodes.registerType("home-backlighting-controller", {
|
|
3
|
+
category: "home automation",
|
|
4
|
+
color: "#c7d9f1",
|
|
5
|
+
defaults: {
|
|
6
|
+
name: {value: ""},
|
|
7
|
+
area: {value: "lounge", required: true},
|
|
8
|
+
pollMs: {value: 2000, required: true, validate: RED.validators.number()},
|
|
9
|
+
confirmationTimeoutMs: {value: 15000, required: true, validate: RED.validators.number()},
|
|
10
|
+
maxAttempts: {value: 2, required: true, validate: RED.validators.number()}
|
|
11
|
+
},
|
|
12
|
+
inputs: 1,
|
|
13
|
+
outputs: 2,
|
|
14
|
+
outputLabels: ["backlighting commands", "theatre target state"],
|
|
15
|
+
icon: "font-awesome/fa-television",
|
|
16
|
+
label: function() { return this.name || "TV backlighting"; }
|
|
17
|
+
});
|
|
18
|
+
</script>
|
|
19
|
+
|
|
20
|
+
<script type="text/html" data-template-name="home-backlighting-controller">
|
|
21
|
+
<div class="form-row"><label for="node-input-name">Name</label><input type="text" id="node-input-name"></div>
|
|
22
|
+
<div class="form-row"><label for="node-input-area">Area</label><input type="text" id="node-input-area"></div>
|
|
23
|
+
<div class="form-row"><label for="node-input-pollMs">Poll ms</label><input type="number" id="node-input-pollMs"></div>
|
|
24
|
+
<div class="form-row"><label for="node-input-confirmationTimeoutMs">Confirm ms</label><input type="number" id="node-input-confirmationTimeoutMs"></div>
|
|
25
|
+
<div class="form-row"><label for="node-input-maxAttempts">Attempts</label><input type="number" id="node-input-maxAttempts"></div>
|
|
26
|
+
</script>
|
|
27
|
+
|
|
28
|
+
<script type="text/html" data-help-name="home-backlighting-controller">
|
|
29
|
+
<p>Owns semantic TV backlight power intent with bounded command retries and observation-gated confirmation.</p>
|
|
30
|
+
<p>Input <code>msg.backlighting</code> carries generation-scoped intent, observation, command result, refresh or tick events. Output 1 emits <code>msg.backlightingCommand</code> for the deployment transport. Output 2 emits mandatory theatre <code>target_state</code> evidence. A command acknowledgement never counts as confirmed power.</p>
|
|
31
|
+
</script>
|