Files
generalFunctions/src/helper/childRegistrationUtils.js

111 lines
3.6 KiB
JavaScript
Raw Normal View History

2025-06-10 12:36:39 +02:00
class ChildRegistrationUtils {
constructor(mainClass) {
this.mainClass = mainClass;
2025-06-10 12:36:39 +02:00
this.logger = mainClass.logger;
this.registeredChildren = new Map();
2025-06-10 12:36:39 +02:00
}
async registerChild(child, positionVsParent, distance) {
2026-02-23 13:17:47 +01:00
if (!child || typeof child !== 'object') {
this.logger?.warn('registerChild skipped: invalid child payload');
return false;
}
if (!child.config?.functionality || !child.config?.general) {
this.logger?.warn('registerChild skipped: missing child config/functionality/general');
return false;
}
const softwareType = (child.config.functionality.softwareType || '').toLowerCase();
2026-02-23 13:17:47 +01:00
const name = child.config.general.name || child.config.general.id || 'unknown';
const id = child.config.general.id || name;
this.logger.debug(`Registering child: ${name} (${id}) as ${softwareType} at ${positionVsParent}`);
2025-06-10 12:36:39 +02:00
// Enhanced child setup - multiple parents
2025-09-29 16:06:06 +02:00
if (Array.isArray(child.parent)) {
child.parent.push(this.mainClass);
} else {
child.parent = [this.mainClass];
2025-09-29 16:06:06 +02:00
}
child.positionVsParent = positionVsParent;
2025-06-10 12:36:39 +02:00
// Enhanced measurement container with rich context
if (child.measurements) {
child.measurements.setChildId(id);
child.measurements.setChildName(name);
child.measurements.setParentRef(this.mainClass);
2025-06-10 12:36:39 +02:00
}
// Store child in your expected structure
this._storeChild(child, softwareType);
2025-06-10 12:36:39 +02:00
// Track registration for utilities
this.registeredChildren.set(id, {
2025-06-10 12:36:39 +02:00
child,
softwareType,
position: positionVsParent,
registeredAt: Date.now()
2025-06-10 12:36:39 +02:00
});
// IMPORTANT: Only call parent registration - no automatic handling and if parent has this function then try to register this child
if (typeof this.mainClass.registerChild === 'function') {
return this.mainClass.registerChild(child, softwareType);
}
2025-06-10 12:36:39 +02:00
this.logger.info(`✅ Child ${name} registered successfully`);
2026-02-23 13:17:47 +01:00
return true;
2025-06-10 12:36:39 +02:00
}
_storeChild(child, softwareType) {
// Maintain your existing structure
if (!this.mainClass.child) this.mainClass.child = {};
2026-02-23 13:17:47 +01:00
const typeKey = softwareType || 'unknown';
if (!this.mainClass.child[typeKey]) this.mainClass.child[typeKey] = {};
const { category = "sensor" } = child.config.asset || {};
2026-02-23 13:17:47 +01:00
if (!this.mainClass.child[typeKey][category]) {
this.mainClass.child[typeKey][category] = [];
2025-06-10 12:36:39 +02:00
}
2026-02-23 13:17:47 +01:00
this.mainClass.child[typeKey][category].push(child);
2025-06-10 12:36:39 +02:00
}
// NEW: Utility methods for parent to use
getChildrenOfType(softwareType, category = null) {
if (!this.mainClass.child[softwareType]) return [];
if (category) {
return this.mainClass.child[softwareType][category] || [];
2025-06-10 12:36:39 +02:00
}
// Return all children of this software type
return Object.values(this.mainClass.child[softwareType]).flat();
}
2025-06-10 12:36:39 +02:00
getChildById(childId) {
return this.registeredChildren.get(childId)?.child || null;
}
2025-06-10 12:36:39 +02:00
getAllChildren() {
return Array.from(this.registeredChildren.values()).map(r => r.child);
2025-06-10 12:36:39 +02:00
}
// NEW: Debugging utilities
logChildStructure() {
this.logger.debug('Current child structure:', JSON.stringify(
Object.keys(this.mainClass.child).reduce((acc, softwareType) => {
acc[softwareType] = Object.keys(this.mainClass.child[softwareType]).reduce((catAcc, category) => {
catAcc[category] = this.mainClass.child[softwareType][category].map(c => ({
id: c.config.general.id,
name: c.config.general.name
}));
return catAcc;
}, {});
return acc;
}, {}), null, 2
));
}
2025-06-10 12:36:39 +02:00
}
2026-02-23 13:17:47 +01:00
module.exports = ChildRegistrationUtils;