|
|
|
@ -2843,19 +2843,19 @@ exports.motorAction = async (req, reply) => {
|
|
|
|
|
const { customerId } = req.params;
|
|
|
|
|
const { action, motor_id: motorId, start_instance_id, phone, threshold_type, manual_threshold_time, manual_threshold_litres } = req.body;
|
|
|
|
|
|
|
|
|
|
if (!motorId) throw new Error("Motor ID is required.");
|
|
|
|
|
// if (!motorId) throw new Error("Motor ID is required.");
|
|
|
|
|
|
|
|
|
|
const users = await User.findOne({ customerId });
|
|
|
|
|
if (!users) return reply.status(404).send({ error: "User not found" });
|
|
|
|
|
// const users = await User.findOne({ customerId });
|
|
|
|
|
// if (!users) return reply.status(404).send({ error: "User not found" });
|
|
|
|
|
|
|
|
|
|
let loggedInUser = users.phone === phone ?
|
|
|
|
|
{ role: "Customer", name: users.username, phone: users.phone } :
|
|
|
|
|
users.staff?.staff?.find(staff => staff.phone === phone) ?
|
|
|
|
|
{ role: "Staff", name: users.staff.staff.find(staff => staff.phone === phone).name, phone } : null;
|
|
|
|
|
// let loggedInUser = users.phone === phone ?
|
|
|
|
|
// { role: "Customer", name: users.username, phone: users.phone } :
|
|
|
|
|
// users.staff?.staff?.find(staff => staff.phone === phone) ?
|
|
|
|
|
// { role: "Staff", name: users.staff.staff.find(staff => staff.phone === phone).name, phone } : null;
|
|
|
|
|
|
|
|
|
|
if (!loggedInUser) return reply.status(404).send({ error: "User not found" });
|
|
|
|
|
// if (!loggedInUser) return reply.status(404).send({ error: "User not found" });
|
|
|
|
|
|
|
|
|
|
const fcmToken = users.fcmIds ? users.fcmIds.filter(id => id) : [];
|
|
|
|
|
// const fcmToken = users.fcmIds ? users.fcmIds.filter(id => id) : [];
|
|
|
|
|
const receiverTank = await Tank.findOne({ customerId, tankName: req.body.to, tankLocation: req.body.to_type.toLowerCase() });
|
|
|
|
|
if (!receiverTank) throw new Error("Receiver tank not found.");
|
|
|
|
|
|
|
|
|
@ -2892,7 +2892,7 @@ exports.motorAction = async (req, reply) => {
|
|
|
|
|
}}
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
eventEmitter.emit("motorStart", customerId, fcmToken, tankName, blockName, startTime, "Mobile APP", manual_threshold_time, typeOfWater, motorId, loggedInUser.phone);
|
|
|
|
|
// eventEmitter.emit("motorStart", customerId, fcmToken, tankName, blockName, startTime, "Mobile APP", manual_threshold_time, typeOfWater, motorId, loggedInUser.phone);
|
|
|
|
|
//this.publishMotorStopStatus(motorId, motorStopStatus);
|
|
|
|
|
reply.code(200).send({ message: "Motor started successfully." });
|
|
|
|
|
|
|
|
|
@ -2958,32 +2958,32 @@ exports.motorAction = async (req, reply) => {
|
|
|
|
|
// blockName
|
|
|
|
|
// );
|
|
|
|
|
|
|
|
|
|
const notificationKey = `${customerId}_${motorId}_threshold`;
|
|
|
|
|
|
|
|
|
|
// Check if the notification has already been sent
|
|
|
|
|
if (!notificationTracker.get(notificationKey)) {
|
|
|
|
|
console.log("Sending threshold time notification...");
|
|
|
|
|
|
|
|
|
|
eventEmitter.emit(
|
|
|
|
|
"sendThresholdTimeNotification",
|
|
|
|
|
customerId,
|
|
|
|
|
fcmToken,
|
|
|
|
|
manual_threshold_time,
|
|
|
|
|
motorId,
|
|
|
|
|
tankName,
|
|
|
|
|
blockName
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
// Mark notification as sent
|
|
|
|
|
notificationTracker.set(notificationKey, true);
|
|
|
|
|
|
|
|
|
|
// Optionally, reset the flag after some time (e.g., 24 hours)
|
|
|
|
|
setTimeout(() => {
|
|
|
|
|
notificationTracker.delete(notificationKey);
|
|
|
|
|
}, 24 * 60 * 60 * 1000); // Reset after 24 hours
|
|
|
|
|
} else {
|
|
|
|
|
console.log("Notification already sent, skipping...");
|
|
|
|
|
}
|
|
|
|
|
// const notificationKey = `${customerId}_${motorId}_threshold`;
|
|
|
|
|
|
|
|
|
|
// // Check if the notification has already been sent
|
|
|
|
|
// if (!notificationTracker.get(notificationKey)) {
|
|
|
|
|
// console.log("Sending threshold time notification...");
|
|
|
|
|
|
|
|
|
|
// eventEmitter.emit(
|
|
|
|
|
// "sendThresholdTimeNotification",
|
|
|
|
|
// customerId,
|
|
|
|
|
// fcmToken,
|
|
|
|
|
// manual_threshold_time,
|
|
|
|
|
// motorId,
|
|
|
|
|
// tankName,
|
|
|
|
|
// blockName
|
|
|
|
|
// );
|
|
|
|
|
|
|
|
|
|
// // Mark notification as sent
|
|
|
|
|
// notificationTracker.set(notificationKey, true);
|
|
|
|
|
|
|
|
|
|
// // Optionally, reset the flag after some time (e.g., 24 hours)
|
|
|
|
|
// setTimeout(() => {
|
|
|
|
|
// notificationTracker.delete(notificationKey);
|
|
|
|
|
// }, 24 * 60 * 60 * 1000); // Reset after 24 hours
|
|
|
|
|
// } else {
|
|
|
|
|
// console.log("Notification already sent, skipping...");
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
const currentTime = moment().tz('Asia/Kolkata').format('DD-MMM-YYYY - HH:mm');
|
|
|
|
|
await Tank.updateOne(
|
|
|
|
@ -3191,7 +3191,7 @@ async function stopMotor(motorId, customerId, start_instance_id) {
|
|
|
|
|
delete motorIntervals[motorId];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
eventEmitter.emit("motorStop", customerId, [], "", "", currentTime, "Mobile APP", 0, "", motorId, "");
|
|
|
|
|
//eventEmitter.emit("motorStop", customerId, [], "", "", currentTime, "Mobile APP", 0, "", motorId, "");
|
|
|
|
|
|
|
|
|
|
const motorData = await MotorData.findOne({ customerId, motor_id: motorId, start_instance_id });
|
|
|
|
|
if (motorData) {
|
|
|
|
|