diff --git a/src/controllers/tanksController.js b/src/controllers/tanksController.js index 94ec5114..9bad0786 100644 --- a/src/controllers/tanksController.js +++ b/src/controllers/tanksController.js @@ -306,11 +306,12 @@ exports.motorAction = async (req, reply) => { let supplier_waterlevel = parseInt(supplier_tank_info2.waterlevel.replace(/,/g, ''), 10) const newWaterLevel = receiver_waterlevel + 250//Math.floor(supplier_waterlevel * 0.1); - const newSupplierWaterLevel = supplier_waterlevel//Math.floor(supplier_waterlevel * 0.1); + const newSupplierWaterLevel = supplier_waterlevel-250//Math.floor(supplier_waterlevel * 0.1); const supplier_capacity = parseInt(supplier_tank_info.capacity.replace(/,/g, ''), 10) console.log((newSupplierWaterLevel/supplier_capacity)*100) // 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") { + console.log((newSupplierWaterLevel/supplier_capacity)*100,(newWaterLevel/receiver_capacity)*100,(newWaterLevel/receiver_capacity)*100,) clearInterval(intervals[interval_variable]); // Clear the interval for this tank delete intervals[interval_variable]; @@ -344,12 +345,14 @@ exports.motorAction = async (req, reply) => { // receiver_waterlevel = newWaterLevel; console.log((newSupplierWaterLevel/supplier_capacity)*100) // console.log((newWaterLevel/receiver_capacity)*100) - console.log(receiver_tank+""+newWaterLevel+""+"bore to sump") + console.log(newWaterLevel+""+receiver_type) + console.log(newSupplierWaterLevel+""+supplier_tank) + await Tank.findOneAndUpdate({customerId, tankName: receiver_tank,tankLocation:receiver_type}, { $set: { waterlevel: newWaterLevel } }) + await Tank.findOneAndUpdate({customerId, tankName: supplier_tank,tankLocation:supplier_tank_type}, { $set: { waterlevel: newSupplierWaterLevel } }) + //if (supplier_tank_info2.motor_status==="0"){ - supplier_waterlevel = parseInt(supplier_tank_info2.waterlevel.replace(/,/g, ''), 10)-250 - console.log(supplier_tank+""+newSupplierWaterLevel+""+"s to oh") - await Tank.findOneAndUpdate({customerId, tankName: supplier_tank,tankLocation:supplier_tank_type}, { $set: { waterlevel: supplier_waterlevel } }) + // await Tank.findOneAndUpdate({customerId, tankName: supplier_tank,tankLocation:supplier_tank_type}, { $set: { waterlevel: supplier_waterlevel } }) // } } @@ -556,51 +559,11 @@ exports.motorAction = async (req, reply) => { } // console.log(customerId,req.body.from,req.body.from_type,receiver_tank,req.body.to_type,) - motorData = { - - customerId:customerId, - supplierTank : req.body.from, - supplier_type: req.body.from_type, - receiverTank: receiver_tank, - receiver_type: req.body.to_type, - startTime: req.body.startTime, - stopTime: req.body.stopTime, - - - - }; - var motorData = new MotorData(motorData); - - checkFormEncoding = isUserFormUrlEncoded(req); - if (checkFormEncoding.isUserFormUrlEncoded) { - usertobeInserted = checkFormEncoding.motorData; - console.log("thsi true url string"); - motorData.customerId = customerId; - motorData.supplierTank = req.body.from; - motorData.receiverTank = receiver_tank; - motorData.supplier_type = req.body.from_type; - motorData.receiver_type = req.body.to_type; - motorData.startTime = usertobeInserted.startTime; - motorData.stopTime = usertobeInserted.stopTime; - - } - const motor_data = await motorData.save(); - console.log(motor_data) - // reply.send({ status_code: 200, data: motor_data }); - - - - - reply.send({ status_code: 200, "start time": start_time, data: motor_data}); - console.log(start_time) - return motor_data - - } else if (action === "stop") { - const stop_time = new Date().toLocaleString('en-US', {timeZone: 'Asia/Kolkata'}) + stop_time = new Date().toLocaleString('en-US', {timeZone: 'Asia/Kolkata'}) clearInterval(intervals[interval_variable]); // Clear the interval for this tank delete intervals[interval_variable]; @@ -626,14 +589,56 @@ exports.motorAction = async (req, reply) => { ); } + motorData = { + + + customerId:customerId, + supplierTank : supplier_tank, + supplier_type: supplier_tank_type, + receiverTank: receiver_tank, + receiver_type: receiver_type, + // startTime: startTime, + stopTime: req.body.stopTime, + + + + }; + var motorData = new MotorData(motorData); + + checkFormEncoding = isUserFormUrlEncoded(req); + if (checkFormEncoding.isUserFormUrlEncoded) { + usertobeInserted = checkFormEncoding.motorData; + console.log("thsi true url string"); + motorData.customerId = customerId; + motorData.supplierTank = supplierTank; + motorData.receiverTank = receiver_tank; + motorData.supplier_type = supplier_type; + motorData.receiver_type = receiver_type; + //motorData.startTime = startTime; + motorData.stopTime = stop_time; + + + } + const motor_data = await motorData.save(); + console.log(motor_data) + // reply.send({ status_code: 200, data: motor_data }); + + + + + // reply.send({ status_code: 200, "start time": start_time, data: motor_data}); + console.log(start_time) + // return motor_data + + // 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,data: motor_data}); } else { throw new Error("Invalid action"); } @@ -1030,15 +1035,15 @@ exports.startUpdateLoop = async (request, reply) => { }, updateInterval); }; - exports.updatewaterlevelsatmidnight = async (req, reply) => { try { - // Schedule the task to run every day at midnight + // Schedule the task to run every day at 10 seconds past the minute cron.schedule('0 0 * * *', async () => { try { const tanks = await Tank.find({ customerId: req.query.customerId }); for (const tank of tanks) { tank.waterlevel_at_midnight = tank.waterlevel; + console.log(tank.waterlevel_at_midnight) await tank.save(); } console.log('Waterlevel noted in waterlevel_at_midnight');