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 {