change in iot

master
varun 1 year ago
parent 7c6e6c746d
commit f7a6472f91

@ -1252,208 +1252,307 @@ exports.calculateCapacity = async (req, reply) => {
exports.IotDevice = async (req, reply) => { exports.IotDevice = async (req, reply) => {
try { try {
const { hardwareId, mode, tanks } = req.body; const { hardwareId, mode, tanks } = req.body;
// create a new tank document with the current date and time // create a new tank document with the current date and time
const currentDate = new Date(); const currentDate = new Date();
const date = currentDate.toISOString(); // save the date as an ISO string const date = currentDate.toISOString(); // save the date as an ISO string
const time = currentDate.toLocaleTimeString('en-IN', { hour12: false, timeZone: 'Asia/Kolkata' }); const time = currentDate.toLocaleTimeString('en-IN', { hour12: false, timeZone: 'Asia/Kolkata' });
// Create an array of tank documents // Create an array of tank documents
const tankDocuments = tanks.map(tank => ({ const tankDocuments = tanks.map(tank => ({
tankhardwareId: tank.tankhardwareId, tankhardwareId: tank.tankhardwareId,
tankHeight: tank.tankHeight, tankHeight: tank.tankHeight,
maxLevel: tank.maxLevel, maxLevel: tank.maxLevel,
minLevel: tank.minLevel, minLevel: tank.minLevel,
date: date, date: date,
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 excess records (keep only the latest three records) // Delete excess records (keep only the latest three records)
const recordsToKeep = 3; const recordsToKeep = 3;
const recordsToDelete = await IotData.find({ hardwareId }) const recordsToDelete = await IotData.find({ hardwareId })
.sort({ date: -1, time: -1 }) .sort({ date: -1, time: -1 })
.skip(recordsToKeep); .skip(recordsToKeep);
for (const record of recordsToDelete) { for (const record of recordsToDelete) {
await record.remove(); 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
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 })
.sort({ date: -1, time: -1 });
reply.code(200).send({ latestOttanks });
} catch (err) {
// send an error response
reply.code(500).send({ error: err.message });
}
};
// Update waterlevel in tanksSchema for each tank // exports.IotDevice2 = async (req, reply) => {
for (const tank of tanks) { // try {
const { tankhardwareId, tankHeight } = tank; // const { hardwareId, mode, tanks } = req.body;
// Find the corresponding tank in tanksSchema // // create a new tank document with the current date and time
const existingTank = await Tank.findOne({ hardwareId, tankhardwareId }); // 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) { // if (existingTank) {
// Update the waterlevel using the tankHeight value // // Update the waterlevel using the tankHeight value
const tank_height1 = (parseInt(existingTank.height.replace(/,/g, ''), 10)) * 30.48; // const tank_height1 = (parseInt(existingTank.height.replace(/,/g, ''), 10)) * 30.48;
console.log(tank_height1, 25); // console.log(tank_height1, 25);
// The value of tank_height1 is a number, not a string, so you cannot use replace on it. // // 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. // // If you want to format it with commas, you can create a function to add commas to a number.
function numberWithCommas(x) { // function numberWithCommas(x) {
return x.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ","); // return x.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
} // }
// Now you can use the function to format the tank_height1 value with commas. // // Now you can use the function to format the tank_height1 value with commas.
const formatted_tank_height1 = numberWithCommas(tank_height1); // const formatted_tank_height1 = numberWithCommas(tank_height1);
console.log(formatted_tank_height1, 25); // console.log(formatted_tank_height1, 25);
const tank_height = parseInt(formatted_tank_height1.replace(/,/g, ''), 10); // const tank_height = parseInt(formatted_tank_height1.replace(/,/g, ''), 10);
console.log(tank_height); // console.log(tank_height);
// console.log(tank_height,1) // // console.log(tank_height,1)
const water_level_height = tank_height - tankHeight // const water_level_height = tank_height - tankHeight
console.log(water_level_height,2) // console.log(water_level_height,2)
const waterCapacityPerCm = parseInt(existingTank.waterCapacityPerCm.replace(/,/g, ''), 10) // const waterCapacityPerCm = parseInt(existingTank.waterCapacityPerCm.replace(/,/g, ''), 10)
console.log(waterCapacityPerCm,3) // console.log(waterCapacityPerCm,3)
const water_level = water_level_height * waterCapacityPerCm; // const water_level = water_level_height * waterCapacityPerCm;
console.log(water_level, 4); // console.log(water_level, 4);
// Function to add commas to a number // // Function to add commas to a number
function numberWithCommas(x) { // function numberWithCommas(x) {
return x.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ","); // return x.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
} // }
const formatted_water_level = numberWithCommas(water_level); // const formatted_water_level = numberWithCommas(water_level);
console.log(formatted_water_level, 4); // console.log(formatted_water_level, 4);
existingTank.waterlevel = parseInt(formatted_water_level.replace(/,/g, ''), 10); // existingTank.waterlevel = parseInt(formatted_water_level.replace(/,/g, ''), 10);
console.log(existingTank.waterlevel); // console.log(existingTank.waterlevel);
// Save the updated tank document // // Save the updated tank document
await existingTank.save(); // await existingTank.save();
for (const outputConnection of existingTank.connections.outputConnections) { // for (const outputConnection of existingTank.connections.outputConnections) {
const linkedTank = await Tank.findOne({ customerId: customerId, tankName: outputConnection.outputConnections, tankLocation: outputConnection.output_type }); // const linkedTank = await Tank.findOne({ customerId: customerId, tankName: outputConnection.outputConnections, tankLocation: outputConnection.output_type });
if (linkedTank) { // if (linkedTank) {
// linkedTank.waterlevel = existingTank.waterlevel; // // linkedTank.waterlevel = existingTank.waterlevel;
//await linkedTank.save(); // //await linkedTank.save();
// Update water level of tanks linked through input connections of the linked tank // // Update water level of tanks linked through input connections of the linked tank
for (const inputConnection of linkedTank.connections.inputConnections) { // for (const inputConnection of linkedTank.connections.inputConnections) {
if (inputConnection.inputConnections === tank_name) { // if (inputConnection.inputConnections === tank_name) {
inputConnection.water_level = water_level.toString(); // inputConnection.water_level = water_level.toString();
await linkedTank.save(); // await linkedTank.save();
} // }
} // }
} // }
} // }
} // }
} // }
// Send the latest three documents // // 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 });
reply.code(200).send({ latestOttanks }); // reply.code(200).send({ latestOttanks });
} catch (err) { // } catch (err) {
// send an error response // // send an error response
reply.code(500).send({ error: err.message }); // reply.code(500).send({ error: err.message });
} // }
}; // };
exports.IotDevice1 = async (req, reply) => { // exports.IotDevice1 = async (req, reply) => {
try { // try {
const { hardwareId, mode, tanks } = req.body; // 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 // // create a new tank document with the current date and time
for (const tank of tanks) { // const currentDate = new Date();
const { tankhardwareId, tankHeight } = tank; // const date = currentDate.toISOString(); // save the date as an ISO string
// const time = currentDate.toLocaleTimeString('en-IN', { hour12: false, timeZone: 'Asia/Kolkata' });
if (tankHeight === null || tankHeight === undefined) {
continue; // Skip this iteration and move to the next tank // // Create an array of tank documents
} // const tankDocuments = tanks.map(tank => ({
// Find the corresponding tank in tanksSchema // tankhardwareId: tank.tankhardwareId,
const existingTank = await Tank.findOne({ hardwareId, 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;
// 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 });
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_height = parseInt(existingTank.height.replace(/,/g, ''), 10) * 30.48;
const water_level_height = tank_height - tankHeight; // const water_level_height = tank_height - tankHeight;
const customerId = existingTank.customerId; // const customerId = existingTank.customerId;
const waterCapacityPerCm = parseInt(existingTank.waterCapacityPerCm.replace(/,/g, ''), 10); // const waterCapacityPerCm = parseInt(existingTank.waterCapacityPerCm.replace(/,/g, ''), 10);
let water_level = water_level_height * waterCapacityPerCm; // let water_level = water_level_height * waterCapacityPerCm;
water_level = Math.round(water_level); // Round to nearest whole number // water_level = Math.round(water_level); // Round to nearest whole number
existingTank.waterlevel = water_level.toString(); // Convert to string as per schema definition // existingTank.waterlevel = water_level.toString(); // Convert to string as per schema definition
const tank_name = existingTank.tankName; // const tank_name = existingTank.tankName;
// 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 // // Update water level of tanks linked through output connections
for (const outputConnection of existingTank.connections.outputConnections) { // for (const outputConnection of existingTank.connections.outputConnections) {
const linkedTank = await Tank.findOne({ customerId: customerId, tankName: outputConnection.outputConnections, tankLocation: outputConnection.output_type }); // const linkedTank = await Tank.findOne({ customerId: customerId, tankName: outputConnection.outputConnections, tankLocation: outputConnection.output_type });
if (linkedTank) { // if (linkedTank) {
// linkedTank.waterlevel = existingTank.waterlevel; // // linkedTank.waterlevel = existingTank.waterlevel;
//await linkedTank.save(); // //await linkedTank.save();
// Update water level of tanks linked through input connections of the linked tank // // Update water level of tanks linked through input connections of the linked tank
for (const inputConnection of linkedTank.connections.inputConnections) { // for (const inputConnection of linkedTank.connections.inputConnections) {
if (inputConnection.inputConnections === tank_name) { // if (inputConnection.inputConnections === tank_name) {
inputConnection.water_level = water_level.toString(); // inputConnection.water_level = water_level.toString();
await linkedTank.save(); // await linkedTank.save();
} // }
} // }
} // }
} // }
} // }
} // }
// Send the latest three documents // // 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 });
reply.code(200).send({ latestOttanks }); // reply.code(200).send({ latestOttanks });
} catch (err) { // } catch (err) {
// send an error response // // send an error response
reply.code(500).send({ error: err.message }); // reply.code(500).send({ error: err.message });
} // }
}; // };

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

Loading…
Cancel
Save