|
|
@ -87,7 +87,10 @@ async function deleteOldRecords() {
|
|
|
|
|
|
|
|
|
|
|
|
exports.addTanks = async (req, reply) => {
|
|
|
|
exports.addTanks = async (req, reply) => {
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const customerId = req.params.customerId;
|
|
|
|
const customerId = req.params.customerId;
|
|
|
|
const { hardwareId, tankhardwareId,tankName,tankLocation } = req.body;
|
|
|
|
const { hardwareId, tankhardwareId,tankName,tankLocation } = req.body;
|
|
|
|
|
|
|
|
|
|
|
@ -114,6 +117,8 @@ exports.addTanks = async (req, reply) => {
|
|
|
|
capacity: req.body.capacity,
|
|
|
|
capacity: req.body.capacity,
|
|
|
|
typeOfWater: req.body.typeOfWater,
|
|
|
|
typeOfWater: req.body.typeOfWater,
|
|
|
|
tankLocation: req.body.tankLocation.toLowerCase(),
|
|
|
|
tankLocation: req.body.tankLocation.toLowerCase(),
|
|
|
|
|
|
|
|
waterCapacityPerCm:req.body.waterCapacityPerCm,
|
|
|
|
|
|
|
|
height:req.body.height
|
|
|
|
// ... other fields
|
|
|
|
// ... other fields
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
@ -937,6 +942,70 @@ exports.consumption = async (req, reply) => {
|
|
|
|
// };
|
|
|
|
// };
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// exports.calculateCapacity = async (req, reply) => {
|
|
|
|
|
|
|
|
// try {
|
|
|
|
|
|
|
|
// const shape = req.body.shape;
|
|
|
|
|
|
|
|
// if (shape === "rectangle") {
|
|
|
|
|
|
|
|
// const { length, width, height } = req.body;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// // Convert input units from feet to meters
|
|
|
|
|
|
|
|
// const length_m = length * 0.3048;
|
|
|
|
|
|
|
|
// const width_m = width * 0.3048;
|
|
|
|
|
|
|
|
// const height_m = height * 0.3048;
|
|
|
|
|
|
|
|
// console.log(length_m,width_m,height_m)
|
|
|
|
|
|
|
|
// // Ensure all parameters are valid numbers
|
|
|
|
|
|
|
|
// if (isNaN(length_m) || isNaN(width_m) || isNaN(height_m)) {
|
|
|
|
|
|
|
|
// reply.code(400).send("Invalid input parameters");
|
|
|
|
|
|
|
|
// return;
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// // Calculate the capacity of the water tank in liters
|
|
|
|
|
|
|
|
// const capacity = length_m * width_m * height_m * 1000;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// reply.send({ status_code: 200, capacity: capacity });
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// return { message: "success" };
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
// if (shape === "cylinder") {
|
|
|
|
|
|
|
|
// console.log("hii1");
|
|
|
|
|
|
|
|
// const { length, diameter } = req.body;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// // Convert input units from feet to meters
|
|
|
|
|
|
|
|
// const length_m = length * 0.3048;
|
|
|
|
|
|
|
|
// const diameter_m = diameter * 0.3048;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// // Ensure all parameters are valid numbers
|
|
|
|
|
|
|
|
// if (isNaN(length_m) || isNaN(diameter_m)) {
|
|
|
|
|
|
|
|
// reply.code(400).send("Invalid input parameters");
|
|
|
|
|
|
|
|
// return;
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// // Calculate the capacity of the water tank in liters
|
|
|
|
|
|
|
|
// const radius = diameter_m / 2;
|
|
|
|
|
|
|
|
// const volume = Math.PI * Math.pow(radius, 2) * length_m;
|
|
|
|
|
|
|
|
// const capacity = volume * 1000;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// reply.send({ status_code: 200, capacity: capacity });
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// return { message: "success" };
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// // Add similar conversions for other shapes if necessary
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// if (shape === "userdefined") {
|
|
|
|
|
|
|
|
// const capacity = req.body;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// reply.send({ status_code: 200, capacity: capacity });
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// return { message: "success" };
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
// } catch (err) {
|
|
|
|
|
|
|
|
// throw boom.boomify(err);
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
// };
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
exports.calculateCapacity = async (req, reply) => {
|
|
|
|
exports.calculateCapacity = async (req, reply) => {
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
const shape = req.body.shape;
|
|
|
|
const shape = req.body.shape;
|
|
|
@ -947,7 +1016,7 @@ exports.calculateCapacity = async (req, reply) => {
|
|
|
|
const length_m = length * 0.3048;
|
|
|
|
const length_m = length * 0.3048;
|
|
|
|
const width_m = width * 0.3048;
|
|
|
|
const width_m = width * 0.3048;
|
|
|
|
const height_m = height * 0.3048;
|
|
|
|
const height_m = height * 0.3048;
|
|
|
|
console.log(length_m,width_m,height_m)
|
|
|
|
|
|
|
|
// Ensure all parameters are valid numbers
|
|
|
|
// Ensure all parameters are valid numbers
|
|
|
|
if (isNaN(length_m) || isNaN(width_m) || isNaN(height_m)) {
|
|
|
|
if (isNaN(length_m) || isNaN(width_m) || isNaN(height_m)) {
|
|
|
|
reply.code(400).send("Invalid input parameters");
|
|
|
|
reply.code(400).send("Invalid input parameters");
|
|
|
@ -957,12 +1026,14 @@ exports.calculateCapacity = async (req, reply) => {
|
|
|
|
// Calculate the capacity of the water tank in liters
|
|
|
|
// Calculate the capacity of the water tank in liters
|
|
|
|
const capacity = length_m * width_m * height_m * 1000;
|
|
|
|
const capacity = length_m * width_m * height_m * 1000;
|
|
|
|
|
|
|
|
|
|
|
|
reply.send({ status_code: 200, capacity: capacity });
|
|
|
|
// Calculate the water capacity for a 1 centimeter height
|
|
|
|
|
|
|
|
const waterCapacityPerCm = length_m * width_m * 0.01 * 1000;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
reply.send({ status_code: 200, capacity: capacity, waterCapacityPerCm: waterCapacityPerCm });
|
|
|
|
|
|
|
|
|
|
|
|
return { message: "success" };
|
|
|
|
return { message: "success" };
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (shape === "cylinder") {
|
|
|
|
if (shape === "cylinder") {
|
|
|
|
console.log("hii1");
|
|
|
|
|
|
|
|
const { length, diameter } = req.body;
|
|
|
|
const { length, diameter } = req.body;
|
|
|
|
|
|
|
|
|
|
|
|
// Convert input units from feet to meters
|
|
|
|
// Convert input units from feet to meters
|
|
|
@ -980,7 +1051,10 @@ exports.calculateCapacity = async (req, reply) => {
|
|
|
|
const volume = Math.PI * Math.pow(radius, 2) * length_m;
|
|
|
|
const volume = Math.PI * Math.pow(radius, 2) * length_m;
|
|
|
|
const capacity = volume * 1000;
|
|
|
|
const capacity = volume * 1000;
|
|
|
|
|
|
|
|
|
|
|
|
reply.send({ status_code: 200, capacity: capacity });
|
|
|
|
// Calculate the water capacity for a 1 centimeter height
|
|
|
|
|
|
|
|
const waterCapacityPerCm = Math.PI * Math.pow(radius, 2) * 0.01 * 1000;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
reply.send({ status_code: 200, capacity: capacity, waterCapacityPerCm: waterCapacityPerCm });
|
|
|
|
|
|
|
|
|
|
|
|
return { message: "success" };
|
|
|
|
return { message: "success" };
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -988,9 +1062,12 @@ exports.calculateCapacity = async (req, reply) => {
|
|
|
|
// Add similar conversions for other shapes if necessary
|
|
|
|
// Add similar conversions for other shapes if necessary
|
|
|
|
|
|
|
|
|
|
|
|
if (shape === "userdefined") {
|
|
|
|
if (shape === "userdefined") {
|
|
|
|
const capacity = req.body;
|
|
|
|
const capacity = req.body.capacity; // Assuming capacity is provided directly
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Calculate the water capacity for a 1 centimeter height
|
|
|
|
|
|
|
|
const waterCapacityPerCm = capacity / req.body.height; // Assuming height of the shape is provided
|
|
|
|
|
|
|
|
|
|
|
|
reply.send({ status_code: 200, capacity: capacity });
|
|
|
|
reply.send({ status_code: 200, capacity: capacity, waterCapacityPerCm: waterCapacityPerCm });
|
|
|
|
|
|
|
|
|
|
|
|
return { message: "success" };
|
|
|
|
return { message: "success" };
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -1001,6 +1078,53 @@ exports.calculateCapacity = async (req, reply) => {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// exports.IotDevice = async (req, reply) => {
|
|
|
|
|
|
|
|
// try {
|
|
|
|
|
|
|
|
// const { hardwareId, mode, tanks } = req.body;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// // create a new tank document with the current date and time
|
|
|
|
|
|
|
|
// const currentDate = new Date();
|
|
|
|
|
|
|
|
// const date = currentDate.toISOString(); // save the date as an ISO string
|
|
|
|
|
|
|
|
// const time = currentDate.toLocaleTimeString('en-IN', { hour12: false, timeZone: 'Asia/Kolkata' });
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// // Create an array of tank documents
|
|
|
|
|
|
|
|
// const tankDocuments = tanks.map(tank => ({
|
|
|
|
|
|
|
|
// tankhardwareId: tank.tankhardwareId,
|
|
|
|
|
|
|
|
// tankHeight: tank.tankHeight,
|
|
|
|
|
|
|
|
// maxLevel: tank.maxLevel,
|
|
|
|
|
|
|
|
// minLevel: tank.minLevel,
|
|
|
|
|
|
|
|
// date: date,
|
|
|
|
|
|
|
|
// time: time
|
|
|
|
|
|
|
|
// }));
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// // create a new IotData document with the provided data
|
|
|
|
|
|
|
|
// const ottank = new IotData({ hardwareId, mode, tanks: tankDocuments, date, time });
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// // save the document to MongoDB
|
|
|
|
|
|
|
|
// await ottank.save();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// // delete previous records except the three latest ones
|
|
|
|
|
|
|
|
// const previousRecords = await IotData.find({ hardwareId })
|
|
|
|
|
|
|
|
// .sort({ date: -1, time: -1 })
|
|
|
|
|
|
|
|
// .skip(3); // skip the three latest documents
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// for (const record of previousRecords) {
|
|
|
|
|
|
|
|
// await record.remove();
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// // get the latest three documents sorted in descending order of date and time
|
|
|
|
|
|
|
|
// const latestOttanks = await IotData.find({ hardwareId })
|
|
|
|
|
|
|
|
// .sort({ date: -1, time: -1 })
|
|
|
|
|
|
|
|
// .limit(3);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// // send the latest documents
|
|
|
|
|
|
|
|
// reply.code(200).send({ latestOttanks });
|
|
|
|
|
|
|
|
// } catch (err) {
|
|
|
|
|
|
|
|
// // send an error response
|
|
|
|
|
|
|
|
// reply.code(500).send({ error: err.message });
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
// };
|
|
|
|
|
|
|
|
|
|
|
|
exports.IotDevice = async (req, reply) => {
|
|
|
|
exports.IotDevice = async (req, reply) => {
|
|
|
|
try {
|
|
|
|
try {
|
|
|
@ -1021,28 +1145,80 @@ exports.IotDevice = async (req, reply) => {
|
|
|
|
time: time
|
|
|
|
time: time
|
|
|
|
}));
|
|
|
|
}));
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// create a new IotData document with the provided data
|
|
|
|
// create a new IotData document with the provided data
|
|
|
|
const ottank = new IotData({ hardwareId, mode, tanks: tankDocuments, date, time });
|
|
|
|
const ottank = new IotData({ hardwareId, mode, tanks: tankDocuments, date, time });
|
|
|
|
|
|
|
|
|
|
|
|
// save the document to MongoDB
|
|
|
|
// save the document to MongoDB
|
|
|
|
await ottank.save();
|
|
|
|
await ottank.save();
|
|
|
|
|
|
|
|
|
|
|
|
// delete previous records except the three latest ones
|
|
|
|
// Delete excess records (keep only the latest three records)
|
|
|
|
const previousRecords = await IotData.find({ hardwareId })
|
|
|
|
const recordsToKeep = 3;
|
|
|
|
|
|
|
|
const recordsToDelete = await IotData.find({ hardwareId })
|
|
|
|
.sort({ date: -1, time: -1 })
|
|
|
|
.sort({ date: -1, time: -1 })
|
|
|
|
.skip(3); // skip the three latest documents
|
|
|
|
.skip(recordsToKeep);
|
|
|
|
|
|
|
|
|
|
|
|
for (const record of previousRecords) {
|
|
|
|
for (const record of recordsToDelete) {
|
|
|
|
await record.remove();
|
|
|
|
await record.remove();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// get the latest three documents sorted in descending order of date and time
|
|
|
|
// Update waterlevel in tanksSchema for each tank
|
|
|
|
|
|
|
|
for (const tank of tanks) {
|
|
|
|
|
|
|
|
const { tankhardwareId, tankHeight } = tank;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Find the corresponding tank in tanksSchema
|
|
|
|
|
|
|
|
const existingTank = await Tank.findOne({ hardwareId, tankhardwareId });
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 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);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
await existingTank.save();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Send the latest three documents
|
|
|
|
const latestOttanks = await IotData.find({ hardwareId })
|
|
|
|
const latestOttanks = await IotData.find({ hardwareId })
|
|
|
|
.sort({ date: -1, time: -1 })
|
|
|
|
.sort({ date: -1, time: -1 });
|
|
|
|
.limit(3);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// send the latest documents
|
|
|
|
|
|
|
|
reply.code(200).send({ latestOttanks });
|
|
|
|
reply.code(200).send({ latestOttanks });
|
|
|
|
} catch (err) {
|
|
|
|
} catch (err) {
|
|
|
|
// send an error response
|
|
|
|
// send an error response
|
|
|
@ -1051,7 +1227,6 @@ exports.IotDevice = async (req, reply) => {
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// exports.getIotD = async(req, reply) => {
|
|
|
|
// exports.getIotD = async(req, reply) => {
|
|
|
|
// try {
|
|
|
|
// try {
|
|
|
|
// await IotData.find({hardwareId: req.query.hardwareId})
|
|
|
|
// await IotData.find({hardwareId: req.query.hardwareId})
|
|
|
|