hydraloop-diagram-react 1.2.0 → 1.2.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/model.d.ts CHANGED
@@ -254,10 +254,34 @@ export declare const SKIM_STATES: Set<string>;
254
254
  export declare function routineAt(ds: Pick<Dataset, "routines">, side: RoutineSide, t: number): {
255
255
  current: RoutineEvent | undefined;
256
256
  start: number | undefined;
257
- lastIncoming: number | undefined;
258
257
  lastTransfer: number | undefined;
259
258
  };
260
259
  export declare function stateAt(ds: Dataset, t: number): DeviceState;
260
+ export type RoutineSegment = Interval & {
261
+ side: RoutineSide;
262
+ name: string;
263
+ phase: TrainPhase;
264
+ /** Sub-states in order, with their start time */
265
+ states: {
266
+ t: number;
267
+ state: string;
268
+ }[];
269
+ };
270
+ /** Routines as intervals per side. Consecutive sub-states are merged. */
271
+ export declare function routineSegments(ds: Dataset): RoutineSegment[];
272
+ export type ValveRoutineSegment = RoutineSegment & {
273
+ /** The train the A/B valve selects instead, so this train takes no water */
274
+ valve?: Side;
275
+ };
276
+ export declare function valveRoutineSegments(ds: Dataset): ValveRoutineSegment[];
277
+ /**
278
+ * When greywater last entered `side` before `t`: the start of the latest
279
+ * INCOMING_WATER the A/B valve sent to it. That is either the train's own
280
+ * INCOMING_WATER while the valve selects it, or the other train's while the
281
+ * valve selects this one, as HDM sometimes only reports it for the train the
282
+ * valve doesn't select.
283
+ */
284
+ export declare function lastIncomingWater(segments: ValveRoutineSegment[], side: Side, t: number): number | undefined;
261
285
  /** T2 at which a train is full and starts FILTH_REMOVAL */
262
286
  export declare const TRAIN_FULL_LITERS = 134;
263
287
  /** Routines in which a train accepts greywater */
package/dist/model.js CHANGED
@@ -187,7 +187,6 @@ export const SKIM_STATES = new Set([
187
187
  export function routineAt(ds, side, t) {
188
188
  let current;
189
189
  let start;
190
- let lastIncoming;
191
190
  let lastTransfer;
192
191
  for (const r of ds.routines) {
193
192
  if (r.t > t)
@@ -196,14 +195,12 @@ export function routineAt(ds, side, t) {
196
195
  continue;
197
196
  if (!current || current.name !== r.name) {
198
197
  start = r.t;
199
- if (r.name === "INCOMING_WATER")
200
- lastIncoming = r.t;
201
198
  if (r.name === "TRANSFER")
202
199
  lastTransfer = r.t;
203
200
  }
204
201
  current = r;
205
202
  }
206
- return { current, start, lastIncoming, lastTransfer };
203
+ return { current, start, lastTransfer };
207
204
  }
208
205
  export function stateAt(ds, t) {
209
206
  const tanks = {};
@@ -238,8 +235,11 @@ export function stateAt(ds, t) {
238
235
  const active = (ds.behaviors ?? []).filter((b) => !OTHER_BEHAVIORS.has(b.name) &&
239
236
  b.start <= t &&
240
237
  (b.open ? t <= b.end : t < b.end));
238
+ const segments = valveRoutineSegments(ds);
239
+ /** Trains with reported incoming water while the A/B valve selects the other */
240
+ const incomingElsewhere = new Set();
241
241
  const train = (side) => {
242
- const { current, start, lastIncoming, lastTransfer } = routineAt(ds, side, t);
242
+ const { current, start, lastTransfer } = routineAt(ds, side, t);
243
243
  if (current)
244
244
  routine[side] = current;
245
245
  const waste = io[sideIo("waste_clean_tank", side)];
@@ -267,7 +267,7 @@ export function stateAt(ds, t) {
267
267
  const behaviors = active.filter((b) => b.side === side);
268
268
  const base = {
269
269
  side,
270
- lastIncoming,
270
+ lastIncoming: lastIncomingWater(segments, side, t),
271
271
  lastTransfer,
272
272
  evidence,
273
273
  t2ToT1,
@@ -298,6 +298,25 @@ export function stateAt(ds, t) {
298
298
  since: (draining ? waste : transfer)?.since,
299
299
  };
300
300
  }
301
+ // HDM runs INCOMING_WATER and the SKIM_LEVEL_DETERMINATION after it on
302
+ // both trains, but greywater only enters the train the A/B valve selects
303
+ const segment = segments.find((s) => s.side === side &&
304
+ s.start <= t &&
305
+ (t < s.end || (s.open && t === s.end)));
306
+ if (current && segment?.valve && routinePhase(current.name) !== "idle") {
307
+ if (routinePhase(current.name) === "receiving") {
308
+ incomingElsewhere.add(side);
309
+ }
310
+ return {
311
+ ...base,
312
+ evidence: [...evidence, `A/B valve set to ${segment.valve}`],
313
+ phase: "idle",
314
+ label: PHASE_LABELS.idle,
315
+ detail: `HDM routine: ${routineText}`,
316
+ confidence: "valve",
317
+ since: segment.start,
318
+ };
319
+ }
301
320
  if (current) {
302
321
  return {
303
322
  ...base,
@@ -403,13 +422,14 @@ export function stateAt(ds, t) {
403
422
  // the A/B valve doesn't select, while only the selected train's T2 rises.
404
423
  // So incoming water on either side goes to the train of the A/B valve.
405
424
  const filling = fillingTrain(ds, t);
406
- const incoming = ["A", "B"].filter((s) => trains[s].phase === "receiving");
425
+ const incoming = ["A", "B"].filter((s) => trains[s].phase === "receiving" || incomingElsewhere.has(s));
407
426
  if (filling && incoming.length > 0) {
408
427
  const other = filling === "A" ? "B" : "A";
409
428
  if (trains[other].phase === "receiving") {
410
429
  trains[other] = {
411
430
  ...trains[other],
412
431
  phase: "idle",
432
+ label: PHASE_LABELS.idle,
413
433
  confidence: "valve",
414
434
  evidence: [...trains[other].evidence, `A/B valve set to ${filling}`],
415
435
  };
@@ -445,6 +465,105 @@ export function stateAt(ds, t) {
445
465
  room: roomAt(ds, t),
446
466
  };
447
467
  }
468
+ /** Routines as intervals per side. Consecutive sub-states are merged. */
469
+ export function routineSegments(ds) {
470
+ const result = [];
471
+ const open = new Map();
472
+ for (const r of ds.routines) {
473
+ if (r.t > ds.until)
474
+ break;
475
+ const current = open.get(r.side);
476
+ if (current && current.name === r.name) {
477
+ current.states.push({ t: r.t, state: r.state });
478
+ continue;
479
+ }
480
+ if (current) {
481
+ current.end = r.t;
482
+ current.open = false;
483
+ result.push(current);
484
+ }
485
+ open.set(r.side, {
486
+ side: r.side,
487
+ name: r.name,
488
+ phase: routinePhase(r.name),
489
+ start: r.t,
490
+ end: ds.until,
491
+ open: true,
492
+ clipped: r.t < ds.from,
493
+ states: [{ t: r.t, state: r.state }],
494
+ });
495
+ }
496
+ result.push(...open.values());
497
+ return result
498
+ .filter((s) => s.end > ds.from)
499
+ .map((s) => ({ ...s, start: Math.max(s.start, ds.from) }))
500
+ .sort((a, b) => a.start - b.start);
501
+ }
502
+ /**
503
+ * Routine segments where STANDBY, INCOMING_WATER and SKIM_LEVEL_DETERMINATION
504
+ * are split at A/B valve switches. The parts while the valve selects the other
505
+ * train get `valve` set to that train, as no greywater enters, and get the
506
+ * "idle" phase (see `stateAt`). The firmware runs INCOMING_WATER and the
507
+ * SKIM_LEVEL_DETERMINATION after it on both trains, but on the train that
508
+ * takes no water it only closes the release air valve again. Unless it checks
509
+ * the level of a full train (FIND_HIGHEST_WATER_POINT), which starts filth
510
+ * removal.
511
+ */
512
+ const VALVE_ROUTINES = new Set([
513
+ "STANDBY",
514
+ "INCOMING_WATER",
515
+ "SKIM_LEVEL_DETERMINATION",
516
+ ]);
517
+ export function valveRoutineSegments(ds) {
518
+ const switches = (ds.io[IO.abSelector] ?? []).map((p) => p.t);
519
+ return routineSegments(ds).flatMap((s) => {
520
+ if (!VALVE_ROUTINES.has(s.name) || s.side === "SYSTEM")
521
+ return [s];
522
+ if (s.states.some((x) => x.state === "FIND_HIGHEST_WATER_POINT")) {
523
+ return [s];
524
+ }
525
+ const cuts = [s.start, ...switches.filter((t) => t > s.start && t < s.end)];
526
+ const parts = [];
527
+ cuts.forEach((start, i) => {
528
+ const end = cuts[i + 1] ?? s.end;
529
+ const filling = fillingTrain(ds, start);
530
+ const valve = filling && filling !== s.side ? filling : undefined;
531
+ const last = parts[parts.length - 1];
532
+ if (last && last.valve === valve) {
533
+ last.end = end;
534
+ return;
535
+ }
536
+ parts.push({
537
+ ...s,
538
+ start,
539
+ end,
540
+ open: s.open && end === s.end,
541
+ phase: valve ? "idle" : s.phase,
542
+ valve,
543
+ });
544
+ });
545
+ return parts;
546
+ });
547
+ }
548
+ /**
549
+ * When greywater last entered `side` before `t`: the start of the latest
550
+ * INCOMING_WATER the A/B valve sent to it. That is either the train's own
551
+ * INCOMING_WATER while the valve selects it, or the other train's while the
552
+ * valve selects this one, as HDM sometimes only reports it for the train the
553
+ * valve doesn't select.
554
+ */
555
+ export function lastIncomingWater(segments, side, t) {
556
+ let last;
557
+ for (const s of segments) {
558
+ if (s.start > t)
559
+ break;
560
+ if (s.name !== "INCOMING_WATER" || s.side === "SYSTEM")
561
+ continue;
562
+ if ((s.valve ?? s.side) === side)
563
+ last = s.start;
564
+ }
565
+ return last;
566
+ }
448
567
  // ── Room for greywater ─────────────────────────────────────────────────────
449
568
  // Derived from 30 days of an H600's data, not documented by Hydraloop:
450
569
  // - Greywater only enters the train selected by the A/B valve, and only in
package/dist/version.d.ts CHANGED
@@ -1 +1 @@
1
- export declare const VERSION = "1.2.0";
1
+ export declare const VERSION = "1.2.1";
package/dist/version.js CHANGED
@@ -1 +1 @@
1
- export const VERSION = "1.2.0";
1
+ export const VERSION = "1.2.1";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "hydraloop-diagram-react",
3
- "version": "1.2.0",
3
+ "version": "1.2.1",
4
4
  "description": "React flow diagram of a Hydraloop H600, and the model that derives what the device is doing from its signals",
5
5
  "type": "module",
6
6
  "exports": {
package/readme.md CHANGED
@@ -62,6 +62,7 @@ Times in the tooltips use the runtime's locale and time zone. As they can differ
62
62
 
63
63
  - `stateAt(dataset, t)`: the `DeviceState` at time `t` (milliseconds since epoch): tank levels and trends, IO values, the phase of each train and how it was determined, T3, T4, the supply of the outlets, and `room`.
64
64
  - `roomAt(dataset, t)`: how many liters of greywater the H600 can take before they are bypassed to the sewer.
65
+ - `valveRoutineSegments(dataset)`: the routines of each side as intervals (`routineSegments(dataset)`), split where the A/B valve switches. HDM runs `INCOMING_WATER` and the `SKIM_LEVEL_DETERMINATION` after it on both trains, but greywater only enters the train the valve selects. The parts of the other train have `valve` set to the selected train. `lastIncomingWater(segments, side, t)` is when greywater last entered a train. `stateAt()` uses both.
65
66
  - `routineAt(dataset, side, t)`, `fillingTrain(dataset, t)`, `isScheduledRestart(dataset, interval)`, `ventedLevels(dataset, side)`, `onIntervals(points, from, until)`, `stepAt(points, t)`, `valueAt(points, t)`
66
67
  - `IO`: the names of the IO signals with known roles. `sideIo(name, side)` adds the `_b` suffix of train B.
67
68
  - `caption(captions, name)`, `humanize(text)`, `capacityNote(level)`: labels