hydraloop-diagram-react 0.0.0 → 1.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/dist/flow-diagram.d.ts +11 -0
- package/dist/flow-diagram.js +513 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.js +3 -0
- package/dist/model.d.ts +320 -0
- package/dist/model.js +662 -0
- package/dist/styles.css +586 -0
- package/dist/version.d.ts +1 -0
- package/dist/version.js +1 -0
- package/license +15 -0
- package/package.json +39 -3
- package/readme.md +125 -0
package/dist/model.js
ADDED
|
@@ -0,0 +1,662 @@
|
|
|
1
|
+
// Model of a Hydraloop H600: works out what the device is doing at a point in
|
|
2
|
+
// time (`stateAt`) from a `Dataset` of tank levels, IO signals, routines and
|
|
3
|
+
// firmware behaviors, as loaded from the internal HDM API. `FlowDiagram`
|
|
4
|
+
// draws the resulting `DeviceState`. Has no dependencies, so it also works
|
|
5
|
+
// without React, e.g. on a server.
|
|
6
|
+
//
|
|
7
|
+
// The phase of each train comes from the routine that HDM reports for its
|
|
8
|
+
// side (e.g. `INCOMING_WATER`, `TRANSFER`). Where no routine is known, it
|
|
9
|
+
// falls back to valve signals. Each state says how it was determined.
|
|
10
|
+
export const TANK_IDS = ["t1a", "t2a", "t1b", "t2b", "t3"];
|
|
11
|
+
/**
|
|
12
|
+
* IO names as returned by `hydraloop.local.graph.getIo()`. B-side IOs of the
|
|
13
|
+
* H600 have a `_b` suffix. The roles were derived by correlating the signals
|
|
14
|
+
* with each other and with the routines of an H600 (firmware 9.24).
|
|
15
|
+
*/
|
|
16
|
+
export const IO = {
|
|
17
|
+
/** Sends incoming greywater to the sewer while on */
|
|
18
|
+
bypass: "grey_water_bypass",
|
|
19
|
+
/**
|
|
20
|
+
* Selects the train that receives greywater (0: A, 1: B). Switched when a
|
|
21
|
+
* train starts filth removal, and for a minute during a restart
|
|
22
|
+
*/
|
|
23
|
+
abSelector: "grey_water_ab_selection_valve",
|
|
24
|
+
uvValve: "uv_valve",
|
|
25
|
+
uvPower: "uv_power",
|
|
26
|
+
auxiliary: "auxiliary",
|
|
27
|
+
/** Outlets are supplied from T4 while on (stays on for hours) */
|
|
28
|
+
backupWater: "backup_water",
|
|
29
|
+
preBackupWater: "pre_backup_water_valve",
|
|
30
|
+
backFlush: "back_flush",
|
|
31
|
+
/** Drains T3 */
|
|
32
|
+
wasteStorageTank: "waste_storage_tank",
|
|
33
|
+
distributionPump: "distribution_pump",
|
|
34
|
+
/** A valve, despite its name: routes the distribution pump back through UV into T3 */
|
|
35
|
+
circulation: "circulation_pump",
|
|
36
|
+
/** Demand sensors of the outlets */
|
|
37
|
+
toilet: "toilet",
|
|
38
|
+
wash: "wash",
|
|
39
|
+
/** Float sensors of T4 */
|
|
40
|
+
t4Max: "tank4",
|
|
41
|
+
t4Min: "tank4_enough_water",
|
|
42
|
+
};
|
|
43
|
+
export function sideIo(name, side) {
|
|
44
|
+
return side === "A" ? name : `${name}_b`;
|
|
45
|
+
}
|
|
46
|
+
// ── Tank capacities ────────────────────────────────────────────────────────
|
|
47
|
+
// Estimated from Hydraloop's H600 V2.1 CAD model, not certified by Hydraloop:
|
|
48
|
+
// T1 holds about 24 L, T2 about 200 L. The `t1t2Levels` calibration table
|
|
49
|
+
// ends at 185 L, which fits T2 (T2 drops by about as much as T3 rises during
|
|
50
|
+
// transfers), but not T1: T1 reads up to ~930 pressure points, past the
|
|
51
|
+
// table's end at 703 points, which would be 185 L in a 24 L tank.
|
|
52
|
+
/** Physical capacity in liters, estimated from the H600 CAD model */
|
|
53
|
+
export const TANK_CAPACITY = {
|
|
54
|
+
t1a: 24,
|
|
55
|
+
t1b: 24,
|
|
56
|
+
t2a: 200,
|
|
57
|
+
t2b: 200,
|
|
58
|
+
};
|
|
59
|
+
/** `PASSIVE_CIRCULATE` → `Passive circulate` */
|
|
60
|
+
export function humanize(text) {
|
|
61
|
+
const words = text.replace(/_/g, " ").trim().toLowerCase();
|
|
62
|
+
return words.charAt(0).toUpperCase() + words.slice(1);
|
|
63
|
+
}
|
|
64
|
+
const IO_LABELS = {
|
|
65
|
+
[IO.bypass]: "Bypass",
|
|
66
|
+
[IO.abSelector]: "A/B valve",
|
|
67
|
+
[IO.uvValve]: "UV valve",
|
|
68
|
+
[IO.uvPower]: "UV-C",
|
|
69
|
+
[IO.backupWater]: "Backup water",
|
|
70
|
+
[IO.preBackupWater]: "Pre backup water valve",
|
|
71
|
+
[IO.wasteStorageTank]: "T3 drain",
|
|
72
|
+
[IO.circulation]: "Circulation valve",
|
|
73
|
+
[IO.distributionPump]: "Distribution pump",
|
|
74
|
+
[IO.toilet]: "Toilet demand",
|
|
75
|
+
[IO.wash]: "Washer demand",
|
|
76
|
+
[IO.t4Max]: "T4 max float",
|
|
77
|
+
[IO.t4Min]: "T4 min float",
|
|
78
|
+
transfer_path: "Transfer A",
|
|
79
|
+
transfer_path_b: "Transfer B",
|
|
80
|
+
waste_clean_tank: "Waste A",
|
|
81
|
+
waste_clean_tank_b: "Waste B",
|
|
82
|
+
};
|
|
83
|
+
/**
|
|
84
|
+
* Behaviors that explain nothing the app shows. `pressingOptButton` is
|
|
85
|
+
* engaged for about 12 hours at a time, nearly always.
|
|
86
|
+
*/
|
|
87
|
+
export const OTHER_BEHAVIORS = new Set(["pressingOptButton"]);
|
|
88
|
+
export function caption(captions, name) {
|
|
89
|
+
if (IO_LABELS[name])
|
|
90
|
+
return IO_LABELS[name];
|
|
91
|
+
// The graph config captions are often just the IO name
|
|
92
|
+
const text = captions[name] && captions[name] !== name ? captions[name] : name;
|
|
93
|
+
const label = humanize(text.replace(/_b$/, ""));
|
|
94
|
+
return /_b$/.test(text) ? `${label} B` : label;
|
|
95
|
+
}
|
|
96
|
+
// ── Lookups ────────────────────────────────────────────────────────────────
|
|
97
|
+
/** Index of the last point at or before `t`, or -1 */
|
|
98
|
+
function indexAt(points, t) {
|
|
99
|
+
let lo = 0;
|
|
100
|
+
let hi = points.length - 1;
|
|
101
|
+
let found = -1;
|
|
102
|
+
while (lo <= hi) {
|
|
103
|
+
const mid = (lo + hi) >> 1;
|
|
104
|
+
if (points[mid].t <= t) {
|
|
105
|
+
found = mid;
|
|
106
|
+
lo = mid + 1;
|
|
107
|
+
}
|
|
108
|
+
else {
|
|
109
|
+
hi = mid - 1;
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
return found;
|
|
113
|
+
}
|
|
114
|
+
/** Step value: the last known value at or before `t` */
|
|
115
|
+
export function stepAt(points, t) {
|
|
116
|
+
if (!points?.length)
|
|
117
|
+
return undefined;
|
|
118
|
+
const i = indexAt(points, t);
|
|
119
|
+
return i === -1 ? undefined : points[i];
|
|
120
|
+
}
|
|
121
|
+
/** Linearly interpolated value at `t`, undefined outside of the data */
|
|
122
|
+
export function valueAt(points, t) {
|
|
123
|
+
if (!points?.length)
|
|
124
|
+
return undefined;
|
|
125
|
+
const i = indexAt(points, t);
|
|
126
|
+
if (i === -1)
|
|
127
|
+
return undefined;
|
|
128
|
+
const a = points[i];
|
|
129
|
+
const b = points[i + 1];
|
|
130
|
+
// Don't interpolate across gaps in the data
|
|
131
|
+
if (!b)
|
|
132
|
+
return t - a.t <= 10 * 60_000 ? a.v : undefined;
|
|
133
|
+
if (b.t - a.t > 30 * 60_000)
|
|
134
|
+
return t - a.t <= 10 * 60_000 ? a.v : undefined;
|
|
135
|
+
return a.v + ((t - a.t) / (b.t - a.t)) * (b.v - a.v);
|
|
136
|
+
}
|
|
137
|
+
/** When the IO last changed its on/off state at or before `t` */
|
|
138
|
+
function onSince(points, t) {
|
|
139
|
+
let i = indexAt(points, t);
|
|
140
|
+
if (i === -1)
|
|
141
|
+
return undefined;
|
|
142
|
+
const on = points[i].v > 0;
|
|
143
|
+
while (i > 0 && points[i - 1].v > 0 === on)
|
|
144
|
+
i--;
|
|
145
|
+
return points[i].t;
|
|
146
|
+
}
|
|
147
|
+
/** HDM routine names per phase, as reported by `getRoutineData()` */
|
|
148
|
+
const ROUTINE_PHASES = {
|
|
149
|
+
STANDBY: "idle",
|
|
150
|
+
INCOMING_WATER: "receiving",
|
|
151
|
+
SKIM_LEVEL_DETERMINATION: "treating",
|
|
152
|
+
FILTH_REMOVAL: "cleaning",
|
|
153
|
+
TRANSFER: "transferring",
|
|
154
|
+
};
|
|
155
|
+
export function routinePhase(name) {
|
|
156
|
+
if (ROUTINE_PHASES[name])
|
|
157
|
+
return ROUTINE_PHASES[name];
|
|
158
|
+
const text = name.toLowerCase();
|
|
159
|
+
if (/clean|filth|flush|waste|purge|drain/.test(text))
|
|
160
|
+
return "cleaning";
|
|
161
|
+
if (/transfer/.test(text))
|
|
162
|
+
return "transferring";
|
|
163
|
+
if (/incoming|fill|collect|intake|inlet/.test(text))
|
|
164
|
+
return "receiving";
|
|
165
|
+
if (/skim|treat|aerat|bio/.test(text))
|
|
166
|
+
return "treating";
|
|
167
|
+
if (/standby|idle|wait|passive/.test(text))
|
|
168
|
+
return "idle";
|
|
169
|
+
return "other";
|
|
170
|
+
}
|
|
171
|
+
const PHASE_LABELS = {
|
|
172
|
+
cleaning: "Self-cleaning",
|
|
173
|
+
transferring: "Transferring to T3",
|
|
174
|
+
receiving: "Receiving greywater",
|
|
175
|
+
treating: "Treating",
|
|
176
|
+
idle: "Standby",
|
|
177
|
+
other: "Other",
|
|
178
|
+
unknown: "Unknown",
|
|
179
|
+
};
|
|
180
|
+
const TREND_WINDOW = 10 * 60_000;
|
|
181
|
+
/** `FILTH_REMOVAL` sub-states while T1 is held at the skim overflow */
|
|
182
|
+
export const SKIM_STATES = new Set([
|
|
183
|
+
"REMOVE_FILTH_ON_HEIGHT",
|
|
184
|
+
"SKIM_LAST_WATER",
|
|
185
|
+
]);
|
|
186
|
+
/** The current routine of a side and when it started (first sub-state) */
|
|
187
|
+
export function routineAt(ds, side, t) {
|
|
188
|
+
let current;
|
|
189
|
+
let start;
|
|
190
|
+
let lastIncoming;
|
|
191
|
+
let lastTransfer;
|
|
192
|
+
for (const r of ds.routines) {
|
|
193
|
+
if (r.t > t)
|
|
194
|
+
break;
|
|
195
|
+
if (r.side !== side)
|
|
196
|
+
continue;
|
|
197
|
+
if (!current || current.name !== r.name) {
|
|
198
|
+
start = r.t;
|
|
199
|
+
if (r.name === "INCOMING_WATER")
|
|
200
|
+
lastIncoming = r.t;
|
|
201
|
+
if (r.name === "TRANSFER")
|
|
202
|
+
lastTransfer = r.t;
|
|
203
|
+
}
|
|
204
|
+
current = r;
|
|
205
|
+
}
|
|
206
|
+
return { current, start, lastIncoming, lastTransfer };
|
|
207
|
+
}
|
|
208
|
+
export function stateAt(ds, t) {
|
|
209
|
+
const tanks = {};
|
|
210
|
+
for (const id of TANK_IDS) {
|
|
211
|
+
const liters = valueAt(ds.tanks[id], t);
|
|
212
|
+
const before = valueAt(ds.tanks[id], t - TREND_WINDOW);
|
|
213
|
+
const max = ds.tankMax[id];
|
|
214
|
+
tanks[id] = {
|
|
215
|
+
liters,
|
|
216
|
+
max,
|
|
217
|
+
capacity: TANK_CAPACITY[id],
|
|
218
|
+
pct: liters === undefined || !max
|
|
219
|
+
? undefined
|
|
220
|
+
: Math.max(0, Math.min(1, liters / max)),
|
|
221
|
+
trend: liters === undefined || before === undefined
|
|
222
|
+
? undefined
|
|
223
|
+
: (liters - before) / (TREND_WINDOW / 60_000),
|
|
224
|
+
};
|
|
225
|
+
}
|
|
226
|
+
const io = {};
|
|
227
|
+
for (const [name, points] of Object.entries(ds.io)) {
|
|
228
|
+
const p = stepAt(points, t);
|
|
229
|
+
io[name] = p && {
|
|
230
|
+
on: p.v > 0,
|
|
231
|
+
value: p.v,
|
|
232
|
+
since: onSince(points, t),
|
|
233
|
+
manual: (stepAt(ds.manual?.[name], t)?.v ?? 0) > 0,
|
|
234
|
+
};
|
|
235
|
+
}
|
|
236
|
+
const on = (name) => io[name]?.on;
|
|
237
|
+
const routine = {};
|
|
238
|
+
const active = (ds.behaviors ?? []).filter((b) => !OTHER_BEHAVIORS.has(b.name) &&
|
|
239
|
+
b.start <= t &&
|
|
240
|
+
(b.open ? t <= b.end : t < b.end));
|
|
241
|
+
const train = (side) => {
|
|
242
|
+
const { current, start, lastIncoming, lastTransfer } = routineAt(ds, side, t);
|
|
243
|
+
if (current)
|
|
244
|
+
routine[side] = current;
|
|
245
|
+
const waste = io[sideIo("waste_clean_tank", side)];
|
|
246
|
+
const transfer = io[sideIo("transfer_path", side)];
|
|
247
|
+
const air = io[sideIo("air_pump", side)];
|
|
248
|
+
const bubbles = io[sideIo("bubbles_block", side)];
|
|
249
|
+
const evidence = [
|
|
250
|
+
transfer?.on && "Transfer valve open",
|
|
251
|
+
waste?.on && "Waste valve open",
|
|
252
|
+
air?.on && "Air pump running",
|
|
253
|
+
bubbles?.on && "Bubbles block closed",
|
|
254
|
+
].filter((e) => Boolean(e));
|
|
255
|
+
// T2 drains through T1 while T1 is emptied to the drain or into T3
|
|
256
|
+
const t2Trend = tanks[side === "A" ? "t2a" : "t2b"].trend;
|
|
257
|
+
const t2ToT1 = Boolean(waste?.on || transfer?.on) &&
|
|
258
|
+
t2Trend !== undefined &&
|
|
259
|
+
t2Trend <= -0.5;
|
|
260
|
+
// During `FILTH_REMOVAL`, the air pump runs continuously and holds T1 at
|
|
261
|
+
// the top of its range, so filth spills over the skim overflow
|
|
262
|
+
const skimming = current?.name === "FILTH_REMOVAL" &&
|
|
263
|
+
SKIM_STATES.has(current.state) &&
|
|
264
|
+
air?.on !== false;
|
|
265
|
+
if (skimming)
|
|
266
|
+
evidence.push("Skimming over the overflow (inferred)");
|
|
267
|
+
const behaviors = active.filter((b) => b.side === side);
|
|
268
|
+
const base = {
|
|
269
|
+
side,
|
|
270
|
+
lastIncoming,
|
|
271
|
+
lastTransfer,
|
|
272
|
+
evidence,
|
|
273
|
+
t2ToT1,
|
|
274
|
+
skimming,
|
|
275
|
+
behaviors,
|
|
276
|
+
pressurized: holdsPressure(ds, side, t),
|
|
277
|
+
};
|
|
278
|
+
const routineText = current
|
|
279
|
+
? [humanize(current.name), current.state && humanize(current.state)]
|
|
280
|
+
.filter(Boolean)
|
|
281
|
+
.join(" · ")
|
|
282
|
+
: undefined;
|
|
283
|
+
// An open waste or transfer valve moves water, whatever the routine is
|
|
284
|
+
if (waste?.on || transfer?.on) {
|
|
285
|
+
const draining = Boolean(waste?.on);
|
|
286
|
+
return {
|
|
287
|
+
...base,
|
|
288
|
+
phase: draining ? "cleaning" : "transferring",
|
|
289
|
+
label: draining ? "Draining to waste" : PHASE_LABELS.transferring,
|
|
290
|
+
detail: [
|
|
291
|
+
behaviors.length > 0 &&
|
|
292
|
+
`HDM behavior: ${behaviors.map((b) => b.label).join(", ")}`,
|
|
293
|
+
routineText && `HDM routine: ${routineText}`,
|
|
294
|
+
]
|
|
295
|
+
.filter(Boolean)
|
|
296
|
+
.join(". "),
|
|
297
|
+
confidence: "valve",
|
|
298
|
+
since: (draining ? waste : transfer)?.since,
|
|
299
|
+
};
|
|
300
|
+
}
|
|
301
|
+
if (current) {
|
|
302
|
+
return {
|
|
303
|
+
...base,
|
|
304
|
+
phase: routinePhase(current.name),
|
|
305
|
+
label: humanize(current.name),
|
|
306
|
+
detail: current.state ? humanize(current.state) : undefined,
|
|
307
|
+
confidence: "reported",
|
|
308
|
+
since: start,
|
|
309
|
+
};
|
|
310
|
+
}
|
|
311
|
+
if (!waste && !transfer) {
|
|
312
|
+
return {
|
|
313
|
+
...base,
|
|
314
|
+
phase: "unknown",
|
|
315
|
+
label: PHASE_LABELS.unknown,
|
|
316
|
+
detail: undefined,
|
|
317
|
+
confidence: "unknown",
|
|
318
|
+
since: undefined,
|
|
319
|
+
};
|
|
320
|
+
}
|
|
321
|
+
return {
|
|
322
|
+
...base,
|
|
323
|
+
phase: air?.on ? "treating" : "idle",
|
|
324
|
+
label: PHASE_LABELS[air?.on ? "treating" : "idle"],
|
|
325
|
+
detail: undefined,
|
|
326
|
+
confidence: "inferred",
|
|
327
|
+
since: undefined,
|
|
328
|
+
};
|
|
329
|
+
};
|
|
330
|
+
const trains = { A: train("A"), B: train("B") };
|
|
331
|
+
const system = routineAt(ds, "SYSTEM", t).current;
|
|
332
|
+
if (system)
|
|
333
|
+
routine.SYSTEM = system;
|
|
334
|
+
const receivingFrom = ["A", "B"].filter((s) => io[sideIo("transfer_path", s)]?.on);
|
|
335
|
+
const uv = on(IO.uvPower);
|
|
336
|
+
const circulating = on(IO.circulation);
|
|
337
|
+
const draining = on(IO.wasteStorageTank);
|
|
338
|
+
const t3Base = { receivingFrom, uv, circulating, draining };
|
|
339
|
+
let t3;
|
|
340
|
+
if (draining) {
|
|
341
|
+
t3 = {
|
|
342
|
+
...t3Base,
|
|
343
|
+
label: "Draining",
|
|
344
|
+
confidence: "valve",
|
|
345
|
+
evidence: ["T3 drain valve open"],
|
|
346
|
+
};
|
|
347
|
+
}
|
|
348
|
+
else if (receivingFrom.length) {
|
|
349
|
+
t3 = {
|
|
350
|
+
...t3Base,
|
|
351
|
+
label: `Receiving from ${receivingFrom.join(" and ")}`,
|
|
352
|
+
confidence: "valve",
|
|
353
|
+
evidence: receivingFrom.map((s) => `Transfer ${s} open`),
|
|
354
|
+
};
|
|
355
|
+
}
|
|
356
|
+
else if (circulating) {
|
|
357
|
+
const pumpOff = on(IO.distributionPump) === false;
|
|
358
|
+
t3 = {
|
|
359
|
+
...t3Base,
|
|
360
|
+
label: pumpOff
|
|
361
|
+
? "Circulation valve open, pump off"
|
|
362
|
+
: "Circulating through UV",
|
|
363
|
+
confidence: "valve",
|
|
364
|
+
evidence: [
|
|
365
|
+
"Circulation valve open",
|
|
366
|
+
...(pumpOff ? ["Distribution pump off"] : []),
|
|
367
|
+
...(uv ? ["UV on"] : []),
|
|
368
|
+
],
|
|
369
|
+
};
|
|
370
|
+
}
|
|
371
|
+
else {
|
|
372
|
+
t3 = {
|
|
373
|
+
...t3Base,
|
|
374
|
+
label: "Holding",
|
|
375
|
+
confidence: io[IO.circulation] ? "valve" : "unknown",
|
|
376
|
+
evidence: [],
|
|
377
|
+
};
|
|
378
|
+
}
|
|
379
|
+
const backup = on(IO.backupWater);
|
|
380
|
+
const pumping = on(IO.distributionPump);
|
|
381
|
+
const supply = {
|
|
382
|
+
pumping,
|
|
383
|
+
toOutlets: pumping,
|
|
384
|
+
source: backup === undefined ? "unknown" : backup ? "backup" : "recycled",
|
|
385
|
+
toilet: on(IO.toilet),
|
|
386
|
+
wash: on(IO.wash),
|
|
387
|
+
auxiliary: on(IO.auxiliary),
|
|
388
|
+
};
|
|
389
|
+
const t4Max = on(IO.t4Max);
|
|
390
|
+
const t4Min = on(IO.t4Min);
|
|
391
|
+
const t4 = {
|
|
392
|
+
max: t4Max,
|
|
393
|
+
min: t4Min,
|
|
394
|
+
label: t4Max === undefined && t4Min === undefined
|
|
395
|
+
? "No float data"
|
|
396
|
+
: t4Max
|
|
397
|
+
? "Full"
|
|
398
|
+
: t4Min
|
|
399
|
+
? "Above min"
|
|
400
|
+
: "Below min",
|
|
401
|
+
};
|
|
402
|
+
// HDM often reports INCOMING_WATER for both trains, or only for the one
|
|
403
|
+
// the A/B valve doesn't select, while only the selected train's T2 rises.
|
|
404
|
+
// So incoming water on either side goes to the train of the A/B valve.
|
|
405
|
+
const filling = fillingTrain(ds, t);
|
|
406
|
+
const incoming = ["A", "B"].filter((s) => trains[s].phase === "receiving");
|
|
407
|
+
if (filling && incoming.length > 0) {
|
|
408
|
+
const other = filling === "A" ? "B" : "A";
|
|
409
|
+
if (trains[other].phase === "receiving") {
|
|
410
|
+
trains[other] = {
|
|
411
|
+
...trains[other],
|
|
412
|
+
phase: "idle",
|
|
413
|
+
confidence: "valve",
|
|
414
|
+
evidence: [...trains[other].evidence, `A/B valve set to ${filling}`],
|
|
415
|
+
};
|
|
416
|
+
}
|
|
417
|
+
const current = routine[filling];
|
|
418
|
+
if (trains[filling].confidence === "reported" &&
|
|
419
|
+
current &&
|
|
420
|
+
ACCEPTING_ROUTINES.has(current.name)) {
|
|
421
|
+
trains[filling] = {
|
|
422
|
+
...trains[filling],
|
|
423
|
+
phase: "receiving",
|
|
424
|
+
confidence: "valve",
|
|
425
|
+
evidence: [...trains[filling].evidence, `A/B valve set to ${filling}`],
|
|
426
|
+
};
|
|
427
|
+
}
|
|
428
|
+
}
|
|
429
|
+
const inlet = {
|
|
430
|
+
bypass: on(IO.bypass),
|
|
431
|
+
receiving: filling ? (incoming.length > 0 ? [filling] : []) : incoming,
|
|
432
|
+
};
|
|
433
|
+
const systemBehaviors = active.filter((b) => b.side === "SYSTEM");
|
|
434
|
+
return {
|
|
435
|
+
t,
|
|
436
|
+
tanks,
|
|
437
|
+
io,
|
|
438
|
+
routine,
|
|
439
|
+
inlet,
|
|
440
|
+
trains,
|
|
441
|
+
t3,
|
|
442
|
+
t4,
|
|
443
|
+
supply,
|
|
444
|
+
systemBehaviors,
|
|
445
|
+
room: roomAt(ds, t),
|
|
446
|
+
};
|
|
447
|
+
}
|
|
448
|
+
// ── Room for greywater ─────────────────────────────────────────────────────
|
|
449
|
+
// Derived from 30 days of an H600's data, not documented by Hydraloop:
|
|
450
|
+
// - Greywater only enters the train selected by the A/B valve, and only in
|
|
451
|
+
// STANDBY (or during INCOMING_WATER).
|
|
452
|
+
// - After each incoming water, SKIM_LEVEL_DETERMINATION checks the train
|
|
453
|
+
// with T2 vented. Once T2 holds about 134 L, the train starts
|
|
454
|
+
// FILTH_REMOVAL, the A/B valve switches to the other train, and the train
|
|
455
|
+
// doesn't accept water for the ~4.5 h it lasts.
|
|
456
|
+
// - The bypass opens when neither train can accept water. It is also tested
|
|
457
|
+
// for about a minute every 12 hours, while the trains accept water.
|
|
458
|
+
// So T2 is taken from its last venting, which happens about once an hour.
|
|
459
|
+
// Greywater also fills T1, but T1 holds at most about 24 L.
|
|
460
|
+
/** T2 at which a train is full and starts FILTH_REMOVAL */
|
|
461
|
+
export const TRAIN_FULL_LITERS = 134;
|
|
462
|
+
/** Routines in which a train accepts greywater */
|
|
463
|
+
export const ACCEPTING_ROUTINES = new Set([
|
|
464
|
+
"STANDBY",
|
|
465
|
+
"INCOMING_WATER",
|
|
466
|
+
"TRANSFER",
|
|
467
|
+
]);
|
|
468
|
+
/**
|
|
469
|
+
* Without outages, bypass openings shorter than this are taken for the
|
|
470
|
+
* scheduled restart
|
|
471
|
+
*/
|
|
472
|
+
const RESTART_MAX = 2 * 60_000;
|
|
473
|
+
/**
|
|
474
|
+
* Whether a bypass opening is from the controller's scheduled restart every
|
|
475
|
+
* 12 hours. The bypass valve falls open while the controller is down. Falls
|
|
476
|
+
* back to the duration if the outages could not be loaded.
|
|
477
|
+
*/
|
|
478
|
+
export function isScheduledRestart(ds, interval) {
|
|
479
|
+
if (!ds.outages) {
|
|
480
|
+
return (!interval.open &&
|
|
481
|
+
!interval.clipped &&
|
|
482
|
+
interval.end - interval.start < RESTART_MAX);
|
|
483
|
+
}
|
|
484
|
+
return ds.outages.some((o) => o.classification === "scheduled_restart" &&
|
|
485
|
+
o.start < interval.end + 60_000 &&
|
|
486
|
+
o.end > interval.start - 60_000);
|
|
487
|
+
}
|
|
488
|
+
/**
|
|
489
|
+
* The train that the A/B valve sends greywater to. The valve is also
|
|
490
|
+
* switched for a minute during a restart, which is skipped.
|
|
491
|
+
*/
|
|
492
|
+
export function fillingTrain(ds, t) {
|
|
493
|
+
const points = ds.io[IO.abSelector];
|
|
494
|
+
if (!points?.length)
|
|
495
|
+
return undefined;
|
|
496
|
+
let i = indexAt(points, t);
|
|
497
|
+
while (i > 0 && (stepAt(ds.io[IO.bypass], points[i].t)?.v ?? 0) > 0)
|
|
498
|
+
i--;
|
|
499
|
+
if (i === -1)
|
|
500
|
+
return undefined;
|
|
501
|
+
return points[i].v > 0 ? "B" : "A";
|
|
502
|
+
}
|
|
503
|
+
/**
|
|
504
|
+
* Whether T2 holds air pressure at `t`: the air pump last ran with the add air
|
|
505
|
+
* valve open after the release air valve was last open
|
|
506
|
+
*/
|
|
507
|
+
function holdsPressure(ds, side, t) {
|
|
508
|
+
const pump = onIntervals(ds.io[sideIo("air_pump", side)], -Infinity, t);
|
|
509
|
+
const add = onIntervals(ds.io[sideIo("add_air_valve", side)], -Infinity, t);
|
|
510
|
+
let added = -Infinity;
|
|
511
|
+
for (const p of pump) {
|
|
512
|
+
for (const a of add) {
|
|
513
|
+
if (Math.max(p.start, a.start) < Math.min(p.end, a.end)) {
|
|
514
|
+
added = Math.max(added, Math.min(p.end, a.end));
|
|
515
|
+
}
|
|
516
|
+
}
|
|
517
|
+
}
|
|
518
|
+
const released = onIntervals(ds.io[sideIo("release_air", side)], -Infinity, t).at(-1)?.end;
|
|
519
|
+
return added > (released ?? -Infinity);
|
|
520
|
+
}
|
|
521
|
+
/** Vent openings shorter than this don't settle the levels */
|
|
522
|
+
const MIN_VENT = 60_000;
|
|
523
|
+
/** Openings of a release air valve that are long enough to settle T2 */
|
|
524
|
+
export function ventings(points, until) {
|
|
525
|
+
return onIntervals(points, -Infinity, until).filter((i) => i.end - i.start >= MIN_VENT);
|
|
526
|
+
}
|
|
527
|
+
/** T2 in liters at the end of a venting: the median of its second half */
|
|
528
|
+
export function ventedLevel(t2, vent) {
|
|
529
|
+
// The second half of the vent, after the water settled
|
|
530
|
+
const from = (vent.start + vent.end) / 2;
|
|
531
|
+
const levels = [];
|
|
532
|
+
for (let i = Math.max(0, indexAt(t2, from)); i < t2.length; i++) {
|
|
533
|
+
const p = t2[i];
|
|
534
|
+
if (p.t > vent.end)
|
|
535
|
+
break;
|
|
536
|
+
if (p.t >= from)
|
|
537
|
+
levels.push(p.v);
|
|
538
|
+
}
|
|
539
|
+
if (levels.length === 0)
|
|
540
|
+
return undefined;
|
|
541
|
+
levels.sort((a, b) => a - b);
|
|
542
|
+
return { t: vent.end, v: levels[levels.length >> 1] };
|
|
543
|
+
}
|
|
544
|
+
/**
|
|
545
|
+
* T2 in liters, measured while it was vented, one point per venting.
|
|
546
|
+
* Starts with the last venting before the range, if it was loaded.
|
|
547
|
+
*/
|
|
548
|
+
export function ventedLevels(ds, side) {
|
|
549
|
+
const t2 = ds.tanks[side === "A" ? "t2a" : "t2b"];
|
|
550
|
+
const before = ds.ventedBefore?.[side];
|
|
551
|
+
const result = before ? [before] : [];
|
|
552
|
+
for (const vent of ventings(ds.io[sideIo("release_air", side)], ds.until)) {
|
|
553
|
+
const level = ventedLevel(t2, vent);
|
|
554
|
+
if (level && level.t > (before?.t ?? -Infinity))
|
|
555
|
+
result.push(level);
|
|
556
|
+
}
|
|
557
|
+
return result;
|
|
558
|
+
}
|
|
559
|
+
const roomCache = new WeakMap();
|
|
560
|
+
export function roomAt(ds, t) {
|
|
561
|
+
let cache = roomCache.get(ds);
|
|
562
|
+
if (!cache) {
|
|
563
|
+
cache = {
|
|
564
|
+
vented: { A: ventedLevels(ds, "A"), B: ventedLevels(ds, "B") },
|
|
565
|
+
bypass: onIntervals(ds.io[IO.bypass], ds.from, ds.until),
|
|
566
|
+
};
|
|
567
|
+
roomCache.set(ds, cache);
|
|
568
|
+
}
|
|
569
|
+
const { vented } = cache;
|
|
570
|
+
const opening = cache.bypass.find((i) => i.start <= t && t < i.end);
|
|
571
|
+
const bypassOpen = opening !== undefined || (stepAt(ds.io[IO.bypass], t)?.v ?? 0) > 0;
|
|
572
|
+
const restart = opening !== undefined && isScheduledRestart(ds, opening);
|
|
573
|
+
const bypass = bypassOpen && !restart;
|
|
574
|
+
const train = (side) => {
|
|
575
|
+
const last = stepAt(vented[side], t);
|
|
576
|
+
const routine = routineAt(ds, side, t).current;
|
|
577
|
+
const waste = (stepAt(ds.io[sideIo("waste_clean_tank", side)], t)?.v ?? 0) > 0;
|
|
578
|
+
const busy = waste
|
|
579
|
+
? "Waste valve open"
|
|
580
|
+
: routine && !ACCEPTING_ROUTINES.has(routine.name)
|
|
581
|
+
? humanize(routine.name)
|
|
582
|
+
: undefined;
|
|
583
|
+
if (busy) {
|
|
584
|
+
return {
|
|
585
|
+
side,
|
|
586
|
+
liters: 0,
|
|
587
|
+
t2: last?.v,
|
|
588
|
+
measuredAt: last?.t,
|
|
589
|
+
accepting: false,
|
|
590
|
+
reason: `${busy}, not accepting water`,
|
|
591
|
+
};
|
|
592
|
+
}
|
|
593
|
+
if (!last) {
|
|
594
|
+
return {
|
|
595
|
+
side,
|
|
596
|
+
liters: undefined,
|
|
597
|
+
t2: undefined,
|
|
598
|
+
measuredAt: undefined,
|
|
599
|
+
accepting: routine ? true : undefined,
|
|
600
|
+
reason: "No venting with level data yet",
|
|
601
|
+
};
|
|
602
|
+
}
|
|
603
|
+
return {
|
|
604
|
+
side,
|
|
605
|
+
liters: Math.max(0, TRAIN_FULL_LITERS - last.v),
|
|
606
|
+
t2: last.v,
|
|
607
|
+
measuredAt: last.t,
|
|
608
|
+
accepting: true,
|
|
609
|
+
reason: last.v >= TRAIN_FULL_LITERS ? "Full" : "Accepting water",
|
|
610
|
+
};
|
|
611
|
+
};
|
|
612
|
+
const trains = { A: train("A"), B: train("B") };
|
|
613
|
+
const known = [trains.A.liters, trains.B.liters].filter((l) => l !== undefined);
|
|
614
|
+
const partial = !bypass && known.length === 1;
|
|
615
|
+
const liters = bypass
|
|
616
|
+
? 0
|
|
617
|
+
: known.length === 0
|
|
618
|
+
? undefined
|
|
619
|
+
: known.reduce((sum, l) => sum + l, 0);
|
|
620
|
+
return {
|
|
621
|
+
liters,
|
|
622
|
+
partial,
|
|
623
|
+
bypass,
|
|
624
|
+
restart,
|
|
625
|
+
filling: fillingTrain(ds, t),
|
|
626
|
+
trains,
|
|
627
|
+
};
|
|
628
|
+
}
|
|
629
|
+
/** Intervals during which an IO was on, within [from, until] */
|
|
630
|
+
export function onIntervals(points, from, until) {
|
|
631
|
+
if (!points?.length)
|
|
632
|
+
return [];
|
|
633
|
+
const result = [];
|
|
634
|
+
let start;
|
|
635
|
+
let clipped = false;
|
|
636
|
+
for (const p of points) {
|
|
637
|
+
if (p.t > until)
|
|
638
|
+
break;
|
|
639
|
+
const isOn = p.v > 0;
|
|
640
|
+
if (isOn && start === undefined) {
|
|
641
|
+
clipped = p.t < from;
|
|
642
|
+
start = Math.max(p.t, from);
|
|
643
|
+
}
|
|
644
|
+
else if (!isOn && start !== undefined) {
|
|
645
|
+
if (p.t >= from)
|
|
646
|
+
result.push({ start, end: p.t, open: false, clipped });
|
|
647
|
+
start = undefined;
|
|
648
|
+
}
|
|
649
|
+
}
|
|
650
|
+
if (start !== undefined) {
|
|
651
|
+
result.push({ start, end: until, open: true, clipped });
|
|
652
|
+
}
|
|
653
|
+
return result;
|
|
654
|
+
}
|
|
655
|
+
/** Explains the capacity of a tank whose scale is the sensor's range */
|
|
656
|
+
export function capacityNote(level) {
|
|
657
|
+
if (level.capacity === undefined)
|
|
658
|
+
return "";
|
|
659
|
+
return level.capacity === Math.round(level.max)
|
|
660
|
+
? `The tank holds about ${level.capacity} L (estimated from Hydraloop's 3D model).`
|
|
661
|
+
: `The scale is the sensor's range of ${Math.round(level.max)} L. The tank holds about ${level.capacity} L (estimated from Hydraloop's 3D model).`;
|
|
662
|
+
}
|