diff --git a/src/controllers/tanksController.js b/src/controllers/tanksController.js index df3ac654..788de4a1 100644 --- a/src/controllers/tanksController.js +++ b/src/controllers/tanksController.js @@ -2739,6 +2739,118 @@ exports.IotDevice = async (req, reply) => { +exports.IotDeviceforstandalone = async (req, reply) => { + try { + const { hardwareId, Motor_status, 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, + date: date, + time: time + })); + + // create a new IotData document with the provided data + const ottank = new IotData({ hardwareId, Motor_status, 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) { + continue; // Skip to the next tank if not found + } + + const customerId = existingTank.customerId; + const tank_name = existingTank.tankName; + + // Update the waterlevel using the tankHeight value + const tank_height1 = (parseInt(existingTank.height.replace(/,/g, ''), 10)) * 30.48; + const tank_height = parseInt(tank_height1.toFixed(0), 10); // Ensure it's an integer + const water_level_height = tank_height - tankHeight; + const waterCapacityPerCm = parseInt(existingTank.waterCapacityPerCm.replace(/,/g, ''), 10); + + const water_level = parseInt(water_level_height * waterCapacityPerCm, 10); + + if (water_level >= 0) { + existingTank.waterlevel = water_level; + + // Save the updated tank document + await existingTank.save(); + + // Update linked tanks + for (const outputConnection of existingTank.connections.outputConnections) { + const linkedTank = await Tank.findOne({ customerId, tankName: outputConnection.outputConnections, tankLocation: outputConnection.output_type }); + if (linkedTank) { + for (const inputConnection of linkedTank.connections.inputConnections) { + if (inputConnection.inputConnections === tank_name) { + inputConnection.water_level = water_level; + await linkedTank.save(); + } + } + } + } + } + + } + const status = req.body.Motor_status; + + + // Find the tank that contains the specified motor_id in its inputConnections + const tank = await Tank.findOne({ "connections.inputConnections.motor_id": hardwareId }); + + if (!tank) { + return reply.status(404).send({ + status_code: 404, + message: 'Motor not found for the specified motor_id' + }); + } + + // Find the inputConnection with the specified motor_id + const inputConnection = tank.connections.inputConnections.find(conn => conn.motor_id === hardwareId); + + // Update the motor_status of the inputConnection + inputConnection.motor_status = status; + + + // Save the updated tank + await tank.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 }); + } +}; + + diff --git a/src/routes/tanksRoute.js b/src/routes/tanksRoute.js index 76448fe8..56f01617 100644 --- a/src/routes/tanksRoute.js +++ b/src/routes/tanksRoute.js @@ -487,6 +487,7 @@ module.exports = function (fastify, opts, next) { + fastify.get("/api/APIRead", { schema: {