Files
generalFunctions/src/registry/backends/FileBackend.js
znetsixe 0a4b52f517 feat(registry): AssetResolver + diffuser supplier curves (Jäger / Aerostrip / PIK / PRK)
Two related changes bundled together because the diffuser curve files
only make sense once the registry namespace they live in exists.

src/registry — new asset-metadata resolver:
- AssetResolver with synchronous resolve(namespace, id) + lazy cache,
  async refresh() for future remote pulls.
- FileBackend (per-id or single-file layouts, case-insensitive) and a
  stub HttpBackend (disabled unless EVOLV_ASSET_REMOTE=1).
- Namespaces: curves, menu, monsterSamples, monsterSpecs, units. Menu
  namespace re-keys by inner softwareType + filename so editors that
  pass either string resolve to the same tree.
- README explains how to add a namespace.
- AssetCategoryManager (datasets/assetData/index.js) becomes a thin
  facade over the resolver so existing consumers don't move.
- 246/246 tests pass — including the 39-test registry suite.

datasets/assetData — file moves + new diffuser data:
- modelData/*.json deleted; curves/*.json is the canonical home.
- New diffuser.json menu tree with GVA, Jäger, Aquaconsult/Entec,
  PIK/PRK suppliers.
- gva-elastox-r.json migrated from the inline _loadSpecs hardcode,
  re-tagged coverageBasis="bottom-coverage-pct" (the legacy 2.4
  elements/m² was a prior mis-conversion; we can't recover the
  original % so it's a single-point curve under key "0").
- jaeger-jetflex-td-65-2-g-epdm-1000.json — extracted from the Jäger
  EPDM-1000mm SSOTE/DWP chart on the data sheet (vector-PDF read).
  SSOTE 8.20→6.40 %/m, DWP 25→48 mbar across Q 2-12 Nm³/h. Single
  coverage (vendor doesn't state test conditions).
- aerostrip-phoenix.json — 4-coverage SOTE family at 4.75 m water
  depth (DD 5/10/15/20 %, flux 10-70 Nm³/h·m²) from the Entec/de
  Winter 2023-11-22 dataset; DWP curve from the 21 % @ 4.05 m chart.
- pik300.json / prk300.json — 5-coverage SOTE + SSOTR (DD 5-25 %)
  with split DWP per model variant, water depth ≈ 4.0 m inferred from
  the SOTE↔SSOTR ratio in the source spreadsheet.

src/configs/diffuser.json:
- New asset.{model, assetTagNumber} block so the editor's selected
  model id survives validation.
- diffuser.density description corrected to "Bottom coverage [%]";
  default 2.4 → 15 (typical fine-bubble install).

src/configs/{rotatingMachine,valve}.json: small alignment edits that
came with the registry phase.

src/menu/asset.js + src/menu/aquonSamples.js: rewritten as facades
over assetResolver, keeping the editor-side cascade behaviour intact.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-12 17:12:13 +02:00

97 lines
3.1 KiB
JavaScript

'use strict';
const fs = require('fs');
const path = require('path');
/**
* FileBackend — reads JSON payloads from a directory on disk.
*
* Two layouts supported:
* - 'per-id' one file per id (filename minus .json is the id)
* - 'single-file' one file containing an array; index by a field name
*
* Returns Map<lowerCaseId, payload>. Case-insensitive lookups by default —
* matches how loadCurve worked historically.
*/
class FileBackend {
constructor(opts = {}) {
const {
baseDir,
layout = 'per-id',
filePath,
arrayKey,
indexField,
exclude = [],
caseInsensitive = true,
} = opts;
if (!baseDir) throw new TypeError('FileBackend: baseDir is required');
if (layout !== 'per-id' && layout !== 'single-file') {
throw new TypeError(`FileBackend: unsupported layout '${layout}'`);
}
if (layout === 'single-file' && !filePath) {
throw new TypeError('FileBackend: single-file layout requires filePath');
}
this.baseDir = baseDir;
this.layout = layout;
this.filePath = filePath;
this.arrayKey = arrayKey;
this.indexField = indexField;
this.exclude = new Set(exclude);
this.caseInsensitive = caseInsensitive;
}
_norm(k) {
return this.caseInsensitive ? String(k).toLowerCase() : String(k);
}
loadAll() {
if (this.layout === 'per-id') return this._loadPerId();
return this._loadSingleFile();
}
async refresh() {
// No actual I/O penalty on local disk; the async surface exists so
// callers can `await resolver.refresh()` symmetrically with future
// HttpBackend implementations.
return this.loadAll();
}
_loadPerId() {
const map = new Map();
if (!fs.existsSync(this.baseDir)) return map;
const entries = fs.readdirSync(this.baseDir, { withFileTypes: true });
for (const entry of entries) {
if (!entry.isFile()) continue;
if (!entry.name.endsWith('.json')) continue;
const id = path.basename(entry.name, '.json');
if (this.exclude.has(id)) continue;
const raw = fs.readFileSync(path.join(this.baseDir, entry.name), 'utf8');
const data = JSON.parse(raw);
map.set(this._norm(id), data);
}
return map;
}
_loadSingleFile() {
const full = path.resolve(this.baseDir, this.filePath);
if (!fs.existsSync(full)) return new Map();
const data = JSON.parse(fs.readFileSync(full, 'utf8'));
const arr = this.arrayKey ? data[this.arrayKey] : data;
if (!Array.isArray(arr)) {
throw new Error(
`FileBackend(single-file): expected array at ${this.arrayKey || '<root>'} in ${full}`,
);
}
const map = new Map();
for (const entry of arr) {
const k = entry && this.indexField ? entry[this.indexField] : null;
if (k != null) map.set(this._norm(k), entry);
}
return map;
}
}
module.exports = FileBackend;