diff --git a/src/controllers/tanksController.js b/src/controllers/tanksController.js index a2be8b22..6e429ad1 100644 --- a/src/controllers/tanksController.js +++ b/src/controllers/tanksController.js @@ -1583,7 +1583,7 @@ eventEmitter.on( const message = `Tank Name: '${tankName}'\n` + `Pump started at: '${startTime}'\n` + - `Initiated by user(s): ${userNames}\n` + + `Initiated by : ${userNames}\n` + `Pump started by: '${motorOnType.toUpperCase()}'\n` + `Will stop at after: '${manual_threshold_time}' mins`; @@ -1609,7 +1609,7 @@ eventEmitter.on('motorStop', async (fcmTokens, tankName,stopTime, motorOnType) const message = `Tank Name: '${tankName}'\n` + `Pump stopped at: '${stopTime}'\n` + - `Initiated by user(s): ${userNames}\n` + + `Initiated by : ${userNames}\n` + `Motor On Type: '${motorOnType}'`; // Send the notification @@ -2183,7 +2183,7 @@ console.log(fcmToken) const blockName = req.body.from || "Unknown Block"; // Provide a fallback if `from` is missing const tankName = req.body.to || "Unknown Tank"; // Provide a fallback if `to` is missing const stopTime = req.body.stopTime - const motorOnType = req.body.motor_on_type || "App"; + const motorOnType = req.body.motor_on_type || "APP"; const manual_threshold_time = req.body.manual_threshold_time; if (action === "start") { @@ -2206,7 +2206,7 @@ console.log(fcmToken) motorIntervals[motorId] = setTimeout(async () => { try { // Perform threshold time logic (e.g., notification, motor stop) - console.log(`Threshold time of ${manualThresholdTime} minutes reached for motor ${motorId}`); + console.log(`Threshold time of ${manual_threshold_time} minutes reached for motor ${motorId}`); await Tank.updateOne( { customerId, "connections.inputConnections.motor_id": motorId }, { $set: { "connections.inputConnections.$.motor_stop_status": "1" } } @@ -2243,6 +2243,43 @@ console.log(fcmToken) "connections.inputConnections.$.threshold_type": "time" } } ); + await Tank.updateOne( + { customerId, "connections.inputConnections.motor_id": motorId }, + { + $set: { + "connections.inputConnections.$.motor_stop_status": "2", + "connections.inputConnections.$.motor_on_type": "manual", + } + } + ); + + // const startMessage = `Motor supplying water to '${tankName}' in block '${blockName}' started manually at ${startTime}.`; + // eventEmitter.emit('sendMotorStartNotification', fcmToken, startMessage); + + // Schedule threshold check + // const thresholdTimeMs = manual_threshold_time * 60 * 1000; + motorIntervals[motorId] = setTimeout(async () => { + try { + // const stopMessage = `Threshold time of ${manual_threshold_time} minutes reached for motor supplying water to '${tankName}' in block '${blockName}'.`; + // eventEmitter.emit('sendThresholdTimeNotification', fcmToken, stopMessage); + + await Tank.updateOne( + { customerId, "connections.inputConnections.motor_id": motorId }, + { + $set: { + "connections.inputConnections.$.motor_stop_status": "1", + "connections.inputConnections.$.manual_threshold_time": null, + "connections.inputConnections.$.threshold_type": null, + } + } + ); + + delete motorIntervals[motorId]; + } catch (error) { + console.error("Error during threshold handling:", error); + } + }, thresholdTimeMs); + reply.code(200).send({ message: "Motor started successfully." }); // Schedule water level checks after motor start motorIntervals[motorId] = setInterval(async () => { @@ -2279,6 +2316,25 @@ console.log(fcmToken) stopTime, motorOnType ); + + await Tank.updateOne( + { customerId, "connections.inputConnections.motor_id": motorId }, + { + $set: { + "connections.inputConnections.$.motor_stop_status": "1", + "connections.inputConnections.$.motor_on_type": "manual", + } + } + ); + + // const stopMessage = `Motor supplying water to '${tankName}' in block '${blockName}' stopped manually at ${stopTime}.`; + // eventEmitter.emit('sendMotorStopNotification', fcmToken, stopMessage); + + if (motorIntervals[motorId]) { + clearTimeout(motorIntervals[motorId]); + delete motorIntervals[motorId]; + } + } else { throw new Error("Invalid action provided."); }