@ -1681,6 +1681,34 @@ eventEmitter.on('sendCriticalHighWaterNotification', async (fcmTokens, tankInfo,
// await sendNotification(fcmTokens, 'High Water Level', `Motor ID: ${motorId}, water level reached above 90% at ${timestamp}. Current Water Level: ${waterLevel} Ltrs`);
// });
eventEmitter . on ( 'sendThresholdTimeNotification' , async ( fcmTokens , message ) => {
try {
await sendNotification ( fcmTokens , 'Threshold Time Reached' , message , 'Motor Alert' ) ;
console . log ( "Threshold time notification sent successfully." ) ;
} catch ( error ) {
console . error ( "Error sending threshold time notification:" , error ) ;
}
} ) ;
eventEmitter . on ( 'sendMotorStartNotification' , async ( fcmTokens , message ) => {
try {
await sendNotification ( fcmTokens , "Motor Started" , message ) ;
console . log ( "Manual method time notification sent successfully." ) ;
} catch ( error ) {
console . error ( "Error sending thresmanual method time notification:" , error ) ;
}
} ) ;
eventEmitter . on ( 'sendMotorStopNotification' , async ( fcmTokens , message ) => {
try {
await sendNotification ( fcmTokens , "Motor Stopped" , message ) ;
console . log ( "Manual method time notification sent successfully." ) ;
} catch ( error ) {
console . error ( "Error sending thresmanual method time notification:" , error ) ;
}
} ) ;
// Function to emit events with timestamps
const emitWithTimestamp = ( eventName , fcmTokens , motorId , waterLevel ) => {
@ -2150,11 +2178,57 @@ 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 || " a pp";
const motorOnType = req . body . motor _on _type || " A pp";
if ( action === "start" ) {
motorStopStatus = "2" ;
const startTime = req . body . startTime ;
const startMessage = ` The motor supplying water to ' ${ tankName } ' in block ' ${ blockName } ' has started manually at ${ new Date ( ) . toISOString ( ) } . ` ;
eventEmitter . emit ( "sendMotorStartNotification" , fcmToken , startMessage ) ;
await Tank . updateOne (
{ customerId , "connections.inputConnections.motor_id" : motorId } ,
{ $set : { "connections.inputConnections.$.motor_stop_status" : motorStopStatus } }
) ;
const thresholdTimeMs = req . body . manual _threshold _time * 60 * 1000 ; // Convert minutes to milliseconds
//const startTime = new Date(); // Record the start time
//const startTime = req.body.startTime;
// Schedule a task to send a notification when the threshold time is reached
motorIntervals [ motorId ] = setTimeout ( async ( ) => {
try {
// Fetch the latest tank and motor data
const receiverTank = await Tank . findOne ( {
customerId ,
tankName : req . body . to ,
tankLocation : req . body . to _type . toLowerCase ( ) ,
} ) ;
// Send a notification for threshold time reached
const message = ` Threshold time of ${ req . body . manual _threshold _time } minutes has been reached for the motor supplying ' ${ receiverTank . tankName } ' located at ' ${ receiverTank . tankLocation } '. Please review the motor operation. ` ;
eventEmitter . emit ( 'sendThresholdTimeNotification' , fcmToken , message ) ;
// Optionally update the tank or motor state in the database
await Tank . updateOne (
{ customerId , tankName : receiverTank . tankName } ,
{ $set : { notificationSentThresholdTime : true } }
) ;
// Optionally stop the motor if needed
await Tank . updateOne (
{ customerId , "connections.inputConnections.motor_id" : motorId } ,
{ $set : { "connections.inputConnections.$.motor_stop_status" : "1" } }
) ;
clearTimeout ( motorIntervals [ motorId ] ) ;
delete motorIntervals [ motorId ] ;
} catch ( error ) {
console . error ( "Error handling threshold time notification:" , error ) ;
}
} , thresholdTimeMs )
const stopCriteria =
motorOnType === "time"
? ` ${ req . body . manual _threshold _time } minutes `
@ -2171,7 +2245,7 @@ console.log(fcmToken)
startTime ,
motorOnType ,
stopCriteria ,
stopTime
stopTime , manual _threshold _time
) ;
await Tank . updateOne (
@ -2199,6 +2273,26 @@ console.log(fcmToken)
} , 30000 ) ; // Check every 30 seconds
} else if ( action === "stop" ) {
motorStopStatus = "1" ; // If action is stop, set stop status to "1"
// Emit stop notification
const stopMessage = ` The motor supplying water to ' ${ tankName } ' in block ' ${ blockName } ' was stopped manually at ${ stopTime } . ` ;
eventEmitter . emit ( "sendMotorStopNotification" , fcmToken , stopMessage ) ;
await Tank . updateOne (
{ customerId , "connections.inputConnections.motor_id" : motorId } ,
{
$set : {
"connections.inputConnections.$.motor_stop_status" : motorStopStatus ,
"connections.inputConnections.$.motor_on_type" : "manual" ,
"connections.inputConnections.$.stopTime" : stopTime
}
}
) ;
// Clear intervals if any
if ( motorIntervals [ motorId ] ) {
clearInterval ( motorIntervals [ motorId ] ) ;
delete motorIntervals [ motorId ] ;
}
eventEmitter . emit (
"motorStop" ,
fcmToken ,