From 59a3325ec5794fb425acaefbe6d95be1b8d2ec58 Mon Sep 17 00:00:00 2001 From: varun Date: Thu, 16 Mar 2023 03:51:03 -0400 Subject: [PATCH 01/13] made changes in profile picture and created get all tanker data --- src/controllers/tankersController.js | 18 ++++++++++++++++++ src/controllers/userController.js | 2 +- src/models/User.js | 2 +- src/routes/tankersRoute.js | 16 ++++++++++++++++ 4 files changed, 36 insertions(+), 2 deletions(-) diff --git a/src/controllers/tankersController.js b/src/controllers/tankersController.js index 4f63e0fe..7a33bb23 100644 --- a/src/controllers/tankersController.js +++ b/src/controllers/tankersController.js @@ -178,6 +178,24 @@ exports.getTanker = async (req, reply) => { } }; +exports.getallTanker = async (req, reply) => { + try { + await Tanker.find() + .exec() + .then((docs) => { + reply.send({ status_code: 200, data: docs, count: docs.length }); + }) + .catch((err) => { + console.log(err); + reply.send({ error: err }); + }); + } catch (err) { + throw boom.boomify(err); + } +}; + + + diff --git a/src/controllers/userController.js b/src/controllers/userController.js index df0fd65d..6e558809 100644 --- a/src/controllers/userController.js +++ b/src/controllers/userController.js @@ -342,7 +342,7 @@ exports.delPhoneUser = async (req, reply) => { exports.uploadProfilePicture = async (req, reply) => { try { const customerId = req.params.customerId; - const picture = new Buffer.from(req.body.picture, 'base64'); + const picture = req.body.picture; let profilePicture = await ProfilePicture.findOne({ customerId }); diff --git a/src/models/User.js b/src/models/User.js index b59a853e..4a7c3000 100644 --- a/src/models/User.js +++ b/src/models/User.js @@ -117,7 +117,7 @@ const profilePictureSchema = new Schema({ required: true }, picture: { - type: Buffer, + type: String, required: true } }); diff --git a/src/routes/tankersRoute.js b/src/routes/tankersRoute.js index 1d9407ce..95fbbe7d 100644 --- a/src/routes/tankersRoute.js +++ b/src/routes/tankersRoute.js @@ -232,6 +232,22 @@ module.exports = function (fastify, opts, next) { handler: tankersController.getTanker, }); + fastify.get("/api/getallTankers", { + schema: { + tags: ["Supplier"], + description: "This is for Get all Tanker Data", + summary: "This is for to Get all Tanker Data", + + security: [ + { + basicAuth: [], + }, + ], + }, + preHandler: fastify.auth([fastify.authenticate]), + handler: tankersController.getallTanker, + }); + fastify.route({ method: "POST", url: "/api/addBores/:customerId", From f5edc7c60d4c0b3f909cfaf403a4d082f35a66b4 Mon Sep 17 00:00:00 2001 From: varun Date: Thu, 16 Mar 2023 03:53:49 -0400 Subject: [PATCH 02/13] initial update removed in updating tank levels --- src/controllers/tanksController.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/controllers/tanksController.js b/src/controllers/tanksController.js index 0d32f301..ff66269c 100644 --- a/src/controllers/tanksController.js +++ b/src/controllers/tanksController.js @@ -121,6 +121,8 @@ exports.getTank = async (req, reply) => { throw boom.boomify(err); } }; + + //exports.getTanklevels = async (req, reply) => { // try { // const customerId = req.params.customerId; @@ -150,7 +152,7 @@ exports.updateTanklevels = async (req, reply) => { for (const tank of tanks) { const tankId = tank._id; let capacity = parseInt(tank.capacity.replace(/,/g, ''), 10); - let waterLevel = capacity - 100; // initial water level + let waterLevel = parseInt(tank.waterlevel.replace(/,/g, ''), 10); const intervalId = setInterval(async function () { const newWaterLevel = Math.floor(waterLevel - 200); From 857210108388fe7db41e8fb64bdea58aa88e9ba3 Mon Sep 17 00:00:00 2001 From: varun Date: Thu, 16 Mar 2023 03:57:08 -0400 Subject: [PATCH 03/13] checking start and stop --- src/controllers/tanksController.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/controllers/tanksController.js b/src/controllers/tanksController.js index ff66269c..ce387ba3 100644 --- a/src/controllers/tanksController.js +++ b/src/controllers/tanksController.js @@ -363,7 +363,7 @@ exports.motorAction = async (req, reply) => { if(supplier_tank_type==="bore" && receiver_type === "sump"){ const receiver_capacity = parseInt(receiver_tank_info.capacity.replace(/,/g, ''), 10) - console.log(receiver_capacity,"0") + console.log(receiver_capacity,"0",receiver_tank_info.tankName) await Tank.findOneAndUpdate({customerId, tankName: receiver_tank,tankLocation:receiver_type}, { $set: { motor_status: "1" } }); let receiver_waterlevel = parseInt(receiver_tank_info.waterlevel.replace(/,/g, ''), 10) @@ -373,7 +373,7 @@ exports.motorAction = async (req, reply) => { const rcvr_info = await Tank.findOne({ customerId ,tankName:receiver_tank,tankLocation:receiver_type}); console.log(rcvr_info.motor_status) const newWaterLevel = receiver_waterlevel+450; - console.log(newWaterLevel,"2") + console.log(newWaterLevel,"2",receiver_tank_info.tankName) // Check if updating should stop if ((newWaterLevel/receiver_capacity)*100 >= 97 || (newWaterLevel/receiver_capacity)*100 >= desired_water_percentage || rcvr_info.motor_status === "0" ) { @@ -385,7 +385,7 @@ exports.motorAction = async (req, reply) => { receiver_waterlevel = newWaterLevel; - console.log((newWaterLevel/receiver_capacity)*100,"4") + console.log((newWaterLevel/receiver_capacity)*100,"4",receiver_tank_info.tankName) await Promise.all([ Tank.findOneAndUpdate({customerId, tankName: receiver_tank,tankLocation:receiver_type}, { $set: { waterlevel: newWaterLevel } }), ]); From e60f9a018ea506df9b4312d1271688c51ddcd9f2 Mon Sep 17 00:00:00 2001 From: varun Date: Thu, 16 Mar 2023 04:01:18 -0400 Subject: [PATCH 04/13] profile picture --- src/controllers/userController.js | 1 + src/models/User.js | 1 + 2 files changed, 2 insertions(+) diff --git a/src/controllers/userController.js b/src/controllers/userController.js index 6e558809..870d2bd4 100644 --- a/src/controllers/userController.js +++ b/src/controllers/userController.js @@ -388,6 +388,7 @@ exports.logout = async (request, reply) => { + // controller.js const http = require('https'); diff --git a/src/models/User.js b/src/models/User.js index 4a7c3000..ea410169 100644 --- a/src/models/User.js +++ b/src/models/User.js @@ -122,6 +122,7 @@ const profilePictureSchema = new Schema({ } }); + const ProfilePicture = mongoose.model('ProfilePicture', profilePictureSchema); const Counter = mongoose.model('Counter', CounterSchema); const User = mongoose.model("User", userSchema); From 2e5bc65d630232ca8a92a6e2c8a567ef3b22b76e Mon Sep 17 00:00:00 2001 From: varun Date: Thu, 16 Mar 2023 04:50:42 -0400 Subject: [PATCH 05/13] start and stop --- src/controllers/tanksController.js | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/src/controllers/tanksController.js b/src/controllers/tanksController.js index ce387ba3..7764fd8a 100644 --- a/src/controllers/tanksController.js +++ b/src/controllers/tanksController.js @@ -263,7 +263,7 @@ exports.motorAction = async (req, reply) => { if(action === "start"){ - // start_time = new Date().toLocaleString('en-US', {timeZone: 'Asia/Kolkata'}) + start_time = new Date().toLocaleString('en-US', {timeZone: 'Asia/Kolkata'}) supplier_tank = req.body.from supplier_tank_type = (req.body.from_type).toLowerCase() @@ -371,6 +371,7 @@ exports.motorAction = async (req, reply) => { intervalId = setInterval(async function () { // Calculate new water levels const rcvr_info = await Tank.findOne({ customerId ,tankName:receiver_tank,tankLocation:receiver_type}); + console.log(rcvr_info) console.log(rcvr_info.motor_status) const newWaterLevel = receiver_waterlevel+450; console.log(newWaterLevel,"2",receiver_tank_info.tankName) @@ -424,14 +425,15 @@ exports.motorAction = async (req, reply) => { } const motor_data = await motorData.save(); - reply.send({ status_code: 200, data: motor_data }); - return motor_data - + // reply.send({ status_code: 200, data: motor_data }); + - // reply.send({ status_code: 200, "start time": start_time}); - //console.log(start_time) + reply.send({ status_code: 200, "start time": start_time, data: motor_data}); + console.log(start_time) + return motor_data + } From c8768e3db645798dd0c5b28a60c5f96d92d76070 Mon Sep 17 00:00:00 2001 From: varun Date: Thu, 16 Mar 2023 04:53:19 -0400 Subject: [PATCH 06/13] start and stop --- src/controllers/tanksController.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/controllers/tanksController.js b/src/controllers/tanksController.js index 7764fd8a..8189046e 100644 --- a/src/controllers/tanksController.js +++ b/src/controllers/tanksController.js @@ -265,9 +265,9 @@ exports.motorAction = async (req, reply) => { if(action === "start"){ start_time = new Date().toLocaleString('en-US', {timeZone: 'Asia/Kolkata'}) - supplier_tank = req.body.from - supplier_tank_type = (req.body.from_type).toLowerCase() - receiver_type = (req.body.to_type).toLowerCase() + const supplier_tank = req.body.from + const supplier_tank_type = (req.body.from_type).toLowerCase() + const receiver_type = (req.body.to_type).toLowerCase() console.log(supplier_tank) From 203ea714cec9d048b164f6589b8323fa267d95bf Mon Sep 17 00:00:00 2001 From: varun Date: Thu, 16 Mar 2023 04:55:06 -0400 Subject: [PATCH 07/13] start and stop --- src/controllers/tanksController.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/controllers/tanksController.js b/src/controllers/tanksController.js index 8189046e..d11dde9b 100644 --- a/src/controllers/tanksController.js +++ b/src/controllers/tanksController.js @@ -278,7 +278,7 @@ exports.motorAction = async (req, reply) => { const supplier_tank_info1 = await Tank.findOne({ customerId ,tankName:supplier_tank,tankLocation:supplier_tank_type}); console.log(supplier_tank_info1) - initial_update = parseInt(supplier_tank_info1.capacity.replace(/,/g, ''), 10)-200; + const initial_update = parseInt(supplier_tank_info1.capacity.replace(/,/g, ''), 10)-200; await Tank.findOneAndUpdate({customerId, tankName: supplier_tank,tankLocation:supplier_tank_type}, { $set: { waterlevel: initial_update } }); const supplier_tank_info = await Tank.findOne({ customerId ,tankName:supplier_tank,tankLocation:supplier_tank_type}); @@ -443,7 +443,7 @@ exports.motorAction = async (req, reply) => { // clearInterval(intervalId); await Tank.findOneAndUpdate({customerId, tankName: receiver_tank,tankLocation:(req.body.to_type).toLowerCase()}, { $set: { motor_status: "0" } }); - // reply.send({ status_code: 200, "stop time": stop_time}); + reply.send({ status_code: 200, "stop time": stop_time}); } else { throw new Error("Invalid action"); } From dc65e33af5fba3e505506684d7a913448f73dd6e Mon Sep 17 00:00:00 2001 From: varun Date: Thu, 16 Mar 2023 05:38:41 -0400 Subject: [PATCH 08/13] start and stop --- src/controllers/tanksController.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/controllers/tanksController.js b/src/controllers/tanksController.js index d11dde9b..9291627f 100644 --- a/src/controllers/tanksController.js +++ b/src/controllers/tanksController.js @@ -438,7 +438,7 @@ exports.motorAction = async (req, reply) => { } else if (action === "stop") { - // stop_time = new Date().toLocaleString('en-US', {timeZone: 'Asia/Kolkata'}) + stop_time = new Date().toLocaleString('en-US', {timeZone: 'Asia/Kolkata'}) // console.log(stop_time) // clearInterval(intervalId); await Tank.findOneAndUpdate({customerId, tankName: receiver_tank,tankLocation:(req.body.to_type).toLowerCase()}, { $set: { motor_status: "0" } }); From 2c5dd9f475e6373f76cec56ee3e7011a702dd1dd Mon Sep 17 00:00:00 2001 From: varun Date: Thu, 16 Mar 2023 05:42:31 -0400 Subject: [PATCH 09/13] start and stop --- src/controllers/tanksController.js | 1 + 1 file changed, 1 insertion(+) diff --git a/src/controllers/tanksController.js b/src/controllers/tanksController.js index 9291627f..90529cbd 100644 --- a/src/controllers/tanksController.js +++ b/src/controllers/tanksController.js @@ -375,6 +375,7 @@ exports.motorAction = async (req, reply) => { console.log(rcvr_info.motor_status) const newWaterLevel = receiver_waterlevel+450; console.log(newWaterLevel,"2",receiver_tank_info.tankName) + // Check if updating should stop if ((newWaterLevel/receiver_capacity)*100 >= 97 || (newWaterLevel/receiver_capacity)*100 >= desired_water_percentage || rcvr_info.motor_status === "0" ) { From 25369ffb7dad228a459a5bee0e09ec64074ccff7 Mon Sep 17 00:00:00 2001 From: varun Date: Thu, 16 Mar 2023 06:41:19 -0400 Subject: [PATCH 10/13] start and stop --- src/controllers/tanksController.js | 36 +++++++++++++----------------- 1 file changed, 15 insertions(+), 21 deletions(-) diff --git a/src/controllers/tanksController.js b/src/controllers/tanksController.js index 90529cbd..21bbad45 100644 --- a/src/controllers/tanksController.js +++ b/src/controllers/tanksController.js @@ -250,7 +250,6 @@ exports.getTanklevels = async (req, reply) => { - exports.motorAction = async (req, reply) => { try { //let start_time,stop_time @@ -263,14 +262,12 @@ exports.motorAction = async (req, reply) => { if(action === "start"){ - start_time = new Date().toLocaleString('en-US', {timeZone: 'Asia/Kolkata'}) + // start_time = new Date().toLocaleString('en-US', {timeZone: 'Asia/Kolkata'}) - const supplier_tank = req.body.from - const supplier_tank_type = (req.body.from_type).toLowerCase() - const receiver_type = (req.body.to_type).toLowerCase() + supplier_tank = req.body.from + supplier_tank_type = (req.body.from_type).toLowerCase() + receiver_type = (req.body.to_type).toLowerCase() console.log(supplier_tank) - - if(supplier_tank_type==="sump" && receiver_type === "overhead"){ @@ -278,7 +275,7 @@ exports.motorAction = async (req, reply) => { const supplier_tank_info1 = await Tank.findOne({ customerId ,tankName:supplier_tank,tankLocation:supplier_tank_type}); console.log(supplier_tank_info1) - const initial_update = parseInt(supplier_tank_info1.capacity.replace(/,/g, ''), 10)-200; + initial_update = parseInt(supplier_tank_info1.capacity.replace(/,/g, ''), 10)-200; await Tank.findOneAndUpdate({customerId, tankName: supplier_tank,tankLocation:supplier_tank_type}, { $set: { waterlevel: initial_update } }); const supplier_tank_info = await Tank.findOne({ customerId ,tankName:supplier_tank,tankLocation:supplier_tank_type}); @@ -363,7 +360,7 @@ exports.motorAction = async (req, reply) => { if(supplier_tank_type==="bore" && receiver_type === "sump"){ const receiver_capacity = parseInt(receiver_tank_info.capacity.replace(/,/g, ''), 10) - console.log(receiver_capacity,"0",receiver_tank_info.tankName) + console.log(receiver_capacity,"0") await Tank.findOneAndUpdate({customerId, tankName: receiver_tank,tankLocation:receiver_type}, { $set: { motor_status: "1" } }); let receiver_waterlevel = parseInt(receiver_tank_info.waterlevel.replace(/,/g, ''), 10) @@ -371,11 +368,9 @@ exports.motorAction = async (req, reply) => { intervalId = setInterval(async function () { // Calculate new water levels const rcvr_info = await Tank.findOne({ customerId ,tankName:receiver_tank,tankLocation:receiver_type}); - console.log(rcvr_info) console.log(rcvr_info.motor_status) const newWaterLevel = receiver_waterlevel+450; - console.log(newWaterLevel,"2",receiver_tank_info.tankName) - + console.log(newWaterLevel,"2") // Check if updating should stop if ((newWaterLevel/receiver_capacity)*100 >= 97 || (newWaterLevel/receiver_capacity)*100 >= desired_water_percentage || rcvr_info.motor_status === "0" ) { @@ -387,7 +382,7 @@ exports.motorAction = async (req, reply) => { receiver_waterlevel = newWaterLevel; - console.log((newWaterLevel/receiver_capacity)*100,"4",receiver_tank_info.tankName) + console.log((newWaterLevel/receiver_capacity)*100,"4") await Promise.all([ Tank.findOneAndUpdate({customerId, tankName: receiver_tank,tankLocation:receiver_type}, { $set: { waterlevel: newWaterLevel } }), ]); @@ -426,25 +421,24 @@ exports.motorAction = async (req, reply) => { } const motor_data = await motorData.save(); - // reply.send({ status_code: 200, data: motor_data }); - + reply.send({ status_code: 200, data: motor_data }); + return motor_data - - reply.send({ status_code: 200, "start time": start_time, data: motor_data}); - console.log(start_time) - return motor_data + + // reply.send({ status_code: 200, "start time": start_time}); + //console.log(start_time) } else if (action === "stop") { - stop_time = new Date().toLocaleString('en-US', {timeZone: 'Asia/Kolkata'}) + // stop_time = new Date().toLocaleString('en-US', {timeZone: 'Asia/Kolkata'}) // console.log(stop_time) // clearInterval(intervalId); await Tank.findOneAndUpdate({customerId, tankName: receiver_tank,tankLocation:(req.body.to_type).toLowerCase()}, { $set: { motor_status: "0" } }); - reply.send({ status_code: 200, "stop time": stop_time}); + // reply.send({ status_code: 200, "stop time": stop_time}); } else { throw new Error("Invalid action"); } From ff9a79b5d89ecb7a93dc08f54a1f208737209087 Mon Sep 17 00:00:00 2001 From: varun Date: Thu, 16 Mar 2023 06:47:46 -0400 Subject: [PATCH 11/13] start and stop --- src/controllers/tanksController.js | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/src/controllers/tanksController.js b/src/controllers/tanksController.js index 21bbad45..facf7ac7 100644 --- a/src/controllers/tanksController.js +++ b/src/controllers/tanksController.js @@ -262,11 +262,11 @@ exports.motorAction = async (req, reply) => { if(action === "start"){ - // start_time = new Date().toLocaleString('en-US', {timeZone: 'Asia/Kolkata'}) + start_time = new Date().toLocaleString('en-US', {timeZone: 'Asia/Kolkata'}) - supplier_tank = req.body.from - supplier_tank_type = (req.body.from_type).toLowerCase() - receiver_type = (req.body.to_type).toLowerCase() + const supplier_tank = req.body.from + const supplier_tank_type = (req.body.from_type).toLowerCase() + const receiver_type = (req.body.to_type).toLowerCase() console.log(supplier_tank) @@ -275,7 +275,7 @@ exports.motorAction = async (req, reply) => { const supplier_tank_info1 = await Tank.findOne({ customerId ,tankName:supplier_tank,tankLocation:supplier_tank_type}); console.log(supplier_tank_info1) - initial_update = parseInt(supplier_tank_info1.capacity.replace(/,/g, ''), 10)-200; + const initial_update = parseInt(supplier_tank_info1.capacity.replace(/,/g, ''), 10)-200; await Tank.findOneAndUpdate({customerId, tankName: supplier_tank,tankLocation:supplier_tank_type}, { $set: { waterlevel: initial_update } }); const supplier_tank_info = await Tank.findOne({ customerId ,tankName:supplier_tank,tankLocation:supplier_tank_type}); @@ -421,24 +421,25 @@ exports.motorAction = async (req, reply) => { } const motor_data = await motorData.save(); - reply.send({ status_code: 200, data: motor_data }); - return motor_data - + //reply.send({ status_code: 200, data: motor_data }); + - // reply.send({ status_code: 200, "start time": start_time}); + reply.send({ status_code: 200, "start time": start_time,data: motor_data}); + return motor_data + //console.log(start_time) } else if (action === "stop") { - // stop_time = new Date().toLocaleString('en-US', {timeZone: 'Asia/Kolkata'}) + stop_time = new Date().toLocaleString('en-US', {timeZone: 'Asia/Kolkata'}) // console.log(stop_time) // clearInterval(intervalId); await Tank.findOneAndUpdate({customerId, tankName: receiver_tank,tankLocation:(req.body.to_type).toLowerCase()}, { $set: { motor_status: "0" } }); - // reply.send({ status_code: 200, "stop time": stop_time}); + reply.send({ status_code: 200, "stop time": stop_time}); } else { throw new Error("Invalid action"); } From 54b50ad8e48756b554ea02abada029069c6ebd6a Mon Sep 17 00:00:00 2001 From: varun Date: Fri, 17 Mar 2023 01:39:18 -0400 Subject: [PATCH 12/13] start and stop -main --- src/controllers/tanksController.js | 71 +++++++++++++++++++++--------- 1 file changed, 49 insertions(+), 22 deletions(-) diff --git a/src/controllers/tanksController.js b/src/controllers/tanksController.js index facf7ac7..3f2f89d1 100644 --- a/src/controllers/tanksController.js +++ b/src/controllers/tanksController.js @@ -199,17 +199,17 @@ exports.updateTanklevels1 = async (req, reply) => { const tank_type = tank.tankLocation let capacity = parseInt(tank.capacity.replace(/,/g, ''), 10); - let waterLevel = capacity - 100; // initial water level + const waterLevel = parseInt(tank.waterLevel.replace(/,/g, ''), 10); + //capacity - 100; // initial water level const intervalId = setInterval(async function () { - const newWaterLevel = Math.floor(waterLevel - 200); + const motor_status = tank.motor_status + if(motor_status === "0"){ + const newWaterLevel = Math.floor(waterLevel - 200); console.log(tank.tankName,newWaterLevel) const result = await Tank.findOneAndUpdate( { customerId, tankName, }, { $set: { waterlevel: newWaterLevel } } ); - - // console.log(result); - if (newWaterLevel === 0) { clearInterval(intervals[tankName]); console.log(`Stopped updating ${tankName}`); @@ -217,6 +217,31 @@ exports.updateTanklevels1 = async (req, reply) => { } waterLevel = newWaterLevel; + + } + else{ + if(tank.tankLocation==="overhead",motor_status==="1"){ + tank_waterlevel = math.floor(waterLevel + 250) + const supplier_water= await findOne({customerId:req.params.customerId,tankName:req.body.from,tankLocation:req.body.from_type}) + const supplier_waterlevel = parseInt(supplier_water.waterlevel .replace(/,/g, ''), 10); + const newSupplierWaterLevel = math.floor(supplier_waterlevel - 250) + + await Tank.findOneAndUpdate({customerId:req.params.customerId,tankName:req.body.from,tankLocation:req.body.from_type}, { $set: { waterlevel: newSupplierWaterLevel } }) + await tank.save() + } + if(tank.tankLocation==="sump",motor_status==="1"){ + const sumpwaterlevel = math.floor(waterLevel + 250) + await tank.save() + } + + + } + + + // console.log(result); + + + }, 2000); intervals[tankName] = intervalId; @@ -249,7 +274,6 @@ exports.getTanklevels = async (req, reply) => { - exports.motorAction = async (req, reply) => { try { //let start_time,stop_time @@ -262,12 +286,14 @@ exports.motorAction = async (req, reply) => { if(action === "start"){ - start_time = new Date().toLocaleString('en-US', {timeZone: 'Asia/Kolkata'}) + start_time = new Date().toLocaleString('en-US', {timeZone: 'Asia/Kolkata'}) - const supplier_tank = req.body.from - const supplier_tank_type = (req.body.from_type).toLowerCase() - const receiver_type = (req.body.to_type).toLowerCase() + const supplier_tank = req.body.from + const supplier_tank_type = (req.body.from_type).toLowerCase() + const receiver_type = (req.body.to_type).toLowerCase() console.log(supplier_tank) + + if(supplier_tank_type==="sump" && receiver_type === "overhead"){ @@ -275,12 +301,13 @@ exports.motorAction = async (req, reply) => { const supplier_tank_info1 = await Tank.findOne({ customerId ,tankName:supplier_tank,tankLocation:supplier_tank_type}); console.log(supplier_tank_info1) - const initial_update = parseInt(supplier_tank_info1.capacity.replace(/,/g, ''), 10)-200; - await Tank.findOneAndUpdate({customerId, tankName: supplier_tank,tankLocation:supplier_tank_type}, { $set: { waterlevel: initial_update } }); + //const initial_update = parseInt(supplier_tank_info1.waterlevel.replace(/,/g, ''), 10)-200; + // await Tank.findOneAndUpdate({customerId, tankName: supplier_tank,tankLocation:supplier_tank_type}, { $set: { waterlevel: initial_update } }); const supplier_tank_info = await Tank.findOne({ customerId ,tankName:supplier_tank,tankLocation:supplier_tank_type}); // await changingfrom_tankwaterlevel(customerId,initial_update,supplier_tank_info); let supplier_waterlevel = parseInt(supplier_tank_info.waterlevel.replace(/,/g, ''), 10) + console.log(supplier_waterlevel) let receiver_waterlevel = parseInt(receiver_tank_info.waterlevel.replace(/,/g, ''), 10) intervalId = setInterval(async function () { // Calculate new water levels @@ -360,7 +387,7 @@ exports.motorAction = async (req, reply) => { if(supplier_tank_type==="bore" && receiver_type === "sump"){ const receiver_capacity = parseInt(receiver_tank_info.capacity.replace(/,/g, ''), 10) - console.log(receiver_capacity,"0") + console.log(receiver_capacity,"0",receiver_tank_info.tankName) await Tank.findOneAndUpdate({customerId, tankName: receiver_tank,tankLocation:receiver_type}, { $set: { motor_status: "1" } }); let receiver_waterlevel = parseInt(receiver_tank_info.waterlevel.replace(/,/g, ''), 10) @@ -368,9 +395,10 @@ exports.motorAction = async (req, reply) => { intervalId = setInterval(async function () { // Calculate new water levels const rcvr_info = await Tank.findOne({ customerId ,tankName:receiver_tank,tankLocation:receiver_type}); + console.log(rcvr_info) console.log(rcvr_info.motor_status) const newWaterLevel = receiver_waterlevel+450; - console.log(newWaterLevel,"2") + console.log(newWaterLevel,"2",receiver_tank_info.tankName) // Check if updating should stop if ((newWaterLevel/receiver_capacity)*100 >= 97 || (newWaterLevel/receiver_capacity)*100 >= desired_water_percentage || rcvr_info.motor_status === "0" ) { @@ -382,7 +410,7 @@ exports.motorAction = async (req, reply) => { receiver_waterlevel = newWaterLevel; - console.log((newWaterLevel/receiver_capacity)*100,"4") + console.log((newWaterLevel/receiver_capacity)*100,"4",receiver_tank_info.tankName) await Promise.all([ Tank.findOneAndUpdate({customerId, tankName: receiver_tank,tankLocation:receiver_type}, { $set: { waterlevel: newWaterLevel } }), ]); @@ -421,15 +449,15 @@ exports.motorAction = async (req, reply) => { } const motor_data = await motorData.save(); - //reply.send({ status_code: 200, data: motor_data }); - + // reply.send({ status_code: 200, data: motor_data }); + - reply.send({ status_code: 200, "start time": start_time,data: motor_data}); - return motor_data + reply.send({ status_code: 200, "start time": start_time, data: motor_data}); + console.log(start_time) + return motor_data - //console.log(start_time) } @@ -439,7 +467,7 @@ exports.motorAction = async (req, reply) => { // clearInterval(intervalId); await Tank.findOneAndUpdate({customerId, tankName: receiver_tank,tankLocation:(req.body.to_type).toLowerCase()}, { $set: { motor_status: "0" } }); - reply.send({ status_code: 200, "stop time": stop_time}); + reply.send({ status_code: 200, "stop time": stop_time}); } else { throw new Error("Invalid action"); } @@ -452,7 +480,6 @@ exports.motorAction = async (req, reply) => { }; }; - exports.consumption = async (req, reply) => { try { From 2c969dc1be8a23beb3858f96270a3845c8b63171 Mon Sep 17 00:00:00 2001 From: varun Date: Fri, 17 Mar 2023 01:54:21 -0400 Subject: [PATCH 13/13] start and stop -main changes --- src/controllers/tanksController.js | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/src/controllers/tanksController.js b/src/controllers/tanksController.js index 3f2f89d1..0f55c36f 100644 --- a/src/controllers/tanksController.js +++ b/src/controllers/tanksController.js @@ -283,6 +283,7 @@ exports.motorAction = async (req, reply) => { const receiver_tank_info = await Tank.findOne({ customerId ,tankName:receiver_tank,tankLocation:(req.body.to_type).toLowerCase()}); const receiver_capacity = parseInt((receiver_tank_info.capacity).replace(/,/g, ''), 10) const desired_water_percentage = parseInt((req.body.percentage).replace(/,/g, ''), 10) + const intervals = {}; if(action === "start"){ @@ -309,7 +310,7 @@ exports.motorAction = async (req, reply) => { let supplier_waterlevel = parseInt(supplier_tank_info.waterlevel.replace(/,/g, ''), 10) console.log(supplier_waterlevel) let receiver_waterlevel = parseInt(receiver_tank_info.waterlevel.replace(/,/g, ''), 10) - intervalId = setInterval(async function () { + intervals[receiver_tank] = setInterval(async function () { // Calculate new water levels const rcvr_info = await Tank.findOne({ customerId ,tankName:receiver_tank,tankLocation:receiver_type}); const newWaterLevel = receiver_waterlevel + 350//Math.floor(supplier_waterlevel * 0.1); @@ -318,10 +319,11 @@ 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 || rcvr_info.motor_status === "0") { - clearInterval(intervalId) + clearInterval(intervals[receiver_tank]); // Clear the interval for this tank + delete intervals[receiver_tank]; await Tank.findOneAndUpdate({customerId, tankName: receiver_tank,tankLocation:receiver_type}, { $set: { motor_status: "0" } }); - console.log("end"); + console.log("end for"+receiver_tank); } else { // Update water levels in database supplier_waterlevel = newSupplierWaterLevel; @@ -392,7 +394,7 @@ exports.motorAction = async (req, reply) => { let receiver_waterlevel = parseInt(receiver_tank_info.waterlevel.replace(/,/g, ''), 10) console.log(receiver_waterlevel,"1") - intervalId = setInterval(async function () { + intervals[receiver_tank] = setInterval(async function () { // Calculate new water levels const rcvr_info = await Tank.findOne({ customerId ,tankName:receiver_tank,tankLocation:receiver_type}); console.log(rcvr_info) @@ -403,8 +405,9 @@ exports.motorAction = async (req, reply) => { if ((newWaterLevel/receiver_capacity)*100 >= 97 || (newWaterLevel/receiver_capacity)*100 >= desired_water_percentage || rcvr_info.motor_status === "0" ) { await Tank.findOneAndUpdate({customerId, tankName: receiver_tank,tankLocation:receiver_type}, { $set: { motor_status: "0" } }); - clearInterval(intervalId) - console.log("end"); + clearInterval(intervals[receiver_tank]); // Clear the interval for this tank + delete intervals[receiver_tank]; + console.log("end for" + receiver_tank); } else { // Update water levels in database