Varun 1 year ago
commit 6a2b9b88e5

@ -1373,6 +1373,10 @@ exports.motorAction = async (req, reply) => {
throw new Error("Motor ID is required.");
}
const users = await User.find({ customerId: customerId });
const fcmToken = users.map(user => user.fcmId).filter(fcmId => fcmId);
console.log(fcmToken)
// Determine the motor stop status based on the action
let motorStopStatus;
if (action === "start") {
@ -1402,6 +1406,10 @@ exports.motorAction = async (req, reply) => {
// Send immediate response to the client
reply.code(200).send({ message: "Motor stopped successfully." });
if (fcmToken) {
await sendNotification(fcmToken, 'Motor Stopped', `Motor ${motorId} has been stopped.`);
}
// Perform stop operations in the background
(async () => {
await delay(300000);
@ -1429,6 +1437,12 @@ exports.motorAction = async (req, reply) => {
}
}
);
if (receiverFinalWaterLevel / parseInt(receiverTank.capacity, 10) * 100 >= 90) {
if (fcmToken) {
await sendNotification(fcmToken, 'High Water Level Alert', `Water level in tank ${motorData.receiverTank} has reached 90%.`);
}
}
}
})();
@ -1440,6 +1454,10 @@ exports.motorAction = async (req, reply) => {
{ customerId, "connections.inputConnections.motor_id": motorId },
{ $set: { "connections.inputConnections.$.motor_stop_status": "2" } }
);
// Send start notification
if (fcmToken) {
await sendNotification(fcmToken, 'Motor Started', `Motor ${motorId} has been started.`);
}
}
// Check threshold settings if action is start
@ -1501,6 +1519,10 @@ exports.motorAction = async (req, reply) => {
}
}
);
// Send notification after motor stops due to time threshold
if (fcmToken) {
await sendNotification(fcmToken, 'Motor Stopped After Time Threshold', `Motor ${motorId} has been stopped after reaching the set time threshold.`);
}
clearInterval(intervalId);
await delay(300000);
@ -1526,6 +1548,11 @@ exports.motorAction = async (req, reply) => {
}
}
);
if (receiverFinalWaterLevel / parseInt(receiverTank.capacity, 10) * 100 <= 20) {
if (fcmToken) {
await sendNotification(fcmToken, 'Low Water Level Alert', `Water level in tank ${motorData.receiverTank} has dropped below 20%.`);
}
}
}
}
}, 60000);
@ -1623,6 +1650,12 @@ exports.motorAction = async (req, reply) => {
}
}
);
// Send low water level notification
if (receiverFinalWaterLevel / parseInt(receiverTank.capacity, 10) * 100 <= 20) {
if (fcmToken) {
await sendNotification(fcmToken, 'Low Water Level Alert', `Water level in tank ${motorData.receiverTank} has dropped below 20%.`);
}
}
}
}
}, 20000); // Check water level every minute

Loading…
Cancel
Save