@ -1465,24 +1465,82 @@ admin.initializeApp({
// });
// Handle motor start event with timestamp
eventEmitter . on ( 'motorStart' , async ( fcmTokens , timestamp , motorId , waterLevel ) => {
await sendNotification ( fcmTokens , 'Motor Started' , ` Motor ID: ${ motorId } started successfully at ${ timestamp } . Current Water Level: ${ waterLevel } Ltrs ` ) ;
// eventEmitter.on('motorStart', async (fcmTokens, timestamp, motorId, waterLevel) => {
// await sendNotification(fcmTokens, 'Motor Started', `Motor ID: ${motorId} started successfully at ${timestamp}. Current Water Level: ${waterLevel} Ltrs`);
// });
eventEmitter . on ( 'motorStart' , async ( fcmTokens , timestamp , motorId , waterLevel , blockName , tankName , startTime , motorOnType , stopCriteria ) => {
const message = ` Water supply from ' ${ blockName } ' to ' ${ tankName } ' started at ${ startTime } by ' ${ motorOnType } ' mode and will stop after ${ stopCriteria } . Current Water Level: ${ waterLevel } Ltrs. ` ;
await sendNotification ( fcmTokens , 'Motor Started' , message ) ;
} ) ;
// Emit motor stop event with motorId
eventEmitter . on ( 'motorStop' , async ( fcmTokens , timestamp , motorId , waterLevel ) => {
await sendNotification ( fcmTokens , 'Motor Stopped' , ` Motor ID: ${ motorId } stopped successfully at ${ timestamp } .Current Water Level: ${ waterLevel } Ltrs ` ) ;
eventEmitter . on ( 'motorStop' , async ( fcmTokens , timestamp , motorId , waterLevel , blockName , tankName , stopTime , motorOnType ) => {
const message = ` Water supply from ' ${ blockName } ' to ' ${ tankName } ' stopped at ${ stopTime } by ' ${ motorOnType } ' mode. Current Water Level: ${ waterLevel } Ltrs. ` ;
await sendNotification ( fcmTokens , 'Motor Stopped' , message ) ;
} ) ;
// Event listener to handle notification
eventEmitter . on ( 'sendLowWaterNotification' , async ( fcmToken , tankInfo ) => {
const message = formatWaterLevelMessage ( tankInfo , 'low' ) ;
sendNotification ( fcmToken , message ) ;
} ) ;
eventEmitter . on ( 'sendVeryLowWaterNotification' , async ( fcmToken , tankInfo ) => {
const message = formatWaterLevelMessage ( tankInfo , 'very low' ) ;
sendNotification ( fcmToken , message ) ;
} ) ;
eventEmitter . on ( 'sendCriticalLowWaterNotification' , async ( fcmToken , tankInfo ) => {
const message = formatWaterLevelMessage ( tankInfo , 'critically low' ) ;
sendNotification ( fcmToken , message ) ;
} ) ;
const formatWaterLevelMessage = ( tankInfo , levelType ) => {
const tankName = tankInfo . tankName ;
const tankLocation = tankInfo . tankLocation ;
const waterLevel = parseInt ( tankInfo . waterlevel , 10 ) ;
const capacity = parseInt ( tankInfo . capacity , 10 ) ;
const volumeInLitres = ( capacity * waterLevel ) / 100 ; // assuming the capacity is in litres
let levelDescription = '' ;
if ( levelType === 'low' ) {
levelDescription = ` ${ waterLevel } % ( ${ volumeInLitres . toFixed ( 2 ) } L) ` ;
} else if ( levelType === 'very low' ) {
levelDescription = ` ${ waterLevel } % ( ${ volumeInLitres . toFixed ( 2 ) } L) ` ;
} else if ( levelType === 'critically low' ) {
levelDescription = ` ${ waterLevel } % ( ${ volumeInLitres . toFixed ( 2 ) } L) ` ;
}
return ` Water level in ' ${ tankName } ', located at ' ${ tankLocation } ', type of water: ${ tankInfo . waterType } is ${ levelType } at ${ levelDescription } . Action: start motor now. ` ;
} ;
// Emit low water level event with motorId
eventEmitter . on ( 'lowWaterLevel' , async ( fcmTokens , timestamp , motorId , waterLevel ) => {
await sendNotification ( fcmTokens , 'Low Water Level' , ` Motor ID: ${ motorId } , water level dropped below 20% at ${ timestamp } . Current Water Level: ${ waterLevel } Ltrs ` ) ;
// eventEmitter.on('lowWaterLevel', async (fcmTokens, timestamp, motorId, waterLevel) => {
// await sendNotification(fcmTokens, 'Low Water Level', `Motor ID: ${motorId}, water level dropped below 20% at ${timestamp}. Current Water Level: ${waterLevel} Ltrs`);
// });
eventEmitter . on ( 'sendHighWaterNotification' , async ( fcmTokens , tankInfo , startTime , stopTime ) => {
const message = ` Attention: Water level in ' ${ tankInfo . tankName } ' located at ' ${ tankInfo . tankLocation } ' is high at ${ tankInfo . waterLevel } % ( ${ tankInfo . volumeInLitres } L). Please stop the motor. Motor running from ${ startTime } to ${ stopTime } . ` ;
await sendNotification ( fcmTokens , 'High Water Level' , message , 'Stop Motor' ) ;
} ) ;
// Emit high water level event with motorId
eventEmitter . on ( 'highWaterLevel' , async ( fcmTokens , timestamp , motorId , waterLevel ) => {
await sendNotification ( fcmTokens , 'High Water Level' , ` Motor ID: ${ motorId } , water level reached above 90% at ${ timestamp } . Current Water Level: ${ waterLevel } Ltrs ` ) ;
// Event listener for very high water level notification
eventEmitter . on ( 'sendVeryHighWaterNotification' , async ( fcmTokens , tankInfo , startTime , stopTime ) => {
const message = ` Attention: Water level in ' ${ tankInfo . tankName } ' located at ' ${ tankInfo . tankLocation } ' is very high at ${ tankInfo . waterLevel } % ( ${ tankInfo . volumeInLitres } L). Please stop the motor. Motor running from ${ startTime } to ${ stopTime } . ` ;
await sendNotification ( fcmTokens , 'Very High Water Level' , message , 'Stop Motor' ) ;
} ) ;
// Event listener for critically high water level notification
eventEmitter . on ( 'sendCriticalHighWaterNotification' , async ( fcmTokens , tankInfo , startTime , stopTime ) => {
const message = ` Attention: Water level in ' ${ tankInfo . tankName } ' located at ' ${ tankInfo . tankLocation } ' is critically high at ${ tankInfo . waterLevel } % ( ${ tankInfo . volumeInLitres } L). Water may overflow. Please stop the motor. Motor running from ${ startTime } to ${ stopTime } . ` ;
await sendNotification ( fcmTokens , 'Critical High Water Level' , message , 'Stop Motor' ) ;
} ) ;
// Emit high water level event with motorId
// eventEmitter.on('highWaterLevel', async (fcmTokens, timestamp, motorId, waterLevel) => {
// await sendNotification(fcmTokens, 'High Water Level', `Motor ID: ${motorId}, water level reached above 90% at ${timestamp}. Current Water Level: ${waterLevel} Ltrs`);
// });
// Function to emit events with timestamps
@ -1856,8 +1914,10 @@ exports.motorAction = async (req, reply) => {
// Define thresholds for water levels
const lowWaterThreshold = 20 ; // Low water level percentage threshold
const highWaterThreshold = 90 ; // High water level percentage threshold
//const highWaterThreshold = 90; // High water level percentage threshold
const highWaterThreshold = 70 ; // High water level percentage threshold
const veryHighWaterThreshold = 80 ; // Very High water level percentage threshold
const criticalHighWaterThreshold = 85 ;
// Ensure motor_id is provided
if ( ! motorId ) {
throw new Error ( "Motor ID is required." ) ;
@ -1870,15 +1930,79 @@ exports.motorAction = async (req, reply) => {
const receiverTank = await Tank . findOne ( { customerId , tankName : req . body . to , tankLocation : req . body . to _type . toLowerCase ( ) } ) ;
console . log ( receiverTank )
const currentWaterLevel = parseInt ( receiverTank . waterlevel , 10 ) ;
const waterLevelThresholds = { low : 30 , veryLow : 20 , criticallyLow : 10 } ;
// Check if the water level is below any of the thresholds
if ( currentWaterLevel < waterLevelThresholds . criticallyLow ) {
if ( ! receiverTank . notificationSentCritical ) {
eventEmitter . emit ( 'sendCriticalLowWaterNotification' , fcmToken , receiverTank ) ;
await Tank . updateOne ( { customerId , tankName : receiverTank . tankName } , { $set : { notificationSentCritical : true } } ) ;
}
} else if ( currentWaterLevel < waterLevelThresholds . veryLow ) {
if ( ! receiverTank . notificationSentVeryLow ) {
eventEmitter . emit ( 'sendVeryLowWaterNotification' , fcmToken , receiverTank ) ;
await Tank . updateOne ( { customerId , tankName : receiverTank . tankName } , { $set : { notificationSentVeryLow : true } } ) ;
}
} else if ( currentWaterLevel < waterLevelThresholds . low ) {
if ( ! receiverTank . notificationSentLow ) {
eventEmitter . emit ( 'sendLowWaterNotification' , fcmToken , receiverTank ) ;
await Tank . updateOne ( { customerId , tankName : receiverTank . tankName } , { $set : { notificationSentLow : true } } ) ;
}
}
// Check for critical high water level
if ( currentWaterLevel >= criticalHighWaterThreshold ) {
if ( ! receiverTank . notificationSentCriticalHigh ) {
eventEmitter . emit ( 'sendCriticalHighWaterNotification' , fcmToken , receiverTank ) ;
await Tank . updateOne ( { customerId , tankName : receiverTank . tankName } , { $set : { notificationSentCriticalHigh : true } } ) ;
}
}
// Check for very high water level
else if ( currentWaterLevel >= veryHighWaterThreshold ) {
if ( ! receiverTank . notificationSentVeryHigh ) {
eventEmitter . emit ( 'sendVeryHighWaterNotification' , fcmToken , receiverTank ) ;
await Tank . updateOne ( { customerId , tankName : receiverTank . tankName } , { $set : { notificationSentVeryHigh : true } } ) ;
}
}
// Check for high water level
else if ( currentWaterLevel >= highWaterThreshold ) {
if ( ! receiverTank . notificationSentHigh ) {
eventEmitter . emit ( 'sendHighWaterNotification' , fcmToken , receiverTank ) ;
await Tank . updateOne ( { customerId , tankName : receiverTank . tankName } , { $set : { notificationSentHigh : true } } ) ;
}
}
// Determine the motor stop status based on the action
let motorStopStatus ;
if ( action === "start" ) {
motorStopStatus = "2" ; // If action is start, set stop status to "2"
emitWithTimestamp ( 'motorStart' , fcmToken , motorId , currentWaterLevel ) ;
const startTime = req . body . startTime ;
const motorOnType = req . body . motor _on _type || "manual" ;
const stopCriteria =
motorOnType === "time"
? ` ${ req . body . manual _threshold _time } minutes `
: ` ${ req . body . manual _threshold _litres } litres ` ;
eventEmitter . emit (
"motorStart" ,
fcmToken ,
new Date ( ) . toISOString ( ) ,
motorId ,
currentWaterLevel ,
req . body . from , // Block Name
req . body . to , // Tank Name
startTime ,
motorOnType ,
stopCriteria
) ;
// Add any additional logic for starting the motor here
await Tank . updateOne (
{ customerId , "connections.inputConnections.motor_id" : motorId } ,
{ $set : { "connections.inputConnections.$.motor_stop_status" : "2" } }
) ;
reply . code ( 200 ) . send ( { message : "Motor started successfully." } ) ;
} else if ( action === "stop" ) {
motorStopStatus = "1" ; // If action is stop, set stop status to "1"
emitWithTimestamp ( 'motorStop' , fcmToken , motorId , currentWaterLevel ) ;
e ventEmitter. emit ( 'motorStop' , fcmToken , motorId , currentWaterLevel , blockName , tankName , stopTime , motorOnType ) ;
} else {
throw new Error ( "Invalid action provided." ) ;
}