|
|
|
@ -1154,60 +1154,57 @@ exports.IotDevice = async (req, reply) => {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Update waterlevel in tanksSchema for each tank
|
|
|
|
|
for (const tank of tanks) {
|
|
|
|
|
const { tankhardwareId, tankHeight } = tank;
|
|
|
|
|
for (const tank of tanks) {
|
|
|
|
|
const { tankhardwareId, tankHeight } = tank;
|
|
|
|
|
|
|
|
|
|
// Skip if tankHeight is null or undefined
|
|
|
|
|
if (tankHeight === null || tankHeight === undefined) {
|
|
|
|
|
continue; // Skip this iteration and move to the next tank
|
|
|
|
|
}
|
|
|
|
|
// Find the corresponding tank in tanksSchema
|
|
|
|
|
const existingTank = await Tank.findOne({ hardwareId, tankhardwareId });
|
|
|
|
|
|
|
|
|
|
// Find the corresponding tank in tanksSchema
|
|
|
|
|
const existingTank = await Tank.findOne({ hardwareId, tankhardwareId });
|
|
|
|
|
|
|
|
|
|
if (existingTank) {
|
|
|
|
|
// Update the waterlevel using the tankHeight value
|
|
|
|
|
if (existingTank) {
|
|
|
|
|
// Update the waterlevel using the tankHeight value
|
|
|
|
|
|
|
|
|
|
const tank_height1 = (parseInt(existingTank.height.replace(/,/g, ''), 10)) * 30.48;
|
|
|
|
|
console.log(tank_height1, 25);
|
|
|
|
|
const tank_height1 = (parseInt(existingTank.height.replace(/,/g, ''), 10)) * 30.48;
|
|
|
|
|
console.log(tank_height1, 25);
|
|
|
|
|
|
|
|
|
|
// 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, ",");
|
|
|
|
|
}
|
|
|
|
|
// 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, ",");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 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);
|
|
|
|
|
// 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 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);
|
|
|
|
|
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, ",");
|
|
|
|
|
}
|
|
|
|
|
// 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);
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
|
|
existingTank.waterlevel = parseInt(formatted_water_level.replace(/,/g, ''), 10);
|
|
|
|
|
console.log(existingTank.waterlevel);
|
|
|
|
|
|
|
|
|
|
// Save the updated tank document
|
|
|
|
|
await existingTank.save();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Save the updated tank document
|
|
|
|
|
await existingTank.save();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Send the latest three documents
|
|
|
|
|
const latestOttanks = await IotData.find({ hardwareId })
|
|
|
|
@ -1220,7 +1217,6 @@ for (const tank of tanks) {
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// exports.getIotD = async(req, reply) => {
|
|
|
|
|
// try {
|
|
|
|
|
// await IotData.find({hardwareId: req.query.hardwareId})
|
|
|
|
|