From e87faa948a0457102e6076d30f7e561091d6de44 Mon Sep 17 00:00:00 2001 From: varun Date: Wed, 15 May 2024 07:56:20 -0400 Subject: [PATCH] change in iotdevice --- src/controllers/tanksController.js | 282 ++++++++++++++--------------- 1 file changed, 141 insertions(+), 141 deletions(-) diff --git a/src/controllers/tanksController.js b/src/controllers/tanksController.js index 704fc2e8..06198bf3 100644 --- a/src/controllers/tanksController.js +++ b/src/controllers/tanksController.js @@ -1203,53 +1203,53 @@ exports.calculateCapacity = async (req, reply) => { } }; -exports.IotDevice = async (req, reply) => { - try { - const { hardwareId, mode, tanks } = req.body; +// 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 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 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 }); +// // 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(); +// // 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 +// // 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(); - } +// 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); +// // 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 }); - } -}; +// // send the latest documents +// reply.code(200).send({ latestOttanks }); +// } catch (err) { +// // send an error response +// reply.code(500).send({ error: err.message }); +// } +// }; @@ -1367,124 +1367,124 @@ for (const outputConnection of existingTank.connections.outputConnections) { } }; -exports.IotDevice1 = async (req, reply) => { - try { - const { hardwareId, mode, tanks } = req.body; +// exports.IotDevice1 = 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(); - } - console.log(tanks) - // Update waterlevel in tanksSchema for each tank - for (const tank of tanks) { - console.log(tank) - const { tankhardwareId, tankHeight } = tank; - console.log(hardwareId,tankhardwareId) - // Find the corresponding tank in tanksSchema - const existingTank = await Tank.findOne({ hardwareId, tankhardwareId }); - if (!existingTank) { - console.log(`No tank found for tankhardwareId '${tankhardwareId}'. Skipping.`); - continue; // Skip to the next iteration - } - console.log(existingTank,"existing tank") - if (existingTank) { - // Update the waterlevel using the tankHeight value +// // 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(); +// } +// console.log(tanks) +// // Update waterlevel in tanksSchema for each tank +// for (const tank of tanks) { +// console.log(tank) +// const { tankhardwareId, tankHeight } = tank; +// console.log(hardwareId,tankhardwareId) +// // Find the corresponding tank in tanksSchema +// const existingTank = await Tank.findOne({ hardwareId, tankhardwareId }); +// if (!existingTank) { +// console.log(`No tank found for tankhardwareId '${tankhardwareId}'. Skipping.`); +// continue; // Skip to the next iteration +// } +// console.log(existingTank,"existing tank") +// 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); - -// Function to add commas to a number -function numberWithCommas(x) { - return x.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ","); -} +// 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); +// 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) => {