244 lines
7.8 KiB
JavaScript
244 lines
7.8 KiB
JavaScript
// ChildRegistrationUtils.js
|
|
class ChildRegistrationUtils {
|
|
constructor(mainClass) {
|
|
this.mainClass = mainClass; // Reference to the main class
|
|
this.logger = mainClass.logger;
|
|
}
|
|
|
|
async registerChild(child, positionVsParent) {
|
|
const { softwareType } = child.config.functionality;
|
|
const { name, id, unit } = child.config.general;
|
|
const { type = "", subType = "" } = child.config.asset || {};
|
|
const emitter = child.emitter;
|
|
|
|
//define position vs parent in child
|
|
child.positionVsParent = positionVsParent;
|
|
child.parent = this.mainClass;
|
|
|
|
if (!this.mainClass.child) this.mainClass.child = {};
|
|
if (!this.mainClass.child[softwareType])
|
|
this.mainClass.child[softwareType] = {};
|
|
if (!this.mainClass.child[softwareType][type])
|
|
this.mainClass.child[softwareType][type] = {};
|
|
if (!this.mainClass.child[softwareType][type][subType])
|
|
this.mainClass.child[softwareType][type][subType] = {};
|
|
|
|
// Use an array to handle multiple subtypes
|
|
if (!Array.isArray(this.mainClass.child[softwareType][type][subType])) {
|
|
this.mainClass.child[softwareType][type][subType] = [];
|
|
}
|
|
|
|
// Update the child in the cloud when available and supply the new child on base of tagcode OLIFANT WE NEED TO FIX THIS SO ITS DYNAMIC!
|
|
/*
|
|
try{
|
|
const url = "https://pimmoerman.nl/rdlab/tagcode.app/v2.1/api/asset/create_asset.php?";
|
|
const TagCode = child.config.asset.tagCode;
|
|
//console.log(`Register child => ${TagCode}`);
|
|
const completeURL = url + `asset_product_model_id=1&asset_product_model_uuid=123456789&asset_name=AssetNaam&asset_description=Beschrijving&asset_status=actief&asset_profile_id=1&asset_location_id=1&asset_process_id=11&asset_tag_number=${TagCode}&child_assets=[L6616]`;
|
|
|
|
await fetch(completeURL, {
|
|
method: 'POST',
|
|
headers: {
|
|
'Content-Type': 'application/json'
|
|
}
|
|
});
|
|
|
|
}catch(e){
|
|
console.log("Error saving assetID and tagnumber", e);
|
|
}*/
|
|
|
|
// Push the new child to the array of the mainclass so we can track the childs
|
|
this.mainClass.child[softwareType][type][subType].push({
|
|
name,
|
|
id,
|
|
unit,
|
|
emitter,
|
|
});
|
|
|
|
//then connect the child depending on the type subtype etc..
|
|
this.connectChild(
|
|
id,
|
|
softwareType,
|
|
emitter,
|
|
type,
|
|
child,
|
|
subType,
|
|
positionVsParent
|
|
);
|
|
}
|
|
|
|
connectChild(
|
|
id,
|
|
softwareType,
|
|
emitter,
|
|
type,
|
|
child,
|
|
subType,
|
|
positionVsParent
|
|
) {
|
|
this.logger.debug(
|
|
`Connecting child id=${id}: desc=${softwareType}, type=${type},subType=${subType}, position=${positionVsParent}`
|
|
);
|
|
|
|
switch (softwareType) {
|
|
case "measurement":
|
|
this.logger.debug(
|
|
`Registering measurement child: ${id} with type=${type}`
|
|
);
|
|
this.connectMeasurement(child, subType, positionVsParent);
|
|
break;
|
|
|
|
case "machine":
|
|
this.logger.debug(`Registering complete machine child: ${id}`);
|
|
this.connectMachine(child);
|
|
break;
|
|
|
|
case "valve":
|
|
this.logger.debug(`Registering complete valve child: ${id}`);
|
|
this.connectValve(child);
|
|
break;
|
|
|
|
case "actuator":
|
|
this.logger.debug(`Registering linear actuator child: ${id}`);
|
|
this.connectActuator(child,positionVsParent);
|
|
break;
|
|
|
|
default:
|
|
this.logger.error(`Child registration unrecognized desc: ${desc}`);
|
|
this.logger.error(`Unrecognized softwareType: ${softwareType}`);
|
|
}
|
|
}
|
|
|
|
connectMeasurement(child, subType, position) {
|
|
this.logger.debug(
|
|
`Connecting measurement child: ${subType} with position=${position}`
|
|
);
|
|
|
|
// Check if subType is valid
|
|
if (!subType) {
|
|
this.logger.error(`Invalid subType for measurement: ${subType}`);
|
|
return;
|
|
}
|
|
|
|
// initialize the measurement to a number - logging each step for debugging
|
|
try {
|
|
this.logger.debug(
|
|
`Initializing measurement: ${subType}, position: ${position} value: 0`
|
|
);
|
|
const typeResult = this.mainClass.measurements.type(subType);
|
|
const variantResult = typeResult.variant("measured");
|
|
const positionResult = variantResult.position(position);
|
|
positionResult.value(0);
|
|
|
|
this.logger.debug(
|
|
`Subscribing on mAbs event for measurement: ${subType}, position: ${position}`
|
|
);
|
|
// Listen for the mAbs event and update the measurement
|
|
|
|
this.logger.debug(
|
|
`Successfully initialized measurement: ${subType}, position: ${position}`
|
|
);
|
|
} catch (error) {
|
|
this.logger.error(`Failed to initialize measurement: ${error.message}`);
|
|
return;
|
|
}
|
|
|
|
child.emitter.on("mAbs", (value) => {
|
|
// Use the same method chaining approach that worked during initialization
|
|
this.mainClass.measurements
|
|
.type(subType)
|
|
.variant("measured")
|
|
.position(position)
|
|
.value(value);
|
|
this.mainClass.updateMeasurement("measured", subType, value, position);
|
|
//this.logger.debug(`--------->>>>>>>>>Updated measurement: ${subType}, value: ${value}, position: ${position}`);
|
|
});
|
|
}
|
|
|
|
connectMachine(machine) {
|
|
if (!machine) {
|
|
this.logger.error("Invalid machine provided.");
|
|
return;
|
|
}
|
|
|
|
const machineId = Object.keys(this.mainClass.machines).length + 1;
|
|
this.mainClass.machines[machineId] = machine;
|
|
|
|
this.logger.info(
|
|
`Setting up pressureChange listener for machine ${machineId}`
|
|
);
|
|
|
|
machine.emitter.on("pressureChange", () =>
|
|
this.mainClass.handlePressureChange(machine)
|
|
);
|
|
|
|
//update of child triggers the handler
|
|
this.mainClass.handleChildChange();
|
|
|
|
this.logger.info(`Machine ${machineId} registered successfully.`);
|
|
}
|
|
|
|
connectValve(valve) {
|
|
if (!valve) {
|
|
this.logger.warn("Invalid valve provided.");
|
|
return;
|
|
}
|
|
const valveId = Object.keys(this.mainClass.valves).length + 1;
|
|
this.mainClass.valves[valveId] = valve; // Gooit valve object in de valves attribute met valve objects
|
|
|
|
valve.state.emitter.on("positionChange", (data) => {
|
|
//ValveGroupController abboneren op klepstand verandering
|
|
this.mainClass.logger.debug(`Position change of valve detected: ${data}`);
|
|
this.mainClass.calcValveFlows();
|
|
}); //bepaal nieuwe flow per valve
|
|
valve.emitter.on("deltaPChange", () => {
|
|
this.mainClass.logger.debug("DeltaP change of valve detected");
|
|
this.mainClass.calcMaxDeltaP();
|
|
}); //bepaal nieuwe max deltaP
|
|
|
|
this.logger.info(`Valve ${valveId} registered successfully.`);
|
|
}
|
|
|
|
connectActuator(actuator, positionVsParent) {
|
|
if (!actuator) {
|
|
this.logger.warn("Invalid actuator provided.");
|
|
return;
|
|
}
|
|
|
|
//Special case gateGroupControl
|
|
if (
|
|
this.mainClass.config.functionality.softwareType == "gateGroupControl"
|
|
) {
|
|
if (Object.keys(this.mainClass.actuators).length < 2) {
|
|
if (positionVsParent == "downstream") {
|
|
this.mainClass.actuators[0] = actuator;
|
|
}
|
|
|
|
if (positionVsParent == "upstream") {
|
|
this.mainClass.actuators[1] = actuator;
|
|
}
|
|
//define emitters
|
|
actuator.state.emitter.on("positionChange", (data) => {
|
|
this.mainClass.logger.debug(`Position change of actuator detected: ${data}`);
|
|
this.mainClass.eventUpdate();
|
|
});
|
|
|
|
//define emitters
|
|
actuator.state.emitter.on("stateChange", (data) => {
|
|
this.mainClass.logger.debug(`State change of actuator detected: ${data}`);
|
|
this.mainClass.eventUpdate();
|
|
});
|
|
|
|
} else {
|
|
this.logger.error(
|
|
"Too many actuators registered. Only two are allowed."
|
|
);
|
|
}
|
|
}
|
|
}
|
|
|
|
//wanneer hij deze ontvangt is deltaP van een van de valves veranderd (kan ook zijn niet child zijn, maar dat maakt niet uit)
|
|
}
|
|
|
|
module.exports = ChildRegistrationUtils;
|