|
|
@ -1175,7 +1175,6 @@ exports.calculateCapacity = async (req, reply) => {
|
|
|
|
// reply.code(500).send({ error: err.message });
|
|
|
|
// reply.code(500).send({ error: err.message });
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
// };
|
|
|
|
// };
|
|
|
|
|
|
|
|
|
|
|
|
exports.IotDevice = async (req, reply) => {
|
|
|
|
exports.IotDevice = async (req, reply) => {
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
const { hardwareId, mode, tanks } = req.body;
|
|
|
|
const { hardwareId, mode, tanks } = req.body;
|
|
|
@ -1218,49 +1217,34 @@ exports.IotDevice = async (req, reply) => {
|
|
|
|
// Find the corresponding tank in tanksSchema
|
|
|
|
// Find the corresponding tank in tanksSchema
|
|
|
|
const existingTank = await Tank.findOne({ hardwareId, tankhardwareId });
|
|
|
|
const existingTank = await Tank.findOne({ hardwareId, tankhardwareId });
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (existingTank) {
|
|
|
|
if (existingTank) {
|
|
|
|
// Update the waterlevel using the tankHeight value
|
|
|
|
// Update the waterlevel using the tankHeight value
|
|
|
|
|
|
|
|
const tank_height = parseInt(existingTank.height.replace(/,/g, ''), 10) * 30.48;
|
|
|
|
const tank_height1 = (parseInt(existingTank.height.replace(/,/g, ''), 10)) * 30.48;
|
|
|
|
const water_level_height = tank_height - tankHeight;
|
|
|
|
console.log(tank_height1, 25);
|
|
|
|
const waterCapacityPerCm = parseInt(existingTank.waterCapacityPerCm.replace(/,/g, ''), 10);
|
|
|
|
|
|
|
|
const water_level = water_level_height * waterCapacityPerCm;
|
|
|
|
// The value of tank_height1 is a number, not a string, so you cannot use replace on it.
|
|
|
|
existingTank.waterlevel = water_level.toString(); // Convert to string as per schema definition
|
|
|
|
// If you want to format it with commas, you can create a function to add commas to a number.
|
|
|
|
|
|
|
|
function numberWithCommas(x) {
|
|
|
|
|
|
|
|
return x.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Now you can use the function to format the tank_height1 value with commas.
|
|
|
|
|
|
|
|
const formatted_tank_height1 = numberWithCommas(tank_height1);
|
|
|
|
|
|
|
|
console.log(formatted_tank_height1, 25);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const tank_height = parseInt(formatted_tank_height1.replace(/,/g, ''), 10);
|
|
|
|
|
|
|
|
console.log(tank_height);
|
|
|
|
|
|
|
|
// console.log(tank_height,1)
|
|
|
|
|
|
|
|
const water_level_height = tank_height - tankHeight
|
|
|
|
|
|
|
|
console.log(water_level_height,2)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const waterCapacityPerCm = parseInt(existingTank.waterCapacityPerCm.replace(/,/g, ''), 10)
|
|
|
|
|
|
|
|
console.log(waterCapacityPerCm,3)
|
|
|
|
|
|
|
|
const water_level = water_level_height * waterCapacityPerCm;
|
|
|
|
|
|
|
|
console.log(water_level, 4);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Function to add commas to a number
|
|
|
|
|
|
|
|
function numberWithCommas(x) {
|
|
|
|
|
|
|
|
return x.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const formatted_water_level = numberWithCommas(water_level);
|
|
|
|
|
|
|
|
console.log(formatted_water_level, 4);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
existingTank.waterlevel = parseInt(formatted_water_level.replace(/,/g, ''), 10);
|
|
|
|
|
|
|
|
console.log(existingTank.waterlevel);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Save the updated tank document
|
|
|
|
// Save the updated tank document
|
|
|
|
await existingTank.save();
|
|
|
|
await existingTank.save();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Update water level of tanks linked through output connections
|
|
|
|
|
|
|
|
for (const outputConnection of existingTank.connections.outputConnections) {
|
|
|
|
|
|
|
|
const linkedTank = await Tank.findOne({ hardwareId: outputConnection.outputConnections });
|
|
|
|
|
|
|
|
if (linkedTank) {
|
|
|
|
|
|
|
|
linkedTank.waterlevel = existingTank.waterlevel;
|
|
|
|
|
|
|
|
await linkedTank.save();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Update water level of tanks linked through input connections
|
|
|
|
|
|
|
|
for (const inputConnection of existingTank.connections.inputConnections) {
|
|
|
|
|
|
|
|
const linkedTank = await Tank.findOne({ hardwareId: inputConnection.inputConnections });
|
|
|
|
|
|
|
|
if (linkedTank) {
|
|
|
|
|
|
|
|
linkedTank.waterlevel = existingTank.waterlevel;
|
|
|
|
|
|
|
|
await linkedTank.save();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|