@ -1336,13 +1336,14 @@ const sendPushNotification = async (registrationToken, title, body) => {
}
}
} ;
} ;
exports . motorAction = async ( req , reply ) => {
exports . motorAction = async ( req , reply ) => {
try {
try {
const customerId = req . params . customerId ;
const customerId = req . params . customerId ;
const action = req . body . action ;
const action = req . body . action ;
const motorId = req . body . motor _id ;
const motorId = req . body . motor _id ;
const start _instance _id = req . body . start _instance _id ;
const start _instance _id = req . body . start _instance _id
console . log ( req . body . startTime )
// Ensure motor_id is provided
// Ensure motor_id is provided
if ( ! motorId ) {
if ( ! motorId ) {
throw new Error ( "Motor ID is required." ) ;
throw new Error ( "Motor ID is required." ) ;
@ -1358,10 +1359,7 @@ exports.motorAction = async (req, reply) => {
throw new Error ( "Invalid action provided." ) ;
throw new Error ( "Invalid action provided." ) ;
}
}
const users = await User . find ( { customerId : customerId } ) ;
// Update the motor stop status immediately if action is stop
const fcmIds = users . map ( user => user . fcmId ) . filter ( fcmId => fcmId ) ;
// Handle motor stop action
if ( action === "stop" ) {
if ( action === "stop" ) {
// Update the motor stop status and other fields
// Update the motor stop status and other fields
await Tank . updateOne (
await Tank . updateOne (
@ -1380,21 +1378,6 @@ exports.motorAction = async (req, reply) => {
// Send immediate response to the client
// Send immediate response to the client
reply . code ( 200 ) . send ( { message : "Motor stopped successfully." } ) ;
reply . code ( 200 ) . send ( { message : "Motor stopped successfully." } ) ;
// Send notification for motor stop
for ( const fcmId of fcmIds ) {
try {
const response = await sendPushNotification ( fcmId , 'Motor Stopped' , ` Motor has stopped at ${ req . body . stopTime } . ` ) ;
console . log ( 'Notification sent successfully:' , response ) ;
if ( response . results [ 0 ] . error === 'NotRegistered' ) {
await User . updateOne ( { fcmId : fcmId } , { $unset : { fcmId : "" } } ) ;
console . log ( ` Removed unregistered FCM ID: ${ fcmId } ` ) ;
}
} catch ( error ) {
console . error ( 'Error sending notification:' , error ) ;
}
}
// Perform stop operations in the background
// Perform stop operations in the background
( async ( ) => {
( async ( ) => {
await delay ( 300000 ) ;
await delay ( 300000 ) ;
@ -1433,26 +1416,16 @@ exports.motorAction = async (req, reply) => {
{ customerId , "connections.inputConnections.motor_id" : motorId } ,
{ customerId , "connections.inputConnections.motor_id" : motorId } ,
{ $set : { "connections.inputConnections.$.motor_stop_status" : "2" } }
{ $set : { "connections.inputConnections.$.motor_stop_status" : "2" } }
) ;
) ;
// Send notification for motor start
for ( const fcmId of fcmIds ) {
try {
const response = await sendPushNotification ( fcmId , 'Motor Started' , ` Motor has started at ${ req . body . startTime } . ` ) ;
console . log ( 'Notification sent successfully:' , response ) ;
if ( response . results [ 0 ] . error === 'NotRegistered' ) {
await User . updateOne ( { fcmId : fcmId } , { $unset : { fcmId : "" } } ) ;
console . log ( ` Removed unregistered FCM ID: ${ fcmId } ` ) ;
}
} catch ( error ) {
console . error ( 'Error sending notification:' , error ) ;
}
}
}
}
// Check threshold settings if action is start
// Check threshold settings if action is start
if ( action === "start" ) {
if ( action === "start" ) {
if ( req . body . threshold _type === "time" ) {
if ( req . body . threshold _type === "time" ) {
// If threshold type is time, update threshold time
// await Tank.updateOne(
// { customerId, "connections.inputConnections.motor_id": motorId },
// { $set: { "connections.inputConnections.$.manual_threshold_time": req.body.manual_threshold_time,startTime:req.body.startTime } }
// );
const receiver _tank _info7 = await Tank . findOne ( { customerId , tankName : req . body . to , tankLocation : req . body . to _type . toLowerCase ( ) } ) ;
const receiver _tank _info7 = await Tank . findOne ( { customerId , tankName : req . body . to , tankLocation : req . body . to _type . toLowerCase ( ) } ) ;
const newMotorData = new MotorData ( {
const newMotorData = new MotorData ( {
@ -1464,7 +1437,7 @@ exports.motorAction = async (req, reply) => {
supplier _type : req . body . from _type ,
supplier _type : req . body . from _type ,
receiver _type : req . body . to _type ,
receiver _type : req . body . to _type ,
startTime : req . body . startTime ,
startTime : req . body . startTime ,
receiverInitialwaterlevel : parseInt ( receiver _tank _info7 . waterlevel , 10 )
receiverInitialwaterlevel : parseInt ( receiver _tank _info7 . waterlevel , 10 )
} ) ;
} ) ;
await newMotorData . save ( ) ;
await newMotorData . save ( ) ;
@ -1473,35 +1446,26 @@ exports.motorAction = async (req, reply) => {
if ( index !== - 1 ) {
if ( index !== - 1 ) {
await Tank . updateOne (
await Tank . updateOne (
{ customerId , "connections.inputConnections.motor_id" : motorId } ,
{ customerId , "connections.inputConnections.motor_id" : motorId } ,
{ $set : { [ ` connections.inputConnections. ${ index } .manual_threshold_time ` ] : req . body . manual _threshold _time , [ ` connections.inputConnections. ${ index } .startTime ` ] : req . body . startTime , [ ` connections.inputConnections. ${ index } .start_instance_id ` ] : start _instance _id } }
{ $set : { [ ` connections.inputConnections. ${ index } .manual_threshold_time ` ] : req . body . manual _threshold _time , [ ` connections.inputConnections. ${ index } .startTime ` ] : req . body . startTime , [ ` connections.inputConnections. ${ index } .start_instance_id ` ] : start _instance _id } }
) ;
) ;
}
}
}
}
// Start monitoring water level based on threshold time
// Start monitoring water level based on threshold time
const thresholdTime = moment ( ) . add ( req . body . manual _threshold _time , 'minutes' ) . toDate ( ) ;
const thresholdTime = moment ( ) . add ( req . body . manual _threshold _time , 'minutes' ) . toDate ( ) ;
const intervalId = setInterval ( async ( ) => {
const intervalId = setInterval ( async ( ) => {
const splr _tank _info3 = await Tank . findOne ( { customerId , tankName : req . body . from , tankLocation : req . body . from _type . toLowerCase ( ) } ) ;
const splr _tank _info3 = await Tank . findOne ( { customerId , tankName : req . body . from , tankLocation : req . body . from _type . toLowerCase ( ) } ) ;
const splr _tank _info3 _waterlevel = parseInt ( splr _tank _info3 . waterlevel , 10 ) ;
const splr _tank _info3 _waterlevel = parseInt ( splr _tank _info3 . waterlevel , 10 ) ;
//console.log(splr_tank_info3_waterlevel,"splr_tank_info3_waterlevel")
const splr _tank _info3 _capacity = parseInt ( splr _tank _info3 . capacity . replace ( /,/g , '' ) , 10 ) ;
const splr _tank _info3 _capacity = parseInt ( splr _tank _info3 . capacity . replace ( /,/g , '' ) , 10 ) ;
// const splr_tank_info3_capacity = parseInt(splr_tank_info3.capacity, 10);
// console.log(splr_tank_info3.capacity,splr_tank_info3_capacity,"splr_tank_info3_capacity")
const splr _tank _info3 _percentage = ( splr _tank _info3 _waterlevel / splr _tank _info3 _capacity ) * 100 ;
const splr _tank _info3 _percentage = ( splr _tank _info3 _waterlevel / splr _tank _info3 _capacity ) * 100 ;
// console.log(splr_tank_info3_percentage, "percentage for less than 20");
if ( new Date ( ) >= thresholdTime || splr _tank _info3 _percentage <= 20 ) {
if ( new Date ( ) >= thresholdTime || splr _tank _info3 _percentage <= 20 ) {
// Send notification for low supplier tank percentage
console . log ( splr _tank _info3 _percentage , )
for ( const fcmId of fcmIds ) {
try {
const response = await sendPushNotification ( fcmId , 'Low Water Level Alert' , ` Supplier tank water level is below 20% ( ${ splr _tank _info3 _percentage . toFixed ( 2 ) } %). ` ) ;
console . log ( 'Notification sent successfully:' , response ) ;
if ( response . results [ 0 ] . error === 'NotRegistered' ) {
await User . updateOne ( { fcmId : fcmId } , { $unset : { fcmId : "" } } ) ;
console . log ( ` Removed unregistered FCM ID: ${ fcmId } ` ) ;
}
} catch ( error ) {
console . error ( 'Error sending notification:' , error ) ;
}
}
await Tank . updateOne (
await Tank . updateOne (
{ customerId , "connections.inputConnections.motor_id" : motorId } ,
{ customerId , "connections.inputConnections.motor_id" : motorId } ,
{
{
@ -1523,74 +1487,113 @@ exports.motorAction = async (req, reply) => {
const receiverFinalWaterLevel = parseInt ( receiverTank . waterlevel , 10 ) ;
const receiverFinalWaterLevel = parseInt ( receiverTank . waterlevel , 10 ) ;
const quantityDelivered = receiverFinalWaterLevel - parseInt ( motorData . receiverInitialwaterlevel , 10 ) ;
const quantityDelivered = receiverFinalWaterLevel - parseInt ( motorData . receiverInitialwaterlevel , 10 ) ;
const water _pumped _till _now = parseInt ( receiverTank . total _water _added _from _midnight , 10 ) ;
const water _pumped _till _now = parseInt ( receiverTank . total _water _added _from _midnight , 10 ) ;
const totalwaterpumped = quantityDelivered + water _pumped _till _now ;
const totalwaterpumped = quantityDelivered + water _pumped _till _now
await Tank . findOneAndUpdate (
await Tank . findOneAndUpdate ( { customerId , tankName : motorData . receiverTank , tankLocation : motorData . receiver _type . toLowerCase ( ) } , { $set : { total _water _added _from _midnight : totalwaterpumped } } )
{ customerId , tankName : motorData . receiverTank , tankLocation : motorData . receiver _type . toLowerCase ( ) } ,
{ $set : { total _water _added _from _midnight : totalwaterpumped } }
const stopTime = formatDate ( new Date ( ) ) ;
) ;
await MotorData . updateOne (
await MotorData . updateOne (
{ customerId , motor _id : motorId , start _instance _id : start _instance _id } ,
{ customerId , motor _id : motorId , start _instance _id : start _instance _id } ,
{
{
$set : {
$set : {
stopTime : formatDate ( new Date ( ) ) ,
stopTime : stopTime ,
receiverfinalwaterlevel : receiverFinalWaterLevel . toString ( ) ,
receiverfinalwaterlevel : receiverFinalWaterLevel . toString ( ) ,
quantity _delivered : quantityDelivered . toString ( )
quantity _delivered : quantityDelivered . toString ( )
}
}
}
}
) ;
) ;
}
}
} else {
}
// Monitor the receiver tank's water level and send notification if max capacity is reached
} , 60000 ) ;
const receiverTank = await Tank . findOne ( { customerId , tankName : req . body . to , tankLocation : req . body . to _type . toLowerCase ( ) } ) ;
} else if ( req . body . threshold _type === "litres" ) {
const currentWaterLevel = parseInt ( receiverTank . waterlevel , 10 ) ;
console . log ( "entered litres" )
const receiverTankCapacity = parseInt ( receiverTank . capacity , 10 ) ;
const receiver _tank _info7 = await Tank . findOne ( { customerId , tankName : req . body . to , tankLocation : req . body . to _type . toLowerCase ( ) } ) ;
if ( currentWaterLevel >= receiverTankCapacity ) {
// Send a notification for maximum water level reached
for ( const fcmId of fcmIds ) {
try {
const response = await sendPushNotification ( fcmId , 'Maximum Water Level Alert' , ` The receiver tank has reached its maximum capacity. ` ) ;
console . log ( 'Notification sent successfully:' , response ) ;
if ( response . results [ 0 ] . error === 'NotRegistered' ) {
const newMotorData = new MotorData ( {
await User . updateOne ( { fcmId : fcmId } , { $unset : { fcmId : "" } } ) ;
customerId : customerId ,
console . log ( ` Removed unregistered FCM ID: ${ fcmId } ` ) ;
motor _id : motorId ,
start _instance _id : start _instance _id ,
supplierTank : req . body . from ,
receiverTank : req . body . to ,
supplier _type : req . body . from _type ,
receiver _type : req . body . to _type ,
startTime : req . body . startTime ,
receiverInitialwaterlevel : parseInt ( receiver _tank _info7 . waterlevel , 10 )
} ) ;
await newMotorData . save ( ) ;
// If threshold type is percentage, calculate percentage threshold
const receiver _tank _info = await Tank . findOne ( { customerId , tankName : req . body . to , tankLocation : req . body . to _type . toLowerCase ( ) } ) ;
const supplier _tank _info = await Tank . findOne ( { customerId , tankName : req . body . from , tankLocation : req . body . from _type . toLowerCase ( ) } ) ;
if ( ! receiver _tank _info ) {
throw new Error ( "Receiver tank not found." ) ;
}
}
} catch ( error ) {
if ( ! supplier _tank _info ) {
console . error ( 'Error sending notification:' , error ) ;
throw new Error ( "Supplierr tank not found." ) ;
}
const supplier _capacity = parseInt ( supplier _tank _info . capacity , 10 ) ;
const supplier _waterLevel = parseInt ( supplier _tank _info . waterlevel , 10 ) ;
const capacity = parseInt ( receiver _tank _info . capacity , 10 ) ;
const waterLevel = parseInt ( receiver _tank _info . waterlevel , 10 ) ;
const desired _percentage = parseInt ( req . body . manual _threshold _litres . replace ( /,/g , '' ) , 10 ) ;
console . log ( desired _percentage )
const threshold _water _level = waterLevel + desired _percentage ;
const supplier _threshold = supplier _waterLevel - desired _percentage
console . log ( supplier _threshold , "supplier_threshold" )
for await ( const tank of Tank . find ( { "connections.inputConnections.motor_id" : motorId } ) ) {
const index = tank . connections . inputConnections . findIndex ( connection => connection . motor _id === motorId ) ;
if ( index !== - 1 ) {
await Tank . updateOne (
{ customerId , "connections.inputConnections.motor_id" : motorId } ,
{ $set : { [ ` connections.inputConnections. ${ index } .manual_threshold_percentage ` ] : supplier _threshold . toString ( ) , [ ` connections.inputConnections. ${ index } .startTime ` ] : req . body . startTime } }
) ;
}
}
}
}
// Stop the motor
// Update water level threshold
// Start monitoring water level based on threshold percentage
const intervalId = setInterval ( async ( ) => {
// Check if water level has reached the threshold percentage
const supplier _tank _info1 = await Tank . findOne ( { customerId , tankName : req . body . from , tankLocation : req . body . from _type . toLowerCase ( ) } ) ;
const current _water _level = parseInt ( supplier _tank _info1 . waterlevel , 10 ) ;
if ( current _water _level <= supplier _threshold ) {
// Stop the motor pump
await Tank . updateOne (
await Tank . updateOne (
{ customerId , "connections.inputConnections.motor_id" : motorId } ,
{ customerId , "connections.inputConnections.motor_id" : motorId } ,
{
{
$set : {
$set : {
"connections.inputConnections.$.motor_stop_status" : "1" ,
"connections.inputConnections.$.motor_stop_status" : "1" ,
"connections.inputConnections.$.threshold_type" : null ,
"connections.inputConnections.$.threshold_type" : null ,
"connections.inputConnections.$.manual_threshold_time" : null ,
"connections.inputConnections.$.manual_threshold_time" : null ,
"connections.inputConnections.$.manual_threshold_percentage" : null
"connections.inputConnections.$.manual_threshold_percentage" : null
}
}
}
}
) ;
) ;
clearInterval ( intervalId ) ; // Stop monitoring water level
clearInterval ( intervalId ) ; // Stop monitoring
await delay ( 300000 ) ;
await delay ( 300000 ) ;
const motorData = await MotorData . findOne ( { customerId , motor _id : motorId , start _instance _id : start _instance _id } ) ;
const motorData = await MotorData . findOne ( { customerId , motor _id : motorId , start _instance _id : start _instance _id } ) ;
if ( motorData ) {
if ( motorData ) {
const receiverFinalWaterLevel = currentWaterLevel ;
const receiverTank = await Tank . findOne ( { customerId , tankName : motorData . receiverTank , tankLocation : motorData . receiver _type . toLowerCase ( ) } ) ;
const receiverFinalWaterLevel = parseInt ( receiverTank . waterlevel , 10 ) ;
const quantityDelivered = receiverFinalWaterLevel - parseInt ( motorData . receiverInitialwaterlevel , 10 ) ;
const quantityDelivered = receiverFinalWaterLevel - parseInt ( motorData . receiverInitialwaterlevel , 10 ) ;
const stopTime = formatDate ( new Date ( ) ) ;
const stopTime = formatDate ( new Date ( ) ) ;
await MotorData . updateOne (
await MotorData . updateOne (
{ customerId , motor _id : motorId , start _instance _id : start _instance _id } ,
{ customerId , motor _id : motorId , start _instance _id : start _instance _id } ,
{
{
$set : {
$set : {
stopTime : stopTime ,
stopTime : stopTime ,
receiverfinalwaterlevel : receiverFinalWaterLevel . toString ( ) ,
receiverfinalwaterlevel : receiverFinalWaterLevel . toString ( ) ,
quantity _delivered : quantityDelivered . toString ( )
quantity _delivered : quantityDelivered . toString ( )
}
}
@ -1598,19 +1601,21 @@ exports.motorAction = async (req, reply) => {
) ;
) ;
}
}
}
}
}
} , 20000 ) ; // Check water level every minute
} , 20000 ) ; // Check every 20 seconds
}
}
}
}
reply . code ( 200 ) . send ( { message : "Motor action processed successfully." } ) ;
// Respond with success message
} catch ( error ) {
reply . code ( 200 ) . send ( { message : ` Motor ${ action === "start" ? "started" : "stopped" } successfully. ` } ) ;
console . error ( error ) ;
} catch ( err ) {
reply . code ( 500 ) . send ( { error : error . message } ) ;
// Handle errors
throw boom . boomify ( err ) ;
}
}
} ;
} ;
// exports.motorAction = async (req, reply) => {
// exports.motorAction = async (req, reply) => {
// try {
// try {
// const customerId = req.params.customerId;
// const customerId = req.params.customerId;