From 4d22f4605bbdfc6534ba84536253c6887998f9d8 Mon Sep 17 00:00:00 2001 From: varun Date: Fri, 10 Mar 2023 05:15:33 -0500 Subject: [PATCH] motor on and off status functionality --- src/controllers/tanksController.js | 11 +++++++++++ src/models/tanks.js | 1 + 2 files changed, 12 insertions(+) diff --git a/src/controllers/tanksController.js b/src/controllers/tanksController.js index be2a3b3d..95cfcee2 100644 --- a/src/controllers/tanksController.js +++ b/src/controllers/tanksController.js @@ -270,9 +270,12 @@ exports.motorAction = async (req, reply) => { supplier_tank = req.body.from supplier_tank_type = req.body.from_type receiver_type = req.body.to_type + console.log(supplier_tank) + await Tank.findOneAndUpdate({customerId, tankName: receiver_tank}, { $set: { motor_status: 1 } }); if(supplier_tank_type==="sump" && receiver_type === "overhead"){ const supplier_tank_info1 = await Tank.findOne({ customerId ,tankName:supplier_tank}); + console.log(supplier_tank_info1) initial_update = parseInt(supplier_tank_info1.capacity.replace(/,/g, ''), 10)-200; await Tank.findOneAndUpdate({customerId, tankName: supplier_tank}, { $set: { waterlevel: initial_update } }); const supplier_tank_info = await Tank.findOne({ customerId ,tankName:supplier_tank}); @@ -289,6 +292,8 @@ exports.motorAction = async (req, reply) => { // Check if updating should stop if ((newSupplierWaterLevel/supplier_capacity)*100 <= 5 || (newWaterLevel/receiver_capacity)*100 >= 95 || (newWaterLevel/receiver_capacity)*100 >= desired_water_percentage) { clearInterval(intervalId) + await Tank.findOneAndUpdate({customerId, tankName: receiver_tank}, { $set: { motor_status: 0 } }); + console.log("end"); } else { // Update water levels in database @@ -307,6 +312,8 @@ exports.motorAction = async (req, reply) => { if(supplier_tank_type==="bore" && receiver_type === "sump"){ const receiver_capacity = receiver_tank_info.capacity + await Tank.findOneAndUpdate({customerId, tankName: receiver_tank}, { $set: { motor_status: 1 } }); + let receiver_waterlevel = parseInt(receiver_tank_info.waterlevel.replace(/,/g, ''), 10) intervalId = setInterval(async function () { // Calculate new water levels @@ -314,6 +321,8 @@ exports.motorAction = async (req, reply) => { // Check if updating should stop if ((newWaterLevel/receiver_capacity)*100 >= 97 || (newWaterLevel/receiver_capacity)*100 >= desired_water_percentage ) { clearInterval(intervalId) + await Tank.findOneAndUpdate({customerId, tankName: receiver_tank}, { $set: { motor_status: 0 } }); + console.log("end"); } else { // Update water levels in database @@ -404,6 +413,8 @@ exports.motorAction = async (req, reply) => { // stop_time = new Date().toLocaleString('en-US', {timeZone: 'Asia/Kolkata'}) // console.log(stop_time) clearInterval(intervalId); + await Tank.findOneAndUpdate({customerId, tankName: receiver_tank}, { $set: { motor_status: 0 } }); + // reply.send({ status_code: 200, "stop time": stop_time}); } else { throw new Error("Invalid action"); diff --git a/src/models/tanks.js b/src/models/tanks.js index 6e70f6df..b804b6df 100644 --- a/src/models/tanks.js +++ b/src/models/tanks.js @@ -16,6 +16,7 @@ const tanksSchema = new mongoose.Schema({ typeOfWater: { type: String, default: null }, waterlevel: { type: String, default: "0" }, tankLocation: { type: String, default: null }, + motor_status: { type: String, default: 0 }, connections: { source: { type: String}, inputConnections: [{ inputConnections: String,input_type:String }],