From 8d07a8e50d9497e3ce6ebc24bb8e82ca4b23cff6 Mon Sep 17 00:00:00 2001 From: Varun Date: Mon, 2 Sep 2024 13:27:13 +0530 Subject: [PATCH] write api for iot for integrated --- src/controllers/tanksController.js | 109 +++++++++++++++++++++++++++++ src/models/tanks.js | 4 +- src/routes/tanksRoute.js | 35 +++++++++ 3 files changed, 146 insertions(+), 2 deletions(-) diff --git a/src/controllers/tanksController.js b/src/controllers/tanksController.js index f961d237..da543b12 100644 --- a/src/controllers/tanksController.js +++ b/src/controllers/tanksController.js @@ -2749,6 +2749,115 @@ 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; + console.log(hardwareId) + + // 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/models/tanks.js b/src/models/tanks.js index 67469115..3a023f7f 100644 --- a/src/models/tanks.js +++ b/src/models/tanks.js @@ -119,8 +119,8 @@ const motordataSchema = new mongoose.Schema({ const tankSchema = new mongoose.Schema({ tankhardwareId: { type: String }, tankHeight: { type: String, required: true }, - maxLevel: { type: String, required: true }, - minLevel: { type: String, required: true }, + maxLevel: { type: String,default:null }, + minLevel: { type: String,default:null }, date: { type: String, required: true }, time: { type: String, required: true } }); diff --git a/src/routes/tanksRoute.js b/src/routes/tanksRoute.js index 76448fe8..bc597103 100644 --- a/src/routes/tanksRoute.js +++ b/src/routes/tanksRoute.js @@ -484,6 +484,41 @@ module.exports = function (fastify, opts, next) { }); + fastify.route({ + method: "POST", + url: "/api/APIWriteforstandalone", + schema: { + tags: ["Tank"], + description: "This is for creating an IOT Device for slave integrated in master", + summary: "this is to Create IOT Device for slave integrated in master", + body: { + type: "object", + properties: { + hardwareId: { type: "string" }, + Motor_status: { type: "string" }, + tanks: { + type: "array", + items: { + type: "object", + properties: { + tankhardwareId: { type: "string" }, + tankHeight: { type: "string" } + }, + required: ["tankhardwareId", "tankHeight"] + } + } + }, + required: ["hardwareId", "Motor_status", "tanks"] + }, + security: [ + { + basicAuth: [] + } + ] + }, + handler: tanksController.IotDeviceforstandalone + }); +