diff --git a/src/controllers/tanksController.js b/src/controllers/tanksController.js index b54e674b..97da9b21 100644 --- a/src/controllers/tanksController.js +++ b/src/controllers/tanksController.js @@ -646,7 +646,7 @@ let supplier_tanks = []; // const receiver_tank = req.body.to // 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 desired_water_percentage = parseInt((req.body.percentage).replace(/,/g, ''), 10) // const supplier_tank = req.body.from @@ -2869,7 +2869,21 @@ exports.motorAction = async (req, reply) => { } if (action === "start") { - const startTime = req.body.startTime; + const startTime = moment().tz('Asia/Kolkata').format('DD-MMM-YYYY - HH:mm'); + + const newMotorData = new MotorData({ + customerId, + motor_id: motorId, + start_instance_id, + supplierTank: req.body.from, + receiverTank: req.body.to, + supplier_type: req.body.from_type, + receiver_type: req.body.to_type, + startTime, + receiverInitialwaterlevel: parseInt(receiverTank.waterlevel.replace(/,/g, ''), 10) + }); + await newMotorData.save(); + await Tank.updateOne( { customerId, "connections.inputConnections.motor_id": motorId }, { $set: { @@ -2884,6 +2898,32 @@ exports.motorAction = async (req, reply) => { this.publishMotorStopStatus(motorId, motorStopStatus); reply.code(200).send({ message: "Motor started successfully." }); + if (threshold_type === "time") { + const thresholdTime = new Date(new Date().getTime() + manual_threshold_time * 60000); + motorIntervals[motorId] = setInterval(async () => { + const supplierTank = await Tank.findOne({ customerId, tankName: req.body.from, tankLocation: req.body.from_type.toLowerCase() }); + const currentWaterLevel = parseInt(supplierTank.waterlevel.replace(/,/g, ''), 10); + const notificationTracker = new Map(); + if (new Date() >= thresholdTime) { + console.log("Threshold time reached. Stopping motor."); + clearInterval(motorIntervals[motorId]); + delete motorIntervals[motorId]; + await stopMotor(motorId, customerId, start_instance_id); + } + }, 30000); + } else if (threshold_type === "litres") { + motorIntervals[motorId] = setInterval(async () => { + const receiverTank = await Tank.findOne({ customerId, tankName: req.body.to, tankLocation: req.body.to_type.toLowerCase() }); + const currentWaterLevel = parseInt(receiverTank.waterlevel.replace(/,/g, ''), 10); + const thresholdWaterLevel = currentWaterLevel + parseInt(manual_threshold_litres.replace(/,/g, ''), 10); + if (currentWaterLevel >= thresholdWaterLevel) { + clearInterval(motorIntervals[motorId]); + delete motorIntervals[motorId]; + await stopMotor(motorId, customerId, start_instance_id); + } + }, 30000); + } + } else if (action === "stop") { await stopMotor(motorId, customerId, start_instance_id); this.publishMotorStopStatus(motorId, motorStopStatus); @@ -2916,7 +2956,7 @@ async function stopMotor(motorId, customerId, start_instance_id) { const motorData = await MotorData.findOne({ customerId, motor_id: motorId, start_instance_id }); if (motorData) { - const startTime = moment(motorData.startTime, 'YYYY-MM-DDTHH:mm:ss.SSSZ'); + const startTime = moment(motorData.startTime, 'DD-MMM-YYYY - HH:mm'); const runtime = moment.duration(moment(currentTime, 'DD-MMM-YYYY - HH:mm').diff(startTime)).asSeconds(); const receiverTank = await Tank.findOne({ customerId, tankName: motorData.receiverTank, tankLocation: motorData.receiver_type.toLowerCase() }); @@ -2932,7 +2972,6 @@ async function stopMotor(motorId, customerId, start_instance_id) { - // exports.motorAction = async (req, reply) => { // try { // const customerId = req.params.customerId;