@ -1565,17 +1565,18 @@ admin.initializeApp({
// });
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. ` ;
const message = ` MotorId '${ motorId } ' 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 , blockName , tankName , stopTime , motorOnType ) => {
const message = ` Water supply from '${ blockName } ' to ' ${ tankName } ' stopped at ${ stopTime } by ' ${ motorOnType } ' mode. Current Water Level: ${ waterLevel } Ltrs. ` ;
eventEmitter . on ( 'motorStop' , async ( fcmTokens , motorId, waterLevel , blockName , tankName , stopTime , motorOnType ) => {
const message = ` MotorId '${ motorId } ' 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' ) ;
@ -2027,50 +2028,50 @@ exports.motorAction = async (req, reply) => {
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 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 } } ) ;
}
}
// 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 ;
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 || new Date ( ) . toISOString ( ) ;
const motorOnType = req . body . motor _on _type || " manual ";
const motorOnType = req . body . motor _on _type || " application ";
if ( action === "start" ) {
motorStopStatus = "2" ;
@ -4659,6 +4660,8 @@ client.on('message', async (topic, message) => {
time
} ) ) ;
// Save IoT data for the received tanks
const iotTankData = new IotData ( {
hardwareId : hw _Id , // Updated variable name
@ -4730,53 +4733,6 @@ client.on('message', async (topic, message) => {
const inputConnection = motorTank . connections . inputConnections . find ( conn => conn . motor _id === hw _Id ) ; // Updated variable name
if ( inputConnection ) {
inputConnection . motor _status = status ; // Update motor status
console . log ( inputConnection . motor _stop _status , "inputConnection.motor_stop_status" )
if ( inputConnection . motor _stop _status === "1" && status === "2" ) {
console . log ( "got into forced manual" )
console . log ( inputConnection . motor _on _type , "before if motor on type" )
// Check if motor_on_type is not already "forced_manual"
if ( inputConnection . motor _on _type !== "forced_manual" ) {
inputConnection . motor _on _type = "forced_manual" ;
console . log ( "entered forced manual of if" )
inputConnection . motor _stop _status = "2" ;
// Update startTime to the current time in the specified format
const currentTime = new Date ( ) ;
const formattedTime = currentTime . toLocaleString ( 'en-GB' , {
day : '2-digit' ,
month : 'short' ,
year : 'numeric' ,
hour : '2-digit' ,
minute : '2-digit' ,
hour12 : false ,
} ) . replace ( ',' , '' ) ;
inputConnection . startTime = formattedTime ;
}
}
if ( inputConnection . motor _stop _status === "2" && status === "1" ) {
console . log ( "got into forced manual stop" )
console . log ( inputConnection . motor _on _type , "before if motor on type stop" )
// Check if motor_on_type is not already "forced_manual"
if ( inputConnection . motor _on _type = "forced_manual" ) {
inputConnection . motor _on _type = "manual" ;
console . log ( "entered forced manual of if of stop" )
// Update startTime to the current time in the specified format
inputConnection . motor _stop _status = "1" ;
}
}
await motorTank . save ( ) ; // Save the updated tank
}
@ -4789,6 +4745,8 @@ client.on('message', async (topic, message) => {
} ) ;
// Function to publish motor stop status
// exports.publishMotorStopStatus = async (motor_id, motor_stop_status) => {
// const payload = {
@ -5038,5 +4996,5 @@ async function removeDuplicates() {
}
// Run the remove duplicates function
removeDuplicates ( ) ;
// removeDuplicates();
console . log ( "this is for testing autopush,line located in tankscontroller" )