diff --git a/src/specificClass.js b/src/specificClass.js index ca96884..ef00497 100644 --- a/src/specificClass.js +++ b/src/specificClass.js @@ -63,10 +63,21 @@ class PumpingStation { this.stations[child.config.general.id] = child; } else if (softwareType === 'machinegroup') { this.machineGroups[child.config.general.id] = child; - this._registerPredictedFlowChild(child); } - if (softwareType === 'machine' || softwareType === 'pumpingstation' || softwareType === 'machinegroup') { + // Register predicted-flow subscription. Only register the HIGHEST- + // level aggregator: if a machinegroup is present, subscribe to IT + // (its flow.predicted already aggregates all child machines). Do NOT + // also subscribe to individual machines — that would double-count + // because each pump's flow is included in the group total. + // + // Individual machines (softwareType='machine') are only subscribed + // when there is NO machinegroup parent — i.e., pumps wired directly + // to the pumping station without an MGC in between. + if (softwareType === 'machinegroup' || softwareType === 'pumpingstation') { + this._registerPredictedFlowChild(child); + } else if (softwareType === 'machine' && Object.keys(this.machineGroups).length === 0) { + // Direct-child machine, no group above it — register its flow. this._registerPredictedFlowChild(child); } } @@ -97,18 +108,21 @@ class PumpingStation { const childId = child.config.general.id ?? childName; let posKey; - let eventNames; + let eventName; switch (position) { case 'downstream': case 'out': case 'atequipment': posKey = 'out'; - eventNames = ['flow.predicted.downstream', 'flow.predicted.atequipment']; + // Subscribe to ONE event only. 'downstream' is the most specific + // — avoids double-counting from 'atequipment' which carries the + // same total flow on a different event name. + eventName = 'flow.predicted.downstream'; break; case 'upstream': case 'in': posKey = 'in'; - eventNames = ['flow.predicted.upstream', 'flow.predicted.atequipment']; + eventName = 'flow.predicted.upstream'; break; default: this.logger.warn(`Unsupported predicted flow position "${position}" from ${childName}`); @@ -132,7 +146,7 @@ class PumpingStation { .value(eventData.value, ts, unit); }; - eventNames.forEach((ev) => child.measurements.emitter.on(ev, handler)); + child.measurements.emitter.on(eventName, handler); } /* --------------------------- Calibration --------------------------- */