changes in connections

master
varun 1 year ago
parent 13769846bd
commit 950345f7dd

@ -535,48 +535,45 @@ exports.createConnections = async (req, body) => {
tankInfo.connections.outputConnections = usertobeInserted.outputConnections.map(connection => {
return {
outputConnections: connection.outputConnections,
output_type: connection.output_type,
motor_id: connection.motor_id || null,
motor_status: connection.motor_status || "0",
motor_stop_status: connection.motor_stop_status || "1",
outputismotor: connection.hasOwnProperty("outputismotor") ? connection.outputismotor : false,
capacity: connection.capacity || null,
water_level: connection.water_level || null
output_type: connection.output_type,
motor_id: connection.motor_id || null,
motor_status: connection.motor_status || "0",
motor_stop_status: connection.motor_stop_status || "1",
outputismotor: connection.hasOwnProperty("outputismotor") ? connection.outputismotor : false,
capacity: connection.capacity || null,
water_level: connection.water_level || null
};
});
}
const tank_connections = await tankInfo.save();
const connection_data_check = tank_connections.connections.inputConnections;
const sump_names = connection_data_check.map(d => d.inputConnections);
console.log(sump_names);
const connection_data = usertobeInserted.outputConnections;
console.log(connection_data, "connection_data");
for (const data of connection_data) {
if (data['output_type'] === "overhead") {
const tankName = data['outputConnections'];
if (sump_names.includes(tankname)) {
console.log(`${tankname} exists in ${sump_names}`);
} else {
const tankConnections = await Tank.findOneAndUpdate(
{ customerId: customerId.toString(), tankName: tankName },
{
$addToSet: {
'connections.inputConnections': {
$each: [{ inputConnections: tankname, input_type: 'sump', inputismotor: data.outputismotor || false }],
},
// 'connections.outputConnections': {
// $each: [{ outputConnections: tankname, output_type: 'overhead'}],
// },
// Check if the current tank is already included in the inputConnections of any other tanks
const tanksWithCurrentTankAsInput = await Tank.find({ 'connections.inputConnections.inputConnections': tankname });
// If the current tank is not in the inputConnections of any other tank, add it
if (tanksWithCurrentTankAsInput.length === 0) {
// Loop through each tank to add the current tank as an inputConnection
for (const tank of tanksWithCurrentTankAsInput) {
await Tank.findOneAndUpdate(
{ customerId: customerId.toString(), tankName: tank.tankName },
{
$addToSet: {
'connections.inputConnections': {
inputConnections: tankname,
input_type: 'sump',
inputismotor: data.outputismotor || false
}
}
},
},
{ new: true }
);
console.log("tankConnections", tankConnections.connections.inputConnections);
console.log("tankConnections", tankConnections.connections.outputConnections);
{ new: true }
);
}
}
}
}
@ -590,6 +587,7 @@ exports.createConnections = async (req, body) => {
// exports.createConnections = async (req, body) => {
// try {
// const customerId = req.params.customerId;

@ -1252,218 +1252,218 @@ 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 excess records (keep only the latest three records)
const recordsToKeep = 3;
const recordsToDelete = await IotData.find({ hardwareId })
.sort({ date: -1, time: -1 })
.skip(recordsToKeep);
for (const record of recordsToDelete) {
await record.remove();
}
// 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
// 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 excess records (keep only the latest three records)
// const recordsToKeep = 3;
// const recordsToDelete = await IotData.find({ hardwareId })
// .sort({ date: -1, time: -1 })
// .skip(recordsToKeep);
// for (const record of recordsToDelete) {
// await record.remove();
// }
const tank_height1 = (parseInt(existingTank.height.replace(/,/g, ''), 10)) * 30.48;
console.log(tank_height1, 25);
// // Update waterlevel in tanksSchema for each tank
// for (const tank of tanks) {
// const { tankhardwareId, tankHeight } = tank;
// 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, ",");
}
// // Find the corresponding tank in tanksSchema
// const existingTank = await Tank.findOne({ hardwareId, tankhardwareId });
// 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)
// if (existingTank) {
// // Update the waterlevel using the tankHeight value
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 tank_height1 = (parseInt(existingTank.height.replace(/,/g, ''), 10)) * 30.48;
// console.log(tank_height1, 25);
// 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, ",");
// }
const formatted_water_level = numberWithCommas(water_level);
console.log(formatted_water_level, 4);
// // 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);
existingTank.waterlevel = parseInt(formatted_water_level.replace(/,/g, ''), 10);
console.log(existingTank.waterlevel);
// 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, ",");
// }
// Save the updated tank document
await existingTank.save();
}
}
// const formatted_water_level = numberWithCommas(water_level);
// console.log(formatted_water_level, 4);
// Send the latest three documents
const latestOttanks = await IotData.find({ hardwareId })
.sort({ date: -1, time: -1 });
// existingTank.waterlevel = parseInt(formatted_water_level.replace(/,/g, ''), 10);
// console.log(existingTank.waterlevel);
reply.code(200).send({ latestOttanks });
} catch (err) {
// send an error response
reply.code(500).send({ error: err.message });
}
};
// exports.IotDevice2 = 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 updated tank document
// await existingTank.save();
// }
// }
// // save the document to MongoDB
// await ottank.save();
// // Send the latest three documents
// const latestOttanks = await IotData.find({ hardwareId })
// .sort({ date: -1, time: -1 });
// // Delete excess records (keep only the latest three records)
// const recordsToKeep = 3;
// const recordsToDelete = await IotData.find({ hardwareId })
// .sort({ date: -1, time: -1 })
// .skip(recordsToKeep);
// reply.code(200).send({ latestOttanks });
// } catch (err) {
// // send an error response
// reply.code(500).send({ error: err.message });
// }
// };
// for (const record of recordsToDelete) {
// await record.remove();
// }
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 excess records (keep only the latest three records)
const recordsToKeep = 3;
const recordsToDelete = await IotData.find({ hardwareId })
.sort({ date: -1, time: -1 })
.skip(recordsToKeep);
for (const record of recordsToDelete) {
await record.remove();
}
// // Update waterlevel in tanksSchema for each tank
// for (const tank of tanks) {
// const { tankhardwareId, tankHeight } = tank;
// 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 });
// 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();
// for (const outputConnection of existingTank.connections.outputConnections) {
// const linkedTank = await Tank.findOne({ customerId: customerId, tankName: outputConnection.outputConnections, tankLocation: outputConnection.output_type });
// if (linkedTank) {
// // linkedTank.waterlevel = existingTank.waterlevel;
// //await linkedTank.save();
// 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 });
if (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 = water_level.toString();
// 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 = water_level.toString();
await linkedTank.save();
}
}
}
}
}
}
// // Send the latest three documents
// const latestOttanks = await IotData.find({ hardwareId })
// .sort({ date: -1, time: -1 });
// Send the latest three documents
const latestOttanks = await IotData.find({ hardwareId })
.sort({ date: -1, time: -1 });
// reply.code(200).send({ latestOttanks });
// } catch (err) {
// // send an error response
// reply.code(500).send({ error: err.message });
// }
// };
reply.code(200).send({ latestOttanks });
} catch (err) {
// send an error response
reply.code(500).send({ error: err.message });
}
};
// exports.IotDevice1 = async (req, reply) => {

@ -158,7 +158,7 @@ module.exports = function (fastify, opts, next) {
},
],
},
preHandler: fastify.auth([fastify.authenticate]),
//preHandler: fastify.auth([fastify.authenticate]),
handler: createConnectionController.createConnections,
});

Loading…
Cancel
Save