change in iotdata if tank not found for tankhardwareId

master
varun 1 year ago
parent 8cb4f80c25
commit 5ee7cb7c39

@ -1343,75 +1343,40 @@ exports.IotDevice = async (req, reply) => {
for (const tank of tanks) {
const { tankhardwareId, tankHeight } = tank;
// console.log(tankhardwareId)
// Find the corresponding tank in tanksSchema
const existingTank = await Tank.findOne({ hardwareId, tankhardwareId });
const customerId = existingTank.customerId
// console.log(existingTank,tankhardwareId,customerId)
const tank_name = existingTank.tankName
if (existingTank) {
// Update the waterlevel using the tankHeight value
const tank_height1 = (parseInt(existingTank.height.replace(/,/g, ''), 10)) * 30.48;
// The value of tank_height1 is a number, not a string, so you cannot use replace on it.
// 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, ",");
if (!existingTank) {
continue; // Skip to the next tank if not found
}
// Now you can use the function to format the tank_height1 value with commas.
const formatted_tank_height1 = numberWithCommas(tank_height1);
const tank_height = parseInt(formatted_tank_height1.replace(/,/g, ''), 10);
const water_level_height = tank_height - tankHeight
const waterCapacityPerCm = parseInt(existingTank.waterCapacityPerCm.replace(/,/g, ''), 10)
const customerId = existingTank.customerId;
const tank_name = existingTank.tankName;
// Update the waterlevel using the tankHeight value
const tank_height1 = (parseInt(existingTank.height.replace(/,/g, ''), 10)) * 30.48;
const tank_height = parseInt(tank_height1.toFixed(0), 10); // Ensure it's an integer
const water_level_height = tank_height - tankHeight;
const waterCapacityPerCm = parseInt(existingTank.waterCapacityPerCm.replace(/,/g, ''), 10);
const water_level = water_level_height * waterCapacityPerCm;
// 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);
existingTank.waterlevel = parseInt(formatted_water_level.replace(/,/g, ''), 10);
existingTank.waterlevel = water_level;
// Save the updated tank document
await existingTank.save();
for (const outputConnection of existingTank.connections.outputConnections) {
const linkedTank = await Tank.findOne({ customerId: customerId, tankName: outputConnection.outputConnections, tankLocation: outputConnection.output_type });
// Update linked tanks
for (const outputConnection of existingTank.connections.outputConnections) {
const linkedTank = await Tank.findOne({ customerId, tankName: outputConnection.outputConnections, tankLocation: outputConnection.output_type });
if (linkedTank) {
console.log(linkedTank)
// linkedTank.waterlevel = existingTank.waterlevel;
//await linkedTank.save();
// Update water level of tanks linked through input connections of the linked tank
for (const inputConnection of linkedTank.connections.inputConnections) {
if (inputConnection.inputConnections === tank_name) {
inputConnection.water_level = parseInt(formatted_water_level.replace(/,/g, ''), 10)
inputConnection.water_level = water_level;
await linkedTank.save();
}
}
}
}
}
}
// Send the latest three documents
const latestOttanks = await IotData.find({ hardwareId })
@ -1430,6 +1395,7 @@ existingTank.waterlevel = parseInt(formatted_water_level.replace(/,/g, ''), 10);
// exports.IotDevice3 = async (req, reply) => {
// try {
// const { hardwareId, mode, tanks } = req.body;

Loading…
Cancel
Save